blob: 6f13ee9573588c9db1bc298dccd677926c0f1ef2 [file] [log] [blame]
miu@chromium.org39076642014-05-05 20:32:551// 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
blundell@chromium.org16a30912014-06-04 00:20:045#ifndef COMPONENTS_METRICS_METRICS_STATE_MANAGER_H_
6#define COMPONENTS_METRICS_METRICS_STATE_MANAGER_H_
miu@chromium.org39076642014-05-05 20:32:557
8#include <string>
9
10#include "base/basictypes.h"
isherman@chromium.org3c70256f2014-05-22 03:02:1211#include "base/callback.h"
miu@chromium.org39076642014-05-05 20:32:5512#include "base/gtest_prod_util.h"
13#include "base/memory/scoped_ptr.h"
14#include "base/metrics/field_trial.h"
15
16class PrefService;
17class PrefRegistrySimple;
18
19namespace metrics {
20
21class ClonedInstallDetector;
22
23// Responsible for managing MetricsService state prefs, specifically the UMA
24// client id and low entropy source. Code outside the metrics directory should
25// not be instantiating or using this class directly.
26class MetricsStateManager {
27 public:
28 virtual ~MetricsStateManager();
29
30 // Returns true if the user opted in to sending metric reports.
miu@chromium.org39076642014-05-05 20:32:5531 bool IsMetricsReportingEnabled();
32
33 // Returns the client ID for this client, or the empty string if the user is
34 // not opted in to metrics reporting.
35 const std::string& client_id() const { return client_id_; }
36
37 // Forces the client ID to be generated. This is useful in case it's needed
38 // before recording.
39 void ForceClientIdCreation();
40
41 // Checks if this install was cloned or imaged from another machine. If a
42 // clone is detected, resets the client id and low entropy source. This
43 // should not be called more than once.
blundell@chromium.org61b0d482014-05-20 14:49:1044 void CheckForClonedInstall(
45 scoped_refptr<base::SingleThreadTaskRunner> task_runner);
miu@chromium.org39076642014-05-05 20:32:5546
47 // Returns the preferred entropy provider used to seed persistent activities
48 // based on whether or not metrics reporting is permitted on this client.
49 //
50 // If metrics reporting is enabled, this method returns an entropy provider
51 // that has a high source of entropy, partially based on the client ID.
52 // Otherwise, it returns an entropy provider that is based on a low entropy
53 // source.
54 scoped_ptr<const base::FieldTrial::EntropyProvider> CreateEntropyProvider();
55
56 // Creates the MetricsStateManager, enforcing that only a single instance
57 // of the class exists at a time. Returns NULL if an instance exists already.
isherman@chromium.org3c70256f2014-05-22 03:02:1258 static scoped_ptr<MetricsStateManager> Create(
59 PrefService* local_state,
60 const base::Callback<bool(void)>& is_reporting_enabled_callback);
miu@chromium.org39076642014-05-05 20:32:5561
62 // Registers local state prefs used by this class.
63 static void RegisterPrefs(PrefRegistrySimple* registry);
64
65 private:
66 FRIEND_TEST_ALL_PREFIXES(MetricsStateManagerTest, EntropySourceUsed_Low);
67 FRIEND_TEST_ALL_PREFIXES(MetricsStateManagerTest, EntropySourceUsed_High);
68 FRIEND_TEST_ALL_PREFIXES(MetricsStateManagerTest, LowEntropySource0NotReset);
69 FRIEND_TEST_ALL_PREFIXES(MetricsStateManagerTest,
70 PermutedEntropyCacheClearedWhenLowEntropyReset);
71 FRIEND_TEST_ALL_PREFIXES(MetricsStateManagerTest, ResetMetricsIDs);
72
73 // Designates which entropy source was returned from this class.
74 // This is used for testing to validate that we return the correct source
75 // depending on the state of the service.
76 enum EntropySourceType {
77 ENTROPY_SOURCE_NONE,
78 ENTROPY_SOURCE_LOW,
79 ENTROPY_SOURCE_HIGH,
80 };
81
isherman@chromium.org3c70256f2014-05-22 03:02:1282 // Creates the MetricsStateManager with the given |local_state|. Calls
83 // |is_reporting_enabled_callback| to query whether metrics reporting is
84 // enabled. Clients should instead use Create(), which enforces a single
85 // instance of this class is alive at any given time.
86 MetricsStateManager(
87 PrefService* local_state,
88 const base::Callback<bool(void)>& is_reporting_enabled_callback);
miu@chromium.org39076642014-05-05 20:32:5589
90 // Returns the low entropy source for this client. This is a random value
91 // that is non-identifying amongst browser clients. This method will
92 // generate the entropy source value if it has not been called before.
93 int GetLowEntropySource();
94
95 // Returns the first entropy source that was returned by this service since
96 // start up, or NONE if neither was returned yet. This is exposed for testing
97 // only.
98 EntropySourceType entropy_source_returned() const {
99 return entropy_source_returned_;
100 }
101
102 // Reset the client id and low entropy source if the kMetricsResetMetricIDs
103 // pref is true.
104 void ResetMetricsIDsIfNecessary();
105
106 // Whether an instance of this class exists. Used to enforce that there aren't
107 // multiple instances of this class at a given time.
108 static bool instance_exists_;
109
110 // Weak pointer to the local state prefs store.
isherman@chromium.org3c70256f2014-05-22 03:02:12111 PrefService* const local_state_;
112
113 const base::Callback<bool(void)> is_reporting_enabled_callback_;
miu@chromium.org39076642014-05-05 20:32:55114
115 // The identifier that's sent to the server with the log reports.
116 std::string client_id_;
117
118 // The non-identifying low entropy source value.
119 int low_entropy_source_;
120
121 // The last entropy source returned by this service, used for testing.
122 EntropySourceType entropy_source_returned_;
123
124 scoped_ptr<ClonedInstallDetector> cloned_install_detector_;
125
126 DISALLOW_COPY_AND_ASSIGN(MetricsStateManager);
127};
128
129} // namespace metrics
130
blundell@chromium.org16a30912014-06-04 00:20:04131#endif // COMPONENTS_METRICS_METRICS_STATE_MANAGER_H_