OLD | NEW |
| (Empty) |
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. | |
4 | |
5 #ifndef COMPONENTS_SYNC_DRIVER_SYNC_SERVICE_H_ | |
6 #define COMPONENTS_SYNC_DRIVER_SYNC_SERVICE_H_ | |
7 | |
8 #include <memory> | |
9 #include <string> | |
10 | |
11 #include "base/callback.h" | |
12 #include "base/location.h" | |
13 #include "base/macros.h" | |
14 #include "base/memory/weak_ptr.h" | |
15 #include "base/time/time.h" | |
16 #include "components/sync/base/model_type.h" | |
17 #include "components/sync/core/connection_status.h" | |
18 #include "components/sync_driver/data_type_encryption_handler.h" | |
19 #include "components/sync_driver/sync_service_observer.h" | |
20 #include "google_apis/gaia/google_service_auth_error.h" | |
21 | |
22 class GoogleServiceAuthError; | |
23 | |
24 namespace browser_sync { | |
25 class ProtocolEventObserver; | |
26 } | |
27 | |
28 namespace syncer { | |
29 | |
30 class BaseTransaction; | |
31 class JsController; | |
32 class TypeDebugInfoObserver; | |
33 struct SyncStatus; | |
34 struct UserShare; | |
35 | |
36 namespace sessions { | |
37 class SyncSessionSnapshot; | |
38 } // namespace sessions | |
39 | |
40 } // namespace syncer | |
41 | |
42 namespace sync_driver { | |
43 | |
44 class DataTypeController; | |
45 class LocalDeviceInfoProvider; | |
46 class OpenTabsUIDelegate; | |
47 class SyncClient; | |
48 | |
49 // UIs that need to prevent Sync startup should hold an instance of this class | |
50 // until the user has finished modifying sync settings. This is not an inner | |
51 // class of SyncService to enable forward declarations. | |
52 class SyncSetupInProgressHandle { | |
53 public: | |
54 // UIs should not construct this directly, but instead call | |
55 // SyncService::GetSetupInProgress(). | |
56 explicit SyncSetupInProgressHandle(base::Closure on_destroy); | |
57 | |
58 ~SyncSetupInProgressHandle(); | |
59 | |
60 private: | |
61 base::Closure on_destroy_; | |
62 }; | |
63 | |
64 class SyncService : public DataTypeEncryptionHandler { | |
65 public: | |
66 // Used to specify the kind of passphrase with which sync data is encrypted. | |
67 enum PassphraseType { | |
68 IMPLICIT, // The user did not provide a custom passphrase for encryption. | |
69 // We implicitly use the GAIA password in such cases. | |
70 EXPLICIT, // The user selected the "use custom passphrase" radio button | |
71 // during sync setup and provided a passphrase. | |
72 }; | |
73 | |
74 // Passed as an argument to RequestStop to control whether or not the sync | |
75 // backend should clear its data directory when it shuts down. See | |
76 // RequestStop for more information. | |
77 enum SyncStopDataFate { | |
78 KEEP_DATA, | |
79 CLEAR_DATA, | |
80 }; | |
81 | |
82 // Status of sync server connection, sync token and token request. | |
83 struct SyncTokenStatus { | |
84 SyncTokenStatus(); | |
85 | |
86 // Sync server connection status reported by sync backend. | |
87 base::Time connection_status_update_time; | |
88 syncer::ConnectionStatus connection_status; | |
89 | |
90 // Times when OAuth2 access token is requested and received. | |
91 base::Time token_request_time; | |
92 base::Time token_receive_time; | |
93 | |
94 // Error returned by OAuth2TokenService for token request and time when | |
95 // next request is scheduled. | |
96 GoogleServiceAuthError last_get_token_error; | |
97 base::Time next_token_request_time; | |
98 }; | |
99 | |
100 ~SyncService() override {} | |
101 | |
102 // Whether sync is enabled by user or not. This does not necessarily mean | |
103 // that sync is currently running (due to delayed startup, unrecoverable | |
104 // errors, or shutdown). See IsSyncActive below for checking whether sync | |
105 // is actually running. | |
106 virtual bool IsFirstSetupComplete() const = 0; | |
107 | |
108 // Whether sync is allowed to start. Command line flags, platform-level | |
109 // overrides, and account-level overrides are examples of reasons this | |
110 // might be false. | |
111 virtual bool IsSyncAllowed() const = 0; | |
112 | |
113 // Returns true if sync is fully initialized and active. This implies that | |
114 // an initial configuration has successfully completed, although there may | |
115 // be datatype specific, auth, or other transient errors. To see which | |
116 // datetypes are actually syncing, see GetActiveTypes() below. | |
117 virtual bool IsSyncActive() const = 0; | |
118 | |
119 // Triggers a GetUpdates call for the specified |types|, pulling any new data | |
120 // from the sync server. | |
121 virtual void TriggerRefresh(const syncer::ModelTypeSet& types) = 0; | |
122 | |
123 // Get the set of current active data types (those chosen or configured by | |
124 // the user which have not also encountered a runtime error). | |
125 // Note that if the Sync engine is in the middle of a configuration, this | |
126 // will the the empty set. Once the configuration completes the set will | |
127 // be updated. | |
128 virtual syncer::ModelTypeSet GetActiveDataTypes() const = 0; | |
129 | |
130 // Returns the SyncClient instance associated with this service. | |
131 virtual SyncClient* GetSyncClient() const = 0; | |
132 | |
133 // Adds/removes an observer. SyncService does not take ownership of the | |
134 // observer. | |
135 virtual void AddObserver(SyncServiceObserver* observer) = 0; | |
136 virtual void RemoveObserver(SyncServiceObserver* observer) = 0; | |
137 | |
138 // Returns true if |observer| has already been added as an observer. | |
139 virtual bool HasObserver(const SyncServiceObserver* observer) const = 0; | |
140 | |
141 // --------------------------------------------------------------------------- | |
142 // TODO(sync): The methods below were pulled from ProfileSyncService, and | |
143 // should be evaluated to see if they should stay. | |
144 | |
145 // Called when a datatype (SyncableService) has a need for sync to start | |
146 // ASAP, presumably because a local change event has occurred but we're | |
147 // still in deferred start mode, meaning the SyncableService hasn't been | |
148 // told to MergeDataAndStartSyncing yet. | |
149 virtual void OnDataTypeRequestsSyncStartup(syncer::ModelType type) = 0; | |
150 | |
151 // Returns true if sync is allowed, requested, and the user is logged in. | |
152 // (being logged in does not mean that tokens are available - tokens may | |
153 // be missing because they have not loaded yet, or because they were deleted | |
154 // due to http://crbug.com/121755). | |
155 virtual bool CanSyncStart() const = 0; | |
156 | |
157 // Stops sync at the user's request. |data_fate| controls whether the sync | |
158 // backend should clear its data directory when it shuts down. Generally | |
159 // KEEP_DATA is used when the user just stops sync, and CLEAR_DATA is used | |
160 // when they sign out of the profile entirely. | |
161 virtual void RequestStop(SyncStopDataFate data_fate) = 0; | |
162 | |
163 // The user requests that sync start. This only actually starts sync if | |
164 // IsSyncAllowed is true and the user is signed in. Once sync starts, | |
165 // other things such as IsFirstSetupComplete being false can still prevent | |
166 // it from moving into the "active" state. | |
167 virtual void RequestStart() = 0; | |
168 | |
169 // Returns the set of types which are preferred for enabling. This is a | |
170 // superset of the active types (see GetActiveDataTypes()). | |
171 virtual syncer::ModelTypeSet GetPreferredDataTypes() const = 0; | |
172 | |
173 // Called when a user chooses which data types to sync. |sync_everything| | |
174 // represents whether they chose the "keep everything synced" option; if | |
175 // true, |chosen_types| will be ignored and all data types will be synced. | |
176 // |sync_everything| means "sync all current and future data types." | |
177 // |chosen_types| must be a subset of syncer::UserSelectableTypes(). | |
178 virtual void OnUserChoseDatatypes(bool sync_everything, | |
179 syncer::ModelTypeSet chosen_types) = 0; | |
180 | |
181 // Called whe Sync has been setup by the user and can be started. | |
182 virtual void SetFirstSetupComplete() = 0; | |
183 | |
184 // Returns true if initial sync setup is in progress (does not return true | |
185 // if the user is customizing sync after already completing setup once). | |
186 // SyncService uses this to determine if it's OK to start syncing, or if the | |
187 // user is still setting up the initial sync configuration. | |
188 virtual bool IsFirstSetupInProgress() const = 0; | |
189 | |
190 // Called by the UI to notify the SyncService that UI is visible so it will | |
191 // not start syncing. This tells sync whether it's safe to start downloading | |
192 // data types yet (we don't start syncing until after sync setup is complete). | |
193 // The UI calls this and holds onto the instance for as long as any part of | |
194 // the signin wizard is displayed (even just the login UI). | |
195 // When the last outstanding handle is deleted, this kicks off the sync engine | |
196 // to ensure that data download starts. In this case, | |
197 // |ReconfigureDatatypeManager| will get triggered. | |
198 virtual std::unique_ptr<SyncSetupInProgressHandle> | |
199 GetSetupInProgressHandle() = 0; | |
200 | |
201 // Used by tests. | |
202 virtual bool IsSetupInProgress() const = 0; | |
203 | |
204 // Whether the data types active for the current mode have finished | |
205 // configuration. | |
206 virtual bool ConfigurationDone() const = 0; | |
207 | |
208 virtual const GoogleServiceAuthError& GetAuthError() const = 0; | |
209 virtual bool HasUnrecoverableError() const = 0; | |
210 | |
211 // Returns true if the SyncBackendHost has told us it's ready to accept | |
212 // changes. This should only be used for sync's internal configuration logic | |
213 // (such as deciding when to prompt for an encryption passphrase). | |
214 virtual bool IsBackendInitialized() const = 0; | |
215 | |
216 // Return the active OpenTabsUIDelegate. If sessions is not enabled or not | |
217 // currently syncing, returns nullptr. | |
218 virtual OpenTabsUIDelegate* GetOpenTabsUIDelegate() = 0; | |
219 | |
220 // Returns true if OnPassphraseRequired has been called for decryption and | |
221 // we have an encrypted data type enabled. | |
222 virtual bool IsPassphraseRequiredForDecryption() const = 0; | |
223 | |
224 // Returns the time the current explicit passphrase (if any), was set. | |
225 // If no secondary passphrase is in use, or no time is available, returns an | |
226 // unset base::Time. | |
227 virtual base::Time GetExplicitPassphraseTime() const = 0; | |
228 | |
229 // Returns true if a secondary (explicit) passphrase is being used. It is not | |
230 // legal to call this method before the backend is initialized. | |
231 virtual bool IsUsingSecondaryPassphrase() const = 0; | |
232 | |
233 // Turns on encryption for all data. Callers must call OnUserChoseDatatypes() | |
234 // after calling this to force the encryption to occur. | |
235 virtual void EnableEncryptEverything() = 0; | |
236 | |
237 // Returns true if we are currently set to encrypt all the sync data. | |
238 virtual bool IsEncryptEverythingEnabled() const = 0; | |
239 | |
240 // Asynchronously sets the passphrase to |passphrase| for encryption. |type| | |
241 // specifies whether the passphrase is a custom passphrase or the GAIA | |
242 // password being reused as a passphrase. | |
243 // TODO(atwilson): Change this so external callers can only set an EXPLICIT | |
244 // passphrase with this API. | |
245 virtual void SetEncryptionPassphrase(const std::string& passphrase, | |
246 PassphraseType type) = 0; | |
247 | |
248 // Asynchronously decrypts pending keys using |passphrase|. Returns false | |
249 // immediately if the passphrase could not be used to decrypt a locally cached | |
250 // copy of encrypted keys; returns true otherwise. | |
251 virtual bool SetDecryptionPassphrase(const std::string& passphrase) | |
252 WARN_UNUSED_RESULT = 0; | |
253 | |
254 // Checks whether the Cryptographer is ready to encrypt and decrypt updates | |
255 // for sensitive data types. Caller must be holding a | |
256 // syncapi::BaseTransaction to ensure thread safety. | |
257 virtual bool IsCryptographerReady( | |
258 const syncer::BaseTransaction* trans) const = 0; | |
259 | |
260 // TODO(akalin): This is called mostly by ModelAssociators and | |
261 // tests. Figure out how to pass the handle to the ModelAssociators | |
262 // directly, figure out how to expose this to tests, and remove this | |
263 // function. | |
264 virtual syncer::UserShare* GetUserShare() const = 0; | |
265 | |
266 // Returns DeviceInfo provider for the local device. | |
267 virtual LocalDeviceInfoProvider* GetLocalDeviceInfoProvider() const = 0; | |
268 | |
269 // Registers a data type controller with the sync service. This | |
270 // makes the data type controller available for use, it does not | |
271 // enable or activate the synchronization of the data type (see | |
272 // ActivateDataType). Takes ownership of the pointer. | |
273 virtual void RegisterDataTypeController( | |
274 DataTypeController* data_type_controller) = 0; | |
275 | |
276 // Called to re-enable a type disabled by DisableDatatype(..). Note, this does | |
277 // not change the preferred state of a datatype, and is not persisted across | |
278 // restarts. | |
279 virtual void ReenableDatatype(syncer::ModelType type) = 0; | |
280 | |
281 // Return sync token status. | |
282 virtual SyncTokenStatus GetSyncTokenStatus() const = 0; | |
283 | |
284 // Get a description of the sync status for displaying in the user interface. | |
285 virtual std::string QuerySyncStatusSummaryString() = 0; | |
286 | |
287 // Initializes a struct of status indicators with data from the backend. | |
288 // Returns false if the backend was not available for querying; in that case | |
289 // the struct will be filled with default data. | |
290 virtual bool QueryDetailedSyncStatus(syncer::SyncStatus* result) = 0; | |
291 | |
292 // Returns a user-friendly string form of last synced time (in minutes). | |
293 virtual base::string16 GetLastSyncedTimeString() const = 0; | |
294 | |
295 // Returns a human readable string describing backend initialization state. | |
296 virtual std::string GetBackendInitializationStateString() const = 0; | |
297 | |
298 virtual syncer::sessions::SyncSessionSnapshot GetLastSessionSnapshot() | |
299 const = 0; | |
300 | |
301 // Returns a ListValue indicating the status of all registered types. | |
302 // | |
303 // The format is: | |
304 // [ {"name": <name>, "value": <value>, "status": <status> }, ... ] | |
305 // where <name> is a type's name, <value> is a string providing details for | |
306 // the type's status, and <status> is one of "error", "warning" or "ok" | |
307 // depending on the type's current status. | |
308 // | |
309 // This function is used by about_sync_util.cc to help populate the about:sync | |
310 // page. It returns a ListValue rather than a DictionaryValue in part to make | |
311 // it easier to iterate over its elements when constructing that page. | |
312 virtual base::Value* GetTypeStatusMap() const = 0; | |
313 | |
314 virtual const GURL& sync_service_url() const = 0; | |
315 | |
316 virtual std::string unrecoverable_error_message() const = 0; | |
317 virtual tracked_objects::Location unrecoverable_error_location() const = 0; | |
318 | |
319 virtual void AddProtocolEventObserver( | |
320 browser_sync::ProtocolEventObserver* observer) = 0; | |
321 virtual void RemoveProtocolEventObserver( | |
322 browser_sync::ProtocolEventObserver* observer) = 0; | |
323 | |
324 virtual void AddTypeDebugInfoObserver( | |
325 syncer::TypeDebugInfoObserver* observer) = 0; | |
326 virtual void RemoveTypeDebugInfoObserver( | |
327 syncer::TypeDebugInfoObserver* observer) = 0; | |
328 | |
329 // Returns a weak pointer to the service's JsController. | |
330 virtual base::WeakPtr<syncer::JsController> GetJsController() = 0; | |
331 | |
332 // Asynchronously fetches base::Value representations of all sync nodes and | |
333 // returns them to the specified callback on this thread. | |
334 // | |
335 // These requests can live a long time and return when you least expect it. | |
336 // For safety, the callback should be bound to some sort of WeakPtr<> or | |
337 // scoped_refptr<>. | |
338 virtual void GetAllNodes( | |
339 const base::Callback<void(std::unique_ptr<base::ListValue>)>& | |
340 callback) = 0; | |
341 | |
342 protected: | |
343 SyncService() {} | |
344 | |
345 private: | |
346 DISALLOW_COPY_AND_ASSIGN(SyncService); | |
347 }; | |
348 | |
349 } // namespace sync_driver | |
350 | |
351 #endif // COMPONENTS_SYNC_DRIVER_SYNC_SERVICE_H_ | |
OLD | NEW |