diff options
author | akalin@chromium.org <akalin@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2011-06-28 22:34:27 +0000 |
---|---|---|
committer | akalin@chromium.org <akalin@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2011-06-28 22:34:27 +0000 |
commit | f9682409ab601aad49dc953ae7b41108af157175 (patch) | |
tree | c0404e5ee7a10c98df94ca6395860c34021b6c3e | |
parent | 5569c2d0bea4a8c73cf95ac2e44fab2ac91740de (diff) | |
download | chromium_src-f9682409ab601aad49dc953ae7b41108af157175.zip chromium_src-f9682409ab601aad49dc953ae7b41108af157175.tar.gz chromium_src-f9682409ab601aad49dc953ae7b41108af157175.tar.bz2 |
[Sync] Simplify notifier code now that we have a single sync thread
Notifiers used to be created on the UI thread but used on the sync
thread. This made the thread-verification code more complicated.
Make notifiers be created and used on the sync thread only.
Use const string refs instead of (dangerously) calling c_str().
BUG=79174
TEST=
Review URL: http://codereview.chromium.org/7134103
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@90855 0039d316-1c4b-4281-b951-d872f2087c98
17 files changed, 107 insertions, 167 deletions
diff --git a/chrome/browser/sync/engine/syncapi.cc b/chrome/browser/sync/engine/syncapi.cc index 22ddaea..9d85b51 100644 --- a/chrome/browser/sync/engine/syncapi.cc +++ b/chrome/browser/sync/engine/syncapi.cc @@ -1199,7 +1199,7 @@ class SyncManager::SyncInternal bool use_ssl, HttpPostProviderFactory* post_factory, ModelSafeWorkerRegistrar* model_safe_worker_registrar, - const char* user_agent, + const std::string& user_agent, const SyncCredentials& credentials, sync_notifier::SyncNotifier* sync_notifier, const std::string& restored_key_for_bootstrapping, @@ -1591,7 +1591,7 @@ class SyncManager::SyncInternal scoped_ptr<SyncScheduler> scheduler_; // The SyncNotifier which notifies us when updates need to be downloaded. - sync_notifier::SyncNotifier* sync_notifier_; + scoped_ptr<sync_notifier::SyncNotifier> sync_notifier_; // A multi-purpose status watch object that aggregates stats from various // sync components. @@ -1641,12 +1641,12 @@ SyncManager::SyncManager(const std::string& name) : data_(new SyncInternal(name, ALLOW_THIS_IN_INITIALIZER_LIST(this))) {} bool SyncManager::Init(const FilePath& database_location, - const char* sync_server_and_path, + const std::string& sync_server_and_path, int sync_server_port, bool use_ssl, HttpPostProviderFactory* post_factory, ModelSafeWorkerRegistrar* registrar, - const char* user_agent, + const std::string& user_agent, const SyncCredentials& credentials, sync_notifier::SyncNotifier* sync_notifier, const std::string& restored_key_for_bootstrapping, @@ -1763,7 +1763,7 @@ bool SyncManager::SyncInternal::Init( bool use_ssl, HttpPostProviderFactory* post_factory, ModelSafeWorkerRegistrar* model_safe_worker_registrar, - const char* user_agent, + const std::string& user_agent, const SyncCredentials& credentials, sync_notifier::SyncNotifier* sync_notifier, const std::string& restored_key_for_bootstrapping, @@ -1777,7 +1777,7 @@ bool SyncManager::SyncInternal::Init( registrar_ = model_safe_worker_registrar; setup_for_test_mode_ = setup_for_test_mode; - sync_notifier_ = sync_notifier; + sync_notifier_.reset(sync_notifier); share_.dir_manager.reset(new DirectoryManager(database_location)); @@ -2210,12 +2210,12 @@ void SyncManager::SyncInternal::Shutdown() { // Automatically stops the scheduler. scheduler_.reset(); - // We NULL out sync_notifer_ so that any pending tasks do not - // trigger further notifications. - // TODO(akalin): NULL the other member variables defensively, too. - if (sync_notifier_) { + if (sync_notifier_.get()) { sync_notifier_->RemoveObserver(this); } + sync_notifier_.reset(); + + // TODO(akalin): NULL other member variables defensively, too. // |this| is about to be destroyed, so we have to ensure any // messages that were posted to sync_loop_ are flushed out, else @@ -2565,7 +2565,7 @@ void SyncManager::SyncInternal::OnSyncEngineEvent( (event.snapshot->syncer_status.num_successful_commits > 0); if (is_notifiable_commit) { allstatus_.IncrementNotifiableCommits(); - if (sync_notifier_) { + if (sync_notifier_.get()) { sync_notifier_->SendNotification(); } else { VLOG(1) << "Not sending notification: sync_notifier_ is NULL"; diff --git a/chrome/browser/sync/engine/syncapi.h b/chrome/browser/sync/engine/syncapi.h index 8a80c74..38e9d70 100644 --- a/chrome/browser/sync/engine/syncapi.h +++ b/chrome/browser/sync/engine/syncapi.h @@ -860,14 +860,14 @@ class SyncManager { // |model_safe_worker| ownership is given to the SyncManager. // |user_agent| is a 7-bit ASCII string suitable for use as the User-Agent // HTTP header. Used internally when collecting stats to classify clients. - // |sync_notifier| used to listen for notifications, not owned. + // |sync_notifier| is owned and used to listen for notifications. bool Init(const FilePath& database_location, - const char* sync_server_and_path, + const std::string& sync_server_and_path, int sync_server_port, bool use_ssl, HttpPostProviderFactory* post_factory, browser_sync::ModelSafeWorkerRegistrar* registrar, - const char* user_agent, + const std::string& user_agent, const SyncCredentials& credentials, sync_notifier::SyncNotifier* sync_notifier, const std::string& restored_key_for_bootstrapping, diff --git a/chrome/browser/sync/engine/syncapi_unittest.cc b/chrome/browser/sync/engine/syncapi_unittest.cc index 90ffe3f..2ff2f53 100644 --- a/chrome/browser/sync/engine/syncapi_unittest.cc +++ b/chrome/browser/sync/engine/syncapi_unittest.cc @@ -690,10 +690,15 @@ class SyncManagerTest : public testing::Test, protected: SyncManagerTest() : ui_thread_(BrowserThread::UI, &ui_loop_), + sync_notifier_mock_(NULL), sync_manager_("Test sync manager"), sync_notifier_observer_(NULL), update_enabled_types_call_count_(0) {} + virtual ~SyncManagerTest() { + EXPECT_FALSE(sync_notifier_mock_); + } + // Test implementation. void SetUp() { ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); @@ -702,7 +707,7 @@ class SyncManagerTest : public testing::Test, credentials.email = "foo@bar.com"; credentials.sync_token = "sometoken"; - sync_notifier_mock_.reset(new StrictMock<SyncNotifierMock>()); + sync_notifier_mock_ = new StrictMock<SyncNotifierMock>(); EXPECT_CALL(*sync_notifier_mock_, AddObserver(_)). WillOnce(Invoke(this, &SyncManagerTest::SyncNotifierAddObserver)); EXPECT_CALL(*sync_notifier_mock_, SetState("")); @@ -717,9 +722,10 @@ class SyncManagerTest : public testing::Test, EXPECT_FALSE(sync_notifier_observer_); + // Takes ownership of |sync_notifier_mock_|. sync_manager_.Init(temp_dir_.path(), "bogus", 0, false, new TestHttpPostProviderFactory(), this, "bogus", - credentials, sync_notifier_mock_.get(), "", + credentials, sync_notifier_mock_, "", true /* setup_for_test_mode */); EXPECT_TRUE(sync_notifier_observer_); @@ -743,6 +749,7 @@ class SyncManagerTest : public testing::Test, void TearDown() { sync_manager_.RemoveObserver(&observer_); sync_manager_.Shutdown(); + sync_notifier_mock_ = NULL; EXPECT_FALSE(sync_notifier_observer_); } @@ -827,7 +834,7 @@ class SyncManagerTest : public testing::Test, ScopedTempDir temp_dir_; // Sync Id's for the roots of the enabled datatypes. std::map<ModelType, int64> type_roots_; - scoped_ptr<StrictMock<SyncNotifierMock> > sync_notifier_mock_; + StrictMock<SyncNotifierMock>* sync_notifier_mock_; protected: SyncManager sync_manager_; diff --git a/chrome/browser/sync/glue/sync_backend_host.cc b/chrome/browser/sync/glue/sync_backend_host.cc index e8bd581..5e8bb8d 100644 --- a/chrome/browser/sync/glue/sync_backend_host.cc +++ b/chrome/browser/sync/glue/sync_backend_host.cc @@ -90,7 +90,8 @@ void SyncBackendHost::Initialize( SyncFrontend* frontend, const GURL& sync_service_url, const syncable::ModelTypeSet& types, - net::URLRequestContextGetter* baseline_context_getter, + const scoped_refptr<net::URLRequestContextGetter>& + baseline_context_getter, const SyncCredentials& credentials, bool delete_sync_data_folder) { if (!sync_thread_.Start()) @@ -127,13 +128,9 @@ void SyncBackendHost::Initialize( registrar_.routing_info.erase(syncable::PASSWORDS); } - // TODO(akalin): Create SyncNotifier here and pass it in as part of - // DoInitializeOptions. - core_->CreateSyncNotifier(baseline_context_getter); - InitCore(Core::DoInitializeOptions( sync_service_url, - MakeHttpBridgeFactory(baseline_context_getter), + baseline_context_getter, credentials, delete_sync_data_folder, RestoreEncryptionBootstrapToken(), @@ -181,7 +178,7 @@ JsBackend* SyncBackendHost::GetJsBackend() { } sync_api::HttpPostProviderFactory* SyncBackendHost::MakeHttpBridgeFactory( - net::URLRequestContextGetter* getter) { + const scoped_refptr<net::URLRequestContextGetter>& getter) { return new HttpBridgeFactory(getter); } @@ -627,24 +624,16 @@ void SyncBackendHost::Core::FinishConfigureDataTypesOnFrontendLoop() { } -void SyncBackendHost::Core::CreateSyncNotifier( - const scoped_refptr<net::URLRequestContextGetter>& request_context_getter) { - const std::string& client_info = webkit_glue::GetUserAgent(GURL()); - SyncNotifierFactory sync_notifier_factory(client_info); - sync_notifier_.reset(sync_notifier_factory.CreateSyncNotifier( - *CommandLine::ForCurrentProcess(), - request_context_getter)); -} - SyncBackendHost::Core::DoInitializeOptions::DoInitializeOptions( const GURL& service_url, - sync_api::HttpPostProviderFactory* http_bridge_factory, + const scoped_refptr<net::URLRequestContextGetter>& + request_context_getter, const sync_api::SyncCredentials& credentials, bool delete_sync_data_folder, const std::string& restored_key_for_bootstrapping, bool setup_for_test_mode) : service_url(service_url), - http_bridge_factory(http_bridge_factory), + request_context_getter(request_context_getter), credentials(credentials), delete_sync_data_folder(delete_sync_data_folder), restored_key_for_bootstrapping(restored_key_for_bootstrapping), @@ -763,16 +752,22 @@ void SyncBackendHost::Core::DoInitialize(const DoInitializeOptions& options) { sync_manager_.reset(new sync_api::SyncManager(name_)), sync_manager_->AddObserver(this); const FilePath& path_str = host_->sync_data_folder_path(); + const std::string& client_info = webkit_glue::GetUserAgent(GURL()); + SyncNotifierFactory sync_notifier_factory(client_info); + scoped_ptr<sync_notifier::SyncNotifier> sync_notifier( + sync_notifier_factory.CreateSyncNotifier( + *CommandLine::ForCurrentProcess(), + options.request_context_getter)); success = sync_manager_->Init( path_str, - (options.service_url.host() + options.service_url.path()).c_str(), + options.service_url.host() + options.service_url.path(), options.service_url.EffectiveIntPort(), options.service_url.SchemeIsSecure(), - options.http_bridge_factory, + host_->MakeHttpBridgeFactory(options.request_context_getter), host_, // ModelSafeWorkerRegistrar. - MakeUserAgentForSyncApi().c_str(), + MakeUserAgentForSyncApi(), options.credentials, - sync_notifier_.get(), + sync_notifier.release(), options.restored_key_for_bootstrapping, options.setup_for_test_mode); DCHECK(success) << "Syncapi initialization failed!"; diff --git a/chrome/browser/sync/glue/sync_backend_host.h b/chrome/browser/sync/glue/sync_backend_host.h index f3468f6..5555c6a 100644 --- a/chrome/browser/sync/glue/sync_backend_host.h +++ b/chrome/browser/sync/glue/sync_backend_host.h @@ -39,10 +39,6 @@ namespace net { class URLRequestContextGetter; } -namespace sync_notifier { -class SyncNotifier; -} // namespace sync_notifier - namespace browser_sync { namespace sessions { @@ -135,7 +131,8 @@ class SyncBackendHost : public browser_sync::ModelSafeWorkerRegistrar { void Initialize(SyncFrontend* frontend, const GURL& service_url, const syncable::ModelTypeSet& types, - net::URLRequestContextGetter* baseline_context_getter, + const scoped_refptr<net::URLRequestContextGetter>& + baseline_context_getter, const sync_api::SyncCredentials& credentials, bool delete_sync_data_folder); @@ -321,7 +318,8 @@ class SyncBackendHost : public browser_sync::ModelSafeWorkerRegistrar { struct DoInitializeOptions { DoInitializeOptions( const GURL& service_url, - sync_api::HttpPostProviderFactory* http_bridge_factory, + const scoped_refptr<net::URLRequestContextGetter>& + request_context_getter, const sync_api::SyncCredentials& credentials, bool delete_sync_data_folder, const std::string& restored_key_for_bootstrapping, @@ -329,7 +327,7 @@ class SyncBackendHost : public browser_sync::ModelSafeWorkerRegistrar { ~DoInitializeOptions(); GURL service_url; - sync_api::HttpPostProviderFactory* http_bridge_factory; + scoped_refptr<net::URLRequestContextGetter> request_context_getter; sync_api::SyncCredentials credentials; std::string lsid; bool delete_sync_data_folder; @@ -337,10 +335,6 @@ class SyncBackendHost : public browser_sync::ModelSafeWorkerRegistrar { bool setup_for_test_mode; }; - // Called on |frontend_loop_|. - void CreateSyncNotifier(const scoped_refptr<net::URLRequestContextGetter>& - request_context_getter); - // Note: // // The Do* methods are the various entry points from our SyncBackendHost. @@ -433,14 +427,15 @@ class SyncBackendHost : public browser_sync::ModelSafeWorkerRegistrar { // Special form of initialization that does not try and authenticate the // last known user (since it will fail in test mode) and does some extra // setup to nudge the syncapi into a usable state. - void DoInitializeForTest(const std::wstring& test_user, - sync_api::HttpPostProviderFactory* factory, - bool delete_sync_data_folder) { + void DoInitializeForTest( + const std::wstring& test_user, + const scoped_refptr<net::URLRequestContextGetter>& getter, + bool delete_sync_data_folder) { // Construct dummy credentials for test. sync_api::SyncCredentials credentials; credentials.email = WideToUTF8(test_user); credentials.sync_token = "token"; - DoInitialize(DoInitializeOptions(GURL(), factory, credentials, + DoInitialize(DoInitializeOptions(GURL(), getter, credentials, delete_sync_data_folder, "", true)); } @@ -524,8 +519,6 @@ class SyncBackendHost : public browser_sync::ModelSafeWorkerRegistrar { // The top-level syncapi entry point. Lives on the sync thread. scoped_ptr<sync_api::SyncManager> sync_manager_; - scoped_ptr<sync_notifier::SyncNotifier> sync_notifier_; - JsSyncManagerObserver sync_manager_observer_; JsEventRouter* parent_router_; @@ -559,9 +552,10 @@ class SyncBackendHost : public browser_sync::ModelSafeWorkerRegistrar { // Allows tests to perform alternate core initialization work. virtual void InitCore(const Core::DoInitializeOptions& options); - // Factory method for HttpPostProviderFactories. + // Factory method for HttpPostProviderFactories. Should be + // thread-safe. virtual sync_api::HttpPostProviderFactory* MakeHttpBridgeFactory( - net::URLRequestContextGetter* getter); + const scoped_refptr<net::URLRequestContextGetter>& getter); MessageLoop* sync_loop() { return sync_thread_.message_loop(); } diff --git a/chrome/browser/sync/notifier/non_blocking_invalidation_notifier.cc b/chrome/browser/sync/notifier/non_blocking_invalidation_notifier.cc index 203b832..0164efe 100644 --- a/chrome/browser/sync/notifier/non_blocking_invalidation_notifier.cc +++ b/chrome/browser/sync/notifier/non_blocking_invalidation_notifier.cc @@ -133,7 +133,7 @@ NonBlockingInvalidationNotifier::NonBlockingInvalidationNotifier( const notifier::NotifierOptions& notifier_options, const std::string& client_info) : core_(new Core), - construction_message_loop_proxy_( + parent_message_loop_proxy_( base::MessageLoopProxy::CreateForCurrentThread()), io_message_loop_proxy_(notifier_options.request_context_getter-> GetIOMessageLoopProxy()) { @@ -147,7 +147,7 @@ NonBlockingInvalidationNotifier::NonBlockingInvalidationNotifier( } NonBlockingInvalidationNotifier::~NonBlockingInvalidationNotifier() { - DCHECK(construction_message_loop_proxy_->BelongsToCurrentThread()); + DCHECK(parent_message_loop_proxy_->BelongsToCurrentThread()); if (!io_message_loop_proxy_->PostTask( FROM_HERE, NewRunnableMethod( @@ -158,18 +158,18 @@ NonBlockingInvalidationNotifier::~NonBlockingInvalidationNotifier() { void NonBlockingInvalidationNotifier::AddObserver( SyncNotifierObserver* observer) { - CheckOrSetValidThread(); + DCHECK(parent_message_loop_proxy_->BelongsToCurrentThread()); core_->AddObserver(observer); } void NonBlockingInvalidationNotifier::RemoveObserver( SyncNotifierObserver* observer) { - CheckOrSetValidThread(); + DCHECK(parent_message_loop_proxy_->BelongsToCurrentThread()); core_->RemoveObserver(observer); } void NonBlockingInvalidationNotifier::SetState(const std::string& state) { - CheckOrSetValidThread(); + DCHECK(parent_message_loop_proxy_->BelongsToCurrentThread()); if (!io_message_loop_proxy_->PostTask( FROM_HERE, NewRunnableMethod( @@ -181,7 +181,7 @@ void NonBlockingInvalidationNotifier::SetState(const std::string& state) { void NonBlockingInvalidationNotifier::UpdateCredentials( const std::string& email, const std::string& token) { - CheckOrSetValidThread(); + DCHECK(parent_message_loop_proxy_->BelongsToCurrentThread()); if (!io_message_loop_proxy_->PostTask( FROM_HERE, NewRunnableMethod( @@ -193,7 +193,7 @@ void NonBlockingInvalidationNotifier::UpdateCredentials( void NonBlockingInvalidationNotifier::UpdateEnabledTypes( const syncable::ModelTypeSet& types) { - CheckOrSetValidThread(); + DCHECK(parent_message_loop_proxy_->BelongsToCurrentThread()); if (!io_message_loop_proxy_->PostTask( FROM_HERE, NewRunnableMethod( @@ -204,18 +204,9 @@ void NonBlockingInvalidationNotifier::UpdateEnabledTypes( } void NonBlockingInvalidationNotifier::SendNotification() { - CheckOrSetValidThread(); + DCHECK(parent_message_loop_proxy_->BelongsToCurrentThread()); // InvalidationClient doesn't implement SendNotification(), so no // need to forward on the call. } -void NonBlockingInvalidationNotifier::CheckOrSetValidThread() { - if (method_message_loop_proxy_) { - DCHECK(method_message_loop_proxy_->BelongsToCurrentThread()); - } else { - method_message_loop_proxy_ = - base::MessageLoopProxy::CreateForCurrentThread(); - } -} - } // namespace sync_notifier diff --git a/chrome/browser/sync/notifier/non_blocking_invalidation_notifier.h b/chrome/browser/sync/notifier/non_blocking_invalidation_notifier.h index 362e4fb..e71a146 100644 --- a/chrome/browser/sync/notifier/non_blocking_invalidation_notifier.h +++ b/chrome/browser/sync/notifier/non_blocking_invalidation_notifier.h @@ -40,13 +40,11 @@ class NonBlockingInvalidationNotifier : public SyncNotifier { virtual void SendNotification(); private: - void CheckOrSetValidThread(); // The real guts of NonBlockingInvalidationNotifier, which allows this class // to not be refcounted. class Core; scoped_refptr<Core> core_; - scoped_refptr<base::MessageLoopProxy> construction_message_loop_proxy_; - scoped_refptr<base::MessageLoopProxy> method_message_loop_proxy_; + scoped_refptr<base::MessageLoopProxy> parent_message_loop_proxy_; scoped_refptr<base::MessageLoopProxy> io_message_loop_proxy_; DISALLOW_COPY_AND_ASSIGN(NonBlockingInvalidationNotifier); }; diff --git a/chrome/browser/sync/notifier/p2p_notifier.cc b/chrome/browser/sync/notifier/p2p_notifier.cc index 46d9a9e..8b57512 100644 --- a/chrome/browser/sync/notifier/p2p_notifier.cc +++ b/chrome/browser/sync/notifier/p2p_notifier.cc @@ -26,17 +26,17 @@ P2PNotifier::P2PNotifier( notifier_options)), logged_in_(false), notifications_enabled_(false), - construction_message_loop_proxy_( + parent_message_loop_proxy_( base::MessageLoopProxy::CreateForCurrentThread()) { talk_mediator_->SetDelegate(this); } P2PNotifier::~P2PNotifier() { - DCHECK(construction_message_loop_proxy_->BelongsToCurrentThread()); + DCHECK(parent_message_loop_proxy_->BelongsToCurrentThread()); } void P2PNotifier::AddObserver(SyncNotifierObserver* observer) { - CheckOrSetValidThread(); + DCHECK(parent_message_loop_proxy_->BelongsToCurrentThread()); observer_list_.AddObserver(observer); } @@ -45,7 +45,7 @@ void P2PNotifier::AddObserver(SyncNotifierObserver* observer) { // Users will need to call UpdateCredentials again to use the same object. // TODO(akalin): Think of a better solution to fix this. void P2PNotifier::RemoveObserver(SyncNotifierObserver* observer) { - CheckOrSetValidThread(); + DCHECK(parent_message_loop_proxy_->BelongsToCurrentThread()); observer_list_.RemoveObserver(observer); // Logout after the last observer is removed. @@ -55,12 +55,12 @@ void P2PNotifier::RemoveObserver(SyncNotifierObserver* observer) { } void P2PNotifier::SetState(const std::string& state) { - CheckOrSetValidThread(); + DCHECK(parent_message_loop_proxy_->BelongsToCurrentThread()); } void P2PNotifier::UpdateCredentials( const std::string& email, const std::string& token) { - CheckOrSetValidThread(); + DCHECK(parent_message_loop_proxy_->BelongsToCurrentThread()); // If already logged in, the new credentials will take effect on the // next reconnection. talk_mediator_->SetAuthToken(email, token, SYNC_SERVICE_NAME); @@ -83,13 +83,13 @@ void P2PNotifier::UpdateCredentials( } void P2PNotifier::UpdateEnabledTypes(const syncable::ModelTypeSet& types) { - CheckOrSetValidThread(); + DCHECK(parent_message_loop_proxy_->BelongsToCurrentThread()); enabled_types_ = types; MaybeEmitNotification(); } void P2PNotifier::SendNotification() { - CheckOrSetValidThread(); + DCHECK(parent_message_loop_proxy_->BelongsToCurrentThread()); VLOG(1) << "Sending XMPP notification..."; notifier::Notification notification; notification.channel = kSyncNotificationChannel; @@ -98,7 +98,7 @@ void P2PNotifier::SendNotification() { } void P2PNotifier::OnNotificationStateChange(bool notifications_enabled) { - CheckOrSetValidThread(); + DCHECK(parent_message_loop_proxy_->BelongsToCurrentThread()); notifications_enabled_ = notifications_enabled; FOR_EACH_OBSERVER(SyncNotifierObserver, observer_list_, OnNotificationStateChange(notifications_enabled_)); @@ -107,7 +107,7 @@ void P2PNotifier::OnNotificationStateChange(bool notifications_enabled) { void P2PNotifier::OnIncomingNotification( const notifier::Notification& notification) { - CheckOrSetValidThread(); + DCHECK(parent_message_loop_proxy_->BelongsToCurrentThread()); VLOG(1) << "Sync received P2P notification."; if (notification.channel != kSyncNotificationChannel) { LOG(WARNING) << "Notification from unexpected source: " @@ -138,13 +138,4 @@ void P2PNotifier::MaybeEmitNotification() { OnIncomingNotification(type_payloads)); } -void P2PNotifier::CheckOrSetValidThread() { - if (method_message_loop_proxy_) { - DCHECK(method_message_loop_proxy_->BelongsToCurrentThread()); - } else { - method_message_loop_proxy_ = - base::MessageLoopProxy::CreateForCurrentThread(); - } -} - } // namespace sync_notifier diff --git a/chrome/browser/sync/notifier/p2p_notifier.h b/chrome/browser/sync/notifier/p2p_notifier.h index e5b2743..aed0db6 100644 --- a/chrome/browser/sync/notifier/p2p_notifier.h +++ b/chrome/browser/sync/notifier/p2p_notifier.h @@ -55,7 +55,6 @@ class P2PNotifier // Call OnIncomingNotification() on observers if we have a non-empty // set of enabled types. void MaybeEmitNotification(); - void CheckOrSetValidThread(); ObserverList<SyncNotifierObserver> observer_list_; @@ -68,8 +67,7 @@ class P2PNotifier bool notifications_enabled_; syncable::ModelTypeSet enabled_types_; - scoped_refptr<base::MessageLoopProxy> construction_message_loop_proxy_; - scoped_refptr<base::MessageLoopProxy> method_message_loop_proxy_; + scoped_refptr<base::MessageLoopProxy> parent_message_loop_proxy_; }; } // namespace sync_notifier diff --git a/chrome/browser/sync/notifier/sync_notifier.h b/chrome/browser/sync/notifier/sync_notifier.h index 5bf6c8c..da15259 100644 --- a/chrome/browser/sync/notifier/sync_notifier.h +++ b/chrome/browser/sync/notifier/sync_notifier.h @@ -5,20 +5,6 @@ // Interface to the sync notifier, which is an object that receives // notifications when updates are available for a set of sync types. // All the observers are notified when such an event happens. -// -// A SyncNotifier must be destroyed on the same thread it was created on, -// and all its methods must be called on the same thread (not necessarily -// the one it was created on). If the methods thread is different from the -// creation thread, then the methods thread must not exist when the SyncNotifier -// is created and destroyed. -// -// In particular, the SyncNotifier will be created on the UI thread, the syncer -// core thread will be created, the SyncNotifier will be used on that core -// thread, the syncer core thread will be destroyed, and then the SyncNotifier -// will be destroyed. -// -// TODO(akalin): Remove the code to deal with this situation once the syncer -// core thread goes away. #ifndef CHROME_BROWSER_SYNC_NOTIFIER_SYNC_NOTIFIER_H_ #define CHROME_BROWSER_SYNC_NOTIFIER_SYNC_NOTIFIER_H_ diff --git a/chrome/browser/sync/profile_sync_service.cc b/chrome/browser/sync/profile_sync_service.cc index 2bcdc98..121679d 100644 --- a/chrome/browser/sync/profile_sync_service.cc +++ b/chrome/browser/sync/profile_sync_service.cc @@ -343,10 +343,13 @@ void ProfileSyncService::InitializeBackend(bool delete_sync_data_folder) { SyncCredentials credentials = GetCredentials(); + scoped_refptr<net::URLRequestContextGetter> request_context_getter( + profile_->GetRequestContext()); + backend_->Initialize(this, sync_service_url_, types, - profile_->GetRequestContext(), + request_context_getter, credentials, delete_sync_data_folder); } diff --git a/chrome/browser/sync/test_profile_sync_service.cc b/chrome/browser/sync/test_profile_sync_service.cc index 4f4ad07..7c1168a 100644 --- a/chrome/browser/sync/test_profile_sync_service.cc +++ b/chrome/browser/sync/test_profile_sync_service.cc @@ -72,7 +72,7 @@ void SyncBackendHostForProfileSyncTest:: sync_api::HttpPostProviderFactory* SyncBackendHostForProfileSyncTest::MakeHttpBridgeFactory( - net::URLRequestContextGetter* getter) { + const scoped_refptr<net::URLRequestContextGetter>& getter) { return new browser_sync::TestHttpBridgeFactory; } @@ -84,7 +84,7 @@ void SyncBackendHostForProfileSyncTest::InitCore( NewRunnableMethod(core_.get(), &SyncBackendHost::Core::DoInitializeForTest, user, - options.http_bridge_factory, + options.request_context_getter, options.delete_sync_data_folder)); // TODO(akalin): Figure out a better way to do this. diff --git a/chrome/browser/sync/test_profile_sync_service.h b/chrome/browser/sync/test_profile_sync_service.h index 780560c..d9bba61 100644 --- a/chrome/browser/sync/test_profile_sync_service.h +++ b/chrome/browser/sync/test_profile_sync_service.h @@ -55,7 +55,7 @@ class SyncBackendHostForProfileSyncTest const tracked_objects::Location&); virtual sync_api::HttpPostProviderFactory* MakeHttpBridgeFactory( - net::URLRequestContextGetter* getter); + const scoped_refptr<net::URLRequestContextGetter>& getter); virtual void InitCore(const Core::DoInitializeOptions& options); diff --git a/jingle/notifier/listener/mediator_thread_impl.cc b/jingle/notifier/listener/mediator_thread_impl.cc index 6826e78..a9d7052 100644 --- a/jingle/notifier/listener/mediator_thread_impl.cc +++ b/jingle/notifier/listener/mediator_thread_impl.cc @@ -212,33 +212,33 @@ void MediatorThreadImpl::Core::OnDisconnect() { MediatorThreadImpl::MediatorThreadImpl(const NotifierOptions& notifier_options) : core_(new Core(notifier_options)), - construction_message_loop_proxy_( + parent_message_loop_proxy_( base::MessageLoopProxy::CreateForCurrentThread()), io_message_loop_proxy_( notifier_options.request_context_getter->GetIOMessageLoopProxy()) { } MediatorThreadImpl::~MediatorThreadImpl() { - DCHECK(construction_message_loop_proxy_->BelongsToCurrentThread()); + DCHECK(parent_message_loop_proxy_->BelongsToCurrentThread()); LogoutImpl(); } void MediatorThreadImpl::AddObserver(Observer* observer) { - CheckOrSetValidThread(); + DCHECK(parent_message_loop_proxy_->BelongsToCurrentThread()); core_->AddObserver(observer); } void MediatorThreadImpl::RemoveObserver(Observer* observer) { - CheckOrSetValidThread(); + DCHECK(parent_message_loop_proxy_->BelongsToCurrentThread()); core_->RemoveObserver(observer); } void MediatorThreadImpl::Start() { - DCHECK(construction_message_loop_proxy_->BelongsToCurrentThread()); + DCHECK(parent_message_loop_proxy_->BelongsToCurrentThread()); } void MediatorThreadImpl::Login(const buzz::XmppClientSettings& settings) { - CheckOrSetValidThread(); + DCHECK(parent_message_loop_proxy_->BelongsToCurrentThread()); io_message_loop_proxy_->PostTask( FROM_HERE, NewRunnableMethod(core_.get(), @@ -247,12 +247,12 @@ void MediatorThreadImpl::Login(const buzz::XmppClientSettings& settings) { } void MediatorThreadImpl::Logout() { - CheckOrSetValidThread(); + DCHECK(parent_message_loop_proxy_->BelongsToCurrentThread()); LogoutImpl(); } void MediatorThreadImpl::ListenForUpdates() { - CheckOrSetValidThread(); + DCHECK(parent_message_loop_proxy_->BelongsToCurrentThread()); io_message_loop_proxy_->PostTask( FROM_HERE, NewRunnableMethod(core_.get(), @@ -261,7 +261,7 @@ void MediatorThreadImpl::ListenForUpdates() { void MediatorThreadImpl::SubscribeForUpdates( const SubscriptionList& subscriptions) { - CheckOrSetValidThread(); + DCHECK(parent_message_loop_proxy_->BelongsToCurrentThread()); io_message_loop_proxy_->PostTask( FROM_HERE, NewRunnableMethod( @@ -272,7 +272,7 @@ void MediatorThreadImpl::SubscribeForUpdates( void MediatorThreadImpl::SendNotification( const Notification& data) { - CheckOrSetValidThread(); + DCHECK(parent_message_loop_proxy_->BelongsToCurrentThread()); io_message_loop_proxy_->PostTask( FROM_HERE, NewRunnableMethod(core_.get(), @@ -282,7 +282,7 @@ void MediatorThreadImpl::SendNotification( void MediatorThreadImpl::UpdateXmppSettings( const buzz::XmppClientSettings& settings) { - CheckOrSetValidThread(); + DCHECK(parent_message_loop_proxy_->BelongsToCurrentThread()); io_message_loop_proxy_->PostTask( FROM_HERE, NewRunnableMethod(core_.get(), @@ -292,7 +292,7 @@ void MediatorThreadImpl::UpdateXmppSettings( void MediatorThreadImpl::TriggerOnConnectForTest( base::WeakPtr<talk_base::Task> base_task) { - CheckOrSetValidThread(); + DCHECK(parent_message_loop_proxy_->BelongsToCurrentThread()); io_message_loop_proxy_->PostTask( FROM_HERE, NewRunnableMethod(core_.get(), @@ -307,13 +307,4 @@ void MediatorThreadImpl::LogoutImpl() { &MediatorThreadImpl::Core::Disconnect)); } -void MediatorThreadImpl::CheckOrSetValidThread() { - if (method_message_loop_proxy_) { - DCHECK(method_message_loop_proxy_->BelongsToCurrentThread()); - } else { - method_message_loop_proxy_ = - base::MessageLoopProxy::CreateForCurrentThread(); - } -} - } // namespace notifier diff --git a/jingle/notifier/listener/mediator_thread_impl.h b/jingle/notifier/listener/mediator_thread_impl.h index 0656757..ab85ccd 100644 --- a/jingle/notifier/listener/mediator_thread_impl.h +++ b/jingle/notifier/listener/mediator_thread_impl.h @@ -70,7 +70,6 @@ class MediatorThreadImpl : public MediatorThread { void TriggerOnConnectForTest(base::WeakPtr<talk_base::Task> base_task); private: - void CheckOrSetValidThread(); // The logic of Logout without the thread check so it can be called in the // d'tor. void LogoutImpl(); @@ -78,8 +77,7 @@ class MediatorThreadImpl : public MediatorThread { // refcounted. class Core; scoped_refptr<Core> core_; - scoped_refptr<base::MessageLoopProxy> construction_message_loop_proxy_; - scoped_refptr<base::MessageLoopProxy> method_message_loop_proxy_; + scoped_refptr<base::MessageLoopProxy> parent_message_loop_proxy_; scoped_refptr<base::MessageLoopProxy> io_message_loop_proxy_; DISALLOW_COPY_AND_ASSIGN(MediatorThreadImpl); }; diff --git a/jingle/notifier/listener/talk_mediator_impl.cc b/jingle/notifier/listener/talk_mediator_impl.cc index 5b90663..7760261 100644 --- a/jingle/notifier/listener/talk_mediator_impl.cc +++ b/jingle/notifier/listener/talk_mediator_impl.cc @@ -16,19 +16,18 @@ TalkMediatorImpl::TalkMediatorImpl( : delegate_(NULL), mediator_thread_(mediator_thread), notifier_options_(notifier_options), - construction_message_loop_(MessageLoop::current()), - method_message_loop_(NULL) { + parent_message_loop_(MessageLoop::current()) { mediator_thread_->Start(); state_.started = 1; } TalkMediatorImpl::~TalkMediatorImpl() { - DCHECK_EQ(MessageLoop::current(), construction_message_loop_); + DCHECK_EQ(MessageLoop::current(), parent_message_loop_); DCHECK(!state_.started); } bool TalkMediatorImpl::Login() { - CheckOrSetValidThread(); + DCHECK_EQ(MessageLoop::current(), parent_message_loop_); // Connect to the mediator thread and start processing messages. mediator_thread_->AddObserver(this); if (state_.initialized && !state_.logging_in && !state_.logged_in) { @@ -40,7 +39,7 @@ bool TalkMediatorImpl::Login() { } bool TalkMediatorImpl::Logout() { - CheckOrSetValidThread(); + DCHECK_EQ(MessageLoop::current(), parent_message_loop_); if (state_.started) { state_.started = 0; state_.logging_in = 0; @@ -55,19 +54,19 @@ bool TalkMediatorImpl::Logout() { } void TalkMediatorImpl::SendNotification(const Notification& data) { - CheckOrSetValidThread(); + DCHECK_EQ(MessageLoop::current(), parent_message_loop_); mediator_thread_->SendNotification(data); } void TalkMediatorImpl::SetDelegate(TalkMediator::Delegate* delegate) { - DCHECK_EQ(MessageLoop::current(), construction_message_loop_); + DCHECK_EQ(MessageLoop::current(), parent_message_loop_); delegate_ = delegate; } void TalkMediatorImpl::SetAuthToken(const std::string& email, const std::string& token, const std::string& token_service) { - CheckOrSetValidThread(); + DCHECK_EQ(MessageLoop::current(), parent_message_loop_); xmpp_settings_ = MakeXmppClientSettings(notifier_options_, email, token, token_service); @@ -81,7 +80,7 @@ void TalkMediatorImpl::SetAuthToken(const std::string& email, } void TalkMediatorImpl::AddSubscription(const Subscription& subscription) { - CheckOrSetValidThread(); + DCHECK_EQ(MessageLoop::current(), parent_message_loop_); subscriptions_.push_back(subscription); if (state_.logged_in) { VLOG(1) << "Resubscribing for updates, a new service got added"; @@ -91,7 +90,7 @@ void TalkMediatorImpl::AddSubscription(const Subscription& subscription) { void TalkMediatorImpl::OnConnectionStateChange(bool logged_in) { - CheckOrSetValidThread(); + DCHECK_EQ(MessageLoop::current(), parent_message_loop_); // If we just lost connection, then the MediatorThread implementation will // try to log in again. We need to set state_.logging_in to true in that case. state_.logging_in = !logged_in; @@ -110,7 +109,7 @@ void TalkMediatorImpl::OnConnectionStateChange(bool logged_in) { } void TalkMediatorImpl::OnSubscriptionStateChange(bool subscribed) { - CheckOrSetValidThread(); + DCHECK_EQ(MessageLoop::current(), parent_message_loop_); VLOG(1) << "P2P: " << (subscribed ? "subscribed" : "unsubscribed"); if (delegate_) delegate_->OnNotificationStateChange(subscribed); @@ -118,26 +117,18 @@ void TalkMediatorImpl::OnSubscriptionStateChange(bool subscribed) { void TalkMediatorImpl::OnIncomingNotification( const Notification& notification) { - CheckOrSetValidThread(); + DCHECK_EQ(MessageLoop::current(), parent_message_loop_); VLOG(1) << "P2P: Updates are available on the server."; if (delegate_) delegate_->OnIncomingNotification(notification); } void TalkMediatorImpl::OnOutgoingNotification() { - CheckOrSetValidThread(); + DCHECK_EQ(MessageLoop::current(), parent_message_loop_); VLOG(1) << "P2P: Peers were notified that updates are available on the " "server."; if (delegate_) delegate_->OnOutgoingNotification(); } -void TalkMediatorImpl::CheckOrSetValidThread() { - if (method_message_loop_) { - DCHECK_EQ(MessageLoop::current(), method_message_loop_); - } else { - method_message_loop_ = MessageLoop::current(); - } -} - } // namespace notifier diff --git a/jingle/notifier/listener/talk_mediator_impl.h b/jingle/notifier/listener/talk_mediator_impl.h index 718bb89..93f122a 100644 --- a/jingle/notifier/listener/talk_mediator_impl.h +++ b/jingle/notifier/listener/talk_mediator_impl.h @@ -79,8 +79,6 @@ class TalkMediatorImpl unsigned int logged_in : 1; // Logged in the mediator's authenticator. }; - void CheckOrSetValidThread(); - // Delegate, which we don't own. May be NULL. TalkMediator::Delegate* delegate_; @@ -97,8 +95,7 @@ class TalkMediatorImpl SubscriptionList subscriptions_; - MessageLoop* construction_message_loop_; - MessageLoop* method_message_loop_; + MessageLoop* parent_message_loop_; FRIEND_TEST_ALL_PREFIXES(TalkMediatorImplTest, SetAuthToken); FRIEND_TEST_ALL_PREFIXES(TalkMediatorImplTest, SendNotification); |