Home | History | Annotate | Download | only in sync_driver
      1 // Copyright (c) 2012 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 COMPONENTS_SYNC_DRIVER_SYNC_PREFS_H_
      6 #define COMPONENTS_SYNC_DRIVER_SYNC_PREFS_H_
      7 
      8 #include "base/basictypes.h"
      9 #include "base/compiler_specific.h"
     10 #include "base/memory/weak_ptr.h"
     11 #include "base/observer_list.h"
     12 #include "base/prefs/pref_member.h"
     13 #include "base/threading/non_thread_safe.h"
     14 #include "base/time/time.h"
     15 #include "sync/internal_api/public/base/model_type.h"
     16 #include "sync/notifier/invalidation_state_tracker.h"
     17 
     18 class PrefService;
     19 class ProfileIOData;
     20 
     21 namespace user_prefs {
     22 class PrefRegistrySyncable;
     23 }
     24 
     25 namespace sync_driver {
     26 
     27 class SyncPrefObserver {
     28  public:
     29   // Called whenever the pref that controls whether sync is managed
     30   // changes.
     31   virtual void OnSyncManagedPrefChange(bool is_sync_managed) = 0;
     32 
     33  protected:
     34   virtual ~SyncPrefObserver();
     35 };
     36 
     37 // SyncPrefs is a helper class that manages getting, setting, and
     38 // persisting global sync preferences.  It is not thread-safe, and
     39 // lives on the UI thread.
     40 //
     41 // TODO(akalin): Some classes still read the prefs directly.  Consider
     42 // passing down a pointer to SyncPrefs to them.  A list of files:
     43 //
     44 //   profile_sync_service_startup_unittest.cc
     45 //   profile_sync_service.cc
     46 //   sync_setup_flow.cc
     47 //   sync_setup_wizard.cc
     48 //   sync_setup_wizard_unittest.cc
     49 //   two_client_preferences_sync_test.cc
     50 class SyncPrefs : NON_EXPORTED_BASE(public base::NonThreadSafe),
     51                   public base::SupportsWeakPtr<SyncPrefs> {
     52  public:
     53   // |pref_service| may not be NULL.
     54   // Does not take ownership of |pref_service|.
     55   explicit SyncPrefs(PrefService* pref_service);
     56 
     57   // For testing.
     58   SyncPrefs();
     59 
     60   virtual ~SyncPrefs();
     61 
     62   static void RegisterProfilePrefs(user_prefs::PrefRegistrySyncable* registry);
     63 
     64   void AddSyncPrefObserver(SyncPrefObserver* sync_pref_observer);
     65   void RemoveSyncPrefObserver(SyncPrefObserver* sync_pref_observer);
     66 
     67   // Clears important sync preferences.
     68   void ClearPreferences();
     69 
     70   // Getters and setters for global sync prefs.
     71 
     72   bool HasSyncSetupCompleted() const;
     73   void SetSyncSetupCompleted();
     74 
     75   bool SyncHasAuthError() const;
     76   void SetSyncAuthError(bool error);
     77 
     78   bool IsStartSuppressed() const;
     79   void SetStartSuppressed(bool is_suppressed);
     80 
     81   base::Time GetLastSyncedTime() const;
     82   void SetLastSyncedTime(base::Time time);
     83 
     84   bool HasKeepEverythingSynced() const;
     85   void SetKeepEverythingSynced(bool keep_everything_synced);
     86 
     87   // The returned set is guaranteed to be a subset of
     88   // |registered_types|.  Returns |registered_types| directly if
     89   // HasKeepEverythingSynced() is true.
     90   syncer::ModelTypeSet GetPreferredDataTypes(
     91       syncer::ModelTypeSet registered_types) const;
     92   // |preferred_types| should be a subset of |registered_types|.  All
     93   // types in |preferred_types| are marked preferred, and all types in
     94   // |registered_types| \ |preferred_types| are marked not preferred.
     95   // Changes are still made to the prefs even if
     96   // HasKeepEverythingSynced() is true, but won't be visible until
     97   // SetKeepEverythingSynced(false) is called.
     98   void SetPreferredDataTypes(syncer::ModelTypeSet registered_types,
     99                              syncer::ModelTypeSet preferred_types);
    100 
    101   // This pref is set outside of sync.
    102   bool IsManaged() const;
    103 
    104   // Use this encryption bootstrap token if we're using an explicit passphrase.
    105   std::string GetEncryptionBootstrapToken() const;
    106   void SetEncryptionBootstrapToken(const std::string& token);
    107 
    108   // Use this keystore bootstrap token if we're not using an explicit
    109   // passphrase.
    110   std::string GetKeystoreEncryptionBootstrapToken() const;
    111   void SetKeystoreEncryptionBootstrapToken(const std::string& token);
    112 
    113   // Use this for the unique machine tag used for session sync.
    114   std::string GetSyncSessionsGUID() const;
    115   void SetSyncSessionsGUID(const std::string& guid);
    116 
    117   // Maps |data_type| to its corresponding preference name.
    118   static const char* GetPrefNameForDataType(syncer::ModelType data_type);
    119 
    120 #if defined(OS_CHROMEOS)
    121   // Use this spare bootstrap token only when setting up sync for the first
    122   // time.
    123   std::string GetSpareBootstrapToken() const;
    124   void SetSpareBootstrapToken(const std::string& token);
    125 #endif
    126 
    127   // Merges the given set of types with the set of acknowledged types.
    128   void AcknowledgeSyncedTypes(syncer::ModelTypeSet types);
    129 
    130   // Get/Set number of rollback attempts allowed.
    131   virtual int GetRemainingRollbackTries() const;
    132   virtual void SetRemainingRollbackTries(int times);
    133 
    134   // Get/set/clear first sync time of current user. Used to roll back browsing
    135   // data later when user signs out.
    136   base::Time GetFirstSyncTime() const;
    137   void SetFirstSyncTime(base::Time time);
    138   void ClearFirstSyncTime();
    139 
    140   // For testing.
    141 
    142   void SetManagedForTest(bool is_managed);
    143   syncer::ModelTypeSet GetAcknowledgeSyncedTypesForTest() const;
    144 
    145  private:
    146   void RegisterPrefGroups();
    147 
    148   static void RegisterDataTypePreferredPref(
    149       user_prefs::PrefRegistrySyncable* prefs,
    150       syncer::ModelType type,
    151       bool is_preferred);
    152   bool GetDataTypePreferred(syncer::ModelType type) const;
    153   void SetDataTypePreferred(syncer::ModelType type, bool is_preferred);
    154 
    155   // Returns a ModelTypeSet based on |types| expanded to include pref groups
    156   // (see |pref_groups_|), but as a subset of |registered_types|.
    157   syncer::ModelTypeSet ResolvePrefGroups(syncer::ModelTypeSet registered_types,
    158                                          syncer::ModelTypeSet types) const;
    159 
    160   void OnSyncManagedPrefChanged();
    161 
    162   // May be NULL.
    163   PrefService* const pref_service_;
    164 
    165   ObserverList<SyncPrefObserver> sync_pref_observers_;
    166 
    167   // The preference that controls whether sync is under control by
    168   // configuration management.
    169   BooleanPrefMember pref_sync_managed_;
    170 
    171   // Groups of prefs that always have the same value as a "master" pref.
    172   // For example, the APPS group has {APP_NOTIFICATIONS, APP_SETTINGS}
    173   // (as well as APPS, but that is implied), so
    174   //   pref_groups_[syncer::APPS] =       { syncer::APP_NOTIFICATIONS,
    175   //                                          syncer::APP_SETTINGS }
    176   //   pref_groups_[syncer::EXTENSIONS] = { syncer::EXTENSION_SETTINGS }
    177   // etc.
    178   typedef std::map<syncer::ModelType, syncer::ModelTypeSet> PrefGroupsMap;
    179   PrefGroupsMap pref_groups_;
    180 
    181   DISALLOW_COPY_AND_ASSIGN(SyncPrefs);
    182 };
    183 
    184 }  // namespace browser_sync
    185 
    186 #endif  // COMPONENTS_SYNC_DRIVER_SYNC_PREFS_H_
    187