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