| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 // This file defines a service that collects information about the user | 5 // This file defines a service that collects information about the user |
| 6 // experience in order to help improve future versions of the app. | 6 // experience in order to help improve future versions of the app. |
| 7 | 7 |
| 8 #ifndef COMPONENTS_METRICS_METRICS_SERVICE_H_ | 8 #ifndef COMPONENTS_METRICS_METRICS_SERVICE_H_ |
| 9 #define COMPONENTS_METRICS_METRICS_SERVICE_H_ | 9 #define COMPONENTS_METRICS_METRICS_SERVICE_H_ |
| 10 | 10 |
| 11 #include <stdint.h> | 11 #include <stdint.h> |
| 12 | 12 |
| 13 #include <map> | 13 #include <map> |
| 14 #include <memory> | 14 #include <memory> |
| 15 #include <string> | 15 #include <string> |
| 16 #include <vector> | 16 #include <vector> |
| 17 | 17 |
| 18 #include "base/gtest_prod_util.h" | 18 #include "base/gtest_prod_util.h" |
| 19 #include "base/macros.h" | 19 #include "base/macros.h" |
| 20 #include "base/memory/weak_ptr.h" | 20 #include "base/memory/weak_ptr.h" |
| 21 #include "base/metrics/field_trial.h" | 21 #include "base/metrics/field_trial.h" |
| 22 #include "base/metrics/histogram_flattener.h" | 22 #include "base/metrics/histogram_flattener.h" |
| 23 #include "base/metrics/histogram_snapshot_manager.h" | 23 #include "base/metrics/histogram_snapshot_manager.h" |
| 24 #include "base/metrics/user_metrics.h" | 24 #include "base/metrics/user_metrics.h" |
| 25 #include "base/observer_list.h" | 25 #include "base/observer_list.h" |
| 26 #include "base/threading/thread_checker.h" | 26 #include "base/threading/thread_checker.h" |
| 27 #include "base/time/time.h" | 27 #include "base/time/time.h" |
| 28 #include "build/build_config.h" | 28 #include "build/build_config.h" |
| 29 #include "components/metrics/clean_exit_beacon.h" | 29 #include "components/metrics/clean_exit_beacon.h" |
| 30 #include "components/metrics/data_use_tracker.h" | |
| 31 #include "components/metrics/execution_phase.h" | 30 #include "components/metrics/execution_phase.h" |
| 32 #include "components/metrics/metrics_log.h" | 31 #include "components/metrics/metrics_log.h" |
| 33 #include "components/metrics/metrics_log_manager.h" | 32 #include "components/metrics/metrics_log_manager.h" |
| 34 #include "components/metrics/metrics_log_store.h" | 33 #include "components/metrics/metrics_log_store.h" |
| 35 #include "components/metrics/metrics_provider.h" | 34 #include "components/metrics/metrics_provider.h" |
| 35 #include "components/metrics/metrics_reporting_service.h" |
| 36 #include "components/metrics/net/network_metrics_provider.h" | 36 #include "components/metrics/net/network_metrics_provider.h" |
| 37 #include "components/variations/synthetic_trials.h" | 37 #include "components/variations/synthetic_trials.h" |
| 38 | 38 |
| 39 class PrefService; | 39 class PrefService; |
| 40 class PrefRegistrySimple; | 40 class PrefRegistrySimple; |
| 41 | 41 |
| 42 namespace base { | 42 namespace base { |
| 43 class HistogramSamples; | 43 class HistogramSamples; |
| 44 class PrefService; | 44 class PrefService; |
| 45 } | 45 } |
| 46 | 46 |
| 47 namespace variations { | 47 namespace variations { |
| 48 struct ActiveGroupId; | 48 struct ActiveGroupId; |
| 49 } | 49 } |
| 50 | 50 |
| 51 namespace metrics { | 51 namespace metrics { |
| 52 | 52 |
| 53 class MetricsLogUploader; | |
| 54 class MetricsRotationScheduler; | 53 class MetricsRotationScheduler; |
| 55 class MetricsUploadScheduler; | |
| 56 class MetricsServiceAccessor; | 54 class MetricsServiceAccessor; |
| 57 class MetricsServiceClient; | 55 class MetricsServiceClient; |
| 58 class MetricsStateManager; | 56 class MetricsStateManager; |
| 59 | 57 |
| 60 // See metrics_service.cc for a detailed description. | 58 // See metrics_service.cc for a detailed description. |
| 61 class MetricsService : public base::HistogramFlattener { | 59 class MetricsService : public base::HistogramFlattener { |
| 62 public: | 60 public: |
| 63 // Creates the MetricsService with the given |state_manager|, |client|, and | 61 // Creates the MetricsService with the given |state_manager|, |client|, and |
| 64 // |local_state|. Does not take ownership of the paramaters; instead stores | 62 // |local_state|. Does not take ownership of the paramaters; instead stores |
| 65 // a weak pointer to each. Caller should ensure that the parameters are valid | 63 // a weak pointer to each. Caller should ensure that the parameters are valid |
| (...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 152 // Saves in the preferences if the crash report registration was successful. | 150 // Saves in the preferences if the crash report registration was successful. |
| 153 // This count is eventually send via UMA logs. | 151 // This count is eventually send via UMA logs. |
| 154 void RecordBreakpadRegistration(bool success); | 152 void RecordBreakpadRegistration(bool success); |
| 155 | 153 |
| 156 // Saves in the preferences if the browser is running under a debugger. | 154 // Saves in the preferences if the browser is running under a debugger. |
| 157 // This count is eventually send via UMA logs. | 155 // This count is eventually send via UMA logs. |
| 158 void RecordBreakpadHasDebugger(bool has_debugger); | 156 void RecordBreakpadHasDebugger(bool has_debugger); |
| 159 | 157 |
| 160 bool recording_active() const; | 158 bool recording_active() const; |
| 161 bool reporting_active() const; | 159 bool reporting_active() const; |
| 160 bool has_unsent_logs() const; |
| 162 | 161 |
| 163 // Redundant test to ensure that we are notified of a clean exit. | 162 // Redundant test to ensure that we are notified of a clean exit. |
| 164 // This value should be true when process has completed shutdown. | 163 // This value should be true when process has completed shutdown. |
| 165 static bool UmaMetricsProperlyShutdown(); | 164 static bool UmaMetricsProperlyShutdown(); |
| 166 | 165 |
| 167 // Public accessor that returns the list of synthetic field trials. It must | 166 // Public accessor that returns the list of synthetic field trials. It must |
| 168 // only be used for testing. | 167 // only be used for testing. |
| 169 void GetCurrentSyntheticFieldTrialsForTesting( | 168 void GetCurrentSyntheticFieldTrialsForTesting( |
| 170 std::vector<variations::ActiveGroupId>* synthetic_trials); | 169 std::vector<variations::ActiveGroupId>* synthetic_trials); |
| 171 | 170 |
| (...skipping 21 matching lines...) Expand all Loading... |
| 193 void PushExternalLog(const std::string& log); | 192 void PushExternalLog(const std::string& log); |
| 194 | 193 |
| 195 // Updates data usage tracking prefs with the specified values. | 194 // Updates data usage tracking prefs with the specified values. |
| 196 void UpdateMetricsUsagePrefs(const std::string& service_name, | 195 void UpdateMetricsUsagePrefs(const std::string& service_name, |
| 197 int message_size, | 196 int message_size, |
| 198 bool is_cellular); | 197 bool is_cellular); |
| 199 | 198 |
| 200 protected: | 199 protected: |
| 201 // Exposed for testing. | 200 // Exposed for testing. |
| 202 MetricsLogManager* log_manager() { return &log_manager_; } | 201 MetricsLogManager* log_manager() { return &log_manager_; } |
| 203 MetricsLogStore* log_store() { return &log_store_; } | 202 MetricsLogStore* log_store() { |
| 203 return reporting_service_.metrics_log_store(); |
| 204 } |
| 204 | 205 |
| 205 private: | 206 private: |
| 206 friend class MetricsServiceAccessor; | 207 friend class MetricsServiceAccessor; |
| 207 | 208 |
| 208 // The MetricsService has a lifecycle that is stored as a state. | 209 // The MetricsService has a lifecycle that is stored as a state. |
| 209 // See metrics_service.cc for description of this lifecycle. | 210 // See metrics_service.cc for description of this lifecycle. |
| 210 enum State { | 211 enum State { |
| 211 INITIALIZED, // Constructor was called. | 212 INITIALIZED, // Constructor was called. |
| 212 INIT_TASK_SCHEDULED, // Waiting for deferred init tasks to finish. | 213 INIT_TASK_SCHEDULED, // Waiting for deferred init tasks to finish. |
| 213 INIT_TASK_DONE, // Waiting for timer to send initial log. | 214 INIT_TASK_DONE, // Waiting for timer to send initial log. |
| (...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 300 // that metrics should be reported. If not, this is a no-op. | 301 // that metrics should be reported. If not, this is a no-op. |
| 301 void StartSchedulerIfNecessary(); | 302 void StartSchedulerIfNecessary(); |
| 302 | 303 |
| 303 // Starts the process of uploading metrics data. | 304 // Starts the process of uploading metrics data. |
| 304 void StartScheduledUpload(); | 305 void StartScheduledUpload(); |
| 305 | 306 |
| 306 // Called by the client via a callback when final log info collection is | 307 // Called by the client via a callback when final log info collection is |
| 307 // complete. | 308 // complete. |
| 308 void OnFinalLogInfoCollectionDone(); | 309 void OnFinalLogInfoCollectionDone(); |
| 309 | 310 |
| 310 // If recording is enabled, begins uploading the next completed log from | |
| 311 // the log manager, staging it if necessary. | |
| 312 void SendNextLog(); | |
| 313 | |
| 314 // Returns true if any of the registered metrics providers have critical | 311 // Returns true if any of the registered metrics providers have critical |
| 315 // stability metrics to report in an initial stability log. | 312 // stability metrics to report in an initial stability log. |
| 316 bool ProvidersHaveInitialStabilityMetrics(); | 313 bool ProvidersHaveInitialStabilityMetrics(); |
| 317 | 314 |
| 318 // Prepares the initial stability log, which is only logged when the previous | 315 // Prepares the initial stability log, which is only logged when the previous |
| 319 // run of Chrome crashed. This log contains any stability metrics left over | 316 // run of Chrome crashed. This log contains any stability metrics left over |
| 320 // from that previous run, and only these stability metrics. It uses the | 317 // from that previous run, and only these stability metrics. It uses the |
| 321 // system profile from the previous session. |prefs_previous_version| is used | 318 // system profile from the previous session. |prefs_previous_version| is used |
| 322 // to validate the version number recovered from the system profile. Returns | 319 // to validate the version number recovered from the system profile. Returns |
| 323 // true if a log was created. | 320 // true if a log was created. |
| 324 bool PrepareInitialStabilityLog(const std::string& prefs_previous_version); | 321 bool PrepareInitialStabilityLog(const std::string& prefs_previous_version); |
| 325 | 322 |
| 326 // Prepares the initial metrics log, which includes startup histograms and | 323 // Prepares the initial metrics log, which includes startup histograms and |
| 327 // profiler data, as well as incremental stability-related metrics. | 324 // profiler data, as well as incremental stability-related metrics. |
| 328 void PrepareInitialMetricsLog(); | 325 void PrepareInitialMetricsLog(); |
| 329 | 326 |
| 330 // Uploads the currently staged log (which must be non-null). | |
| 331 void SendStagedLog(); | |
| 332 | |
| 333 // Called after transmission completes (either successfully or with failure). | |
| 334 void OnLogUploadComplete(int response_code); | |
| 335 | |
| 336 // Reads, increments and then sets the specified long preference that is | 327 // Reads, increments and then sets the specified long preference that is |
| 337 // stored as a string. | 328 // stored as a string. |
| 338 void IncrementLongPrefsValue(const char* path); | 329 void IncrementLongPrefsValue(const char* path); |
| 339 | 330 |
| 340 // Records that the browser was shut down cleanly. | 331 // Records that the browser was shut down cleanly. |
| 341 void LogCleanShutdown(bool end_completed); | 332 void LogCleanShutdown(bool end_completed); |
| 342 | 333 |
| 343 // Notifies observers on a synthetic trial list change. | 334 // Notifies observers on a synthetic trial list change. |
| 344 void NotifySyntheticTrialObservers(); | 335 void NotifySyntheticTrialObservers(); |
| 345 | 336 |
| 346 // Returns a list of synthetic field trials that are older than |time|. | 337 // Returns a list of synthetic field trials that are older than |time|. |
| 347 void GetSyntheticFieldTrialsOlderThan( | 338 void GetSyntheticFieldTrialsOlderThan( |
| 348 base::TimeTicks time, | 339 base::TimeTicks time, |
| 349 std::vector<variations::ActiveGroupId>* synthetic_trials); | 340 std::vector<variations::ActiveGroupId>* synthetic_trials); |
| 350 | 341 |
| 351 // Creates a new MetricsLog instance with the given |log_type|. | 342 // Creates a new MetricsLog instance with the given |log_type|. |
| 352 std::unique_ptr<MetricsLog> CreateLog(MetricsLog::LogType log_type); | 343 std::unique_ptr<MetricsLog> CreateLog(MetricsLog::LogType log_type); |
| 353 | 344 |
| 354 // Records the current environment (system profile) in |log|. | 345 // Records the current environment (system profile) in |log|. |
| 355 void RecordCurrentEnvironment(MetricsLog* log); | 346 void RecordCurrentEnvironment(MetricsLog* log); |
| 356 | 347 |
| 357 // Record complete list of histograms into the current log. | 348 // Record complete list of histograms into the current log. |
| 358 // Called when we close a log. | 349 // Called when we close a log. |
| 359 void RecordCurrentHistograms(); | 350 void RecordCurrentHistograms(); |
| 360 | 351 |
| 361 // Record complete list of stability histograms into the current log, | 352 // Record complete list of stability histograms into the current log, |
| 362 // i.e., histograms with the |kUmaStabilityHistogramFlag| flag set. | 353 // i.e., histograms with the |kUmaStabilityHistogramFlag| flag set. |
| 363 void RecordCurrentStabilityHistograms(); | 354 void RecordCurrentStabilityHistograms(); |
| 364 | 355 |
| 356 // Sub-Service for uploading logs. |
| 357 MetricsReportingService reporting_service_; |
| 358 |
| 365 // Manager for the various in-flight logs. | 359 // Manager for the various in-flight logs. |
| 366 MetricsLogManager log_manager_; | 360 MetricsLogManager log_manager_; |
| 367 | 361 |
| 368 // Store of logs ready to be uploaded. | |
| 369 MetricsLogStore log_store_; | |
| 370 | |
| 371 // |histogram_snapshot_manager_| prepares histogram deltas for transmission. | 362 // |histogram_snapshot_manager_| prepares histogram deltas for transmission. |
| 372 base::HistogramSnapshotManager histogram_snapshot_manager_; | 363 base::HistogramSnapshotManager histogram_snapshot_manager_; |
| 373 | 364 |
| 374 // Used to manage various metrics reporting state prefs, such as client id, | 365 // Used to manage various metrics reporting state prefs, such as client id, |
| 375 // low entropy source and whether metrics reporting is enabled. Weak pointer. | 366 // low entropy source and whether metrics reporting is enabled. Weak pointer. |
| 376 MetricsStateManager* const state_manager_; | 367 MetricsStateManager* const state_manager_; |
| 377 | 368 |
| 378 // Used to interact with the embedder. Weak pointer; must outlive |this| | 369 // Used to interact with the embedder. Weak pointer; must outlive |this| |
| 379 // instance. | 370 // instance. |
| 380 MetricsServiceClient* const client_; | 371 MetricsServiceClient* const client_; |
| 381 | 372 |
| 382 // Registered metrics providers. | 373 // Registered metrics providers. |
| 383 std::vector<std::unique_ptr<MetricsProvider>> metrics_providers_; | 374 std::vector<std::unique_ptr<MetricsProvider>> metrics_providers_; |
| 384 | 375 |
| 385 PrefService* local_state_; | 376 PrefService* local_state_; |
| 386 | 377 |
| 387 CleanExitBeacon clean_exit_beacon_; | 378 CleanExitBeacon clean_exit_beacon_; |
| 388 | 379 |
| 389 base::ActionCallback action_callback_; | 380 base::ActionCallback action_callback_; |
| 390 | 381 |
| 391 // Indicate whether recording and reporting are currently happening. | 382 // Indicate whether recording and reporting are currently happening. |
| 392 // These should not be set directly, but by calling SetRecording and | 383 // These should not be set directly, but by calling SetRecording and |
| 393 // SetReporting. | 384 // SetReporting. |
| 394 RecordingState recording_state_; | 385 RecordingState recording_state_; |
| 395 bool reporting_active_; | |
| 396 | 386 |
| 397 // Indicate whether test mode is enabled, where the initial log should never | 387 // Indicate whether test mode is enabled, where the initial log should never |
| 398 // be cut, and logs are neither persisted nor uploaded. | 388 // be cut, and logs are neither persisted nor uploaded. |
| 399 bool test_mode_active_; | 389 bool test_mode_active_; |
| 400 | 390 |
| 401 // The progression of states made by the browser are recorded in the following | 391 // The progression of states made by the browser are recorded in the following |
| 402 // state. | 392 // state. |
| 403 State state_; | 393 State state_; |
| 404 | 394 |
| 405 // The initial metrics log, used to record startup metrics (histograms and | 395 // The initial metrics log, used to record startup metrics (histograms and |
| 406 // profiler data). Note that if a crash occurred in the previous session, an | 396 // profiler data). Note that if a crash occurred in the previous session, an |
| 407 // initial stability log may be sent before this. | 397 // initial stability log may be sent before this. |
| 408 std::unique_ptr<MetricsLog> initial_metrics_log_; | 398 std::unique_ptr<MetricsLog> initial_metrics_log_; |
| 409 | 399 |
| 410 // Instance of the helper class for uploading logs. | |
| 411 std::unique_ptr<MetricsLogUploader> log_uploader_; | |
| 412 | |
| 413 // Whether there is a current log upload in progress. | |
| 414 bool log_upload_in_progress_; | |
| 415 | |
| 416 // Whether the MetricsService object has received any notifications since | 400 // Whether the MetricsService object has received any notifications since |
| 417 // the last time a transmission was sent. | 401 // the last time a transmission was sent. |
| 418 bool idle_since_last_transmission_; | 402 bool idle_since_last_transmission_; |
| 419 | 403 |
| 420 // A number that identifies the how many times the app has been launched. | 404 // A number that identifies the how many times the app has been launched. |
| 421 int session_id_; | 405 int session_id_; |
| 422 | 406 |
| 423 // The scheduler for determining when log rotations should happen. | 407 // The scheduler for determining when log rotations should happen. |
| 424 std::unique_ptr<MetricsRotationScheduler> rotation_scheduler_; | 408 std::unique_ptr<MetricsRotationScheduler> rotation_scheduler_; |
| 425 // The scheduler for determining when uploads should happen. | |
| 426 std::unique_ptr<MetricsUploadScheduler> upload_scheduler_; | |
| 427 | 409 |
| 428 // Stores the time of the first call to |GetUptimes()|. | 410 // Stores the time of the first call to |GetUptimes()|. |
| 429 base::TimeTicks first_updated_time_; | 411 base::TimeTicks first_updated_time_; |
| 430 | 412 |
| 431 // Stores the time of the last call to |GetUptimes()|. | 413 // Stores the time of the last call to |GetUptimes()|. |
| 432 base::TimeTicks last_updated_time_; | 414 base::TimeTicks last_updated_time_; |
| 433 | 415 |
| 434 // Field trial groups that map to Chrome configuration states. | 416 // Field trial groups that map to Chrome configuration states. |
| 435 SyntheticTrialGroups synthetic_trial_groups_; | 417 SyntheticTrialGroups synthetic_trial_groups_; |
| 436 | 418 |
| 437 // List of observers of |synthetic_trial_groups_| changes. | 419 // List of observers of |synthetic_trial_groups_| changes. |
| 438 base::ObserverList<variations::SyntheticTrialObserver> | 420 base::ObserverList<variations::SyntheticTrialObserver> |
| 439 synthetic_trial_observer_list_; | 421 synthetic_trial_observer_list_; |
| 440 | 422 |
| 441 // Redundant marker to check that we completed our shutdown, and set the | 423 // Redundant marker to check that we completed our shutdown, and set the |
| 442 // exited-cleanly bit in the prefs. | 424 // exited-cleanly bit in the prefs. |
| 443 static ShutdownCleanliness clean_shutdown_status_; | 425 static ShutdownCleanliness clean_shutdown_status_; |
| 444 | 426 |
| 445 FRIEND_TEST_ALL_PREFIXES(MetricsServiceTest, IsPluginProcess); | 427 FRIEND_TEST_ALL_PREFIXES(MetricsServiceTest, IsPluginProcess); |
| 446 FRIEND_TEST_ALL_PREFIXES(MetricsServiceTest, | 428 FRIEND_TEST_ALL_PREFIXES(MetricsServiceTest, |
| 447 PermutedEntropyCacheClearedWhenLowEntropyReset); | 429 PermutedEntropyCacheClearedWhenLowEntropyReset); |
| 448 FRIEND_TEST_ALL_PREFIXES(MetricsServiceTest, RegisterSyntheticTrial); | 430 FRIEND_TEST_ALL_PREFIXES(MetricsServiceTest, RegisterSyntheticTrial); |
| 449 FRIEND_TEST_ALL_PREFIXES(MetricsServiceTest, | 431 FRIEND_TEST_ALL_PREFIXES(MetricsServiceTest, |
| 450 RegisterSyntheticMultiGroupFieldTrial); | 432 RegisterSyntheticMultiGroupFieldTrial); |
| 451 | 433 |
| 452 // Pointer used for obtaining data use pref updater callback on above layers. | |
| 453 std::unique_ptr<DataUseTracker> data_use_tracker_; | |
| 454 | |
| 455 base::ThreadChecker thread_checker_; | 434 base::ThreadChecker thread_checker_; |
| 456 | 435 |
| 457 // Weak pointers factory used to post task on different threads. All weak | 436 // Weak pointers factory used to post task on different threads. All weak |
| 458 // pointers managed by this factory have the same lifetime as MetricsService. | 437 // pointers managed by this factory have the same lifetime as MetricsService. |
| 459 base::WeakPtrFactory<MetricsService> self_ptr_factory_; | 438 base::WeakPtrFactory<MetricsService> self_ptr_factory_; |
| 460 | 439 |
| 461 DISALLOW_COPY_AND_ASSIGN(MetricsService); | 440 DISALLOW_COPY_AND_ASSIGN(MetricsService); |
| 462 }; | 441 }; |
| 463 | 442 |
| 464 } // namespace metrics | 443 } // namespace metrics |
| 465 | 444 |
| 466 #endif // COMPONENTS_METRICS_METRICS_SERVICE_H_ | 445 #endif // COMPONENTS_METRICS_METRICS_SERVICE_H_ |
| OLD | NEW |