OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef SYNC_INTERNAL_API_PUBLIC_SYNC_MANAGER_H_ | 5 #ifndef SYNC_INTERNAL_API_PUBLIC_SYNC_MANAGER_H_ |
6 #define SYNC_INTERNAL_API_PUBLIC_SYNC_MANAGER_H_ | 6 #define SYNC_INTERNAL_API_PUBLIC_SYNC_MANAGER_H_ |
7 | 7 |
8 #include <string> | 8 #include <string> |
9 #include <vector> | 9 #include <vector> |
10 | 10 |
11 #include "base/basictypes.h" | 11 #include "base/basictypes.h" |
12 #include "base/callback_forward.h" | 12 #include "base/callback_forward.h" |
13 #include "base/file_path.h" | 13 #include "base/file_path.h" |
14 #include "base/memory/ref_counted.h" | 14 #include "base/memory/ref_counted.h" |
15 #include "base/task_runner.h" | 15 #include "base/task_runner.h" |
16 #include "base/threading/thread_checker.h" | 16 #include "base/threading/thread_checker.h" |
17 #include "base/time.h" | 17 #include "base/time.h" |
18 #include "sync/internal_api/public/base/model_type.h" | 18 #include "sync/internal_api/public/base/model_type.h" |
19 #include "sync/internal_api/public/change_record.h" | 19 #include "sync/internal_api/public/change_record.h" |
20 #include "sync/internal_api/public/configure_reason.h" | 20 #include "sync/internal_api/public/configure_reason.h" |
21 #include "sync/internal_api/public/engine/model_safe_worker.h" | 21 #include "sync/internal_api/public/engine/model_safe_worker.h" |
22 #include "sync/internal_api/public/engine/sync_status.h" | 22 #include "sync/internal_api/public/engine/sync_status.h" |
| 23 #include "sync/internal_api/public/sync_encryption_handler.h" |
23 #include "sync/internal_api/public/util/report_unrecoverable_error_function.h" | 24 #include "sync/internal_api/public/util/report_unrecoverable_error_function.h" |
24 #include "sync/internal_api/public/util/weak_handle.h" | 25 #include "sync/internal_api/public/util/weak_handle.h" |
25 #include "sync/notifier/invalidation_util.h" | 26 #include "sync/notifier/invalidation_util.h" |
26 #include "sync/protocol/sync_protocol_error.h" | 27 #include "sync/protocol/sync_protocol_error.h" |
27 | 28 |
28 namespace sync_pb { | 29 namespace sync_pb { |
29 class EncryptedData; | 30 class EncryptedData; |
30 } // namespace sync_pb | 31 } // namespace sync_pb |
31 | 32 |
32 namespace syncer { | 33 namespace syncer { |
33 | 34 |
34 class BaseTransaction; | 35 class BaseTransaction; |
35 class Encryptor; | 36 class Encryptor; |
36 struct Experiments; | 37 struct Experiments; |
37 class ExtensionsActivityMonitor; | 38 class ExtensionsActivityMonitor; |
38 class HttpPostProviderFactory; | 39 class HttpPostProviderFactory; |
39 class InternalComponentsFactory; | 40 class InternalComponentsFactory; |
40 class JsBackend; | 41 class JsBackend; |
41 class JsEventHandler; | 42 class JsEventHandler; |
| 43 class SyncEncryptionHandler; |
42 class SyncNotifier; | 44 class SyncNotifier; |
43 class SyncNotifierObserver; | 45 class SyncNotifierObserver; |
44 class SyncScheduler; | 46 class SyncScheduler; |
45 class UnrecoverableErrorHandler; | 47 class UnrecoverableErrorHandler; |
46 struct UserShare; | 48 struct UserShare; |
47 | 49 |
48 namespace sessions { | 50 namespace sessions { |
49 class SyncSessionSnapshot; | 51 class SyncSessionSnapshot; |
50 } // namespace sessions | 52 } // namespace sessions |
51 | 53 |
52 // Used by SyncManager::OnConnectionStatusChange(). | 54 // Used by SyncManager::OnConnectionStatusChange(). |
53 enum ConnectionStatus { | 55 enum ConnectionStatus { |
54 CONNECTION_OK, | 56 CONNECTION_OK, |
55 CONNECTION_AUTH_ERROR, | 57 CONNECTION_AUTH_ERROR, |
56 CONNECTION_SERVER_ERROR | 58 CONNECTION_SERVER_ERROR |
57 }; | 59 }; |
58 | 60 |
59 // Reasons due to which Cryptographer might require a passphrase. | |
60 enum PassphraseRequiredReason { | |
61 REASON_PASSPHRASE_NOT_REQUIRED = 0, // Initial value. | |
62 REASON_ENCRYPTION = 1, // The cryptographer requires a | |
63 // passphrase for its first attempt at | |
64 // encryption. Happens only during | |
65 // migration or upgrade. | |
66 REASON_DECRYPTION = 2, // The cryptographer requires a | |
67 // passphrase for its first attempt at | |
68 // decryption. | |
69 }; | |
70 | |
71 | |
72 // Contains everything needed to talk to and identify a user account. | 61 // Contains everything needed to talk to and identify a user account. |
73 struct SyncCredentials { | 62 struct SyncCredentials { |
74 std::string email; | 63 std::string email; |
75 std::string sync_token; | 64 std::string sync_token; |
76 }; | 65 }; |
77 | 66 |
78 // SyncManager encapsulates syncable::Directory and serves as the parent of all | 67 // SyncManager encapsulates syncable::Directory and serves as the parent of all |
79 // other objects in the sync API. If multiple threads interact with the same | 68 // other objects in the sync API. If multiple threads interact with the same |
80 // local sync repository (i.e. the same sqlite database), they should share a | 69 // local sync repository (i.e. the same sqlite database), they should share a |
81 // single SyncManager instance. The caller should typically create one | 70 // single SyncManager instance. The caller should typically create one |
(...skipping 98 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
180 virtual void OnSyncCycleCompleted( | 169 virtual void OnSyncCycleCompleted( |
181 const sessions::SyncSessionSnapshot& snapshot) = 0; | 170 const sessions::SyncSessionSnapshot& snapshot) = 0; |
182 | 171 |
183 // Called when the status of the connection to the sync server has | 172 // Called when the status of the connection to the sync server has |
184 // changed. | 173 // changed. |
185 virtual void OnConnectionStatusChange(ConnectionStatus status) = 0; | 174 virtual void OnConnectionStatusChange(ConnectionStatus status) = 0; |
186 | 175 |
187 // Called when a new auth token is provided by the sync server. | 176 // Called when a new auth token is provided by the sync server. |
188 virtual void OnUpdatedToken(const std::string& token) = 0; | 177 virtual void OnUpdatedToken(const std::string& token) = 0; |
189 | 178 |
190 // Called when user interaction is required to obtain a valid passphrase. | |
191 // - If the passphrase is required for encryption, |reason| will be | |
192 // REASON_ENCRYPTION. | |
193 // - If the passphrase is required for the decryption of data that has | |
194 // already been encrypted, |reason| will be REASON_DECRYPTION. | |
195 // - If the passphrase is required because decryption failed, and a new | |
196 // passphrase is required, |reason| will be REASON_SET_PASSPHRASE_FAILED. | |
197 // | |
198 // |pending_keys| is a copy of the cryptographer's pending keys, that may be | |
199 // cached by the frontend for subsequent use by the UI. | |
200 virtual void OnPassphraseRequired( | |
201 PassphraseRequiredReason reason, | |
202 const sync_pb::EncryptedData& pending_keys) = 0; | |
203 | |
204 // Called when the passphrase provided by the user has been accepted and is | |
205 // now used to encrypt sync data. | |
206 virtual void OnPassphraseAccepted() = 0; | |
207 | |
208 // |bootstrap_token| is an opaque base64 encoded representation of the key | |
209 // generated by the current passphrase, and is provided to the observer for | |
210 // persistence purposes and use in a future initialization of sync (e.g. | |
211 // after restart). The boostrap token will always be derived from the most | |
212 // recent GAIA password (for accounts with implicit passphrases), even if | |
213 // the data is still encrypted with an older GAIA password. For accounts | |
214 // with explicit passphrases, it will be the most recently seen custom | |
215 // passphrase. | |
216 virtual void OnBootstrapTokenUpdated( | |
217 const std::string& bootstrap_token) = 0; | |
218 | |
219 // Called when initialization is complete to the point that SyncManager can | 179 // Called when initialization is complete to the point that SyncManager can |
220 // process changes. This does not necessarily mean authentication succeeded | 180 // process changes. This does not necessarily mean authentication succeeded |
221 // or that the SyncManager is online. | 181 // or that the SyncManager is online. |
222 // IMPORTANT: Creating any type of transaction before receiving this | 182 // IMPORTANT: Creating any type of transaction before receiving this |
223 // notification is illegal! | 183 // notification is illegal! |
224 // WARNING: Calling methods on the SyncManager before receiving this | 184 // WARNING: Calling methods on the SyncManager before receiving this |
225 // message, unless otherwise specified, produces undefined behavior. | 185 // message, unless otherwise specified, produces undefined behavior. |
226 // | 186 // |
227 // |js_backend| is what about:sync interacts with. It can emit | 187 // |js_backend| is what about:sync interacts with. It can emit |
228 // the following events: | 188 // the following events: |
(...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
297 const WeakHandle<syncer::JsBackend>& js_backend, | 257 const WeakHandle<syncer::JsBackend>& js_backend, |
298 bool success, | 258 bool success, |
299 syncer::ModelTypeSet restored_types) = 0; | 259 syncer::ModelTypeSet restored_types) = 0; |
300 | 260 |
301 // We are no longer permitted to communicate with the server. Sync should | 261 // We are no longer permitted to communicate with the server. Sync should |
302 // be disabled and state cleaned up at once. This can happen for a number | 262 // be disabled and state cleaned up at once. This can happen for a number |
303 // of reasons, e.g. swapping from a test instance to production, or a | 263 // of reasons, e.g. swapping from a test instance to production, or a |
304 // global stop syncing operation has wiped the store. | 264 // global stop syncing operation has wiped the store. |
305 virtual void OnStopSyncingPermanently() = 0; | 265 virtual void OnStopSyncingPermanently() = 0; |
306 | 266 |
307 // Called when the set of encrypted types or the encrypt | |
308 // everything flag has been changed. Note that encryption isn't | |
309 // complete until the OnEncryptionComplete() notification has been | |
310 // sent (see below). | |
311 // | |
312 // |encrypted_types| will always be a superset of | |
313 // Cryptographer::SensitiveTypes(). If |encrypt_everything| is | |
314 // true, |encrypted_types| will be the set of all known types. | |
315 // | |
316 // Until this function is called, observers can assume that the | |
317 // set of encrypted types is Cryptographer::SensitiveTypes() and | |
318 // that the encrypt everything flag is false. | |
319 // | |
320 // Called from within a transaction. | |
321 virtual void OnEncryptedTypesChanged( | |
322 ModelTypeSet encrypted_types, | |
323 bool encrypt_everything) = 0; | |
324 | |
325 // Called after we finish encrypting the current set of encrypted | |
326 // types. | |
327 // | |
328 // Called from within a transaction. | |
329 virtual void OnEncryptionComplete() = 0; | |
330 | |
331 virtual void OnActionableError( | 267 virtual void OnActionableError( |
332 const SyncProtocolError& sync_protocol_error) = 0; | 268 const SyncProtocolError& sync_protocol_error) = 0; |
333 | 269 |
334 protected: | 270 protected: |
335 virtual ~Observer(); | 271 virtual ~Observer(); |
336 }; | 272 }; |
337 | 273 |
338 SyncManager(); | 274 SyncManager(); |
339 virtual ~SyncManager(); | 275 virtual ~SyncManager(); |
340 | 276 |
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
410 // Forwards to the underlying notifier (see | 346 // Forwards to the underlying notifier (see |
411 // SyncNotifier::UpdateRegisteredIds()). | 347 // SyncNotifier::UpdateRegisteredIds()). |
412 virtual void UpdateRegisteredInvalidationIds( | 348 virtual void UpdateRegisteredInvalidationIds( |
413 SyncNotifierObserver* handler, | 349 SyncNotifierObserver* handler, |
414 const ObjectIdSet& ids) = 0; | 350 const ObjectIdSet& ids) = 0; |
415 | 351 |
416 // Put the syncer in normal mode ready to perform nudges and polls. | 352 // Put the syncer in normal mode ready to perform nudges and polls. |
417 virtual void StartSyncingNormally( | 353 virtual void StartSyncingNormally( |
418 const ModelSafeRoutingInfo& routing_info) = 0; | 354 const ModelSafeRoutingInfo& routing_info) = 0; |
419 | 355 |
420 // Attempts to re-encrypt encrypted data types using the passphrase provided. | |
421 // Notifies observers of the result of the operation via OnPassphraseAccepted | |
422 // or OnPassphraseRequired, updates the nigori node, and does re-encryption as | |
423 // appropriate. If an explicit password has been set previously, we drop | |
424 // subsequent requests to set a passphrase. If the cryptographer has pending | |
425 // keys, and a new implicit passphrase is provided, we try decrypting the | |
426 // pending keys with it, and if that fails, we cache the passphrase for | |
427 // re-encryption once the pending keys are decrypted. | |
428 virtual void SetEncryptionPassphrase(const std::string& passphrase, | |
429 bool is_explicit) = 0; | |
430 | |
431 // Provides a passphrase for decrypting the user's existing sync data. | |
432 // Notifies observers of the result of the operation via OnPassphraseAccepted | |
433 // or OnPassphraseRequired, updates the nigori node, and does re-encryption as | |
434 // appropriate if there is a previously cached encryption passphrase. It is an | |
435 // error to call this when we don't have pending keys. | |
436 virtual void SetDecryptionPassphrase(const std::string& passphrase) = 0; | |
437 | |
438 // Switches the mode of operation to CONFIGURATION_MODE and performs | 356 // Switches the mode of operation to CONFIGURATION_MODE and performs |
439 // any configuration tasks needed as determined by the params. Once complete, | 357 // any configuration tasks needed as determined by the params. Once complete, |
440 // syncer will remain in CONFIGURATION_MODE until StartSyncingNormally is | 358 // syncer will remain in CONFIGURATION_MODE until StartSyncingNormally is |
441 // called. | 359 // called. |
442 // |ready_task| is invoked when the configuration completes. | 360 // |ready_task| is invoked when the configuration completes. |
443 // |retry_task| is invoked if the configuration job could not immediately | 361 // |retry_task| is invoked if the configuration job could not immediately |
444 // execute. |ready_task| will still be called when it eventually | 362 // execute. |ready_task| will still be called when it eventually |
445 // does finish. | 363 // does finish. |
446 virtual void ConfigureSyncer( | 364 virtual void ConfigureSyncer( |
447 ConfigureReason reason, | 365 ConfigureReason reason, |
448 const ModelTypeSet& types_to_config, | 366 const ModelTypeSet& types_to_config, |
449 const ModelSafeRoutingInfo& new_routing_info, | 367 const ModelSafeRoutingInfo& new_routing_info, |
450 const base::Closure& ready_task, | 368 const base::Closure& ready_task, |
451 const base::Closure& retry_task) = 0; | 369 const base::Closure& retry_task) = 0; |
452 | 370 |
453 // Adds a listener to be notified of sync events. | 371 // Adds a listener to be notified of sync events. |
454 // NOTE: It is OK (in fact, it's probably a good idea) to call this before | 372 // NOTE: It is OK (in fact, it's probably a good idea) to call this before |
455 // having received OnInitializationCompleted. | 373 // having received OnInitializationCompleted. |
456 virtual void AddObserver(Observer* observer) = 0; | 374 virtual void AddObserver(Observer* observer) = 0; |
457 | 375 |
458 // Remove the given observer. Make sure to call this if the | 376 // Remove the given observer. Make sure to call this if the |
459 // Observer is being destroyed so the SyncManager doesn't | 377 // Observer is being destroyed so the SyncManager doesn't |
460 // potentially dereference garbage. | 378 // potentially dereference garbage. |
461 virtual void RemoveObserver(Observer* observer) = 0; | 379 virtual void RemoveObserver(Observer* observer) = 0; |
462 | 380 |
463 // Status-related getter. May be called on any thread. | 381 // Status-related getter. May be called on any thread. |
464 virtual SyncStatus GetDetailedStatus() const = 0; | 382 virtual SyncStatus GetDetailedStatus() const = 0; |
465 | 383 |
466 // Whether or not the Nigori node is encrypted using an explicit passphrase. | |
467 // May be called on any thread. | |
468 virtual bool IsUsingExplicitPassphrase() = 0; | |
469 | |
470 // Extracts the keystore encryption bootstrap token if a keystore key existed. | 384 // Extracts the keystore encryption bootstrap token if a keystore key existed. |
471 // Returns true if bootstrap token successfully extracted, false otherwise. | 385 // Returns true if bootstrap token successfully extracted, false otherwise. |
472 virtual bool GetKeystoreKeyBootstrapToken(std::string* token) = 0; | 386 virtual bool GetKeystoreKeyBootstrapToken(std::string* token) = 0; |
473 | 387 |
474 // Call periodically from a database-safe thread to persist recent changes | 388 // Call periodically from a database-safe thread to persist recent changes |
475 // to the syncapi model. | 389 // to the syncapi model. |
476 virtual void SaveChanges() = 0; | 390 virtual void SaveChanges() = 0; |
477 | 391 |
478 // Initiates shutdown of various components in the sync engine. Must be | 392 // Initiates shutdown of various components in the sync engine. Must be |
479 // called from the main thread to allow preempting ongoing tasks on the sync | 393 // called from the main thread to allow preempting ongoing tasks on the sync |
480 // loop (that may be blocked on I/O). The semantics of |callback| are the | 394 // loop (that may be blocked on I/O). The semantics of |callback| are the |
481 // same as with StartConfigurationMode. If provided and a scheduler / sync | 395 // same as with StartConfigurationMode. If provided and a scheduler / sync |
482 // loop exists, it will be invoked from the sync loop by the scheduler to | 396 // loop exists, it will be invoked from the sync loop by the scheduler to |
483 // notify that all work has been flushed + cancelled, and it is idle. | 397 // notify that all work has been flushed + cancelled, and it is idle. |
484 // If no scheduler exists, the callback is run immediately (from the loop | 398 // If no scheduler exists, the callback is run immediately (from the loop |
485 // this was created on, which is the sync loop), as sync is effectively | 399 // this was created on, which is the sync loop), as sync is effectively |
486 // stopped. | 400 // stopped. |
487 virtual void StopSyncingForShutdown(const base::Closure& callback) = 0; | 401 virtual void StopSyncingForShutdown(const base::Closure& callback) = 0; |
488 | 402 |
489 // Issue a final SaveChanges, and close sqlite handles. | 403 // Issue a final SaveChanges, and close sqlite handles. |
490 virtual void ShutdownOnSyncThread() = 0; | 404 virtual void ShutdownOnSyncThread() = 0; |
491 | 405 |
492 // May be called from any thread. | 406 // May be called from any thread. |
493 virtual UserShare* GetUserShare() = 0; | 407 virtual UserShare* GetUserShare() = 0; |
494 | 408 |
495 // Inform the cryptographer of the most recent passphrase and set of | |
496 // encrypted types (from nigori node), then ensure all data that | |
497 // needs encryption is encrypted with the appropriate passphrase. | |
498 // | |
499 // May trigger OnPassphraseRequired(). Otherwise, it will trigger | |
500 // OnEncryptedTypesChanged() if necessary (see comments for | |
501 // OnEncryptedTypesChanged()), and then OnEncryptionComplete(). | |
502 // | |
503 // Also updates or adds device information to the nigori node. | |
504 // | |
505 // Note: opens a transaction, so must only be called after syncapi | |
506 // has been initialized. | |
507 virtual void RefreshNigori(const std::string& chrome_version, | |
508 const base::Closure& done_callback) = 0; | |
509 | |
510 // Enable encryption of all sync data. Once enabled, it can never be | |
511 // disabled without clearing the server data. | |
512 // | |
513 // This will trigger OnEncryptedTypesChanged() if necessary (see | |
514 // comments for OnEncryptedTypesChanged()). It then may trigger | |
515 // OnPassphraseRequired(), but otherwise it will trigger | |
516 // OnEncryptionComplete(). | |
517 virtual void EnableEncryptEverything() = 0; | |
518 | |
519 // Reads the nigori node to determine if any experimental features should | |
520 // be enabled. | |
521 // Note: opens a transaction. May be called on any thread. | |
522 virtual bool ReceivedExperiment(Experiments* experiments) = 0; | |
523 | |
524 // Uses a read-only transaction to determine if the directory being synced has | 409 // Uses a read-only transaction to determine if the directory being synced has |
525 // any remaining unsynced items. May be called on any thread. | 410 // any remaining unsynced items. May be called on any thread. |
526 virtual bool HasUnsyncedItems() = 0; | 411 virtual bool HasUnsyncedItems() = 0; |
| 412 |
| 413 // Returns the SyncManager's encryption handler. |
| 414 virtual SyncEncryptionHandler* GetEncryptionHandler() = 0; |
527 }; | 415 }; |
528 | 416 |
529 } // namespace syncer | 417 } // namespace syncer |
530 | 418 |
531 #endif // SYNC_INTERNAL_API_PUBLIC_SYNC_MANAGER_H_ | 419 #endif // SYNC_INTERNAL_API_PUBLIC_SYNC_MANAGER_H_ |
OLD | NEW |