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