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

Side by Side Diff: chrome/browser/sync/engine/syncapi.cc

Issue 386030: Relieve SyncerSession,SyncCycleState, SyncProcessState, SyncerSession, Syncer... (Closed) Base URL: svn://chrome-svn/chrome/trunk/src/
Patch Set: '' Created 11 years, 1 month 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
« no previous file with comments | « chrome/browser/sync/engine/sync_process_state.cc ('k') | chrome/browser/sync/engine/syncer.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2006-2009 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2006-2009 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 "chrome/browser/sync/engine/syncapi.h" 5 #include "chrome/browser/sync/engine/syncapi.h"
6 6
7 #include "build/build_config.h" 7 #include "build/build_config.h"
8 8
9 #if defined(OS_WIN) 9 #if defined(OS_WIN)
10 #include <windows.h> 10 #include <windows.h>
11 #include <iphlpapi.h> 11 #include <iphlpapi.h>
12 #endif 12 #endif
13 13
14 #include <iomanip> 14 #include <iomanip>
15 #include <list> 15 #include <list>
16 #include <string> 16 #include <string>
17 #include <vector> 17 #include <vector>
18 18
19 #include "base/basictypes.h" 19 #include "base/basictypes.h"
20 #include "base/command_line.h" 20 #include "base/command_line.h"
21 #include "base/platform_thread.h" 21 #include "base/platform_thread.h"
22 #include "base/scoped_ptr.h" 22 #include "base/scoped_ptr.h"
23 #include "base/string_util.h" 23 #include "base/string_util.h"
24 #include "base/task.h" 24 #include "base/task.h"
25 #include "base/utf_string_conversions.h" 25 #include "base/utf_string_conversions.h"
26 #include "chrome/browser/sync/engine/all_status.h" 26 #include "chrome/browser/sync/engine/all_status.h"
27 #include "chrome/browser/sync/engine/auth_watcher.h" 27 #include "chrome/browser/sync/engine/auth_watcher.h"
28 #include "chrome/browser/sync/engine/change_reorder_buffer.h" 28 #include "chrome/browser/sync/engine/change_reorder_buffer.h"
29 #include "chrome/browser/sync/engine/client_command_channel.h"
30 #include "chrome/browser/sync/engine/model_safe_worker.h" 29 #include "chrome/browser/sync/engine/model_safe_worker.h"
31 #include "chrome/browser/sync/engine/net/gaia_authenticator.h" 30 #include "chrome/browser/sync/engine/net/gaia_authenticator.h"
32 #include "chrome/browser/sync/engine/net/server_connection_manager.h" 31 #include "chrome/browser/sync/engine/net/server_connection_manager.h"
33 #include "chrome/browser/sync/engine/net/syncapi_server_connection_manager.h" 32 #include "chrome/browser/sync/engine/net/syncapi_server_connection_manager.h"
34 #include "chrome/browser/sync/engine/syncer.h" 33 #include "chrome/browser/sync/engine/syncer.h"
35 #include "chrome/browser/sync/engine/syncer_thread.h" 34 #include "chrome/browser/sync/engine/syncer_thread.h"
36 #include "chrome/browser/sync/notifier/listener/talk_mediator.h" 35 #include "chrome/browser/sync/notifier/listener/talk_mediator.h"
37 #include "chrome/browser/sync/notifier/listener/talk_mediator_impl.h" 36 #include "chrome/browser/sync/notifier/listener/talk_mediator_impl.h"
38 #include "chrome/browser/sync/protocol/service_constants.h" 37 #include "chrome/browser/sync/protocol/service_constants.h"
38 #include "chrome/browser/sync/sessions/sync_session_context.h"
39 #include "chrome/browser/sync/syncable/directory_manager.h" 39 #include "chrome/browser/sync/syncable/directory_manager.h"
40 #include "chrome/browser/sync/syncable/syncable.h" 40 #include "chrome/browser/sync/syncable/syncable.h"
41 #include "chrome/browser/sync/util/character_set_converters.h" 41 #include "chrome/browser/sync/util/character_set_converters.h"
42 #include "chrome/browser/sync/util/closure.h" 42 #include "chrome/browser/sync/util/closure.h"
43 #include "chrome/browser/sync/util/crypto_helpers.h" 43 #include "chrome/browser/sync/util/crypto_helpers.h"
44 #include "chrome/browser/sync/util/event_sys.h" 44 #include "chrome/browser/sync/util/event_sys.h"
45 #include "chrome/browser/sync/util/path_helpers.h" 45 #include "chrome/browser/sync/util/path_helpers.h"
46 #include "chrome/browser/sync/util/user_settings.h" 46 #include "chrome/browser/sync/util/user_settings.h"
47 47
48 #if defined(OS_WIN) 48 #if defined(OS_WIN)
49 #pragma comment(lib, "iphlpapi.lib") 49 #pragma comment(lib, "iphlpapi.lib")
50 #endif 50 #endif
51 51
52 using browser_sync::AllStatus; 52 using browser_sync::AllStatus;
53 using browser_sync::AllStatusEvent; 53 using browser_sync::AllStatusEvent;
54 using browser_sync::AuthWatcher; 54 using browser_sync::AuthWatcher;
55 using browser_sync::AuthWatcherEvent; 55 using browser_sync::AuthWatcherEvent;
56 using browser_sync::ClientCommandChannel;
57 using browser_sync::Syncer; 56 using browser_sync::Syncer;
58 using browser_sync::SyncerEvent; 57 using browser_sync::SyncerEvent;
59 using browser_sync::SyncerStatus;
60 using browser_sync::SyncerThread; 58 using browser_sync::SyncerThread;
61 using browser_sync::SyncerThreadFactory;
62 using browser_sync::UserSettings; 59 using browser_sync::UserSettings;
63 using browser_sync::TalkMediator; 60 using browser_sync::TalkMediator;
64 using browser_sync::TalkMediatorImpl; 61 using browser_sync::TalkMediatorImpl;
62 using browser_sync::sessions::SyncSessionContext;
65 using std::list; 63 using std::list;
66 using std::hex; 64 using std::hex;
67 using std::string; 65 using std::string;
68 using std::vector; 66 using std::vector;
69 using syncable::Directory; 67 using syncable::Directory;
70 using syncable::DirectoryManager; 68 using syncable::DirectoryManager;
71 69
72 typedef GoogleServiceAuthError AuthError; 70 typedef GoogleServiceAuthError AuthError;
73 71
74 static const int kServerReachablePollingIntervalMsec = 60000 * 60; 72 static const int kServerReachablePollingIntervalMsec = 60000 * 60;
(...skipping 566 matching lines...) Expand 10 before | Expand all | Expand 10 after
641 scoped_ptr<HttpPostProviderFactory> post_factory_; 639 scoped_ptr<HttpPostProviderFactory> post_factory_;
642 DISALLOW_COPY_AND_ASSIGN(BridgedGaiaAuthenticator); 640 DISALLOW_COPY_AND_ASSIGN(BridgedGaiaAuthenticator);
643 }; 641 };
644 642
645 ////////////////////////////////////////////////////////////////////////// 643 //////////////////////////////////////////////////////////////////////////
646 // SyncManager's implementation: SyncManager::SyncInternal 644 // SyncManager's implementation: SyncManager::SyncInternal
647 class SyncManager::SyncInternal { 645 class SyncManager::SyncInternal {
648 public: 646 public:
649 explicit SyncInternal(SyncManager* sync_manager) 647 explicit SyncInternal(SyncManager* sync_manager)
650 : observer_(NULL), 648 : observer_(NULL),
651 command_channel_(0),
652 auth_problem_(AuthError::NONE), 649 auth_problem_(AuthError::NONE),
653 sync_manager_(sync_manager), 650 sync_manager_(sync_manager),
654 address_watch_thread_("SyncEngine_AddressWatcher"), 651 address_watch_thread_("SyncEngine_AddressWatcher"),
655 notification_pending_(false), 652 notification_pending_(false),
656 initialized_(false) { 653 initialized_(false) {
657 } 654 }
658 655
659 ~SyncInternal() { } 656 ~SyncInternal() { }
660 657
661 bool Init(const FilePath& database_location, 658 bool Init(const FilePath& database_location,
(...skipping 150 matching lines...) Expand 10 before | Expand all | Expand 10 after
812 UserShare share_; 809 UserShare share_;
813 810
814 // A wrapper around a sqlite store used for caching authentication data, 811 // A wrapper around a sqlite store used for caching authentication data,
815 // last user information, current sync-related URLs, and more. 812 // last user information, current sync-related URLs, and more.
816 scoped_ptr<UserSettings> user_settings_; 813 scoped_ptr<UserSettings> user_settings_;
817 814
818 // Observer registered via SetObserver/RemoveObserver. 815 // Observer registered via SetObserver/RemoveObserver.
819 // WARNING: This can be NULL! 816 // WARNING: This can be NULL!
820 Observer* observer_; 817 Observer* observer_;
821 818
822 // A sink for client commands from the syncer needed to create a SyncerThread.
823 ClientCommandChannel command_channel_;
824
825 // The ServerConnectionManager used to abstract communication between the 819 // The ServerConnectionManager used to abstract communication between the
826 // client (the Syncer) and the sync server. 820 // client (the Syncer) and the sync server.
827 scoped_ptr<SyncAPIServerConnectionManager> connection_manager_; 821 scoped_ptr<SyncAPIServerConnectionManager> connection_manager_;
828 822
829 // The thread that runs the Syncer. Needs to be explicitly Start()ed. 823 // The thread that runs the Syncer. Needs to be explicitly Start()ed.
830 scoped_refptr<SyncerThread> syncer_thread_; 824 scoped_refptr<SyncerThread> syncer_thread_;
831 825
832 // Notification (xmpp) handler. 826 // Notification (xmpp) handler.
833 scoped_ptr<TalkMediator> talk_mediator_; 827 scoped_ptr<TalkMediator> talk_mediator_;
834 828
(...skipping 155 matching lines...) Expand 10 before | Expand all | Expand 10 after
990 gaia_url, 984 gaia_url,
991 user_settings_.get(), 985 user_settings_.get(),
992 gaia_auth, 986 gaia_auth,
993 talk_mediator()); 987 talk_mediator());
994 988
995 talk_mediator()->WatchAuthWatcher(auth_watcher()); 989 talk_mediator()->WatchAuthWatcher(auth_watcher());
996 allstatus()->WatchAuthWatcher(auth_watcher()); 990 allstatus()->WatchAuthWatcher(auth_watcher());
997 authwatcher_hookup_.reset(NewEventListenerHookup(auth_watcher_->channel(), 991 authwatcher_hookup_.reset(NewEventListenerHookup(auth_watcher_->channel(),
998 this, &SyncInternal::HandleAuthWatcherEvent)); 992 this, &SyncInternal::HandleAuthWatcherEvent));
999 993
1000 // Tell the SyncerThread to use the ModelSafeWorker for bookmark model work. 994 // Build a SyncSessionContext and store the worker in it.
1001 // We set up both sides of the "bridge" here, with the ModelSafeWorkerBridge 995 // We set up both sides of the "bridge" here, with the ModelSafeWorkerBridge
1002 // on the Syncer side, and |model_safe_worker| on the API client side. 996 // on the Syncer side, and |model_safe_worker| on the API client side.
1003 ModelSafeWorkerBridge* worker = new ModelSafeWorkerBridge(model_safe_worker); 997 ModelSafeWorkerBridge* worker = new ModelSafeWorkerBridge(model_safe_worker);
998 SyncSessionContext* context = new SyncSessionContext(
999 connection_manager_.get(), dir_manager(), worker);
1004 1000
1005 syncer_thread_ = SyncerThreadFactory::Create(&command_channel_, 1001 // The SyncerThread takes ownership of |context|.
1006 dir_manager(), 1002 syncer_thread_ = new SyncerThread(context, &allstatus_);
1007 connection_manager(),
1008 &allstatus_,
1009 worker);
1010 syncer_thread()->WatchTalkMediator(talk_mediator()); 1003 syncer_thread()->WatchTalkMediator(talk_mediator());
1011 allstatus()->WatchSyncerThread(syncer_thread()); 1004 allstatus()->WatchSyncerThread(syncer_thread());
1012 1005
1013 syncer_thread()->Start(); // Start the syncer thread. This won't actually 1006 syncer_thread()->Start(); // Start the syncer thread. This won't actually
1014 // result in any syncing until at least the 1007 // result in any syncing until at least the
1015 // DirectoryManager broadcasts the OPENED event, 1008 // DirectoryManager broadcasts the OPENED event,
1016 // and a valid server connection is detected. 1009 // and a valid server connection is detected.
1017 1010
1018 if (attempt_last_user_authentication) 1011 if (attempt_last_user_authentication)
1019 AuthenticateForLastKnownUser(); 1012 AuthenticateForLastKnownUser();
(...skipping 288 matching lines...) Expand 10 before | Expand all | Expand 10 after
1308 allstatus()->status().max_consecutive_errors}; 1301 allstatus()->status().max_consecutive_errors};
1309 return return_status; 1302 return return_status;
1310 } 1303 }
1311 1304
1312 void SyncManager::SyncInternal::HandleSyncerEvent(const SyncerEvent& event) { 1305 void SyncManager::SyncInternal::HandleSyncerEvent(const SyncerEvent& event) {
1313 if (!initialized()) { 1306 if (!initialized()) {
1314 // We get here if A) We have successfully authenticated at least once 1307 // We get here if A) We have successfully authenticated at least once
1315 // (because we attach HandleSyncerEvent only once we receive notification 1308 // (because we attach HandleSyncerEvent only once we receive notification
1316 // of successful authentication [locally or otherwise]), but B) the initial 1309 // of successful authentication [locally or otherwise]), but B) the initial
1317 // sync had not completed at that time. 1310 // sync had not completed at that time.
1318 if (SyncerStatus(event.last_session).IsShareUsable()) 1311 if (event.snapshot->is_share_usable)
1319 MarkAndNotifyInitializationComplete(); 1312 MarkAndNotifyInitializationComplete();
1320 return; 1313 return;
1321 } 1314 }
1322 1315
1323 if (!observer_) 1316 if (!observer_)
1324 return; 1317 return;
1325 1318
1326 // Only send an event if this is due to a cycle ending and this cycle 1319 // Only send an event if this is due to a cycle ending and this cycle
1327 // concludes a canonical "sync" process; that is, based on what is known 1320 // concludes a canonical "sync" process; that is, based on what is known
1328 // locally we are "all happy" and up-to-date. There may be new changes on 1321 // locally we are "all happy" and up-to-date. There may be new changes on
1329 // the server, but we'll get them on a subsequent sync. 1322 // the server, but we'll get them on a subsequent sync.
1330 // 1323 //
1331 // Notifications are sent at the end of every sync cycle, regardless of 1324 // Notifications are sent at the end of every sync cycle, regardless of
1332 // whether we should sync again. 1325 // whether we should sync again.
1333 if (event.what_happened == SyncerEvent::SYNC_CYCLE_ENDED) { 1326 if (event.what_happened == SyncerEvent::SYNC_CYCLE_ENDED) {
1334 if (!event.last_session->HasMoreToSync()) { 1327 if (!event.snapshot->has_more_to_sync) {
1335 observer_->OnSyncCycleCompleted(); 1328 observer_->OnSyncCycleCompleted();
1336 } 1329 }
1337 1330
1338 // TODO(chron): Consider changing this back to track HasMoreToSync 1331 // TODO(chron): Consider changing this back to track has_more_to_sync
1339 // Only notify peers if a successful commit has occurred. 1332 // only notify peers if a successful commit has occurred.
1340 if (event.last_session && event.last_session->HadSuccessfulCommits()) { 1333 if (event.snapshot->syncer_status.num_successful_commits > 0) {
1341 // We use a member variable here because talk may not have connected yet. 1334 // We use a member variable here because talk may not have connected yet.
1342 // The notification must be stored until it can be sent. 1335 // The notification must be stored until it can be sent.
1343 notification_pending_ = true; 1336 notification_pending_ = true;
1344 } 1337 }
1345 1338
1346 // SyncCycles are started by the following events: creation of the syncer, 1339 // SyncCycles are started by the following events: creation of the syncer,
1347 // (re)connection to buzz, local changes, peer notifications of updates. 1340 // (re)connection to buzz, local changes, peer notifications of updates.
1348 // Peers will be notified of changes made while there is no buzz connection 1341 // Peers will be notified of changes made while there is no buzz connection
1349 // immediately after a connection has been re-established. 1342 // immediately after a connection has been re-established.
1350 // the next sync cycle. 1343 // the next sync cycle.
1351 // TODO(brg): Move this to TalkMediatorImpl as a SyncerThread event hook. 1344 // TODO(brg): Move this to TalkMediatorImpl as a SyncerThread event hook.
1352 if (notification_pending_ && talk_mediator()) { 1345 if (notification_pending_ && talk_mediator()) {
1353 LOG(INFO) << "Sending XMPP notification..."; 1346 LOG(INFO) << "Sending XMPP notification...";
1354 bool success = talk_mediator()->SendNotification(); 1347 bool success = talk_mediator()->SendNotification();
1355 if (success) { 1348 if (success) {
1356 notification_pending_ = false; 1349 notification_pending_ = false;
1357 } 1350 }
1358 } else { 1351 } else {
1359 LOG(INFO) << "Didn't send XMPP notification!" 1352 LOG(INFO) << "Didn't send XMPP notification!"
1360 << " event.last_session: " << event.last_session 1353 << " event.snapshot.did_commit_items: "
1361 << " event.last_session->items_committed(): " 1354 << event.snapshot->did_commit_items
1362 << event.last_session->items_committed()
1363 << " talk_mediator(): " << talk_mediator(); 1355 << " talk_mediator(): " << talk_mediator();
1364 } 1356 }
1365 } 1357 }
1366 } 1358 }
1367 1359
1368 void SyncManager::SyncInternal::HandleAuthWatcherEvent( 1360 void SyncManager::SyncInternal::HandleAuthWatcherEvent(
1369 const AuthWatcherEvent& event) { 1361 const AuthWatcherEvent& event) {
1370 // We don't care about an authentication attempt starting event, and we 1362 // We don't care about an authentication attempt starting event, and we
1371 // don't want to reset our state to GoogleServiceAuthError::NONE because the 1363 // don't want to reset our state to GoogleServiceAuthError::NONE because the
1372 // fact that an _attempt_ is starting doesn't change the fact that we have an 1364 // fact that an _attempt_ is starting doesn't change the fact that we have an
(...skipping 30 matching lines...) Expand all
1403 dir_change_hookup_.reset(NewEventListenerHookup( 1395 dir_change_hookup_.reset(NewEventListenerHookup(
1404 lookup->changes_channel(), this, 1396 lookup->changes_channel(), this,
1405 &SyncInternal::HandleChangeEvent)); 1397 &SyncInternal::HandleChangeEvent));
1406 1398
1407 if (lookup->initial_sync_ended()) 1399 if (lookup->initial_sync_ended())
1408 MarkAndNotifyInitializationComplete(); 1400 MarkAndNotifyInitializationComplete();
1409 } 1401 }
1410 { 1402 {
1411 // Start watching the syncer channel directly here. 1403 // Start watching the syncer channel directly here.
1412 DCHECK(syncer_thread() != NULL); 1404 DCHECK(syncer_thread() != NULL);
1413 syncer_event_.reset(NewEventListenerHookup(syncer_thread()->channel(), 1405 syncer_event_.reset(
1414 this, &SyncInternal::HandleSyncerEvent)); 1406 NewEventListenerHookup(syncer_thread()->relay_channel(), this,
1407 &SyncInternal::HandleSyncerEvent));
1415 } 1408 }
1416 return; 1409 return;
1417 // Authentication failures translate to GoogleServiceAuthError events. 1410 // Authentication failures translate to GoogleServiceAuthError events.
1418 case AuthWatcherEvent::GAIA_AUTH_FAILED: // Invalid GAIA credentials. 1411 case AuthWatcherEvent::GAIA_AUTH_FAILED: // Invalid GAIA credentials.
1419 if (event.auth_results->auth_error == browser_sync::CaptchaRequired) { 1412 if (event.auth_results->auth_error == browser_sync::CaptchaRequired) {
1420 auth_problem_ = AuthError::CAPTCHA_REQUIRED; 1413 auth_problem_ = AuthError::CAPTCHA_REQUIRED;
1421 std::string url_string("http://www.google.com/accounts/"); 1414 std::string url_string("http://www.google.com/accounts/");
1422 url_string += event.auth_results->captcha_url; 1415 url_string += event.auth_results->captcha_url;
1423 GURL captcha(url_string); 1416 GURL captcha(url_string);
1424 observer_->OnAuthError(AuthError::FromCaptchaChallenge( 1417 observer_->OnAuthError(AuthError::FromCaptchaChallenge(
(...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after
1508 BaseTransaction::~BaseTransaction() { 1501 BaseTransaction::~BaseTransaction() {
1509 delete lookup_; 1502 delete lookup_;
1510 } 1503 }
1511 1504
1512 UserShare* SyncManager::GetUserShare() const { 1505 UserShare* SyncManager::GetUserShare() const {
1513 DCHECK(data_->initialized()) << "GetUserShare requires initialization!"; 1506 DCHECK(data_->initialized()) << "GetUserShare requires initialization!";
1514 return data_->GetUserShare(); 1507 return data_->GetUserShare();
1515 } 1508 }
1516 1509
1517 } // namespace sync_api 1510 } // namespace sync_api
OLDNEW
« no previous file with comments | « chrome/browser/sync/engine/sync_process_state.cc ('k') | chrome/browser/sync/engine/syncer.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698