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_SYNC_MANAGER_H_ | 5 #ifndef SYNC_INTERNAL_API_SYNC_MANAGER_H_ |
6 #define SYNC_INTERNAL_API_SYNC_MANAGER_H_ | 6 #define SYNC_INTERNAL_API_SYNC_MANAGER_H_ |
7 | 7 |
8 #include <string> | 8 #include <string> |
9 #include <vector> | 9 #include <vector> |
10 | 10 |
11 #include "net/base/network_change_notifier.h" | 11 #include "net/base/network_change_notifier.h" |
12 #include "sync/engine/all_status.h" | 12 #include "sync/engine/all_status.h" |
13 #include "sync/engine/net/server_connection_manager.h" | 13 #include "sync/engine/net/server_connection_manager.h" |
14 #include "sync/engine/sync_engine_event.h" | 14 #include "sync/engine/sync_engine_event.h" |
15 #include "sync/engine/throttled_data_type_tracker.h" | 15 #include "sync/engine/throttled_data_type_tracker.h" |
16 #include "sync/engine/traffic_recorder.h" | 16 #include "sync/engine/traffic_recorder.h" |
17 #include "sync/internal_api/change_reorder_buffer.h" | 17 #include "sync/internal_api/change_reorder_buffer.h" |
18 #include "sync/internal_api/debug_info_event_listener.h" | 18 #include "sync/internal_api/debug_info_event_listener.h" |
19 #include "sync/internal_api/js_mutation_event_observer.h" | 19 #include "sync/internal_api/js_mutation_event_observer.h" |
| 20 #include "sync/internal_api/js_sync_encryption_handler_observer.h" |
20 #include "sync/internal_api/js_sync_manager_observer.h" | 21 #include "sync/internal_api/js_sync_manager_observer.h" |
21 #include "sync/internal_api/public/sync_manager.h" | 22 #include "sync/internal_api/public/sync_manager.h" |
| 23 #include "sync/internal_api/sync_encryption_handler_impl.h" |
22 #include "sync/js/js_backend.h" | 24 #include "sync/js/js_backend.h" |
23 #include "sync/notifier/notifications_disabled_reason.h" | 25 #include "sync/notifier/notifications_disabled_reason.h" |
24 #include "sync/notifier/sync_notifier_observer.h" | 26 #include "sync/notifier/sync_notifier_observer.h" |
25 #include "sync/syncable/directory_change_delegate.h" | 27 #include "sync/syncable/directory_change_delegate.h" |
26 #include "sync/util/cryptographer.h" | 28 #include "sync/util/cryptographer.h" |
27 #include "sync/util/time.h" | 29 #include "sync/util/time.h" |
28 | 30 |
29 namespace syncer { | 31 namespace syncer { |
30 | 32 |
31 class SyncAPIServerConnectionManager; | 33 class SyncAPIServerConnectionManager; |
32 class WriteNode; | 34 class WriteNode; |
33 class WriteTransaction; | 35 class WriteTransaction; |
34 | 36 |
35 namespace sessions { | 37 namespace sessions { |
36 class SyncSessionContext; | 38 class SyncSessionContext; |
37 } | 39 } |
38 | 40 |
39 // SyncManager encapsulates syncable::Directory and serves as the parent of all | 41 // SyncManager encapsulates syncable::Directory and serves as the parent of all |
40 // other objects in the sync API. If multiple threads interact with the same | 42 // other objects in the sync API. If multiple threads interact with the same |
41 // local sync repository (i.e. the same sqlite database), they should share a | 43 // local sync repository (i.e. the same sqlite database), they should share a |
42 // single SyncManager instance. The caller should typically create one | 44 // single SyncManager instance. The caller should typically create one |
43 // SyncManager for the lifetime of a user session. | 45 // SyncManager for the lifetime of a user session. |
44 // | 46 // |
45 // Unless stated otherwise, all methods of SyncManager should be called on the | 47 // Unless stated otherwise, all methods of SyncManager should be called on the |
46 // same thread. | 48 // same thread. |
47 class SyncManagerImpl : public SyncManager, | 49 class SyncManagerImpl : public SyncManager, |
48 public net::NetworkChangeNotifier::IPAddressObserver, | 50 public net::NetworkChangeNotifier::IPAddressObserver, |
49 public Cryptographer::Observer, | |
50 public SyncNotifierObserver, | 51 public SyncNotifierObserver, |
51 public JsBackend, | 52 public JsBackend, |
52 public SyncEngineEventListener, | 53 public SyncEngineEventListener, |
53 public ServerConnectionEventListener, | 54 public ServerConnectionEventListener, |
54 public syncable::DirectoryChangeDelegate { | 55 public syncable::DirectoryChangeDelegate, |
| 56 public SyncEncryptionHandler::Observer { |
55 public: | 57 public: |
56 // Create an uninitialized SyncManager. Callers must Init() before using. | 58 // Create an uninitialized SyncManager. Callers must Init() before using. |
57 explicit SyncManagerImpl(const std::string& name); | 59 explicit SyncManagerImpl(const std::string& name); |
58 virtual ~SyncManagerImpl(); | 60 virtual ~SyncManagerImpl(); |
59 | 61 |
60 // SyncManager implementation. | 62 // SyncManager implementation. |
61 virtual void Init( | 63 virtual void Init( |
62 const FilePath& database_location, | 64 const FilePath& database_location, |
63 const WeakHandle<JsEventHandler>& event_handler, | 65 const WeakHandle<JsEventHandler>& event_handler, |
64 const std::string& sync_server_and_path, | 66 const std::string& sync_server_and_path, |
(...skipping 23 matching lines...) Expand all Loading... |
88 const ModelTypeSet& enabled_types) OVERRIDE; | 90 const ModelTypeSet& enabled_types) OVERRIDE; |
89 virtual void RegisterInvalidationHandler( | 91 virtual void RegisterInvalidationHandler( |
90 SyncNotifierObserver* handler) OVERRIDE; | 92 SyncNotifierObserver* handler) OVERRIDE; |
91 virtual void UpdateRegisteredInvalidationIds( | 93 virtual void UpdateRegisteredInvalidationIds( |
92 SyncNotifierObserver* handler, | 94 SyncNotifierObserver* handler, |
93 const ObjectIdSet& ids) OVERRIDE; | 95 const ObjectIdSet& ids) OVERRIDE; |
94 virtual void UnregisterInvalidationHandler( | 96 virtual void UnregisterInvalidationHandler( |
95 SyncNotifierObserver* handler) OVERRIDE; | 97 SyncNotifierObserver* handler) OVERRIDE; |
96 virtual void StartSyncingNormally( | 98 virtual void StartSyncingNormally( |
97 const ModelSafeRoutingInfo& routing_info) OVERRIDE; | 99 const ModelSafeRoutingInfo& routing_info) OVERRIDE; |
98 virtual void SetEncryptionPassphrase(const std::string& passphrase, | |
99 bool is_explicit) OVERRIDE; | |
100 virtual void SetDecryptionPassphrase(const std::string& passphrase) OVERRIDE; | |
101 virtual void ConfigureSyncer( | 100 virtual void ConfigureSyncer( |
102 ConfigureReason reason, | 101 ConfigureReason reason, |
103 const ModelTypeSet& types_to_config, | 102 const ModelTypeSet& types_to_config, |
104 const ModelSafeRoutingInfo& new_routing_info, | 103 const ModelSafeRoutingInfo& new_routing_info, |
105 const base::Closure& ready_task, | 104 const base::Closure& ready_task, |
106 const base::Closure& retry_task) OVERRIDE; | 105 const base::Closure& retry_task) OVERRIDE; |
107 virtual void AddObserver(SyncManager::Observer* observer) OVERRIDE; | 106 virtual void AddObserver(SyncManager::Observer* observer) OVERRIDE; |
108 virtual void RemoveObserver(SyncManager::Observer* observer) OVERRIDE; | 107 virtual void RemoveObserver(SyncManager::Observer* observer) OVERRIDE; |
109 virtual SyncStatus GetDetailedStatus() const OVERRIDE; | 108 virtual SyncStatus GetDetailedStatus() const OVERRIDE; |
110 virtual bool IsUsingExplicitPassphrase() OVERRIDE; | |
111 virtual bool GetKeystoreKeyBootstrapToken(std::string* token) OVERRIDE; | 109 virtual bool GetKeystoreKeyBootstrapToken(std::string* token) OVERRIDE; |
112 virtual void SaveChanges() OVERRIDE; | 110 virtual void SaveChanges() OVERRIDE; |
113 virtual void StopSyncingForShutdown(const base::Closure& callback) OVERRIDE; | 111 virtual void StopSyncingForShutdown(const base::Closure& callback) OVERRIDE; |
114 virtual void ShutdownOnSyncThread() OVERRIDE; | 112 virtual void ShutdownOnSyncThread() OVERRIDE; |
115 virtual UserShare* GetUserShare() OVERRIDE; | 113 virtual UserShare* GetUserShare() OVERRIDE; |
116 | |
117 // Update the Cryptographer from the current nigori node and write back any | |
118 // necessary changes to the nigori node. We also detect missing encryption | |
119 // keys and write them into the nigori node. | |
120 // Also updates or adds the device information into the nigori node. | |
121 // Note: opens a transaction and can trigger an ON_PASSPHRASE_REQUIRED, so | |
122 // should only be called after syncapi is fully initialized. | |
123 // Calls the callback argument with true if cryptographer is ready, false | |
124 // otherwise. | |
125 virtual void RefreshNigori(const std::string& chrome_version, | |
126 const base::Closure& done_callback) OVERRIDE; | |
127 | |
128 virtual void EnableEncryptEverything() OVERRIDE; | |
129 virtual bool ReceivedExperiment(Experiments* experiments) OVERRIDE; | 114 virtual bool ReceivedExperiment(Experiments* experiments) OVERRIDE; |
130 virtual bool HasUnsyncedItems() OVERRIDE; | 115 virtual bool HasUnsyncedItems() OVERRIDE; |
| 116 virtual SyncEncryptionHandler* GetEncryptionHandler() OVERRIDE; |
| 117 |
| 118 // SyncEncryptionHandler::Observer implementation. |
| 119 virtual void OnPassphraseRequired( |
| 120 PassphraseRequiredReason reason, |
| 121 const sync_pb::EncryptedData& pending_keys) OVERRIDE; |
| 122 virtual void OnPassphraseAccepted() OVERRIDE; |
| 123 virtual void OnBootstrapTokenUpdated( |
| 124 const std::string& bootstrap_token) OVERRIDE; |
| 125 virtual void OnEncryptedTypesChanged( |
| 126 ModelTypeSet encrypted_types, |
| 127 bool encrypt_everything) OVERRIDE; |
| 128 virtual void OnEncryptionComplete() OVERRIDE; |
| 129 virtual void OnCryptographerStateChanged( |
| 130 Cryptographer* cryptographer) OVERRIDE; |
131 | 131 |
132 // Return the currently active (validated) username for use with syncable | 132 // Return the currently active (validated) username for use with syncable |
133 // types. | 133 // types. |
134 const std::string& username_for_share() const; | 134 const std::string& username_for_share() const; |
135 | 135 |
136 static int GetDefaultNudgeDelay(); | 136 static int GetDefaultNudgeDelay(); |
137 static int GetPreferencesNudgeDelay(); | 137 static int GetPreferencesNudgeDelay(); |
138 | 138 |
139 // SyncEngineEventListener implementation. | 139 // SyncEngineEventListener implementation. |
140 virtual void OnSyncEngineEvent(const SyncEngineEvent& event) OVERRIDE; | 140 virtual void OnSyncEngineEvent(const SyncEngineEvent& event) OVERRIDE; |
(...skipping 18 matching lines...) Expand all Loading... |
159 virtual ModelTypeSet HandleTransactionEndingChangeEvent( | 159 virtual ModelTypeSet HandleTransactionEndingChangeEvent( |
160 const syncable::ImmutableWriteTransactionInfo& write_transaction_info, | 160 const syncable::ImmutableWriteTransactionInfo& write_transaction_info, |
161 syncable::BaseTransaction* trans) OVERRIDE; | 161 syncable::BaseTransaction* trans) OVERRIDE; |
162 virtual void HandleCalculateChangesChangeEventFromSyncApi( | 162 virtual void HandleCalculateChangesChangeEventFromSyncApi( |
163 const syncable::ImmutableWriteTransactionInfo& write_transaction_info, | 163 const syncable::ImmutableWriteTransactionInfo& write_transaction_info, |
164 syncable::BaseTransaction* trans) OVERRIDE; | 164 syncable::BaseTransaction* trans) OVERRIDE; |
165 virtual void HandleCalculateChangesChangeEventFromSyncer( | 165 virtual void HandleCalculateChangesChangeEventFromSyncer( |
166 const syncable::ImmutableWriteTransactionInfo& write_transaction_info, | 166 const syncable::ImmutableWriteTransactionInfo& write_transaction_info, |
167 syncable::BaseTransaction* trans) OVERRIDE; | 167 syncable::BaseTransaction* trans) OVERRIDE; |
168 | 168 |
169 // Cryptographer::Observer implementation. | |
170 virtual void OnEncryptedTypesChanged( | |
171 ModelTypeSet encrypted_types, | |
172 bool encrypt_everything) OVERRIDE; | |
173 | |
174 // SyncNotifierObserver implementation. | 169 // SyncNotifierObserver implementation. |
175 virtual void OnNotificationsEnabled() OVERRIDE; | 170 virtual void OnNotificationsEnabled() OVERRIDE; |
176 virtual void OnNotificationsDisabled( | 171 virtual void OnNotificationsDisabled( |
177 NotificationsDisabledReason reason) OVERRIDE; | 172 NotificationsDisabledReason reason) OVERRIDE; |
178 virtual void OnIncomingNotification( | 173 virtual void OnIncomingNotification( |
179 const ObjectIdPayloadMap& id_payloads, | 174 const ObjectIdPayloadMap& id_payloads, |
180 IncomingNotificationSource source) OVERRIDE; | 175 IncomingNotificationSource source) OVERRIDE; |
181 | 176 |
182 // Called only by our NetworkChangeNotifier. | 177 // Called only by our NetworkChangeNotifier. |
183 virtual void OnIPAddressChanged() OVERRIDE; | 178 virtual void OnIPAddressChanged() OVERRIDE; |
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
233 | 228 |
234 // Purge those types from |previously_enabled_types| that are no longer | 229 // Purge those types from |previously_enabled_types| that are no longer |
235 // enabled in |currently_enabled_types|. | 230 // enabled in |currently_enabled_types|. |
236 bool PurgeDisabledTypes(ModelTypeSet previously_enabled_types, | 231 bool PurgeDisabledTypes(ModelTypeSet previously_enabled_types, |
237 ModelTypeSet currently_enabled_types); | 232 ModelTypeSet currently_enabled_types); |
238 | 233 |
239 void RequestNudgeForDataTypes( | 234 void RequestNudgeForDataTypes( |
240 const tracked_objects::Location& nudge_location, | 235 const tracked_objects::Location& nudge_location, |
241 ModelTypeSet type); | 236 ModelTypeSet type); |
242 | 237 |
243 void NotifyCryptographerState(Cryptographer* cryptographer); | |
244 | |
245 // If this is a deletion for a password, sets the legacy | 238 // If this is a deletion for a password, sets the legacy |
246 // ExtraPasswordChangeRecordData field of |buffer|. Otherwise sets | 239 // ExtraPasswordChangeRecordData field of |buffer|. Otherwise sets |
247 // |buffer|'s specifics field to contain the unencrypted data. | 240 // |buffer|'s specifics field to contain the unencrypted data. |
248 void SetExtraChangeRecordData(int64 id, | 241 void SetExtraChangeRecordData(int64 id, |
249 ModelType type, | 242 ModelType type, |
250 ChangeReorderBuffer* buffer, | 243 ChangeReorderBuffer* buffer, |
251 Cryptographer* cryptographer, | 244 Cryptographer* cryptographer, |
252 const syncable::EntryKernel& original, | 245 const syncable::EntryKernel& original, |
253 bool existed_before, | 246 bool existed_before, |
254 bool exists_now); | 247 bool exists_now); |
255 | 248 |
256 // Stores the current set of encryption keys (if the cryptographer is ready) | 249 // Internal callback used by GetSessionName. |
257 // and encrypted types into the nigori node. | 250 // TODO(rlarocque): not currently called from anywhere. This should be |
258 void UpdateNigoriEncryptionState(Cryptographer* cryptographer, | 251 // hooked up to something once we start preserving device information again. |
259 WriteNode* nigori_node); | 252 void UpdateSessionNameCallback(const std::string& chrome_version, |
260 | 253 const std::string& session_name); |
261 // Internal callback of UpdateCryptographerAndNigoriCallback. | |
262 void UpdateCryptographerAndNigoriCallback( | |
263 const std::string& chrome_version, | |
264 const base::Closure& done_callback, | |
265 const std::string& session_name); | |
266 | |
267 // Updates the nigori node with any new encrypted types and then | |
268 // encrypts the nodes for those new data types as well as other | |
269 // nodes that should be encrypted but aren't. Triggers | |
270 // OnPassphraseRequired if the cryptographer isn't ready. | |
271 void RefreshEncryption(); | |
272 | |
273 void ReEncryptEverything(WriteTransaction* trans); | |
274 | |
275 // The final step of SetEncryptionPassphrase and SetDecryptionPassphrase that | |
276 // notifies observers of the result of the set passphrase operation, updates | |
277 // the nigori node, and does re-encryption. | |
278 // |success|: true if the operation was successful and false otherwise. If | |
279 // success == false, we send an OnPassphraseRequired notification. | |
280 // |bootstrap_token|: used to inform observers if the cryptographer's | |
281 // bootstrap token was updated. | |
282 // |is_explicit|: used to differentiate between a custom passphrase (true) and | |
283 // a GAIA passphrase that is implicitly used for encryption | |
284 // (false). | |
285 // |trans| and |nigori_node|: used to access data in the cryptographer. | |
286 void FinishSetPassphrase( | |
287 bool success, | |
288 const std::string& bootstrap_token, | |
289 bool is_explicit, | |
290 WriteTransaction* trans, | |
291 WriteNode* nigori_node); | |
292 | 254 |
293 // Called for every notification. This updates the notification statistics | 255 // Called for every notification. This updates the notification statistics |
294 // to be displayed in about:sync. | 256 // to be displayed in about:sync. |
295 void UpdateNotificationInfo( | 257 void UpdateNotificationInfo( |
296 const ModelTypePayloadMap& type_payloads); | 258 const ModelTypePayloadMap& type_payloads); |
297 | 259 |
298 // Checks for server reachabilty and requests a nudge. | 260 // Checks for server reachabilty and requests a nudge. |
299 void OnIPAddressChangedImpl(); | 261 void OnIPAddressChangedImpl(); |
300 | 262 |
301 // Helper function used only by the constructor. | 263 // Helper function used only by the constructor. |
(...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
395 | 357 |
396 // Map used to store the notification info to be displayed in | 358 // Map used to store the notification info to be displayed in |
397 // about:sync page. | 359 // about:sync page. |
398 NotificationInfoMap notification_info_map_; | 360 NotificationInfoMap notification_info_map_; |
399 | 361 |
400 // These are for interacting with chrome://sync-internals. | 362 // These are for interacting with chrome://sync-internals. |
401 JsMessageHandlerMap js_message_handlers_; | 363 JsMessageHandlerMap js_message_handlers_; |
402 WeakHandle<JsEventHandler> js_event_handler_; | 364 WeakHandle<JsEventHandler> js_event_handler_; |
403 JsSyncManagerObserver js_sync_manager_observer_; | 365 JsSyncManagerObserver js_sync_manager_observer_; |
404 JsMutationEventObserver js_mutation_event_observer_; | 366 JsMutationEventObserver js_mutation_event_observer_; |
| 367 JsSyncEncryptionHandlerObserver js_sync_encryption_handler_observer_; |
405 | 368 |
406 ThrottledDataTypeTracker throttled_data_type_tracker_; | 369 ThrottledDataTypeTracker throttled_data_type_tracker_; |
407 | 370 |
408 // This is for keeping track of client events to send to the server. | 371 // This is for keeping track of client events to send to the server. |
409 DebugInfoEventListener debug_info_event_listener_; | 372 DebugInfoEventListener debug_info_event_listener_; |
410 | 373 |
411 TrafficRecorder traffic_recorder_; | 374 TrafficRecorder traffic_recorder_; |
412 | 375 |
413 Encryptor* encryptor_; | 376 Encryptor* encryptor_; |
414 UnrecoverableErrorHandler* unrecoverable_error_handler_; | 377 UnrecoverableErrorHandler* unrecoverable_error_handler_; |
415 ReportUnrecoverableErrorFunction report_unrecoverable_error_function_; | 378 ReportUnrecoverableErrorFunction report_unrecoverable_error_function_; |
416 | 379 |
417 // The number of times we've automatically (i.e. not via SetPassphrase or | 380 // Sync's encryption handler. It tracks the set of encrypted types, manages |
418 // conflict resolver) updated the nigori's encryption keys in this chrome | 381 // changing passphrases, and in general handles sync-specific interactions |
419 // instantiation. | 382 // with the cryptographer. |
420 int nigori_overwrite_count_; | 383 scoped_ptr<SyncEncryptionHandlerImpl> sync_encryption_handler_; |
421 | 384 |
422 DISALLOW_COPY_AND_ASSIGN(SyncManagerImpl); | 385 DISALLOW_COPY_AND_ASSIGN(SyncManagerImpl); |
423 }; | 386 }; |
424 | 387 |
425 } // namespace syncer | 388 } // namespace syncer |
426 | 389 |
427 #endif // SYNC_INTERNAL_API_SYNC_MANAGER_H_ | 390 #endif // SYNC_INTERNAL_API_SYNC_MANAGER_H_ |
OLD | NEW |