Commit 1d33fc6f authored by Jan Wilken Dörrie's avatar Jan Wilken Dörrie Committed by Commit Bot

[ppapi] Use base::OnceCallback in RunWhileLocked and CallWhileUnlocked

This change updates ppapi::RunWhileLocked and ppapi::CallWhileUnlocked
to use base::OnceClosure and base::OnceCallback instead of base::Closure
and base::Callback.

Bug: 714018
Change-Id: I7e827ecffe2baca3a681cf3e27982263c89f9f34
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2011927
Commit-Queue: Jan Wilken Dörrie <jdoerrie@chromium.org>
Reviewed-by: default avatarRaymes Khoury <raymes@chromium.org>
Cr-Commit-Position: refs/heads/master@{#734397}
parent 7bcec019
...@@ -210,10 +210,9 @@ int32_t FileIOResource::Query(PP_FileInfo* info, ...@@ -210,10 +210,9 @@ int32_t FileIOResource::Query(PP_FileInfo* info,
// completion task to write the result. // completion task to write the result.
scoped_refptr<QueryOp> query_op(new QueryOp(file_holder_)); scoped_refptr<QueryOp> query_op(new QueryOp(file_holder_));
base::PostTaskAndReplyWithResult( base::PostTaskAndReplyWithResult(
PpapiGlobals::Get()->GetFileTaskRunner(), PpapiGlobals::Get()->GetFileTaskRunner(), FROM_HERE,
FROM_HERE, base::BindOnce(&FileIOResource::QueryOp::DoWork, query_op),
Bind(&FileIOResource::QueryOp::DoWork, query_op), RunWhileLocked(base::BindOnce(&TrackedCallback::Run, callback)));
RunWhileLocked(Bind(&TrackedCallback::Run, callback)));
callback->set_completion_task( callback->set_completion_task(
Bind(&FileIOResource::OnQueryComplete, this, query_op, info)); Bind(&FileIOResource::OnQueryComplete, this, query_op, info));
...@@ -478,10 +477,9 @@ int32_t FileIOResource::ReadValidated(int64_t offset, ...@@ -478,10 +477,9 @@ int32_t FileIOResource::ReadValidated(int64_t offset,
scoped_refptr<ReadOp> read_op( scoped_refptr<ReadOp> read_op(
new ReadOp(file_holder_, offset, bytes_to_read)); new ReadOp(file_holder_, offset, bytes_to_read));
base::PostTaskAndReplyWithResult( base::PostTaskAndReplyWithResult(
PpapiGlobals::Get()->GetFileTaskRunner(), PpapiGlobals::Get()->GetFileTaskRunner(), FROM_HERE,
FROM_HERE, base::BindOnce(&FileIOResource::ReadOp::DoWork, read_op),
Bind(&FileIOResource::ReadOp::DoWork, read_op), RunWhileLocked(base::BindOnce(&TrackedCallback::Run, callback)));
RunWhileLocked(Bind(&TrackedCallback::Run, callback)));
callback->set_completion_task( callback->set_completion_task(
Bind(&FileIOResource::OnReadComplete, this, read_op, array_output)); Bind(&FileIOResource::OnReadComplete, this, read_op, array_output));
...@@ -520,10 +518,9 @@ int32_t FileIOResource::WriteValidated( ...@@ -520,10 +518,9 @@ int32_t FileIOResource::WriteValidated(
scoped_refptr<WriteOp> write_op(new WriteOp( scoped_refptr<WriteOp> write_op(new WriteOp(
file_holder_, offset, std::move(copy), bytes_to_write, append)); file_holder_, offset, std::move(copy), bytes_to_write, append));
base::PostTaskAndReplyWithResult( base::PostTaskAndReplyWithResult(
PpapiGlobals::Get()->GetFileTaskRunner(), PpapiGlobals::Get()->GetFileTaskRunner(), FROM_HERE,
FROM_HERE, base::BindOnce(&FileIOResource::WriteOp::DoWork, write_op),
Bind(&FileIOResource::WriteOp::DoWork, write_op), RunWhileLocked(base::BindOnce(&TrackedCallback::Run, callback)));
RunWhileLocked(Bind(&TrackedCallback::Run, callback)));
callback->set_completion_task(Bind(&FileIOResource::OnWriteComplete, this)); callback->set_completion_task(Bind(&FileIOResource::OnWriteComplete, this));
return PP_OK_COMPLETIONPENDING; return PP_OK_COMPLETIONPENDING;
...@@ -612,10 +609,9 @@ void FileIOResource::OnRequestWriteQuotaComplete( ...@@ -612,10 +609,9 @@ void FileIOResource::OnRequestWriteQuotaComplete(
scoped_refptr<WriteOp> write_op(new WriteOp( scoped_refptr<WriteOp> write_op(new WriteOp(
file_holder_, offset, std::move(buffer), bytes_to_write, append)); file_holder_, offset, std::move(buffer), bytes_to_write, append));
base::PostTaskAndReplyWithResult( base::PostTaskAndReplyWithResult(
PpapiGlobals::Get()->GetFileTaskRunner(), PpapiGlobals::Get()->GetFileTaskRunner(), FROM_HERE,
FROM_HERE, base::BindOnce(&FileIOResource::WriteOp::DoWork, write_op),
Bind(&FileIOResource::WriteOp::DoWork, write_op), RunWhileLocked(base::BindOnce(&TrackedCallback::Run, callback)));
RunWhileLocked(Bind(&TrackedCallback::Run, callback)));
callback->set_completion_task(Bind(&FileIOResource::OnWriteComplete, this)); callback->set_completion_task(Bind(&FileIOResource::OnWriteComplete, this));
} }
} }
......
...@@ -109,9 +109,9 @@ bool MessageHandler::LoopIsValid() const { ...@@ -109,9 +109,9 @@ bool MessageHandler::LoopIsValid() const {
void MessageHandler::HandleMessage(ScopedPPVar var) { void MessageHandler::HandleMessage(ScopedPPVar var) {
message_loop_->task_runner()->PostTask( message_loop_->task_runner()->PostTask(
FROM_HERE, RunWhileLocked(base::Bind(&HandleMessageWrapper, FROM_HERE, RunWhileLocked(base::BindOnce(&HandleMessageWrapper,
handler_if_->HandleMessage, handler_if_->HandleMessage,
instance_, user_data_, var))); instance_, user_data_, var)));
} }
void MessageHandler::HandleBlockingMessage( void MessageHandler::HandleBlockingMessage(
...@@ -119,7 +119,7 @@ void MessageHandler::HandleBlockingMessage( ...@@ -119,7 +119,7 @@ void MessageHandler::HandleBlockingMessage(
std::unique_ptr<IPC::Message> reply_msg) { std::unique_ptr<IPC::Message> reply_msg) {
message_loop_->task_runner()->PostTask( message_loop_->task_runner()->PostTask(
FROM_HERE, FROM_HERE,
RunWhileLocked(base::Bind( RunWhileLocked(base::BindOnce(
&HandleBlockingMessageWrapper, handler_if_->HandleBlockingMessage, &HandleBlockingMessageWrapper, handler_if_->HandleBlockingMessage,
instance_, user_data_, var, base::Passed(std::move(reply_msg))))); instance_, user_data_, var, base::Passed(std::move(reply_msg)))));
} }
......
...@@ -74,7 +74,7 @@ void CallOnMainThread(int delay_in_ms, ...@@ -74,7 +74,7 @@ void CallOnMainThread(int delay_in_ms,
PpapiGlobals::Get()->GetMainThreadMessageLoop()->PostDelayedTask( PpapiGlobals::Get()->GetMainThreadMessageLoop()->PostDelayedTask(
FROM_HERE, FROM_HERE,
RunWhileLocked(base::Bind(&CallbackWrapper, callback, result)), RunWhileLocked(base::BindOnce(&CallbackWrapper, callback, result)),
base::TimeDelta::FromMilliseconds(delay_in_ms)); base::TimeDelta::FromMilliseconds(delay_in_ms));
} }
......
...@@ -284,9 +284,9 @@ void ImageDataCache::Add(ImageData* image_data) { ...@@ -284,9 +284,9 @@ void ImageDataCache::Add(ImageData* image_data) {
// Schedule a timer to invalidate this entry. // Schedule a timer to invalidate this entry.
PpapiGlobals::Get()->GetMainThreadMessageLoop()->PostDelayedTask( PpapiGlobals::Get()->GetMainThreadMessageLoop()->PostDelayedTask(
FROM_HERE, FROM_HERE,
RunWhileLocked(base::Bind(&ImageDataCache::OnTimer, RunWhileLocked(base::BindOnce(&ImageDataCache::OnTimer,
weak_factory_.GetWeakPtr(), weak_factory_.GetWeakPtr(),
image_data->pp_instance())), image_data->pp_instance())),
base::TimeDelta::FromSeconds(kMaxAgeSeconds)); base::TimeDelta::FromSeconds(kMaxAgeSeconds));
} }
......
...@@ -636,7 +636,7 @@ void PPB_Instance_Proxy::SelectionChanged(PP_Instance instance) { ...@@ -636,7 +636,7 @@ void PPB_Instance_Proxy::SelectionChanged(PP_Instance instance) {
if (!data->is_request_surrounding_text_pending) { if (!data->is_request_surrounding_text_pending) {
PpapiGlobals::Get()->GetMainThreadMessageLoop()->PostTask( PpapiGlobals::Get()->GetMainThreadMessageLoop()->PostTask(
FROM_HERE, FROM_HERE,
RunWhileLocked(base::Bind(&RequestSurroundingText, instance))); RunWhileLocked(base::BindOnce(&RequestSurroundingText, instance)));
data->is_request_surrounding_text_pending = true; data->is_request_surrounding_text_pending = true;
} }
} }
......
...@@ -95,9 +95,8 @@ int32_t MessageLoopResource::AttachToCurrentThread() { ...@@ -95,9 +95,8 @@ int32_t MessageLoopResource::AttachToCurrentThread() {
task_runner_ = base::ThreadTaskRunnerHandle::Get(); task_runner_ = base::ThreadTaskRunnerHandle::Get();
// Post all pending work to the task executor. // Post all pending work to the task executor.
for (size_t i = 0; i < pending_tasks_.size(); i++) { for (auto& info : pending_tasks_) {
const TaskInfo& info = pending_tasks_[i]; PostClosure(info.from_here, std::move(info.closure), info.delay_ms);
PostClosure(info.from_here, info.closure, info.delay_ms);
} }
pending_tasks_.clear(); pending_tasks_.clear();
...@@ -116,7 +115,7 @@ int32_t MessageLoopResource::Run() { ...@@ -116,7 +115,7 @@ int32_t MessageLoopResource::Run() {
nested_invocations_++; nested_invocations_++;
CallWhileUnlocked( CallWhileUnlocked(
base::Bind(&base::RunLoop::Run, base::Unretained(run_loop_))); base::BindOnce(&base::RunLoop::Run, base::Unretained(run_loop_)));
nested_invocations_--; nested_invocations_--;
run_loop_ = previous_run_loop; run_loop_ = previous_run_loop;
...@@ -188,17 +187,17 @@ bool MessageLoopResource::IsCurrent() const { ...@@ -188,17 +187,17 @@ bool MessageLoopResource::IsCurrent() const {
} }
void MessageLoopResource::PostClosure(const base::Location& from_here, void MessageLoopResource::PostClosure(const base::Location& from_here,
const base::Closure& closure, base::OnceClosure closure,
int64_t delay_ms) { int64_t delay_ms) {
if (task_runner_.get()) { if (task_runner_.get()) {
task_runner_->PostDelayedTask(from_here, closure, task_runner_->PostDelayedTask(from_here, std::move(closure),
base::TimeDelta::FromMilliseconds(delay_ms)); base::TimeDelta::FromMilliseconds(delay_ms));
} else { } else {
TaskInfo info; TaskInfo info;
info.from_here = FROM_HERE; info.from_here = FROM_HERE;
info.closure = closure; info.closure = std::move(closure);
info.delay_ms = delay_ms; info.delay_ms = delay_ms;
pending_tasks_.push_back(info); pending_tasks_.push_back(std::move(info));
} }
} }
......
...@@ -59,7 +59,7 @@ class PPAPI_PROXY_EXPORT MessageLoopResource : public MessageLoopShared { ...@@ -59,7 +59,7 @@ class PPAPI_PROXY_EXPORT MessageLoopResource : public MessageLoopShared {
private: private:
struct TaskInfo { struct TaskInfo {
base::Location from_here; base::Location from_here;
base::Closure closure; base::OnceClosure closure;
int64_t delay_ms; int64_t delay_ms;
}; };
...@@ -74,7 +74,7 @@ class PPAPI_PROXY_EXPORT MessageLoopResource : public MessageLoopShared { ...@@ -74,7 +74,7 @@ class PPAPI_PROXY_EXPORT MessageLoopResource : public MessageLoopShared {
// This only makes sense for user code and completely thread-safe // This only makes sense for user code and completely thread-safe
// proxy operations (e.g., MessageLoop::QuitClosure). // proxy operations (e.g., MessageLoop::QuitClosure).
void PostClosure(const base::Location& from_here, void PostClosure(const base::Location& from_here,
const base::Closure& closure, base::OnceClosure closure,
int64_t delay_ms) override; int64_t delay_ms) override;
base::SingleThreadTaskRunner* GetTaskRunner() override; base::SingleThreadTaskRunner* GetTaskRunner() override;
bool CurrentlyHandlingBlockingMessage() override; bool CurrentlyHandlingBlockingMessage() override;
......
...@@ -380,9 +380,8 @@ void PPB_Var_Deprecated_Proxy::OnMsgReleaseObject(int64_t object_id) { ...@@ -380,9 +380,8 @@ void PPB_Var_Deprecated_Proxy::OnMsgReleaseObject(int64_t object_id) {
// then remove this. // then remove this.
PpapiGlobals::Get()->GetMainThreadMessageLoop()->PostNonNestableTask( PpapiGlobals::Get()->GetMainThreadMessageLoop()->PostNonNestableTask(
FROM_HERE, FROM_HERE,
RunWhileLocked(base::Bind(&PPB_Var_Deprecated_Proxy::DoReleaseObject, RunWhileLocked(base::BindOnce(&PPB_Var_Deprecated_Proxy::DoReleaseObject,
task_factory_.GetWeakPtr(), task_factory_.GetWeakPtr(), object_id)));
object_id)));
} }
void PPB_Var_Deprecated_Proxy::OnMsgHasProperty( void PPB_Var_Deprecated_Proxy::OnMsgHasProperty(
......
...@@ -245,7 +245,7 @@ class CallbackMockResource : public Resource { ...@@ -245,7 +245,7 @@ class CallbackMockResource : public Resource {
// |thread_checker_| will bind to the background thread. // |thread_checker_| will bind to the background thread.
thread_checker_.DetachFromThread(); thread_checker_.DetachFromThread();
loop_resource->task_runner()->PostTask( loop_resource->task_runner()->PostTask(
FROM_HERE, RunWhileLocked(base::Bind( FROM_HERE, RunWhileLocked(base::BindOnce(
&CallbackMockResource::CreateCallbacks, this))); &CallbackMockResource::CreateCallbacks, this)));
} }
......
...@@ -190,8 +190,7 @@ void UDPSocketFilter::RecvQueue::DataReceivedOnIOThread( ...@@ -190,8 +190,7 @@ void UDPSocketFilter::RecvQueue::DataReceivedOnIOThread(
base::Unretained(recvfrom_addr_resource_))); base::Unretained(recvfrom_addr_resource_)));
last_recvfrom_addr_ = addr; last_recvfrom_addr_ = addr;
PpapiGlobals::Get()->GetMainThreadMessageLoop()->PostTask( PpapiGlobals::Get()->GetMainThreadMessageLoop()->PostTask(
FROM_HERE, FROM_HERE, RunWhileLocked(base::BindOnce(slot_available_callback_)));
RunWhileLocked(slot_available_callback_));
} }
read_buffer_ = NULL; read_buffer_ = NULL;
......
...@@ -181,8 +181,9 @@ void PPB_Audio_Shared::StopThread() { ...@@ -181,8 +181,9 @@ void PPB_Audio_Shared::StopThread() {
} else { } else {
if (audio_thread_.get()) { if (audio_thread_.get()) {
auto local_audio_thread(std::move(audio_thread_)); auto local_audio_thread(std::move(audio_thread_));
CallWhileUnlocked(base::Bind(&base::DelegateSimpleThread::Join, CallWhileUnlocked(
base::Unretained(local_audio_thread.get()))); base::BindOnce(&base::DelegateSimpleThread::Join,
base::Unretained(local_audio_thread.get())));
} }
} }
} }
......
...@@ -44,7 +44,7 @@ class PPAPI_SHARED_EXPORT MessageLoopShared ...@@ -44,7 +44,7 @@ class PPAPI_SHARED_EXPORT MessageLoopShared
// This only makes sense for user code and completely thread-safe // This only makes sense for user code and completely thread-safe
// proxy operations (e.g., MessageLoop::QuitClosure). // proxy operations (e.g., MessageLoop::QuitClosure).
virtual void PostClosure(const base::Location& from_here, virtual void PostClosure(const base::Location& from_here,
const base::Closure& closure, base::OnceClosure closure,
int64_t delay_ms) = 0; int64_t delay_ms) = 0;
virtual base::SingleThreadTaskRunner* GetTaskRunner() = 0; virtual base::SingleThreadTaskRunner* GetTaskRunner() = 0;
......
...@@ -89,9 +89,9 @@ ProxyLock::LockingDisablerForTest::~LockingDisablerForTest() { ...@@ -89,9 +89,9 @@ ProxyLock::LockingDisablerForTest::~LockingDisablerForTest() {
g_disable_locking_for_thread.Get().Set(false); g_disable_locking_for_thread.Get().Set(false);
} }
void CallWhileUnlocked(const base::Closure& closure) { void CallWhileUnlocked(base::OnceClosure closure) {
ProxyAutoUnlock lock; ProxyAutoUnlock lock;
closure.Run(); std::move(closure).Run();
} }
} // namespace ppapi } // namespace ppapi
...@@ -164,7 +164,7 @@ ReturnType CallWhileUnlocked(ReturnType (*function)(A1, A2, A3, A4, A5), ...@@ -164,7 +164,7 @@ ReturnType CallWhileUnlocked(ReturnType (*function)(A1, A2, A3, A4, A5),
ProxyAutoUnlock unlock; ProxyAutoUnlock unlock;
return function(p1, p2, p3, p4, p5); return function(p1, p2, p3, p4, p5);
} }
void PPAPI_SHARED_EXPORT CallWhileUnlocked(const base::Closure& closure); void PPAPI_SHARED_EXPORT CallWhileUnlocked(base::OnceClosure closure);
namespace internal { namespace internal {
...@@ -178,9 +178,9 @@ class RunWhileLockedHelper; ...@@ -178,9 +178,9 @@ class RunWhileLockedHelper;
template <> template <>
class RunWhileLockedHelper<void()> { class RunWhileLockedHelper<void()> {
public: public:
typedef base::Callback<void()> CallbackType; typedef base::OnceCallback<void()> CallbackType;
explicit RunWhileLockedHelper(const CallbackType& callback) explicit RunWhileLockedHelper(CallbackType callback)
: callback_(new CallbackType(callback)) { : callback_(std::move(callback)) {
// CallWhileLocked and destruction might happen on a different thread from // CallWhileLocked and destruction might happen on a different thread from
// creation. // creation.
thread_checker_.DetachFromThread(); thread_checker_.DetachFromThread();
...@@ -201,8 +201,8 @@ class RunWhileLockedHelper<void()> { ...@@ -201,8 +201,8 @@ class RunWhileLockedHelper<void()> {
// Use a scope and local Callback to ensure that the callback is cleared // Use a scope and local Callback to ensure that the callback is cleared
// before the lock is released, even in the unlikely event that Run() // before the lock is released, even in the unlikely event that Run()
// throws an exception. // throws an exception.
std::unique_ptr<CallbackType> temp_callback(std::move(ptr->callback_)); CallbackType temp_callback = std::move(ptr->callback_);
temp_callback->Run(); std::move(temp_callback).Run();
} }
} }
...@@ -234,13 +234,13 @@ class RunWhileLockedHelper<void()> { ...@@ -234,13 +234,13 @@ class RunWhileLockedHelper<void()> {
base::ScopedAllowCrossThreadRefCountAccess base::ScopedAllowCrossThreadRefCountAccess
allow_cross_thread_ref_count_access; allow_cross_thread_ref_count_access;
callback_.reset(); callback_.Reset();
} }
} }
private: private:
DISALLOW_COPY_AND_ASSIGN(RunWhileLockedHelper); DISALLOW_COPY_AND_ASSIGN(RunWhileLockedHelper);
std::unique_ptr<CallbackType> callback_; CallbackType callback_;
// Used to ensure that the Callback is run and deleted on the same thread. // Used to ensure that the Callback is run and deleted on the same thread.
base::ThreadChecker thread_checker_; base::ThreadChecker thread_checker_;
...@@ -249,9 +249,9 @@ class RunWhileLockedHelper<void()> { ...@@ -249,9 +249,9 @@ class RunWhileLockedHelper<void()> {
template <typename P1> template <typename P1>
class RunWhileLockedHelper<void(P1)> { class RunWhileLockedHelper<void(P1)> {
public: public:
typedef base::Callback<void(P1)> CallbackType; typedef base::OnceCallback<void(P1)> CallbackType;
explicit RunWhileLockedHelper(const CallbackType& callback) explicit RunWhileLockedHelper(CallbackType callback)
: callback_(new CallbackType(callback)) { : callback_(std::move(callback)) {
thread_checker_.DetachFromThread(); thread_checker_.DetachFromThread();
} }
static void CallWhileLocked(std::unique_ptr<RunWhileLockedHelper> ptr, static void CallWhileLocked(std::unique_ptr<RunWhileLockedHelper> ptr,
...@@ -263,8 +263,8 @@ class RunWhileLockedHelper<void(P1)> { ...@@ -263,8 +263,8 @@ class RunWhileLockedHelper<void(P1)> {
base::ScopedAllowCrossThreadRefCountAccess base::ScopedAllowCrossThreadRefCountAccess
allow_cross_thread_ref_count_access; allow_cross_thread_ref_count_access;
{ {
std::unique_ptr<CallbackType> temp_callback(std::move(ptr->callback_)); CallbackType temp_callback = std::move(ptr->callback_);
temp_callback->Run(p1); std::move(temp_callback).Run(p1);
} }
} }
~RunWhileLockedHelper() { ~RunWhileLockedHelper() {
...@@ -275,22 +275,22 @@ class RunWhileLockedHelper<void(P1)> { ...@@ -275,22 +275,22 @@ class RunWhileLockedHelper<void(P1)> {
// |lock| above protects the access to Resource instances. // |lock| above protects the access to Resource instances.
base::ScopedAllowCrossThreadRefCountAccess base::ScopedAllowCrossThreadRefCountAccess
allow_cross_thread_ref_count_access; allow_cross_thread_ref_count_access;
callback_.reset(); callback_.Reset();
} }
} }
private: private:
DISALLOW_COPY_AND_ASSIGN(RunWhileLockedHelper); DISALLOW_COPY_AND_ASSIGN(RunWhileLockedHelper);
std::unique_ptr<CallbackType> callback_; CallbackType callback_;
base::ThreadChecker thread_checker_; base::ThreadChecker thread_checker_;
}; };
template <typename P1, typename P2> template <typename P1, typename P2>
class RunWhileLockedHelper<void(P1, P2)> { class RunWhileLockedHelper<void(P1, P2)> {
public: public:
typedef base::Callback<void(P1, P2)> CallbackType; typedef base::OnceCallback<void(P1, P2)> CallbackType;
explicit RunWhileLockedHelper(const CallbackType& callback) explicit RunWhileLockedHelper(CallbackType callback)
: callback_(new CallbackType(callback)) { : callback_(std::move(callback)) {
thread_checker_.DetachFromThread(); thread_checker_.DetachFromThread();
} }
static void CallWhileLocked(std::unique_ptr<RunWhileLockedHelper> ptr, static void CallWhileLocked(std::unique_ptr<RunWhileLockedHelper> ptr,
...@@ -303,8 +303,8 @@ class RunWhileLockedHelper<void(P1, P2)> { ...@@ -303,8 +303,8 @@ class RunWhileLockedHelper<void(P1, P2)> {
base::ScopedAllowCrossThreadRefCountAccess base::ScopedAllowCrossThreadRefCountAccess
allow_cross_thread_ref_count_access; allow_cross_thread_ref_count_access;
{ {
std::unique_ptr<CallbackType> temp_callback(std::move(ptr->callback_)); CallbackType temp_callback = std::move(ptr->callback_);
temp_callback->Run(p1, p2); std::move(temp_callback).Run(p1, p2);
} }
} }
~RunWhileLockedHelper() { ~RunWhileLockedHelper() {
...@@ -315,22 +315,22 @@ class RunWhileLockedHelper<void(P1, P2)> { ...@@ -315,22 +315,22 @@ class RunWhileLockedHelper<void(P1, P2)> {
// |lock| above protects the access to Resource instances. // |lock| above protects the access to Resource instances.
base::ScopedAllowCrossThreadRefCountAccess base::ScopedAllowCrossThreadRefCountAccess
allow_cross_thread_ref_count_access; allow_cross_thread_ref_count_access;
callback_.reset(); callback_.Reset();
} }
} }
private: private:
DISALLOW_COPY_AND_ASSIGN(RunWhileLockedHelper); DISALLOW_COPY_AND_ASSIGN(RunWhileLockedHelper);
std::unique_ptr<CallbackType> callback_; CallbackType callback_;
base::ThreadChecker thread_checker_; base::ThreadChecker thread_checker_;
}; };
template <typename P1, typename P2, typename P3> template <typename P1, typename P2, typename P3>
class RunWhileLockedHelper<void(P1, P2, P3)> { class RunWhileLockedHelper<void(P1, P2, P3)> {
public: public:
typedef base::Callback<void(P1, P2, P3)> CallbackType; typedef base::OnceCallback<void(P1, P2, P3)> CallbackType;
explicit RunWhileLockedHelper(const CallbackType& callback) explicit RunWhileLockedHelper(CallbackType callback)
: callback_(new CallbackType(callback)) { : callback_(std::move(callback)) {
thread_checker_.DetachFromThread(); thread_checker_.DetachFromThread();
} }
static void CallWhileLocked(std::unique_ptr<RunWhileLockedHelper> ptr, static void CallWhileLocked(std::unique_ptr<RunWhileLockedHelper> ptr,
...@@ -344,8 +344,8 @@ class RunWhileLockedHelper<void(P1, P2, P3)> { ...@@ -344,8 +344,8 @@ class RunWhileLockedHelper<void(P1, P2, P3)> {
base::ScopedAllowCrossThreadRefCountAccess base::ScopedAllowCrossThreadRefCountAccess
allow_cross_thread_ref_count_access; allow_cross_thread_ref_count_access;
{ {
std::unique_ptr<CallbackType> temp_callback(std::move(ptr->callback_)); CallbackType temp_callback = std::move(ptr->callback_);
temp_callback->Run(p1, p2, p3); std::move(temp_callback).Run(p1, p2, p3);
} }
} }
~RunWhileLockedHelper() { ~RunWhileLockedHelper() {
...@@ -356,13 +356,13 @@ class RunWhileLockedHelper<void(P1, P2, P3)> { ...@@ -356,13 +356,13 @@ class RunWhileLockedHelper<void(P1, P2, P3)> {
// |lock| above protects the access to Resource instances. // |lock| above protects the access to Resource instances.
base::ScopedAllowCrossThreadRefCountAccess base::ScopedAllowCrossThreadRefCountAccess
allow_cross_thread_ref_count_access; allow_cross_thread_ref_count_access;
callback_.reset(); callback_.Reset();
} }
} }
private: private:
DISALLOW_COPY_AND_ASSIGN(RunWhileLockedHelper); DISALLOW_COPY_AND_ASSIGN(RunWhileLockedHelper);
std::unique_ptr<CallbackType> callback_; CallbackType callback_;
base::ThreadChecker thread_checker_; base::ThreadChecker thread_checker_;
}; };
...@@ -381,9 +381,10 @@ class RunWhileLockedHelper<void(P1, P2, P3)> { ...@@ -381,9 +381,10 @@ class RunWhileLockedHelper<void(P1, P2, P3)> {
// want to ensure that the ProxyLock is acquired for the duration of the task // want to ensure that the ProxyLock is acquired for the duration of the task
// that the Callback runs. // that the Callback runs.
// EXAMPLE USAGE: // EXAMPLE USAGE:
// GetMainThreadMessageLoop()->PostDelayedTask( // GetMainThreadMessageLoop()->PostDelayedTask(
// FROM_HERE, // FROM_HERE,
// RunWhileLocked(base::Bind(&CallbackWrapper, callback, result)), // RunWhileLocked(
// base::BindOnce(&CallbackWrapper, std::move(callback), result)),
// delay_in_ms); // delay_in_ms);
// //
// In normal usage like the above, this all should "just work". However, if you // In normal usage like the above, this all should "just work". However, if you
...@@ -397,14 +398,15 @@ class RunWhileLockedHelper<void(P1, P2, P3)> { ...@@ -397,14 +398,15 @@ class RunWhileLockedHelper<void(P1, P2, P3)> {
// was run (but can be destroyed with or without the proxy lock acquired). Or // was run (but can be destroyed with or without the proxy lock acquired). Or
// (3) destroyed without the proxy lock acquired. // (3) destroyed without the proxy lock acquired.
template <class FunctionType> template <class FunctionType>
inline base::Callback<FunctionType> RunWhileLocked( inline base::OnceCallback<FunctionType> RunWhileLocked(
const base::Callback<FunctionType>& callback) { base::OnceCallback<FunctionType> callback) {
// NOTE: the reason we use "scoped_ptr" here instead of letting the callback // NOTE: the reason we use "scoped_ptr" here instead of letting the callback
// own it via base::Owned is kind of subtle. Imagine for the moment that we // own it via base::Owned is kind of subtle. Imagine for the moment that we
// call RunWhileLocked without the ProxyLock: // call RunWhileLocked without the ProxyLock:
// { // {
// base::Callback<void ()> local_callback = base::Bind(&Foo); // base::OnceCallback<void ()> local_callback = base::BinOnced(&Foo);
// some_task_runner.PostTask(FROM_HERE, RunWhileLocked(local_callback)); // some_task_runner.PostTask(FROM_HERE,
// RunWhileLocked(std::move(local_callback)));
// } // }
// In this case, since we don't have a lock synchronizing us, it's possible // In this case, since we don't have a lock synchronizing us, it's possible
// for the callback to run on the other thread before we return and destroy // for the callback to run on the other thread before we return and destroy
...@@ -415,8 +417,8 @@ inline base::Callback<FunctionType> RunWhileLocked( ...@@ -415,8 +417,8 @@ inline base::Callback<FunctionType> RunWhileLocked(
// on this thread, which will violate the RunWhileLockedHelper's assumption // on this thread, which will violate the RunWhileLockedHelper's assumption
// that it is destroyed on the same thread where it is run. // that it is destroyed on the same thread where it is run.
std::unique_ptr<internal::RunWhileLockedHelper<FunctionType>> helper( std::unique_ptr<internal::RunWhileLockedHelper<FunctionType>> helper(
new internal::RunWhileLockedHelper<FunctionType>(callback)); new internal::RunWhileLockedHelper<FunctionType>(std::move(callback)));
return base::Bind( return base::BindOnce(
&internal::RunWhileLockedHelper<FunctionType>::CallWhileLocked, &internal::RunWhileLockedHelper<FunctionType>::CallWhileLocked,
base::Passed(std::move(helper))); base::Passed(std::move(helper)));
} }
......
...@@ -76,20 +76,20 @@ TEST_F(PpapiProxyLockTest, Locking) { ...@@ -76,20 +76,20 @@ TEST_F(PpapiProxyLockTest, Locking) {
TestGlobals globals; TestGlobals globals;
expect_to_be_locked = true; expect_to_be_locked = true;
base::Callback<void()> cb0; base::OnceCallback<void()> cb0;
{ {
ProxyAutoLock lock; ProxyAutoLock lock;
cb0 = RunWhileLocked(base::Bind(TestCallback_0)); cb0 = RunWhileLocked(base::BindOnce(TestCallback_0));
} }
cb0.Run(); std::move(cb0).Run();
ASSERT_EQ(1, called_num); ASSERT_EQ(1, called_num);
called_num = 0; called_num = 0;
{ {
ProxyAutoLock lock; ProxyAutoLock lock;
cb0 = RunWhileLocked(base::Bind(TestCallback_1, 123)); cb0 = RunWhileLocked(base::BindOnce(TestCallback_1, 123));
} }
cb0.Run(); std::move(cb0).Run();
ASSERT_EQ(1, called_num); ASSERT_EQ(1, called_num);
called_num = 0; called_num = 0;
...@@ -97,55 +97,56 @@ TEST_F(PpapiProxyLockTest, Locking) { ...@@ -97,55 +97,56 @@ TEST_F(PpapiProxyLockTest, Locking) {
ProxyAutoLock lock; ProxyAutoLock lock;
scoped_refptr<CheckLockStateInDestructor> object = scoped_refptr<CheckLockStateInDestructor> object =
new CheckLockStateInDestructor(); new CheckLockStateInDestructor();
cb0 = cb0 = RunWhileLocked(
RunWhileLocked(base::Bind(&CheckLockStateInDestructor::Method, object)); base::BindOnce(&CheckLockStateInDestructor::Method, object));
// Note after this scope, the Callback owns the only reference. // Note after this scope, the Callback owns the only reference.
} }
cb0.Run(); std::move(cb0).Run();
ASSERT_EQ(1, called_num); ASSERT_EQ(1, called_num);
called_num = 0; called_num = 0;
base::Callback<void(int)> cb1; base::OnceCallback<void(int)> cb1;
{ {
ProxyAutoLock lock; ProxyAutoLock lock;
cb1 = RunWhileLocked(base::Bind(TestCallback_1)); cb1 = RunWhileLocked(base::BindOnce(TestCallback_1));
} }
cb1.Run(123); std::move(cb1).Run(123);
ASSERT_EQ(1, called_num); ASSERT_EQ(1, called_num);
called_num = 0; called_num = 0;
base::Callback<void(int, const std::string&)> cb2; base::OnceCallback<void(int, const std::string&)> cb2;
{ {
ProxyAutoLock lock; ProxyAutoLock lock;
cb2 = RunWhileLocked(base::Bind(TestCallback_2)); cb2 = RunWhileLocked(base::BindOnce(TestCallback_2));
} }
cb2.Run(123, std::string("yo")); std::move(cb2).Run(123, std::string("yo"));
ASSERT_EQ(1, called_num); ASSERT_EQ(1, called_num);
called_num = 0; called_num = 0;
base::Callback<void(int, const std::string&, Param)> cb3; base::OnceCallback<void(int, const std::string&, Param)> cb3;
{ {
ProxyAutoLock lock; ProxyAutoLock lock;
cb3 = RunWhileLocked(base::Bind(TestCallback_3)); cb3 = RunWhileLocked(base::BindOnce(TestCallback_3));
} }
cb3.Run(123, std::string("yo"), Param()); std::move(cb3).Run(123, std::string("yo"), Param());
ASSERT_EQ(1, called_num); ASSERT_EQ(1, called_num);
called_num = 0; called_num = 0;
base::Callback<void(const std::string&)> cb1_string; base::OnceCallback<void(const std::string&)> cb1_string;
{ {
ProxyAutoLock lock; ProxyAutoLock lock;
cb1_string = RunWhileLocked(base::Bind(TestCallback_2, 123)); cb1_string = RunWhileLocked(base::BindOnce(TestCallback_2, 123));
} }
cb1_string.Run(std::string("yo")); std::move(cb1_string).Run(std::string("yo"));
ASSERT_EQ(1, called_num); ASSERT_EQ(1, called_num);
called_num = 0; called_num = 0;
{ {
ProxyAutoLock lock; ProxyAutoLock lock;
cb0 = RunWhileLocked(base::Bind(TestCallback_2, 123, std::string("yo"))); cb0 =
RunWhileLocked(base::BindOnce(TestCallback_2, 123, std::string("yo")));
} }
cb0.Run(); std::move(cb0).Run();
ASSERT_EQ(1, called_num); ASSERT_EQ(1, called_num);
called_num = 0; called_num = 0;
} }
...@@ -168,16 +169,13 @@ TEST_F(PpapiProxyLockTest, Unlocking) { ...@@ -168,16 +169,13 @@ TEST_F(PpapiProxyLockTest, Unlocking) {
called_num = 0; called_num = 0;
} }
{ {
// TODO(dmichael): Make const-ref arguments work properly with type CallWhileUnlocked(TestCallback_2, 123, std::string("yo"));
// deduction.
CallWhileUnlocked<void, int, const std::string&>(
TestCallback_2, 123, std::string("yo"));
ASSERT_EQ(1, called_num); ASSERT_EQ(1, called_num);
called_num = 0; called_num = 0;
} }
{ {
base::Callback<void()> callback(base::Bind(TestCallback_0)); base::OnceCallback<void()> callback(base::BindOnce(TestCallback_0));
CallWhileUnlocked(callback); CallWhileUnlocked(std::move(callback));
ASSERT_EQ(1, called_num); ASSERT_EQ(1, called_num);
called_num = 0; called_num = 0;
} }
......
...@@ -18,9 +18,9 @@ class ThreadAwareCallbackBase::Core : public base::RefCountedThreadSafe<Core> { ...@@ -18,9 +18,9 @@ class ThreadAwareCallbackBase::Core : public base::RefCountedThreadSafe<Core> {
void MarkAsAborted() { aborted_ = true; } void MarkAsAborted() { aborted_ = true; }
void RunIfNotAborted(const base::Closure& closure) { void RunIfNotAborted(base::OnceClosure closure) {
if (!aborted_) if (!aborted_)
CallWhileUnlocked(closure); CallWhileUnlocked(std::move(closure));
} }
private: private:
...@@ -44,14 +44,15 @@ bool ThreadAwareCallbackBase::HasTargetLoop() { ...@@ -44,14 +44,15 @@ bool ThreadAwareCallbackBase::HasTargetLoop() {
} }
void ThreadAwareCallbackBase::InternalRunOnTargetThread( void ThreadAwareCallbackBase::InternalRunOnTargetThread(
const base::Closure& closure) { base::OnceClosure closure) {
if (target_loop_.get() != PpapiGlobals::Get()->GetCurrentMessageLoop()) { if (target_loop_.get() != PpapiGlobals::Get()->GetCurrentMessageLoop()) {
target_loop_->PostClosure( target_loop_->PostClosure(
FROM_HERE, FROM_HERE,
RunWhileLocked(base::Bind(&Core::RunIfNotAborted, core_, closure)), RunWhileLocked(
base::BindOnce(&Core::RunIfNotAborted, core_, std::move(closure))),
0); 0);
} else { } else {
CallWhileUnlocked(closure); CallWhileUnlocked(std::move(closure));
} }
} }
......
...@@ -24,7 +24,7 @@ class PPAPI_SHARED_EXPORT ThreadAwareCallbackBase { ...@@ -24,7 +24,7 @@ class PPAPI_SHARED_EXPORT ThreadAwareCallbackBase {
static bool HasTargetLoop(); static bool HasTargetLoop();
void InternalRunOnTargetThread(const base::Closure& closure); void InternalRunOnTargetThread(base::OnceClosure closure);
private: private:
class Core; class Core;
......
...@@ -256,17 +256,17 @@ void TrackedCallback::PostRunWithLock(int32_t result) { ...@@ -256,17 +256,17 @@ void TrackedCallback::PostRunWithLock(int32_t result) {
// directly. // directly.
SignalBlockingCallback(result); SignalBlockingCallback(result);
} else { } else {
base::Closure callback_closure( base::OnceClosure callback_closure(
RunWhileLocked(base::Bind(&TrackedCallback::Run, this, result))); RunWhileLocked(base::BindOnce(&TrackedCallback::Run, this, result)));
if (target_loop_) { if (target_loop_) {
target_loop_->PostClosure(FROM_HERE, callback_closure, 0); target_loop_->PostClosure(FROM_HERE, std::move(callback_closure), 0);
} else { } else {
// We must be running in-process and on the main thread (the Enter // We must be running in-process and on the main thread (the Enter
// classes protect against having a null target_loop_ otherwise). // classes protect against having a null target_loop_ otherwise).
DCHECK(IsMainThread()); DCHECK(IsMainThread());
DCHECK(PpapiGlobals::Get()->IsHostGlobals()); DCHECK(PpapiGlobals::Get()->IsHostGlobals());
base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, base::ThreadTaskRunnerHandle::Get()->PostTask(
callback_closure); FROM_HERE, std::move(callback_closure));
} }
} }
is_scheduled_ = true; is_scheduled_ = true;
......
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