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   enum HashedPrefStyle {
     26     HASHED_PREF_STYLE_NEW,
     27     HASHED_PREF_STYLE_DEPRECATED,
     28   };
     29 
     30   explicit PrefMetricsService(Profile* profile);
     31   virtual ~PrefMetricsService();
     32 
     33   class Factory : public BrowserContextKeyedServiceFactory {
     34    public:
     35     static Factory* GetInstance();
     36     static PrefMetricsService* GetForProfile(Profile* profile);
     37    private:
     38     friend struct DefaultSingletonTraits<Factory>;
     39 
     40     Factory();
     41     virtual ~Factory();
     42 
     43     // BrowserContextKeyedServiceFactory implementation
     44     virtual BrowserContextKeyedService* BuildServiceInstanceFor(
     45         content::BrowserContext* profile) const OVERRIDE;
     46     virtual bool ServiceIsCreatedWithBrowserContext() const OVERRIDE;
     47     virtual bool ServiceIsNULLWhileTesting() const OVERRIDE;
     48     virtual content::BrowserContext* GetBrowserContextToUse(
     49         content::BrowserContext* context) const OVERRIDE;
     50   };
     51 
     52   // Registers preferences in local state.
     53   static void RegisterPrefs(PrefRegistrySimple* registry);
     54 
     55  private:
     56   friend class PrefMetricsServiceTest;
     57 
     58   // Function to log a Value to a histogram
     59   typedef base::Callback<void(const std::string&, const Value*)>
     60       LogHistogramValueCallback;
     61 
     62   // For unit testing only.
     63   PrefMetricsService(Profile* profile,
     64                      PrefService* local_settings,
     65                      const std::string& device_id,
     66                      const char** tracked_pref_paths,
     67                      int tracked_pref_path_count);
     68 
     69   // Record prefs state on browser context creation.
     70   void RecordLaunchPrefs();
     71 
     72   // Register callbacks for synced pref changes.
     73   void RegisterSyncedPrefObservers();
     74 
     75   // Registers a histogram logging callback for a synced pref change.
     76   void AddPrefObserver(const std::string& path,
     77                        const std::string& histogram_name_prefix,
     78                        const LogHistogramValueCallback& callback);
     79 
     80   // Generic callback to observe a synced pref change.
     81   void OnPrefChanged(const std::string& histogram_name_prefix,
     82                      const LogHistogramValueCallback& callback,
     83                      const std::string& path,
     84                      bool from_sync);
     85 
     86   // Callback for a boolean pref change histogram.
     87   void LogBooleanPrefChange(const std::string& histogram_name,
     88                             const Value* value);
     89 
     90   // Callback for an integer pref change histogram.
     91   void LogIntegerPrefChange(int boundary_value,
     92                             const std::string& histogram_name,
     93                             const 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 
    116   Profile* profile_;
    117   PrefService* prefs_;
    118   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 
    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