Home | History | Annotate | Download | only in prefs
      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
      3 // found in the LICENSE file.
      4 
      5 #ifndef CHROME_BROWSER_PREFS_PREF_METRICS_SERVICE_H_
      6 #define CHROME_BROWSER_PREFS_PREF_METRICS_SERVICE_H_
      7 
      8 #include <map>
      9 #include <string>
     10 
     11 #include "base/memory/scoped_ptr.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"
     16 #include "chrome/browser/profiles/profile.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"
     19 
     20 class PrefRegistrySimple;
     21 
     22 // PrefMetricsService is responsible for recording prefs-related UMA stats.
     23 class PrefMetricsService : public BrowserContextKeyedService {
     24  public:
     25   explicit PrefMetricsService(Profile* profile);
     26   virtual ~PrefMetricsService();
     27 
     28   class Factory : public BrowserContextKeyedServiceFactory {
     29    public:
     30     static Factory* GetInstance();
     31     static PrefMetricsService* GetForProfile(Profile* profile);
     32    private:
     33     friend struct DefaultSingletonTraits<Factory>;
     34 
     35     Factory();
     36     virtual ~Factory();
     37 
     38     // BrowserContextKeyedServiceFactory implementation
     39     virtual BrowserContextKeyedService* BuildServiceInstanceFor(
     40         content::BrowserContext* profile) const OVERRIDE;
     41     virtual bool ServiceIsCreatedWithBrowserContext() const OVERRIDE;
     42     virtual bool ServiceIsNULLWhileTesting() const OVERRIDE;
     43     virtual content::BrowserContext* GetBrowserContextToUse(
     44         content::BrowserContext* context) const OVERRIDE;
     45   };
     46 
     47   // Registers preferences in local state.
     48   static void RegisterPrefs(PrefRegistrySimple* registry);
     49 
     50  private:
     51   friend class PrefMetricsServiceTest;
     52 
     53   // Function to log a Value to a histogram
     54   typedef base::Callback<void(const std::string&, const Value*)>
     55       LogHistogramValueCallback;
     56 
     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.
     65   void RecordLaunchPrefs();
     66 
     67   // Register callbacks for synced pref changes.
     68   void RegisterSyncedPrefObservers();
     69 
     70   // Registers a histogram logging callback for a synced pref change.
     71   void AddPrefObserver(const std::string& path,
     72                        const std::string& histogram_name_prefix,
     73                        const LogHistogramValueCallback& callback);
     74 
     75   // Generic callback to observe a synced pref change.
     76   void OnPrefChanged(const std::string& histogram_name_prefix,
     77                      const LogHistogramValueCallback& callback,
     78                      const std::string& path,
     79                      bool from_sync);
     80 
     81   // Callback for a boolean pref change histogram.
     82   void LogBooleanPrefChange(const std::string& histogram_name,
     83                             const Value* value);
     84 
     85   // Callback for an integer pref change histogram.
     86   void LogIntegerPrefChange(int boundary_value,
     87                             const std::string& histogram_name,
     88                             const Value* value);
     89 
     90   // Callback for a list pref change. Each item in the list
     91   // is logged using the given histogram callback.
     92   void LogListPrefChange(const LogHistogramValueCallback& item_callback,
     93                          const std::string& histogram_name,
     94                          const Value* value);
     95 
     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_;
    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_;
    131 
    132   base::WeakPtrFactory<PrefMetricsService> weak_factory_;
    133 
    134   DISALLOW_COPY_AND_ASSIGN(PrefMetricsService);
    135 };
    136 
    137 #endif  // CHROME_BROWSER_PREFS_PREF_METRICS_SERVICE_H_
    138