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