metrics_service_client.h 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. // Copyright 2014 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 COMPONENTS_METRICS_METRICS_SERVICE_CLIENT_H_
  5. #define COMPONENTS_METRICS_METRICS_SERVICE_CLIENT_H_
  6. #include <stdint.h>
  7. #include <memory>
  8. #include <string>
  9. #include "base/callback.h"
  10. #include "base/time/time.h"
  11. #include "components/metrics/metrics_log_store.h"
  12. #include "components/metrics/metrics_log_uploader.h"
  13. #include "components/metrics/metrics_reporting_default_state.h"
  14. #include "third_party/metrics_proto/system_profile.pb.h"
  15. #include "url/gurl.h"
  16. namespace ukm {
  17. class UkmService;
  18. }
  19. namespace network_time {
  20. class NetworkTimeTracker;
  21. }
  22. namespace variations {
  23. class SyntheticTrialRegistry;
  24. }
  25. namespace metrics {
  26. class MetricsLogUploader;
  27. class MetricsService;
  28. // An abstraction of operations that depend on the embedder's (e.g. Chrome)
  29. // environment.
  30. class MetricsServiceClient {
  31. public:
  32. MetricsServiceClient();
  33. MetricsServiceClient(const MetricsServiceClient&) = delete;
  34. MetricsServiceClient& operator=(const MetricsServiceClient&) = delete;
  35. virtual ~MetricsServiceClient();
  36. // Returns the synthetic trial registry shared by MetricsService and
  37. // UkmService.
  38. virtual variations::SyntheticTrialRegistry* GetSyntheticTrialRegistry() = 0;
  39. // Returns the MetricsService instance that this client is associated with.
  40. // With the exception of testing contexts, the returned instance must be valid
  41. // for the lifetime of this object (typically, the embedder's client
  42. // implementation will own the MetricsService instance being returned).
  43. virtual MetricsService* GetMetricsService() = 0;
  44. // Returns the UkmService instance that this client is associated with.
  45. virtual ukm::UkmService* GetUkmService();
  46. // Returns true if metrics should be uploaded for the given |user_id|, which
  47. // corresponds to the |user_id| field in ChromeUserMetricsExtension.
  48. virtual bool ShouldUploadMetricsForUserId(uint64_t user_id);
  49. // Registers the client id with other services (e.g. crash reporting), called
  50. // when metrics recording gets enabled.
  51. virtual void SetMetricsClientId(const std::string& client_id) = 0;
  52. // Returns the product value to use in uploaded reports, which will be used to
  53. // set the ChromeUserMetricsExtension.product field. See comments on that
  54. // field on why it's an int32_t rather than an enum.
  55. virtual int32_t GetProduct() = 0;
  56. // Returns the current application locale (e.g. "en-US").
  57. virtual std::string GetApplicationLocale() = 0;
  58. // Return a NetworkTimeTracker for access to a server-provided clock.
  59. virtual const network_time::NetworkTimeTracker* GetNetworkTimeTracker() = 0;
  60. // Retrieves the brand code string associated with the install, returning
  61. // false if no brand code is available.
  62. virtual bool GetBrand(std::string* brand_code) = 0;
  63. // Returns the release channel (e.g. stable, beta, etc) of the application.
  64. virtual SystemProfileProto::Channel GetChannel() = 0;
  65. // Returns true if the application is on the extended stable channel.
  66. virtual bool IsExtendedStableChannel() = 0;
  67. // Returns the version of the application as a string.
  68. virtual std::string GetVersionString() = 0;
  69. // Called by the metrics service when a new environment has been recorded.
  70. // Takes the serialized environment as a parameter. The contents of
  71. // |serialized_environment| are consumed by the call, but the caller maintains
  72. // ownership.
  73. virtual void OnEnvironmentUpdate(std::string* serialized_environment) {}
  74. // Called prior to a metrics log being closed, allowing the client to collect
  75. // extra histograms that will go in that log. Asynchronous API - the client
  76. // implementation should call |done_callback| when complete.
  77. virtual void CollectFinalMetricsForLog(base::OnceClosure done_callback) = 0;
  78. // Get the URL of the metrics server.
  79. virtual GURL GetMetricsServerUrl();
  80. // Get the fallback HTTP URL of the metrics server.
  81. virtual GURL GetInsecureMetricsServerUrl();
  82. // Creates a MetricsLogUploader with the specified parameters (see comments on
  83. // MetricsLogUploader for details).
  84. virtual std::unique_ptr<MetricsLogUploader> CreateUploader(
  85. const GURL& server_url,
  86. const GURL& insecure_server_url,
  87. base::StringPiece mime_type,
  88. metrics::MetricsLogUploader::MetricServiceType service_type,
  89. const MetricsLogUploader::UploadCallback& on_upload_complete) = 0;
  90. // Returns the interval between upload attempts. Checks if debugging flags
  91. // have been set, otherwise defaults to GetStandardUploadInterval().
  92. base::TimeDelta GetUploadInterval();
  93. // Returns the standard interval between upload attempts.
  94. virtual base::TimeDelta GetStandardUploadInterval() = 0;
  95. // Whether or not the MetricsService should start up quickly and upload the
  96. // initial report quickly. By default, this work may be delayed by some
  97. // amount. Only the default behavior should be used in production, but clients
  98. // can override this in tests if tests need to make assertions on the log
  99. // data.
  100. virtual bool ShouldStartUpFastForTesting() const;
  101. // Called when loading state changed, e.g. start/stop loading.
  102. virtual void LoadingStateChanged(bool is_loading) {}
  103. // Called on renderer crashes in some embedders (e.g., those that do not use
  104. // //content and thus do not have //content's notification system available
  105. // as a mechanism for observing renderer crashes).
  106. virtual void OnRendererProcessCrash() {}
  107. // Returns whether metrics reporting is managed by policy.
  108. virtual bool IsReportingPolicyManaged();
  109. // Gets information about the default value for the metrics reporting checkbox
  110. // shown during first-run.
  111. virtual EnableMetricsDefault GetMetricsReportingDefaultState();
  112. // Returns whether cellular logic is enabled for metrics reporting.
  113. virtual bool IsUMACellularUploadLogicEnabled();
  114. // Returns whether the allowlist for external experiment ids is enabled. Some
  115. // embedders like WebLayer disable it. For Chrome, it should be enabled.
  116. virtual bool IsExternalExperimentAllowlistEnabled();
  117. // Returns true iff UKM is allowed for all profiles.
  118. // See //components/ukm/observers/ukm_consent_state_observer.h for details.
  119. virtual bool IsUkmAllowedForAllProfiles();
  120. // Returns true iff UKM is allowed to capture extensions for all profiles.
  121. // See //components/ukm/observers/ukm_consent_state_observer.h for details.
  122. virtual bool IsUkmAllowedWithExtensionsForAllProfiles();
  123. // Returns whether UKM notification listeners were attached to all profiles.
  124. virtual bool AreNotificationListenersEnabledOnAllProfiles();
  125. // Gets the app package name (as defined by the embedder). Since package name
  126. // is only meaningful for Android, other platforms should return the empty
  127. // string (this is the same as the default behavior). If the package name
  128. // should not be logged for privacy/fingerprintability reasons, the embedder
  129. // should return the empty string.
  130. virtual std::string GetAppPackageNameIfLoggable();
  131. // Gets the key used to sign metrics uploads. This will be used to compute an
  132. // HMAC-SHA256 signature of an uploaded log.
  133. virtual std::string GetUploadSigningKey();
  134. // Checks if the cloned install detector says that client ids should be reset.
  135. virtual bool ShouldResetClientIdsOnClonedInstall();
  136. // Specifies local log storage requirements and restrictions.
  137. virtual MetricsLogStore::StorageLimits GetStorageLimits() const;
  138. // Sets the callback to run MetricsServiceManager::UpdateRunningServices.
  139. void SetUpdateRunningServicesCallback(const base::RepeatingClosure& callback);
  140. // Notify MetricsServiceManager to UpdateRunningServices using callback.
  141. void UpdateRunningServices();
  142. // Checks if the user has forced metrics collection on via the override flag.
  143. bool IsMetricsReportingForceEnabled() const;
  144. // Initializes per-user metrics collection. For more details what per-user
  145. // metrics collection is, refer to MetricsService::InitPerUserMetrics.
  146. //
  147. // Since the concept of a user is only applicable in Ash Chrome, this function
  148. // should no-op for other platforms.
  149. virtual void InitPerUserMetrics() {}
  150. // Updates the current user's metrics consent. This allows embedders to update
  151. // the user consent. If there is no current user, then this function will
  152. // no-op.
  153. //
  154. // Since the concept of a user is only applicable on Ash Chrome, this function
  155. // should no-op for other platforms.
  156. virtual void UpdateCurrentUserMetricsConsent(bool user_metrics_consent) {}
  157. // Returns the current user metrics consent if it should be applied to decide
  158. // the current metrics reporting state. This allows embedders to determine
  159. // when a user metric consent state should not be applied (ie no logged in
  160. // user or managed policy).
  161. //
  162. // Will return absl::nullopt if there is no current user or current user
  163. // metrics consent should not be applied to determine metrics reporting state.
  164. //
  165. // Not all platforms support per-user consent. If per-user consent is not
  166. // supported, this function should return absl::nullopt.
  167. virtual absl::optional<bool> GetCurrentUserMetricsConsent() const;
  168. // Returns the current user id.
  169. //
  170. // Will return absl::nullopt if there is no current user, metrics reporting is
  171. // disabled, or current user should not have a user id.
  172. //
  173. // Not all platforms support per-user consent. If per-user consent is not
  174. // supported, this function should return absl::nullopt.
  175. virtual absl::optional<std::string> GetCurrentUserId() const;
  176. private:
  177. base::RepeatingClosure update_running_services_;
  178. };
  179. } // namespace metrics
  180. #endif // COMPONENTS_METRICS_METRICS_SERVICE_CLIENT_H_