Add base::OnceClosure support in cef_closure_task.h (see issue #3140)
This commit is contained in:
parent
3545e20b01
commit
cc2759d07e
|
@ -42,14 +42,16 @@
|
|||
#include "include/cef_task.h"
|
||||
|
||||
///
|
||||
// Helpers for asynchronously executing a base::Closure (bound function or
|
||||
// method) on a CEF thread. Creation of base::Closures can be facilitated using
|
||||
// base::Bind. See include/base/cef_callback.h for complete usage instructions.
|
||||
// Helpers for asynchronously executing a base::[Once|Repeating]Closure (bound
|
||||
// function or method) on a CEF thread. Creation of a
|
||||
// base::[Once|Repeating]Closure can be facilitated using
|
||||
// base::Bind[Once|Repeating]. See include/base/cef_callback.h for complete
|
||||
// usage instructions.
|
||||
//
|
||||
// TO use these helpers you should include this header and the header that
|
||||
// defines base::Bind.
|
||||
// To use these helpers you should include this header and the header that
|
||||
// defines base::Bind[Once|Repeating].
|
||||
//
|
||||
// #include "include/base/cef_bind.h"
|
||||
// #include "include/base/cef_callback.h"
|
||||
// #include "include/wrapper/cef_closure_task.h"
|
||||
//
|
||||
// Example of executing a bound function:
|
||||
|
@ -59,7 +61,7 @@
|
|||
//
|
||||
// // Post a task that will execute MyFunc on the UI thread and pass an |arg|
|
||||
// // value of 5.
|
||||
// CefPostTask(TID_UI, base::Bind(&MyFunc, 5));
|
||||
// CefPostTask(TID_UI, base::BindOnce(&MyFunc, 5));
|
||||
//
|
||||
// Example of executing a bound method:
|
||||
//
|
||||
|
@ -78,25 +80,31 @@
|
|||
// // Post a task that will execute MyClass::MyMethod on the UI thread and pass
|
||||
// // an |arg| value of 5. |instance| will be kept alive until after the task
|
||||
// // completes.
|
||||
// CefPostTask(TID_UI, base::Bind(&MyClass::MyMethod, instance, 5));
|
||||
// CefPostTask(TID_UI, base::BindOnce(&MyClass::MyMethod, instance, 5));
|
||||
///
|
||||
|
||||
///
|
||||
// Create a CefTask that wraps a base::Closure. Can be used in combination with
|
||||
// CefTaskRunner.
|
||||
// Create a CefTask that wraps a base::[Once|Repeating]Closure. Can be used in
|
||||
// combination with CefTaskRunner.
|
||||
///
|
||||
CefRefPtr<CefTask> CefCreateClosureTask(const base::Closure& closure);
|
||||
CefRefPtr<CefTask> CefCreateClosureTask(base::OnceClosure closure);
|
||||
CefRefPtr<CefTask> CefCreateClosureTask(const base::RepeatingClosure& closure);
|
||||
|
||||
///
|
||||
// Post a Closure for execution on the specified thread.
|
||||
// Post a base::[Once|Repeating]Closure for execution on the specified thread.
|
||||
///
|
||||
bool CefPostTask(CefThreadId threadId, const base::Closure& closure);
|
||||
bool CefPostTask(CefThreadId threadId, base::OnceClosure closure);
|
||||
bool CefPostTask(CefThreadId threadId, const base::RepeatingClosure& closure);
|
||||
|
||||
///
|
||||
// Post a Closure for delayed execution on the specified thread.
|
||||
// Post a base::[Once|Repeating]Closure for delayed execution on the specified
|
||||
// thread.
|
||||
///
|
||||
bool CefPostDelayedTask(CefThreadId threadId,
|
||||
const base::Closure& closure,
|
||||
base::OnceClosure closure,
|
||||
int64 delay_ms);
|
||||
bool CefPostDelayedTask(CefThreadId threadId,
|
||||
const base::RepeatingClosure& closure,
|
||||
int64 delay_ms);
|
||||
|
||||
#endif // CEF_INCLUDE_WRAPPER_CEF_CLOSURE_TASK_H_
|
||||
|
|
|
@ -3,39 +3,74 @@
|
|||
// can be found in the LICENSE file.
|
||||
|
||||
#include "include/wrapper/cef_closure_task.h"
|
||||
|
||||
#include <memory>
|
||||
|
||||
#include "include/base/cef_callback.h"
|
||||
|
||||
namespace {
|
||||
|
||||
class CefClosureTask : public CefTask {
|
||||
class CefOnceClosureTask : public CefTask {
|
||||
public:
|
||||
explicit CefClosureTask(const base::Closure& closure) : closure_(closure) {}
|
||||
explicit CefOnceClosureTask(base::OnceClosure closure)
|
||||
: closure_(std::move(closure)) {}
|
||||
|
||||
// CefTask method
|
||||
virtual void Execute() override {
|
||||
void Execute() override { std::move(closure_).Run(); }
|
||||
|
||||
private:
|
||||
base::OnceClosure closure_;
|
||||
|
||||
IMPLEMENT_REFCOUNTING(CefOnceClosureTask);
|
||||
DISALLOW_COPY_AND_ASSIGN(CefOnceClosureTask);
|
||||
};
|
||||
|
||||
class CefRepeatingClosureTask : public CefTask {
|
||||
public:
|
||||
explicit CefRepeatingClosureTask(const base::RepeatingClosure& closure)
|
||||
: closure_(closure) {}
|
||||
|
||||
// CefTask method
|
||||
void Execute() override {
|
||||
closure_.Run();
|
||||
closure_.Reset();
|
||||
}
|
||||
|
||||
private:
|
||||
base::Closure closure_;
|
||||
base::RepeatingClosure closure_;
|
||||
|
||||
IMPLEMENT_REFCOUNTING(CefClosureTask);
|
||||
DISALLOW_COPY_AND_ASSIGN(CefClosureTask);
|
||||
IMPLEMENT_REFCOUNTING(CefRepeatingClosureTask);
|
||||
DISALLOW_COPY_AND_ASSIGN(CefRepeatingClosureTask);
|
||||
};
|
||||
|
||||
} // namespace
|
||||
|
||||
CefRefPtr<CefTask> CefCreateClosureTask(const base::Closure& closure) {
|
||||
return new CefClosureTask(closure);
|
||||
CefRefPtr<CefTask> CefCreateClosureTask(base::OnceClosure closure) {
|
||||
return new CefOnceClosureTask(std::move(closure));
|
||||
}
|
||||
|
||||
bool CefPostTask(CefThreadId threadId, const base::Closure& closure) {
|
||||
return CefPostTask(threadId, new CefClosureTask(closure));
|
||||
CefRefPtr<CefTask> CefCreateClosureTask(const base::RepeatingClosure& closure) {
|
||||
return new CefRepeatingClosureTask(closure);
|
||||
}
|
||||
|
||||
bool CefPostTask(CefThreadId threadId, base::OnceClosure closure) {
|
||||
return CefPostTask(threadId, new CefOnceClosureTask(std::move(closure)));
|
||||
}
|
||||
|
||||
bool CefPostTask(CefThreadId threadId, const base::RepeatingClosure& closure) {
|
||||
return CefPostTask(threadId, new CefRepeatingClosureTask(closure));
|
||||
}
|
||||
|
||||
bool CefPostDelayedTask(CefThreadId threadId,
|
||||
const base::Closure& closure,
|
||||
base::OnceClosure closure,
|
||||
int64 delay_ms) {
|
||||
return CefPostDelayedTask(threadId, new CefClosureTask(closure), delay_ms);
|
||||
return CefPostDelayedTask(
|
||||
threadId, new CefOnceClosureTask(std::move(closure)), delay_ms);
|
||||
}
|
||||
|
||||
bool CefPostDelayedTask(CefThreadId threadId,
|
||||
const base::RepeatingClosure& closure,
|
||||
int64 delay_ms) {
|
||||
return CefPostDelayedTask(threadId, new CefRepeatingClosureTask(closure),
|
||||
delay_ms);
|
||||
}
|
||||
|
|
|
@ -108,7 +108,7 @@ void PostTaskEvent1(bool* ran_test,
|
|||
event->Signal();
|
||||
}
|
||||
|
||||
void PostTask1(bool* ran_test, CefRefPtr<CefWaitableEvent> event) {
|
||||
void PostOnceTask1(bool* ran_test, CefRefPtr<CefWaitableEvent> event) {
|
||||
// Currently on the FILE thread.
|
||||
CefRefPtr<CefTaskRunner> runner = CefTaskRunner::GetForThread(TID_IO);
|
||||
EXPECT_TRUE(runner.get());
|
||||
|
@ -116,17 +116,41 @@ void PostTask1(bool* ran_test, CefRefPtr<CefWaitableEvent> event) {
|
|||
EXPECT_TRUE(runner->BelongsToThread(TID_IO));
|
||||
|
||||
runner->PostTask(CefCreateClosureTask(
|
||||
base::Bind(&PostTaskEvent1, ran_test, event, runner)));
|
||||
base::BindOnce(&PostTaskEvent1, ran_test, event, runner)));
|
||||
}
|
||||
|
||||
void PostDelayedTask1(bool* ran_test, CefRefPtr<CefWaitableEvent> event) {
|
||||
void PostRepeatingTask1(bool* ran_test, CefRefPtr<CefWaitableEvent> event) {
|
||||
// Currently on the FILE thread.
|
||||
CefRefPtr<CefTaskRunner> runner = CefTaskRunner::GetForThread(TID_IO);
|
||||
EXPECT_TRUE(runner.get());
|
||||
EXPECT_FALSE(runner->BelongsToCurrentThread());
|
||||
EXPECT_TRUE(runner->BelongsToThread(TID_IO));
|
||||
|
||||
runner->PostDelayedTask(CefCreateClosureTask(base::Bind(
|
||||
runner->PostTask(CefCreateClosureTask(
|
||||
base::BindRepeating(&PostTaskEvent1, ran_test, event, runner)));
|
||||
}
|
||||
|
||||
void PostOnceDelayedTask1(bool* ran_test, CefRefPtr<CefWaitableEvent> event) {
|
||||
// Currently on the FILE thread.
|
||||
CefRefPtr<CefTaskRunner> runner = CefTaskRunner::GetForThread(TID_IO);
|
||||
EXPECT_TRUE(runner.get());
|
||||
EXPECT_FALSE(runner->BelongsToCurrentThread());
|
||||
EXPECT_TRUE(runner->BelongsToThread(TID_IO));
|
||||
|
||||
runner->PostDelayedTask(CefCreateClosureTask(base::BindOnce(
|
||||
&PostTaskEvent1, ran_test, event, runner)),
|
||||
0);
|
||||
}
|
||||
|
||||
void PostRepeatingDelayedTask1(bool* ran_test,
|
||||
CefRefPtr<CefWaitableEvent> event) {
|
||||
// Currently on the FILE thread.
|
||||
CefRefPtr<CefTaskRunner> runner = CefTaskRunner::GetForThread(TID_IO);
|
||||
EXPECT_TRUE(runner.get());
|
||||
EXPECT_FALSE(runner->BelongsToCurrentThread());
|
||||
EXPECT_TRUE(runner->BelongsToThread(TID_IO));
|
||||
|
||||
runner->PostDelayedTask(CefCreateClosureTask(base::BindRepeating(
|
||||
&PostTaskEvent1, ran_test, event, runner)),
|
||||
0);
|
||||
}
|
||||
|
@ -139,82 +163,169 @@ void PostTaskEvent2(bool* ran_test, CefRefPtr<CefWaitableEvent> event) {
|
|||
event->Signal();
|
||||
}
|
||||
|
||||
void PostTask2(bool* ran_test, CefRefPtr<CefWaitableEvent> event) {
|
||||
void PostOnceTask2(bool* ran_test, CefRefPtr<CefWaitableEvent> event) {
|
||||
// Currently on the FILE thread.
|
||||
EXPECT_FALSE(CefCurrentlyOn(TID_IO));
|
||||
|
||||
CefPostTask(TID_IO, CefCreateClosureTask(
|
||||
base::Bind(&PostTaskEvent2, ran_test, event)));
|
||||
base::BindOnce(&PostTaskEvent2, ran_test, event)));
|
||||
}
|
||||
|
||||
void PostDelayedTask2(bool* ran_test, CefRefPtr<CefWaitableEvent> event) {
|
||||
void PostRepeatingTask2(bool* ran_test, CefRefPtr<CefWaitableEvent> event) {
|
||||
// Currently on the FILE thread.
|
||||
EXPECT_FALSE(CefCurrentlyOn(TID_IO));
|
||||
|
||||
CefPostTask(TID_IO, CefCreateClosureTask(base::BindRepeating(
|
||||
&PostTaskEvent2, ran_test, event)));
|
||||
}
|
||||
|
||||
void PostOnceDelayedTask2(bool* ran_test, CefRefPtr<CefWaitableEvent> event) {
|
||||
// Currently on the FILE thread.
|
||||
EXPECT_FALSE(CefCurrentlyOn(TID_IO));
|
||||
|
||||
CefPostDelayedTask(
|
||||
TID_IO,
|
||||
CefCreateClosureTask(base::Bind(&PostTaskEvent2, ran_test, event)), 0);
|
||||
CefCreateClosureTask(base::BindOnce(&PostTaskEvent2, ran_test, event)),
|
||||
0);
|
||||
}
|
||||
|
||||
void PostRepeatingDelayedTask2(bool* ran_test,
|
||||
CefRefPtr<CefWaitableEvent> event) {
|
||||
// Currently on the FILE thread.
|
||||
EXPECT_FALSE(CefCurrentlyOn(TID_IO));
|
||||
|
||||
CefPostDelayedTask(TID_IO,
|
||||
CefCreateClosureTask(
|
||||
base::BindRepeating(&PostTaskEvent2, ran_test, event)),
|
||||
0);
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
TEST(TaskTest, GetForCurrentThread) {
|
||||
TEST(TaskTest, GetOnceForCurrentThread) {
|
||||
bool ran_test = false;
|
||||
CefRefPtr<CefWaitableEvent> event =
|
||||
CefWaitableEvent::CreateWaitableEvent(true, false);
|
||||
CefPostTask(
|
||||
TID_FILE_USER_VISIBLE,
|
||||
CefCreateClosureTask(base::Bind(&GetForCurrentThread, &ran_test, event)));
|
||||
CefPostTask(TID_FILE_USER_VISIBLE,
|
||||
CefCreateClosureTask(
|
||||
base::BindOnce(&GetForCurrentThread, &ran_test, event)));
|
||||
WaitForEvent(event);
|
||||
EXPECT_TRUE(ran_test);
|
||||
}
|
||||
|
||||
TEST(TaskTest, GetForThread) {
|
||||
TEST(TaskTest, GetRepeatingForCurrentThread) {
|
||||
bool ran_test = false;
|
||||
CefRefPtr<CefWaitableEvent> event =
|
||||
CefWaitableEvent::CreateWaitableEvent(true, false);
|
||||
CefPostTask(TID_FILE_USER_VISIBLE, CefCreateClosureTask(base::Bind(
|
||||
CefPostTask(TID_FILE_USER_VISIBLE,
|
||||
CefCreateClosureTask(
|
||||
base::BindRepeating(&GetForCurrentThread, &ran_test, event)));
|
||||
WaitForEvent(event);
|
||||
EXPECT_TRUE(ran_test);
|
||||
}
|
||||
|
||||
TEST(TaskTest, GetOnceForThread) {
|
||||
bool ran_test = false;
|
||||
CefRefPtr<CefWaitableEvent> event =
|
||||
CefWaitableEvent::CreateWaitableEvent(true, false);
|
||||
CefPostTask(TID_FILE_USER_VISIBLE, CefCreateClosureTask(base::BindOnce(
|
||||
&GetForThread, &ran_test, event)));
|
||||
WaitForEvent(event);
|
||||
EXPECT_TRUE(ran_test);
|
||||
}
|
||||
|
||||
TEST(TaskTest, PostTask1) {
|
||||
TEST(TaskTest, GetRepeatingForThread) {
|
||||
bool ran_test = false;
|
||||
CefRefPtr<CefWaitableEvent> event =
|
||||
CefWaitableEvent::CreateWaitableEvent(true, false);
|
||||
CefPostTask(TID_FILE_USER_VISIBLE, CefCreateClosureTask(base::BindRepeating(
|
||||
&GetForThread, &ran_test, event)));
|
||||
WaitForEvent(event);
|
||||
EXPECT_TRUE(ran_test);
|
||||
}
|
||||
|
||||
TEST(TaskTest, PostOnceTask1) {
|
||||
bool ran_test = false;
|
||||
CefRefPtr<CefWaitableEvent> event =
|
||||
CefWaitableEvent::CreateWaitableEvent(true, false);
|
||||
CefPostTask(TID_FILE_USER_VISIBLE, CefCreateClosureTask(base::BindOnce(
|
||||
&PostOnceTask1, &ran_test, event)));
|
||||
WaitForEvent(event);
|
||||
EXPECT_TRUE(ran_test);
|
||||
}
|
||||
|
||||
TEST(TaskTest, PostRepeatingTask1) {
|
||||
bool ran_test = false;
|
||||
CefRefPtr<CefWaitableEvent> event =
|
||||
CefWaitableEvent::CreateWaitableEvent(true, false);
|
||||
CefPostTask(TID_FILE_USER_VISIBLE,
|
||||
CefCreateClosureTask(base::Bind(&PostTask1, &ran_test, event)));
|
||||
CefCreateClosureTask(
|
||||
base::BindRepeating(&PostRepeatingTask1, &ran_test, event)));
|
||||
WaitForEvent(event);
|
||||
EXPECT_TRUE(ran_test);
|
||||
}
|
||||
|
||||
TEST(TaskTest, PostDelayedTask1) {
|
||||
bool ran_test = false;
|
||||
CefRefPtr<CefWaitableEvent> event =
|
||||
CefWaitableEvent::CreateWaitableEvent(true, false);
|
||||
CefPostTask(TID_FILE_USER_VISIBLE, CefCreateClosureTask(base::Bind(
|
||||
&PostDelayedTask1, &ran_test, event)));
|
||||
WaitForEvent(event);
|
||||
EXPECT_TRUE(ran_test);
|
||||
}
|
||||
|
||||
TEST(TaskTest, PostTask2) {
|
||||
TEST(TaskTest, PostOnceDelayedTask1) {
|
||||
bool ran_test = false;
|
||||
CefRefPtr<CefWaitableEvent> event =
|
||||
CefWaitableEvent::CreateWaitableEvent(true, false);
|
||||
CefPostTask(TID_FILE_USER_VISIBLE,
|
||||
CefCreateClosureTask(base::Bind(&PostTask2, &ran_test, event)));
|
||||
CefCreateClosureTask(
|
||||
base::BindOnce(&PostOnceDelayedTask1, &ran_test, event)));
|
||||
WaitForEvent(event);
|
||||
EXPECT_TRUE(ran_test);
|
||||
}
|
||||
|
||||
TEST(TaskTest, PostDelayedTask2) {
|
||||
TEST(TaskTest, PostRepeatingDelayedTask1) {
|
||||
bool ran_test = false;
|
||||
CefRefPtr<CefWaitableEvent> event =
|
||||
CefWaitableEvent::CreateWaitableEvent(true, false);
|
||||
CefPostTask(TID_FILE_USER_VISIBLE, CefCreateClosureTask(base::Bind(
|
||||
&PostDelayedTask2, &ran_test, event)));
|
||||
CefPostTask(TID_FILE_USER_VISIBLE,
|
||||
CefCreateClosureTask(base::BindRepeating(
|
||||
&PostRepeatingDelayedTask1, &ran_test, event)));
|
||||
WaitForEvent(event);
|
||||
EXPECT_TRUE(ran_test);
|
||||
}
|
||||
|
||||
TEST(TaskTest, PostOnceTask2) {
|
||||
bool ran_test = false;
|
||||
CefRefPtr<CefWaitableEvent> event =
|
||||
CefWaitableEvent::CreateWaitableEvent(true, false);
|
||||
CefPostTask(TID_FILE_USER_VISIBLE, CefCreateClosureTask(base::BindOnce(
|
||||
&PostOnceTask2, &ran_test, event)));
|
||||
WaitForEvent(event);
|
||||
EXPECT_TRUE(ran_test);
|
||||
}
|
||||
|
||||
TEST(TaskTest, PostRepeatingTask2) {
|
||||
bool ran_test = false;
|
||||
CefRefPtr<CefWaitableEvent> event =
|
||||
CefWaitableEvent::CreateWaitableEvent(true, false);
|
||||
CefPostTask(TID_FILE_USER_VISIBLE,
|
||||
CefCreateClosureTask(
|
||||
base::BindRepeating(&PostRepeatingTask2, &ran_test, event)));
|
||||
WaitForEvent(event);
|
||||
EXPECT_TRUE(ran_test);
|
||||
}
|
||||
|
||||
TEST(TaskTest, PostOnceDelayedTask2) {
|
||||
bool ran_test = false;
|
||||
CefRefPtr<CefWaitableEvent> event =
|
||||
CefWaitableEvent::CreateWaitableEvent(true, false);
|
||||
CefPostTask(TID_FILE_USER_VISIBLE,
|
||||
CefCreateClosureTask(
|
||||
base::BindOnce(&PostOnceDelayedTask2, &ran_test, event)));
|
||||
WaitForEvent(event);
|
||||
EXPECT_TRUE(ran_test);
|
||||
}
|
||||
|
||||
TEST(TaskTest, PostRepeatingDelayedTask2) {
|
||||
bool ran_test = false;
|
||||
CefRefPtr<CefWaitableEvent> event =
|
||||
CefWaitableEvent::CreateWaitableEvent(true, false);
|
||||
CefPostTask(TID_FILE_USER_VISIBLE,
|
||||
CefCreateClosureTask(base::BindRepeating(
|
||||
&PostRepeatingDelayedTask2, &ran_test, event)));
|
||||
WaitForEvent(event);
|
||||
EXPECT_TRUE(ran_test);
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue