aw_apps_package_names_allowlist_component_loader_policy.h 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. // Copyright 2021 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #ifndef ANDROID_WEBVIEW_BROWSER_COMPONENT_UPDATER_LOADER_POLICIES_AW_APPS_PACKAGE_NAMES_ALLOWLIST_COMPONENT_LOADER_POLICY_H_
  5. #define ANDROID_WEBVIEW_BROWSER_COMPONENT_UPDATER_LOADER_POLICIES_AW_APPS_PACKAGE_NAMES_ALLOWLIST_COMPONENT_LOADER_POLICY_H_
  6. #include <stdint.h>
  7. #include <memory>
  8. #include <string>
  9. #include <vector>
  10. #include "android_webview/common/metrics/app_package_name_logging_rule.h"
  11. #include "base/callback.h"
  12. #include "base/containers/flat_map.h"
  13. #include "base/files/scoped_file.h"
  14. #include "components/component_updater/android/component_loader_policy.h"
  15. #include "third_party/abseil-cpp/absl/types/optional.h"
  16. namespace base {
  17. class DictionaryValue;
  18. class Time;
  19. class Version;
  20. } // namespace base
  21. namespace android_webview {
  22. class AwMetricsServiceClient;
  23. // All these constants have to be kept in sync with the allowlist generation
  24. // server, see http://go/aw-package-names-allowlist-bloomfilter.
  25. constexpr char kAllowlistBloomFilterFileName[] = "allowlistbloomfilter";
  26. constexpr char kBloomFilterNumHashKey[] = "bloomfilter_num_hash";
  27. constexpr char kBloomFilterNumBitsKey[] = "bloomfilter_num_bits";
  28. constexpr char kExpiryDateKey[] = "expiry_date";
  29. // Minimum time to throttle querying the app package names allowlist from the
  30. // component updater service, used when there is no valid cached allowlist
  31. // result. Exposed for testing only.
  32. extern const base::TimeDelta kWebViewAppsMinAllowlistThrottleTimeDelta;
  33. // Maximum time to throttle querying the app package names allowlist from the
  34. // component updater service, used when there is a valid cached allowlist
  35. // result. Exposed for testing only.
  36. extern const base::TimeDelta kWebViewAppsMaxAllowlistThrottleTimeDelta;
  37. // A callback for the result of loading and looking up the allowlist. If the
  38. // allowlist loading fails, it will be called with a null record.
  39. using AllowListLookupCallback =
  40. base::OnceCallback<void(absl::optional<AppPackageNameLoggingRule>)>;
  41. // Defines a loader responsible for receiving the allowlist for apps package
  42. // names that can be included in UMA records and lookup the embedding app's name
  43. // in that list.
  44. class AwAppsPackageNamesAllowlistComponentLoaderPolicy
  45. : public component_updater::ComponentLoaderPolicy {
  46. public:
  47. // These values are persisted to logs. Entries should not be renumbered and
  48. // numeric values should never be reused.
  49. enum class AllowlistPraseStatus {
  50. kSuccess = 0,
  51. kUsingCache = 1,
  52. kMissingFields = 2,
  53. kExpiredAllowlist = 3,
  54. kMissingAllowlistFile = 4,
  55. kIOError = 5,
  56. kMalformedBloomFilter = 6,
  57. kMaxValue = kMalformedBloomFilter,
  58. };
  59. // `app_package_name` the embedding app package name.
  60. // `cached_record` the cached lookup result of a previous successfully
  61. // loaded allowlist, if any.
  62. // `lookup_callback` callback to report the result of looking up
  63. // `app_package_name` in the packages names allowlist.
  64. AwAppsPackageNamesAllowlistComponentLoaderPolicy(
  65. std::string app_package_name,
  66. absl::optional<AppPackageNameLoggingRule> cached_record,
  67. AllowListLookupCallback lookup_callback);
  68. ~AwAppsPackageNamesAllowlistComponentLoaderPolicy() override;
  69. AwAppsPackageNamesAllowlistComponentLoaderPolicy(
  70. const AwAppsPackageNamesAllowlistComponentLoaderPolicy&) = delete;
  71. AwAppsPackageNamesAllowlistComponentLoaderPolicy& operator=(
  72. const AwAppsPackageNamesAllowlistComponentLoaderPolicy&) = delete;
  73. // The following methods override ComponentLoaderPolicy.
  74. void ComponentLoaded(
  75. const base::Version& version,
  76. base::flat_map<std::string, base::ScopedFD>& fd_map,
  77. std::unique_ptr<base::DictionaryValue> manifest) override;
  78. void ComponentLoadFailed(
  79. component_updater::ComponentLoadResult error) override;
  80. void GetHash(std::vector<uint8_t>* hash) const override;
  81. std::string GetMetricsSuffix() const override;
  82. private:
  83. std::string app_package_name_;
  84. absl::optional<AppPackageNameLoggingRule> cached_record_;
  85. AllowListLookupCallback lookup_callback_;
  86. };
  87. void LoadPackageNamesAllowlistComponent(
  88. component_updater::ComponentLoaderPolicyVector& policies,
  89. AwMetricsServiceClient* metrics_service_client);
  90. } // namespace android_webview
  91. #endif // ANDROID_WEBVIEW_BROWSER_COMPONENT_UPDATER_LOADER_POLICIES_AW_APPS_PACKAGE_NAMES_ALLOWLIST_COMPONENT_LOADER_POLICY_H_