summaryrefslogtreecommitdiffstats
path: root/chrome/browser/sync/sync_prefs.h
blob: 460633ab3d0d675092795ff9879ace78f5aabb97 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
// Copyright (c) 2011 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef CHROME_BROWSER_SYNC_SYNC_PREFS_H_
#define CHROME_BROWSER_SYNC_SYNC_PREFS_H_
#pragma once

#include "base/basictypes.h"
#include "base/compiler_specific.h"
#include "base/memory/weak_ptr.h"
#include "base/observer_list.h"
#include "base/threading/non_thread_safe.h"
#include "base/time.h"
#include "chrome/browser/prefs/pref_member.h"
#include "chrome/browser/sync/notifier/invalidation_version_tracker.h"
#include "chrome/browser/sync/syncable/model_type.h"
#include "content/public/browser/notification_observer.h"

class PrefService;

namespace browser_sync {

class SyncPrefObserver {
 public:
  // Called whenever the pref that controls whether sync is managed
  // changes.
  virtual void OnSyncManagedPrefChange(bool is_sync_managed) = 0;

 protected:
  virtual ~SyncPrefObserver();
};

// SyncPrefs is a helper class that manages getting, setting, and
// persisting global sync preferences.  It is not thread-safe, and
// lives on the UI thread.
//
// TODO(akalin): Some classes still read the prefs directly.  Consider
// passing down a pointer to SyncPrefs to them.  A list of files:
//
//   profile_sync_service_startup_unittest.cc
//   profile_sync_service.cc
//   sync_setup_flow.cc
//   sync_setup_wizard.cc
//   sync_setup_wizard_unittest.cc
//   two_client_preferences_sync_test.cc
class SyncPrefs : public base::SupportsWeakPtr<SyncPrefs>,
                  public sync_notifier::InvalidationVersionTracker,
                  public content::NotificationObserver {
 public:
  // |pref_service| may be NULL (for unit tests), but in that case no
  // setter methods should be called.  Does not take ownership of
  // |pref_service|.
  explicit SyncPrefs(PrefService* pref_service);

  virtual ~SyncPrefs();

  void AddSyncPrefObserver(SyncPrefObserver* sync_pref_observer);
  void RemoveSyncPrefObserver(SyncPrefObserver* sync_pref_observer);

  // Clears important sync preferences.
  void ClearPreferences();

  // Getters and setters for global sync prefs.

  bool HasSyncSetupCompleted() const;
  void SetSyncSetupCompleted();

  bool IsStartSuppressed() const;
  void SetStartSuppressed(bool is_suppressed);

  std::string GetGoogleServicesUsername() const;

  base::Time GetLastSyncedTime() const;
  void SetLastSyncedTime(base::Time time);

  bool HasKeepEverythingSynced() const;
  void SetKeepEverythingSynced(bool keep_everything_synced);

  // The returned set is guaranteed to be a subset of
  // |registered_types|.  Returns |registered_types| directly if
  // HasKeepEverythingSynced() is true.
  syncable::ModelTypeSet GetPreferredDataTypes(
      const syncable::ModelTypeSet& registered_types) const;
  // |preferred_types| should be a subset of |registered_types|.  All
  // types in |preferred_types| are marked preferred, and all types in
  // |registered_types| \ |preferred_types| are marked not preferred.
  // Changes are still made to the prefs even if
  // HasKeepEverythingSynced() is true, but won't be visible until
  // SetKeepEverythingSynced(false) is called.
  void SetPreferredDataTypes(
    const syncable::ModelTypeSet& registered_types,
    const syncable::ModelTypeSet& preferred_types);

  // This pref is set outside of sync.
  bool IsManaged() const;

  std::string GetEncryptionBootstrapToken() const;
  void SetEncryptionBootstrapToken(const std::string& token);

  // InvalidationVersionTracker implementation.
  virtual sync_notifier::InvalidationVersionMap
      GetAllMaxVersions() const OVERRIDE;
  virtual void SetMaxVersion(syncable::ModelType model_type,
                             int64 max_version) OVERRIDE;

  // Merges the given set of types with the set of acknowledged types.
  void AcknowledgeSyncedTypes(const syncable::ModelTypeSet& types);

  // content::NotificationObserver implementation.
  virtual void Observe(int type,
                       const content::NotificationSource& source,
                       const content::NotificationDetails& details) OVERRIDE;

  // For testing.

  void SetManagedForTest(bool is_managed);
  syncable::ModelTypeSet GetAcknowledgeSyncedTypesForTest() const;

 private:
  void RegisterPreferences();

  void RegisterDataTypePreferredPref(
      syncable::ModelType type, bool is_preferred);
  bool GetDataTypePreferred(syncable::ModelType type) const;
  void SetDataTypePreferred(syncable::ModelType type, bool is_preferred);

  base::NonThreadSafe non_thread_safe_;

  // May be NULL.
  PrefService* const pref_service_;

  ObserverList<SyncPrefObserver> sync_pref_observers_;

  // The preference that controls whether sync is under control by
  // configuration management.
  BooleanPrefMember pref_sync_managed_;

  DISALLOW_COPY_AND_ASSIGN(SyncPrefs);
};

}  // namespace browser_sync

#endif  // CHROME_BROWSER_SYNC_SYNC_PREFS_H_