diff options
author | ajwong@chromium.org <ajwong@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2011-08-18 23:13:01 +0000 |
---|---|---|
committer | ajwong@chromium.org <ajwong@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2011-08-18 23:13:01 +0000 |
commit | c31af70db21e370eee35a677f6fcc62a2963784a (patch) | |
tree | b3f1908471efc10ac35d61cbffa2c7d160d6247f /content/browser | |
parent | d55c2380d728c2cc29c1a84970bb1d6f51c397d7 (diff) | |
download | chromium_src-c31af70db21e370eee35a677f6fcc62a2963784a.zip chromium_src-c31af70db21e370eee35a677f6fcc62a2963784a.tar.gz chromium_src-c31af70db21e370eee35a677f6fcc62a2963784a.tar.bz2 |
Implementation of PostTaskAndReply() in MessageLoopProxy and BrowserThread.
This ensures that the request/reply closures are always deleted on the origin
thread, or leaked if the task cannot be completed (due to message loop
shutdown).
BUG=86301
TEST=new unittests
Review URL: http://codereview.chromium.org/7210053
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@97387 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'content/browser')
-rw-r--r-- | content/browser/browser_thread.cc | 12 | ||||
-rw-r--r-- | content/browser/browser_thread.h | 6 | ||||
-rw-r--r-- | content/browser/browser_thread_unittest.cc | 18 |
3 files changed, 35 insertions, 1 deletions
diff --git a/content/browser/browser_thread.cc b/content/browser/browser_thread.cc index b4d95ce..c3b4005 100644 --- a/content/browser/browser_thread.cc +++ b/content/browser/browser_thread.cc @@ -4,6 +4,7 @@ #include "content/browser/browser_thread.h" +#include "base/bind.h" #include "base/message_loop.h" #include "base/message_loop_proxy.h" #include "base/threading/thread_restrictions.h" @@ -221,6 +222,17 @@ bool BrowserThread::PostNonNestableDelayedTask( } // static +bool BrowserThread::PostTaskAndReply( + ID identifier, + const tracked_objects::Location& from_here, + const base::Closure& task, + const base::Closure& reply) { + return GetMessageLoopProxyForThread(identifier)->PostTaskAndReply(from_here, + task, + reply); +} + +// static bool BrowserThread::GetCurrentThreadIdentifier(ID* identifier) { // We shouldn't use MessageLoop::current() since it uses LazyInstance which // may be deleted by ~AtExitManager when a WorkerPool thread calls this diff --git a/content/browser/browser_thread.h b/content/browser/browser_thread.h index 112ce3f..3c47fc0 100644 --- a/content/browser/browser_thread.h +++ b/content/browser/browser_thread.h @@ -128,6 +128,12 @@ class BrowserThread : public base::Thread { Task* task, int64 delay_ms); + static bool PostTaskAndReply( + ID identifier, + const tracked_objects::Location& from_here, + const base::Closure& task, + const base::Closure& reply); + template <class T> static bool DeleteSoon(ID identifier, const tracked_objects::Location& from_here, diff --git a/content/browser/browser_thread_unittest.cc b/content/browser/browser_thread_unittest.cc index 0a3ff17..f5f6e47 100644 --- a/content/browser/browser_thread_unittest.cc +++ b/content/browser/browser_thread_unittest.cc @@ -2,6 +2,7 @@ // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. +#include "base/bind.h" #include "base/memory/scoped_ptr.h" #include "base/message_loop.h" #include "base/message_loop_proxy.h" @@ -34,6 +35,9 @@ class BrowserThreadTest : public testing::Test { message_loop->PostTask(FROM_HERE, new MessageLoop::QuitTask); } + static void DoNothing() { + } + class DummyTask : public Task { public: explicit DummyTask(bool* deleted) : deleted_(deleted) { } @@ -130,6 +134,19 @@ TEST_F(BrowserThreadTest, ReleaseViaMessageLoopProxy) { MessageLoop::current()->Run(); } +TEST_F(BrowserThreadTest, PostTaskAndReply) { + // Most of the heavy testing for PostTaskAndReply() is done inside the + // MessageLoopProxy test. This just makes sure we get piped through at all. + ASSERT_TRUE(BrowserThread::PostTaskAndReply( + BrowserThread::FILE, + FROM_HERE, + base::Bind(&BrowserThreadTest::DoNothing), + base::Bind(&MessageLoop::Quit, + base::Unretained(MessageLoop::current()->current())))); + MessageLoop::current()->Run(); +} + + TEST_F(BrowserThreadTest, TaskToNonExistentThreadIsDeletedViaMessageLoopProxy) { bool deleted = false; scoped_refptr<base::MessageLoopProxy> message_loop_proxy = @@ -163,4 +180,3 @@ TEST_F(BrowserThreadTest, PostTaskViaMessageLoopProxyAfterThreadIsDeleted) { EXPECT_FALSE(ret); EXPECT_TRUE(deleted); } - |