OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 CHROME_BROWSER_SYNC_GLUE_SYNC_BACKEND_HOST_H_ | 5 #ifndef CHROME_BROWSER_SYNC_GLUE_SYNC_BACKEND_HOST_H_ |
6 #define CHROME_BROWSER_SYNC_GLUE_SYNC_BACKEND_HOST_H_ | 6 #define CHROME_BROWSER_SYNC_GLUE_SYNC_BACKEND_HOST_H_ |
7 #pragma once | 7 #pragma once |
8 | 8 |
9 #include <map> | 9 #include <map> |
10 #include <string> | 10 #include <string> |
11 #include <vector> | 11 #include <vector> |
12 | 12 |
13 #include "base/callback.h" | 13 #include "base/callback.h" |
14 #include "base/compiler_specific.h" | 14 #include "base/compiler_specific.h" |
15 #include "base/file_path.h" | 15 #include "base/file_path.h" |
16 #include "base/gtest_prod_util.h" | 16 #include "base/gtest_prod_util.h" |
17 #include "base/memory/ref_counted.h" | 17 #include "base/memory/ref_counted.h" |
18 #include "base/message_loop.h" | 18 #include "base/message_loop.h" |
19 #include "base/synchronization/lock.h" | 19 #include "base/synchronization/lock.h" |
20 #include "base/threading/thread.h" | 20 #include "base/threading/thread.h" |
21 #include "base/timer.h" | 21 #include "base/timer.h" |
22 #include "chrome/browser/sync/engine/configure_reason.h" | 22 #include "chrome/browser/sync/engine/configure_reason.h" |
23 #include "chrome/browser/sync/engine/model_safe_worker.h" | 23 #include "chrome/browser/sync/engine/model_safe_worker.h" |
24 #include "chrome/browser/sync/engine/syncapi.h" | |
25 #include "chrome/browser/sync/glue/data_type_controller.h" | |
26 #include "chrome/browser/sync/glue/ui_model_worker.h" | 24 #include "chrome/browser/sync/glue/ui_model_worker.h" |
27 #include "chrome/browser/sync/js/js_backend.h" | 25 #include "chrome/browser/sync/js/js_backend.h" |
28 #include "chrome/browser/sync/notifier/sync_notifier_factory.h" | 26 #include "chrome/browser/sync/notifier/sync_notifier_factory.h" |
29 #include "chrome/browser/sync/syncable/model_type.h" | 27 #include "chrome/browser/sync/syncable/model_type.h" |
30 #include "chrome/browser/sync/weak_handle.h" | 28 #include "chrome/browser/sync/weak_handle.h" |
31 #include "chrome/common/net/gaia/google_service_auth_error.h" | 29 #include "chrome/common/net/gaia/google_service_auth_error.h" |
32 #include "googleurl/src/gurl.h" | 30 #include "googleurl/src/gurl.h" |
33 #include "net/url_request/url_request_context_getter.h" | 31 #include "net/url_request/url_request_context_getter.h" |
34 | 32 |
35 class CancelableTask; | 33 class CancelableTask; |
36 class Profile; | 34 class Profile; |
37 | 35 |
38 namespace net { | 36 namespace net { |
39 class URLRequestContextGetter; | 37 class URLRequestContextGetter; |
40 } | 38 } |
41 | 39 |
42 namespace browser_sync { | 40 namespace browser_sync { |
43 | 41 |
44 namespace sessions { | 42 namespace sessions { |
45 struct SyncSessionSnapshot; | 43 struct SyncSessionSnapshot; |
46 } | 44 } |
47 | 45 |
48 class ChangeProcessor; | 46 class ChangeProcessor; |
49 class DataTypeController; | |
50 class JsEventHandler; | 47 class JsEventHandler; |
51 | 48 |
52 // SyncFrontend is the interface used by SyncBackendHost to communicate with | 49 // SyncFrontend is the interface used by SyncBackendHost to communicate with |
53 // the entity that created it and, presumably, is interested in sync-related | 50 // the entity that created it and, presumably, is interested in sync-related |
54 // activity. | 51 // activity. |
55 // NOTE: All methods will be invoked by a SyncBackendHost on the same thread | 52 // NOTE: All methods will be invoked by a SyncBackendHost on the same thread |
56 // used to create that SyncBackendHost. | 53 // used to create that SyncBackendHost. |
57 class SyncFrontend { | 54 class SyncFrontend { |
58 public: | 55 public: |
59 SyncFrontend() {} | 56 SyncFrontend() {} |
(...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
126 SyncBackendHost(); | 123 SyncBackendHost(); |
127 virtual ~SyncBackendHost(); | 124 virtual ~SyncBackendHost(); |
128 | 125 |
129 // Called on |frontend_loop_| to kick off asynchronous initialization. | 126 // Called on |frontend_loop_| to kick off asynchronous initialization. |
130 // As a fallback when no cached auth information is available, try to | 127 // As a fallback when no cached auth information is available, try to |
131 // bootstrap authentication using |lsid|, if it isn't empty. | 128 // bootstrap authentication using |lsid|, if it isn't empty. |
132 // Optionally delete the Sync Data folder (if it's corrupt). | 129 // Optionally delete the Sync Data folder (if it's corrupt). |
133 void Initialize(SyncFrontend* frontend, | 130 void Initialize(SyncFrontend* frontend, |
134 const WeakHandle<JsEventHandler>& event_handler, | 131 const WeakHandle<JsEventHandler>& event_handler, |
135 const GURL& service_url, | 132 const GURL& service_url, |
136 const syncable::ModelTypeSet& types, | 133 const syncable::ModelTypeSet& initial_types, |
137 const sync_api::SyncCredentials& credentials, | 134 const sync_api::SyncCredentials& credentials, |
138 bool delete_sync_data_folder); | 135 bool delete_sync_data_folder); |
139 | 136 |
140 // Called from |frontend_loop| to update SyncCredentials. | 137 // Called from |frontend_loop| to update SyncCredentials. |
141 void UpdateCredentials(const sync_api::SyncCredentials& credentials); | 138 void UpdateCredentials(const sync_api::SyncCredentials& credentials); |
142 | 139 |
143 // This starts the SyncerThread running a Syncer object to communicate with | 140 // This starts the SyncerThread running a Syncer object to communicate with |
144 // sync servers. Until this is called, no changes will leave or enter this | 141 // sync servers. Until this is called, no changes will leave or enter this |
145 // browser from the cloud / sync servers. | 142 // browser from the cloud / sync servers. |
146 // Called on |frontend_loop_|. | 143 // Called on |frontend_loop_|. |
147 virtual void StartSyncingWithServer(); | 144 virtual void StartSyncingWithServer(); |
148 | 145 |
149 // Called on |frontend_loop_| to asynchronously set the passphrase. | 146 // Called on |frontend_loop_| to asynchronously set the passphrase. |
150 // |is_explicit| is true if the call is in response to the user explicitly | 147 // |is_explicit| is true if the call is in response to the user explicitly |
151 // setting a passphrase as opposed to implicitly (from the users' perspective) | 148 // setting a passphrase as opposed to implicitly (from the users' perspective) |
152 // using their Google Account password. An implicit SetPassphrase will *not* | 149 // using their Google Account password. An implicit SetPassphrase will *not* |
153 // *not* override an explicit passphrase set previously. | 150 // *not* override an explicit passphrase set previously. |
154 void SetPassphrase(const std::string& passphrase, bool is_explicit); | 151 void SetPassphrase(const std::string& passphrase, bool is_explicit); |
155 | 152 |
156 // Called on |frontend_loop_| to kick off shutdown. | 153 // Called on |frontend_loop_| to kick off shutdown. |
157 // |sync_disabled| indicates if syncing is being disabled or not. | 154 // |sync_disabled| indicates if syncing is being disabled or not. |
158 // See the implementation and Core::DoShutdown for details. | 155 // See the implementation and Core::DoShutdown for details. |
159 void Shutdown(bool sync_disabled); | 156 void Shutdown(bool sync_disabled); |
160 | 157 |
161 // Changes the set of data types that are currently being synced. | 158 // Changes the set of data types that are currently being synced. |
162 // The ready_task will be run when all of the requested data types | 159 // The ready_task will be run when all of the requested data types |
163 // are up-to-date and ready for activation. The task will cancelled | 160 // are up-to-date and ready for activation. The task will be |
164 // upon shutdown. The method takes ownership of the task pointer. | 161 // cancelled upon shutdown. |
165 virtual void ConfigureDataTypes( | 162 virtual void ConfigureDataTypes( |
166 const DataTypeController::TypeMap& data_type_controllers, | 163 const syncable::ModelTypeSet& types_to_add, |
167 const syncable::ModelTypeSet& types, | 164 const syncable::ModelTypeSet& types_to_remove, |
168 sync_api::ConfigureReason reason, | 165 sync_api::ConfigureReason reason, |
169 base::Callback<void(bool)> ready_task, | 166 base::Callback<void(bool)> ready_task, |
170 bool enable_nigori); | 167 bool enable_nigori); |
171 | 168 |
172 // Makes an asynchronous call to syncer to switch to config mode. When done | 169 // Makes an asynchronous call to syncer to switch to config mode. When done |
173 // syncer will call us back on FinishConfigureDataTypes. | 170 // syncer will call us back on FinishConfigureDataTypes. |
174 virtual void StartConfiguration(Callback0::Type* callback); | 171 virtual void StartConfiguration(Callback0::Type* callback); |
175 | 172 |
176 // Encrypts the specified datatypes and marks them as needing encryption on | 173 // Encrypts the specified datatypes and marks them as needing encryption on |
177 // other machines. This affects all machines synced to this account and all | 174 // other machines. This affects all machines synced to this account and all |
178 // data belonging to the specified types. | 175 // data belonging to the specified types. |
179 // Note: actual work is done on sync_thread_'s message loop. | 176 // Note: actual work is done on sync_thread_'s message loop. |
180 virtual void EncryptDataTypes( | 177 virtual void EncryptDataTypes( |
181 const syncable::ModelTypeSet& encrypted_types); | 178 const syncable::ModelTypeSet& encrypted_types); |
182 | 179 |
183 syncable::ModelTypeSet GetEncryptedDataTypes() const; | 180 syncable::ModelTypeSet GetEncryptedDataTypes() const; |
184 | 181 |
185 // Activates change processing for the given data type. This must | 182 // Activates change processing for the given data type. This must |
186 // be called synchronously with the data type's model association so | 183 // be called synchronously with the data type's model association so |
187 // no changes are dropped between model association and change | 184 // no changes are dropped between model association and change |
188 // processor activation. | 185 // processor activation. |
189 void ActivateDataType(DataTypeController* data_type_controller, | 186 void ActivateDataType( |
190 ChangeProcessor* change_processor); | 187 syncable::ModelType type, ModelSafeGroup group, |
| 188 ChangeProcessor* change_processor); |
191 | 189 |
192 // Deactivates change processing for the given data type. | 190 // Deactivates change processing for the given data type. |
193 void DeactivateDataType(DataTypeController* data_type_controller, | 191 void DeactivateDataType(syncable::ModelType type); |
194 ChangeProcessor* change_processor); | |
195 | 192 |
196 // Asks the server to clear all data associated with ChromeSync. | 193 // Asks the server to clear all data associated with ChromeSync. |
197 virtual bool RequestClearServerData(); | 194 virtual bool RequestClearServerData(); |
198 | 195 |
199 // Called on |frontend_loop_| to obtain a handle to the UserShare needed | 196 // Called on |frontend_loop_| to obtain a handle to the UserShare needed |
200 // for creating transactions. | 197 // for creating transactions. |
201 sync_api::UserShare* GetUserShare() const; | 198 sync_api::UserShare* GetUserShare() const; |
202 | 199 |
203 // Called from any thread to obtain current status information in detailed or | 200 // Called from any thread to obtain current status information in detailed or |
204 // summarized form. | 201 // summarized form. |
(...skipping 145 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
350 // 2) Then, from |frontend_loop_|, halt the sync_thread_ (which is | 347 // 2) Then, from |frontend_loop_|, halt the sync_thread_ (which is |
351 // a blocking call). This causes syncapi thread-exit handlers | 348 // a blocking call). This causes syncapi thread-exit handlers |
352 // to run and make use of cached pointers to various components | 349 // to run and make use of cached pointers to various components |
353 // owned implicitly by us. | 350 // owned implicitly by us. |
354 // 3) Destroy this Core. That will delete syncapi components in a | 351 // 3) Destroy this Core. That will delete syncapi components in a |
355 // safe order because the thread that was using them has exited | 352 // safe order because the thread that was using them has exited |
356 // (in step 2). | 353 // (in step 2). |
357 void DoShutdown(bool stopping_sync); | 354 void DoShutdown(bool stopping_sync); |
358 | 355 |
359 // Posts a config request on the sync thread. | 356 // Posts a config request on the sync thread. |
360 virtual void DoRequestConfig(const syncable::ModelTypeBitSet& added_types, | 357 virtual void DoRequestConfig( |
| 358 const syncable::ModelTypeBitSet& types_to_config, |
361 sync_api::ConfigureReason reason); | 359 sync_api::ConfigureReason reason); |
362 | 360 |
363 // Start the configuration mode. | 361 // Start the configuration mode. |
364 virtual void DoStartConfiguration(Callback0::Type* callback); | 362 virtual void DoStartConfiguration(Callback0::Type* callback); |
365 | 363 |
366 // Set the base request context to use when making HTTP calls. | 364 // Set the base request context to use when making HTTP calls. |
367 // This method will add a reference to the context to persist it | 365 // This method will add a reference to the context to persist it |
368 // on the IO thread. Must be removed from IO thread. | 366 // on the IO thread. Must be removed from IO thread. |
369 | 367 |
370 sync_api::SyncManager* sync_manager() { return sync_manager_.get(); } | 368 sync_api::SyncManager* sync_manager() { return sync_manager_.get(); } |
(...skipping 30 matching lines...) Expand all Loading... |
401 "", true)); | 399 "", true)); |
402 } | 400 } |
403 #endif | 401 #endif |
404 | 402 |
405 private: | 403 private: |
406 friend class base::RefCountedThreadSafe<SyncBackendHost::Core>; | 404 friend class base::RefCountedThreadSafe<SyncBackendHost::Core>; |
407 friend class SyncBackendHostForProfileSyncTest; | 405 friend class SyncBackendHostForProfileSyncTest; |
408 | 406 |
409 virtual ~Core(); | 407 virtual ~Core(); |
410 | 408 |
411 // Return change processor for a particular model (return NULL on failure). | |
412 ChangeProcessor* GetProcessor(syncable::ModelType modeltype); | |
413 | |
414 // Invoked when initialization of syncapi is complete and we can start | 409 // Invoked when initialization of syncapi is complete and we can start |
415 // our timer. | 410 // our timer. |
416 // This must be called from the thread on which SaveChanges is intended to | 411 // This must be called from the thread on which SaveChanges is intended to |
417 // be run on; the host's |sync_thread_|. | 412 // be run on; the host's |sync_thread_|. |
418 void StartSavingChanges(); | 413 void StartSavingChanges(); |
419 | 414 |
420 // Invoked periodically to tell the syncapi to persist its state | 415 // Invoked periodically to tell the syncapi to persist its state |
421 // by writing to disk. | 416 // by writing to disk. |
422 // This is called from the thread we were created on (which is the | 417 // This is called from the thread we were created on (which is the |
423 // SyncBackendHost |sync_thread_|), using a repeating timer that is kicked | 418 // SyncBackendHost |sync_thread_|), using a repeating timer that is kicked |
(...skipping 27 matching lines...) Expand all Loading... |
451 sessions::SyncSessionSnapshot* snapshot); | 446 sessions::SyncSessionSnapshot* snapshot); |
452 | 447 |
453 void HandleStopSyncingPermanentlyOnFrontendLoop(); | 448 void HandleStopSyncingPermanentlyOnFrontendLoop(); |
454 | 449 |
455 // Called to handle success/failure of clearing server data | 450 // Called to handle success/failure of clearing server data |
456 void HandleClearServerDataSucceededOnFrontendLoop(); | 451 void HandleClearServerDataSucceededOnFrontendLoop(); |
457 void HandleClearServerDataFailedOnFrontendLoop(); | 452 void HandleClearServerDataFailedOnFrontendLoop(); |
458 | 453 |
459 void FinishConfigureDataTypesOnFrontendLoop(); | 454 void FinishConfigureDataTypesOnFrontendLoop(); |
460 | 455 |
461 // Return true if a model lives on the current thread. | |
462 bool IsCurrentThreadSafeForModel(syncable::ModelType model_type); | |
463 | |
464 Profile* profile_; | 456 Profile* profile_; |
465 | 457 |
466 // Our parent SyncBackendHost | 458 // Our parent SyncBackendHost |
467 SyncBackendHost* host_; | 459 SyncBackendHost* host_; |
468 | 460 |
469 // The timer used to periodically call SaveChanges. | 461 // The timer used to periodically call SaveChanges. |
470 base::RepeatingTimer<Core> save_changes_timer_; | 462 base::RepeatingTimer<Core> save_changes_timer_; |
471 | 463 |
472 // The top-level syncapi entry point. Lives on the sync thread. | 464 // The top-level syncapi entry point. Lives on the sync thread. |
473 scoped_ptr<sync_api::SyncManager> sync_manager_; | 465 scoped_ptr<sync_api::SyncManager> sync_manager_; |
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
521 struct PendingConfigureDataTypesState { | 513 struct PendingConfigureDataTypesState { |
522 PendingConfigureDataTypesState(); | 514 PendingConfigureDataTypesState(); |
523 ~PendingConfigureDataTypesState(); | 515 ~PendingConfigureDataTypesState(); |
524 | 516 |
525 // A task that should be called once data type configuration is | 517 // A task that should be called once data type configuration is |
526 // complete. | 518 // complete. |
527 base::Callback<void(bool)> ready_task; | 519 base::Callback<void(bool)> ready_task; |
528 | 520 |
529 // The set of types that we are waiting to be initially synced in a | 521 // The set of types that we are waiting to be initially synced in a |
530 // configuration cycle. | 522 // configuration cycle. |
531 syncable::ModelTypeSet initial_types; | 523 syncable::ModelTypeSet types_to_add; |
532 | 524 |
533 // Additional details about which types were added. | 525 // Additional details about which types were added. |
534 syncable::ModelTypeBitSet added_types; | 526 syncable::ModelTypeSet added_types; |
535 sync_api::ConfigureReason reason; | 527 sync_api::ConfigureReason reason; |
536 }; | 528 }; |
537 | 529 |
538 UIModelWorker* ui_worker(); | 530 UIModelWorker* ui_worker(); |
539 | 531 |
540 // Helper function for ConfigureDataTypes() that fills in |state| | 532 // Return change processor for a particular model (return NULL on failure). |
541 // and |deleted_type|. Does not take ownership of routing_info|. | 533 ChangeProcessor* GetProcessor(syncable::ModelType modeltype); |
| 534 |
| 535 // Like GetProcessor(), but |registrar_lock_| must already be |
| 536 // held. |
| 537 ChangeProcessor* GetProcessorUnsafe(syncable::ModelType modeltype); |
| 538 |
| 539 // Return true if a model lives on the current thread. |
| 540 bool IsCurrentThreadSafeForModel(syncable::ModelType model_type) const; |
| 541 |
| 542 // Helper function for ConfigureDataTypes() that fills in |state|. |
| 543 // Does not take ownership of |routing_info|. |
542 static void GetPendingConfigModeState( | 544 static void GetPendingConfigModeState( |
543 const DataTypeController::TypeMap& data_type_controllers, | 545 const syncable::ModelTypeSet& types_to_add, |
544 const syncable::ModelTypeSet& types, | 546 const syncable::ModelTypeSet& types_to_remove, |
545 base::Callback<void(bool)> ready_task, | 547 base::Callback<void(bool)> ready_task, |
546 ModelSafeRoutingInfo* routing_info, | 548 ModelSafeRoutingInfo* routing_info, |
547 sync_api::ConfigureReason reason, | 549 sync_api::ConfigureReason reason, |
548 bool nigori_enabled, | 550 PendingConfigureDataTypesState* state); |
549 PendingConfigureDataTypesState* state, | |
550 bool* deleted_type); | |
551 | 551 |
552 // For convenience, checks if initialization state is INITIALIZED. | 552 // For convenience, checks if initialization state is INITIALIZED. |
553 bool initialized() const { return initialization_state_ == INITIALIZED; } | 553 bool initialized() const { return initialization_state_ == INITIALIZED; } |
554 | 554 |
555 // A thread where all the sync operations happen. | 555 // A thread where all the sync operations happen. |
556 base::Thread sync_thread_; | 556 base::Thread sync_thread_; |
557 | 557 |
558 // A reference to the MessageLoop used to construct |this|, so we know how | 558 // A reference to the MessageLoop used to construct |this|, so we know how |
559 // to safely talk back to the SyncFrontend. | 559 // to safely talk back to the SyncFrontend. |
560 MessageLoop* const frontend_loop_; | 560 MessageLoop* const frontend_loop_; |
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
606 | 606 |
607 // UI-thread cache of the last SyncSessionSnapshot received from syncapi. | 607 // UI-thread cache of the last SyncSessionSnapshot received from syncapi. |
608 scoped_ptr<sessions::SyncSessionSnapshot> last_snapshot_; | 608 scoped_ptr<sessions::SyncSessionSnapshot> last_snapshot_; |
609 | 609 |
610 DISALLOW_COPY_AND_ASSIGN(SyncBackendHost); | 610 DISALLOW_COPY_AND_ASSIGN(SyncBackendHost); |
611 }; | 611 }; |
612 | 612 |
613 } // namespace browser_sync | 613 } // namespace browser_sync |
614 | 614 |
615 #endif // CHROME_BROWSER_SYNC_GLUE_SYNC_BACKEND_HOST_H_ | 615 #endif // CHROME_BROWSER_SYNC_GLUE_SYNC_BACKEND_HOST_H_ |
OLD | NEW |