summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--base/base.gypi1
-rw-r--r--base/message_loop_proxy.h45
-rw-r--r--chrome/browser/chrome_thread.cc47
-rw-r--r--chrome/browser/chrome_thread.h7
-rw-r--r--chrome/browser/chrome_thread_unittest.cc81
5 files changed, 167 insertions, 14 deletions
diff --git a/base/base.gypi b/base/base.gypi
index 9b5c41b..20c1a63 100644
--- a/base/base.gypi
+++ b/base/base.gypi
@@ -117,6 +117,7 @@
'memory_debug.h',
'message_loop.cc',
'message_loop.h',
+ 'message_loop_proxy.h',
'message_pump.h',
'message_pump_default.cc',
'message_pump_default.h',
diff --git a/base/message_loop_proxy.h b/base/message_loop_proxy.h
new file mode 100644
index 0000000..0cfe7ee
--- /dev/null
+++ b/base/message_loop_proxy.h
@@ -0,0 +1,45 @@
+// 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 BASE_MESSAGE_LOOP_PROXY_H_
+#define BASE_MESSAGE_LOOP_PROXY_H_
+
+#include "base/basictypes.h"
+#include "base/ref_counted.h"
+#include "base/task.h"
+
+// This class provides a thread-safe refcounted interface to the Post* methods
+// of a message loop. This class can outlive the target message loop.
+class MessageLoopProxy : public base::RefCountedThreadSafe<MessageLoopProxy> {
+ public:
+ // These are the same methods in message_loop.h, but are guaranteed to either
+ // get posted to the MessageLoop if it's still alive, or be deleted otherwise.
+ // They return true iff the thread existed and the task was posted. Note that
+ // even if the task is posted, there's no guarantee that it will run, since
+ // the target thread may already have a Quit message in its queue.
+ virtual bool PostTask(const tracked_objects::Location& from_here,
+ Task* task) = 0;
+ virtual bool PostDelayedTask(const tracked_objects::Location& from_here,
+ Task* task, int64 delay_ms) = 0;
+ virtual bool PostNonNestableTask(const tracked_objects::Location& from_here,
+ Task* task) = 0;
+ virtual bool PostNonNestableDelayedTask(
+ const tracked_objects::Location& from_here,
+ Task* task,
+ int64 delay_ms) = 0;
+
+ template <class T>
+ bool DeleteSoon(const tracked_objects::Location& from_here,
+ T* object) {
+ return PostNonNestableTask(from_here, new DeleteTask<T>(object));
+ }
+ template <class T>
+ bool ReleaseSoon(const tracked_objects::Location& from_here,
+ T* object) {
+ return PostNonNestableTask(from_here, new ReleaseTask<T>(object));
+ }
+};
+
+#endif // BASE_MESSAGE_LOOP_PROXY_H_
+
diff --git a/chrome/browser/chrome_thread.cc b/chrome/browser/chrome_thread.cc
index 300c2f6..6421a79 100644
--- a/chrome/browser/chrome_thread.cc
+++ b/chrome/browser/chrome_thread.cc
@@ -5,6 +5,7 @@
#include "chrome/browser/chrome_thread.h"
#include "base/message_loop.h"
+#include "base/message_loop_proxy.h"
// Friendly names for the well-known threads.
static const char* chrome_thread_names[ChromeThread::ID_COUNT] = {
@@ -19,6 +20,44 @@ static const char* chrome_thread_names[ChromeThread::ID_COUNT] = {
#endif
};
+// An implementation of MessageLoopProxy to be used in conjunction
+// with ChromeThread.
+class ChromeThreadMessageLoopProxy : public MessageLoopProxy {
+ public:
+ explicit ChromeThreadMessageLoopProxy(ChromeThread::ID identifier)
+ : id_(identifier) {
+ }
+
+ // MessageLoopProxy implementation.
+ virtual bool PostTask(const tracked_objects::Location& from_here,
+ Task* task) {
+ return ChromeThread::PostTask(id_, from_here, task);
+ }
+
+ virtual bool PostDelayedTask(const tracked_objects::Location& from_here,
+ Task* task, int64 delay_ms) {
+ return ChromeThread::PostDelayedTask(id_, from_here, task, delay_ms);
+ }
+
+ virtual bool PostNonNestableTask(const tracked_objects::Location& from_here,
+ Task* task) {
+ return ChromeThread::PostNonNestableTask(id_, from_here, task);
+ }
+
+ virtual bool PostNonNestableDelayedTask(
+ const tracked_objects::Location& from_here,
+ Task* task,
+ int64 delay_ms) {
+ return ChromeThread::PostNonNestableDelayedTask(id_, from_here, task,
+ delay_ms);
+ }
+
+ private:
+ ChromeThread::ID id_;
+ DISALLOW_COPY_AND_ASSIGN(ChromeThreadMessageLoopProxy);
+};
+
+
Lock ChromeThread::lock_;
ChromeThread* ChromeThread::chrome_threads_[ID_COUNT];
@@ -122,6 +161,14 @@ bool ChromeThread::GetCurrentThreadIdentifier(ID* identifier) {
}
// static
+scoped_refptr<MessageLoopProxy> ChromeThread::GetMessageLoopProxyForThread(
+ ID identifier) {
+ scoped_refptr<MessageLoopProxy> proxy =
+ new ChromeThreadMessageLoopProxy(identifier);
+ return proxy;
+}
+
+// static
bool ChromeThread::PostTaskHelper(
ID identifier,
const tracked_objects::Location& from_here,
diff --git a/chrome/browser/chrome_thread.h b/chrome/browser/chrome_thread.h
index 9628e2f..676f6b7 100644
--- a/chrome/browser/chrome_thread.h
+++ b/chrome/browser/chrome_thread.h
@@ -9,6 +9,8 @@
#include "base/task.h"
#include "base/thread.h"
+class MessageLoopProxy;
+
///////////////////////////////////////////////////////////////////////////////
// ChromeThread
//
@@ -126,6 +128,11 @@ class ChromeThread : public base::Thread {
// sets identifier to its ID. Otherwise returns false.
static bool GetCurrentThreadIdentifier(ID* identifier);
+ // Callers can hold on to a refcounted MessageLoopProxy beyond the lifetime
+ // of the thread.
+ static scoped_refptr<MessageLoopProxy> GetMessageLoopProxyForThread(
+ ID identifier);
+
// Use these templates in conjuction with RefCountedThreadSafe when you want
// to ensure that an object is deleted on a specific thread. This is needed
// when an object can hop between threads (i.e. IO -> FILE -> IO), and thread
diff --git a/chrome/browser/chrome_thread_unittest.cc b/chrome/browser/chrome_thread_unittest.cc
index 58a3dc3..a6ae766 100644
--- a/chrome/browser/chrome_thread_unittest.cc
+++ b/chrome/browser/chrome_thread_unittest.cc
@@ -3,6 +3,7 @@
// found in the LICENSE file.
#include "base/message_loop.h"
+#include "base/message_loop_proxy.h"
#include "chrome/browser/chrome_thread.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "testing/platform_test.h"
@@ -10,25 +11,25 @@
class ChromeThreadTest : public testing::Test {
public:
void Release() {
- CHECK(ChromeThread::CurrentlyOn(ChromeThread::FILE));
+ CHECK(ChromeThread::CurrentlyOn(ChromeThread::UI));
loop_.PostTask(FROM_HERE, new MessageLoop::QuitTask);
}
protected:
virtual void SetUp() {
+ ui_thread_.reset(new ChromeThread(ChromeThread::UI));
file_thread_.reset(new ChromeThread(ChromeThread::FILE));
- io_thread_.reset(new ChromeThread(ChromeThread::IO));
+ ui_thread_->Start();
file_thread_->Start();
- io_thread_->Start();
}
virtual void TearDown() {
+ ui_thread_->Stop();
file_thread_->Stop();
- io_thread_->Stop();
}
static void BasicFunction(MessageLoop* message_loop) {
- CHECK(ChromeThread::CurrentlyOn(ChromeThread::IO));
+ CHECK(ChromeThread::CurrentlyOn(ChromeThread::FILE));
message_loop->PostTask(FROM_HERE, new MessageLoop::QuitTask);
}
@@ -47,15 +48,15 @@ class ChromeThreadTest : public testing::Test {
bool* deleted_;
};
- class DeletedOnIO
+ class DeletedOnFile
: public base::RefCountedThreadSafe<
- DeletedOnIO, ChromeThread::DeleteOnIOThread> {
+ DeletedOnFile, ChromeThread::DeleteOnFileThread> {
public:
- explicit DeletedOnIO(MessageLoop* message_loop)
+ explicit DeletedOnFile(MessageLoop* message_loop)
: message_loop_(message_loop) { }
- ~DeletedOnIO() {
- CHECK(ChromeThread::CurrentlyOn(ChromeThread::IO));
+ ~DeletedOnFile() {
+ CHECK(ChromeThread::CurrentlyOn(ChromeThread::FILE));
message_loop_->PostTask(FROM_HERE, new MessageLoop::QuitTask());
}
@@ -73,20 +74,20 @@ class ChromeThreadTest : public testing::Test {
};
private:
+ scoped_ptr<ChromeThread> ui_thread_;
scoped_ptr<ChromeThread> file_thread_;
- scoped_ptr<ChromeThread> io_thread_;
MessageLoop loop_;
};
TEST_F(ChromeThreadTest, PostTask) {
ChromeThread::PostTask(
- ChromeThread::IO, FROM_HERE,
+ ChromeThread::FILE, FROM_HERE,
NewRunnableFunction(&BasicFunction, MessageLoop::current()));
MessageLoop::current()->Run();
}
TEST_F(ChromeThreadTest, Release) {
- ChromeThread::ReleaseSoon(ChromeThread::FILE, FROM_HERE, this);
+ ChromeThread::ReleaseSoon(ChromeThread::UI, FROM_HERE, this);
MessageLoop::current()->Run();
}
@@ -100,7 +101,8 @@ TEST_F(ChromeThreadTest, TaskToNonExistentThreadIsDeleted) {
TEST_F(ChromeThreadTest, ReleasedOnCorrectThread) {
{
- scoped_refptr<DeletedOnIO> test(new DeletedOnIO(MessageLoop::current()));
+ scoped_refptr<DeletedOnFile> test(
+ new DeletedOnFile(MessageLoop::current()));
}
MessageLoop::current()->Run();
}
@@ -108,3 +110,54 @@ TEST_F(ChromeThreadTest, ReleasedOnCorrectThread) {
TEST_F(ChromeThreadTest, NotReleasedIfTargetThreadNonExistent) {
scoped_refptr<NeverDeleted> test(new NeverDeleted());
}
+
+TEST_F(ChromeThreadTest, PostTaskViaMessageLoopProxy) {
+ scoped_refptr<MessageLoopProxy> message_loop_proxy =
+ ChromeThread::GetMessageLoopProxyForThread(ChromeThread::FILE);
+ message_loop_proxy->PostTask(FROM_HERE,
+ NewRunnableFunction(&BasicFunction,
+ MessageLoop::current()));
+ MessageLoop::current()->Run();
+}
+
+TEST_F(ChromeThreadTest, ReleaseViaMessageLoopProxy) {
+ scoped_refptr<MessageLoopProxy> message_loop_proxy =
+ ChromeThread::GetMessageLoopProxyForThread(ChromeThread::UI);
+ message_loop_proxy->ReleaseSoon(FROM_HERE, this);
+ MessageLoop::current()->Run();
+}
+
+TEST_F(ChromeThreadTest, TaskToNonExistentThreadIsDeletedViaMessageLoopProxy) {
+ bool deleted = false;
+ scoped_refptr<MessageLoopProxy> message_loop_proxy =
+ ChromeThread::GetMessageLoopProxyForThread(ChromeThread::WEBKIT);
+ message_loop_proxy->PostTask(FROM_HERE, new DummyTask(&deleted));
+ EXPECT_TRUE(deleted);
+}
+
+TEST_F(ChromeThreadTest, PostTaskViaMessageLoopProxyAfterThreadExits) {
+ scoped_ptr<ChromeThread> io_thread(new ChromeThread(ChromeThread::IO));
+ io_thread->Start();
+ io_thread->Stop();
+
+ bool deleted = false;
+ scoped_refptr<MessageLoopProxy> message_loop_proxy =
+ ChromeThread::GetMessageLoopProxyForThread(ChromeThread::IO);
+ bool ret = message_loop_proxy->PostTask(FROM_HERE, new DummyTask(&deleted));
+ EXPECT_FALSE(ret);
+ EXPECT_TRUE(deleted);
+}
+
+TEST_F(ChromeThreadTest, PostTaskViaMessageLoopProxyAfterThreadIsDeleted) {
+ {
+ scoped_ptr<ChromeThread> io_thread(new ChromeThread(ChromeThread::IO));
+ io_thread->Start();
+ }
+ bool deleted = false;
+ scoped_refptr<MessageLoopProxy> message_loop_proxy =
+ ChromeThread::GetMessageLoopProxyForThread(ChromeThread::IO);
+ bool ret = message_loop_proxy->PostTask(FROM_HERE, new DummyTask(&deleted));
+ EXPECT_FALSE(ret);
+ EXPECT_TRUE(deleted);
+}
+