OLD | NEW |
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 #include <iomanip> | 9 #include <iomanip> |
10 #include <list> | 10 #include <list> |
(...skipping 759 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
770 private: | 770 private: |
771 const std::string gaia_source_; | 771 const std::string gaia_source_; |
772 scoped_ptr<HttpPostProviderFactory> post_factory_; | 772 scoped_ptr<HttpPostProviderFactory> post_factory_; |
773 DISALLOW_COPY_AND_ASSIGN(BridgedGaiaAuthenticator); | 773 DISALLOW_COPY_AND_ASSIGN(BridgedGaiaAuthenticator); |
774 }; | 774 }; |
775 | 775 |
776 ////////////////////////////////////////////////////////////////////////// | 776 ////////////////////////////////////////////////////////////////////////// |
777 // SyncManager's implementation: SyncManager::SyncInternal | 777 // SyncManager's implementation: SyncManager::SyncInternal |
778 class SyncManager::SyncInternal | 778 class SyncManager::SyncInternal |
779 : public net::NetworkChangeNotifier::Observer, | 779 : public net::NetworkChangeNotifier::Observer, |
780 public TalkMediator::Delegate { | 780 public TalkMediator::Delegate, |
| 781 public browser_sync::ChannelEventHandler<syncable::DirectoryChangeEvent>, |
| 782 public browser_sync::ChannelEventHandler<SyncerEvent>{ |
781 static const int kDefaultNudgeDelayMilliseconds; | 783 static const int kDefaultNudgeDelayMilliseconds; |
782 static const int kPreferencesNudgeDelayMilliseconds; | 784 static const int kPreferencesNudgeDelayMilliseconds; |
783 public: | 785 public: |
784 explicit SyncInternal(SyncManager* sync_manager) | 786 explicit SyncInternal(SyncManager* sync_manager) |
785 : core_message_loop_(NULL), | 787 : core_message_loop_(NULL), |
786 observer_(NULL), | 788 observer_(NULL), |
787 auth_problem_(AuthError::NONE), | 789 auth_problem_(AuthError::NONE), |
788 sync_manager_(sync_manager), | 790 sync_manager_(sync_manager), |
789 registrar_(NULL), | 791 registrar_(NULL), |
790 notification_pending_(false), | 792 notification_pending_(false), |
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
838 // Tell the sync engine to start the syncing process. | 840 // Tell the sync engine to start the syncing process. |
839 void StartSyncing(); | 841 void StartSyncing(); |
840 | 842 |
841 // Call periodically from a database-safe thread to persist recent changes | 843 // Call periodically from a database-safe thread to persist recent changes |
842 // to the syncapi model. | 844 // to the syncapi model. |
843 void SaveChanges(); | 845 void SaveChanges(); |
844 | 846 |
845 // This listener is called upon completion of a syncable transaction, and | 847 // This listener is called upon completion of a syncable transaction, and |
846 // builds the list of sync-engine initiated changes that will be forwarded to | 848 // builds the list of sync-engine initiated changes that will be forwarded to |
847 // the SyncManager's Observers. | 849 // the SyncManager's Observers. |
848 void HandleChangeEvent(const syncable::DirectoryChangeEvent& event); | 850 virtual void HandleChannelEvent(const syncable::DirectoryChangeEvent& event); |
849 void HandleTransactionCompleteChangeEvent( | 851 void HandleTransactionCompleteChangeEvent( |
850 const syncable::DirectoryChangeEvent& event); | 852 const syncable::DirectoryChangeEvent& event); |
851 void HandleCalculateChangesChangeEventFromSyncApi( | 853 void HandleCalculateChangesChangeEventFromSyncApi( |
852 const syncable::DirectoryChangeEvent& event); | 854 const syncable::DirectoryChangeEvent& event); |
853 void HandleCalculateChangesChangeEventFromSyncer( | 855 void HandleCalculateChangesChangeEventFromSyncer( |
854 const syncable::DirectoryChangeEvent& event); | 856 const syncable::DirectoryChangeEvent& event); |
855 | 857 |
856 // This listener is called by the syncer channel for all syncer events. | 858 // This listener is called by the syncer channel for all syncer events. |
857 void HandleSyncerEvent(const SyncerEvent& event); | 859 virtual void HandleChannelEvent(const SyncerEvent& event); |
858 | 860 |
859 // We have a direct hookup to the authwatcher to be notified for auth failures | 861 // We have a direct hookup to the authwatcher to be notified for auth failures |
860 // on startup, to serve our UI needs. | 862 // on startup, to serve our UI needs. |
861 void HandleAuthWatcherEvent(const AuthWatcherEvent& event); | 863 void HandleAuthWatcherEvent(const AuthWatcherEvent& event); |
862 | 864 |
863 // Login to the talk mediator with the given credentials. | 865 // Login to the talk mediator with the given credentials. |
864 void TalkMediatorLogin( | 866 void TalkMediatorLogin( |
865 const std::string& email, const std::string& token); | 867 const std::string& email, const std::string& token); |
866 | 868 |
867 // TalkMediator::Delegate implementation. | 869 // TalkMediator::Delegate implementation. |
(...skipping 193 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1061 scoped_refptr<AuthWatcher> auth_watcher_; | 1063 scoped_refptr<AuthWatcher> auth_watcher_; |
1062 | 1064 |
1063 // Each element of this array is a store of change records produced by | 1065 // Each element of this array is a store of change records produced by |
1064 // HandleChangeEvent during the CALCULATE_CHANGES step. The changes are | 1066 // HandleChangeEvent during the CALCULATE_CHANGES step. The changes are |
1065 // segregated by model type, and are stored here to be processed and | 1067 // segregated by model type, and are stored here to be processed and |
1066 // forwarded to the observer slightly later, at the TRANSACTION_COMPLETE | 1068 // forwarded to the observer slightly later, at the TRANSACTION_COMPLETE |
1067 // step by HandleTransactionCompleteChangeEvent. | 1069 // step by HandleTransactionCompleteChangeEvent. |
1068 ChangeReorderBuffer change_buffers_[syncable::MODEL_TYPE_COUNT]; | 1070 ChangeReorderBuffer change_buffers_[syncable::MODEL_TYPE_COUNT]; |
1069 | 1071 |
1070 // The event listener hookup that is registered for HandleChangeEvent. | 1072 // The event listener hookup that is registered for HandleChangeEvent. |
1071 scoped_ptr<EventListenerHookup> dir_change_hookup_; | 1073 scoped_ptr<browser_sync::ChannelHookup<syncable::DirectoryChangeEvent> > |
| 1074 dir_change_hookup_; |
1072 | 1075 |
1073 // The event listener hookup registered for HandleSyncerEvent. | 1076 // The event listener hookup registered for HandleSyncerEvent. |
1074 scoped_ptr<EventListenerHookup> syncer_event_; | 1077 scoped_ptr<browser_sync::ChannelHookup<SyncerEvent> > syncer_event_; |
1075 | 1078 |
1076 // The event listener hookup registered for HandleAuthWatcherEvent. | 1079 // The event listener hookup registered for HandleAuthWatcherEvent. |
1077 scoped_ptr<EventListenerHookup> authwatcher_hookup_; | 1080 scoped_ptr<EventListenerHookup> authwatcher_hookup_; |
1078 | 1081 |
1079 // Our cache of a recent authentication problem. If no authentication problem | 1082 // Our cache of a recent authentication problem. If no authentication problem |
1080 // occurred, or if the last problem encountered has been cleared (by a | 1083 // occurred, or if the last problem encountered has been cleared (by a |
1081 // subsequent AuthWatcherEvent), this is set to NONE. | 1084 // subsequent AuthWatcherEvent), this is set to NONE. |
1082 AuthError::State auth_problem_; | 1085 AuthError::State auth_problem_; |
1083 | 1086 |
1084 // The sync dir_manager to which we belong. | 1087 // The sync dir_manager to which we belong. |
(...skipping 183 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1268 // Build a SyncSessionContext and store the worker in it. | 1271 // Build a SyncSessionContext and store the worker in it. |
1269 SyncSessionContext* context = new SyncSessionContext( | 1272 SyncSessionContext* context = new SyncSessionContext( |
1270 connection_manager_.get(), auth_watcher(), | 1273 connection_manager_.get(), auth_watcher(), |
1271 dir_manager(), model_safe_worker_registrar); | 1274 dir_manager(), model_safe_worker_registrar); |
1272 | 1275 |
1273 // The SyncerThread takes ownership of |context|. | 1276 // The SyncerThread takes ownership of |context|. |
1274 syncer_thread_ = new SyncerThread(context, &allstatus_); | 1277 syncer_thread_ = new SyncerThread(context, &allstatus_); |
1275 allstatus_.WatchSyncerThread(syncer_thread()); | 1278 allstatus_.WatchSyncerThread(syncer_thread()); |
1276 | 1279 |
1277 // Subscribe to the syncer thread's channel. | 1280 // Subscribe to the syncer thread's channel. |
1278 syncer_event_.reset( | 1281 syncer_event_.reset(syncer_thread()->relay_channel()->AddObserver(this)); |
1279 NewEventListenerHookup(syncer_thread()->relay_channel(), this, | |
1280 &SyncInternal::HandleSyncerEvent)); | |
1281 | 1282 |
1282 bool attempting_auth = false; | 1283 bool attempting_auth = false; |
1283 std::string username, auth_token; | 1284 std::string username, auth_token; |
1284 if (attempt_last_user_authentication && | 1285 if (attempt_last_user_authentication && |
1285 auth_watcher()->settings()->GetLastUserAndServiceToken( | 1286 auth_watcher()->settings()->GetLastUserAndServiceToken( |
1286 SYNC_SERVICE_NAME, &username, &auth_token)) { | 1287 SYNC_SERVICE_NAME, &username, &auth_token)) { |
1287 if (invalidate_last_user_auth_token) { | 1288 if (invalidate_last_user_auth_token) { |
1288 auth_token += "bogus"; | 1289 auth_token += "bogus"; |
1289 } | 1290 } |
1290 attempting_auth = AuthenticateForUser(username, auth_token); | 1291 attempting_auth = AuthenticateForUser(username, auth_token); |
(...skipping 204 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1495 void SyncManager::SyncInternal::OnIPAddressChanged() { | 1496 void SyncManager::SyncInternal::OnIPAddressChanged() { |
1496 LOG(INFO) << "IP address change detected"; | 1497 LOG(INFO) << "IP address change detected"; |
1497 // TODO(akalin): CheckServerReachable() can block, which may cause | 1498 // TODO(akalin): CheckServerReachable() can block, which may cause |
1498 // jank if we try to shut down sync. Fix this. | 1499 // jank if we try to shut down sync. Fix this. |
1499 connection_manager()->CheckServerReachable(); | 1500 connection_manager()->CheckServerReachable(); |
1500 } | 1501 } |
1501 | 1502 |
1502 // Listen to model changes, filter out ones initiated by the sync API, and | 1503 // Listen to model changes, filter out ones initiated by the sync API, and |
1503 // saves the rest (hopefully just backend Syncer changes resulting from | 1504 // saves the rest (hopefully just backend Syncer changes resulting from |
1504 // ApplyUpdates) to data_->changelist. | 1505 // ApplyUpdates) to data_->changelist. |
1505 void SyncManager::SyncInternal::HandleChangeEvent( | 1506 void SyncManager::SyncInternal::HandleChannelEvent( |
1506 const syncable::DirectoryChangeEvent& event) { | 1507 const syncable::DirectoryChangeEvent& event) { |
1507 if (event.todo == syncable::DirectoryChangeEvent::TRANSACTION_COMPLETE) { | 1508 if (event.todo == syncable::DirectoryChangeEvent::TRANSACTION_COMPLETE) { |
1508 HandleTransactionCompleteChangeEvent(event); | 1509 HandleTransactionCompleteChangeEvent(event); |
1509 return; | 1510 return; |
1510 } else if (event.todo == syncable::DirectoryChangeEvent::CALCULATE_CHANGES) { | 1511 } else if (event.todo == syncable::DirectoryChangeEvent::CALCULATE_CHANGES) { |
1511 if (event.writer == syncable::SYNCAPI) { | 1512 if (event.writer == syncable::SYNCAPI) { |
1512 HandleCalculateChangesChangeEventFromSyncApi(event); | 1513 HandleCalculateChangesChangeEventFromSyncApi(event); |
1513 return; | 1514 return; |
1514 } | 1515 } |
1515 HandleCalculateChangesChangeEventFromSyncer(event); | 1516 HandleCalculateChangesChangeEventFromSyncer(event); |
(...skipping 152 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1668 allstatus_.status().initial_sync_ended, | 1669 allstatus_.status().initial_sync_ended, |
1669 allstatus_.status().syncer_stuck, | 1670 allstatus_.status().syncer_stuck, |
1670 allstatus_.status().updates_available, | 1671 allstatus_.status().updates_available, |
1671 allstatus_.status().updates_received, | 1672 allstatus_.status().updates_received, |
1672 allstatus_.status().disk_full, | 1673 allstatus_.status().disk_full, |
1673 false, // TODO(ncarter): invalid store? | 1674 false, // TODO(ncarter): invalid store? |
1674 allstatus_.status().max_consecutive_errors}; | 1675 allstatus_.status().max_consecutive_errors}; |
1675 return return_status; | 1676 return return_status; |
1676 } | 1677 } |
1677 | 1678 |
1678 void SyncManager::SyncInternal::HandleSyncerEvent(const SyncerEvent& event) { | 1679 void SyncManager::SyncInternal::HandleChannelEvent(const SyncerEvent& event) { |
1679 if (!initialized()) { | 1680 if (!initialized()) { |
1680 // This could be the first time that the syncer has completed a full | 1681 // This could be the first time that the syncer has completed a full |
1681 // download; if so, we should signal that initialization is complete. | 1682 // download; if so, we should signal that initialization is complete. |
1682 if (event.snapshot->is_share_usable) | 1683 if (event.snapshot->is_share_usable) |
1683 MarkAndNotifyInitializationComplete(); | 1684 MarkAndNotifyInitializationComplete(); |
1684 return; | 1685 return; |
1685 } | 1686 } |
1686 | 1687 |
1687 if (!observer_) | 1688 if (!observer_) |
1688 return; | 1689 return; |
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1752 observer_->OnAuthError(AuthError::None()); | 1753 observer_->OnAuthError(AuthError::None()); |
1753 | 1754 |
1754 // Hook up the DirectoryChangeEvent listener, HandleChangeEvent. | 1755 // Hook up the DirectoryChangeEvent listener, HandleChangeEvent. |
1755 { | 1756 { |
1756 syncable::ScopedDirLookup lookup(dir_manager(), username_for_share()); | 1757 syncable::ScopedDirLookup lookup(dir_manager(), username_for_share()); |
1757 if (!lookup.good()) { | 1758 if (!lookup.good()) { |
1758 DCHECK(false) << "ScopedDirLookup creation failed; unable to hook " | 1759 DCHECK(false) << "ScopedDirLookup creation failed; unable to hook " |
1759 << "up directory change event listener!"; | 1760 << "up directory change event listener!"; |
1760 return; | 1761 return; |
1761 } | 1762 } |
1762 dir_change_hookup_.reset(NewEventListenerHookup( | 1763 dir_change_hookup_.reset(lookup->AddChangeObserver(this)); |
1763 lookup->changes_channel(), this, | |
1764 &SyncInternal::HandleChangeEvent)); | |
1765 } | 1764 } |
1766 if (InitialSyncEndedForAllEnabledTypes()) | 1765 if (InitialSyncEndedForAllEnabledTypes()) |
1767 MarkAndNotifyInitializationComplete(); | 1766 MarkAndNotifyInitializationComplete(); |
1768 | 1767 |
1769 if (!event.auth_token.empty()) { | 1768 if (!event.auth_token.empty()) { |
1770 core_message_loop_->PostTask( | 1769 core_message_loop_->PostTask( |
1771 FROM_HERE, | 1770 FROM_HERE, |
1772 NewRunnableMethod( | 1771 NewRunnableMethod( |
1773 this, &SyncManager::SyncInternal::TalkMediatorLogin, | 1772 this, &SyncManager::SyncInternal::TalkMediatorLogin, |
1774 event.user_email, event.auth_token)); | 1773 event.user_email, event.auth_token)); |
(...skipping 141 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1916 DCHECK(false) << "Could not open directory when running in test mode"; | 1915 DCHECK(false) << "Could not open directory when running in test mode"; |
1917 | 1916 |
1918 // Hook up the DirectoryChangeEvent listener, HandleChangeEvent. | 1917 // Hook up the DirectoryChangeEvent listener, HandleChangeEvent. |
1919 { | 1918 { |
1920 syncable::ScopedDirLookup lookup(dir_manager(), username_for_share()); | 1919 syncable::ScopedDirLookup lookup(dir_manager(), username_for_share()); |
1921 if (!lookup.good()) { | 1920 if (!lookup.good()) { |
1922 DCHECK(false) << "ScopedDirLookup creation failed; unable to hook " | 1921 DCHECK(false) << "ScopedDirLookup creation failed; unable to hook " |
1923 << "up directory change event listener!"; | 1922 << "up directory change event listener!"; |
1924 return; | 1923 return; |
1925 } | 1924 } |
1926 dir_change_hookup_.reset(NewEventListenerHookup( | 1925 dir_change_hookup_.reset(lookup->AddChangeObserver(this)); |
1927 lookup->changes_channel(), this, | |
1928 &SyncInternal::HandleChangeEvent)); | |
1929 } | 1926 } |
1930 MarkAndNotifyInitializationComplete(); | 1927 MarkAndNotifyInitializationComplete(); |
1931 } | 1928 } |
1932 | 1929 |
1933 ////////////////////////////////////////////////////////////////////////// | 1930 ////////////////////////////////////////////////////////////////////////// |
1934 // BaseTransaction member definitions | 1931 // BaseTransaction member definitions |
1935 BaseTransaction::BaseTransaction(UserShare* share) | 1932 BaseTransaction::BaseTransaction(UserShare* share) |
1936 : lookup_(NULL) { | 1933 : lookup_(NULL) { |
1937 DCHECK(share && share->dir_manager.get()); | 1934 DCHECK(share && share->dir_manager.get()); |
1938 lookup_ = new syncable::ScopedDirLookup(share->dir_manager.get(), | 1935 lookup_ = new syncable::ScopedDirLookup(share->dir_manager.get(), |
1939 share->authenticated_name); | 1936 share->authenticated_name); |
1940 if (!(lookup_->good())) | 1937 if (!(lookup_->good())) |
1941 DCHECK(false) << "ScopedDirLookup failed on valid DirManager."; | 1938 DCHECK(false) << "ScopedDirLookup failed on valid DirManager."; |
1942 } | 1939 } |
1943 BaseTransaction::~BaseTransaction() { | 1940 BaseTransaction::~BaseTransaction() { |
1944 delete lookup_; | 1941 delete lookup_; |
1945 } | 1942 } |
1946 | 1943 |
1947 UserShare* SyncManager::GetUserShare() const { | 1944 UserShare* SyncManager::GetUserShare() const { |
1948 DCHECK(data_->initialized()) << "GetUserShare requires initialization!"; | 1945 DCHECK(data_->initialized()) << "GetUserShare requires initialization!"; |
1949 return data_->GetUserShare(); | 1946 return data_->GetUserShare(); |
1950 } | 1947 } |
1951 | 1948 |
1952 SyncManager::ExtraAutofillChangeRecordData::~ExtraAutofillChangeRecordData() { | 1949 SyncManager::ExtraAutofillChangeRecordData::~ExtraAutofillChangeRecordData() { |
1953 delete pre_deletion_data; | 1950 delete pre_deletion_data; |
1954 } | 1951 } |
1955 | 1952 |
1956 } // namespace sync_api | 1953 } // namespace sync_api |
OLD | NEW |