Commit dd00e5b8 authored by Peter Kasting's avatar Peter Kasting Committed by Commit Bot

Allow WebDataService APIs to be called from any thread.

Calling one of these APIs results in creating a WebDataRequest object to allow
calling back with the result.  This object assumed the caller had a
ThreadTaskRunnerHandle, but there's no reason callers need to have such a
handle.  Instead, allow calling any task back, using the thread or sequenced
task runner if available, or simply PostTask() if not.

Bug: 689520
Change-Id: Icf901e6de5a5b629bcf9d17617baf46a45502844
Reviewed-on: https://chromium-review.googlesource.com/578572
Commit-Queue: Peter Kasting <pkasting@chromium.org>
Reviewed-by: default avatarRobert Liao <robliao@chromium.org>
Cr-Commit-Position: refs/heads/master@{#488152}
parent 0ddd2a9b
...@@ -11,6 +11,8 @@ ...@@ -11,6 +11,8 @@
#include "base/memory/ptr_util.h" #include "base/memory/ptr_util.h"
#include "base/profiler/scoped_tracker.h" #include "base/profiler/scoped_tracker.h"
#include "base/stl_util.h" #include "base/stl_util.h"
#include "base/task_scheduler/post_task.h"
#include "base/threading/sequenced_task_runner_handle.h"
#include "base/threading/thread_task_runner_handle.h" #include "base/threading/thread_task_runner_handle.h"
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
...@@ -36,11 +38,12 @@ bool WebDataRequest::IsActive() { ...@@ -36,11 +38,12 @@ bool WebDataRequest::IsActive() {
WebDataRequest::WebDataRequest(WebDataRequestManager* manager, WebDataRequest::WebDataRequest(WebDataRequestManager* manager,
WebDataServiceConsumer* consumer, WebDataServiceConsumer* consumer,
WebDataServiceBase::Handle handle) WebDataServiceBase::Handle handle)
: task_runner_(base::ThreadTaskRunnerHandle::Get()), : task_runner_(base::SequencedTaskRunnerHandle::IsSet()
? base::SequencedTaskRunnerHandle::Get()
: nullptr),
atomic_manager_(reinterpret_cast<base::subtle::AtomicWord>(manager)), atomic_manager_(reinterpret_cast<base::subtle::AtomicWord>(manager)),
consumer_(consumer), consumer_(consumer),
handle_(handle) { handle_(handle) {
DCHECK(task_runner_);
DCHECK(IsActive()); DCHECK(IsActive());
static_assert(sizeof(atomic_manager_) == sizeof(manager), "size mismatch"); static_assert(sizeof(atomic_manager_) == sizeof(manager), "size mismatch");
} }
...@@ -54,7 +57,7 @@ WebDataServiceConsumer* WebDataRequest::GetConsumer() { ...@@ -54,7 +57,7 @@ WebDataServiceConsumer* WebDataRequest::GetConsumer() {
return consumer_; return consumer_;
} }
scoped_refptr<base::SingleThreadTaskRunner> WebDataRequest::GetTaskRunner() { scoped_refptr<base::SequencedTaskRunner> WebDataRequest::GetTaskRunner() {
return task_runner_; return task_runner_;
} }
...@@ -99,12 +102,17 @@ void WebDataRequestManager::CancelRequest(WebDataServiceBase::Handle h) { ...@@ -99,12 +102,17 @@ void WebDataRequestManager::CancelRequest(WebDataServiceBase::Handle h) {
void WebDataRequestManager::RequestCompleted( void WebDataRequestManager::RequestCompleted(
std::unique_ptr<WebDataRequest> request, std::unique_ptr<WebDataRequest> request,
std::unique_ptr<WDTypedResult> result) { std::unique_ptr<WDTypedResult> result) {
scoped_refptr<base::SingleThreadTaskRunner> task_runner = // Careful: Don't swap this below the BindOnce() call below, since that
// effectively does a std::move() on |request|!
scoped_refptr<base::SequencedTaskRunner> task_runner =
request->GetTaskRunner(); request->GetTaskRunner();
task_runner->PostTask( auto task =
FROM_HERE,
base::BindOnce(&WebDataRequestManager::RequestCompletedOnThread, this, base::BindOnce(&WebDataRequestManager::RequestCompletedOnThread, this,
base::Passed(&request), base::Passed(&result))); base::Passed(&request), base::Passed(&result));
if (task_runner)
task_runner->PostTask(FROM_HERE, std::move(task));
else
base::PostTask(FROM_HERE, std::move(task));
} }
WebDataRequestManager::~WebDataRequestManager() { WebDataRequestManager::~WebDataRequestManager() {
......
...@@ -15,7 +15,6 @@ ...@@ -15,7 +15,6 @@
#include "base/atomicops.h" #include "base/atomicops.h"
#include "base/macros.h" #include "base/macros.h"
#include "base/memory/ref_counted.h" #include "base/memory/ref_counted.h"
#include "base/single_thread_task_runner.h"
#include "base/synchronization/lock.h" #include "base/synchronization/lock.h"
#include "components/webdata/common/web_data_results.h" #include "components/webdata/common/web_data_results.h"
#include "components/webdata/common/web_data_service_base.h" #include "components/webdata/common/web_data_service_base.h"
...@@ -25,6 +24,10 @@ ...@@ -25,6 +24,10 @@
class WebDataServiceConsumer; class WebDataServiceConsumer;
class WebDataRequestManager; class WebDataRequestManager;
namespace base {
class SequencedTaskRunner;
}
////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////
// //
// WebData requests // WebData requests
...@@ -60,15 +63,16 @@ class WebDataRequest { ...@@ -60,15 +63,16 @@ class WebDataRequest {
// Retrieves the |consumer_| set in the constructor. // Retrieves the |consumer_| set in the constructor.
WebDataServiceConsumer* GetConsumer(); WebDataServiceConsumer* GetConsumer();
// Retrieves the original task runner of the request. // Retrieves the original task runner of the request. This may be null if the
scoped_refptr<base::SingleThreadTaskRunner> GetTaskRunner(); // original task was not posted as a sequenced task.
scoped_refptr<base::SequencedTaskRunner> GetTaskRunner();
// Marks the current request as inactive, either due to cancellation or // Marks the current request as inactive, either due to cancellation or
// completion. // completion.
void MarkAsInactive(); void MarkAsInactive();
// Tracks task runner that the request originated on. // Tracks task runner that the request originated on.
const scoped_refptr<base::SingleThreadTaskRunner> task_runner_; const scoped_refptr<base::SequencedTaskRunner> task_runner_;
// The manager associated with this request. This is stored as a raw (untyped) // The manager associated with this request. This is stored as a raw (untyped)
// pointer value because it does double duty as the flag indicating whether or // pointer value because it does double duty as the flag indicating whether or
......
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