diff options
author | akalin@chromium.org <akalin@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2010-05-06 21:59:55 +0000 |
---|---|---|
committer | akalin@chromium.org <akalin@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2010-05-06 21:59:55 +0000 |
commit | 3c3065d5e74b5487f94f2f8f17be9169d4f805fc (patch) | |
tree | 314c61f8c7d2a6b1464e38455c6b69d807e95ec4 /chrome/browser | |
parent | dde46b66c618a3ab242ddfbceb0290699a32012c (diff) | |
download | chromium_src-3c3065d5e74b5487f94f2f8f17be9169d4f805fc.zip chromium_src-3c3065d5e74b5487f94f2f8f17be9169d4f805fc.tar.gz chromium_src-3c3065d5e74b5487f94f2f8f17be9169d4f805fc.tar.bz2 |
Moved everything in browser/sync/net to common/net.
This is so it can be used by both common/net/notifier and browser/sync.
Removed unused and rotted unittest file.
BUG=42606
TEST=trybots
Review URL: http://codereview.chromium.org/2022001
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@46634 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chrome/browser')
14 files changed, 0 insertions, 1262 deletions
diff --git a/chrome/browser/sync/net/fake_network_change_notifier_thread.cc b/chrome/browser/sync/net/fake_network_change_notifier_thread.cc deleted file mode 100644 index f0d9c25..0000000 --- a/chrome/browser/sync/net/fake_network_change_notifier_thread.cc +++ /dev/null @@ -1,80 +0,0 @@ -// 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/browser/sync/net/fake_network_change_notifier_thread.h" - -#include "base/logging.h" -#include "base/message_loop.h" -#include "chrome/browser/sync/net/thread_blocker.h" -#include "net/base/mock_network_change_notifier.h" - -// We manage the lifetime of -// browser_sync::FakeNetworkChangeNotifierThread ourselves. -template <> -struct RunnableMethodTraits<browser_sync::FakeNetworkChangeNotifierThread> { - void RetainCallee(browser_sync::FakeNetworkChangeNotifierThread*) {} - void ReleaseCallee(browser_sync::FakeNetworkChangeNotifierThread*) {} -}; - -namespace browser_sync { - -FakeNetworkChangeNotifierThread::FakeNetworkChangeNotifierThread() - : thread_("FakeNetworkChangeNotifierThread") {} - -FakeNetworkChangeNotifierThread::~FakeNetworkChangeNotifierThread() { - CHECK(!network_change_notifier_.get()); - CHECK(!thread_blocker_.get()); - CHECK(!thread_.IsRunning()); -} - -void FakeNetworkChangeNotifierThread::Start() { - CHECK(thread_.Start()); - thread_blocker_.reset(new ThreadBlocker(&thread_)); - network_change_notifier_.reset(new net::MockNetworkChangeNotifier()); - thread_blocker_->Block(); -} - -void FakeNetworkChangeNotifierThread::Pump() { - thread_blocker_->Unblock(); - thread_blocker_->Block(); -} - -void FakeNetworkChangeNotifierThread::Stop() { - thread_blocker_->Unblock(); - network_change_notifier_.reset(); - thread_blocker_.reset(); - thread_.Stop(); -} - -void FakeNetworkChangeNotifierThread::NotifyIPAddressChange() { - CHECK(thread_.IsRunning()); - GetMessageLoop()->PostTask( - FROM_HERE, - NewRunnableMethod( - this, - &FakeNetworkChangeNotifierThread:: - NotifyIPAddressChangeOnSourceThread)); -} - -void FakeNetworkChangeNotifierThread::NotifyIPAddressChangeOnSourceThread() { - CHECK_EQ(MessageLoop::current(), GetMessageLoop()); - CHECK(network_change_notifier_.get()); - network_change_notifier_->NotifyIPAddressChange(); -} - -MessageLoop* FakeNetworkChangeNotifierThread::GetMessageLoop() const { - CHECK(thread_.IsRunning()); - MessageLoop* message_loop = thread_.message_loop(); - CHECK(message_loop); - return message_loop; -} - -net::NetworkChangeNotifier* -FakeNetworkChangeNotifierThread::GetNetworkChangeNotifier() const { - CHECK_EQ(MessageLoop::current(), GetMessageLoop()); - CHECK(network_change_notifier_.get()); - return network_change_notifier_.get(); -} - -} // namespace browser_sync diff --git a/chrome/browser/sync/net/fake_network_change_notifier_thread.h b/chrome/browser/sync/net/fake_network_change_notifier_thread.h deleted file mode 100644 index ccc3225..0000000 --- a/chrome/browser/sync/net/fake_network_change_notifier_thread.h +++ /dev/null @@ -1,64 +0,0 @@ -// 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_BROWSER_SYNC_NET_FAKE_NETWORK_CHANGE_NOTIFIER_THREAD_H_ -#define CHROME_BROWSER_SYNC_NET_FAKE_NETWORK_CHANGE_NOTIFIER_THREAD_H_ - -// A fake implementation of NetworkChangeNotifierThread used for -// unit-testing. - -#include "base/basictypes.h" -#include "base/scoped_ptr.h" -#include "base/thread.h" -#include "chrome/browser/sync/net/network_change_notifier_thread.h" - -namespace net { -class MockNetworkChangeNotifier; -} // namespace net - -namespace browser_sync { - -class ThreadBlocker; - -class FakeNetworkChangeNotifierThread : public NetworkChangeNotifierThread { - public: - FakeNetworkChangeNotifierThread(); - - virtual ~FakeNetworkChangeNotifierThread(); - - // Starts the thread in a blocked state and initializes the network - // change notifier. - void Start(); - - // Runs the tasks that are currently blocked. After this call, - // thread remains in a blocked state. A call to this function is a - // memory barrier. - void Pump(); - - // Stops the thread. - void Stop(); - - // Trigger an IP address change notification on the owned network - // change notifier on the owned thread. - void NotifyIPAddressChange(); - - // Implementation of NetworkChangeNotifierThread. - - virtual MessageLoop* GetMessageLoop() const; - - virtual net::NetworkChangeNotifier* GetNetworkChangeNotifier() const; - - private: - void NotifyIPAddressChangeOnSourceThread(); - - base::Thread thread_; - scoped_ptr<ThreadBlocker> thread_blocker_; - scoped_ptr<net::MockNetworkChangeNotifier> network_change_notifier_; - - DISALLOW_COPY_AND_ASSIGN(FakeNetworkChangeNotifierThread); -}; - -} // namespace browser_sync - -#endif // CHROME_BROWSER_SYNC_NET_FAKE_NETWORK_CHANGE_NOTIFIER_THREAD_H_ diff --git a/chrome/browser/sync/net/fake_network_change_notifier_thread_unittest.cc b/chrome/browser/sync/net/fake_network_change_notifier_thread_unittest.cc deleted file mode 100644 index 02828ce..0000000 --- a/chrome/browser/sync/net/fake_network_change_notifier_thread_unittest.cc +++ /dev/null @@ -1,121 +0,0 @@ -// 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/browser/sync/net/fake_network_change_notifier_thread.h" - -#include "base/basictypes.h" -#include "base/logging.h" -#include "base/message_loop.h" -#include "base/task.h" -#include "net/base/network_change_notifier.h" -#include "testing/gtest/include/gtest/gtest.h" - -namespace browser_sync { -class FlagToggler; -} // namespace browser_sync - -// We manage the lifetime of browser_sync::FlagToggler ourselves. -template <> -struct RunnableMethodTraits<browser_sync::FlagToggler> { - void RetainCallee(browser_sync::FlagToggler*) {} - void ReleaseCallee(browser_sync::FlagToggler*) {} -}; - -namespace browser_sync { - -// Utility class that toggles a flag every time it receives an IP -// address change notification. -class FlagToggler : public net::NetworkChangeNotifier::Observer { - public: - FlagToggler() : flag_(false) {} - - virtual ~FlagToggler() {} - - bool flag() const { return flag_; } - - void ToggleFlag() { - flag_ = !flag_; - } - - void Observe(NetworkChangeNotifierThread* thread) { - thread->GetNetworkChangeNotifier()->AddObserver(this); - } - - void Unobserve(NetworkChangeNotifierThread* thread) { - thread->GetNetworkChangeNotifier()->RemoveObserver(this); - } - - // net::NetworkChangeNotifier::Observer implementation. - virtual void OnIPAddressChanged() { - ToggleFlag(); - } - - private: - bool flag_; - - DISALLOW_COPY_AND_ASSIGN(FlagToggler); -}; - -namespace { - -class FakeNetworkChangeNotifierTest : public testing::Test { - protected: - FakeNetworkChangeNotifierTest() {} - - virtual ~FakeNetworkChangeNotifierTest() {} - - virtual void SetUp() { - thread_.Start(); - } - - virtual void TearDown() { - thread_.Stop(); - } - - FakeNetworkChangeNotifierThread thread_; - FlagToggler flag_toggler_; - - private: - DISALLOW_COPY_AND_ASSIGN(FakeNetworkChangeNotifierTest); -}; - -TEST_F(FakeNetworkChangeNotifierTest, Pump) { - thread_.GetMessageLoop()->PostTask( - FROM_HERE, NewRunnableMethod(&flag_toggler_, &FlagToggler::ToggleFlag)); - EXPECT_FALSE(flag_toggler_.flag()); - thread_.Pump(); - EXPECT_TRUE(flag_toggler_.flag()); -} - -TEST_F(FakeNetworkChangeNotifierTest, Basic) { - thread_.GetMessageLoop()->PostTask( - FROM_HERE, - NewRunnableMethod(&flag_toggler_, &FlagToggler::Observe, &thread_)); - thread_.NotifyIPAddressChange(); - thread_.GetMessageLoop()->PostTask( - FROM_HERE, - NewRunnableMethod(&flag_toggler_, &FlagToggler::Unobserve, &thread_)); - EXPECT_FALSE(flag_toggler_.flag()); - thread_.Pump(); - EXPECT_TRUE(flag_toggler_.flag()); -} - -TEST_F(FakeNetworkChangeNotifierTest, Multiple) { - FlagToggler observer; - thread_.GetMessageLoop()->PostTask( - FROM_HERE, - NewRunnableMethod(&flag_toggler_, &FlagToggler::Observe, &thread_)); - thread_.NotifyIPAddressChange(); - thread_.NotifyIPAddressChange(); - thread_.GetMessageLoop()->PostTask( - FROM_HERE, - NewRunnableMethod(&flag_toggler_, &FlagToggler::Unobserve, &thread_)); - EXPECT_FALSE(flag_toggler_.flag()); - thread_.Pump(); - EXPECT_FALSE(flag_toggler_.flag()); -} - -} // namespace - -} // namespace browser_sync diff --git a/chrome/browser/sync/net/mock_network_change_observer.h b/chrome/browser/sync/net/mock_network_change_observer.h deleted file mode 100644 index 91a5ef9..0000000 --- a/chrome/browser/sync/net/mock_network_change_observer.h +++ /dev/null @@ -1,32 +0,0 @@ -// 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_BROWSER_SYNC_NET_MOCK_NETWORK_CHANGE_OBSERVER_H_ -#define CHROME_BROWSER_SYNC_NET_MOCK_NETWORK_CHANGE_OBSERVER_H_ - -#include "base/basictypes.h" -#include "net/base/network_change_notifier.h" -#include "testing/gmock/include/gmock/gmock.h" - -// This class is a mock net::NetworkChangeNotifier::Observer used in -// unit tests. - -namespace browser_sync { - -class MockNetworkChangeObserver - : public net::NetworkChangeNotifier::Observer { - public: - MockNetworkChangeObserver() {} - - virtual ~MockNetworkChangeObserver() {} - - MOCK_METHOD0(OnIPAddressChanged, void()); - - private: - DISALLOW_COPY_AND_ASSIGN(MockNetworkChangeObserver); -}; - -} // namespace browser_sync - -#endif // CHROME_BROWSER_SYNC_NET_MOCK_NETWORK_CHANGE_OBSERVER_H_ diff --git a/chrome/browser/sync/net/network_change_notifier_proxy.cc b/chrome/browser/sync/net/network_change_notifier_proxy.cc deleted file mode 100644 index e1b3797..0000000 --- a/chrome/browser/sync/net/network_change_notifier_proxy.cc +++ /dev/null @@ -1,59 +0,0 @@ -// 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/browser/sync/net/network_change_notifier_proxy.h" - -#include "base/logging.h" -#include "base/message_loop.h" -#include "chrome/browser/sync/net/network_change_observer_proxy.h" - -namespace browser_sync { - -NetworkChangeNotifierProxy::NetworkChangeNotifierProxy( - NetworkChangeNotifierThread* source_thread) - : observer_proxy_(new NetworkChangeObserverProxy( - source_thread, MessageLoop::current())), - observer_repeater_(&observers_) { - // TODO(akalin): We get this from NonThreadSafe, which - // net::NetworkChangeNotifier inherits from. Interface classes - // really shouldn't be inheriting from NonThreadSafe; make it so - // that all the implementations of net::NetworkChangeNotifier - // inherit from NonThreadSafe directly and - // net::NetworkChangeNotifier doesn't. - DCHECK(CalledOnValidThread()); - DCHECK(observer_proxy_); - observer_proxy_->Attach(&observer_repeater_); -} - -NetworkChangeNotifierProxy::~NetworkChangeNotifierProxy() { - DCHECK(CalledOnValidThread()); - observer_proxy_->Detach(); -} - -void NetworkChangeNotifierProxy::AddObserver( - net::NetworkChangeNotifier::Observer* observer) { - DCHECK(CalledOnValidThread()); - observers_.AddObserver(observer); -} - -void NetworkChangeNotifierProxy::RemoveObserver( - net::NetworkChangeNotifier::Observer* observer) { - DCHECK(CalledOnValidThread()); - observers_.RemoveObserver(observer); -} - -NetworkChangeNotifierProxy::ObserverRepeater::ObserverRepeater( - NetworkObserverList* observers) : observers_(observers) { - DCHECK(observers_); -} - -NetworkChangeNotifierProxy::ObserverRepeater::~ObserverRepeater() {} - -void NetworkChangeNotifierProxy::ObserverRepeater::OnIPAddressChanged() { - DCHECK(CalledOnValidThread()); - FOR_EACH_OBSERVER(net::NetworkChangeNotifier::Observer, - *observers_, OnIPAddressChanged()); -} - -} // namespace browser_sync diff --git a/chrome/browser/sync/net/network_change_notifier_proxy.h b/chrome/browser/sync/net/network_change_notifier_proxy.h deleted file mode 100644 index 2789abc..0000000 --- a/chrome/browser/sync/net/network_change_notifier_proxy.h +++ /dev/null @@ -1,71 +0,0 @@ -// 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_BROWSER_SYNC_NET_NETWORK_CHANGE_NOTIFIER_PROXY_H_ -#define CHROME_BROWSER_SYNC_NET_NETWORK_CHANGE_NOTIFIER_PROXY_H_ - -// NetworkChangeNotifierProxy is a class that lets observers listen to -// a NetworkChangeNotifier that lives on another thread. - -#include "base/basictypes.h" -#include "base/non_thread_safe.h" -#include "base/observer_list.h" -#include "base/ref_counted.h" -#include "net/base/network_change_notifier.h" - -class MessageLoop; - -namespace browser_sync { - -class NetworkChangeNotifierThread; -class NetworkChangeObserverProxy; - -class NetworkChangeNotifierProxy : public net::NetworkChangeNotifier { - public: - // |source_thread| must be guaranteed to outlive the current thread. - // Does not take ownership of any arguments. - NetworkChangeNotifierProxy( - NetworkChangeNotifierThread* source_thread); - - virtual ~NetworkChangeNotifierProxy(); - - // net::NetworkChangeNotifier implementation. - - virtual void AddObserver(net::NetworkChangeNotifier::Observer* observer); - - virtual void RemoveObserver(net::NetworkChangeNotifier::Observer* observer); - - private: - typedef ObserverList<net::NetworkChangeNotifier::Observer, true> - NetworkObserverList; - - // Utility class that routes received notifications to a list of - // observers. - class ObserverRepeater : public NonThreadSafe, - public net::NetworkChangeNotifier::Observer { - public: - // Does not take ownership of the given observer list. - explicit ObserverRepeater(NetworkObserverList* observers); - - virtual ~ObserverRepeater(); - - // net::NetworkChangeNotifier::Observer implementation. - virtual void OnIPAddressChanged(); - - private: - NetworkObserverList* observers_; - - DISALLOW_COPY_AND_ASSIGN(ObserverRepeater); - }; - - scoped_refptr<NetworkChangeObserverProxy> observer_proxy_; - NetworkObserverList observers_; - ObserverRepeater observer_repeater_; - - DISALLOW_COPY_AND_ASSIGN(NetworkChangeNotifierProxy); -}; - -} // namespace browser_sync - -#endif // CHROME_BROWSER_SYNC_NET_NETWORK_CHANGE_NOTIFIER_PROXY_H_ diff --git a/chrome/browser/sync/net/network_change_notifier_proxy_unittest.cc b/chrome/browser/sync/net/network_change_notifier_proxy_unittest.cc deleted file mode 100644 index d92e078..0000000 --- a/chrome/browser/sync/net/network_change_notifier_proxy_unittest.cc +++ /dev/null @@ -1,101 +0,0 @@ -// 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/browser/sync/net/network_change_notifier_proxy.h" - -#include "base/basictypes.h" -#include "base/logging.h" -#include "base/ref_counted.h" -#include "base/scoped_ptr.h" -#include "chrome/browser/sync/net/fake_network_change_notifier_thread.h" -#include "chrome/browser/sync/net/mock_network_change_observer.h" -#include "net/base/network_change_notifier.h" -#include "testing/gtest/include/gtest/gtest.h" - -namespace browser_sync { - -namespace { - -class NetworkChangeNotifierProxyTest : public testing::Test { - protected: - NetworkChangeNotifierProxyTest() {} - - virtual ~NetworkChangeNotifierProxyTest() {} - - virtual void SetUp() { - source_thread_.Start(); - notifier_proxy_.reset(new NetworkChangeNotifierProxy(&source_thread_)); - } - - virtual void TearDown() { - // Posts a task to the source thread. - notifier_proxy_.reset(); - source_thread_.Stop(); - } - - // Trigger an "IP address changed" event on the source network - // change notifier on the source thread and propagate any generated - // notifications to the target thread. - void NotifyIPAddressChange() { - source_thread_.NotifyIPAddressChange(); - source_thread_.Pump(); - target_message_loop_.RunAllPending(); - } - - FakeNetworkChangeNotifierThread source_thread_; - - MessageLoop target_message_loop_; - MockNetworkChangeObserver target_observer_; - - scoped_ptr<net::NetworkChangeNotifier> notifier_proxy_; - - private: - DISALLOW_COPY_AND_ASSIGN(NetworkChangeNotifierProxyTest); -}; - -TEST_F(NetworkChangeNotifierProxyTest, Basic) { - EXPECT_CALL(target_observer_, OnIPAddressChanged()).Times(1); - - notifier_proxy_->AddObserver(&target_observer_); - NotifyIPAddressChange(); - notifier_proxy_->RemoveObserver(&target_observer_); -} - -TEST_F(NetworkChangeNotifierProxyTest, IgnoresEventAfterRemoveObserver) { - EXPECT_CALL(target_observer_, OnIPAddressChanged()).Times(0); - - notifier_proxy_->AddObserver(&target_observer_); - notifier_proxy_->RemoveObserver(&target_observer_); - NotifyIPAddressChange(); -} - -TEST_F(NetworkChangeNotifierProxyTest, IgnoresEventBeforeRemoveObserver) { - EXPECT_CALL(target_observer_, OnIPAddressChanged()).Times(0); - - NotifyIPAddressChange(); - notifier_proxy_->AddObserver(&target_observer_); - notifier_proxy_->RemoveObserver(&target_observer_); -} - -TEST_F(NetworkChangeNotifierProxyTest, Multiple) { - EXPECT_CALL(target_observer_, OnIPAddressChanged()).Times(0); - - const int kNumObservers = 5; - MockNetworkChangeObserver extra_observers[kNumObservers]; - for (int i = 0; i < kNumObservers; ++i) { - EXPECT_CALL(extra_observers[i], OnIPAddressChanged()).Times(1); - } - - for (int i = 0; i < kNumObservers; ++i) { - notifier_proxy_->AddObserver(&extra_observers[i]); - } - NotifyIPAddressChange(); - for (int i = 0; i < kNumObservers; ++i) { - notifier_proxy_->RemoveObserver(&extra_observers[i]); - } -} - -} // namespace - -} // namespace browser_sync diff --git a/chrome/browser/sync/net/network_change_notifier_thread.h b/chrome/browser/sync/net/network_change_notifier_thread.h deleted file mode 100644 index e7fe375..0000000 --- a/chrome/browser/sync/net/network_change_notifier_thread.h +++ /dev/null @@ -1,37 +0,0 @@ -// 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_BROWSER_SYNC_NET_NETWORK_CHANGE_NOTIFIER_THREAD_H_ -#define CHROME_BROWSER_SYNC_NET_NETWORK_CHANGE_NOTIFIER_THREAD_H_ - -// A simple interface that represents a thread which owns a -// NetworkChangeNotifier. - -class MessageLoop; - -namespace net { -class NetworkChangeNotifier; -} // namespace net - -namespace browser_sync { - -// An instance of this interface must live no longer than the thread -// it represents and its message loop and network change notifier. -class NetworkChangeNotifierThread { - public: - virtual ~NetworkChangeNotifierThread() {} - - // Returns the message loop for the thread that owns the - // NetworkChangeNotifier. Can be called on any thread. - virtual MessageLoop* GetMessageLoop() const = 0; - - // Returns the NetworkChangeNotifier of the thread. This method - // must be called only from the owning thread (i.e., by posting a - // task onto the message loop returned by GetMessageLoop()). - virtual net::NetworkChangeNotifier* GetNetworkChangeNotifier() const = 0; -}; - -} // namespace browser_sync - -#endif // CHROME_BROWSER_SYNC_NET_NETWORK_CHANGE_NOTIFIER_THREAD_H_ diff --git a/chrome/browser/sync/net/network_change_observer_proxy.cc b/chrome/browser/sync/net/network_change_observer_proxy.cc deleted file mode 100644 index 490ba5423..0000000 --- a/chrome/browser/sync/net/network_change_observer_proxy.cc +++ /dev/null @@ -1,138 +0,0 @@ -// 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. - -// Some notes that may help anyone trying to reason about this code: -// -// - The source thread must be guaranteed to outlive the target -// thread. This is so that it is guaranteed that any task posted to -// the source thread will eventually be run. In particular, we want -// to make sure that Detach() causes us to eventually be removed as -// an observer. -// -// Note that this implies that any task posted to the target thread -// from the source thread may not run. But we post only -// TargetObserverOnIPAddressChanged() tasks on the target thread, -// which we can safely drop. -// -// - The source NetworkChangeNotifier must be guaranteed to outlive -// the target thread. This is so that it is guaranteed that any -// task posted to the source thread can safely access the source -// NetworkChangeNotifier. -// -// - Ref-counting this class is necessary, although as a consequence -// we can't make any guarantees about which thread will destroy an -// instance. Earlier versions of this class tried to get away -// without ref-counting. One version deleted the class in -// Unobserve(); this didn't work because there may still be -// TargetObserverOnIPAddressChanged() tasks on the target thread. -// An attempt to fix this was to post a DeleteTask on the target -// thread from Unobserve(), but this meant that there would be no -// way of knowing when on the target thread the instance would be -// deleted. Indeed, as mentioned above, any tasks posted on the -// target thread may not run, so this introduced the possibility of -// a memory leak. -// -// - It is important that all posted tasks that work with a proxy be -// RunnableMethods so that the ref-counting guarantees that the -// proxy is still valid when the task runs. - -#include "chrome/browser/sync/net/network_change_observer_proxy.h" - -#include <cstddef> - -#include "base/logging.h" -#include "base/message_loop.h" -#include "base/task.h" -#include "chrome/browser/sync/net/network_change_notifier_thread.h" -#include "net/base/network_change_notifier.h" - -namespace browser_sync { - -NetworkChangeObserverProxy::NetworkChangeObserverProxy( - const NetworkChangeNotifierThread* source_thread, - MessageLoop* target_message_loop) - : source_thread_(source_thread), - target_message_loop_(target_message_loop), - target_observer_(NULL) { - DCHECK(source_thread_); - MessageLoop* source_message_loop = source_thread_->GetMessageLoop(); - DCHECK(source_message_loop); - DCHECK(target_message_loop_); - DCHECK_NE(source_message_loop, target_message_loop_); - DCHECK_EQ(MessageLoop::current(), target_message_loop_); -} - -NetworkChangeObserverProxy::~NetworkChangeObserverProxy() { - MessageLoop* current_message_loop = MessageLoop::current(); - // We can be deleted on either the source or target thread, so the - // best we can do is check that we're on either. - DCHECK((current_message_loop == source_thread_->GetMessageLoop()) || - (current_message_loop == target_message_loop_)); - // Even though only the target thread uses target_observer_, it - // should still be unset even if we're on the source thread; posting - // a task is effectively a memory barrier. - DCHECK(!target_observer_); -} - -void NetworkChangeObserverProxy::Observe() { - DCHECK_EQ(MessageLoop::current(), source_thread_->GetMessageLoop()); - net::NetworkChangeNotifier* source_network_change_notifier = - source_thread_->GetNetworkChangeNotifier(); - DCHECK(source_network_change_notifier); - source_network_change_notifier->AddObserver(this); -} - -// The Task from which this was called may hold the last reference to -// us (this is how we can get deleted on the source thread). -void NetworkChangeObserverProxy::Unobserve() { - DCHECK_EQ(MessageLoop::current(), source_thread_->GetMessageLoop()); - net::NetworkChangeNotifier* source_network_change_notifier = - source_thread_->GetNetworkChangeNotifier(); - DCHECK(source_network_change_notifier); - source_network_change_notifier->RemoveObserver(this); -} - -void NetworkChangeObserverProxy::Attach( - net::NetworkChangeNotifier::Observer* target_observer) { - DCHECK_EQ(MessageLoop::current(), target_message_loop_); - DCHECK(!target_observer_); - target_observer_ = target_observer; - DCHECK(target_observer_); - source_thread_->GetMessageLoop()->PostTask( - FROM_HERE, - NewRunnableMethod(this, &NetworkChangeObserverProxy::Observe)); -} - -void NetworkChangeObserverProxy::Detach() { - DCHECK_EQ(MessageLoop::current(), target_message_loop_); - DCHECK(target_observer_); - target_observer_ = NULL; - source_thread_->GetMessageLoop()->PostTask( - FROM_HERE, - NewRunnableMethod(this, &NetworkChangeObserverProxy::Unobserve)); -} - -// Although we may get this event after Detach() has been called on -// the target thread, we know that Unobserve() hasn't been called yet. -// But we know that it has been posted, so it at least holds a -// reference to us. -void NetworkChangeObserverProxy::OnIPAddressChanged() { - DCHECK_EQ(MessageLoop::current(), source_thread_->GetMessageLoop()); - target_message_loop_->PostTask( - FROM_HERE, - NewRunnableMethod( - this, - &NetworkChangeObserverProxy::TargetObserverOnIPAddressChanged)); -} - -// The Task from which this was called may hold the last reference to -// us (this is how we can get deleted on the target thread). -void NetworkChangeObserverProxy::TargetObserverOnIPAddressChanged() { - DCHECK_EQ(MessageLoop::current(), target_message_loop_); - if (target_observer_) { - target_observer_->OnIPAddressChanged(); - } -} - -} // namespace browser_sync diff --git a/chrome/browser/sync/net/network_change_observer_proxy.h b/chrome/browser/sync/net/network_change_observer_proxy.h deleted file mode 100644 index 34374e5..0000000 --- a/chrome/browser/sync/net/network_change_observer_proxy.h +++ /dev/null @@ -1,114 +0,0 @@ -// 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_BROWSER_SYNC_NET_NETWORK_CHANGE_OBSERVER_PROXY_H_ -#define CHROME_BROWSER_SYNC_NET_NETWORK_CHANGE_OBSERVER_PROXY_H_ - -// NetworkChangeObserverProxy is a class that listens to a -// NetworkChangeNotifier on one thread (the source thread, which is -// usually the Chrome IO thread) and emits events to a target observer -// on another thread (the target thread). -// -// How to use: -// -// In the target thread, create the observer proxy: -// -// NetworkChangeNotifierThread* source_thread = ...; -// NetworkChangeObserverProxy* proxy = -// new NetworkChangeObserverProxy(source_thread, -// MessageLoop::current()); -// -// Both source_thread and its owned NetworkChangeNotifier must be -// guaranteed to outlive the target (current) thread. -// -// Then, attach the target observer: -// -// proxy->Attach(target_observer); -// -// target_observer will then begin to receive events on the target -// thread. -// -// If you call Attach(), you *must* call Detach() before releasing: -// -// proxy->Detach(); -// proxy->Release(); // omit if proxy is a scoped_refptr -// proxy = NULL; -// -// The proxy may be destroyed on either the source or the target -// thread (depending on which one ends up holding the last reference). - -#include "base/basictypes.h" -#include "base/message_loop.h" -#include "base/ref_counted.h" -#include "net/base/network_change_notifier.h" - -namespace browser_sync { - -class NetworkChangeNotifierThread; - -// TODO(akalin): Remove use of private inheritance. -class NetworkChangeObserverProxy - : public base::RefCountedThreadSafe<NetworkChangeObserverProxy>, - private net::NetworkChangeNotifier::Observer { - public: - // All public methods (including the constructor) must be called on - // the target thread. - - // Does not take ownership of any arguments. - NetworkChangeObserverProxy( - const NetworkChangeNotifierThread* source_thread, - MessageLoop* target_message_loop); - - // After this method is called, |target_observer| will start - // receiving events on the target thread. Once called, Detach() - // must be called before releasing and you cannot call Attach() - // again until you have done so. Does not take ownership of - // |target_observer|. - void Attach(net::NetworkChangeNotifier::Observer* target_observer); - - // After this method is called, the target observer will stop - // receiving events. You can call Attach() again after this method - // is called. - void Detach(); - - protected: - // May be called on either the source or the target thread. Marked - // protected instead of private so that this class can be subclassed - // for unit tests. - virtual ~NetworkChangeObserverProxy(); - - private: - friend class base::RefCountedThreadSafe<NetworkChangeObserverProxy>; - - // Adds ourselves as an observer of - // |source_network_change_notifier_|. Must be called on the source - // thread. - void Observe(); - - // Removes ourselves as an observer of - // |source_network_change_notifier_|. Must be called on the source - // thread. - void Unobserve(); - - // net::NetworkChangeNotifier::Observer implementation. - // - // Called on the source thread. Posts - // TargetObserverOnIPAddressChanged() on the target thread. - virtual void OnIPAddressChanged(); - - // Called on the target thread. Invokes OnIPAddressChanged() on - // |target_observer_|. - void TargetObserverOnIPAddressChanged(); - - const NetworkChangeNotifierThread* source_thread_; - MessageLoop* const target_message_loop_; - // |target_observer_| is used only by the target thread. - net::NetworkChangeNotifier::Observer* target_observer_; - - DISALLOW_COPY_AND_ASSIGN(NetworkChangeObserverProxy); -}; - -} // namespace browser_sync - -#endif // CHROME_BROWSER_SYNC_NET_NETWORK_CHANGE_OBSERVER_PROXY_H_ diff --git a/chrome/browser/sync/net/network_change_observer_proxy_unittest.cc b/chrome/browser/sync/net/network_change_observer_proxy_unittest.cc deleted file mode 100644 index c39184b..0000000 --- a/chrome/browser/sync/net/network_change_observer_proxy_unittest.cc +++ /dev/null @@ -1,230 +0,0 @@ -// 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/browser/sync/net/network_change_observer_proxy.h" - -#include "base/basictypes.h" -#include "base/logging.h" -#include "base/ref_counted.h" -#include "chrome/browser/sync/net/fake_network_change_notifier_thread.h" -#include "chrome/browser/sync/net/mock_network_change_observer.h" -#include "testing/gtest/include/gtest/gtest.h" - -namespace browser_sync { - -namespace { - -// Version of NetworkChangeObserverProxy that records on what thread -// it was deleted. -class DeleteCheckingNetworkChangeObserverProxy - : public NetworkChangeObserverProxy { - public: - // *deleting_message_loop_ must be NULL. It is set to a non-NULL - // *value when this object is deleted. - DeleteCheckingNetworkChangeObserverProxy( - NetworkChangeNotifierThread* source_thread, - MessageLoop* target_message_loop, - MessageLoop** deleting_message_loop) - : NetworkChangeObserverProxy(source_thread, target_message_loop), - deleting_message_loop_(deleting_message_loop) { - CHECK(deleting_message_loop_); - EXPECT_TRUE(*deleting_message_loop_ == NULL); - } - - private: - virtual ~DeleteCheckingNetworkChangeObserverProxy() { - *deleting_message_loop_ = MessageLoop::current(); - } - - MessageLoop** deleting_message_loop_; - - DISALLOW_COPY_AND_ASSIGN(DeleteCheckingNetworkChangeObserverProxy); -}; - -class NetworkChangeObserverProxyTest : public testing::Test { - protected: - NetworkChangeObserverProxyTest() : proxy_deleting_message_loop_(NULL) {} - - virtual ~NetworkChangeObserverProxyTest() {} - - virtual void SetUp() { - source_thread_.Start(); - proxy_deleting_message_loop_ = NULL; - proxy_ = new DeleteCheckingNetworkChangeObserverProxy( - &source_thread_, &target_message_loop_, - &proxy_deleting_message_loop_); - } - - // On TearDown, |proxy_| must be released and both source and target - // must be pumped. - virtual void TearDown() { - EXPECT_TRUE(proxy_ == NULL); - EXPECT_TRUE(proxy_deleting_message_loop_ != NULL); - source_thread_.Stop(); - } - - // Pump any events posted on the source thread. - void PumpSource() { - source_thread_.Pump(); - } - - // Pump any events posted on the target thread (which is just the - // main test thread). - void PumpTarget() { - target_message_loop_.RunAllPending(); - } - - // Trigger an "IP address changed" event on the source network - // change notifier on the source thread. - void NotifyIPAddressChange() { - source_thread_.NotifyIPAddressChange(); - } - - FakeNetworkChangeNotifierThread source_thread_; - - MessageLoop target_message_loop_; - MockNetworkChangeObserver target_observer_; - - MessageLoop* proxy_deleting_message_loop_; - scoped_refptr<NetworkChangeObserverProxy> proxy_; - - private: - DISALLOW_COPY_AND_ASSIGN(NetworkChangeObserverProxyTest); -}; - -// Make sure nothing blows up if we don't attach the proxy. -TEST_F(NetworkChangeObserverProxyTest, DoNothing) { - EXPECT_CALL(target_observer_, OnIPAddressChanged()).Times(0); - - proxy_ = NULL; - // No need to pump. - - EXPECT_EQ(proxy_deleting_message_loop_, &target_message_loop_); -} - -TEST_F(NetworkChangeObserverProxyTest, AttachDetach) { - EXPECT_CALL(target_observer_, OnIPAddressChanged()).Times(0); - - proxy_->Attach(&target_observer_); - proxy_->Detach(); - proxy_ = NULL; - PumpSource(); - - EXPECT_EQ(proxy_deleting_message_loop_, source_thread_.GetMessageLoop()); -} - -TEST_F(NetworkChangeObserverProxyTest, AttachDetachReleaseAfterPump) { - EXPECT_CALL(target_observer_, OnIPAddressChanged()).Times(0); - - proxy_->Attach(&target_observer_); - proxy_->Detach(); - PumpSource(); - proxy_ = NULL; - - EXPECT_EQ(proxy_deleting_message_loop_, &target_message_loop_); -} - -TEST_F(NetworkChangeObserverProxyTest, Basic) { - EXPECT_CALL(target_observer_, OnIPAddressChanged()).Times(1); - - proxy_->Attach(&target_observer_); - NotifyIPAddressChange(); - PumpSource(); - PumpTarget(); - proxy_->Detach(); - proxy_ = NULL; - PumpSource(); - - EXPECT_EQ(proxy_deleting_message_loop_, source_thread_.GetMessageLoop()); -} - -TEST_F(NetworkChangeObserverProxyTest, Multiple) { - const int kTimes = 5; - EXPECT_CALL(target_observer_, OnIPAddressChanged()).Times(kTimes); - - proxy_->Attach(&target_observer_); - for (int i = 0; i < kTimes; ++i) { - NotifyIPAddressChange(); - } - PumpSource(); - PumpTarget(); - proxy_->Detach(); - proxy_ = NULL; - PumpSource(); - - EXPECT_EQ(proxy_deleting_message_loop_, source_thread_.GetMessageLoop()); -} - -TEST_F(NetworkChangeObserverProxyTest, MultipleAttachDetach) { - const int kTimes = 5; - EXPECT_CALL(target_observer_, OnIPAddressChanged()).Times(kTimes); - - for (int i = 0; i < kTimes; ++i) { - proxy_->Attach(&target_observer_); - NotifyIPAddressChange(); - PumpSource(); - PumpTarget(); - proxy_->Detach(); - } - proxy_ = NULL; - PumpSource(); - - EXPECT_EQ(proxy_deleting_message_loop_, source_thread_.GetMessageLoop()); -} - -TEST_F(NetworkChangeObserverProxyTest, IgnoresEventPostedAfterDetach) { - EXPECT_CALL(target_observer_, OnIPAddressChanged()).Times(0); - - proxy_->Attach(&target_observer_); - NotifyIPAddressChange(); - proxy_->Detach(); - proxy_ = NULL; - PumpSource(); - PumpTarget(); - - EXPECT_EQ(proxy_deleting_message_loop_, &target_message_loop_); -} - -TEST_F(NetworkChangeObserverProxyTest, IgnoresEventPostedBeforeDetach) { - EXPECT_CALL(target_observer_, OnIPAddressChanged()).Times(0); - - proxy_->Attach(&target_observer_); - NotifyIPAddressChange(); - PumpSource(); - proxy_->Detach(); - proxy_ = NULL; - PumpTarget(); - PumpSource(); - - EXPECT_EQ(proxy_deleting_message_loop_, source_thread_.GetMessageLoop()); -} - -TEST_F(NetworkChangeObserverProxyTest, IgnoresEventAfterDetach) { - EXPECT_CALL(target_observer_, OnIPAddressChanged()).Times(0); - - proxy_->Attach(&target_observer_); - proxy_->Detach(); - proxy_ = NULL; - NotifyIPAddressChange(); - PumpSource(); - - EXPECT_EQ(proxy_deleting_message_loop_, source_thread_.GetMessageLoop()); -} - -TEST_F(NetworkChangeObserverProxyTest, IgnoresEventBeforeAttach) { - EXPECT_CALL(target_observer_, OnIPAddressChanged()).Times(0); - - NotifyIPAddressChange(); - PumpSource(); - proxy_->Attach(&target_observer_); - proxy_->Detach(); - proxy_ = NULL; - PumpSource(); - - EXPECT_EQ(proxy_deleting_message_loop_, source_thread_.GetMessageLoop()); -} - -} // namespace - -} // namespace browser_sync diff --git a/chrome/browser/sync/net/thread_blocker.cc b/chrome/browser/sync/net/thread_blocker.cc deleted file mode 100644 index df3fafc..0000000 --- a/chrome/browser/sync/net/thread_blocker.cc +++ /dev/null @@ -1,51 +0,0 @@ -// 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/browser/sync/net/thread_blocker.h" - -#include "base/logging.h" -#include "base/message_loop.h" -#include "base/task.h" -#include "base/thread.h" -#include "base/waitable_event.h" - -// Since a ThreadBlocker is outlived by its target thread, we don't -// have to ref-count it. -template <> -struct RunnableMethodTraits<browser_sync::ThreadBlocker> { - void RetainCallee(browser_sync::ThreadBlocker*) {} - void ReleaseCallee(browser_sync::ThreadBlocker*) {} -}; - -namespace browser_sync { - -ThreadBlocker::ThreadBlocker(base::Thread* target_thread) - : target_message_loop_(target_thread->message_loop()), - is_blocked_(false, false), is_finished_blocking_(false, false), - is_unblocked_(false, false) { - DCHECK(target_message_loop_); -} - -void ThreadBlocker::Block() { - DCHECK_NE(MessageLoop::current(), target_message_loop_); - target_message_loop_->PostTask( - FROM_HERE, - NewRunnableMethod(this, &ThreadBlocker::BlockOnTargetThread)); - while (!is_blocked_.Wait()) {} -} - -void ThreadBlocker::Unblock() { - DCHECK_NE(MessageLoop::current(), target_message_loop_); - is_finished_blocking_.Signal(); - while (!is_unblocked_.Wait()) {} -} - -void ThreadBlocker::BlockOnTargetThread() { - DCHECK_EQ(MessageLoop::current(), target_message_loop_); - is_blocked_.Signal(); - while (!is_finished_blocking_.Wait()) {} - is_unblocked_.Signal(); -} - -} // namespace browser_sync diff --git a/chrome/browser/sync/net/thread_blocker.h b/chrome/browser/sync/net/thread_blocker.h deleted file mode 100644 index 3548c5d..0000000 --- a/chrome/browser/sync/net/thread_blocker.h +++ /dev/null @@ -1,51 +0,0 @@ -// 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_BROWSER_SYNC_NET_THREAD_BLOCKER_H_ -#define CHROME_BROWSER_SYNC_NET_THREAD_BLOCKER_H_ - -// This class (mainly used for testing) lets you block and unblock a -// thread at will. -// -// TODO(akalin): Consider moving this to base/ as this class is not -// sync-specific (ask darin about it). - -#include "base/basictypes.h" -#include "base/waitable_event.h" - -class MessageLoop; - -namespace base { -class Thread; -} // namespace base - -namespace browser_sync { - -class ThreadBlocker { - public: - // The given thread must already be started and it must outlive this - // instance. - explicit ThreadBlocker(base::Thread* target_thread); - - // When this function returns, the target thread will be blocked - // until Unblock() is called. Each call to Block() must be matched - // by a call to Unblock(). - void Block(); - - // When this function returns, the target thread is unblocked. - void Unblock(); - - private: - // On the target thread, blocks until Unblock() is called. - void BlockOnTargetThread(); - - MessageLoop* const target_message_loop_; - base::WaitableEvent is_blocked_, is_finished_blocking_, is_unblocked_; - - DISALLOW_COPY_AND_ASSIGN(ThreadBlocker); -}; - -} // namespace browser_sync - -#endif // CHROME_BROWSER_SYNC_NET_THREAD_BLOCKER_H_ diff --git a/chrome/browser/sync/net/thread_blocker_unittest.cc b/chrome/browser/sync/net/thread_blocker_unittest.cc deleted file mode 100644 index 4587e30..0000000 --- a/chrome/browser/sync/net/thread_blocker_unittest.cc +++ /dev/null @@ -1,113 +0,0 @@ -// 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/browser/sync/net/thread_blocker.h" - -#include "base/basictypes.h" -#include "base/lock.h" -#include "base/logging.h" -#include "base/message_loop.h" -#include "base/scoped_ptr.h" -#include "base/task.h" -#include "base/thread.h" -#include "testing/gtest/include/gtest/gtest.h" - -namespace browser_sync { -class Flag; -}; // namespace browser_sync - -// We manage the lifetime of browser_sync::Flag ourselves. -template <> -struct RunnableMethodTraits<browser_sync::Flag> { - void RetainCallee(browser_sync::Flag*) {} - void ReleaseCallee(browser_sync::Flag*) {} -}; - -namespace browser_sync { - -// Utility class that is basically just a thread-safe boolean. -class Flag { - public: - Flag() : flag_(false) {} - - bool IsSet() const { - AutoLock auto_lock(lock_); - return flag_; - } - - void Set() { - AutoLock auto_lock(lock_); - flag_ = true; - } - - void Unset() { - AutoLock auto_lock(lock_); - flag_ = false; - } - - private: - mutable Lock lock_; - bool flag_; - - DISALLOW_COPY_AND_ASSIGN(Flag); -}; - -namespace { - -class ThreadBlockerTest : public testing::Test { - protected: - ThreadBlockerTest() : target_thread_("Target Thread") {} - - virtual ~ThreadBlockerTest() { - CHECK(!thread_blocker_.get()); - } - - virtual void SetUp() { - CHECK(target_thread_.Start()); - thread_blocker_.reset(new ThreadBlocker(&target_thread_)); - } - - virtual void TearDown() { - target_thread_.Stop(); - thread_blocker_.reset(); - } - - base::Thread target_thread_; - scoped_ptr<ThreadBlocker> thread_blocker_; - Flag flag_; - - private: - DISALLOW_COPY_AND_ASSIGN(ThreadBlockerTest); -}; - -TEST_F(ThreadBlockerTest, Basic) { - thread_blocker_->Block(); - target_thread_.message_loop()->PostTask( - FROM_HERE, NewRunnableMethod(&flag_, &Flag::Set)); - EXPECT_FALSE(flag_.IsSet()); - thread_blocker_->Unblock(); - // Need to block again to make sure this thread waits for the posted - // method to run. - thread_blocker_->Block(); - EXPECT_TRUE(flag_.IsSet()); - thread_blocker_->Unblock(); -} - -TEST_F(ThreadBlockerTest, SetUnset) { - thread_blocker_->Block(); - target_thread_.message_loop()->PostTask( - FROM_HERE, NewRunnableMethod(&flag_, &Flag::Set)); - target_thread_.message_loop()->PostTask( - FROM_HERE, NewRunnableMethod(&flag_, &Flag::Unset)); - EXPECT_FALSE(flag_.IsSet()); - thread_blocker_->Unblock(); - // Need to block again here too. - thread_blocker_->Block(); - EXPECT_FALSE(flag_.IsSet()); - thread_blocker_->Unblock(); -} - -} // namespace - -} // namespace browser_sync |