1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310
|
// Copyright 2020 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef COMPONENTS_EMBEDDER_SUPPORT_ANDROID_METRICS_ANDROID_METRICS_SERVICE_CLIENT_H_
#define COMPONENTS_EMBEDDER_SUPPORT_ANDROID_METRICS_ANDROID_METRICS_SERVICE_CLIENT_H_
#include <memory>
#include <string>
#include <string_view>
#include "base/functional/callback_forward.h"
#include "base/memory/raw_ptr.h"
#include "base/memory/scoped_refptr.h"
#include "base/memory/weak_ptr.h"
#include "base/metrics/field_trial.h"
#include "base/scoped_multi_source_observation.h"
#include "base/scoped_observation.h"
#include "base/sequence_checker.h"
#include "base/time/time.h"
#include "components/metrics/enabled_state_provider.h"
#include "components/metrics/metrics_log_uploader.h"
#include "components/metrics/metrics_service_client.h"
#include "components/metrics/persistent_synthetic_trial_observer.h"
#include "components/variations/synthetic_trial_registry.h"
#include "components/version_info/android/channel_getter.h"
#include "content/public/browser/render_process_host_creation_observer.h"
#include "content/public/browser/render_process_host_observer.h"
#include "content/public/browser/web_contents.h"
class PrefRegistrySimple;
class PrefService;
namespace network {
class SharedURLLoaderFactory;
}
namespace metrics {
class MetricsStateManager;
extern const char kCrashpadHistogramAllocatorName[];
// AndroidMetricsServiceClient is a singleton which manages metrics collection
// intended for use by WebView & WebLayer.
//
// Metrics should be enabled iff all these conditions are met:
// - The user has not opted out.
// - The app has not opted out.
// - This client is in the 10% sample (controlled by client ID hash).
// The first two are recorded in |user_consent_| and |app_consent_|, which are
// set by SetHaveMetricsConsent(). The last is recorded in |is_in_sample_|.
//
// Metrics are pseudonymously identified by a randomly-generated "client ID".
// AndroidMetricsServiceClient stores this in prefs, written to the app's data
// directory. There's a different such directory for each user, for each app,
// on each device. So the ID should be unique per (device, app, user) tuple.
//
// In order to be transparent about not associating an ID with an opted out user
// or app, the client ID should only be created and retained when neither the
// user nor the app have opted out. Otherwise, the presence of the ID could give
// the impression that metrics were being collected.
//
// AndroidMetricsServiceClient metrics set up happens like so:
//
// startup
// │
// ├────────────┐
// │ ▼
// │ query for consent
// ▼ │
// Initialize() │
// │ ▼
// │ SetHaveMetricsConsent()
// │ │
// │ ┌──────────┘
// ▼ ▼
// MaybeStartMetrics()
// │
// ▼
// MetricsService::Start()
//
// All the named functions in this diagram happen on the UI thread. Querying GMS
// happens in the background, and the result is posted back to the UI thread, to
// SetHaveMetricsConsent(). Querying GMS is slow, so SetHaveMetricsConsent()
// typically happens after Initialize(), but it may happen before.
//
// Each path sets a flag, |init_finished_| or |set_consent_finished_|, to show
// that path has finished, and then calls MaybeStartMetrics(). When
// MaybeStartMetrics() is called the first time, it sees only one flag is true,
// and does nothing. When MaybeStartMetrics() is called the second time, it
// decides whether to start metrics.
//
// If consent was granted, MaybeStartMetrics() determines sampling by hashing
// the client ID (generating a new ID if there was none). If this client is in
// the sample, it then calls MetricsService::Start(). If consent was not
// granted, MaybeStartMetrics() instead clears the client ID, if any.
//
// To match chrome on other platforms (including android), the MetricsService is
// always created.
class AndroidMetricsServiceClient
: public MetricsServiceClient,
public EnabledStateProvider,
public content::RenderProcessHostCreationObserver,
public content::RenderProcessHostObserver {
public:
AndroidMetricsServiceClient();
~AndroidMetricsServiceClient() override;
AndroidMetricsServiceClient(const AndroidMetricsServiceClient&) = delete;
AndroidMetricsServiceClient& operator=(const AndroidMetricsServiceClient&) =
delete;
static void RegisterPrefs(PrefRegistrySimple* registry);
// Initializes, but does not necessarily start, the MetricsService. See the
// documentation at the top of the file for more details.
void Initialize(PrefService* pref_service);
void SetHaveMetricsConsent(bool user_consent, bool app_consent);
void SetFastStartupForTesting(bool fast_startup_for_testing);
void SetUploadIntervalForTesting(const base::TimeDelta& upload_interval);
// Whether or not consent state has been determined, regardless of whether
// it is positive or negative.
bool IsConsentDetermined() const;
// EnabledStateProvider:
bool IsConsentGiven() const override;
bool IsReportingEnabled() const override;
// Returns the MetricService only if it has been started (which means consent
// was given).
MetricsService* GetMetricsServiceIfStarted();
// MetricsServiceClient:
variations::SyntheticTrialRegistry* GetSyntheticTrialRegistry() override;
MetricsService* GetMetricsService() override;
void SetMetricsClientId(const std::string& client_id) override;
std::string GetApplicationLocale() override;
const network_time::NetworkTimeTracker* GetNetworkTimeTracker() override;
bool GetBrand(std::string* brand_code) override;
SystemProfileProto::Channel GetChannel() override;
bool IsExtendedStableChannel() override;
std::string GetVersionString() override;
void MergeSubprocessHistograms() override;
void CollectFinalMetricsForLog(
const base::OnceClosure done_callback) override;
std::unique_ptr<MetricsLogUploader> CreateUploader(
const GURL& server_url,
const GURL& insecure_server_url,
std::string_view mime_type,
MetricsLogUploader::MetricServiceType service_type,
const MetricsLogUploader::UploadCallback& on_upload_complete) override;
base::TimeDelta GetStandardUploadInterval() override;
bool ShouldStartUpFast() const override;
// Gets the embedding app's package name if it's OK to log. Otherwise, this
// returns the empty string.
std::string GetAppPackageNameIfLoggable() override;
void OnWebContentsCreated(content::WebContents* web_contents);
// content::RenderProcessHostCreationObserver:
void OnRenderProcessHostCreated(content::RenderProcessHost* host) override;
// RenderProcessHostObserver:
void RenderProcessExited(
content::RenderProcessHost* host,
const content::ChildProcessTerminationInfo& info) override;
// Runs |closure| when CollectFinalMetricsForLog() is called, when we begin
// collecting final metrics.
void SetCollectFinalMetricsForLogClosureForTesting(base::OnceClosure closure);
// Runs |listener| after all final metrics have been collected.
void SetOnFinalMetricsCollectedListenerForTesting(
base::RepeatingClosure listener);
metrics::MetricsStateManager* metrics_state_manager() const {
return metrics_state_manager_.get();
}
// GENERATED_JAVA_ENUM_PACKAGE: org.chromium.components.metrics
enum class InstallerPackageType {
// App has been initially preinstalled in the system image.
SYSTEM_APP,
// App has been installed/updated by Google Play Store. Doesn't apply for
// apps whose most recent updates are sideloaded, even if the app was
// installed via Google Play Store.
GOOGLE_PLAY_STORE,
// App has been Sideloaded or installed/updated through a 3rd party app
// store.
OTHER,
};
// Returns the embedding application's package name (unconditionally). The
// value returned by this method shouldn't be logged/stored anywhere, callers
// should use `GetAppPackageNameIfLoggable`.
std::string GetAppPackageName();
// Returns the installer type of the app.
virtual InstallerPackageType GetInstallerPackageType();
protected:
// Called by MaybeStartMetrics() to allow embedder specific initialization.
virtual void OnMetricsStart() = 0;
// Called by MaybeStartMetrics() when metrics collection failed to start.
virtual void OnMetricsNotStarted() = 0;
// Returns the metrics sampling rate, to be used by IsInSample(). This is a
// per mille value, so this integer must always be in the inclusive range [0,
// 1000]. A value of 0 will always be out-of-sample, and a value of 1000 is
// always in-sample.
virtual int GetSampleRatePerMille() const = 0;
// Returns a value in the inclusive range [0, 999], to be compared against a
// per mille sample rate. This value will be based on a persisted value, so it
// should be consistent across restarts. This value should also be mostly
// consistent across upgrades, to avoid significantly impacting IsInSample()
// and ShouldRecordPackageName(). Virtual for testing.
virtual int GetSampleBucketValue() const;
// Determines if the client is within the random sample of clients for which
// we log metrics. If this returns false, MetricsServiceClient should
// indicate reporting is disabled. Sampling is due to storage/bandwidth
// considerations.
virtual bool IsInSample() const;
// Determines if the embedder app is the type of app for which we may log the
// package name. If this returns false, GetAppPackageNameIfLoggable() must
// return empty string. Virtual for testing.
virtual bool CanRecordPackageNameForAppType();
// Determines if this client falls within the group for which the embedding
// app's package name may be included. If this returns false,
// GetAppPackageNameIfLoggable() must return the empty string.
virtual bool ShouldRecordPackageName();
// Caps the rate at which we include package names in UMA logs, expressed as a
// per mille value. See GetSampleRatePerMille() for a description of how per
// mille values are handled.
virtual int GetPackageNameLimitRatePerMille() = 0;
// Called by CreateMetricsService, allows the embedder to register additional
// MetricsProviders. Does nothing by default.
virtual void RegisterAdditionalMetricsProviders(MetricsService* service);
// Returns whether there are any OffTheRecord browsers/tabs open.
virtual bool IsOffTheRecordSessionActive();
// Returns a URLLoaderFactory when the system uploader isn't used.
virtual scoped_refptr<network::SharedURLLoaderFactory> GetURLLoaderFactory();
void EnsureOnValidSequence() const {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
}
PrefService* pref_service() const { return pref_service_; }
private:
void MaybeStartMetrics();
void RegisterForNotifications();
void RegisterMetricsProvidersAndInitState();
void OnApplicationNotIdle();
void OnDidStartLoading();
std::unique_ptr<MetricsStateManager> metrics_state_manager_;
std::unique_ptr<variations::SyntheticTrialRegistry> synthetic_trial_registry_;
// Metrics service observer for synthetic trials.
metrics::PersistentSyntheticTrialObserver synthetic_trial_observer_;
base::ScopedObservation<variations::SyntheticTrialRegistry,
variations::SyntheticTrialObserver>
synthetic_trial_observation_{&synthetic_trial_observer_};
std::unique_ptr<MetricsService> metrics_service_;
base::ScopedMultiSourceObservation<content::RenderProcessHost,
content::RenderProcessHostObserver>
host_observation_{this};
raw_ptr<PrefService> pref_service_ = nullptr;
bool init_finished_ = false;
bool set_consent_finished_ = false;
bool user_consent_ = false;
bool app_consent_ = false;
bool is_client_id_forced_ = false;
bool fast_startup_for_testing_ = false;
bool did_start_metrics_ = false;
// When non-zero, this overrides the default value in
// GetStandardUploadInterval().
base::TimeDelta overridden_upload_interval_;
base::OnceClosure collect_final_metrics_for_log_closure_;
base::RepeatingClosure on_final_metrics_collected_listener_;
#if DCHECK_IS_ON()
bool did_start_metrics_with_consent_ = false;
#endif
// MetricsServiceClient may be created before the UI thread is promoted to
// BrowserThread::UI. Use |sequence_checker_| to enforce that the
// MetricsServiceClient is used on a single thread.
SEQUENCE_CHECKER(sequence_checker_);
base::WeakPtrFactory<AndroidMetricsServiceClient> weak_ptr_factory_{this};
};
} // namespace metrics
#endif // COMPONENTS_EMBEDDER_SUPPORT_ANDROID_METRICS_ANDROID_METRICS_SERVICE_CLIENT_H_
|