summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorjhawkins@chromium.org <jhawkins@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2011-12-01 00:50:32 +0000
committerjhawkins@chromium.org <jhawkins@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2011-12-01 00:50:32 +0000
commite7f48fe5924df6cbd42dff40731ba029d2503678 (patch)
tree2cb2c0b04399b134f5170ba4fa6554a141cfb146
parente6e646d8463bfeb97894e756073add37a401c85b (diff)
downloadchromium_src-e7f48fe5924df6cbd42dff40731ba029d2503678.zip
chromium_src-e7f48fe5924df6cbd42dff40731ba029d2503678.tar.gz
chromium_src-e7f48fe5924df6cbd42dff40731ba029d2503678.tar.bz2
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
-rw-r--r--content/browser/geolocation/gps_location_provider_unittest_linux.cc13
-rw-r--r--content/browser/mach_broker_mac.cc53
-rw-r--r--content/browser/mach_broker_mac.h11
-rw-r--r--net/base/keygen_handler_unittest.cc58
-rw-r--r--net/curvecp/client_packetizer.cc7
-rw-r--r--net/curvecp/client_packetizer.h3
6 files changed, 60 insertions, 85 deletions
diff --git a/content/browser/geolocation/gps_location_provider_unittest_linux.cc b/content/browser/geolocation/gps_location_provider_unittest_linux.cc
index fba137b..bff72db 100644
--- a/content/browser/geolocation/gps_location_provider_unittest_linux.cc
+++ b/content/browser/geolocation/gps_location_provider_unittest_linux.cc
@@ -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());
diff --git a/content/browser/mach_broker_mac.cc b/content/browser/mach_broker_mac.cc
index 09f3e75..8dea5f5 100644
--- a/content/browser/mach_broker_mac.cc
+++ b/content/browser/mach_broker_mac.cc
@@ -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());
+}
diff --git a/content/browser/mach_broker_mac.h b/content/browser/mach_broker_mac.h
index 27a61dd..3a1cea3 100644
--- a/content/browser/mach_broker_mac.h
+++ b/content/browser/mach_broker_mac.h
@@ -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);
};
diff --git a/net/base/keygen_handler_unittest.cc b/net/base/keygen_handler_unittest.cc
index e10a160..b553411 100644
--- a/net/base/keygen_handler_unittest.cc
+++ b/net/base/keygen_handler_unittest.cc
@@ -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);
}
diff --git a/net/curvecp/client_packetizer.cc b/net/curvecp/client_packetizer.cc
index 2d96b1f..72bf4a0 100644
--- a/net/curvecp/client_packetizer.cc
+++ b/net/curvecp/client_packetizer.cc
@@ -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() {
diff --git a/net/curvecp/client_packetizer.h b/net/curvecp/client_packetizer.h
index 4adfe80..4915bbe 100644
--- a/net/curvecp/client_packetizer.h
+++ b/net/curvecp/client_packetizer.h
@@ -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);
};