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_
10 #include "base/location.h"
11 #include "base/macros.h"
12 #include "base/time/time.h"
13 #include "components/sync_driver/data_type_encryption_handler.h"
14 #include "components/sync_driver/sync_service_observer.h"
15 #include "google_apis/gaia/google_service_auth_error.h"
16 #include "sync/internal_api/public/base/model_type.h"
17 #include "sync/internal_api/public/connection_status.h"
19 class GoogleServiceAuthError
;
23 class BaseTransaction
;
28 class SyncSessionSnapshot
;
29 } // namespace sessions
33 namespace sync_driver
{
35 class DataTypeController
;
36 class LocalDeviceInfoProvider
;
37 class OpenTabsUIDelegate
;
39 class SyncService
: public DataTypeEncryptionHandler
{
41 // Used to specify the kind of passphrase with which sync data is encrypted.
43 IMPLICIT
, // The user did not provide a custom passphrase for encryption.
44 // We implicitly use the GAIA password in such cases.
45 EXPLICIT
, // The user selected the "use custom passphrase" radio button
46 // during sync setup and provided a passphrase.
49 // Passed as an argument to RequestStop to control whether or not the sync
50 // backend should clear its data directory when it shuts down. See
51 // RequestStop for more information.
52 enum SyncStopDataFate
{
57 // Status of sync server connection, sync token and token request.
58 struct SyncTokenStatus
{
62 // Sync server connection status reported by sync backend.
63 base::Time connection_status_update_time
;
64 syncer::ConnectionStatus connection_status
;
66 // Times when OAuth2 access token is requested and received.
67 base::Time token_request_time
;
68 base::Time token_receive_time
;
70 // Error returned by OAuth2TokenService for token request and time when
71 // next request is scheduled.
72 GoogleServiceAuthError last_get_token_error
;
73 base::Time next_token_request_time
;
76 ~SyncService() override
{}
78 // Whether sync is enabled by user or not. This does not necessarily mean
79 // that sync is currently running (due to delayed startup, unrecoverable
80 // errors, or shutdown). See IsSyncActive below for checking whether sync
81 // is actually running.
82 virtual bool HasSyncSetupCompleted() const = 0;
84 // Whether sync is allowed to start. Command line flags, platform-level
85 // overrides, and account-level overrides are examples of reasons this
87 virtual bool IsSyncAllowed() const = 0;
89 // Returns true if sync is fully initialized and active. This implies that
90 // an initial configuration has successfully completed, although there may
91 // be datatype specific, auth, or other transient errors. To see which
92 // datetypes are actually syncing, see GetActiveTypes() below.
93 // Note that if sync is in backup or rollback mode, IsSyncActive() will be
95 virtual bool IsSyncActive() const = 0;
97 // Get the set of current active data types (those chosen or configured by
98 // the user which have not also encountered a runtime error).
99 // Note that if the Sync engine is in the middle of a configuration, this
100 // will the the empty set. Once the configuration completes the set will
102 virtual syncer::ModelTypeSet
GetActiveDataTypes() const = 0;
104 // Adds/removes an observer. SyncService does not take ownership of the
106 virtual void AddObserver(SyncServiceObserver
* observer
) = 0;
107 virtual void RemoveObserver(SyncServiceObserver
* observer
) = 0;
109 // Returns true if |observer| has already been added as an observer.
110 virtual bool HasObserver(const SyncServiceObserver
* observer
) const = 0;
112 // ---------------------------------------------------------------------------
113 // TODO(sync): The methods below were pulled from ProfileSyncService, and
114 // should be evaluated to see if they should stay.
116 // Called when a datatype (SyncableService) has a need for sync to start
117 // ASAP, presumably because a local change event has occurred but we're
118 // still in deferred start mode, meaning the SyncableService hasn't been
119 // told to MergeDataAndStartSyncing yet.
120 virtual void OnDataTypeRequestsSyncStartup(syncer::ModelType type
) = 0;
122 // Returns true if sync is allowed, requested, and the user is logged in.
123 // (being logged in does not mean that tokens are available - tokens may
124 // be missing because they have not loaded yet, or because they were deleted
125 // due to http://crbug.com/121755).
126 virtual bool CanSyncStart() const = 0;
128 // Stops sync at the user's request. |data_fate| controls whether the sync
129 // backend should clear its data directory when it shuts down. Generally
130 // KEEP_DATA is used when the user just stops sync, and CLEAR_DATA is used
131 // when they sign out of the profile entirely.
132 virtual void RequestStop(SyncStopDataFate data_fate
) = 0;
134 // The user requests that sync start. This only actually starts sync if
135 // IsSyncAllowed is true and the user is signed in. Once sync starts,
136 // other things such as HasSyncSetupCompleted being false can still prevent
137 // it from moving into the "active" state.
138 virtual void RequestStart() = 0;
140 // Returns the set of types which are preferred for enabling. This is a
141 // superset of the active types (see GetActiveDataTypes()).
142 virtual syncer::ModelTypeSet
GetPreferredDataTypes() const = 0;
144 // Called when a user chooses which data types to sync as part of the sync
145 // setup wizard. |sync_everything| represents whether they chose the
146 // "keep everything synced" option; if true, |chosen_types| will be ignored
147 // and all data types will be synced. |sync_everything| means "sync all
148 // current and future data types."
149 virtual void OnUserChoseDatatypes(bool sync_everything
,
150 syncer::ModelTypeSet chosen_types
) = 0;
152 // Called whe Sync has been setup by the user and can be started.
153 virtual void SetSyncSetupCompleted() = 0;
155 // Returns true if initial sync setup is in progress (does not return true
156 // if the user is customizing sync after already completing setup once).
157 // SyncService uses this to determine if it's OK to start syncing, or if the
158 // user is still setting up the initial sync configuration.
159 virtual bool FirstSetupInProgress() const = 0;
161 // Called by the UI to notify the SyncService that UI is visible so it will
162 // not start syncing. This tells sync whether it's safe to start downloading
163 // data types yet (we don't start syncing until after sync setup is complete).
164 // The UI calls this as soon as any part of the signin wizard is displayed
165 // (even just the login UI).
166 // If |setup_in_progress| is false, this also kicks the sync engine to ensure
167 // that data download starts. In this case, |ReconfigureDatatypeManager| will
169 virtual void SetSetupInProgress(bool setup_in_progress
) = 0;
172 virtual bool setup_in_progress() const = 0;
174 // Whether the data types active for the current mode have finished
176 virtual bool ConfigurationDone() const = 0;
178 virtual const GoogleServiceAuthError
& GetAuthError() const = 0;
179 virtual bool HasUnrecoverableError() const = 0;
181 // Returns true if the SyncBackendHost has told us it's ready to accept
182 // changes. This should only be used for sync's internal configuration logic
183 // (such as deciding when to prompt for an encryption passphrase).
184 virtual bool backend_initialized() const = 0;
186 // Return the active OpenTabsUIDelegate. If sessions is not enabled or not
187 // currently syncing, returns nullptr.
188 virtual OpenTabsUIDelegate
* GetOpenTabsUIDelegate() = 0;
190 // Returns true if OnPassphraseRequired has been called for decryption and
191 // we have an encrypted data type enabled.
192 virtual bool IsPassphraseRequiredForDecryption() const = 0;
194 // Returns the time the current explicit passphrase (if any), was set.
195 // If no secondary passphrase is in use, or no time is available, returns an
197 virtual base::Time
GetExplicitPassphraseTime() const = 0;
199 // Returns true if a secondary (explicit) passphrase is being used. It is not
200 // legal to call this method before the backend is initialized.
201 virtual bool IsUsingSecondaryPassphrase() const = 0;
203 // Turns on encryption for all data. Callers must call OnUserChoseDatatypes()
204 // after calling this to force the encryption to occur.
205 virtual void EnableEncryptEverything() = 0;
207 // Returns true if we are currently set to encrypt all the sync data.
208 virtual bool EncryptEverythingEnabled() const = 0;
210 // Asynchronously sets the passphrase to |passphrase| for encryption. |type|
211 // specifies whether the passphrase is a custom passphrase or the GAIA
212 // password being reused as a passphrase.
213 // TODO(atwilson): Change this so external callers can only set an EXPLICIT
214 // passphrase with this API.
215 virtual void SetEncryptionPassphrase(const std::string
& passphrase
,
216 PassphraseType type
) = 0;
218 // Asynchronously decrypts pending keys using |passphrase|. Returns false
219 // immediately if the passphrase could not be used to decrypt a locally cached
220 // copy of encrypted keys; returns true otherwise.
221 virtual bool SetDecryptionPassphrase(const std::string
& passphrase
)
222 WARN_UNUSED_RESULT
= 0;
224 // Checks whether the Cryptographer is ready to encrypt and decrypt updates
225 // for sensitive data types. Caller must be holding a
226 // syncapi::BaseTransaction to ensure thread safety.
227 virtual bool IsCryptographerReady(
228 const syncer::BaseTransaction
* trans
) const = 0;
230 // TODO(akalin): This is called mostly by ModelAssociators and
231 // tests. Figure out how to pass the handle to the ModelAssociators
232 // directly, figure out how to expose this to tests, and remove this
234 virtual syncer::UserShare
* GetUserShare() const = 0;
236 // Returns DeviceInfo provider for the local device.
237 virtual LocalDeviceInfoProvider
* GetLocalDeviceInfoProvider() const = 0;
239 // Registers a data type controller with the sync service. This
240 // makes the data type controller available for use, it does not
241 // enable or activate the synchronization of the data type (see
242 // ActivateDataType). Takes ownership of the pointer.
243 virtual void RegisterDataTypeController(
244 DataTypeController
* data_type_controller
) = 0;
246 // Called to re-enable a type disabled by DisableDatatype(..). Note, this does
247 // not change the preferred state of a datatype, and is not persisted across
249 virtual void ReenableDatatype(syncer::ModelType type
) = 0;
251 // TODO(zea): Remove these and have the dtc's call directly into the SBH.
252 virtual void DeactivateDataType(syncer::ModelType type
) = 0;
254 // Return sync token status.
255 virtual SyncTokenStatus
GetSyncTokenStatus() const = 0;
257 // Get a description of the sync status for displaying in the user interface.
258 virtual std::string
QuerySyncStatusSummaryString() = 0;
260 // Initializes a struct of status indicators with data from the backend.
261 // Returns false if the backend was not available for querying; in that case
262 // the struct will be filled with default data.
263 virtual bool QueryDetailedSyncStatus(syncer::SyncStatus
* result
) = 0;
265 // Returns a user-friendly string form of last synced time (in minutes).
266 virtual base::string16
GetLastSyncedTimeString() const = 0;
268 // Returns a human readable string describing backend initialization state.
269 virtual std::string
GetBackendInitializationStateString() const = 0;
271 virtual syncer::sessions::SyncSessionSnapshot
GetLastSessionSnapshot()
274 // Returns a ListValue indicating the status of all registered types.
277 // [ {"name": <name>, "value": <value>, "status": <status> }, ... ]
278 // where <name> is a type's name, <value> is a string providing details for
279 // the type's status, and <status> is one of "error", "warning" or "ok"
280 // depending on the type's current status.
282 // This function is used by about_sync_util.cc to help populate the about:sync
283 // page. It returns a ListValue rather than a DictionaryValue in part to make
284 // it easier to iterate over its elements when constructing that page.
285 virtual base::Value
* GetTypeStatusMap() const = 0;
287 virtual const GURL
& sync_service_url() const = 0;
289 virtual std::string
unrecoverable_error_message() const = 0;
290 virtual tracked_objects::Location
unrecoverable_error_location() const = 0;
296 DISALLOW_COPY_AND_ASSIGN(SyncService
);
299 } // namespace sync_driver
301 #endif // COMPONENTS_SYNC_DRIVER_SYNC_SERVICE_H_