Commit e7f48fe5 authored by jhawkins@chromium.org's avatar jhawkins@chromium.org

base::Bind: More random cleanups.

BUG=none
TEST=none

R=groby@chromium.org

Review URL: http://codereview.chromium.org/8734017

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@112362 0039d316-1c4b-4281-b951-d872f2087c98
parent e6e646d8
...@@ -168,13 +168,10 @@ TEST_F(GeolocationGpsProviderLinuxTests, GetPosition) { ...@@ -168,13 +168,10 @@ TEST_F(GeolocationGpsProviderLinuxTests, GetPosition) {
CheckValidPosition(MockLibGps::g_instance_->get_position_, position); CheckValidPosition(MockLibGps::g_instance_->get_position_, position);
} }
class EnableGpsOpenTask : public Task { void EnableGpsOpenCallback() {
public: CHECK(MockLibGps::g_instance_);
virtual void Run() { MockLibGps::g_instance_->gps_open_ret_ = 0;
CHECK(MockLibGps::g_instance_); }
MockLibGps::g_instance_->gps_open_ret_ = 0;
}
};
TEST_F(GeolocationGpsProviderLinuxTests, LibGpsReconnect) { TEST_F(GeolocationGpsProviderLinuxTests, LibGpsReconnect) {
// Setup gpsd reconnect interval to be 1000ms to speed up test. // Setup gpsd reconnect interval to be 1000ms to speed up test.
...@@ -198,7 +195,7 @@ TEST_F(GeolocationGpsProviderLinuxTests, LibGpsReconnect) { ...@@ -198,7 +195,7 @@ TEST_F(GeolocationGpsProviderLinuxTests, LibGpsReconnect) {
// This task makes gps_open() and LibGps::Start() to succeed after // This task makes gps_open() and LibGps::Start() to succeed after
// 1500ms. // 1500ms.
MessageLoop::current()->PostDelayedTask( MessageLoop::current()->PostDelayedTask(
FROM_HERE, new EnableGpsOpenTask(), 1500); FROM_HERE, base::Bind(&EnableGpsOpenCallback), 1500);
MessageLoop::current()->Run(); MessageLoop::current()->Run();
provider_->GetPosition(&position); provider_->GetPosition(&position);
EXPECT_TRUE(position.IsInitialized()); EXPECT_TRUE(position.IsInitialized());
......
...@@ -4,6 +4,8 @@ ...@@ -4,6 +4,8 @@
#include "content/browser/mach_broker_mac.h" #include "content/browser/mach_broker_mac.h"
#include "base/bind.h"
#include "base/bind_helpers.h"
#include "base/command_line.h" #include "base/command_line.h"
#include "base/logging.h" #include "base/logging.h"
#include "base/mac/foundation_util.h" #include "base/mac/foundation_util.h"
...@@ -28,33 +30,6 @@ std::string MachErrorCode(kern_return_t err) { ...@@ -28,33 +30,6 @@ std::string MachErrorCode(kern_return_t err) {
} }
} // namespace } // namespace
// Required because notifications happen on the UI thread.
class RegisterNotificationTask : public Task {
public:
RegisterNotificationTask(
MachBroker* broker)
: broker_(broker) { }
virtual void Run() {
broker_->registrar_.Add(broker_,
content::NOTIFICATION_RENDERER_PROCESS_CLOSED,
content::NotificationService::AllBrowserContextsAndSources());
broker_->registrar_.Add(broker_,
content::NOTIFICATION_RENDERER_PROCESS_TERMINATED,
content::NotificationService::AllBrowserContextsAndSources());
broker_->registrar_.Add(broker_,
content::NOTIFICATION_CHILD_PROCESS_CRASHED,
content::NotificationService::AllBrowserContextsAndSources());
broker_->registrar_.Add(broker_,
content::NOTIFICATION_CHILD_PROCESS_HOST_DISCONNECTED,
content::NotificationService::AllBrowserContextsAndSources());
}
private:
MachBroker* broker_;
DISALLOW_COPY_AND_ASSIGN(RegisterNotificationTask);
};
class MachListenerThreadDelegate : public base::PlatformThread::Delegate { class MachListenerThreadDelegate : public base::PlatformThread::Delegate {
public: public:
MachListenerThreadDelegate(MachBroker* broker) : broker_(broker) { MachListenerThreadDelegate(MachBroker* broker) : broker_(broker) {
...@@ -126,11 +101,6 @@ MachBroker* MachBroker::GetInstance() { ...@@ -126,11 +101,6 @@ MachBroker* MachBroker::GetInstance() {
return Singleton<MachBroker, LeakySingletonTraits<MachBroker> >::get(); return Singleton<MachBroker, LeakySingletonTraits<MachBroker> >::get();
} }
MachBroker::MachBroker() : listener_thread_started_(false) {
}
MachBroker::~MachBroker() {}
void MachBroker::EnsureRunning() { void MachBroker::EnsureRunning() {
lock_.AssertAcquired(); lock_.AssertAcquired();
...@@ -138,7 +108,8 @@ void MachBroker::EnsureRunning() { ...@@ -138,7 +108,8 @@ void MachBroker::EnsureRunning() {
listener_thread_started_ = true; listener_thread_started_ = true;
BrowserThread::PostTask( BrowserThread::PostTask(
BrowserThread::UI, FROM_HERE, new RegisterNotificationTask(this)); BrowserThread::UI, FROM_HERE,
base::Bind(&MachBroker::RegisterNotifications, base::Unretained(this)));
// Intentional leak. This thread is never joined or reaped. // Intentional leak. This thread is never joined or reaped.
base::PlatformThread::CreateNonJoinable( base::PlatformThread::CreateNonJoinable(
...@@ -238,3 +209,19 @@ std::string MachBroker::GetMachPortName() { ...@@ -238,3 +209,19 @@ std::string MachBroker::GetMachPortName() {
const pid_t pid = is_child ? getppid() : getpid(); const pid_t pid = is_child ? getppid() : getpid();
return base::StringPrintf("%s.rohitfork.%d", base::mac::BaseBundleID(), pid); return base::StringPrintf("%s.rohitfork.%d", base::mac::BaseBundleID(), pid);
} }
MachBroker::MachBroker() : listener_thread_started_(false) {
}
MachBroker::~MachBroker() {}
void MachBroker::RegisterNotifications() {
registrar_.Add(this, content::NOTIFICATION_RENDERER_PROCESS_CLOSED,
content::NotificationService::AllBrowserContextsAndSources());
registrar_.Add(this, content::NOTIFICATION_RENDERER_PROCESS_TERMINATED,
content::NotificationService::AllBrowserContextsAndSources());
registrar_.Add(this, content::NOTIFICATION_CHILD_PROCESS_CRASHED,
content::NotificationService::AllBrowserContextsAndSources());
registrar_.Add(this, content::NOTIFICATION_CHILD_PROCESS_HOST_DISCONNECTED,
content::NotificationService::AllBrowserContextsAndSources());
}
...@@ -86,10 +86,15 @@ class MachBroker : public base::ProcessMetrics::PortProvider, ...@@ -86,10 +86,15 @@ class MachBroker : public base::ProcessMetrics::PortProvider,
const content::NotificationSource& source, const content::NotificationSource& source,
const content::NotificationDetails& details) OVERRIDE; const content::NotificationDetails& details) OVERRIDE;
private: private:
// Private constructor. friend class MachBrokerTest;
friend struct DefaultSingletonTraits<MachBroker>;
MachBroker(); MachBroker();
virtual ~MachBroker(); virtual ~MachBroker();
// Callback used to register notifications on the UI thread.
void RegisterNotifications();
// True if the listener thread has been started. // True if the listener thread has been started.
bool listener_thread_started_; bool listener_thread_started_;
...@@ -104,10 +109,6 @@ class MachBroker : public base::ProcessMetrics::PortProvider, ...@@ -104,10 +109,6 @@ class MachBroker : public base::ProcessMetrics::PortProvider,
// Mutex that guards |mach_map_|. // Mutex that guards |mach_map_|.
mutable base::Lock lock_; mutable base::Lock lock_;
friend class MachBrokerTest;
friend class RegisterNotificationTask;
// Needed in order to make the constructor private.
friend struct DefaultSingletonTraits<MachBroker>;
DISALLOW_COPY_AND_ASSIGN(MachBroker); DISALLOW_COPY_AND_ASSIGN(MachBroker);
}; };
......
...@@ -6,14 +6,15 @@ ...@@ -6,14 +6,15 @@
#include <string> #include <string>
#include "build/build_config.h"
#include "base/base64.h" #include "base/base64.h"
#include "base/bind.h"
#include "base/location.h" #include "base/location.h"
#include "base/logging.h" #include "base/logging.h"
#include "base/task.h" #include "base/task.h"
#include "base/threading/worker_pool.h" #include "base/threading/worker_pool.h"
#include "base/threading/thread_restrictions.h" #include "base/threading/thread_restrictions.h"
#include "base/synchronization/waitable_event.h" #include "base/synchronization/waitable_event.h"
#include "build/build_config.h"
#include "crypto/nss_util.h" #include "crypto/nss_util.h"
#include "testing/gtest/include/gtest/gtest.h" #include "testing/gtest/include/gtest/gtest.h"
...@@ -81,41 +82,27 @@ TEST_F(KeygenHandlerTest, SmokeTest) { ...@@ -81,41 +82,27 @@ TEST_F(KeygenHandlerTest, SmokeTest) {
AssertValidSignedPublicKeyAndChallenge(result, "some challenge"); AssertValidSignedPublicKeyAndChallenge(result, "some challenge");
} }
class ConcurrencyTestTask : public Task { void ConcurrencyTestCallback(base::WaitableEvent* event,
public: const std::string& challenge,
ConcurrencyTestTask(base::WaitableEvent* event, std::string* result) {
const std::string& challenge, std::string* result) // We allow Singleton use on the worker thread here since we use a
: event_(event), // WaitableEvent to synchronize, so it's safe.
challenge_(challenge), base::ThreadRestrictions::ScopedAllowSingleton scoped_allow_singleton;
result_(result) { KeygenHandler handler(768, challenge, GURL("http://www.example.com"));
} handler.set_stores_key(false); // Don't leave the key-pair behind.
*result = handler.GenKeyAndSignChallenge();
virtual void Run() { event->Signal();
// We allow Singleton use on the worker thread here since we use a
// WaitableEvent to synchronize, so it's safe.
base::ThreadRestrictions::ScopedAllowSingleton scoped_allow_singleton;
KeygenHandler handler(768, "some challenge",
GURL("http://www.example.com"));
handler.set_stores_key(false); // Don't leave the key-pair behind.
*result_ = handler.GenKeyAndSignChallenge();
event_->Signal();
#if defined(USE_NSS) #if defined(USE_NSS)
// Detach the thread from NSPR. // Detach the thread from NSPR.
// Calling NSS functions attaches the thread to NSPR, which stores // Calling NSS functions attaches the thread to NSPR, which stores
// the NSPR thread ID in thread-specific data. // the NSPR thread ID in thread-specific data.
// The threads in our thread pool terminate after we have called // The threads in our thread pool terminate after we have called
// PR_Cleanup. Unless we detach them from NSPR, net_unittests gets // PR_Cleanup. Unless we detach them from NSPR, net_unittests gets
// segfaults on shutdown when the threads' thread-specific data // segfaults on shutdown when the threads' thread-specific data
// destructors run. // destructors run.
PR_DetachThread(); PR_DetachThread();
#endif #endif
} }
private:
base::WaitableEvent* event_;
std::string challenge_;
std::string* result_;
};
// We asynchronously generate the keys so as not to hang up the IO thread. This // We asynchronously generate the keys so as not to hang up the IO thread. This
// test tries to catch concurrency problems in the keygen implementation. // test tries to catch concurrency problems in the keygen implementation.
...@@ -127,7 +114,8 @@ TEST_F(KeygenHandlerTest, ConcurrencyTest) { ...@@ -127,7 +114,8 @@ TEST_F(KeygenHandlerTest, ConcurrencyTest) {
events[i] = new base::WaitableEvent(false, false); events[i] = new base::WaitableEvent(false, false);
base::WorkerPool::PostTask( base::WorkerPool::PostTask(
FROM_HERE, FROM_HERE,
new ConcurrencyTestTask(events[i], "some challenge", &results[i]), base::Bind(ConcurrencyTestCallback, events[i], "some challenge",
&results[i]),
true); true);
} }
......
...@@ -4,6 +4,7 @@ ...@@ -4,6 +4,7 @@
#include "net/curvecp/client_packetizer.h" #include "net/curvecp/client_packetizer.h"
#include "base/bind.h"
#include "net/base/io_buffer.h" #include "net/base/io_buffer.h"
#include "net/base/net_errors.h" #include "net/base/net_errors.h"
#include "net/base/sys_addrinfo.h" #include "net/base/sys_addrinfo.h"
...@@ -38,7 +39,7 @@ ClientPacketizer::ClientPacketizer() ...@@ -38,7 +39,7 @@ ClientPacketizer::ClientPacketizer()
initiate_sent_(false), initiate_sent_(false),
ALLOW_THIS_IN_INITIALIZER_LIST( ALLOW_THIS_IN_INITIALIZER_LIST(
io_callback_(this, &ClientPacketizer::OnIOComplete)), io_callback_(this, &ClientPacketizer::OnIOComplete)),
ALLOW_THIS_IN_INITIALIZER_LIST(timers_factory_(this)) { ALLOW_THIS_IN_INITIALIZER_LIST(weak_factory_(this)) {
// TODO(mbelshe): Initialize our keys and such properly. // TODO(mbelshe): Initialize our keys and such properly.
// for now we use random values to keep them unique. // for now we use random values to keep them unique.
for (int i = 0; i < 32; ++i) for (int i = 0; i < 32; ++i)
...@@ -315,12 +316,12 @@ int ClientPacketizer::ConnectNextAddress() { ...@@ -315,12 +316,12 @@ int ClientPacketizer::ConnectNextAddress() {
void ClientPacketizer::StartHelloTimer(int milliseconds) { void ClientPacketizer::StartHelloTimer(int milliseconds) {
MessageLoop::current()->PostDelayedTask( MessageLoop::current()->PostDelayedTask(
FROM_HERE, FROM_HERE,
timers_factory_.NewRunnableMethod(&ClientPacketizer::OnHelloTimeout), base::Bind(&ClientPacketizer::OnHelloTimeout, weak_factory_.GetWeakPtr()),
milliseconds); milliseconds);
} }
void ClientPacketizer::RevokeHelloTimer() { void ClientPacketizer::RevokeHelloTimer() {
timers_factory_.RevokeAll(); weak_factory_.InvalidateWeakPtrs();
} }
void ClientPacketizer::OnHelloTimeout() { void ClientPacketizer::OnHelloTimeout() {
......
...@@ -8,6 +8,7 @@ ...@@ -8,6 +8,7 @@
#include "base/compiler_specific.h" #include "base/compiler_specific.h"
#include "base/memory/scoped_ptr.h" #include "base/memory/scoped_ptr.h"
#include "base/memory/weak_ptr.h"
#include "base/task.h" #include "base/task.h"
#include "net/base/address_list.h" #include "net/base/address_list.h"
#include "net/base/completion_callback.h" #include "net/base/completion_callback.h"
...@@ -93,7 +94,7 @@ class ClientPacketizer : public Packetizer { ...@@ -93,7 +94,7 @@ class ClientPacketizer : public Packetizer {
uchar shortterm_public_key_[32]; uchar shortterm_public_key_[32];
OldCompletionCallbackImpl<ClientPacketizer> io_callback_; OldCompletionCallbackImpl<ClientPacketizer> io_callback_;
ScopedRunnableMethodFactory<ClientPacketizer> timers_factory_; base::WeakPtrFactory<ClientPacketizer> weak_factory_;
DISALLOW_COPY_AND_ASSIGN(ClientPacketizer); DISALLOW_COPY_AND_ASSIGN(ClientPacketizer);
}; };
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment