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) {
CheckValidPosition(MockLibGps::g_instance_->get_position_, position);
}
class EnableGpsOpenTask : public Task {
public:
virtual void Run() {
CHECK(MockLibGps::g_instance_);
MockLibGps::g_instance_->gps_open_ret_ = 0;
}
};
void EnableGpsOpenCallback() {
CHECK(MockLibGps::g_instance_);
MockLibGps::g_instance_->gps_open_ret_ = 0;
}
TEST_F(GeolocationGpsProviderLinuxTests, LibGpsReconnect) {
// Setup gpsd reconnect interval to be 1000ms to speed up test.
......@@ -198,7 +195,7 @@ TEST_F(GeolocationGpsProviderLinuxTests, LibGpsReconnect) {
// This task makes gps_open() and LibGps::Start() to succeed after
// 1500ms.
MessageLoop::current()->PostDelayedTask(
FROM_HERE, new EnableGpsOpenTask(), 1500);
FROM_HERE, base::Bind(&EnableGpsOpenCallback), 1500);
MessageLoop::current()->Run();
provider_->GetPosition(&position);
EXPECT_TRUE(position.IsInitialized());
......
......@@ -4,6 +4,8 @@
#include "content/browser/mach_broker_mac.h"
#include "base/bind.h"
#include "base/bind_helpers.h"
#include "base/command_line.h"
#include "base/logging.h"
#include "base/mac/foundation_util.h"
......@@ -28,33 +30,6 @@ std::string MachErrorCode(kern_return_t err) {
}
} // 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 {
public:
MachListenerThreadDelegate(MachBroker* broker) : broker_(broker) {
......@@ -126,11 +101,6 @@ MachBroker* MachBroker::GetInstance() {
return Singleton<MachBroker, LeakySingletonTraits<MachBroker> >::get();
}
MachBroker::MachBroker() : listener_thread_started_(false) {
}
MachBroker::~MachBroker() {}
void MachBroker::EnsureRunning() {
lock_.AssertAcquired();
......@@ -138,7 +108,8 @@ void MachBroker::EnsureRunning() {
listener_thread_started_ = true;
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.
base::PlatformThread::CreateNonJoinable(
......@@ -238,3 +209,19 @@ std::string MachBroker::GetMachPortName() {
const pid_t pid = is_child ? getppid() : getpid();
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,
const content::NotificationSource& source,
const content::NotificationDetails& details) OVERRIDE;
private:
// Private constructor.
friend class MachBrokerTest;
friend struct DefaultSingletonTraits<MachBroker>;
MachBroker();
virtual ~MachBroker();
// Callback used to register notifications on the UI thread.
void RegisterNotifications();
// True if the listener thread has been started.
bool listener_thread_started_;
......@@ -104,10 +109,6 @@ class MachBroker : public base::ProcessMetrics::PortProvider,
// Mutex that guards |mach_map_|.
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);
};
......
......@@ -6,14 +6,15 @@
#include <string>
#include "build/build_config.h"
#include "base/base64.h"
#include "base/bind.h"
#include "base/location.h"
#include "base/logging.h"
#include "base/task.h"
#include "base/threading/worker_pool.h"
#include "base/threading/thread_restrictions.h"
#include "base/synchronization/waitable_event.h"
#include "build/build_config.h"
#include "crypto/nss_util.h"
#include "testing/gtest/include/gtest/gtest.h"
......@@ -81,41 +82,27 @@ TEST_F(KeygenHandlerTest, SmokeTest) {
AssertValidSignedPublicKeyAndChallenge(result, "some challenge");
}
class ConcurrencyTestTask : public Task {
public:
ConcurrencyTestTask(base::WaitableEvent* event,
const std::string& challenge, std::string* result)
: event_(event),
challenge_(challenge),
result_(result) {
}
virtual void Run() {
// 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();
void ConcurrencyTestCallback(base::WaitableEvent* event,
const std::string& challenge,
std::string* result) {
// 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, 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)
// Detach the thread from NSPR.
// Calling NSS functions attaches the thread to NSPR, which stores
// the NSPR thread ID in thread-specific data.
// The threads in our thread pool terminate after we have called
// PR_Cleanup. Unless we detach them from NSPR, net_unittests gets
// segfaults on shutdown when the threads' thread-specific data
// destructors run.
PR_DetachThread();
// Detach the thread from NSPR.
// Calling NSS functions attaches the thread to NSPR, which stores
// the NSPR thread ID in thread-specific data.
// The threads in our thread pool terminate after we have called
// PR_Cleanup. Unless we detach them from NSPR, net_unittests gets
// segfaults on shutdown when the threads' thread-specific data
// destructors run.
PR_DetachThread();
#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
// test tries to catch concurrency problems in the keygen implementation.
......@@ -127,7 +114,8 @@ TEST_F(KeygenHandlerTest, ConcurrencyTest) {
events[i] = new base::WaitableEvent(false, false);
base::WorkerPool::PostTask(
FROM_HERE,
new ConcurrencyTestTask(events[i], "some challenge", &results[i]),
base::Bind(ConcurrencyTestCallback, events[i], "some challenge",
&results[i]),
true);
}
......
......@@ -4,6 +4,7 @@
#include "net/curvecp/client_packetizer.h"
#include "base/bind.h"
#include "net/base/io_buffer.h"
#include "net/base/net_errors.h"
#include "net/base/sys_addrinfo.h"
......@@ -38,7 +39,7 @@ ClientPacketizer::ClientPacketizer()
initiate_sent_(false),
ALLOW_THIS_IN_INITIALIZER_LIST(
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.
// for now we use random values to keep them unique.
for (int i = 0; i < 32; ++i)
......@@ -315,12 +316,12 @@ int ClientPacketizer::ConnectNextAddress() {
void ClientPacketizer::StartHelloTimer(int milliseconds) {
MessageLoop::current()->PostDelayedTask(
FROM_HERE,
timers_factory_.NewRunnableMethod(&ClientPacketizer::OnHelloTimeout),
base::Bind(&ClientPacketizer::OnHelloTimeout, weak_factory_.GetWeakPtr()),
milliseconds);
}
void ClientPacketizer::RevokeHelloTimer() {
timers_factory_.RevokeAll();
weak_factory_.InvalidateWeakPtrs();
}
void ClientPacketizer::OnHelloTimeout() {
......
......@@ -8,6 +8,7 @@
#include "base/compiler_specific.h"
#include "base/memory/scoped_ptr.h"
#include "base/memory/weak_ptr.h"
#include "base/task.h"
#include "net/base/address_list.h"
#include "net/base/completion_callback.h"
......@@ -93,7 +94,7 @@ class ClientPacketizer : public Packetizer {
uchar shortterm_public_key_[32];
OldCompletionCallbackImpl<ClientPacketizer> io_callback_;
ScopedRunnableMethodFactory<ClientPacketizer> timers_factory_;
base::WeakPtrFactory<ClientPacketizer> weak_factory_;
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