Implement multiple alternative services per origin.
[chromium-blink-merge.git] / components / sync_driver / sync_service.h
blob5092d25569d51e52f13bc785c4438bb40a606c08
1 // Copyright 2015 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.
5 #ifndef COMPONENTS_SYNC_DRIVER_SYNC_SERVICE_H_
6 #define COMPONENTS_SYNC_DRIVER_SYNC_SERVICE_H_
8 #include <string>
10 #include "base/macros.h"
11 #include "base/time/time.h"
12 #include "components/sync_driver/data_type_encryption_handler.h"
13 #include "components/sync_driver/sync_service_observer.h"
14 #include "sync/internal_api/public/base/model_type.h"
16 class GoogleServiceAuthError;
18 namespace sync_driver {
20 class OpenTabsUIDelegate;
22 class SyncService : public DataTypeEncryptionHandler {
23 public:
24 // Used to specify the kind of passphrase with which sync data is encrypted.
25 enum PassphraseType {
26 IMPLICIT, // The user did not provide a custom passphrase for encryption.
27 // We implicitly use the GAIA password in such cases.
28 EXPLICIT, // The user selected the "use custom passphrase" radio button
29 // during sync setup and provided a passphrase.
32 // Passed as an argument to RequestStop to control whether or not the sync
33 // backend should clear its data directory when it shuts down. See
34 // RequestStop for more information.
35 enum SyncStopDataFate {
36 KEEP_DATA,
37 CLEAR_DATA,
40 ~SyncService() override {}
42 // Whether sync is enabled by user or not. This does not necessarily mean
43 // that sync is currently running (due to delayed startup, unrecoverable
44 // errors, or shutdown). See IsSyncActive below for checking whether sync
45 // is actually running.
46 virtual bool HasSyncSetupCompleted() const = 0;
48 // Whether sync is allowed to start. Command line flags, platform-level
49 // overrides, and account-level overrides are examples of reasons this
50 // might be false.
51 virtual bool IsSyncAllowed() const = 0;
53 // Returns true if sync is fully initialized and active. This implies that
54 // an initial configuration has successfully completed, although there may
55 // be datatype specific, auth, or other transient errors. To see which
56 // datetypes are actually syncing, see GetActiveTypes() below.
57 // Note that if sync is in backup or rollback mode, IsSyncActive() will be
58 // false.
59 virtual bool IsSyncActive() const = 0;
61 // Get the set of current active data types (those chosen or configured by
62 // the user which have not also encountered a runtime error).
63 // Note that if the Sync engine is in the middle of a configuration, this
64 // will the the empty set. Once the configuration completes the set will
65 // be updated.
66 virtual syncer::ModelTypeSet GetActiveDataTypes() const = 0;
68 // Adds/removes an observer. SyncService does not take ownership of the
69 // observer.
70 virtual void AddObserver(SyncServiceObserver* observer) = 0;
71 virtual void RemoveObserver(SyncServiceObserver* observer) = 0;
73 // Returns true if |observer| has already been added as an observer.
74 virtual bool HasObserver(const SyncServiceObserver* observer) const = 0;
76 // ---------------------------------------------------------------------------
77 // TODO(sync): The methods below were pulled from ProfileSyncService, and
78 // should be evaluated to see if they should stay.
80 // Called when a datatype (SyncableService) has a need for sync to start
81 // ASAP, presumably because a local change event has occurred but we're
82 // still in deferred start mode, meaning the SyncableService hasn't been
83 // told to MergeDataAndStartSyncing yet.
84 virtual void OnDataTypeRequestsSyncStartup(syncer::ModelType type) = 0;
86 // Returns true if sync is allowed, requested, and the user is logged in.
87 // (being logged in does not mean that tokens are available - tokens may
88 // be missing because they have not loaded yet, or because they were deleted
89 // due to http://crbug.com/121755).
90 virtual bool CanSyncStart() const = 0;
92 // Stops sync at the user's request. |data_fate| controls whether the sync
93 // backend should clear its data directory when it shuts down. Generally
94 // KEEP_DATA is used when the user just stops sync, and CLEAR_DATA is used
95 // when they sign out of the profile entirely.
96 virtual void RequestStop(SyncStopDataFate data_fate) = 0;
98 // The user requests that sync start. This only actually starts sync if
99 // IsSyncAllowed is true and the user is signed in. Once sync starts,
100 // other things such as HasSyncSetupCompleted being false can still prevent
101 // it from moving into the "active" state.
102 virtual void RequestStart() = 0;
104 // Returns the set of types which are preferred for enabling. This is a
105 // superset of the active types (see GetActiveDataTypes()).
106 virtual syncer::ModelTypeSet GetPreferredDataTypes() const = 0;
108 // Called when a user chooses which data types to sync as part of the sync
109 // setup wizard. |sync_everything| represents whether they chose the
110 // "keep everything synced" option; if true, |chosen_types| will be ignored
111 // and all data types will be synced. |sync_everything| means "sync all
112 // current and future data types."
113 virtual void OnUserChoseDatatypes(bool sync_everything,
114 syncer::ModelTypeSet chosen_types) = 0;
116 // Called whe Sync has been setup by the user and can be started.
117 virtual void SetSyncSetupCompleted() = 0;
119 // Returns true if initial sync setup is in progress (does not return true
120 // if the user is customizing sync after already completing setup once).
121 // SyncService uses this to determine if it's OK to start syncing, or if the
122 // user is still setting up the initial sync configuration.
123 virtual bool FirstSetupInProgress() const = 0;
125 // Called by the UI to notify the SyncService that UI is visible so it will
126 // not start syncing. This tells sync whether it's safe to start downloading
127 // data types yet (we don't start syncing until after sync setup is complete).
128 // The UI calls this as soon as any part of the signin wizard is displayed
129 // (even just the login UI).
130 // If |setup_in_progress| is false, this also kicks the sync engine to ensure
131 // that data download starts. In this case, |ReconfigureDatatypeManager| will
132 // get triggered.
133 virtual void SetSetupInProgress(bool setup_in_progress) = 0;
135 // Used by tests.
136 virtual bool setup_in_progress() const = 0;
138 // Whether the data types active for the current mode have finished
139 // configuration.
140 virtual bool ConfigurationDone() const = 0;
142 virtual const GoogleServiceAuthError& GetAuthError() const = 0;
143 virtual bool HasUnrecoverableError() const = 0;
145 // Returns true if the SyncBackendHost has told us it's ready to accept
146 // changes. This should only be used for sync's internal configuration logic
147 // (such as deciding when to prompt for an encryption passphrase).
148 virtual bool backend_initialized() const = 0;
150 // Return the active OpenTabsUIDelegate. If sessions is not enabled or not
151 // currently syncing, returns nullptr.
152 virtual OpenTabsUIDelegate* GetOpenTabsUIDelegate() = 0;
154 // Returns true if OnPassphraseRequired has been called for decryption and
155 // we have an encrypted data type enabled.
156 virtual bool IsPassphraseRequiredForDecryption() const = 0;
158 // Returns the time the current explicit passphrase (if any), was set.
159 // If no secondary passphrase is in use, or no time is available, returns an
160 // unset base::Time.
161 virtual base::Time GetExplicitPassphraseTime() const = 0;
163 // Returns true if a secondary (explicit) passphrase is being used. It is not
164 // legal to call this method before the backend is initialized.
165 virtual bool IsUsingSecondaryPassphrase() const = 0;
167 // Turns on encryption for all data. Callers must call OnUserChoseDatatypes()
168 // after calling this to force the encryption to occur.
169 virtual void EnableEncryptEverything() = 0;
171 // Asynchronously sets the passphrase to |passphrase| for encryption. |type|
172 // specifies whether the passphrase is a custom passphrase or the GAIA
173 // password being reused as a passphrase.
174 // TODO(atwilson): Change this so external callers can only set an EXPLICIT
175 // passphrase with this API.
176 virtual void SetEncryptionPassphrase(const std::string& passphrase,
177 PassphraseType type) = 0;
179 // Asynchronously decrypts pending keys using |passphrase|. Returns false
180 // immediately if the passphrase could not be used to decrypt a locally cached
181 // copy of encrypted keys; returns true otherwise.
182 virtual bool SetDecryptionPassphrase(const std::string& passphrase)
183 WARN_UNUSED_RESULT = 0;
185 protected:
186 SyncService() {}
188 private:
189 DISALLOW_COPY_AND_ASSIGN(SyncService);
192 } // namespace sync_driver
194 #endif // COMPONENTS_SYNC_DRIVER_SYNC_SERVICE_H_