OLD | NEW |
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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 #include "build/build_config.h" | 5 #include "build/build_config.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 | 8 |
9 #include "base/file_util.h" | 9 #include "base/file_util.h" |
10 #include "base/task.h" | 10 #include "base/task.h" |
11 #include "base/utf_string_conversions.h" | 11 #include "base/utf_string_conversions.h" |
12 #include "chrome/browser/chrome_thread.h" | 12 #include "chrome/browser/chrome_thread.h" |
| 13 #include "chrome/browser/net/gaia/token_service.h" |
13 #include "chrome/browser/prefs/pref_service.h" | 14 #include "chrome/browser/prefs/pref_service.h" |
14 #include "chrome/browser/profile.h" | 15 #include "chrome/browser/profile.h" |
15 #include "chrome/browser/sync/engine/syncapi.h" | 16 #include "chrome/browser/sync/engine/syncapi.h" |
16 #include "chrome/browser/sync/glue/change_processor.h" | 17 #include "chrome/browser/sync/glue/change_processor.h" |
17 #include "chrome/browser/sync/glue/database_model_worker.h" | 18 #include "chrome/browser/sync/glue/database_model_worker.h" |
18 #include "chrome/browser/sync/glue/history_model_worker.h" | 19 #include "chrome/browser/sync/glue/history_model_worker.h" |
19 #include "chrome/browser/sync/glue/sync_backend_host.h" | 20 #include "chrome/browser/sync/glue/sync_backend_host.h" |
20 #include "chrome/browser/sync/glue/http_bridge.h" | 21 #include "chrome/browser/sync/glue/http_bridge.h" |
21 #include "chrome/browser/sync/glue/password_model_worker.h" | 22 #include "chrome/browser/sync/glue/password_model_worker.h" |
22 #include "chrome/browser/sync/sessions/session_state.h" | 23 #include "chrome/browser/sync/sessions/session_state.h" |
23 #include "chrome/common/chrome_version_info.h" | 24 #include "chrome/common/chrome_version_info.h" |
| 25 #include "chrome/common/net/gaia/gaia_constants.h" |
24 #include "chrome/common/notification_service.h" | 26 #include "chrome/common/notification_service.h" |
25 #include "chrome/common/notification_type.h" | 27 #include "chrome/common/notification_type.h" |
26 #include "chrome/common/pref_names.h" | 28 #include "chrome/common/pref_names.h" |
27 #include "webkit/glue/webkit_glue.h" | 29 #include "webkit/glue/webkit_glue.h" |
28 | 30 |
29 static const int kSaveChangesIntervalSeconds = 10; | 31 static const int kSaveChangesIntervalSeconds = 10; |
30 static const char kGaiaServiceId[] = "chromiumsync"; | |
31 static const char kGaiaSourceForChrome[] = "ChromiumBrowser"; | |
32 static const FilePath::CharType kSyncDataFolderName[] = | 32 static const FilePath::CharType kSyncDataFolderName[] = |
33 FILE_PATH_LITERAL("Sync Data"); | 33 FILE_PATH_LITERAL("Sync Data"); |
34 | 34 |
35 using browser_sync::DataTypeController; | 35 using browser_sync::DataTypeController; |
| 36 typedef TokenService::TokenAvailableDetails TokenAvailableDetails; |
36 | 37 |
37 typedef GoogleServiceAuthError AuthError; | 38 typedef GoogleServiceAuthError AuthError; |
38 | 39 |
39 namespace browser_sync { | 40 namespace browser_sync { |
40 | 41 |
41 using sessions::SyncSessionSnapshot; | 42 using sessions::SyncSessionSnapshot; |
| 43 using sync_api::SyncCredentials; |
42 | 44 |
43 SyncBackendHost::SyncBackendHost( | 45 SyncBackendHost::SyncBackendHost( |
44 SyncFrontend* frontend, | 46 SyncFrontend* frontend, |
45 Profile* profile, | 47 Profile* profile, |
46 const FilePath& profile_path, | 48 const FilePath& profile_path, |
47 const DataTypeController::TypeMap& data_type_controllers) | 49 const DataTypeController::TypeMap& data_type_controllers) |
48 : core_thread_("Chrome_SyncCoreThread"), | 50 : core_thread_("Chrome_SyncCoreThread"), |
49 frontend_loop_(MessageLoop::current()), | 51 frontend_loop_(MessageLoop::current()), |
50 profile_(profile), | 52 profile_(profile), |
51 frontend_(frontend), | 53 frontend_(frontend), |
(...skipping 16 matching lines...) Expand all Loading... |
68 | 70 |
69 SyncBackendHost::~SyncBackendHost() { | 71 SyncBackendHost::~SyncBackendHost() { |
70 DCHECK(!core_ && !frontend_) << "Must call Shutdown before destructor."; | 72 DCHECK(!core_ && !frontend_) << "Must call Shutdown before destructor."; |
71 DCHECK(registrar_.workers.empty()); | 73 DCHECK(registrar_.workers.empty()); |
72 } | 74 } |
73 | 75 |
74 void SyncBackendHost::Initialize( | 76 void SyncBackendHost::Initialize( |
75 const GURL& sync_service_url, | 77 const GURL& sync_service_url, |
76 const syncable::ModelTypeSet& types, | 78 const syncable::ModelTypeSet& types, |
77 URLRequestContextGetter* baseline_context_getter, | 79 URLRequestContextGetter* baseline_context_getter, |
78 const std::string& lsid, | 80 const SyncCredentials& credentials, |
79 bool delete_sync_data_folder, | 81 bool delete_sync_data_folder, |
80 bool invalidate_sync_login, | |
81 bool invalidate_sync_xmpp_login, | |
82 bool use_chrome_async_socket, | 82 bool use_chrome_async_socket, |
83 bool try_ssltcp_first, | 83 bool try_ssltcp_first, |
84 NotificationMethod notification_method) { | 84 NotificationMethod notification_method) { |
85 if (!core_thread_.Start()) | 85 if (!core_thread_.Start()) |
86 return; | 86 return; |
87 | 87 |
88 // Create a worker for the UI thread and route bookmark changes to it. | 88 // Create a worker for the UI thread and route bookmark changes to it. |
89 // TODO(tim): Pull this into a method to reuse. For now we don't even | 89 // TODO(tim): Pull this into a method to reuse. For now we don't even |
90 // need to lock because we init before the syncapi exists and we tear down | 90 // need to lock because we init before the syncapi exists and we tear down |
91 // after the syncapi is destroyed. Make sure to NULL-check workers_ indices | 91 // after the syncapi is destroyed. Make sure to NULL-check workers_ indices |
(...skipping 11 matching lines...) Expand all Loading... |
103 | 103 |
104 // Any datatypes that we want the syncer to pull down must | 104 // Any datatypes that we want the syncer to pull down must |
105 // be in the routing_info map. We set them to group passive, meaning that | 105 // be in the routing_info map. We set them to group passive, meaning that |
106 // updates will be applied, but not dispatched to the UI thread yet. | 106 // updates will be applied, but not dispatched to the UI thread yet. |
107 for (syncable::ModelTypeSet::const_iterator it = types.begin(); | 107 for (syncable::ModelTypeSet::const_iterator it = types.begin(); |
108 it != types.end(); ++it) { | 108 it != types.end(); ++it) { |
109 registrar_.routing_info[(*it)] = GROUP_PASSIVE; | 109 registrar_.routing_info[(*it)] = GROUP_PASSIVE; |
110 } | 110 } |
111 | 111 |
112 InitCore(Core::DoInitializeOptions( | 112 InitCore(Core::DoInitializeOptions( |
113 sync_service_url, lsid.empty(), | 113 sync_service_url, |
114 MakeHttpBridgeFactory(baseline_context_getter), | 114 MakeHttpBridgeFactory(baseline_context_getter), |
115 MakeHttpBridgeFactory(baseline_context_getter), | 115 credentials, |
116 lsid, | |
117 delete_sync_data_folder, | 116 delete_sync_data_folder, |
118 invalidate_sync_login, | |
119 invalidate_sync_xmpp_login, | |
120 use_chrome_async_socket, | 117 use_chrome_async_socket, |
121 try_ssltcp_first, | 118 try_ssltcp_first, |
122 notification_method, | 119 notification_method, |
123 RestoreEncryptionBootstrapToken())); | 120 RestoreEncryptionBootstrapToken())); |
124 } | 121 } |
125 | 122 |
126 void SyncBackendHost::PersistEncryptionBootstrapToken( | 123 void SyncBackendHost::PersistEncryptionBootstrapToken( |
127 const std::string& token) { | 124 const std::string& token) { |
128 PrefService* prefs = profile_->GetPrefs(); | 125 PrefService* prefs = profile_->GetPrefs(); |
129 | 126 |
(...skipping 11 matching lines...) Expand all Loading... |
141 URLRequestContextGetter* getter) { | 138 URLRequestContextGetter* getter) { |
142 return new HttpBridgeFactory(getter); | 139 return new HttpBridgeFactory(getter); |
143 } | 140 } |
144 | 141 |
145 void SyncBackendHost::InitCore(const Core::DoInitializeOptions& options) { | 142 void SyncBackendHost::InitCore(const Core::DoInitializeOptions& options) { |
146 core_thread_.message_loop()->PostTask(FROM_HERE, | 143 core_thread_.message_loop()->PostTask(FROM_HERE, |
147 NewRunnableMethod(core_.get(), &SyncBackendHost::Core::DoInitialize, | 144 NewRunnableMethod(core_.get(), &SyncBackendHost::Core::DoInitialize, |
148 options)); | 145 options)); |
149 } | 146 } |
150 | 147 |
151 void SyncBackendHost::Authenticate(const std::string& username, | 148 void SyncBackendHost::UpdateCredentials(const SyncCredentials& credentials) { |
152 const std::string& password, | |
153 const std::string& captcha) { | |
154 core_thread_.message_loop()->PostTask(FROM_HERE, | 149 core_thread_.message_loop()->PostTask(FROM_HERE, |
155 NewRunnableMethod(core_.get(), &SyncBackendHost::Core::DoAuthenticate, | 150 NewRunnableMethod(core_.get(), |
156 username, password, captcha)); | 151 &SyncBackendHost::Core::DoUpdateCredentials, |
| 152 credentials)); |
157 } | 153 } |
158 | 154 |
159 void SyncBackendHost::StartSyncingWithServer() { | 155 void SyncBackendHost::StartSyncingWithServer() { |
160 core_thread_.message_loop()->PostTask(FROM_HERE, | 156 core_thread_.message_loop()->PostTask(FROM_HERE, |
161 NewRunnableMethod(core_.get(), &SyncBackendHost::Core::DoStartSyncing)); | 157 NewRunnableMethod(core_.get(), &SyncBackendHost::Core::DoStartSyncing)); |
162 } | 158 } |
163 | 159 |
164 void SyncBackendHost::SetPassphrase(const std::string& passphrase) { | 160 void SyncBackendHost::SetPassphrase(const std::string& passphrase) { |
165 core_thread_.message_loop()->PostTask(FROM_HERE, | 161 core_thread_.message_loop()->PostTask(FROM_HERE, |
166 NewRunnableMethod(core_.get(), &SyncBackendHost::Core::DoSetPassphrase, | 162 NewRunnableMethod(core_.get(), &SyncBackendHost::Core::DoSetPassphrase, |
(...skipping 170 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
337 | 333 |
338 void SyncBackendHost::Core::NotifyPassphraseAccepted( | 334 void SyncBackendHost::Core::NotifyPassphraseAccepted( |
339 const std::string& bootstrap_token) { | 335 const std::string& bootstrap_token) { |
340 host_->PersistEncryptionBootstrapToken(bootstrap_token); | 336 host_->PersistEncryptionBootstrapToken(bootstrap_token); |
341 NotificationService::current()->Notify( | 337 NotificationService::current()->Notify( |
342 NotificationType::SYNC_PASSPHRASE_ACCEPTED, | 338 NotificationType::SYNC_PASSPHRASE_ACCEPTED, |
343 NotificationService::AllSources(), | 339 NotificationService::AllSources(), |
344 NotificationService::NoDetails()); | 340 NotificationService::NoDetails()); |
345 } | 341 } |
346 | 342 |
| 343 void SyncBackendHost::Core::NotifyUpdatedToken(const std::string& token) { |
| 344 DCHECK(ChromeThread::CurrentlyOn(ChromeThread::UI)); |
| 345 TokenAvailableDetails details(GaiaConstants::kSyncService, token); |
| 346 NotificationService::current()->Notify( |
| 347 NotificationType::TOKEN_UPDATED, |
| 348 NotificationService::AllSources(), |
| 349 Details<const TokenAvailableDetails>(&details)); |
| 350 } |
| 351 |
347 SyncBackendHost::UserShareHandle SyncBackendHost::GetUserShareHandle() const { | 352 SyncBackendHost::UserShareHandle SyncBackendHost::GetUserShareHandle() const { |
348 DCHECK(syncapi_initialized_); | 353 DCHECK(syncapi_initialized_); |
349 return core_->syncapi()->GetUserShare(); | 354 return core_->syncapi()->GetUserShare(); |
350 } | 355 } |
351 | 356 |
352 SyncBackendHost::Status SyncBackendHost::GetDetailedStatus() { | 357 SyncBackendHost::Status SyncBackendHost::GetDetailedStatus() { |
353 DCHECK(syncapi_initialized_); | 358 DCHECK(syncapi_initialized_); |
354 return core_->syncapi()->GetDetailedStatus(); | 359 return core_->syncapi()->GetDetailedStatus(); |
355 } | 360 } |
356 | 361 |
(...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
425 if (!version_info.IsOfficialBuild()) | 430 if (!version_info.IsOfficialBuild()) |
426 user_agent += "-devel"; | 431 user_agent += "-devel"; |
427 return user_agent; | 432 return user_agent; |
428 } | 433 } |
429 | 434 |
430 void SyncBackendHost::Core::DoInitialize(const DoInitializeOptions& options) { | 435 void SyncBackendHost::Core::DoInitialize(const DoInitializeOptions& options) { |
431 DCHECK(MessageLoop::current() == host_->core_thread_.message_loop()); | 436 DCHECK(MessageLoop::current() == host_->core_thread_.message_loop()); |
432 | 437 |
433 // Blow away the partial or corrupt sync data folder before doing any more | 438 // Blow away the partial or corrupt sync data folder before doing any more |
434 // initialization, if necessary. | 439 // initialization, if necessary. |
435 if (options.delete_sync_data_folder) | 440 if (options.delete_sync_data_folder) { |
436 DeleteSyncDataFolder(); | 441 DeleteSyncDataFolder(); |
| 442 } |
437 | 443 |
438 // Make sure that the directory exists before initializing the backend. | 444 // Make sure that the directory exists before initializing the backend. |
439 // If it already exists, this will do no harm. | 445 // If it already exists, this will do no harm. |
440 bool success = file_util::CreateDirectory(host_->sync_data_folder_path()); | 446 bool success = file_util::CreateDirectory(host_->sync_data_folder_path()); |
441 DCHECK(success); | 447 DCHECK(success); |
442 | 448 |
443 syncapi_->SetObserver(this); | 449 syncapi_->SetObserver(this); |
444 const FilePath& path_str = host_->sync_data_folder_path(); | 450 const FilePath& path_str = host_->sync_data_folder_path(); |
445 success = syncapi_->Init(path_str, | 451 success = syncapi_->Init(path_str, |
446 (options.service_url.host() + options.service_url.path()).c_str(), | 452 (options.service_url.host() + options.service_url.path()).c_str(), |
447 options.service_url.EffectiveIntPort(), | 453 options.service_url.EffectiveIntPort(), |
448 kGaiaServiceId, | |
449 kGaiaSourceForChrome, | |
450 options.service_url.SchemeIsSecure(), | 454 options.service_url.SchemeIsSecure(), |
451 options.http_bridge_factory, | 455 options.http_bridge_factory, |
452 options.auth_http_bridge_factory, | |
453 host_, // ModelSafeWorkerRegistrar. | 456 host_, // ModelSafeWorkerRegistrar. |
454 options.attempt_last_user_authentication, | |
455 options.invalidate_sync_login, | |
456 options.invalidate_sync_xmpp_login, | |
457 MakeUserAgentForSyncapi().c_str(), | 457 MakeUserAgentForSyncapi().c_str(), |
458 options.lsid.c_str(), | 458 options.credentials, |
459 options.use_chrome_async_socket, | 459 options.use_chrome_async_socket, |
460 options.try_ssltcp_first, | 460 options.try_ssltcp_first, |
461 options.notification_method, | 461 options.notification_method, |
462 options.restored_key_for_bootstrapping); | 462 options.restored_key_for_bootstrapping); |
463 DCHECK(success) << "Syncapi initialization failed!"; | 463 DCHECK(success) << "Syncapi initialization failed!"; |
464 } | 464 } |
465 | 465 |
466 void SyncBackendHost::Core::DoAuthenticate(const std::string& username, | 466 void SyncBackendHost::Core::DoUpdateCredentials( |
467 const std::string& password, | 467 const SyncCredentials& credentials) { |
468 const std::string& captcha) { | |
469 DCHECK(MessageLoop::current() == host_->core_thread_.message_loop()); | 468 DCHECK(MessageLoop::current() == host_->core_thread_.message_loop()); |
470 syncapi_->Authenticate(username.c_str(), password.c_str(), captcha.c_str()); | 469 syncapi_->UpdateCredentials(credentials); |
471 } | 470 } |
472 | 471 |
473 void SyncBackendHost::Core::DoStartSyncing() { | 472 void SyncBackendHost::Core::DoStartSyncing() { |
474 DCHECK(MessageLoop::current() == host_->core_thread_.message_loop()); | 473 DCHECK(MessageLoop::current() == host_->core_thread_.message_loop()); |
475 syncapi_->StartSyncing(); | 474 syncapi_->StartSyncing(); |
476 } | 475 } |
477 | 476 |
478 void SyncBackendHost::Core::DoSetPassphrase(const std::string& passphrase) { | 477 void SyncBackendHost::Core::DoSetPassphrase(const std::string& passphrase) { |
479 DCHECK(MessageLoop::current() == host_->core_thread_.message_loop()); | 478 DCHECK(MessageLoop::current() == host_->core_thread_.message_loop()); |
480 syncapi_->SetPassphrase(passphrase); | 479 syncapi_->SetPassphrase(passphrase); |
(...skipping 136 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
617 browser_sync::ModelSafeGroup group = routing_it->second; | 616 browser_sync::ModelSafeGroup group = routing_it->second; |
618 WorkerMap::const_iterator worker_it = host_->registrar_.workers.find(group); | 617 WorkerMap::const_iterator worker_it = host_->registrar_.workers.find(group); |
619 if (worker_it == host_->registrar_.workers.end()) | 618 if (worker_it == host_->registrar_.workers.end()) |
620 return false; | 619 return false; |
621 ModelSafeWorker* worker = worker_it->second; | 620 ModelSafeWorker* worker = worker_it->second; |
622 return worker->CurrentThreadIsWorkThread(); | 621 return worker->CurrentThreadIsWorkThread(); |
623 } | 622 } |
624 | 623 |
625 | 624 |
626 void SyncBackendHost::Core::OnAuthError(const AuthError& auth_error) { | 625 void SyncBackendHost::Core::OnAuthError(const AuthError& auth_error) { |
627 // We could be on SyncEngine_AuthWatcherThread. Post to our core loop so | 626 // Post to our core loop so we can modify state. Could be on another thread. |
628 // we can modify state. | |
629 host_->frontend_loop_->PostTask(FROM_HERE, | 627 host_->frontend_loop_->PostTask(FROM_HERE, |
630 NewRunnableMethod(this, &Core::HandleAuthErrorEventOnFrontendLoop, | 628 NewRunnableMethod(this, &Core::HandleAuthErrorEventOnFrontendLoop, |
631 auth_error)); | 629 auth_error)); |
632 } | 630 } |
633 | 631 |
634 void SyncBackendHost::Core::OnPassphraseRequired() { | 632 void SyncBackendHost::Core::OnPassphraseRequired() { |
635 host_->frontend_loop_->PostTask(FROM_HERE, | 633 host_->frontend_loop_->PostTask(FROM_HERE, |
636 NewRunnableMethod(this, &Core::NotifyPassphraseRequired)); | 634 NewRunnableMethod(this, &Core::NotifyPassphraseRequired)); |
637 } | 635 } |
638 | 636 |
(...skipping 14 matching lines...) Expand all Loading... |
653 host_->frontend_loop_->PostTask( | 651 host_->frontend_loop_->PostTask( |
654 FROM_HERE, | 652 FROM_HERE, |
655 NewRunnableMethod(this, &Core::NotifyResumed)); | 653 NewRunnableMethod(this, &Core::NotifyResumed)); |
656 } | 654 } |
657 | 655 |
658 void SyncBackendHost::Core::OnStopSyncingPermanently() { | 656 void SyncBackendHost::Core::OnStopSyncingPermanently() { |
659 host_->frontend_loop_->PostTask(FROM_HERE, NewRunnableMethod(this, | 657 host_->frontend_loop_->PostTask(FROM_HERE, NewRunnableMethod(this, |
660 &Core::HandleStopSyncingPermanentlyOnFrontendLoop)); | 658 &Core::HandleStopSyncingPermanentlyOnFrontendLoop)); |
661 } | 659 } |
662 | 660 |
| 661 void SyncBackendHost::Core::OnUpdatedToken(const std::string& token) { |
| 662 host_->frontend_loop_->PostTask(FROM_HERE, NewRunnableMethod(this, |
| 663 &Core::NotifyUpdatedToken, token)); |
| 664 } |
| 665 |
663 void SyncBackendHost::Core::HandleStopSyncingPermanentlyOnFrontendLoop() { | 666 void SyncBackendHost::Core::HandleStopSyncingPermanentlyOnFrontendLoop() { |
664 if (!host_ || !host_->frontend_) | 667 if (!host_ || !host_->frontend_) |
665 return; | 668 return; |
666 host_->frontend_->OnStopSyncingPermanently(); | 669 host_->frontend_->OnStopSyncingPermanently(); |
667 } | 670 } |
668 | 671 |
669 void SyncBackendHost::Core::HandleAuthErrorEventOnFrontendLoop( | 672 void SyncBackendHost::Core::HandleAuthErrorEventOnFrontendLoop( |
670 const GoogleServiceAuthError& new_auth_error) { | 673 const GoogleServiceAuthError& new_auth_error) { |
671 if (!host_ || !host_->frontend_) | 674 if (!host_ || !host_->frontend_) |
672 return; | 675 return; |
(...skipping 27 matching lines...) Expand all Loading... |
700 } | 703 } |
701 | 704 |
702 void SyncBackendHost::Core::DeleteSyncDataFolder() { | 705 void SyncBackendHost::Core::DeleteSyncDataFolder() { |
703 if (file_util::DirectoryExists(host_->sync_data_folder_path())) { | 706 if (file_util::DirectoryExists(host_->sync_data_folder_path())) { |
704 if (!file_util::Delete(host_->sync_data_folder_path(), true)) | 707 if (!file_util::Delete(host_->sync_data_folder_path(), true)) |
705 LOG(DFATAL) << "Could not delete the Sync Data folder."; | 708 LOG(DFATAL) << "Could not delete the Sync Data folder."; |
706 } | 709 } |
707 } | 710 } |
708 | 711 |
709 } // namespace browser_sync | 712 } // namespace browser_sync |
OLD | NEW |