| 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" | |
| 26 #include "base/threading/thread_checker.h" | 25 #include "base/threading/thread_checker.h" |
| 27 #include "base/time/time.h" | 26 #include "base/time/time.h" |
| 28 #include "build/build_config.h" | 27 #include "build/build_config.h" |
| 29 #include "components/metrics/clean_exit_beacon.h" | 28 #include "components/metrics/clean_exit_beacon.h" |
| 30 #include "components/metrics/execution_phase.h" | 29 #include "components/metrics/execution_phase.h" |
| 31 #include "components/metrics/metrics_log.h" | 30 #include "components/metrics/metrics_log.h" |
| 32 #include "components/metrics/metrics_log_manager.h" | 31 #include "components/metrics/metrics_log_manager.h" |
| 33 #include "components/metrics/metrics_log_store.h" | 32 #include "components/metrics/metrics_log_store.h" |
| 34 #include "components/metrics/metrics_provider.h" | 33 #include "components/metrics/metrics_provider.h" |
| 35 #include "components/metrics/metrics_reporting_service.h" | 34 #include "components/metrics/metrics_reporting_service.h" |
| 36 #include "components/metrics/net/network_metrics_provider.h" | 35 #include "components/metrics/net/network_metrics_provider.h" |
| 37 #include "components/variations/synthetic_trials.h" | 36 #include "components/variations/synthetic_trial_registry.h" |
| 38 | 37 |
| 39 class PrefService; | 38 class PrefService; |
| 40 class PrefRegistrySimple; | 39 class PrefRegistrySimple; |
| 41 | 40 |
| 42 namespace base { | 41 namespace base { |
| 43 class HistogramSamples; | 42 class HistogramSamples; |
| 44 class PrefService; | 43 class PrefService; |
| 45 } | 44 } |
| 46 | 45 |
| 47 namespace variations { | |
| 48 struct ActiveGroupId; | |
| 49 } | |
| 50 | |
| 51 namespace metrics { | 46 namespace metrics { |
| 52 | 47 |
| 53 class MetricsRotationScheduler; | 48 class MetricsRotationScheduler; |
| 54 class MetricsServiceAccessor; | 49 class MetricsServiceAccessor; |
| 55 class MetricsServiceClient; | 50 class MetricsServiceClient; |
| 56 class MetricsStateManager; | 51 class MetricsStateManager; |
| 57 | 52 |
| 58 // See metrics_service.cc for a detailed description. | 53 // See metrics_service.cc for a detailed description. |
| 59 class MetricsService : public base::HistogramFlattener { | 54 class MetricsService : public base::HistogramFlattener, |
| 55 public variations::SyntheticTrialRegistry { |
| 60 public: | 56 public: |
| 61 // Creates the MetricsService with the given |state_manager|, |client|, and | 57 // Creates the MetricsService with the given |state_manager|, |client|, and |
| 62 // |local_state|. Does not take ownership of the paramaters; instead stores | 58 // |local_state|. Does not take ownership of the paramaters; instead stores |
| 63 // a weak pointer to each. Caller should ensure that the parameters are valid | 59 // a weak pointer to each. Caller should ensure that the parameters are valid |
| 64 // for the lifetime of this class. | 60 // for the lifetime of this class. |
| 65 MetricsService(MetricsStateManager* state_manager, | 61 MetricsService(MetricsStateManager* state_manager, |
| 66 MetricsServiceClient* client, | 62 MetricsServiceClient* client, |
| 67 PrefService* local_state); | 63 PrefService* local_state); |
| 68 ~MetricsService() override; | 64 ~MetricsService() override; |
| 69 | 65 |
| (...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 155 void RecordBreakpadHasDebugger(bool has_debugger); | 151 void RecordBreakpadHasDebugger(bool has_debugger); |
| 156 | 152 |
| 157 bool recording_active() const; | 153 bool recording_active() const; |
| 158 bool reporting_active() const; | 154 bool reporting_active() const; |
| 159 bool has_unsent_logs() const; | 155 bool has_unsent_logs() const; |
| 160 | 156 |
| 161 // Redundant test to ensure that we are notified of a clean exit. | 157 // Redundant test to ensure that we are notified of a clean exit. |
| 162 // This value should be true when process has completed shutdown. | 158 // This value should be true when process has completed shutdown. |
| 163 static bool UmaMetricsProperlyShutdown(); | 159 static bool UmaMetricsProperlyShutdown(); |
| 164 | 160 |
| 165 // Public accessor that returns the list of synthetic field trials. It must | |
| 166 // only be used for testing. | |
| 167 void GetCurrentSyntheticFieldTrialsForTesting( | |
| 168 std::vector<variations::ActiveGroupId>* synthetic_trials); | |
| 169 | |
| 170 // Adds an observer to be notified when the synthetic trials list changes. | |
| 171 void AddSyntheticTrialObserver(variations::SyntheticTrialObserver* observer); | |
| 172 | |
| 173 // Removes an existing observer of synthetic trials list changes. | |
| 174 void RemoveSyntheticTrialObserver( | |
| 175 variations::SyntheticTrialObserver* observer); | |
| 176 | |
| 177 // Register the specified |provider| to provide additional metrics into the | 161 // Register the specified |provider| to provide additional metrics into the |
| 178 // UMA log. Should be called during MetricsService initialization only. | 162 // UMA log. Should be called during MetricsService initialization only. |
| 179 void RegisterMetricsProvider(std::unique_ptr<MetricsProvider> provider); | 163 void RegisterMetricsProvider(std::unique_ptr<MetricsProvider> provider); |
| 180 | 164 |
| 181 // Check if this install was cloned or imaged from another machine. If a | 165 // Check if this install was cloned or imaged from another machine. If a |
| 182 // clone is detected, reset the client id and low entropy source. This | 166 // clone is detected, reset the client id and low entropy source. This |
| 183 // should not be called more than once. | 167 // should not be called more than once. |
| 184 void CheckForClonedInstall(); | 168 void CheckForClonedInstall(); |
| 185 | 169 |
| 186 // Clears the stability metrics that are saved in local state. | 170 // Clears the stability metrics that are saved in local state. |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 220 | 204 |
| 221 // The current state of recording for the MetricsService. The state is UNSET | 205 // The current state of recording for the MetricsService. The state is UNSET |
| 222 // until set to something else, at which point it remains INACTIVE or ACTIVE | 206 // until set to something else, at which point it remains INACTIVE or ACTIVE |
| 223 // for the lifetime of the object. | 207 // for the lifetime of the object. |
| 224 enum RecordingState { | 208 enum RecordingState { |
| 225 INACTIVE, | 209 INACTIVE, |
| 226 ACTIVE, | 210 ACTIVE, |
| 227 UNSET | 211 UNSET |
| 228 }; | 212 }; |
| 229 | 213 |
| 230 typedef std::vector<variations::SyntheticTrialGroup> SyntheticTrialGroups; | |
| 231 | |
| 232 // Registers a field trial name and group to be used to annotate a UMA report | |
| 233 // with a particular Chrome configuration state. A UMA report will be | |
| 234 // annotated with this trial group if and only if all events in the report | |
| 235 // were created after the trial is registered. Only one group name may be | |
| 236 // registered at a time for a given trial_name. Only the last group name that | |
| 237 // is registered for a given trial name will be recorded. The values passed | |
| 238 // in must not correspond to any real field trial in the code. | |
| 239 // Note: Should not be used to replace trials that were registered with | |
| 240 // RegisterMultiGroupSyntheticFieldTrial(). | |
| 241 void RegisterSyntheticFieldTrial( | |
| 242 const variations::SyntheticTrialGroup& trial_group); | |
| 243 | |
| 244 // Similar to RegisterSyntheticFieldTrial(), but registers a synthetic trial | |
| 245 // that has multiple active groups for a given trial name hash. Any previous | |
| 246 // groups registered for |trial_name_hash| will be replaced. | |
| 247 void RegisterSyntheticMultiGroupFieldTrial( | |
| 248 uint32_t trial_name_hash, | |
| 249 const std::vector<uint32_t>& group_name_hashes); | |
| 250 | |
| 251 // Calls into the client to initialize some system profile metrics. | 214 // Calls into the client to initialize some system profile metrics. |
| 252 void StartInitTask(); | 215 void StartInitTask(); |
| 253 | 216 |
| 254 // Callback that moves the state to INIT_TASK_DONE. When this is called, the | 217 // Callback that moves the state to INIT_TASK_DONE. When this is called, the |
| 255 // state should be INIT_TASK_SCHEDULED. | 218 // state should be INIT_TASK_SCHEDULED. |
| 256 void FinishedInitTask(); | 219 void FinishedInitTask(); |
| 257 | 220 |
| 258 void OnUserAction(const std::string& action); | 221 void OnUserAction(const std::string& action); |
| 259 | 222 |
| 260 // Get the amount of uptime since this process started and since the last | 223 // Get the amount of uptime since this process started and since the last |
| (...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 322 // profiler data, as well as incremental stability-related metrics. | 285 // profiler data, as well as incremental stability-related metrics. |
| 323 void PrepareInitialMetricsLog(); | 286 void PrepareInitialMetricsLog(); |
| 324 | 287 |
| 325 // Reads, increments and then sets the specified long preference that is | 288 // Reads, increments and then sets the specified long preference that is |
| 326 // stored as a string. | 289 // stored as a string. |
| 327 void IncrementLongPrefsValue(const char* path); | 290 void IncrementLongPrefsValue(const char* path); |
| 328 | 291 |
| 329 // Records that the browser was shut down cleanly. | 292 // Records that the browser was shut down cleanly. |
| 330 void LogCleanShutdown(bool end_completed); | 293 void LogCleanShutdown(bool end_completed); |
| 331 | 294 |
| 332 // Notifies observers on a synthetic trial list change. | |
| 333 void NotifySyntheticTrialObservers(); | |
| 334 | |
| 335 // Returns a list of synthetic field trials that are older than |time|. | |
| 336 void GetSyntheticFieldTrialsOlderThan( | |
| 337 base::TimeTicks time, | |
| 338 std::vector<variations::ActiveGroupId>* synthetic_trials); | |
| 339 | |
| 340 // Creates a new MetricsLog instance with the given |log_type|. | 295 // Creates a new MetricsLog instance with the given |log_type|. |
| 341 std::unique_ptr<MetricsLog> CreateLog(MetricsLog::LogType log_type); | 296 std::unique_ptr<MetricsLog> CreateLog(MetricsLog::LogType log_type); |
| 342 | 297 |
| 343 // Records the current environment (system profile) in |log|. | 298 // Records the current environment (system profile) in |log|. |
| 344 void RecordCurrentEnvironment(MetricsLog* log); | 299 void RecordCurrentEnvironment(MetricsLog* log); |
| 345 | 300 |
| 346 // Record complete list of histograms into the current log. | 301 // Record complete list of histograms into the current log. |
| 347 // Called when we close a log. | 302 // Called when we close a log. |
| 348 void RecordCurrentHistograms(); | 303 void RecordCurrentHistograms(); |
| 349 | 304 |
| (...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 411 | 366 |
| 412 // The scheduler for determining when log rotations should happen. | 367 // The scheduler for determining when log rotations should happen. |
| 413 std::unique_ptr<MetricsRotationScheduler> rotation_scheduler_; | 368 std::unique_ptr<MetricsRotationScheduler> rotation_scheduler_; |
| 414 | 369 |
| 415 // Stores the time of the first call to |GetUptimes()|. | 370 // Stores the time of the first call to |GetUptimes()|. |
| 416 base::TimeTicks first_updated_time_; | 371 base::TimeTicks first_updated_time_; |
| 417 | 372 |
| 418 // Stores the time of the last call to |GetUptimes()|. | 373 // Stores the time of the last call to |GetUptimes()|. |
| 419 base::TimeTicks last_updated_time_; | 374 base::TimeTicks last_updated_time_; |
| 420 | 375 |
| 421 // Field trial groups that map to Chrome configuration states. | |
| 422 SyntheticTrialGroups synthetic_trial_groups_; | |
| 423 | |
| 424 // List of observers of |synthetic_trial_groups_| changes. | |
| 425 base::ObserverList<variations::SyntheticTrialObserver> | |
| 426 synthetic_trial_observer_list_; | |
| 427 | |
| 428 // Redundant marker to check that we completed our shutdown, and set the | 376 // Redundant marker to check that we completed our shutdown, and set the |
| 429 // exited-cleanly bit in the prefs. | 377 // exited-cleanly bit in the prefs. |
| 430 static ShutdownCleanliness clean_shutdown_status_; | 378 static ShutdownCleanliness clean_shutdown_status_; |
| 431 | 379 |
| 432 FRIEND_TEST_ALL_PREFIXES(MetricsServiceTest, IsPluginProcess); | 380 FRIEND_TEST_ALL_PREFIXES(MetricsServiceTest, IsPluginProcess); |
| 433 FRIEND_TEST_ALL_PREFIXES(MetricsServiceTest, | 381 FRIEND_TEST_ALL_PREFIXES(MetricsServiceTest, |
| 434 PermutedEntropyCacheClearedWhenLowEntropyReset); | 382 PermutedEntropyCacheClearedWhenLowEntropyReset); |
| 435 FRIEND_TEST_ALL_PREFIXES(MetricsServiceTest, RegisterSyntheticTrial); | |
| 436 FRIEND_TEST_ALL_PREFIXES(MetricsServiceTest, | |
| 437 RegisterSyntheticMultiGroupFieldTrial); | |
| 438 FRIEND_TEST_ALL_PREFIXES(MetricsServiceTest, | |
| 439 GetSyntheticFieldTrialActiveGroups); | |
| 440 | 383 |
| 441 base::ThreadChecker thread_checker_; | 384 base::ThreadChecker thread_checker_; |
| 442 | 385 |
| 443 // Weak pointers factory used to post task on different threads. All weak | 386 // Weak pointers factory used to post task on different threads. All weak |
| 444 // pointers managed by this factory have the same lifetime as MetricsService. | 387 // pointers managed by this factory have the same lifetime as MetricsService. |
| 445 base::WeakPtrFactory<MetricsService> self_ptr_factory_; | 388 base::WeakPtrFactory<MetricsService> self_ptr_factory_; |
| 446 | 389 |
| 447 DISALLOW_COPY_AND_ASSIGN(MetricsService); | 390 DISALLOW_COPY_AND_ASSIGN(MetricsService); |
| 448 }; | 391 }; |
| 449 | 392 |
| 450 } // namespace metrics | 393 } // namespace metrics |
| 451 | 394 |
| 452 #endif // COMPONENTS_METRICS_METRICS_SERVICE_H_ | 395 #endif // COMPONENTS_METRICS_METRICS_SERVICE_H_ |
| OLD | NEW |