| 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/basictypes.h" |
| 12 #include "base/callback.h" |
| 13 #include "base/compiler_specific.h" |
| 11 #include "base/memory/scoped_ptr.h" | 14 #include "base/memory/scoped_ptr.h" |
| 12 #include "base/memory/singleton.h" | 15 #include "base/memory/singleton.h" |
| 13 #include "base/memory/weak_ptr.h" | 16 #include "base/memory/weak_ptr.h" |
| 14 #include "base/prefs/pref_change_registrar.h" | 17 #include "base/prefs/pref_change_registrar.h" |
| 15 #include "chrome/browser/prefs/synced_pref_change_registrar.h" | 18 #include "chrome/browser/prefs/synced_pref_change_registrar.h" |
| 16 #include "chrome/browser/profiles/profile.h" | |
| 17 #include "components/browser_context_keyed_service/browser_context_keyed_service
.h" | 19 #include "components/browser_context_keyed_service/browser_context_keyed_service
.h" |
| 18 #include "components/browser_context_keyed_service/browser_context_keyed_service
_factory.h" | 20 #include "components/browser_context_keyed_service/browser_context_keyed_service
_factory.h" |
| 19 | 21 |
| 22 namespace base { |
| 23 class Value; |
| 24 } // namespace base |
| 25 |
| 26 namespace content { |
| 27 class BrowserContext; |
| 28 } // namespace content |
| 29 |
| 30 class Profile; |
| 20 class PrefRegistrySimple; | 31 class PrefRegistrySimple; |
| 32 class PrefService; |
| 21 | 33 |
| 22 // PrefMetricsService is responsible for recording prefs-related UMA stats. | 34 // PrefMetricsService is responsible for recording prefs-related UMA stats. |
| 23 class PrefMetricsService : public BrowserContextKeyedService { | 35 class PrefMetricsService : public BrowserContextKeyedService { |
| 24 public: | 36 public: |
| 25 enum HashedPrefStyle { | |
| 26 HASHED_PREF_STYLE_NEW, | |
| 27 HASHED_PREF_STYLE_DEPRECATED, | |
| 28 }; | |
| 29 | |
| 30 explicit PrefMetricsService(Profile* profile); | 37 explicit PrefMetricsService(Profile* profile); |
| 31 virtual ~PrefMetricsService(); | 38 virtual ~PrefMetricsService(); |
| 32 | 39 |
| 33 class Factory : public BrowserContextKeyedServiceFactory { | 40 class Factory : public BrowserContextKeyedServiceFactory { |
| 34 public: | 41 public: |
| 35 static Factory* GetInstance(); | 42 static Factory* GetInstance(); |
| 36 static PrefMetricsService* GetForProfile(Profile* profile); | 43 static PrefMetricsService* GetForProfile(Profile* profile); |
| 37 private: | 44 private: |
| 38 friend struct DefaultSingletonTraits<Factory>; | 45 friend struct DefaultSingletonTraits<Factory>; |
| 39 | 46 |
| 40 Factory(); | 47 Factory(); |
| 41 virtual ~Factory(); | 48 virtual ~Factory(); |
| 42 | 49 |
| 43 // BrowserContextKeyedServiceFactory implementation | 50 // BrowserContextKeyedServiceFactory implementation |
| 44 virtual BrowserContextKeyedService* BuildServiceInstanceFor( | 51 virtual BrowserContextKeyedService* BuildServiceInstanceFor( |
| 45 content::BrowserContext* profile) const OVERRIDE; | 52 content::BrowserContext* profile) const OVERRIDE; |
| 46 virtual bool ServiceIsCreatedWithBrowserContext() const OVERRIDE; | 53 virtual bool ServiceIsCreatedWithBrowserContext() const OVERRIDE; |
| 47 virtual bool ServiceIsNULLWhileTesting() const OVERRIDE; | 54 virtual bool ServiceIsNULLWhileTesting() const OVERRIDE; |
| 48 virtual content::BrowserContext* GetBrowserContextToUse( | 55 virtual content::BrowserContext* GetBrowserContextToUse( |
| 49 content::BrowserContext* context) const OVERRIDE; | 56 content::BrowserContext* context) const OVERRIDE; |
| 50 }; | 57 }; |
| 51 | 58 |
| 52 // Registers preferences in local state. | |
| 53 static void RegisterPrefs(PrefRegistrySimple* registry); | |
| 54 | |
| 55 private: | 59 private: |
| 56 friend class PrefMetricsServiceTest; | 60 friend class PrefMetricsServiceTest; |
| 57 | 61 |
| 58 // Function to log a Value to a histogram | 62 // Function to log a Value to a histogram |
| 59 typedef base::Callback<void(const std::string&, const Value*)> | 63 typedef base::Callback<void(const std::string&, const base::Value*)> |
| 60 LogHistogramValueCallback; | 64 LogHistogramValueCallback; |
| 61 | 65 |
| 62 // For unit testing only. | 66 // For unit testing only. |
| 63 PrefMetricsService(Profile* profile, | 67 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 | 68 |
| 69 // Record prefs state on browser context creation. | 69 // Record prefs state on browser context creation. |
| 70 void RecordLaunchPrefs(); | 70 void RecordLaunchPrefs(); |
| 71 | 71 |
| 72 // Register callbacks for synced pref changes. | 72 // Register callbacks for synced pref changes. |
| 73 void RegisterSyncedPrefObservers(); | 73 void RegisterSyncedPrefObservers(); |
| 74 | 74 |
| 75 // Registers a histogram logging callback for a synced pref change. | 75 // Registers a histogram logging callback for a synced pref change. |
| 76 void AddPrefObserver(const std::string& path, | 76 void AddPrefObserver(const std::string& path, |
| 77 const std::string& histogram_name_prefix, | 77 const std::string& histogram_name_prefix, |
| 78 const LogHistogramValueCallback& callback); | 78 const LogHistogramValueCallback& callback); |
| 79 | 79 |
| 80 // Generic callback to observe a synced pref change. | 80 // Generic callback to observe a synced pref change. |
| 81 void OnPrefChanged(const std::string& histogram_name_prefix, | 81 void OnPrefChanged(const std::string& histogram_name_prefix, |
| 82 const LogHistogramValueCallback& callback, | 82 const LogHistogramValueCallback& callback, |
| 83 const std::string& path, | 83 const std::string& path, |
| 84 bool from_sync); | 84 bool from_sync); |
| 85 | 85 |
| 86 // Callback for a boolean pref change histogram. | 86 // Callback for a boolean pref change histogram. |
| 87 void LogBooleanPrefChange(const std::string& histogram_name, | 87 void LogBooleanPrefChange(const std::string& histogram_name, |
| 88 const Value* value); | 88 const base::Value* value); |
| 89 | 89 |
| 90 // Callback for an integer pref change histogram. | 90 // Callback for an integer pref change histogram. |
| 91 void LogIntegerPrefChange(int boundary_value, | 91 void LogIntegerPrefChange(int boundary_value, |
| 92 const std::string& histogram_name, | 92 const std::string& histogram_name, |
| 93 const Value* value); | 93 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 | 94 |
| 116 Profile* profile_; | 95 Profile* profile_; |
| 117 PrefService* prefs_; | 96 PrefService* prefs_; |
| 118 PrefService* local_state_; | 97 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 | 98 |
| 129 PrefChangeRegistrar pref_registrar_; | 99 PrefChangeRegistrar pref_registrar_; |
| 130 scoped_ptr<SyncedPrefChangeRegistrar> synced_pref_change_registrar_; | 100 scoped_ptr<SyncedPrefChangeRegistrar> synced_pref_change_registrar_; |
| 131 | 101 |
| 132 base::WeakPtrFactory<PrefMetricsService> weak_factory_; | 102 base::WeakPtrFactory<PrefMetricsService> weak_factory_; |
| 133 | 103 |
| 134 DISALLOW_COPY_AND_ASSIGN(PrefMetricsService); | 104 DISALLOW_COPY_AND_ASSIGN(PrefMetricsService); |
| 135 }; | 105 }; |
| 136 | 106 |
| 137 #endif // CHROME_BROWSER_PREFS_PREF_METRICS_SERVICE_H_ | 107 #endif // CHROME_BROWSER_PREFS_PREF_METRICS_SERVICE_H_ |
| OLD | NEW |