FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
cefpython/src/subprocess/javascript_callback.cpp at cefpython54 · pythonthings/cefpython · GitHub
pythonthings
cefpython
Repository navigation
Code
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
cefpython
/
src
/
subprocess
/
javascript_callback.cpp
Copy path
More file actions
More file actions
Latest commit
History
History
History
104 lines (97 loc) · 3.72 KB
Breadcrumbs
cefpython
/
src
/
subprocess
/
javascript_callback.cpp
Copy path
File metadata and controls
104 lines (97 loc) · 3.72 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
//
Copyright (c) 2012-2014 The CEF Python authors. All rights reserved.
//
License: New BSD License.
//
Website: http://code.google.com/p/cefpython/
#
include
"
javascript_callback.h
"
#
include
<
map
>
#
include
<
sstream
>
#
include
"
DebugLog.h
"
#
include
"
v8utils.h
"
#
include
"
cefpython_app.h
"
template
<
typename
T>
inline
std::string
AnyToString
(
const
T& value)
{
std::ostringstream oss;
oss << value;
return
oss.
str
();
}
typedef
std::map<
int
,
std::pair<CefRefPtr<CefFrame>, CefRefPtr<CefV8Value> > >
JavascriptCallbackMap;
JavascriptCallbackMap g_jsCallbackMap;
int
g_jsCallbackMaxId =
0
;
CefString
PutJavascriptCallback
(
CefRefPtr<CefFrame> frame, CefRefPtr<CefV8Value> jsCallback) {
//
Returns a "####cefpython####" string followed by json encoded data.
//
{"what":"javascript-callback","callbackId":123,
//
"frameId":123,"functionName":"xx"}
int
callbackId = ++g_jsCallbackMaxId;
int64 frameId = frame->
GetIdentifier
();
CefString functionName = jsCallback->
GetFunctionName
();
std::string strCallbackId =
"
####cefpython####
"
;
strCallbackId.
append
(
"
{
"
);
//
JSON format allows only for double quotes.
strCallbackId.
append
(
"
\"
what
\"
:
\"
javascript-callback
\"
"
);
strCallbackId.
append
(
"
,
\"
callbackId
\"
:
"
).
append
(
AnyToString
(callbackId));
strCallbackId.
append
(
"
,
\"
frameId
\"
:
"
).
append
(
AnyToString
(frameId));
strCallbackId.
append
(
"
,
\"
functionName
\"
:
\"
"
).
append
(functionName) \
.
append
(
"
\"
"
);
strCallbackId.
append
(
"
}
"
);
g_jsCallbackMap.
insert
(
std::make_pair
(
callbackId,
std::make_pair
(frame, jsCallback)));
return
strCallbackId;
}
bool
ExecuteJavascriptCallback
(
int
callbackId, CefRefPtr<CefListValue> args) {
if
(g_jsCallbackMap.
empty
()) {
DebugLog
(
"
Renderer: ExecuteJavascriptCallback() FAILED:
"
\
"
callback map is empty
"
);
return
false
;
}
JavascriptCallbackMap::const_iterator it = g_jsCallbackMap.
find
(
callbackId);
if
(it == g_jsCallbackMap.
end
()) {
std::string logMessage =
"
Renderer: ExecuteJavascriptCallback()
"
"
FAILED: callback not found, id=
"
;
logMessage.
append
(
AnyToString
(callbackId));
DebugLog
(logMessage.
c_str
());
return
false
;
}
CefRefPtr<CefFrame> frame = it->
second
.
first
;
CefRefPtr<CefV8Value> callback = it->
second
.
second
;
CefRefPtr<CefV8Context> context = frame->
GetV8Context
();
context->
Enter
();
CefV8ValueList v8Arguments =
CefListValueToCefV8ValueList
(args);
CefRefPtr<CefV8Value> v8ReturnValue = callback->
ExecuteFunction
(
NULL
, v8Arguments);
if
(v8ReturnValue.
get
()) {
context->
Exit
();
return
true
;
}
else
{
context->
Exit
();
DebugLog
(
"
Renderer: ExecuteJavascriptCallback() FAILED:
"
\
"
callback->ExecuteFunction() FAILED
"
);
return
false
;
}
}
void
RemoveJavascriptCallbacksForFrame
(CefRefPtr<CefFrame> frame) {
if
(g_jsCallbackMap.
empty
()) {
return
;
}
JavascriptCallbackMap::iterator it = g_jsCallbackMap.
begin
();
int64 frameId = frame->
GetIdentifier
();
while
(it != g_jsCallbackMap.
end
()) {
if
(it->
second
.
first
->
GetIdentifier
() == frameId) {
//
Pass current iterator and increment it after passing
//
to the function, but before erase() is called, this
//
is important for it to work in a loop. You can't do this:
//
| if (..) erase(it);
//
| ++it;
//
This would cause an infinite loop.
g_jsCallbackMap.
erase
(it++);
DebugLog
(
"
Renderer: RemoveJavascriptCallbacksForFrame():
"
\
"
removed js callback from the map
"
);
}
else
{
++it;
}
}
}
Back
|
FazBrowse Home
|
New Git URL