Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(347)

Unified Diff: chrome/browser/metrics/metrics_state_manager.h

Issue 256143006: Refactor MetricsStateManager class out of MetricsService. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src/
Patch Set: Created 6 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: chrome/browser/metrics/metrics_state_manager.h
===================================================================
--- chrome/browser/metrics/metrics_state_manager.h (revision 0)
+++ chrome/browser/metrics/metrics_state_manager.h (working copy)
@@ -0,0 +1,121 @@
+// Copyright 2014 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#ifndef CHROME_BROWSER_METRICS_METRICS_STATE_MANAGER_H_
+#define CHROME_BROWSER_METRICS_METRICS_STATE_MANAGER_H_
+
+#include <string>
+
+#include "base/basictypes.h"
+#include "base/gtest_prod_util.h"
+#include "base/memory/scoped_ptr.h"
+#include "base/metrics/field_trial.h"
+
+class PrefService;
+class PrefRegistrySimple;
+
+namespace metrics {
+
+class ClonedInstallDetector;
+
+// Responsible for managing MetricsService state prefs, specifically the UMA
+// client id and low entropy source.
+class MetricsStateManager {
+ public:
+ explicit MetricsStateManager(PrefService* local_state);
+ virtual ~MetricsStateManager();
+
+ // Returns true if the user opted in to sending metric reports.
+ bool IsMetricsReportingEnabled();
+
+ // Returns the client ID for this client, or the empty string if the user is
+ // not opted in to metrics reporting.
+ const std::string& client_id() const { return client_id_; }
+
+ // Force the client ID to be generated. This is useful in case it's needed
+ // before recording.
+ void ForceClientIdCreation();
jwd 2014/05/01 15:51:14 Is this needed public? Since it's always called on
Alexei Svitkine (slow) 2014/05/01 18:01:45 It's public because it's also called from MetricsS
+
+ // Check if this install was cloned or imaged from another machine. If a
+ // clone is detected, reset the client id and low entropy source. This
+ // should not be called more than once.
+ void CheckForClonedInstall();
+
+ // Returns the preferred entropy provider used to seed persistent activities
+ // based on whether or not metrics reporting will be permitted on this client.
+ // The caller must determine if metrics reporting will be enabled for this
+ // client and pass that state in as |reporting_will_be_enabled|.
jwd 2014/05/01 15:51:14 Update comment re: |reporting_will_be_enabled|
Alexei Svitkine (slow) 2014/05/01 18:01:45 Done.
+ //
+ // If |reporting_will_be_enabled| is true, this method returns an entropy
+ // provider that has a high source of entropy, partially based on the client
+ // ID. Otherwise, an entropy provider that is based on a low entropy source
+ // is returned.
+ //
+ // Note that this reporting state can not be checked by reporting_active()
+ // because this method may need to be called before the MetricsService needs
+ // to be started.
+ scoped_ptr<const base::FieldTrial::EntropyProvider> CreateEntropyProvider();
+
+ // Registers local state prefs used by this class.
+ static void RegisterPrefs(PrefRegistrySimple* registry);
+
+ private:
+ FRIEND_TEST_ALL_PREFIXES(MetricsStateManagerTest, EntropySourceUsed_Low);
+ FRIEND_TEST_ALL_PREFIXES(MetricsStateManagerTest, EntropySourceUsed_High);
+ FRIEND_TEST_ALL_PREFIXES(MetricsStateManagerTest, LowEntropySource0NotReset);
+ FRIEND_TEST_ALL_PREFIXES(MetricsStateManagerTest,
+ PermutedEntropyCacheClearedWhenLowEntropyReset);
+ FRIEND_TEST_ALL_PREFIXES(MetricsStateManagerTest, ResetMetricsIDs);
+
+ // Designates which entropy source was returned from this MetricsService.
+ // This is used for testing to validate that we return the correct source
+ // depending on the state of the service.
+ enum EntropySourceReturned {
+ LAST_ENTROPY_NONE,
+ LAST_ENTROPY_LOW,
+ LAST_ENTROPY_HIGH,
+ };
+
+ // Returns the low entropy source for this client. This is a random value
+ // that is non-identifying amongst browser clients. This method will
+ // generate the entropy source value if it has not been called before.
+ int GetLowEntropySource();
+
+ // Returns the first entropy source that was returned by this service since
+ // start up, or NONE if neither was returned yet. This is exposed for testing
+ // only.
+ EntropySourceReturned entropy_source_returned() const {
+ return entropy_source_returned_;
+ }
+
+ // Reset the client id and low entropy source if the kMetricsResetMetricIDs
+ // pref is true.
+ void ResetMetricsIDsIfNecessary();
+
+ PrefService* local_state_;
+
+ // TODO(asvitkine): Update this when the user toggles this state.
+ bool metrics_reporting_enabled_;
+
+ // The identifier that's sent to the server with the log reports.
+ std::string client_id_;
+
+ // The non-identifying low entropy source value.
+ int low_entropy_source_;
+
+ // The last entropy source returned by this service, used for testing.
+ EntropySourceReturned entropy_source_returned_;
+
+ // Set to true when |ResetMetricsIDsIfNecessary| is called for the first time.
+ // This prevents multiple resets within the same Chrome session.
+ bool metrics_ids_reset_check_performed_;
+
+ scoped_ptr<ClonedInstallDetector> cloned_install_detector_;
+
+ DISALLOW_COPY_AND_ASSIGN(MetricsStateManager);
+};
+
+} // namespace metrics
+
+#endif // CHROME_BROWSER_METRICS_METRICS_STATE_MANAGER_H_

Powered by Google App Engine
This is Rietveld 408576698