metrics_service.h 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491
  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. // This file defines a service that collects information about the user
  5. // experience in order to help improve future versions of the app.
  6. #ifndef COMPONENTS_METRICS_METRICS_SERVICE_H_
  7. #define COMPONENTS_METRICS_METRICS_SERVICE_H_
  8. #include <stdint.h>
  9. #include <map>
  10. #include <memory>
  11. #include <string>
  12. #include "base/bind.h"
  13. #include "base/callback_forward.h"
  14. #include "base/callback_list.h"
  15. #include "base/gtest_prod_util.h"
  16. #include "base/memory/raw_ptr.h"
  17. #include "base/memory/weak_ptr.h"
  18. #include "base/metrics/field_trial.h"
  19. #include "base/metrics/histogram_flattener.h"
  20. #include "base/metrics/histogram_snapshot_manager.h"
  21. #include "base/metrics/user_metrics.h"
  22. #include "base/observer_list.h"
  23. #include "base/scoped_observation.h"
  24. #include "base/sequence_checker.h"
  25. #include "base/time/time.h"
  26. #include "build/build_config.h"
  27. #include "build/chromeos_buildflags.h"
  28. #include "components/metrics/delegating_provider.h"
  29. #include "components/metrics/metrics_log.h"
  30. #include "components/metrics/metrics_log_manager.h"
  31. #include "components/metrics/metrics_log_store.h"
  32. #include "components/metrics/metrics_provider.h"
  33. #include "components/metrics/metrics_reporting_service.h"
  34. class PrefService;
  35. class PrefRegistrySimple;
  36. FORWARD_DECLARE_TEST(ChromeMetricsServiceClientTest,
  37. TestRegisterMetricsServiceProviders);
  38. FORWARD_DECLARE_TEST(IOSChromeMetricsServiceClientTest,
  39. TestRegisterMetricsServiceProviders);
  40. namespace base {
  41. class HistogramSamples;
  42. class PrefService;
  43. } // namespace base
  44. namespace variations {
  45. class SyntheticTrialRegistry;
  46. }
  47. namespace metrics {
  48. class MetricsRotationScheduler;
  49. class MetricsServiceClient;
  50. class MetricsStateManager;
  51. // Exposed in the header file for tests.
  52. extern const base::Feature kConsolidateMetricsServiceInitialLogLogic;
  53. // See metrics_service.cc for a detailed description.
  54. class MetricsService : public base::HistogramFlattener {
  55. public:
  56. // Creates the MetricsService with the given |state_manager|, |client|, and
  57. // |local_state|. Does not take ownership of the paramaters; instead stores
  58. // a weak pointer to each. Caller should ensure that the parameters are valid
  59. // for the lifetime of this class.
  60. MetricsService(MetricsStateManager* state_manager,
  61. MetricsServiceClient* client,
  62. PrefService* local_state);
  63. MetricsService(const MetricsService&) = delete;
  64. MetricsService& operator=(const MetricsService&) = delete;
  65. ~MetricsService() override;
  66. // Initializes metrics recording state. Updates various bookkeeping values in
  67. // prefs and sets up the scheduler. This is a separate function rather than
  68. // being done by the constructor so that field trials could be created before
  69. // this is run.
  70. void InitializeMetricsRecordingState();
  71. // Starts the metrics system, turning on recording and uploading of metrics.
  72. // Should be called when starting up with metrics enabled, or when metrics
  73. // are turned on.
  74. void Start();
  75. // Starts the metrics system in a special test-only mode. Metrics won't ever
  76. // be uploaded or persisted in this mode, but metrics will be recorded in
  77. // memory.
  78. void StartRecordingForTests();
  79. // Starts updating the "last live" browser timestamp.
  80. void StartUpdatingLastLiveTimestamp();
  81. // Shuts down the metrics system. Should be called at shutdown, or if metrics
  82. // are turned off.
  83. void Stop();
  84. // Enable/disable transmission of accumulated logs and crash reports (dumps).
  85. // Calling Start() automatically enables reporting, but sending is
  86. // asyncronous so this can be called immediately after Start() to prevent
  87. // any uploading.
  88. void EnableReporting();
  89. void DisableReporting();
  90. // Returns the client ID for this client, or the empty string if metrics
  91. // recording is not currently running.
  92. std::string GetClientId() const;
  93. // Set an external provided id for the metrics service. This method can be
  94. // set by a caller which wants to explicitly control the *next* id used by the
  95. // metrics service. Note that setting the external client id will *not* change
  96. // the current metrics client id. In order to change the current client id,
  97. // callers should call ResetClientId to change the current client id to the
  98. // provided id.
  99. void SetExternalClientId(const std::string& id);
  100. // Returns the date at which the current metrics client ID was created as
  101. // an int64_t containing seconds since the epoch.
  102. int64_t GetMetricsReportingEnabledDate();
  103. // Returns true if the last session exited cleanly.
  104. bool WasLastShutdownClean() const;
  105. // Registers local state prefs used by this class.
  106. static void RegisterPrefs(PrefRegistrySimple* registry);
  107. // HistogramFlattener:
  108. void RecordDelta(const base::HistogramBase& histogram,
  109. const base::HistogramSamples& snapshot) override;
  110. // This should be called when the application is not idle, i.e. the user seems
  111. // to be interacting with the application.
  112. void OnApplicationNotIdle();
  113. #if BUILDFLAG(IS_ANDROID) || BUILDFLAG(IS_IOS)
  114. // Called when the application is going into background mode.
  115. // If |keep_recording_in_background| is true, UMA is still recorded and
  116. // reported while in the background.
  117. void OnAppEnterBackground(bool keep_recording_in_background = false);
  118. // Called when the application is coming out of background mode.
  119. void OnAppEnterForeground(bool force_open_new_log = false);
  120. #endif // BUILDFLAG(IS_ANDROID) || BUILDFLAG(IS_IOS)
  121. // Signals that the browser is shutting down cleanly. Intended to be called
  122. // during shutdown after critical shutdown tasks have completed.
  123. void LogCleanShutdown();
  124. bool recording_active() const;
  125. bool reporting_active() const;
  126. bool has_unsent_logs() const;
  127. bool IsMetricsReportingEnabled() const;
  128. // Register the specified |provider| to provide additional metrics into the
  129. // UMA log. Should be called during MetricsService initialization only.
  130. void RegisterMetricsProvider(std::unique_ptr<MetricsProvider> provider);
  131. // Check if this install was cloned or imaged from another machine. If a
  132. // clone is detected, reset the client id and low entropy source. This
  133. // should not be called more than once.
  134. void CheckForClonedInstall();
  135. // Checks if the cloned install detector says that client ids should be reset.
  136. bool ShouldResetClientIdsOnClonedInstall();
  137. // Clears the stability metrics that are saved in local state.
  138. void ClearSavedStabilityMetrics();
  139. // Marks current histograms as reported by snapshotting them, without
  140. // actually saving the deltas. At a higher level, this is used to throw
  141. // away new histogram samples (since the last log) so that they will not
  142. // be included in the next log.
  143. void MarkCurrentHistogramsAsReported();
  144. #if BUILDFLAG(IS_CHROMEOS_ASH)
  145. // Binds a user log store to store unsent logs. This log store will be
  146. // fully managed by MetricsLogStore. This will no-op if another log store has
  147. // already been set.
  148. //
  149. // If this is called before initial logs are recorded, then histograms
  150. // recorded before user log store is set will be included with user histograms
  151. // when initial logs are recorded.
  152. //
  153. // If this is called after initial logs are recorded, then this will flush all
  154. // logs recorded before swapping to |user_log_store|.
  155. void SetUserLogStore(std::unique_ptr<UnsentLogStore> user_log_store);
  156. // Unbinds the user log store. If there was no user log store, then this does
  157. // nothing.
  158. //
  159. // If this is called before initial logs are recorded, then histograms and the
  160. // current log will be discarded.
  161. //
  162. // If called after initial logs are recorded, then this will flush all logs
  163. // before the user log store is unset.
  164. void UnsetUserLogStore();
  165. // Returns true if a user log store has been bound.
  166. bool HasUserLogStore();
  167. // Initializes per-user metrics collection. Logs recorded during a user
  168. // session will be stored within each user's directory and consent to send
  169. // these logs will be controlled by each user. Logs recorded before any user
  170. // logs in or during guest sessions (given device owner has consented) will be
  171. // stored in local_state.
  172. //
  173. // This is in its own function because the MetricsService is created very
  174. // early on and a user metrics service may have dependencies on services that
  175. // are created happen after MetricsService is initialized.
  176. void InitPerUserMetrics();
  177. // Returns the current user metrics consent if it should be applied to
  178. // determine metrics reporting state.
  179. //
  180. // See comments at MetricsServiceClient::GetCurrentUserMetricsConsent() for
  181. // more details.
  182. absl::optional<bool> GetCurrentUserMetricsConsent() const;
  183. // Returns the current logged in user id. See comments at
  184. // MetricsServiceClient::GetCurrentUserId() for more details.
  185. absl::optional<std::string> GetCurrentUserId() const;
  186. // Updates the current user metrics consent. No-ops if no user has logged in.
  187. void UpdateCurrentUserMetricsConsent(bool user_metrics_consent);
  188. #endif // BUILDFLAG(IS_CHROMEOS_ASH)
  189. #if BUILDFLAG(IS_CHROMEOS)
  190. // Forces the client ID to be reset and generates a new client ID. This will
  191. // be called when a user re-consents to metrics collection and the user had
  192. // consented in the past.
  193. //
  194. // This is to preserve the pseudo-anonymous identifier <client_id, user_id>.
  195. void ResetClientId();
  196. #endif // BUILDFLAG(IS_CHROMEOS)
  197. variations::SyntheticTrialRegistry* GetSyntheticTrialRegistry();
  198. MetricsLogStore* LogStoreForTest() {
  199. return reporting_service_.metrics_log_store();
  200. }
  201. // Test hook to safely stage the current log in the log store.
  202. bool StageCurrentLogForTest();
  203. DelegatingProvider* GetDelegatingProviderForTesting() {
  204. return &delegating_provider_;
  205. }
  206. // Observers will be notified when the enablement state changes. The callback
  207. // should accept one boolean argument, which will signal whether or not the
  208. // metrics collection has been enabled.
  209. base::CallbackListSubscription AddEnablementObserver(
  210. const base::RepeatingCallback<void(bool)>& observer);
  211. #if BUILDFLAG(IS_ANDROID) || BUILDFLAG(IS_IOS)
  212. bool IsInForegroundForTesting() const { return is_in_foreground_; }
  213. #endif
  214. protected:
  215. // Sets the persistent system profile. Virtual for tests.
  216. virtual void SetPersistentSystemProfile(const std::string& serialized_proto,
  217. bool complete);
  218. // Records the current environment (system profile) in |log|, and persists
  219. // the results in prefs.
  220. // Exposed for testing.
  221. static std::string RecordCurrentEnvironmentHelper(
  222. MetricsLog* log,
  223. PrefService* local_state,
  224. DelegatingProvider* delegating_provider);
  225. // The MetricsService has a lifecycle that is stored as a state.
  226. // See metrics_service.cc for description of this lifecycle.
  227. enum State {
  228. CONSTRUCTED, // Constructor was called.
  229. INITIALIZED, // InitializeMetricsRecordingState() was called.
  230. INIT_TASK_SCHEDULED, // Waiting for deferred init tasks to finish.
  231. INIT_TASK_DONE, // Waiting for timer to send initial log.
  232. SENDING_LOGS, // Sending logs an creating new ones when we run out.
  233. };
  234. State state() const { return state_; }
  235. private:
  236. // The current state of recording for the MetricsService. The state is UNSET
  237. // until set to something else, at which point it remains INACTIVE or ACTIVE
  238. // for the lifetime of the object.
  239. enum RecordingState {
  240. INACTIVE,
  241. ACTIVE,
  242. UNSET,
  243. };
  244. // Gets the LogStore for UMA logs.
  245. MetricsLogStore* log_store() {
  246. return reporting_service_.metrics_log_store();
  247. }
  248. // Calls into the client to initialize some system profile metrics.
  249. void StartInitTask();
  250. // Callback that moves the state to INIT_TASK_DONE. When this is called, the
  251. // state should be INIT_TASK_SCHEDULED.
  252. void FinishedInitTask();
  253. void OnUserAction(const std::string& action, base::TimeTicks action_time);
  254. // Get the amount of uptime since this process started and since the last
  255. // call to this function. Also updates the cumulative uptime metric (stored
  256. // as a pref) for uninstall. Uptimes are measured using TimeTicks, which
  257. // guarantees that it is monotonic and does not jump if the user changes
  258. // their clock. The TimeTicks implementation also makes the clock not
  259. // count time the computer is suspended.
  260. void GetUptimes(PrefService* pref,
  261. base::TimeDelta* incremental_uptime,
  262. base::TimeDelta* uptime);
  263. // Turns recording on or off.
  264. // DisableRecording() also forces a persistent save of logging state (if
  265. // anything has been recorded, or transmitted).
  266. void EnableRecording();
  267. void DisableRecording();
  268. // If in_idle is true, sets idle_since_last_transmission to true.
  269. // If in_idle is false and idle_since_last_transmission_ is true, sets
  270. // idle_since_last_transmission to false and starts the timer (provided
  271. // starting the timer is permitted).
  272. void HandleIdleSinceLastTransmission(bool in_idle);
  273. // Set up client ID, session ID, etc.
  274. void InitializeMetricsState();
  275. // Opens a new log for recording user experience metrics.
  276. void OpenNewLog();
  277. // Closes out the current log after adding any last information.
  278. void CloseCurrentLog();
  279. // Pushes the text of the current and staged logs into persistent storage.
  280. // Called when Chrome shuts down.
  281. void PushPendingLogsToPersistentStorage();
  282. // Ensures that scheduler is running, assuming the current settings are such
  283. // that metrics should be reported. If not, this is a no-op.
  284. void StartSchedulerIfNecessary();
  285. // Starts the process of uploading metrics data.
  286. void StartScheduledUpload();
  287. // Called by the client via a callback when final log info collection is
  288. // complete.
  289. void OnFinalLogInfoCollectionDone();
  290. // Prepares the initial stability log, which is only logged when the previous
  291. // run of Chrome crashed. This log contains any stability metrics left over
  292. // from that previous run, and only these stability metrics. It uses the
  293. // system profile from the previous session. |prefs_previous_version| is used
  294. // to validate the version number recovered from the system profile. Returns
  295. // true if a log was created.
  296. bool PrepareInitialStabilityLog(const std::string& prefs_previous_version);
  297. // Prepares the initial metrics log, which includes startup histograms and
  298. // profiler data, as well as incremental stability-related metrics.
  299. void PrepareInitialMetricsLog();
  300. // Creates a new MetricsLog instance with the given |log_type|.
  301. std::unique_ptr<MetricsLog> CreateLog(MetricsLog::LogType log_type);
  302. // Records the current environment (system profile) in |log|, and persists
  303. // the results in prefs and GlobalPersistentSystemProfile.
  304. void RecordCurrentEnvironment(MetricsLog* log, bool complete);
  305. // Record complete list of histograms into the current log.
  306. // Called when we close a log.
  307. void RecordCurrentHistograms();
  308. // Record complete list of stability histograms into the current log,
  309. // i.e., histograms with the |kUmaStabilityHistogramFlag| flag set.
  310. void RecordCurrentStabilityHistograms();
  311. // Handle completion of PrepareProviderMetricsLog which is run as a
  312. // background task.
  313. void PrepareProviderMetricsLogDone(
  314. std::unique_ptr<MetricsLog::IndependentMetricsLoader> loader,
  315. bool success);
  316. // Record a single independent profile and associated histogram from
  317. // metrics providers. If this returns true, one was found and there may
  318. // be more.
  319. bool PrepareProviderMetricsLog();
  320. // Records one independent histogram log and then reschedules itself to
  321. // check for others. The interval is so as to not adversely impact the UI.
  322. void PrepareProviderMetricsTask();
  323. // Updates the "last live" browser timestamp and schedules the next update.
  324. void UpdateLastLiveTimestampTask();
  325. // Sub-service for uploading logs.
  326. MetricsReportingService reporting_service_;
  327. // Manager for the various in-flight logs.
  328. MetricsLogManager log_manager_;
  329. // |histogram_snapshot_manager_| prepares histogram deltas for transmission.
  330. base::HistogramSnapshotManager histogram_snapshot_manager_;
  331. // Used to manage various metrics reporting state prefs, such as client id,
  332. // low entropy source and whether metrics reporting is enabled. Weak pointer.
  333. const raw_ptr<MetricsStateManager> state_manager_;
  334. // Used to interact with the embedder. Weak pointer; must outlive |this|
  335. // instance.
  336. const raw_ptr<MetricsServiceClient> client_;
  337. // Registered metrics providers.
  338. DelegatingProvider delegating_provider_;
  339. raw_ptr<PrefService> local_state_;
  340. base::ActionCallback action_callback_;
  341. // Indicate whether recording and reporting are currently happening.
  342. // These should not be set directly, but by calling SetRecording and
  343. // SetReporting.
  344. RecordingState recording_state_;
  345. // Indicate whether test mode is enabled, where the initial log should never
  346. // be cut, and logs are neither persisted nor uploaded.
  347. bool test_mode_active_;
  348. // The progression of states made by the browser are recorded in the following
  349. // state.
  350. State state_;
  351. // The initial metrics log, used to record startup metrics (histograms and
  352. // profiler data). Note that if a crash occurred in the previous session, an
  353. // initial stability log may be sent before this.
  354. std::unique_ptr<MetricsLog> initial_metrics_log_;
  355. // Whether the MetricsService object has received any notifications since
  356. // the last time a transmission was sent.
  357. bool idle_since_last_transmission_;
  358. // A number that identifies the how many times the app has been launched.
  359. int session_id_;
  360. // The scheduler for determining when log rotations should happen.
  361. std::unique_ptr<MetricsRotationScheduler> rotation_scheduler_;
  362. // Stores the time of the first call to |GetUptimes()|.
  363. base::TimeTicks first_updated_time_;
  364. // Stores the time of the last call to |GetUptimes()|.
  365. base::TimeTicks last_updated_time_;
  366. // Indicates if loading of independent metrics is currently active.
  367. bool independent_loader_active_ = false;
  368. // A set of observers that keeps track of the metrics reporting state.
  369. base::RepeatingCallbackList<void(bool)> enablement_observers_;
  370. #if BUILDFLAG(IS_ANDROID) || BUILDFLAG(IS_IOS)
  371. // Indicates whether OnAppEnterForeground() (true) or OnAppEnterBackground
  372. // (false) was called.
  373. bool is_in_foreground_ = false;
  374. #endif
  375. FRIEND_TEST_ALL_PREFIXES(MetricsServiceTest, ActiveFieldTrialsReported);
  376. FRIEND_TEST_ALL_PREFIXES(MetricsServiceTest, IsPluginProcess);
  377. FRIEND_TEST_ALL_PREFIXES(::ChromeMetricsServiceClientTest,
  378. TestRegisterMetricsServiceProviders);
  379. FRIEND_TEST_ALL_PREFIXES(::IOSChromeMetricsServiceClientTest,
  380. TestRegisterMetricsServiceProviders);
  381. SEQUENCE_CHECKER(sequence_checker_);
  382. // Weak pointers factory used to post task on different threads. All weak
  383. // pointers managed by this factory have the same lifetime as MetricsService.
  384. base::WeakPtrFactory<MetricsService> self_ptr_factory_{this};
  385. };
  386. } // namespace metrics
  387. #endif // COMPONENTS_METRICS_METRICS_SERVICE_H_