Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1)

Side by Side Diff: chrome/browser/sync/glue/sync_backend_host.h

Issue 10701085: Revert "Revert 142517 - [Sync] Refactor sync configuration logic." (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Fix deps Created 8 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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 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 <string> 9 #include <string>
10 10
11 #include "base/basictypes.h" 11 #include "base/basictypes.h"
12 #include "base/callback.h" 12 #include "base/callback.h"
13 #include "base/compiler_specific.h" 13 #include "base/compiler_specific.h"
14 #include "base/memory/ref_counted.h" 14 #include "base/memory/ref_counted.h"
15 #include "base/memory/scoped_ptr.h" 15 #include "base/memory/scoped_ptr.h"
16 #include "base/memory/weak_ptr.h" 16 #include "base/memory/weak_ptr.h"
17 #include "base/threading/thread.h" 17 #include "base/threading/thread.h"
18 #include "chrome/browser/sync/glue/backend_data_type_configurer.h" 18 #include "chrome/browser/sync/glue/backend_data_type_configurer.h"
19 #include "chrome/browser/sync/glue/chrome_extensions_activity_monitor.h" 19 #include "chrome/browser/sync/glue/chrome_extensions_activity_monitor.h"
20 #include "chrome/browser/sync/glue/chrome_sync_notification_bridge.h" 20 #include "chrome/browser/sync/glue/chrome_sync_notification_bridge.h"
21 #include "chrome/common/net/gaia/google_service_auth_error.h" 21 #include "chrome/common/net/gaia/google_service_auth_error.h"
22 #include "googleurl/src/gurl.h" 22 #include "googleurl/src/gurl.h"
23 #include "sync/internal_api/public/base/model_type.h" 23 #include "sync/internal_api/public/base/model_type.h"
24 #include "sync/internal_api/public/configure_reason.h" 24 #include "sync/internal_api/public/configure_reason.h"
25 #include "sync/internal_api/public/engine/model_safe_worker.h" 25 #include "sync/internal_api/public/engine/model_safe_worker.h"
26 #include "sync/internal_api/public/sessions/sync_session_snapshot.h" 26 #include "sync/internal_api/public/sessions/sync_session_snapshot.h"
27 #include "sync/internal_api/public/sync_manager.h" 27 #include "sync/internal_api/public/sync_manager.h"
28 #include "sync/internal_api/public/sync_manager_factory.h"
28 #include "sync/internal_api/public/util/report_unrecoverable_error_function.h" 29 #include "sync/internal_api/public/util/report_unrecoverable_error_function.h"
29 #include "sync/internal_api/public/util/unrecoverable_error_handler.h" 30 #include "sync/internal_api/public/util/unrecoverable_error_handler.h"
30 #include "sync/internal_api/public/util/weak_handle.h" 31 #include "sync/internal_api/public/util/weak_handle.h"
31 #include "sync/notifier/sync_notifier_factory.h" 32 #include "sync/notifier/sync_notifier_factory.h"
32 #include "sync/protocol/encryption.pb.h" 33 #include "sync/protocol/encryption.pb.h"
33 #include "sync/protocol/sync_protocol_error.h" 34 #include "sync/protocol/sync_protocol_error.h"
34 35
35 class MessageLoop; 36 class MessageLoop;
36 class Profile; 37 class Profile;
37 38
(...skipping 125 matching lines...) Expand 10 before | Expand all | Expand 10 after
163 // Optionally delete the Sync Data folder (if it's corrupt). 164 // Optionally delete the Sync Data folder (if it's corrupt).
164 // |report_unrecoverable_error_function| can be NULL. 165 // |report_unrecoverable_error_function| can be NULL.
165 // Note: |unrecoverable_error_handler| may be invoked from any thread. 166 // Note: |unrecoverable_error_handler| may be invoked from any thread.
166 void Initialize( 167 void Initialize(
167 SyncFrontend* frontend, 168 SyncFrontend* frontend,
168 const syncer::WeakHandle<syncer::JsEventHandler>& event_handler, 169 const syncer::WeakHandle<syncer::JsEventHandler>& event_handler,
169 const GURL& service_url, 170 const GURL& service_url,
170 syncer::ModelTypeSet initial_types, 171 syncer::ModelTypeSet initial_types,
171 const syncer::SyncCredentials& credentials, 172 const syncer::SyncCredentials& credentials,
172 bool delete_sync_data_folder, 173 bool delete_sync_data_folder,
174 syncer::SyncManagerFactory* sync_manager_factory,
173 syncer::UnrecoverableErrorHandler* unrecoverable_error_handler, 175 syncer::UnrecoverableErrorHandler* unrecoverable_error_handler,
174 syncer::ReportUnrecoverableErrorFunction 176 syncer::ReportUnrecoverableErrorFunction
175 report_unrecoverable_error_function); 177 report_unrecoverable_error_function);
176 178
177 // Called from |frontend_loop| to update SyncCredentials. 179 // Called from |frontend_loop| to update SyncCredentials.
178 void UpdateCredentials(const syncer::SyncCredentials& credentials); 180 void UpdateCredentials(const syncer::SyncCredentials& credentials);
179 181
180 // This starts the SyncerThread running a Syncer object to communicate with 182 // This starts the SyncerThread running a Syncer object to communicate with
181 // sync servers. Until this is called, no changes will leave or enter this 183 // sync servers. Until this is called, no changes will leave or enter this
182 // browser from the cloud / sync servers. 184 // browser from the cloud / sync servers.
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after
218 220
219 // Changes the set of data types that are currently being synced. 221 // Changes the set of data types that are currently being synced.
220 // The ready_task will be run when configuration is done with the 222 // The ready_task will be run when configuration is done with the
221 // set of all types that failed configuration (i.e., if its argument 223 // set of all types that failed configuration (i.e., if its argument
222 // is non-empty, then an error was encountered). 224 // is non-empty, then an error was encountered).
223 virtual void ConfigureDataTypes( 225 virtual void ConfigureDataTypes(
224 syncer::ConfigureReason reason, 226 syncer::ConfigureReason reason,
225 syncer::ModelTypeSet types_to_add, 227 syncer::ModelTypeSet types_to_add,
226 syncer::ModelTypeSet types_to_remove, 228 syncer::ModelTypeSet types_to_remove,
227 NigoriState nigori_state, 229 NigoriState nigori_state,
228 base::Callback<void(syncer::ModelTypeSet)> ready_task, 230 const base::Callback<void(syncer::ModelTypeSet)>& ready_task,
229 base::Callback<void()> retry_callback) OVERRIDE; 231 const base::Callback<void()>& retry_callback) OVERRIDE;
230
231 // Makes an asynchronous call to syncer to switch to config mode. When done
232 // syncer will call us back on FinishConfigureDataTypes.
233 virtual void StartConfiguration(const base::Closure& callback);
234 232
235 // Turns on encryption of all present and future sync data. 233 // Turns on encryption of all present and future sync data.
236 virtual void EnableEncryptEverything(); 234 virtual void EnableEncryptEverything();
237 235
238 // Activates change processing for the given data type. This must 236 // Activates change processing for the given data type. This must
239 // be called synchronously with the data type's model association so 237 // be called synchronously with the data type's model association so
240 // no changes are dropped between model association and change 238 // no changes are dropped between model association and change
241 // processor activation. 239 // processor activation.
242 void ActivateDataType( 240 void ActivateDataType(
243 syncer::ModelType type, syncer::ModelSafeGroup group, 241 syncer::ModelType type, syncer::ModelSafeGroup group,
(...skipping 29 matching lines...) Expand all
273 271
274 void GetModelSafeRoutingInfo(syncer::ModelSafeRoutingInfo* out) const; 272 void GetModelSafeRoutingInfo(syncer::ModelSafeRoutingInfo* out) const;
275 273
276 protected: 274 protected:
277 // The types and functions below are protected so that test 275 // The types and functions below are protected so that test
278 // subclasses can use them. 276 // subclasses can use them.
279 // 277 //
280 // TODO(akalin): Figure out a better way for tests to hook into 278 // TODO(akalin): Figure out a better way for tests to hook into
281 // SyncBackendHost. 279 // SyncBackendHost.
282 280
283 typedef base::Callback<syncer::HttpPostProviderFactory*(void)> 281 typedef base::Callback<scoped_ptr<syncer::HttpPostProviderFactory>(void)>
284 MakeHttpBridgeFactoryFn; 282 MakeHttpBridgeFactoryFn;
285 283
286 struct DoInitializeOptions { 284 struct DoInitializeOptions {
287 DoInitializeOptions( 285 DoInitializeOptions(
288 MessageLoop* sync_loop, 286 MessageLoop* sync_loop,
289 SyncBackendRegistrar* registrar, 287 SyncBackendRegistrar* registrar,
290 const syncer::ModelSafeRoutingInfo& routing_info, 288 const syncer::ModelSafeRoutingInfo& routing_info,
291 const std::vector<syncer::ModelSafeWorker*>& workers, 289 const std::vector<syncer::ModelSafeWorker*>& workers,
292 syncer::ExtensionsActivityMonitor* extensions_activity_monitor, 290 syncer::ExtensionsActivityMonitor* extensions_activity_monitor,
293 const syncer::WeakHandle<syncer::JsEventHandler>& event_handler, 291 const syncer::WeakHandle<syncer::JsEventHandler>& event_handler,
294 const GURL& service_url, 292 const GURL& service_url,
295 MakeHttpBridgeFactoryFn make_http_bridge_factory_fn, 293 MakeHttpBridgeFactoryFn make_http_bridge_factory_fn,
296 const syncer::SyncCredentials& credentials, 294 const syncer::SyncCredentials& credentials,
297 ChromeSyncNotificationBridge* chrome_sync_notification_bridge, 295 ChromeSyncNotificationBridge* chrome_sync_notification_bridge,
298 syncer::SyncNotifierFactory* sync_notifier_factory, 296 syncer::SyncNotifierFactory* sync_notifier_factory,
297 syncer::SyncManagerFactory* sync_manager_factory,
299 bool delete_sync_data_folder, 298 bool delete_sync_data_folder,
300 const std::string& restored_key_for_bootstrapping, 299 const std::string& restored_key_for_bootstrapping,
301 syncer::SyncManager::TestingMode testing_mode, 300 syncer::SyncManager::TestingMode testing_mode,
302 syncer::UnrecoverableErrorHandler* unrecoverable_error_handler, 301 syncer::UnrecoverableErrorHandler* unrecoverable_error_handler,
303 syncer::ReportUnrecoverableErrorFunction 302 syncer::ReportUnrecoverableErrorFunction
304 report_unrecoverable_error_function); 303 report_unrecoverable_error_function);
305 ~DoInitializeOptions(); 304 ~DoInitializeOptions();
306 305
307 MessageLoop* sync_loop; 306 MessageLoop* sync_loop;
308 SyncBackendRegistrar* registrar; 307 SyncBackendRegistrar* registrar;
309 syncer::ModelSafeRoutingInfo routing_info; 308 syncer::ModelSafeRoutingInfo routing_info;
310 std::vector<syncer::ModelSafeWorker*> workers; 309 std::vector<syncer::ModelSafeWorker*> workers;
311 syncer::ExtensionsActivityMonitor* extensions_activity_monitor; 310 syncer::ExtensionsActivityMonitor* extensions_activity_monitor;
312 syncer::WeakHandle<syncer::JsEventHandler> event_handler; 311 syncer::WeakHandle<syncer::JsEventHandler> event_handler;
313 GURL service_url; 312 GURL service_url;
314 // Overridden by tests. 313 // Overridden by tests.
315 MakeHttpBridgeFactoryFn make_http_bridge_factory_fn; 314 MakeHttpBridgeFactoryFn make_http_bridge_factory_fn;
316 syncer::SyncCredentials credentials; 315 syncer::SyncCredentials credentials;
317 ChromeSyncNotificationBridge* const chrome_sync_notification_bridge; 316 ChromeSyncNotificationBridge* const chrome_sync_notification_bridge;
318 syncer::SyncNotifierFactory* const sync_notifier_factory; 317 syncer::SyncNotifierFactory* const sync_notifier_factory;
318 syncer::SyncManagerFactory* const sync_manager_factory;
319 std::string lsid; 319 std::string lsid;
320 bool delete_sync_data_folder; 320 bool delete_sync_data_folder;
321 std::string restored_key_for_bootstrapping; 321 std::string restored_key_for_bootstrapping;
322 syncer::SyncManager::TestingMode testing_mode; 322 syncer::SyncManager::TestingMode testing_mode;
323 syncer::UnrecoverableErrorHandler* unrecoverable_error_handler; 323 syncer::UnrecoverableErrorHandler* unrecoverable_error_handler;
324 syncer::ReportUnrecoverableErrorFunction 324 syncer::ReportUnrecoverableErrorFunction
325 report_unrecoverable_error_function; 325 report_unrecoverable_error_function;
326 }; 326 };
327 327
328 // Allow derived classes to start the sync thread early.
329 // Returns true on success, false otherwise. If the thread was already
330 // running, does nothing and returns true.
331 bool StartSyncThread();
332
333 // Getter for derived classes. May be NULL if the thread is not running.
334 MessageLoop* sync_loop() const { return sync_thread_.message_loop(); }
335
328 // Allows tests to perform alternate core initialization work. 336 // Allows tests to perform alternate core initialization work.
329 virtual void InitCore(const DoInitializeOptions& options); 337 virtual void InitCore(const DoInitializeOptions& options);
330 338
331 // Called from Core::OnSyncCycleCompleted to handle updating frontend 339 // Request the syncer to reconfigure with the specfied params.
332 // thread components. 340 // Virtual for testing.
333 void HandleSyncCycleCompletedOnFrontendLoop( 341 virtual void RequestConfigureSyncer(
334 const syncer::sessions::SyncSessionSnapshot& snapshot); 342 syncer::ConfigureReason reason,
343 syncer::ModelTypeSet types_to_config,
344 const syncer::ModelSafeRoutingInfo& routing_info,
345 const base::Callback<void(syncer::ModelTypeSet)>& ready_task,
346 const base::Closure& retry_callback);
335 347
336 // Called to finish the job of ConfigureDataTypes once the syncer is in 348 // Called when the syncer has finished performing a configuration.
337 // configuration mode. 349 void FinishConfigureDataTypesOnFrontendLoop(
338 void FinishConfigureDataTypesOnFrontendLoop(); 350 const syncer::ModelTypeSet types_to_configure,
339 351 const syncer::ModelTypeSet configured_types,
340 bool IsDownloadingNigoriForTest() const; 352 const base::Callback<void(syncer::ModelTypeSet)>& ready_task);
341 353
342 private: 354 private:
343 // The real guts of SyncBackendHost, to keep the public client API clean. 355 // The real guts of SyncBackendHost, to keep the public client API clean.
344 class Core; 356 class Core;
345 357
346 // An enum representing the steps to initializing the SyncBackendHost. 358 // An enum representing the steps to initializing the SyncBackendHost.
347 enum InitializationState { 359 enum InitializationState {
348 NOT_ATTEMPTED, 360 NOT_ATTEMPTED,
349 CREATING_SYNC_MANAGER, // We're waiting for the first callback from the 361 CREATING_SYNC_MANAGER, // We're waiting for the first callback from the
350 // sync thread to inform us that the sync manager 362 // sync thread to inform us that the sync manager
351 // has been created. 363 // has been created.
352 NOT_INITIALIZED, // Initialization hasn't completed, but we've 364 NOT_INITIALIZED, // Initialization hasn't completed, but we've
353 // constructed a SyncManager. 365 // constructed a SyncManager.
366 CLEANING_NIGORI, // A partially downloaded nigori was found. We
367 // perform a configuration cycle to blow it away.
354 DOWNLOADING_NIGORI, // The SyncManager is initialized, but 368 DOWNLOADING_NIGORI, // The SyncManager is initialized, but
355 // we're fetching encryption information. 369 // we're fetching encryption information.
356 REFRESHING_NIGORI, // The SyncManager is initialized, and we 370 REFRESHING_NIGORI, // The SyncManager is initialized, and we
357 // have the encryption information, but we 371 // have the encryption information, but we
358 // still need to refresh encryption. Also, we need 372 // still need to refresh encryption. Also, we need
359 // to update the device information in the nigori. 373 // to update the device information in the nigori.
360 INITIALIZED, // Initialization is complete. 374 INITIALIZED, // Initialization is complete.
361 }; 375 };
362 376
363 struct PendingConfigureDataTypesState { 377 // InitializationComplete passes through the SyncBackendHost to forward
364 PendingConfigureDataTypesState(); 378 // on to |frontend_|, and so that tests can intercept here if they need to
365 ~PendingConfigureDataTypesState(); 379 // set up initial conditions.
366 380 void HandleInitializationCompletedOnFrontendLoop(
367 // The ready_task will be run when configuration is done with the 381 const syncer::WeakHandle<syncer::JsBackend>& js_backend,
368 // set of all types that failed configuration (i.e., if its 382 bool success);
369 // argument is non-empty, then an error was encountered).
370 base::Callback<void(syncer::ModelTypeSet)> ready_task;
371
372 // The retry callback will be run when the download failed due to a
373 // transient error. This is to notify DTM so it can apropriately inform
374 // the UI. Note: The retry_callback will be run only once and after
375 // that we will not notify DTM until the sync is successful or in a
376 // permanent error state.
377 base::Callback<void()> retry_callback;
378
379 // The set of types that we are waiting to be initially synced in a
380 // configuration cycle.
381 syncer::ModelTypeSet types_to_add;
382
383 // Additional details about which types were added.
384 syncer::ModelTypeSet added_types;
385 syncer::ConfigureReason reason;
386 bool retry_in_progress;
387 };
388 383
389 // Checks if we have received a notice to turn on experimental datatypes 384 // Checks if we have received a notice to turn on experimental datatypes
390 // (via the nigori node) and informs the frontend if that is the case. 385 // (via the nigori node) and informs the frontend if that is the case.
391 // Note: it is illegal to call this before the backend is initialized. 386 // Note: it is illegal to call this before the backend is initialized.
392 void AddExperimentalTypes(); 387 void AddExperimentalTypes();
393 388
394 // Downloading of nigori failed and will be retried. 389 // Downloading of nigori failed and will be retried.
395 virtual void OnNigoriDownloadRetry(); 390 void OnNigoriDownloadRetry();
396 391
397 // InitializationComplete passes through the SyncBackendHost to forward 392 // Called from Core::OnSyncCycleCompleted to handle updating frontend
398 // on to |frontend_|, and so that tests can intercept here if they need to 393 // thread components.
399 // set up initial conditions. 394 void HandleSyncCycleCompletedOnFrontendLoop(
400 virtual void HandleInitializationCompletedOnFrontendLoop( 395 const syncer::sessions::SyncSessionSnapshot& snapshot);
401 const syncer::WeakHandle<syncer::JsBackend>& js_backend, 396
402 bool success); 397 // Called when the syncer failed to perform a configuration and will
398 // eventually retry. FinishingConfigurationOnFrontendLoop(..) will be called
399 // on successful completion.
400 void RetryConfigurationOnFrontendLoop(const base::Closure& retry_callback);
403 401
404 // Helpers to persist a token that can be used to bootstrap sync encryption 402 // Helpers to persist a token that can be used to bootstrap sync encryption
405 // across browser restart to avoid requiring the user to re-enter their 403 // across browser restart to avoid requiring the user to re-enter their
406 // passphrase. |token| must be valid UTF-8 as we use the PrefService for 404 // passphrase. |token| must be valid UTF-8 as we use the PrefService for
407 // storage. 405 // storage.
408 void PersistEncryptionBootstrapToken(const std::string& token); 406 void PersistEncryptionBootstrapToken(const std::string& token);
409 407
410 // For convenience, checks if initialization state is INITIALIZED. 408 // For convenience, checks if initialization state is INITIALIZED.
411 bool initialized() const { return initialization_state_ == INITIALIZED; } 409 bool initialized() const { return initialization_state_ == INITIALIZED; }
412 410
(...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after
493 491
494 syncer::SyncNotifierFactory sync_notifier_factory_; 492 syncer::SyncNotifierFactory sync_notifier_factory_;
495 493
496 ChromeExtensionsActivityMonitor extensions_activity_monitor_; 494 ChromeExtensionsActivityMonitor extensions_activity_monitor_;
497 495
498 scoped_ptr<SyncBackendRegistrar> registrar_; 496 scoped_ptr<SyncBackendRegistrar> registrar_;
499 497
500 // The frontend which we serve (and are owned by). 498 // The frontend which we serve (and are owned by).
501 SyncFrontend* frontend_; 499 SyncFrontend* frontend_;
502 500
503 scoped_ptr<PendingConfigureDataTypesState> pending_download_state_;
504 scoped_ptr<PendingConfigureDataTypesState> pending_config_mode_state_;
505
506 // We cache the cryptographer's pending keys whenever NotifyPassphraseRequired 501 // We cache the cryptographer's pending keys whenever NotifyPassphraseRequired
507 // is called. This way, before the UI calls SetDecryptionPassphrase on the 502 // is called. This way, before the UI calls SetDecryptionPassphrase on the
508 // syncer, it can avoid the overhead of an asynchronous decryption call and 503 // syncer, it can avoid the overhead of an asynchronous decryption call and
509 // give the user immediate feedback about the passphrase entered by first 504 // give the user immediate feedback about the passphrase entered by first
510 // trying to decrypt the cached pending keys on the UI thread. Note that 505 // trying to decrypt the cached pending keys on the UI thread. Note that
511 // SetDecryptionPassphrase can still fail after the cached pending keys are 506 // SetDecryptionPassphrase can still fail after the cached pending keys are
512 // successfully decrypted if the pending keys have changed since the time they 507 // successfully decrypted if the pending keys have changed since the time they
513 // were cached. 508 // were cached.
514 sync_pb::EncryptedData cached_pending_keys_; 509 sync_pb::EncryptedData cached_pending_keys_;
515 510
516 // UI-thread cache of the last SyncSessionSnapshot received from syncapi. 511 // UI-thread cache of the last SyncSessionSnapshot received from syncapi.
517 syncer::sessions::SyncSessionSnapshot last_snapshot_; 512 syncer::sessions::SyncSessionSnapshot last_snapshot_;
518 513
519 DISALLOW_COPY_AND_ASSIGN(SyncBackendHost); 514 DISALLOW_COPY_AND_ASSIGN(SyncBackendHost);
520 }; 515 };
521 516
522 } // namespace browser_sync 517 } // namespace browser_sync
523 518
524 #endif // CHROME_BROWSER_SYNC_GLUE_SYNC_BACKEND_HOST_H_ 519 #endif // CHROME_BROWSER_SYNC_GLUE_SYNC_BACKEND_HOST_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698