diff options
author | sanjeevr@chromium.org <sanjeevr@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2010-05-21 03:25:25 +0000 |
---|---|---|
committer | sanjeevr@chromium.org <sanjeevr@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2010-05-21 03:25:25 +0000 |
commit | b770105f7e97a6ccb3e012f2cc87bea9a5edff3b (patch) | |
tree | 5adb9d8c9514cf96852c9664470d114aff02c461 /chrome/service | |
parent | ba1161fab422b5a0d35c715c6082e1a4c412e44d (diff) | |
download | chromium_src-b770105f7e97a6ccb3e012f2cc87bea9a5edff3b.zip chromium_src-b770105f7e97a6ccb3e012f2cc87bea9a5edff3b.tar.gz chromium_src-b770105f7e97a6ccb3e012f2cc87bea9a5edff3b.tar.bz2 |
Added an implementation of the NetworkChangeNotifierThread interface for the service process and used it.
BUG=None.
TEST=Unit-test provided. Also test cloud print proxy.
Review URL: http://codereview.chromium.org/2086020
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@47881 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chrome/service')
6 files changed, 211 insertions, 3 deletions
diff --git a/chrome/service/cloud_print/cloud_print_proxy_backend.cc b/chrome/service/cloud_print/cloud_print_proxy_backend.cc index 4b0616c..eff9bcf 100644 --- a/chrome/service/cloud_print/cloud_print_proxy_backend.cc +++ b/chrome/service/cloud_print/cloud_print_proxy_backend.cc @@ -15,6 +15,7 @@ #include "chrome/common/deprecated/event_sys-inl.h" #include "chrome/common/net/notifier/listener/talk_mediator_impl.h" #include "chrome/service/gaia/service_gaia_authenticator.h" +#include "chrome/service/net/service_network_change_notifier_thread.h" #include "chrome/service/service_process.h" #include "googleurl/src/gurl.h" @@ -230,8 +231,8 @@ void CloudPrintProxyBackend::Core::DoInitialize(const std::string& lsid, gaia_auth_for_print->set_message_loop(MessageLoop::current()); if (gaia_auth_for_print->AuthenticateWithLsid(lsid, true)) { auth_token_ = gaia_auth_for_print->auth_token(); - talk_mediator_.reset( - new notifier::TalkMediatorImpl(false)); + talk_mediator_.reset(new notifier::TalkMediatorImpl( + g_service_process->network_change_notifier_thread(), false)); talk_mediator_->AddSubscribedServiceUrl(kCloudPrintTalkServiceUrl); talk_mediator_hookup_.reset( NewEventListenerHookup( diff --git a/chrome/service/net/service_network_change_notifier_thread.cc b/chrome/service/net/service_network_change_notifier_thread.cc new file mode 100644 index 0000000..3ccd146 --- /dev/null +++ b/chrome/service/net/service_network_change_notifier_thread.cc @@ -0,0 +1,46 @@ +// Copyright (c) 2010 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "chrome/service/net/service_network_change_notifier_thread.h" + +#include "base/logging.h" +#include "base/message_loop.h" +#include "net/base/network_change_notifier.h" + +ServiceNetworkChangeNotifierThread::ServiceNetworkChangeNotifierThread( + MessageLoop* io_thread_message_loop) + : io_thread_message_loop_(io_thread_message_loop) { + DCHECK(io_thread_message_loop_); +} + +ServiceNetworkChangeNotifierThread::~ServiceNetworkChangeNotifierThread() { + io_thread_message_loop_->DeleteSoon(FROM_HERE, + network_change_notifier_.release()); +} + +void ServiceNetworkChangeNotifierThread::Initialize() { + io_thread_message_loop_->PostTask( + FROM_HERE, + NewRunnableMethod( + this, + &ServiceNetworkChangeNotifierThread::CreateNetworkChangeNotifier)); +} + +MessageLoop* ServiceNetworkChangeNotifierThread::GetMessageLoop() const { + DCHECK(io_thread_message_loop_); + return io_thread_message_loop_; +} + +net::NetworkChangeNotifier* +ServiceNetworkChangeNotifierThread::GetNetworkChangeNotifier() const { + DCHECK(MessageLoop::current() == io_thread_message_loop_); + return network_change_notifier_.get(); +} + +void ServiceNetworkChangeNotifierThread::CreateNetworkChangeNotifier() { + DCHECK(MessageLoop::current() == io_thread_message_loop_); + network_change_notifier_.reset( + net::NetworkChangeNotifier::CreateDefaultNetworkChangeNotifier()); +} + diff --git a/chrome/service/net/service_network_change_notifier_thread.h b/chrome/service/net/service_network_change_notifier_thread.h new file mode 100644 index 0000000..14975a6 --- /dev/null +++ b/chrome/service/net/service_network_change_notifier_thread.h @@ -0,0 +1,50 @@ +// Copyright (c) 2010 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#ifndef CHROME_SERVICE_NET_SERVICE_NETWORK_CHANGE_NOTIFIER_THREAD_H_ +#define CHROME_SERVICE_NET_SERVICE_NETWORK_CHANGE_NOTIFIER_THREAD_H_ + +#include "base/basictypes.h" +#include "base/ref_counted.h" +#include "base/scoped_ptr.h" +#include "chrome/common/net/network_change_notifier_thread.h" + +class MessageLoop; + +namespace net { +class NetworkChangeNotifier; +} // namespace net + +// This is a simple implementation of NetworkChangeNotifierThread. +// Note that Initialize MUST be called before this class can be used. +class ServiceNetworkChangeNotifierThread + : public chrome_common_net::NetworkChangeNotifierThread, + public base::RefCountedThreadSafe<ServiceNetworkChangeNotifierThread> { + public: + // Does not take ownership of |io_thread_message_loop|. This instance must + // live no longer than |io_thread_message_loop|. + // TODO(sanjeevr): Change NetworkChangeNotifierThread to use MessageLoopProxy + explicit ServiceNetworkChangeNotifierThread( + MessageLoop* io_thread_message_loop); + virtual ~ServiceNetworkChangeNotifierThread(); + + // Initialize MUST be called before this class can be used. + void Initialize(); + + // chrome_common_net::NetworkChangeNotifierThread implementation. + + virtual MessageLoop* GetMessageLoop() const; + + virtual net::NetworkChangeNotifier* GetNetworkChangeNotifier() const; + + private: + MessageLoop* const io_thread_message_loop_; + scoped_ptr<net::NetworkChangeNotifier> network_change_notifier_; + + void CreateNetworkChangeNotifier(); + DISALLOW_COPY_AND_ASSIGN(ServiceNetworkChangeNotifierThread); +}; + +#endif // CHROME_SERVICE_NET_SERVICE_NETWORK_CHANGE_NOTIFIER_THREAD_H_ + diff --git a/chrome/service/net/service_network_change_notifier_thread_unittest.cc b/chrome/service/net/service_network_change_notifier_thread_unittest.cc new file mode 100644 index 0000000..917d51a --- /dev/null +++ b/chrome/service/net/service_network_change_notifier_thread_unittest.cc @@ -0,0 +1,100 @@ +// Copyright (c) 2010 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "chrome/service/net/service_network_change_notifier_thread.h" + +#include "base/basictypes.h" +#include "base/message_loop.h" +#include "base/scoped_ptr.h" +#include "base/task.h" +#include "base/thread.h" +#include "chrome/common/net/thread_blocker.h" +#include "testing/gtest/include/gtest/gtest.h" + +namespace { + +class ServiceNetworkChangeNotifierThreadTest : public testing::Test { + protected: + ServiceNetworkChangeNotifierThreadTest() + : io_thread_("ServiceNetworkChangeNotifierThreadTest_IO") { + } + + virtual ~ServiceNetworkChangeNotifierThreadTest() {} + + virtual void SetUp() { + // We need to set the message loop type explicitly because + // IOThread doesn't do it for us and the Linux + // NetworkChangeNotifier expects it. + base::Thread::Options options; + options.message_loop_type = MessageLoop::TYPE_IO; + EXPECT_TRUE(io_thread_.StartWithOptions(options)); + thread_blocker_.reset(new chrome_common_net::ThreadBlocker(&io_thread_)); + thread_blocker_->Block(); + } + + virtual void TearDown() { + // Nothing should be posted on |io_thread_| at this point. + thread_blocker_->Unblock(); + thread_blocker_.reset(); + io_thread_.Stop(); + } + + base::Thread io_thread_; + scoped_ptr<chrome_common_net::ThreadBlocker> thread_blocker_; + + private: + DISALLOW_COPY_AND_ASSIGN(ServiceNetworkChangeNotifierThreadTest); +}; + +void CheckNonNullNetworkChangeNotifier( + ServiceNetworkChangeNotifierThread* network_change_notifier_thread) { + EXPECT_EQ(network_change_notifier_thread->GetMessageLoop(), + MessageLoop::current()); + EXPECT_TRUE( + network_change_notifier_thread->GetNetworkChangeNotifier() != NULL); +} + +void CheckNullNetworkChangeNotifier( + ServiceNetworkChangeNotifierThread* network_change_notifier_thread) { + EXPECT_EQ(network_change_notifier_thread->GetMessageLoop(), + MessageLoop::current()); + EXPECT_TRUE( + network_change_notifier_thread->GetNetworkChangeNotifier() == NULL); +} + +TEST_F(ServiceNetworkChangeNotifierThreadTest, Basic) { + scoped_refptr<ServiceNetworkChangeNotifierThread> change_notifier_thread = + new ServiceNetworkChangeNotifierThread(io_thread_.message_loop()); + EXPECT_EQ(io_thread_.message_loop(), + change_notifier_thread->GetMessageLoop()); + change_notifier_thread->Initialize(); + io_thread_.message_loop()->PostTask( + FROM_HERE, + NewRunnableFunction(&CheckNonNullNetworkChangeNotifier, + change_notifier_thread.get())); + // Pump the thread to make sure the task we just posted is run + // before this test ends. + thread_blocker_->Unblock(); + thread_blocker_->Block(); +} + +TEST_F(ServiceNetworkChangeNotifierThreadTest, Uninitialized) { + scoped_refptr<ServiceNetworkChangeNotifierThread> change_notifier_thread = + new ServiceNetworkChangeNotifierThread(io_thread_.message_loop()); + EXPECT_EQ(io_thread_.message_loop(), + change_notifier_thread->GetMessageLoop()); + // We have not called Initialize. Expect the GetNetworkChangeNotifier() call + // to return NULL. + io_thread_.message_loop()->PostTask( + FROM_HERE, + NewRunnableFunction(&CheckNullNetworkChangeNotifier, + change_notifier_thread.get())); + // Pump the thread to make sure the task we just posted is run + // before this test ends. + thread_blocker_->Unblock(); + thread_blocker_->Block(); +} + +} // namespace + diff --git a/chrome/service/service_process.cc b/chrome/service/service_process.cc index 41bfe8d..9a39e5e 100644 --- a/chrome/service/service_process.cc +++ b/chrome/service/service_process.cc @@ -5,6 +5,7 @@ #include "chrome/service/service_process.h" #include "chrome/service/cloud_print/cloud_print_proxy.h" +#include "chrome/service/net/service_network_change_notifier_thread.h" ServiceProcess* g_service_process = NULL; @@ -24,10 +25,14 @@ bool ServiceProcess::Initialize() { Teardown(); return false; } + network_change_notifier_thread_ = + new ServiceNetworkChangeNotifierThread(io_thread_->message_loop()); + network_change_notifier_thread_->Initialize(); return true; } bool ServiceProcess::Teardown() { + network_change_notifier_thread_ = NULL; io_thread_.reset(); file_thread_.reset(); return true; diff --git a/chrome/service/service_process.h b/chrome/service/service_process.h index 9d980dd..e389deb5 100644 --- a/chrome/service/service_process.h +++ b/chrome/service/service_process.h @@ -5,10 +5,11 @@ #ifndef CHROME_SERVICE_SERVICE_PROCESS_H_ #define CHROME_SERVICE_SERVICE_PROCESS_H_ - +#include "base/ref_counted.h" #include "base/thread.h" class CloudPrintProxy; +class ServiceNetworkChangeNotifierThread; // The ServiceProcess does not inherit from ChildProcess because this // process can live independently of the browser process. @@ -42,11 +43,16 @@ class ServiceProcess { return file_thread_.get(); } CloudPrintProxy* cloud_print_proxy(); + ServiceNetworkChangeNotifierThread* network_change_notifier_thread() const { + return network_change_notifier_thread_.get(); + } private: scoped_ptr<base::Thread> io_thread_; scoped_ptr<base::Thread> file_thread_; scoped_ptr<CloudPrintProxy> cloud_print_proxy_; + scoped_refptr<ServiceNetworkChangeNotifierThread> + network_change_notifier_thread_; DISALLOW_COPY_AND_ASSIGN(ServiceProcess); }; |