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 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
416 const ObjectIdSet& ids) = 0; | 352 const ObjectIdSet& ids) = 0; |
417 | 353 |
418 // Forwards to the underlying notifier (see comments in sync_notifier.h). | 354 // Forwards to the underlying notifier (see comments in sync_notifier.h). |
419 virtual void UnregisterInvalidationHandler( | 355 virtual void UnregisterInvalidationHandler( |
420 SyncNotifierObserver* handler) = 0; | 356 SyncNotifierObserver* handler) = 0; |
421 | 357 |
422 // Put the syncer in normal mode ready to perform nudges and polls. | 358 // Put the syncer in normal mode ready to perform nudges and polls. |
423 virtual void StartSyncingNormally( | 359 virtual void StartSyncingNormally( |
424 const ModelSafeRoutingInfo& routing_info) = 0; | 360 const ModelSafeRoutingInfo& routing_info) = 0; |
425 | 361 |
426 // Attempts to re-encrypt encrypted data types using the passphrase provided. | |
427 // Notifies observers of the result of the operation via OnPassphraseAccepted | |
428 // or OnPassphraseRequired, updates the nigori node, and does re-encryption as | |
429 // appropriate. If an explicit password has been set previously, we drop | |
430 // subsequent requests to set a passphrase. If the cryptographer has pending | |
431 // keys, and a new implicit passphrase is provided, we try decrypting the | |
432 // pending keys with it, and if that fails, we cache the passphrase for | |
433 // re-encryption once the pending keys are decrypted. | |
434 virtual void SetEncryptionPassphrase(const std::string& passphrase, | |
435 bool is_explicit) = 0; | |
436 | |
437 // Provides a passphrase for decrypting the user's existing sync data. | |
438 // Notifies observers of the result of the operation via OnPassphraseAccepted | |
439 // or OnPassphraseRequired, updates the nigori node, and does re-encryption as | |
440 // appropriate if there is a previously cached encryption passphrase. It is an | |
441 // error to call this when we don't have pending keys. | |
442 virtual void SetDecryptionPassphrase(const std::string& passphrase) = 0; | |
443 | |
444 // Switches the mode of operation to CONFIGURATION_MODE and performs | 362 // Switches the mode of operation to CONFIGURATION_MODE and performs |
445 // any configuration tasks needed as determined by the params. Once complete, | 363 // any configuration tasks needed as determined by the params. Once complete, |
446 // syncer will remain in CONFIGURATION_MODE until StartSyncingNormally is | 364 // syncer will remain in CONFIGURATION_MODE until StartSyncingNormally is |
447 // called. | 365 // called. |
448 // |ready_task| is invoked when the configuration completes. | 366 // |ready_task| is invoked when the configuration completes. |
449 // |retry_task| is invoked if the configuration job could not immediately | 367 // |retry_task| is invoked if the configuration job could not immediately |
450 // execute. |ready_task| will still be called when it eventually | 368 // execute. |ready_task| will still be called when it eventually |
451 // does finish. | 369 // does finish. |
452 virtual void ConfigureSyncer( | 370 virtual void ConfigureSyncer( |
453 ConfigureReason reason, | 371 ConfigureReason reason, |
454 const ModelTypeSet& types_to_config, | 372 const ModelTypeSet& types_to_config, |
455 const ModelSafeRoutingInfo& new_routing_info, | 373 const ModelSafeRoutingInfo& new_routing_info, |
456 const base::Closure& ready_task, | 374 const base::Closure& ready_task, |
457 const base::Closure& retry_task) = 0; | 375 const base::Closure& retry_task) = 0; |
458 | 376 |
459 // Adds a listener to be notified of sync events. | 377 // Adds a listener to be notified of sync events. |
460 // NOTE: It is OK (in fact, it's probably a good idea) to call this before | 378 // NOTE: It is OK (in fact, it's probably a good idea) to call this before |
461 // having received OnInitializationCompleted. | 379 // having received OnInitializationCompleted. |
462 virtual void AddObserver(Observer* observer) = 0; | 380 virtual void AddObserver(Observer* observer) = 0; |
463 | 381 |
464 // Remove the given observer. Make sure to call this if the | 382 // Remove the given observer. Make sure to call this if the |
465 // Observer is being destroyed so the SyncManager doesn't | 383 // Observer is being destroyed so the SyncManager doesn't |
466 // potentially dereference garbage. | 384 // potentially dereference garbage. |
467 virtual void RemoveObserver(Observer* observer) = 0; | 385 virtual void RemoveObserver(Observer* observer) = 0; |
468 | 386 |
469 // Status-related getter. May be called on any thread. | 387 // Status-related getter. May be called on any thread. |
470 virtual SyncStatus GetDetailedStatus() const = 0; | 388 virtual SyncStatus GetDetailedStatus() const = 0; |
471 | 389 |
472 // Whether or not the Nigori node is encrypted using an explicit passphrase. | |
473 // May be called on any thread. | |
474 virtual bool IsUsingExplicitPassphrase() = 0; | |
475 | |
476 // Extracts the keystore encryption bootstrap token if a keystore key existed. | 390 // Extracts the keystore encryption bootstrap token if a keystore key existed. |
477 // Returns true if bootstrap token successfully extracted, false otherwise. | 391 // Returns true if bootstrap token successfully extracted, false otherwise. |
478 virtual bool GetKeystoreKeyBootstrapToken(std::string* token) = 0; | 392 virtual bool GetKeystoreKeyBootstrapToken(std::string* token) = 0; |
479 | 393 |
480 // Call periodically from a database-safe thread to persist recent changes | 394 // Call periodically from a database-safe thread to persist recent changes |
481 // to the syncapi model. | 395 // to the syncapi model. |
482 virtual void SaveChanges() = 0; | 396 virtual void SaveChanges() = 0; |
483 | 397 |
484 // Initiates shutdown of various components in the sync engine. Must be | 398 // Initiates shutdown of various components in the sync engine. Must be |
485 // called from the main thread to allow preempting ongoing tasks on the sync | 399 // called from the main thread to allow preempting ongoing tasks on the sync |
486 // loop (that may be blocked on I/O). The semantics of |callback| are the | 400 // loop (that may be blocked on I/O). The semantics of |callback| are the |
487 // same as with StartConfigurationMode. If provided and a scheduler / sync | 401 // same as with StartConfigurationMode. If provided and a scheduler / sync |
488 // loop exists, it will be invoked from the sync loop by the scheduler to | 402 // loop exists, it will be invoked from the sync loop by the scheduler to |
489 // notify that all work has been flushed + cancelled, and it is idle. | 403 // notify that all work has been flushed + cancelled, and it is idle. |
490 // If no scheduler exists, the callback is run immediately (from the loop | 404 // If no scheduler exists, the callback is run immediately (from the loop |
491 // this was created on, which is the sync loop), as sync is effectively | 405 // this was created on, which is the sync loop), as sync is effectively |
492 // stopped. | 406 // stopped. |
493 virtual void StopSyncingForShutdown(const base::Closure& callback) = 0; | 407 virtual void StopSyncingForShutdown(const base::Closure& callback) = 0; |
494 | 408 |
495 // Issue a final SaveChanges, and close sqlite handles. | 409 // Issue a final SaveChanges, and close sqlite handles. |
496 virtual void ShutdownOnSyncThread() = 0; | 410 virtual void ShutdownOnSyncThread() = 0; |
497 | 411 |
498 // May be called from any thread. | 412 // May be called from any thread. |
499 virtual UserShare* GetUserShare() = 0; | 413 virtual UserShare* GetUserShare() = 0; |
500 | 414 |
501 // Inform the cryptographer of the most recent passphrase and set of | |
502 // encrypted types (from nigori node), then ensure all data that | |
503 // needs encryption is encrypted with the appropriate passphrase. | |
504 // | |
505 // May trigger OnPassphraseRequired(). Otherwise, it will trigger | |
506 // OnEncryptedTypesChanged() if necessary (see comments for | |
507 // OnEncryptedTypesChanged()), and then OnEncryptionComplete(). | |
508 // | |
509 // Also updates or adds device information to the nigori node. | |
510 // | |
511 // Note: opens a transaction, so must only be called after syncapi | |
512 // has been initialized. | |
513 virtual void RefreshNigori(const std::string& chrome_version, | |
514 const base::Closure& done_callback) = 0; | |
515 | |
516 // Enable encryption of all sync data. Once enabled, it can never be | |
517 // disabled without clearing the server data. | |
518 // | |
519 // This will trigger OnEncryptedTypesChanged() if necessary (see | |
520 // comments for OnEncryptedTypesChanged()). It then may trigger | |
521 // OnPassphraseRequired(), but otherwise it will trigger | |
522 // OnEncryptionComplete(). | |
523 virtual void EnableEncryptEverything() = 0; | |
524 | |
525 // Reads the nigori node to determine if any experimental features should | 415 // Reads the nigori node to determine if any experimental features should |
526 // be enabled. | 416 // be enabled. |
527 // Note: opens a transaction. May be called on any thread. | 417 // Note: opens a transaction. May be called on any thread. |
528 virtual bool ReceivedExperiment(Experiments* experiments) = 0; | 418 virtual bool ReceivedExperiment(Experiments* experiments) = 0; |
529 | 419 |
530 // Uses a read-only transaction to determine if the directory being synced has | 420 // Uses a read-only transaction to determine if the directory being synced has |
531 // any remaining unsynced items. May be called on any thread. | 421 // any remaining unsynced items. May be called on any thread. |
532 virtual bool HasUnsyncedItems() = 0; | 422 virtual bool HasUnsyncedItems() = 0; |
| 423 |
| 424 // Returns the SyncManager's encryption handler. |
| 425 virtual SyncEncryptionHandler* GetEncryptionHandler() = 0; |
533 }; | 426 }; |
534 | 427 |
535 } // namespace syncer | 428 } // namespace syncer |
536 | 429 |
537 #endif // SYNC_INTERNAL_API_PUBLIC_SYNC_MANAGER_H_ | 430 #endif // SYNC_INTERNAL_API_PUBLIC_SYNC_MANAGER_H_ |
OLD | NEW |