• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright (c) 2013 The Chromium Embedded Framework Authors. All rights
2 // reserved. Use of this source code is governed by a BSD-style license that
3 // can be found in the LICENSE file.
4 
5 #include "tests/ceftests/thread_helper.h"
6 
7 #include <memory>
8 
9 #include "include/wrapper/cef_closure_task.h"
10 
SignalEvent(CefRefPtr<CefWaitableEvent> event)11 void SignalEvent(CefRefPtr<CefWaitableEvent> event) {
12   event->Signal();
13 }
14 
WaitForThread(CefThreadId thread_id,int64 delay_ms)15 void WaitForThread(CefThreadId thread_id, int64 delay_ms) {
16   CefRefPtr<CefWaitableEvent> event =
17       CefWaitableEvent::CreateWaitableEvent(true, false);
18   CefPostDelayedTask(thread_id, base::BindOnce(SignalEvent, event), delay_ms);
19   event->Wait();
20 }
21 
WaitForThread(CefRefPtr<CefTaskRunner> task_runner,int64 delay_ms)22 void WaitForThread(CefRefPtr<CefTaskRunner> task_runner, int64 delay_ms) {
23   CefRefPtr<CefWaitableEvent> event =
24       CefWaitableEvent::CreateWaitableEvent(true, false);
25   task_runner->PostDelayedTask(
26       CefCreateClosureTask(base::BindOnce(SignalEvent, event)), delay_ms);
27   event->Wait();
28 }
29 
RunOnThread(CefThreadId thread_id,base::OnceClosure test_impl,CefRefPtr<CefWaitableEvent> event)30 void RunOnThread(CefThreadId thread_id,
31                  base::OnceClosure test_impl,
32                  CefRefPtr<CefWaitableEvent> event) {
33   if (!CefCurrentlyOn(thread_id)) {
34     CefPostTask(thread_id, base::BindOnce(RunOnThread, thread_id,
35                                           std::move(test_impl), event));
36     return;
37   }
38 
39   std::move(test_impl).Run();
40   SignalEvent(event);
41 }
42 
RunOnThreadAsync(CefThreadId thread_id,base::OnceCallback<void (CefRefPtr<CefWaitableEvent>)> test_impl,CefRefPtr<CefWaitableEvent> event)43 void RunOnThreadAsync(
44     CefThreadId thread_id,
45     base::OnceCallback<void(CefRefPtr<CefWaitableEvent>)> test_impl,
46     CefRefPtr<CefWaitableEvent> event) {
47   if (!CefCurrentlyOn(thread_id)) {
48     CefPostTask(thread_id, base::BindOnce(RunOnThreadAsync, thread_id,
49                                           std::move(test_impl), event));
50     return;
51   }
52 
53   std::move(test_impl).Run(event);
54 }
55