| 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" | |
| 14 #include "base/prefs/pref_change_registrar.h" | |
| 15 #include "chrome/browser/prefs/synced_pref_change_registrar.h" | 13 #include "chrome/browser/prefs/synced_pref_change_registrar.h" |
| 16 #include "chrome/browser/profiles/profile.h" | 14 #include "chrome/browser/profiles/profile.h" |
| 17 #include "components/browser_context_keyed_service/browser_context_keyed_service
.h" | 15 #include "components/browser_context_keyed_service/browser_context_keyed_service
.h" |
| 18 #include "components/browser_context_keyed_service/browser_context_keyed_service
_factory.h" | 16 #include "components/browser_context_keyed_service/browser_context_keyed_service
_factory.h" |
| 19 | 17 |
| 20 class PrefRegistrySimple; | |
| 21 | |
| 22 // PrefMetricsService is responsible for recording prefs-related UMA stats. | 18 // PrefMetricsService is responsible for recording prefs-related UMA stats. |
| 23 class PrefMetricsService : public BrowserContextKeyedService { | 19 class PrefMetricsService : public BrowserContextKeyedService { |
| 24 public: | 20 public: |
| 25 explicit PrefMetricsService(Profile* profile); | 21 explicit PrefMetricsService(Profile* profile); |
| 26 virtual ~PrefMetricsService(); | 22 virtual ~PrefMetricsService(); |
| 27 | 23 |
| 28 class Factory : public BrowserContextKeyedServiceFactory { | 24 class Factory : public BrowserContextKeyedServiceFactory { |
| 29 public: | 25 public: |
| 30 static Factory* GetInstance(); | 26 static Factory* GetInstance(); |
| 31 static PrefMetricsService* GetForProfile(Profile* profile); | 27 static PrefMetricsService* GetForProfile(Profile* profile); |
| 32 private: | 28 private: |
| 33 friend struct DefaultSingletonTraits<Factory>; | 29 friend struct DefaultSingletonTraits<Factory>; |
| 34 | 30 |
| 35 Factory(); | 31 Factory(); |
| 36 virtual ~Factory(); | 32 virtual ~Factory(); |
| 37 | 33 |
| 38 // BrowserContextKeyedServiceFactory implementation | 34 // BrowserContextKeyedServiceFactory implementation |
| 39 virtual BrowserContextKeyedService* BuildServiceInstanceFor( | 35 virtual BrowserContextKeyedService* BuildServiceInstanceFor( |
| 40 content::BrowserContext* profile) const OVERRIDE; | 36 content::BrowserContext* profile) const OVERRIDE; |
| 41 virtual bool ServiceIsCreatedWithBrowserContext() const OVERRIDE; | 37 virtual bool ServiceIsCreatedWithBrowserContext() const OVERRIDE; |
| 42 virtual bool ServiceIsNULLWhileTesting() const OVERRIDE; | 38 virtual bool ServiceIsNULLWhileTesting() const OVERRIDE; |
| 43 virtual content::BrowserContext* GetBrowserContextToUse( | 39 virtual content::BrowserContext* GetBrowserContextToUse( |
| 44 content::BrowserContext* context) const OVERRIDE; | 40 content::BrowserContext* context) const OVERRIDE; |
| 45 }; | 41 }; |
| 46 | 42 |
| 47 // Registers preferences in local state. | |
| 48 static void RegisterPrefs(PrefRegistrySimple* registry); | |
| 49 | |
| 50 private: | 43 private: |
| 51 friend class PrefMetricsServiceTest; | 44 // Use a map to convert domains to their histogram identifiers. Ids are |
| 45 // defined in tools/metrics/histograms/histograms.xml and (usually) also in |
| 46 // chrome/browser/search_engines/prepopulated_engines.json. |
| 47 typedef std::map<std::string, int> DomainIdMap; |
| 52 | 48 |
| 53 // Function to log a Value to a histogram | 49 // Function to log a Value to a histogram |
| 54 typedef base::Callback<void(const std::string&, const Value*)> | 50 typedef base::Callback<void(const std::string&, const Value*)> |
| 55 LogHistogramValueCallback; | 51 LogHistogramValueCallback; |
| 56 | 52 |
| 57 // For unit testing only. | |
| 58 PrefMetricsService(Profile* profile, | |
| 59 PrefService* local_settings, | |
| 60 const std::string& device_id, | |
| 61 const char** tracked_pref_paths, | |
| 62 int tracked_pref_path_count); | |
| 63 | |
| 64 // Record prefs state on browser context creation. | 53 // Record prefs state on browser context creation. |
| 65 void RecordLaunchPrefs(); | 54 void RecordLaunchPrefs(); |
| 66 | 55 |
| 67 // Register callbacks for synced pref changes. | 56 // Register callbacks for synced pref changes. |
| 68 void RegisterSyncedPrefObservers(); | 57 void RegisterSyncedPrefObservers(); |
| 69 | 58 |
| 70 // Registers a histogram logging callback for a synced pref change. | 59 // Registers a histogram logging callback for a synced pref change. |
| 71 void AddPrefObserver(const std::string& path, | 60 void AddPrefObserver(const std::string& path, |
| 72 const std::string& histogram_name_prefix, | 61 const std::string& histogram_name_prefix, |
| 73 const LogHistogramValueCallback& callback); | 62 const LogHistogramValueCallback& callback); |
| (...skipping 12 matching lines...) Expand all Loading... |
| 86 void LogIntegerPrefChange(int boundary_value, | 75 void LogIntegerPrefChange(int boundary_value, |
| 87 const std::string& histogram_name, | 76 const std::string& histogram_name, |
| 88 const Value* value); | 77 const Value* value); |
| 89 | 78 |
| 90 // Callback for a list pref change. Each item in the list | 79 // Callback for a list pref change. Each item in the list |
| 91 // is logged using the given histogram callback. | 80 // is logged using the given histogram callback. |
| 92 void LogListPrefChange(const LogHistogramValueCallback& item_callback, | 81 void LogListPrefChange(const LogHistogramValueCallback& item_callback, |
| 93 const std::string& histogram_name, | 82 const std::string& histogram_name, |
| 94 const Value* value); | 83 const Value* value); |
| 95 | 84 |
| 96 // Callback to receive a unique device_id. | |
| 97 void GetDeviceIdCallback(const std::string& device_id); | |
| 98 | |
| 99 // Checks the tracked preferences against their last known values and reports | |
| 100 // any discrepancies. This must be called after |device_id| has been set. | |
| 101 void CheckTrackedPreferences(); | |
| 102 | |
| 103 // Updates the hash of the tracked preference in local state. This must be | |
| 104 // called after |device_id| has been set. | |
| 105 void UpdateTrackedPreference(const char* path); | |
| 106 | |
| 107 // Removes the tracked preference from local state. Returns 'true' iff. the | |
| 108 // value was present. | |
| 109 bool RemoveTrackedPreference(const char* path); | |
| 110 | |
| 111 // Gets the path to the preference value hash in local state. | |
| 112 std::string GetHashedPrefPath(const char* path); | |
| 113 | |
| 114 // Computes an MD5 hash for the given preference value. | |
| 115 std::string GetHashedPrefValue(const char* path, const base::Value* value); | |
| 116 | |
| 117 void InitializePrefObservers(); | |
| 118 | |
| 119 Profile* profile_; | 85 Profile* profile_; |
| 120 PrefService* prefs_; | |
| 121 PrefService* local_state_; | |
| 122 std::string profile_name_; | |
| 123 std::string pref_hash_seed_; | |
| 124 std::string device_id_; | |
| 125 const char** tracked_pref_paths_; | |
| 126 const int tracked_pref_path_count_; | |
| 127 bool checked_tracked_prefs_; | |
| 128 | |
| 129 PrefChangeRegistrar pref_registrar_; | |
| 130 scoped_ptr<SyncedPrefChangeRegistrar> synced_pref_change_registrar_; | 86 scoped_ptr<SyncedPrefChangeRegistrar> synced_pref_change_registrar_; |
| 131 | |
| 132 base::WeakPtrFactory<PrefMetricsService> weak_factory_; | |
| 133 | |
| 134 DISALLOW_COPY_AND_ASSIGN(PrefMetricsService); | |
| 135 }; | 87 }; |
| 136 | 88 |
| 137 #endif // CHROME_BROWSER_PREFS_PREF_METRICS_SERVICE_H_ | 89 #endif // CHROME_BROWSER_PREFS_PREF_METRICS_SERVICE_H_ |
| OLD | NEW |