Commit 4d89f64d authored by Eric Seckler's avatar Eric Seckler Committed by Commit Bot

content: Remove BrowserThread::Post*Task / GetTaskRunnerForThread.

This removes BrowserThread::Post(NonNestable|)(Delayed|)Task and moves
GetTaskRunnerForThread into the impl (it's still used to vend
TaskRunners in BrowserTaskExecutor).

These methods have been replaced by base/post_task.h in conjunction
with content/public/browser/browser_task_traits.h

Bug: 878356
Change-Id: Ia122dc0921769f43da9271ddd6e1ce2f402df779
Reviewed-on: https://chromium-review.googlesource.com/1235728
Commit-Queue: Eric Seckler <eseckler@chromium.org>
Reviewed-by: default avatarDmitry Gozman <dgozman@chromium.org>
Cr-Commit-Position: refs/heads/master@{#592879}
parent fddf844d
......@@ -25,58 +25,6 @@ namespace content {
namespace {
// An implementation of SingleThreadTaskRunner to be used in conjunction
// with BrowserThread.
// TODO(gab): Consider replacing this with |g_globals->task_runners| -- only
// works if none are requested before starting the threads.
class BrowserThreadTaskRunner : public base::SingleThreadTaskRunner {
public:
explicit BrowserThreadTaskRunner(BrowserThread::ID identifier)
: id_(identifier) {}
// SingleThreadTaskRunner implementation.
bool PostDelayedTask(const base::Location& from_here,
base::OnceClosure task,
base::TimeDelta delay) override {
return BrowserThread::PostDelayedTask(id_, from_here, std::move(task),
delay);
}
bool PostNonNestableDelayedTask(const base::Location& from_here,
base::OnceClosure task,
base::TimeDelta delay) override {
return BrowserThread::PostNonNestableDelayedTask(id_, from_here,
std::move(task), delay);
}
bool RunsTasksInCurrentSequence() const override {
return BrowserThread::CurrentlyOn(id_);
}
protected:
~BrowserThreadTaskRunner() override {}
private:
BrowserThread::ID id_;
DISALLOW_COPY_AND_ASSIGN(BrowserThreadTaskRunner);
};
// A separate helper is used just for the task runners, in order to avoid
// needing to initialize the globals to create a task runner.
struct BrowserThreadTaskRunners {
BrowserThreadTaskRunners() {
for (int i = 0; i < BrowserThread::ID_COUNT; ++i) {
proxies[i] =
new BrowserThreadTaskRunner(static_cast<BrowserThread::ID>(i));
}
}
scoped_refptr<base::SingleThreadTaskRunner> proxies[BrowserThread::ID_COUNT];
};
base::LazyInstance<BrowserThreadTaskRunners>::Leaky g_task_runners =
LAZY_INSTANCE_INITIALIZER;
// State of a given BrowserThread::ID in chronological order throughout the
// browser process' lifetime.
enum BrowserThreadState {
......@@ -162,6 +110,58 @@ bool PostTaskHelper(BrowserThread::ID identifier,
}
}
// An implementation of SingleThreadTaskRunner to be used in conjunction with
// BrowserThread. BrowserThreadTaskRunners are vended by
// BrowserThread::GetTaskRunnerForThread().
//
// TODO(gab): Consider replacing this with |g_globals->task_runners| -- only
// works if none are requested before starting the threads.
class BrowserThreadTaskRunner : public base::SingleThreadTaskRunner {
public:
explicit BrowserThreadTaskRunner(BrowserThread::ID identifier)
: id_(identifier) {}
// SingleThreadTaskRunner implementation.
bool PostDelayedTask(const base::Location& from_here,
base::OnceClosure task,
base::TimeDelta delay) override {
return PostTaskHelper(id_, from_here, std::move(task), delay, true);
}
bool PostNonNestableDelayedTask(const base::Location& from_here,
base::OnceClosure task,
base::TimeDelta delay) override {
return PostTaskHelper(id_, from_here, std::move(task), delay, false);
}
bool RunsTasksInCurrentSequence() const override {
return BrowserThread::CurrentlyOn(id_);
}
protected:
~BrowserThreadTaskRunner() override {}
private:
const BrowserThread::ID id_;
DISALLOW_COPY_AND_ASSIGN(BrowserThreadTaskRunner);
};
// A separate helper is used just for the task runners, in order to avoid
// needing to initialize the globals to create a task runner.
struct BrowserThreadTaskRunners {
BrowserThreadTaskRunners() {
for (int i = 0; i < BrowserThread::ID_COUNT; ++i) {
proxies[i] = base::MakeRefCounted<BrowserThreadTaskRunner>(
static_cast<BrowserThread::ID>(i));
}
}
scoped_refptr<base::SingleThreadTaskRunner> proxies[BrowserThread::ID_COUNT];
};
base::LazyInstance<BrowserThreadTaskRunners>::Leaky g_task_runners =
LAZY_INSTANCE_INITIALIZER;
} // namespace
BrowserThreadImpl::BrowserThreadImpl(
......@@ -275,46 +275,6 @@ std::string BrowserThread::GetDCheckCurrentlyOnErrorMessage(ID expected) {
return result;
}
bool BrowserThread::PostTask(ID identifier,
const base::Location& from_here,
base::OnceClosure task) {
return PostTaskHelper(identifier, from_here, std::move(task),
base::TimeDelta(), true);
}
// static
bool BrowserThread::PostDelayedTask(ID identifier,
const base::Location& from_here,
base::OnceClosure task,
base::TimeDelta delay) {
return PostTaskHelper(identifier, from_here, std::move(task), delay, true);
}
// static
bool BrowserThread::PostNonNestableTask(ID identifier,
const base::Location& from_here,
base::OnceClosure task) {
return PostTaskHelper(identifier, from_here, std::move(task),
base::TimeDelta(), false);
}
// static
bool BrowserThread::PostNonNestableDelayedTask(ID identifier,
const base::Location& from_here,
base::OnceClosure task,
base::TimeDelta delay) {
return PostTaskHelper(identifier, from_here, std::move(task), delay, false);
}
// static
bool BrowserThread::PostTaskAndReply(ID identifier,
const base::Location& from_here,
base::OnceClosure task,
base::OnceClosure reply) {
return GetTaskRunnerForThread(identifier)
->PostTaskAndReply(from_here, std::move(task), std::move(reply));
}
// static
bool BrowserThread::GetCurrentThreadIdentifier(ID* identifier) {
BrowserThreadGlobals& globals = g_globals.Get();
......
......@@ -39,6 +39,10 @@ class CONTENT_EXPORT BrowserThreadImpl : public BrowserThread {
// |identifier|.
static void ResetGlobalsForTesting(BrowserThread::ID identifier);
// Exposed for BrowserTaskExecutor. Other code should use
// base::CreateSingleThreadTaskRunnerWithTraits({BrowserThread::UI/IO}).
using BrowserThread::GetTaskRunnerForThread;
private:
// Restrict instantiation to BrowserProcessSubThread as it performs important
// initialization that shouldn't be bypassed (except by BrowserMainLoop for
......
......@@ -111,10 +111,9 @@ class UIThreadDestructionObserver
: callback_task_runner_(base::ThreadTaskRunnerHandle::Get()),
callback_(callback),
ui_task_runner_(
BrowserThread::GetTaskRunnerForThread(BrowserThread::UI)),
base::CreateSingleThreadTaskRunnerWithTraits({BrowserThread::UI})),
did_shutdown_(did_shutdown) {
BrowserThread::GetTaskRunnerForThread(BrowserThread::UI)
->PostTask(FROM_HERE, base::BindOnce(&Watch, this));
ui_task_runner_->PostTask(FROM_HERE, base::BindOnce(&Watch, this));
}
private:
......@@ -141,15 +140,6 @@ class UIThreadDestructionObserver
bool* did_shutdown_;
};
TEST_F(BrowserThreadTest, PostTask) {
base::RunLoop run_loop;
BrowserThread::PostTask(
BrowserThread::IO, FROM_HERE,
base::BindOnce(&BasicFunction, run_loop.QuitWhenIdleClosure(),
BrowserThread::IO));
run_loop.Run();
}
TEST_F(BrowserThreadTest, PostTaskWithTraits) {
base::RunLoop run_loop;
EXPECT_TRUE(base::PostTaskWithTraits(
......@@ -175,16 +165,6 @@ TEST_F(BrowserThreadTest, ReleasedOnCorrectThread) {
run_loop.Run();
}
TEST_F(BrowserThreadTest, PostTaskViaTaskRunner) {
scoped_refptr<base::SingleThreadTaskRunner> task_runner =
BrowserThread::GetTaskRunnerForThread(BrowserThread::IO);
base::RunLoop run_loop;
task_runner->PostTask(
FROM_HERE, base::BindOnce(&BasicFunction, run_loop.QuitWhenIdleClosure(),
BrowserThread::IO));
run_loop.Run();
}
TEST_F(BrowserThreadTest, PostTaskViaTaskRunnerWithTraits) {
scoped_refptr<base::TaskRunner> task_runner =
base::CreateTaskRunnerWithTraits({BrowserThread::IO});
......@@ -227,16 +207,6 @@ TEST_F(BrowserThreadTest, PostTaskViaCOMSTATaskRunnerWithTraits) {
}
#endif // defined(OS_WIN)
TEST_F(BrowserThreadTest, ReleaseViaTaskRunner) {
scoped_refptr<base::SingleThreadTaskRunner> task_runner =
BrowserThread::GetTaskRunnerForThread(BrowserThread::UI);
base::RunLoop run_loop;
ExpectRelease(run_loop.QuitWhenIdleClosure());
task_runner->ReleaseSoon(FROM_HERE, this);
run_loop.Run();
}
TEST_F(BrowserThreadTest, ReleaseViaTaskRunnerWithTraits) {
scoped_refptr<base::SingleThreadTaskRunner> task_runner =
base::CreateSingleThreadTaskRunnerWithTraits({BrowserThread::UI});
......@@ -246,16 +216,6 @@ TEST_F(BrowserThreadTest, ReleaseViaTaskRunnerWithTraits) {
run_loop.Run();
}
TEST_F(BrowserThreadTest, PostTaskAndReply) {
// Most of the heavy testing for PostTaskAndReply() is done inside the
// task runner test. This just makes sure we get piped through at all.
base::RunLoop run_loop;
ASSERT_TRUE(BrowserThread::PostTaskAndReply(BrowserThread::IO, FROM_HERE,
base::DoNothing(),
run_loop.QuitWhenIdleClosure()));
run_loop.Run();
}
TEST_F(BrowserThreadTest, PostTaskAndReplyWithTraits) {
// Most of the heavy testing for PostTaskAndReply() is done inside the
// task runner test. This just makes sure we get piped through at all.
......
......@@ -4,6 +4,8 @@
#include "content/browser/scheduler/browser_task_executor.h"
#include "content/browser/browser_thread_impl.h"
namespace content {
namespace {
......@@ -89,7 +91,7 @@ scoped_refptr<base::SingleThreadTaskRunner> BrowserTaskExecutor::GetTaskRunner(
const BrowserTaskTraitsExtension& extension) {
BrowserThread::ID thread_id = extension.browser_thread();
DCHECK_LT(thread_id, BrowserThread::ID::ID_COUNT);
return BrowserThread::GetTaskRunnerForThread(thread_id);
return BrowserThreadImpl::GetTaskRunnerForThread(thread_id);
}
} // namespace content
......@@ -8,6 +8,7 @@
#include "base/run_loop.h"
#include "base/task/post_task.h"
#include "base/task/task_scheduler/task_scheduler.h"
#include "content/browser/browser_thread_impl.h"
#include "content/public/browser/browser_task_traits.h"
#include "testing/gtest/include/gtest/gtest.h"
......@@ -37,12 +38,12 @@ class BrowserTaskExecutorTest : public testing::Test {
TEST_F(BrowserTaskExecutorTest, EnsureUIThreadTraitPointsToExpectedQueue) {
EXPECT_EQ(base::CreateSingleThreadTaskRunnerWithTraits({BrowserThread::UI}),
BrowserThread::GetTaskRunnerForThread(BrowserThread::UI));
BrowserThreadImpl::GetTaskRunnerForThread(BrowserThread::UI));
}
TEST_F(BrowserTaskExecutorTest, EnsureIOThreadTraitPointsToExpectedQueue) {
EXPECT_EQ(base::CreateSingleThreadTaskRunnerWithTraits({BrowserThread::IO}),
BrowserThread::GetTaskRunnerForThread(BrowserThread::IO));
BrowserThreadImpl::GetTaskRunnerForThread(BrowserThread::IO));
}
} // namespace content
......@@ -40,7 +40,6 @@ class BrowserThreadImpl;
// message loop.
//
// See browser_task_traits.h for posting Tasks to a BrowserThread.
// TODO(https://crbug.com/878356): Replace uses with base's post_task.h API.
//
// This class automatically handles the lifetime of different threads. You
// should never need to cache pointers to MessageLoops, since they're not thread
......@@ -67,64 +66,11 @@ class CONTENT_EXPORT BrowserThread {
ID_COUNT
};
// DEPRECATED: Please use the API described in browser_task_traits.h instead.
// TODO(https://crbug.com/878356): Replace uses with base::PostTaskWithTraits.
//
// 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.
static bool PostTask(ID identifier,
const base::Location& from_here,
base::OnceClosure task);
static bool PostDelayedTask(ID identifier,
const base::Location& from_here,
base::OnceClosure task,
base::TimeDelta delay);
static bool PostNonNestableTask(ID identifier,
const base::Location& from_here,
base::OnceClosure task);
static bool PostNonNestableDelayedTask(ID identifier,
const base::Location& from_here,
base::OnceClosure task,
base::TimeDelta delay);
static bool PostTaskAndReply(ID identifier,
const base::Location& from_here,
base::OnceClosure task,
base::OnceClosure reply);
template <typename ReturnType, typename ReplyArgType>
static bool PostTaskAndReplyWithResult(
ID identifier,
const base::Location& from_here,
base::OnceCallback<ReturnType()> task,
base::OnceCallback<void(ReplyArgType)> reply) {
scoped_refptr<base::SingleThreadTaskRunner> task_runner =
GetTaskRunnerForThread(identifier);
return base::PostTaskAndReplyWithResult(task_runner.get(), from_here,
std::move(task), std::move(reply));
}
// Callback version of PostTaskAndReplyWithResult above.
// Though RepeatingCallback is convertible to OnceCallback, we need this since
// we cannot use template deduction and object conversion at once on the
// overload resolution.
// TODO(crbug.com/714018): Update all callers of the Callback version to use
// OnceCallback.
template <typename ReturnType, typename ReplyArgType>
static bool PostTaskAndReplyWithResult(
ID identifier,
const base::Location& from_here,
base::Callback<ReturnType()> task,
base::Callback<void(ReplyArgType)> reply) {
return PostTaskAndReplyWithResult(
identifier, from_here,
base::OnceCallback<ReturnType()>(std::move(task)),
base::OnceCallback<void(ReplyArgType)>(std::move(reply)));
}
// NOTE: Task posting APIs have moved to post_task.h. See
// browser_task_traits.h.
// TODO(crbug.com/878356): Consider replacing callsites of this with
// base::CreateTaskRunnerWithTraits({id})->DeleteSoon(..).
template <class T>
static bool DeleteSoon(ID identifier,
const base::Location& from_here,
......@@ -153,6 +99,9 @@ class CONTENT_EXPORT BrowserThread {
// When called after the browser startup is complete, will post |task|
// to |task_runner| immediately.
// Note: see related ContentBrowserClient::PostAfterStartupTask.
//
// TODO(crbug.com/887407): Replace callsites with PostTaskWithTraits and
// appropriate traits (TBD).
static void PostAfterStartupTask(
const base::Location& from_here,
const scoped_refptr<base::TaskRunner>& task_runner,
......@@ -170,15 +119,6 @@ class CONTENT_EXPORT BrowserThread {
// sets identifier to its ID. Otherwise returns false.
static bool GetCurrentThreadIdentifier(ID* identifier) WARN_UNUSED_RESULT;
// DEPRECATED: Please use the API described in browser_task_traits.h instead.
// TODO(https://crbug.com/878356): Replace uses with
// base::Create*TaskRunnerWithTraits.
//
// Callers can hold on to a refcounted task runner beyond the lifetime of the
// thread.
static scoped_refptr<base::SingleThreadTaskRunner> GetTaskRunnerForThread(
ID identifier);
// Sets the delegate for BrowserThread::IO.
//
// Only one delegate may be registered at a time. The delegate may be
......@@ -245,6 +185,11 @@ class CONTENT_EXPORT BrowserThread {
// Returns an appropriate error message for when DCHECK_CURRENTLY_ON() fails.
static std::string GetDCheckCurrentlyOnErrorMessage(ID expected);
protected:
// For DeleteSoon().
static scoped_refptr<base::SingleThreadTaskRunner> GetTaskRunnerForThread(
ID identifier);
private:
friend class BrowserThreadImpl;
......
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