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 CHROME_BROWSER_PREFS_PREF_SERVICE_SYNCABLE_H_
6#define CHROME_BROWSER_PREFS_PREF_SERVICE_SYNCABLE_H_
7
8#include "base/prefs/pref_service.h"
9#include "chrome/browser/prefs/pref_model_associator.h"
10#include "chrome/browser/prefs/synced_pref_observer.h"
11#include "components/pref_registry/pref_registry_syncable.h"
12
13class PrefServiceSyncableObserver;
14class Profile;
15
16namespace syncer {
17class SyncableService;
18}
19
20// A PrefService that can be synced. Users are forced to declare
21// whether preferences are syncable or not when registering them to
22// this PrefService.
23class PrefServiceSyncable : public PrefService {
24 public:
25  // PrefServiceSyncable is a PrefService with added integration for
26  // sync, and knowledge of how to create an incognito
27  // PrefService. For code that does not need to know about the sync
28  // integration, you should use only the plain PrefService type.
29  //
30  // For this reason, Profile does not expose an accessor for the
31  // PrefServiceSyncable type. Instead, you can use the utilities
32  // below to retrieve the PrefServiceSyncable (or its incognito
33  // version) from a Profile.
34  static PrefServiceSyncable* FromProfile(Profile* profile);
35  static PrefServiceSyncable* IncognitoFromProfile(Profile* profile);
36
37  // You may wish to use PrefServiceFactory or one of its subclasses
38  // for simplified construction.
39  PrefServiceSyncable(
40      PrefNotifierImpl* pref_notifier,
41      PrefValueStore* pref_value_store,
42      PersistentPrefStore* user_prefs,
43      user_prefs::PrefRegistrySyncable* pref_registry,
44      base::Callback<void(PersistentPrefStore::PrefReadError)>
45          read_error_callback,
46      bool async);
47  virtual ~PrefServiceSyncable();
48
49  // Creates an incognito copy of the pref service that shares most pref stores
50  // but uses a fresh non-persistent overlay for the user pref store and an
51  // individual extension pref store (to cache the effective extension prefs for
52  // incognito windows).
53  PrefServiceSyncable* CreateIncognitoPrefService(
54      PrefStore* incognito_extension_prefs);
55
56  // Returns true if preferences state has synchronized with the remote
57  // preferences. If true is returned it can be assumed the local preferences
58  // has applied changes from the remote preferences. The two may not be
59  // identical if a change is in flight (from either side).
60  //
61  // TODO(albertb): Given that we now support priority preferences, callers of
62  // this method are likely better off making the preferences they care about
63  // into priority preferences and calling IsPrioritySyncing().
64  bool IsSyncing();
65
66  // Returns true if priority preferences state has synchronized with the remote
67  // priority preferences.
68  bool IsPrioritySyncing();
69
70  // Returns true if the pref under the given name is pulled down from sync.
71  // Note this does not refer to SYNCABLE_PREF.
72  bool IsPrefSynced(const std::string& name) const;
73
74  void AddObserver(PrefServiceSyncableObserver* observer);
75  void RemoveObserver(PrefServiceSyncableObserver* observer);
76
77  // TODO(zea): Have PrefServiceSyncable implement
78  // syncer::SyncableService directly.
79  syncer::SyncableService* GetSyncableService(const syncer::ModelType& type);
80
81  // Do not call this after having derived an incognito or per tab pref service.
82  virtual void UpdateCommandLinePrefStore(PrefStore* cmd_line_store) OVERRIDE;
83
84  void AddSyncedPrefObserver(const std::string& name,
85                             SyncedPrefObserver* observer);
86  void RemoveSyncedPrefObserver(const std::string& name,
87                                SyncedPrefObserver* observer);
88
89 private:
90  friend class PrefModelAssociator;
91
92  void AddRegisteredSyncablePreference(
93      const char* path,
94      const user_prefs::PrefRegistrySyncable::PrefSyncStatus sync_status);
95
96  // Invoked internally when the IsSyncing() state changes.
97  void OnIsSyncingChanged();
98
99  // Process a local preference change. This can trigger new SyncChanges being
100  // sent to the syncer.
101  void ProcessPrefChange(const std::string& name);
102
103  // Whether CreateIncognitoPrefService() has been called to create a
104  // "forked" PrefService.
105  bool pref_service_forked_;
106
107  PrefModelAssociator pref_sync_associator_;
108  PrefModelAssociator priority_pref_sync_associator_;
109
110  ObserverList<PrefServiceSyncableObserver> observer_list_;
111
112  DISALLOW_COPY_AND_ASSIGN(PrefServiceSyncable);
113};
114
115#endif  // CHROME_BROWSER_PREFS_PREF_SERVICE_SYNCABLE_H_
116