Repository navigation
Expand file tree
/
Copy pathcefpython_app.cpp
More file actions
316 lines (288 loc) · 12.5 KB
/
Copy pathcefpython_app.cpp
File metadata and controls
316 lines (288 loc) · 12.5 KB
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
// Copyright (c) 2012-2013 The CEF Python authors. All rights reserved.
// License: New BSD License.
// Website: http://code.google.com/p/cefpython/
#include "cefpython_app.h"
#include "util.h"
#include "include/cef_runnable.h"
#include <stdio.h>
#include <vector>
// Defined as "inline" to get rid of the "already defined" errors
// when linking.
inline void DebugLog(const char* szString)
{
// TODO: get the log_file option from CefSettings.
printf("cefpython: %s\n", szString);
FILE* pFile = fopen("debug.log", "a");
fprintf(pFile, "cefpython_app: %s\n", szString);
fclose(pFile);
}
// -----------------------------------------------------------------------------
// CefApp
// -----------------------------------------------------------------------------
void CefPythonApp::OnBeforeCommandLineProcessing(
const CefString& process_type,
CefRefPtr<CefCommandLine> command_line) {
}
void CefPythonApp::OnRegisterCustomSchemes(
CefRefPtr<CefSchemeRegistrar> registrar) {
}
CefRefPtr<CefResourceBundleHandler> CefPythonApp::GetResourceBundleHandler() {
return NULL;
}
CefRefPtr<CefBrowserProcessHandler> CefPythonApp::GetBrowserProcessHandler() {
return this;
}
CefRefPtr<CefRenderProcessHandler> CefPythonApp::GetRenderProcessHandler() {
DebugLog("Renderer: GetRenderProcessHandler()");
return this;
}
// -----------------------------------------------------------------------------
// CefBrowserProcessHandler
// -----------------------------------------------------------------------------
void CefPythonApp::OnContextInitialized() {
REQUIRE_UI_THREAD();
}
void CefPythonApp::OnBeforeChildProcessLaunch(
CefRefPtr<CefCommandLine> command_line) {}
///
// Called on the browser process IO thread after the main thread has been
// created for a new render process. Provides an opportunity to specify extra
// information that will be passed to
// CefRenderProcessHandler::OnRenderThreadCreated() in the render process. Do
// not keep a reference to |extra_info| outside of this method.
///
void CefPythonApp::OnRenderProcessThreadCreated(
CefRefPtr<CefListValue> extra_info) {
// If you have an existing CefListValue that you would like
// to provide, do this:
// | extra_info = mylist.get()
// The equivalent in Cython is:
// | extra_info.Assign(mylist.get())
REQUIRE_IO_THREAD();
DebugLog("Browser: OnRenderProcessThreadCreated()");
}
// -----------------------------------------------------------------------------
// CefRenderProcessHandler
// -----------------------------------------------------------------------------
///
// Called after the render process main thread has been created. |extra_info|
// is a read-only value originating from
// CefBrowserProcessHandler::OnRenderProcessThreadCreated(). Do not keep a
// reference to |extra_info| outside of this method.
///
void CefPythonApp::OnRenderThreadCreated(CefRefPtr<CefListValue> extra_info) {
}
void CefPythonApp::OnWebKitInitialized() {
}
void CefPythonApp::OnBrowserCreated(CefRefPtr<CefBrowser> browser) {
}
void CefPythonApp::OnBrowserDestroyed(CefRefPtr<CefBrowser> browser) {
RemoveJavascriptBindings(browser);
}
bool CefPythonApp::OnBeforeNavigation(CefRefPtr<CefBrowser> browser,
CefRefPtr<CefFrame> frame,
CefRefPtr<CefRequest> request,
cef_navigation_type_t navigation_type,
bool is_redirect) {
return false;
}
void CefPythonApp::OnContextCreated(CefRefPtr<CefBrowser> browser,
CefRefPtr<CefFrame> frame,
CefRefPtr<CefV8Context> context) {
DebugLog("Renderer: OnContextCreated()");
CefRefPtr<CefProcessMessage> message = CefProcessMessage::Create(
"OnContextCreated");
CefRefPtr<CefListValue> args = message->GetArgumentList();
/*
Sending int64 type using process messaging would require
converting it to a string or a binary, or you could send
two ints, see this topic:
http://www.magpcss.org/ceforum/viewtopic.php?f=6&t=10869
*/
/*
// Example of converting int64 to string. Still need an
// example of converting it back from string.
std::string logMessage = "OnContextCreated(): frameId=";
stringstream stream;
int64 value = frame->GetIdentifier();
stream << value;
logMessage.append(stream.str());
DebugLog(logMessage.c_str());
*/
// TODO: losing int64 precision, the solution is to convert
// it to string and then in the Browser process back
// from string to int64. But it is rather unlikely
// that number of frames will exceed int range, so
// casting it to int for now.
args->SetInt(0, (int)(frame->GetIdentifier()));
browser->SendProcessMessage(PID_BROWSER, message);
CefRefPtr<CefDictionaryValue> jsBindings = GetJavascriptBindings(browser);
if (jsBindings.get()) {
// Javascript bindings are most probably not yet set for
// the main frame, they will be set a moment later due to
// process messaging delay. The code seems to be executed
// only for iframes.
if (frame->IsMain()) {
DoJavascriptBindingsForFrame(browser, frame, context);
} else {
if (jsBindings->GetType("bindToFrames") == VTYPE_BOOL
&& jsBindings->GetBool("bindToFrames")) {
DoJavascriptBindingsForFrame(browser, frame, context);
}
}
}
}
void CefPythonApp::OnContextReleased(CefRefPtr<CefBrowser> browser,
CefRefPtr<CefFrame> frame,
CefRefPtr<CefV8Context> context) {
DebugLog("Renderer: OnContextReleased()");
CefRefPtr<CefProcessMessage> message = CefProcessMessage::Create(
"OnContextReleased");
CefRefPtr<CefListValue> args = message->GetArgumentList();
// TODO: losing int64 precision, the solution is to convert
// it to string and then in the Browser process back
// from string to int64. But it is rather unlikely
// that number of frames will exceed int range, so
// casting it to int for now.
args->SetInt(0, (int)(frame->GetIdentifier()));
browser->SendProcessMessage(PID_BROWSER, message);
}
void CefPythonApp::OnUncaughtException(CefRefPtr<CefBrowser> browser,
CefRefPtr<CefFrame> frame,
CefRefPtr<CefV8Context> context,
CefRefPtr<CefV8Exception> exception,
CefRefPtr<CefV8StackTrace> stackTrace) {
}
void CefPythonApp::OnFocusedNodeChanged(CefRefPtr<CefBrowser> browser,
CefRefPtr<CefFrame> frame,
CefRefPtr<CefDOMNode> node) {
}
///
// Called when a new message is received from a different process. Return true
// if the message was handled or false otherwise. Do not keep a reference to
// or attempt to access the message outside of this callback.
///
bool CefPythonApp::OnProcessMessageReceived(CefRefPtr<CefBrowser> browser,
CefProcessId source_process,
CefRefPtr<CefProcessMessage> message) {
std::string messageName = message->GetName().ToString();
std::string logMessage = "Renderer: OnProcessMessageReceived(): ";
logMessage.append(messageName.c_str());
DebugLog(logMessage.c_str());
if (messageName == "DoJavascriptBindings") {
CefRefPtr<CefListValue> args = message->GetArgumentList();
if (args->GetSize() == 1
&& args->GetType(0) == VTYPE_DICTIONARY
&& args->GetDictionary(0)->IsValid()) {
// Is it necessary to make a copy? It won't harm.
SetJavascriptBindings(browser,
args->GetDictionary(0)->Copy(false));
DoJavascriptBindingsForBrowser(browser);
} else {
DebugLog("Renderer: OnProcessMessageReceived(): invalid arguments,"\
" messageName=DoJavascriptBindings");
return false;
}
}
return false;
}
void CefPythonApp::SetJavascriptBindings(CefRefPtr<CefBrowser> browser,
CefRefPtr<CefDictionaryValue> data) {
javascriptBindings_[browser->GetIdentifier()] = data;
}
CefRefPtr<CefDictionaryValue> CefPythonApp::GetJavascriptBindings(
CefRefPtr<CefBrowser> browser) {
int browserId = browser->GetIdentifier();
if (javascriptBindings_.find(browserId) != javascriptBindings_.end()) {
return javascriptBindings_[browserId];
}
return NULL;
}
void CefPythonApp::RemoveJavascriptBindings(CefRefPtr<CefBrowser> browser) {
int browserId = browser->GetIdentifier();
if (javascriptBindings_.find(browserId) != javascriptBindings_.end()) {
javascriptBindings_.erase(browserId);
}
}
void CefPythonApp::DoJavascriptBindingsForBrowser(
CefRefPtr<CefBrowser> browser) {
// get frame
// get context
// if bindToFrames is true loop through all frames,
// otherwise just the main frame.
// post task on a valid v8 thread
CefRefPtr<CefDictionaryValue> jsBindings = GetJavascriptBindings(browser);
if (!jsBindings.get()) {
// Bindings must be set before this function is called.
DebugLog("Renderer: DoJavascriptBindingsForBrowser() FAILED: " \
"bindings not set");
return;
}
std::vector<int64> frameIds;
if (jsBindings->GetType("bindToFrames") == VTYPE_BOOL
&& jsBindings->GetBool("bindToFrames")) {
browser->GetFrameIdentifiers(frameIds);
} else {
frameIds.push_back(browser->GetMainFrame()->GetIdentifier());
}
/*
Another way:
| for (std::vector<int64>::iterator it = v.begin(); it != v.end(); ++it) {
| CefRefPtr<CefFrame> frame = browser->GetFrame(*it);
*/
for (std::vector<int>::size_type i = 0; i != frameIds.size(); i++) {
CefRefPtr<CefFrame> frame = browser->GetFrame(frameIds[i]);
CefRefPtr<CefV8Context> context = frame->GetV8Context();
CefRefPtr<CefTaskRunner> taskRunner = context->GetTaskRunner();
taskRunner->PostTask(NewCefRunnableMethod(
this, &CefPythonApp::DoJavascriptBindingsForFrame,
browser, frame, context));
}
}
void CefPythonApp::DoJavascriptBindingsForFrame(CefRefPtr<CefBrowser> browser,
CefRefPtr<CefFrame> frame,
CefRefPtr<CefV8Context> context) {
CefRefPtr<CefDictionaryValue> jsBindings = GetJavascriptBindings(browser);
if (!jsBindings.get()) {
// Bindings may not yet be set, it's okay.
DebugLog("Renderer: DoJavascriptBindingsForFrame(): bindings not set");
return;
}
DebugLog("Renderer: DoJavascriptBindingsForFrame(): bindings are set");
if (!(jsBindings->GetType("functions") == VTYPE_LIST
&& jsBindings->GetType("properties") == VTYPE_DICTIONARY
&& jsBindings->GetType("objects") == VTYPE_DICTIONARY
&& jsBindings->GetType("bindToFrames") == VTYPE_BOOL)) {
DebugLog("Renderer: DoJavascriptBindingsForFrame() FAILED: " \
"invalid data [1]");
return;
}
// TODO: properties, objects, other frames.
CefRefPtr<CefListValue> functions = jsBindings->GetList("functions");
CefRefPtr<CefDictionaryValue> properties = \
jsBindings->GetDictionary("properties");
CefRefPtr<CefDictionaryValue> objects = \
jsBindings->GetDictionary("objects");
// Here in this function we bind only for the current frame.
// | bool bindToFrames = jsBindings->GetBool("bindToFrames");
if (!(functions->IsValid() && properties->IsValid()
&& objects->IsValid())) {
DebugLog("Renderer: DoJavascriptBindingsForFrame() FAILED: " \
"invalid data [2]");
return;
}
CefRefPtr<CefV8Value> v8Window = context->GetGlobal();
CefRefPtr<CefV8Value> v8Function;
// FUNCTIONS.
for (unsigned int i = 0; i < functions->GetSize(); i++) {
if (functions->GetType(i) != VTYPE_STRING) {
DebugLog("Renderer: DoJavascriptBindingsForFrame() FAILED: " \
"invalid data [3]");
return;
}
CefString funcName = functions->GetString(i);
v8Function = CefV8Value::CreateFunction(funcName, v8FunctionHandler_);
v8Window->SetValue(funcName, v8Function,
V8_PROPERTY_ATTRIBUTE_NONE);
}
}