| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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 #ifndef CHROME_BROWSER_PREFS_PREF_METRICS_SERVICE_H_ | 5 #ifndef CHROME_BROWSER_PREFS_PREF_METRICS_SERVICE_H_ |
| 6 #define CHROME_BROWSER_PREFS_PREF_METRICS_SERVICE_H_ | 6 #define CHROME_BROWSER_PREFS_PREF_METRICS_SERVICE_H_ |
| 7 | 7 |
| 8 #include <map> | 8 #include <map> |
| 9 #include <string> | 9 #include <string> |
| 10 | 10 |
| 11 #include "base/memory/scoped_ptr.h" | 11 #include "base/memory/scoped_ptr.h" |
| 12 #include "base/memory/singleton.h" | 12 #include "base/memory/singleton.h" |
| 13 #include "base/memory/weak_ptr.h" | 13 #include "base/memory/weak_ptr.h" |
| 14 #include "base/prefs/pref_change_registrar.h" | 14 #include "base/prefs/pref_change_registrar.h" |
| 15 #include "chrome/browser/prefs/synced_pref_change_registrar.h" | 15 #include "chrome/browser/prefs/synced_pref_change_registrar.h" |
| 16 #include "chrome/browser/profiles/profile.h" | 16 #include "chrome/browser/profiles/profile.h" |
| 17 #include "components/browser_context_keyed_service/browser_context_keyed_service
.h" | 17 #include "components/browser_context_keyed_service/browser_context_keyed_service
.h" |
| 18 #include "components/browser_context_keyed_service/browser_context_keyed_service
_factory.h" | 18 #include "components/browser_context_keyed_service/browser_context_keyed_service
_factory.h" |
| 19 | 19 |
| 20 class PrefRegistrySimple; | 20 class PrefRegistrySimple; |
| 21 | 21 |
| 22 // PrefMetricsService is responsible for recording prefs-related UMA stats. | 22 // PrefMetricsService is responsible for recording prefs-related UMA stats. |
| 23 class PrefMetricsService : public BrowserContextKeyedService { | 23 class PrefMetricsService : public BrowserContextKeyedService { |
| 24 public: | 24 public: |
| 25 enum HashedPrefStyle { | |
| 26 HASHED_PREF_STYLE_NEW, | |
| 27 HASHED_PREF_STYLE_DEPRECATED, | |
| 28 }; | |
| 29 | |
| 30 explicit PrefMetricsService(Profile* profile); | 25 explicit PrefMetricsService(Profile* profile); |
| 31 virtual ~PrefMetricsService(); | 26 virtual ~PrefMetricsService(); |
| 32 | 27 |
| 33 class Factory : public BrowserContextKeyedServiceFactory { | 28 class Factory : public BrowserContextKeyedServiceFactory { |
| 34 public: | 29 public: |
| 35 static Factory* GetInstance(); | 30 static Factory* GetInstance(); |
| 36 static PrefMetricsService* GetForProfile(Profile* profile); | 31 static PrefMetricsService* GetForProfile(Profile* profile); |
| 37 private: | 32 private: |
| 38 friend struct DefaultSingletonTraits<Factory>; | 33 friend struct DefaultSingletonTraits<Factory>; |
| 39 | 34 |
| 40 Factory(); | 35 Factory(); |
| 41 virtual ~Factory(); | 36 virtual ~Factory(); |
| 42 | 37 |
| 43 // BrowserContextKeyedServiceFactory implementation | 38 // BrowserContextKeyedServiceFactory implementation |
| 44 virtual BrowserContextKeyedService* BuildServiceInstanceFor( | 39 virtual BrowserContextKeyedService* BuildServiceInstanceFor( |
| 45 content::BrowserContext* profile) const OVERRIDE; | 40 content::BrowserContext* profile) const OVERRIDE; |
| 46 virtual bool ServiceIsCreatedWithBrowserContext() const OVERRIDE; | 41 virtual bool ServiceIsCreatedWithBrowserContext() const OVERRIDE; |
| 47 virtual bool ServiceIsNULLWhileTesting() const OVERRIDE; | 42 virtual bool ServiceIsNULLWhileTesting() const OVERRIDE; |
| 48 virtual content::BrowserContext* GetBrowserContextToUse( | 43 virtual content::BrowserContext* GetBrowserContextToUse( |
| 49 content::BrowserContext* context) const OVERRIDE; | 44 content::BrowserContext* context) const OVERRIDE; |
| 50 }; | 45 }; |
| 51 | 46 |
| 52 // Registers preferences in local state. | |
| 53 static void RegisterPrefs(PrefRegistrySimple* registry); | |
| 54 | |
| 55 private: | 47 private: |
| 56 friend class PrefMetricsServiceTest; | 48 friend class PrefMetricsServiceTest; |
| 57 | 49 |
| 58 // Function to log a Value to a histogram | 50 // Function to log a Value to a histogram |
| 59 typedef base::Callback<void(const std::string&, const Value*)> | 51 typedef base::Callback<void(const std::string&, const base::Value*)> |
| 60 LogHistogramValueCallback; | 52 LogHistogramValueCallback; |
| 61 | 53 |
| 62 // For unit testing only. | 54 // For unit testing only. |
| 63 PrefMetricsService(Profile* profile, | 55 PrefMetricsService(Profile* profile, PrefService* local_settings); |
| 64 PrefService* local_settings, | |
| 65 const std::string& device_id, | |
| 66 const char** tracked_pref_paths, | |
| 67 int tracked_pref_path_count); | |
| 68 | 56 |
| 69 // Record prefs state on browser context creation. | 57 // Record prefs state on browser context creation. |
| 70 void RecordLaunchPrefs(); | 58 void RecordLaunchPrefs(); |
| 71 | 59 |
| 72 // Register callbacks for synced pref changes. | 60 // Register callbacks for synced pref changes. |
| 73 void RegisterSyncedPrefObservers(); | 61 void RegisterSyncedPrefObservers(); |
| 74 | 62 |
| 75 // Registers a histogram logging callback for a synced pref change. | 63 // Registers a histogram logging callback for a synced pref change. |
| 76 void AddPrefObserver(const std::string& path, | 64 void AddPrefObserver(const std::string& path, |
| 77 const std::string& histogram_name_prefix, | 65 const std::string& histogram_name_prefix, |
| 78 const LogHistogramValueCallback& callback); | 66 const LogHistogramValueCallback& callback); |
| 79 | 67 |
| 80 // Generic callback to observe a synced pref change. | 68 // Generic callback to observe a synced pref change. |
| 81 void OnPrefChanged(const std::string& histogram_name_prefix, | 69 void OnPrefChanged(const std::string& histogram_name_prefix, |
| 82 const LogHistogramValueCallback& callback, | 70 const LogHistogramValueCallback& callback, |
| 83 const std::string& path, | 71 const std::string& path, |
| 84 bool from_sync); | 72 bool from_sync); |
| 85 | 73 |
| 86 // Callback for a boolean pref change histogram. | 74 // Callback for a boolean pref change histogram. |
| 87 void LogBooleanPrefChange(const std::string& histogram_name, | 75 void LogBooleanPrefChange(const std::string& histogram_name, |
| 88 const Value* value); | 76 const base::Value* value); |
| 89 | 77 |
| 90 // Callback for an integer pref change histogram. | 78 // Callback for an integer pref change histogram. |
| 91 void LogIntegerPrefChange(int boundary_value, | 79 void LogIntegerPrefChange(int boundary_value, |
| 92 const std::string& histogram_name, | 80 const std::string& histogram_name, |
| 93 const Value* value); | 81 const base::Value* value); |
| 94 | |
| 95 // Callback to receive a unique device_id. | |
| 96 void GetDeviceIdCallback(const std::string& device_id); | |
| 97 | |
| 98 // Checks the tracked preferences against their last known values and reports | |
| 99 // any discrepancies. This must be called after |device_id| has been set. | |
| 100 void CheckTrackedPreferences(); | |
| 101 | |
| 102 // Updates the hash of the tracked preference in local state. This must be | |
| 103 // called after |device_id| has been set. | |
| 104 void UpdateTrackedPreference(const char* path); | |
| 105 | |
| 106 // Computes an MD5 hash for the given preference value. |value| can be | |
| 107 // NULL which will result in the unique hash representing NULL for the pref | |
| 108 // at |path|. | |
| 109 std::string GetHashedPrefValue( | |
| 110 const char* path, | |
| 111 const base::Value* value, | |
| 112 HashedPrefStyle desired_style); | |
| 113 | |
| 114 void InitializePrefObservers(); | |
| 115 | 82 |
| 116 Profile* profile_; | 83 Profile* profile_; |
| 117 PrefService* prefs_; | 84 PrefService* prefs_; |
| 118 PrefService* local_state_; | 85 PrefService* local_state_; |
| 119 std::string profile_name_; | |
| 120 std::string pref_hash_seed_; | |
| 121 std::string device_id_; | |
| 122 const char** tracked_pref_paths_; | |
| 123 const int tracked_pref_path_count_; | |
| 124 | |
| 125 // TODO(gab): preprocessor define this member out on builds that don't use | |
| 126 // DCHECKs (http://crbug.com/322713). | |
| 127 bool checked_tracked_prefs_; | |
| 128 | 86 |
| 129 PrefChangeRegistrar pref_registrar_; | 87 PrefChangeRegistrar pref_registrar_; |
| 130 scoped_ptr<SyncedPrefChangeRegistrar> synced_pref_change_registrar_; | 88 scoped_ptr<SyncedPrefChangeRegistrar> synced_pref_change_registrar_; |
| 131 | 89 |
| 132 base::WeakPtrFactory<PrefMetricsService> weak_factory_; | 90 base::WeakPtrFactory<PrefMetricsService> weak_factory_; |
| 133 | 91 |
| 134 DISALLOW_COPY_AND_ASSIGN(PrefMetricsService); | 92 DISALLOW_COPY_AND_ASSIGN(PrefMetricsService); |
| 135 }; | 93 }; |
| 136 | 94 |
| 137 #endif // CHROME_BROWSER_PREFS_PREF_METRICS_SERVICE_H_ | 95 #endif // CHROME_BROWSER_PREFS_PREF_METRICS_SERVICE_H_ |
| OLD | NEW |