Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 7145ffa

Browse files
committed
Add cef.PostDelayedTask function
1 parent b1f5348 commit 7145ffa

File tree

9 files changed

+75
-16
lines changed

9 files changed

+75
-16
lines changed

README.md

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -156,8 +156,8 @@ directly.
156156
- [Tutorial](docs/Tutorial.md)
157157

158158

159-
### API categories
160-
159+
### API categories
160+
161161
#### Modules
162162

163163
* [cefpython](api/cefpython.md#cefpython) module
@@ -212,9 +212,9 @@ directly.
212212
* [StringVisitor](api/StringVisitor.md#stringvisitor-interface) interface
213213
* [WebRequestClient](api/WebRequestClient.md#webrequestclient-interface) interface
214214

215-
216-
### API index
217-
215+
216+
### API index
217+
218218
* [Application settings](api/ApplicationSettings.md#application-settings)
219219
* [accept_language_list](api/ApplicationSettings.md#accept_language_list)
220220
* [auto_zooming](api/ApplicationSettings.md#auto_zooming)
@@ -366,6 +366,7 @@ directly.
366366
* [MessageLoop](api/cefpython.md#messageloop)
367367
* [MessageLoopWork](api/cefpython.md#messageloopwork)
368368
* [PostTask](api/cefpython.md#posttask)
369+
* [PostDelayedTask](api/cefpython.md#postdelayedtask)
369370
* [QuitMessageLoop](api/cefpython.md#quitmessageloop)
370371
* [SetGlobalClientCallback](api/cefpython.md#setglobalclientcallback)
371372
* [SetOsModalLoop](api/cefpython.md#setosmodalloop)

api/API-index.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,7 @@
153153
* [MessageLoop](cefpython.md#messageloop)
154154
* [MessageLoopWork](cefpython.md#messageloopwork)
155155
* [PostTask](cefpython.md#posttask)
156+
* [PostDelayedTask](cefpython.md#postdelayedtask)
156157
* [QuitMessageLoop](cefpython.md#quitmessageloop)
157158
* [SetGlobalClientCallback](cefpython.md#setglobalclientcallback)
158159
* [SetOsModalLoop](cefpython.md#setosmodalloop)

api/cefpython.md

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ Table of contents:
2323
* [MessageLoop](#messageloop)
2424
* [MessageLoopWork](#messageloopwork)
2525
* [PostTask](#posttask)
26+
* [PostDelayedTask](#postdelayedtask)
2627
* [QuitMessageLoop](#quitmessageloop)
2728
* [SetGlobalClientCallback](#setglobalclientcallback)
2829
* [SetOsModalLoop](#setosmodalloop)
@@ -229,7 +230,7 @@ Description from upstream CEF:
229230

230231
| Parameter | Type |
231232
| --- | --- |
232-
| threadId | int |
233+
| thread | int |
233234
| func | object |
234235
| [args..] | mixed |
235236
| __Return__ | void |
@@ -251,6 +252,20 @@ List of threads in the Renderer process:
251252
* cef.TID_RENDERER: The main thread in the renderer. Used for all webkit and V8 interaction.
252253

253254

255+
### PostDelayedTask
256+
257+
| Parameter | Type |
258+
| --- | --- |
259+
| thread | int |
260+
| delay_ms | int |
261+
| func | object |
262+
| [args..] | mixed |
263+
| __Return__ | void |
264+
265+
Post a task for delayed execution on the specified thread. This
266+
function behaves similarly to PostTask above, but with an additional
267+
|delay_ms| argument.
268+
254269

255270
### QuitMessageLoop
256271

src/client_handler/task.cpp

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,23 @@
77
#include "include/base/cef_bind.h"
88

99
void PostTaskWrapper(int threadId, int taskId) {
10-
// Calling CefPostDelayedTask with 0ms delay seems to give
11-
// better responsiveness than CefPostTask. In wxpython.py
12-
// on Windows the freeze when creating popup window feels
13-
// shorter, when compared to a call to CefPostTask.
10+
CefPostTask(
11+
static_cast<CefThreadId>(threadId),
12+
CefCreateClosureTask(base::Bind(
13+
&PyTaskRunnable,
14+
taskId
15+
))
16+
);
17+
}
18+
19+
void PostDelayedTaskWrapper(int threadId, int64 delay_ms, int taskId) {
1420
CefPostDelayedTask(
1521
static_cast<CefThreadId>(threadId),
1622
CefCreateClosureTask(base::Bind(
1723
&PyTaskRunnable,
1824
taskId
1925
)),
20-
0
26+
delay_ms
2127
);
2228
}
2329

src/client_handler/task.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#include "include/cef_task.h"
1010

1111
void PostTaskWrapper(int threadId, int taskId);
12+
void PostDelayedTaskWrapper(int threadId, int64 delay_ms, int taskId);
1213

1314
CefRefPtr<CefTask> CreateTask_SetCookie(
1415
CefCookieManager* obj,

src/compile_time_constants.pxi

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
# This file was generated by setup.py
2-
DEF UNAME_SYSNAME = "Windows"
2+
DEF UNAME_SYSNAME = "Linux"
33
DEF PY_MAJOR_VERSION = 2

src/extern/cef/cef_task.pxd

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,19 @@
55
from libcpp cimport bool as cpp_bool
66
# noinspection PyUnresolvedReferences
77
cimport cef_types
8+
from cef_types cimport int64
89
from cef_ptr cimport CefRefPtr
910

1011
cdef extern from "include/cef_task.h":
1112
ctypedef int CefThreadId
1213
ctypedef cef_types.cef_thread_id_t CefThreadId
1314

1415
cdef cpp_bool CefCurrentlyOn(CefThreadId)
15-
cdef cpp_bool CefPostTask(CefThreadId threadId, CefRefPtr[CefTask] task)
16+
cdef cpp_bool CefPostTask(CefThreadId threadId,
17+
CefRefPtr[CefTask] task)
18+
cdef cpp_bool CefPostDelayedTask(CefThreadId threadId,
19+
CefRefPtr[CefTask] task,
20+
int64 delay_ms)
1621

1722
cdef cppclass CefTask:
1823
pass

src/extern/task.pxd

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,13 @@ from cef_cookie cimport CefCookie, CefCookieManager
1111
from cef_cookie cimport CefSetCookieCallback, CefDeleteCookiesCallback
1212
# noinspection PyUnresolvedReferences
1313
from libcpp cimport bool as cpp_bool
14+
from cef_types cimport int64
1415

1516

1617
cdef extern from "client_handler/task.h":
1718

1819
void PostTaskWrapper(int threadId, int taskId) nogil
20+
void PostDelayedTaskWrapper(int threadId, int64 delay_ms, int taskId) nogil
1921

2022
cdef CefRefPtr[CefTask] CreateTask_SetCookie(
2123
CefCookieManager* obj,

src/task.pyx

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,11 @@ include "cefpython.pyx"
77
g_taskMaxId = 0
88
g_tasks = {}
99

10-
def PostTask(int threadId, object func, *args):
10+
def PostTask(int thread, object func, *args):
1111
global g_tasks, g_taskMaxId
1212

1313
# Validate threadId.
14-
if threadId not in g_browserProcessThreads:
14+
if thread not in g_browserProcessThreads:
1515
raise Exception("PoastTask failed: requires a browser process thread")
1616

1717
# Validate func.
@@ -31,7 +31,35 @@ def PostTask(int threadId, object func, *args):
3131
# Call C++ wrapper.
3232
cdef int cTaskId = int(g_taskMaxId)
3333
with nogil:
34-
PostTaskWrapper(threadId, cTaskId)
34+
PostTaskWrapper(thread, cTaskId)
35+
36+
37+
def PostDelayedTask(int thread, int delay_ms, object func, *args):
38+
global g_tasks, g_taskMaxId
39+
40+
# Validate threadId.
41+
if thread not in g_browserProcessThreads:
42+
raise Exception("PoastTask failed: requires a browser process thread")
43+
44+
# Validate func.
45+
if not IsFunctionOrMethod(type(func)):
46+
raise Exception("PostTask failed: not a function nor method")
47+
48+
# Params.
49+
cdef list params = list(args)
50+
51+
# Keep func and params until PyTaskRunnable is called.
52+
g_taskMaxId += 1
53+
g_tasks[str(g_taskMaxId)] = {
54+
"func": func,
55+
"params": params
56+
}
57+
58+
# Call C++ wrapper.
59+
cdef int cTaskId = int(g_taskMaxId)
60+
with nogil:
61+
PostDelayedTaskWrapper(thread, delay_ms, cTaskId)
62+
3563

3664
cdef public void PyTaskRunnable(int taskId) except * with gil:
3765
cdef object func

0 commit comments

Comments
 (0)