| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent e234b00 commit 0f58a3e
10 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -7,6 +7,7 @@ | |||
| 7 | 7 | // To be able to use 'public' declarations you need to include Python.h and cefpython.h. | |
| 8 | 8 | // This include must be before including CEF, otherwise you get errors like: | |
| 9 | 9 | // | /usr/include/python2.7/pyconfig.h:1161:0: warning: "_POSIX_C_SOURCE" redefined | |
| 10 | + // This file needs to be included first before CEF. | ||
| 10 | 11 | #include "Python.h" | |
| 11 | 12 | ||
| 12 | 13 | // All the imports that are required when including "cefpython.h". | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -53,12 +53,12 @@ bool ClientHandler::OnProcessMessageReceived(CefRefPtr<CefBrowser> browser, | |||
| 53 | 53 | if (args->GetSize() == 3 | |
| 54 | 54 | && args->GetType(0) == VTYPE_INT // frameId | |
| 55 | 55 | && args->GetType(1) == VTYPE_STRING // funcName | |
| 56 | - && args->GetType(2) == VTYPE_LIST) { // funcArgs | ||
| 56 | + && args->GetType(2) == VTYPE_LIST) { // funcArguments | ||
| 57 | 57 | int64 frameId = args->GetInt(0); | |
| 58 | - CefRefPtr<CefFrame> frame = browser->GetFrame(frameId); | ||
| 59 | 58 | CefString funcName = args->GetString(1); | |
| 60 | - CefRefPtr<CefListValue> funcArgs = args->GetList(2); | ||
| 61 | - V8FunctionHandler_Execute(browser, frame, funcName, funcArgs); | ||
| 59 | + CefRefPtr<CefListValue> funcArguments = args->GetList(2); | ||
| 60 | + CefRefPtr<CefFrame> frame = browser->GetFrame(frameId); | ||
| 61 | + V8FunctionHandler_Execute(browser, frame, funcName, funcArguments); | ||
| 62 | 62 | return true; | |
| 63 | 63 | } else { | |
| 64 | 64 | DebugLog("Browser: OnProcessMessageReceived(): invalid arguments," \ | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -41,8 +41,8 @@ <h3>HTML5 video and accelerated content</h3> | |||
| 41 | 41 | ||
| 42 | 42 | ||
| 43 | 43 | <h3>Javascript bindings</h3> | |
| 44 | - <a href="javascript:external.Print('printing in python console from js')"> | ||
| 45 | - external.Print('printing in python console from js')</a> | ||
| 44 | + <a href="javascript:PyPrint('printing in python console from js')"> | ||
| 45 | + PyPrint('printing in python console from js')</a> | ||
| 46 | 46 | ||
| 47 | 47 | ||
| 48 | 48 | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -106,6 +106,7 @@ def __init__(self): | |||
| 106 | 106 | ||
| 107 | 107 | jsBindings = cefpython.JavascriptBindings( | |
| 108 | 108 | bindToFrames=False, bindToPopups=False) | |
| 109 | + jsBindings.SetFunction("PyPrint", PyPrint) | ||
| 109 | 110 | jsBindings.SetObject("external", JavascriptBindings(self.browser)) | |
| 110 | 111 | self.browser.SetJavascriptBindings(jsBindings) | |
| 111 | 112 | ||
@@ -133,6 +134,9 @@ def OnClose(self, event): | |||
| 133 | 134 | def OnIdle(self, event): | |
| 134 | 135 | cefpython.MessageLoopWork() | |
| 135 | 136 | ||
| 137 | + def PyPrint(message): | ||
| 138 | + print(message) | ||
| 139 | + | ||
| 136 | 140 | class JavascriptBindings: | |
| 137 | 141 | mainBrowser = None | |
| 138 | 142 | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -144,7 +144,8 @@ void CefPythonApp::OnContextCreated(CefRefPtr<CefBrowser> browser, | |||
| 144 | 144 | if (frame->IsMain()) { | |
| 145 | 145 | DoJavascriptBindingsForFrame(browser, frame, context); | |
| 146 | 146 | } else { | |
| 147 | - if (jsBindings->GetType("bindToFrames") == VTYPE_BOOL | ||
| 147 | + if (jsBindings->HasKey("bindToFrames") | ||
| 148 | + && jsBindings->GetType("bindToFrames") == VTYPE_BOOL | ||
| 148 | 149 | && jsBindings->GetBool("bindToFrames")) { | |
| 149 | 150 | DoJavascriptBindingsForFrame(browser, frame, context); | |
| 150 | 151 | } | |
@@ -231,6 +232,52 @@ void CefPythonApp::RemoveJavascriptBindings(CefRefPtr<CefBrowser> browser) { | |||
| 231 | 232 | } | |
| 232 | 233 | } | |
| 233 | 234 | ||
| 235 | + bool CefPythonApp::BindedFunctionExists(CefRefPtr<CefBrowser> browser, | ||
| 236 | + const CefString& funcName) { | ||
| 237 | + CefRefPtr<CefDictionaryValue> jsBindings = GetJavascriptBindings(browser); | ||
| 238 | + if (!jsBindings.get()) { | ||
| 239 | + return false; | ||
| 240 | + } | ||
| 241 | + std::string strFuncName = funcName.ToString(); | ||
| 242 | + size_t dotPosition = strFuncName.find("."); | ||
| 243 | + if (std::string::npos != dotPosition) { | ||
| 244 | + // This is a method call, funcName == "object.method". | ||
| 245 | + CefString objectName(strFuncName.substr(0, dotPosition)); | ||
| 246 | + CefString methodName(strFuncName.substr(dotPosition, std::string::npos)); | ||
| 247 | + if (!(jsBindings->HasKey("objects") | ||
| 248 | + && jsBindings->GetType("objects") == VTYPE_DICTIONARY)) { | ||
| 249 | + DebugLog("Renderer: BindedFunctionExists() FAILED: "\ | ||
| 250 | + "objects dictionary not found"); | ||
| 251 | + return false; | ||
| 252 | + } | ||
| 253 | + CefRefPtr<CefDictionaryValue> objects = \ | ||
| 254 | + jsBindings->GetDictionary("objects"); | ||
| 255 | + if (objects->HasKey(objectName)) { | ||
| 256 | + if (!(objects->GetType(objectName) == VTYPE_DICTIONARY)) { | ||
| 257 | + DebugLog("Renderer: BindedFunctionExists() FAILED: "\ | ||
| 258 | + "objects dictionary has invalid type"); | ||
| 259 | + return false; | ||
| 260 | + } | ||
| 261 | + CefRefPtr<CefDictionaryValue> methods = \ | ||
| 262 | + objects->GetDictionary(objectName); | ||
| 263 | + return methods->HasKey(methodName); | ||
| 264 | + } else { | ||
| 265 | + return false; | ||
| 266 | + } | ||
| 267 | + } else { | ||
| 268 | + // This is a function call. | ||
| 269 | + if (!(jsBindings->HasKey("functions") | ||
| 270 | + && jsBindings->GetType("functions") == VTYPE_DICTIONARY)) { | ||
| 271 | + DebugLog("Renderer: BindedFunctionExists() FAILED: "\ | ||
| 272 | + "functions dictionary not found"); | ||
| 273 | + return false; | ||
| 274 | + } | ||
| 275 | + CefRefPtr<CefDictionaryValue> functions = \ | ||
| 276 | + jsBindings->GetDictionary("functions"); | ||
| 277 | + return functions->HasKey(funcName); | ||
| 278 | + } | ||
| 279 | + } | ||
| 280 | + | ||
| 234 | 281 | void CefPythonApp::DoJavascriptBindingsForBrowser( | |
| 235 | 282 | CefRefPtr<CefBrowser> browser) { | |
| 236 | 283 | // get frame | |
@@ -246,7 +293,8 @@ void CefPythonApp::DoJavascriptBindingsForBrowser( | |||
| 246 | 293 | return; | |
| 247 | 294 | } | |
| 248 | 295 | std::vector<int64> frameIds; | |
| 249 | - if (jsBindings->GetType("bindToFrames") == VTYPE_BOOL | ||
| 296 | + if (jsBindings->HasKey("bindToFrames") | ||
| 297 | + && jsBindings->GetType("bindToFrames") == VTYPE_BOOL | ||
| 250 | 298 | && jsBindings->GetBool("bindToFrames")) { | |
| 251 | 299 | browser->GetFrameIdentifiers(frameIds); | |
| 252 | 300 | } else { | |
@@ -277,16 +325,28 @@ void CefPythonApp::DoJavascriptBindingsForFrame(CefRefPtr<CefBrowser> browser, | |||
| 277 | 325 | return; | |
| 278 | 326 | } | |
| 279 | 327 | DebugLog("Renderer: DoJavascriptBindingsForFrame(): bindings are set"); | |
| 280 | - if (!(jsBindings->GetType("functions") == VTYPE_LIST | ||
| 328 | + if (!(jsBindings->HasKey("functions") | ||
| 329 | + && jsBindings->GetType("functions") == VTYPE_DICTIONARY | ||
| 330 | + && jsBindings->HasKey("properties") | ||
| 281 | 331 | && jsBindings->GetType("properties") == VTYPE_DICTIONARY | |
| 332 | + && jsBindings->HasKey("objects") | ||
| 282 | 333 | && jsBindings->GetType("objects") == VTYPE_DICTIONARY | |
| 334 | + && jsBindings->HasKey("bindToFrames") | ||
| 283 | 335 | && jsBindings->GetType("bindToFrames") == VTYPE_BOOL)) { | |
| 284 | 336 | DebugLog("Renderer: DoJavascriptBindingsForFrame() FAILED: " \ | |
| 285 | 337 | "invalid data [1]"); | |
| 286 | 338 | return; | |
| 287 | 339 | } | |
| 340 | + // A context must be explicitly entered before creating a | ||
| 341 | + // V8 Object, Array, Function or Date asynchronously. | ||
| 342 | + bool enteredContext = false; | ||
| 343 | + if (!context->IsSame(CefV8Context::GetCurrentContext())) { | ||
| 344 | + enteredContext = true; | ||
| 345 | + context->Enter(); | ||
| 346 | + } | ||
| 288 | 347 | // TODO: properties, objects, other frames. | |
| 289 | - CefRefPtr<CefListValue> functions = jsBindings->GetList("functions"); | ||
| 348 | + CefRefPtr<CefDictionaryValue> functions = \ | ||
| 349 | + jsBindings->GetDictionary("functions"); | ||
| 290 | 350 | CefRefPtr<CefDictionaryValue> properties = \ | |
| 291 | 351 | jsBindings->GetDictionary("properties"); | |
| 292 | 352 | CefRefPtr<CefDictionaryValue> objects = \ | |
@@ -297,20 +357,29 @@ void CefPythonApp::DoJavascriptBindingsForFrame(CefRefPtr<CefBrowser> browser, | |||
| 297 | 357 | && objects->IsValid())) { | |
| 298 | 358 | DebugLog("Renderer: DoJavascriptBindingsForFrame() FAILED: " \ | |
| 299 | 359 | "invalid data [2]"); | |
| 360 | + if (enteredContext) | ||
| 361 | + context->Exit(); | ||
| 300 | 362 | return; | |
| 301 | 363 | } | |
| 302 | 364 | CefRefPtr<CefV8Value> v8Window = context->GetGlobal(); | |
| 303 | 365 | CefRefPtr<CefV8Value> v8Function; | |
| 366 | + CefRefPtr<CefV8Handler> v8FunctionHandler(new V8FunctionHandler(this)); | ||
| 304 | 367 | // FUNCTIONS. | |
| 305 | - for (unsigned int i = 0; i < functions->GetSize(); i++) { | ||
| 306 | - if (functions->GetType(i) != VTYPE_STRING) { | ||
| 307 | - DebugLog("Renderer: DoJavascriptBindingsForFrame() FAILED: " \ | ||
| 308 | - "invalid data [3]"); | ||
| 309 | - return; | ||
| 310 | - } | ||
| 311 | - CefString funcName = functions->GetString(i); | ||
| 312 | - v8Function = CefV8Value::CreateFunction(funcName, v8FunctionHandler_); | ||
| 368 | + std::vector<CefString> functionsVector; | ||
| 369 | + if (!functions->GetKeys(functionsVector)) { | ||
| 370 | + DebugLog("Renderer: DoJavascriptBindingsForFrame(): " \ | ||
| 371 | + "functions->GetKeys() FAILED"); | ||
| 372 | + if (enteredContext) | ||
| 373 | + context->Exit(); | ||
| 374 | + return; | ||
| 375 | + } | ||
| 376 | + for (std::vector<CefString>::iterator it = functionsVector.begin(); \ | ||
| 377 | + it != functionsVector.end(); ++it) { | ||
| 378 | + CefString funcName = *it; | ||
| 379 | + v8Function = CefV8Value::CreateFunction(funcName, v8FunctionHandler); | ||
| 313 | 380 | v8Window->SetValue(funcName, v8Function, | |
| 314 | 381 | V8_PROPERTY_ATTRIBUTE_NONE); | |
| 315 | 382 | } | |
| 383 | + if (enteredContext) | ||
| 384 | + context->Exit(); | ||
| 316 | 385 | } | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -24,8 +24,7 @@ class CefPythonApp : | |||
| 24 | 24 | public CefBrowserProcessHandler, | |
| 25 | 25 | public CefRenderProcessHandler { | |
| 26 | 26 | public: | |
| 27 | - CefPythonApp() | ||
| 28 | - : v8FunctionHandler_(new V8FunctionHandler()) { | ||
| 27 | + CefPythonApp() { | ||
| 29 | 28 | } | |
| 30 | 29 | ||
| 31 | 30 | virtual void OnBeforeCommandLineProcessing( | |
@@ -118,17 +117,18 @@ class CefPythonApp : | |||
| 118 | 117 | ||
| 119 | 118 | virtual void RemoveJavascriptBindings(CefRefPtr<CefBrowser> browser) OVERRIDE; | |
| 120 | 119 | ||
| 120 | + virtual bool BindedFunctionExists(CefRefPtr<CefBrowser> browser, | ||
| 121 | + const CefString& funcName) OVERRIDE; | ||
| 122 | + | ||
| 121 | 123 | virtual void DoJavascriptBindingsForBrowser(CefRefPtr<CefBrowser> browser) | |
| 122 | 124 | OVERRIDE; | |
| 123 | 125 | ||
| 124 | 126 | virtual void DoJavascriptBindingsForFrame(CefRefPtr<CefBrowser> browser, | |
| 125 | 127 | CefRefPtr<CefFrame> frame, | |
| 126 | 128 | CefRefPtr<CefV8Context> context) OVERRIDE; | |
| 127 | 129 | ||
| 128 | - | ||
| 129 | 130 | protected: | |
| 130 | 131 | std::map<int, CefRefPtr<CefDictionaryValue> > javascriptBindings_; | |
| 131 | - CefRefPtr<CefV8Handler> v8FunctionHandler_; | ||
| 132 | 132 | ||
| 133 | 133 | private: | |
| 134 | 134 | // Include the default reference counting implementation. | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -2,7 +2,10 @@ | |||
| 2 | 2 | // License: New BSD License. | |
| 3 | 3 | // Website: http://code.google.com/p/cefpython/ | |
| 4 | 4 | ||
| 5 | + #include "cefpython_public_api.h" | ||
| 5 | 6 | #include "v8function_handler.h" | |
| 7 | + #include "v8utils.h" | ||
| 8 | + #include "cefpython_app.h" | ||
| 6 | 9 | ||
| 7 | 10 | // Defined as "inline" to get rid of the "already defined" errors | |
| 8 | 11 | // when linking. | |
@@ -15,17 +18,29 @@ inline void DebugLog(const char* szString) | |||
| 15 | 18 | fclose(pFile); | |
| 16 | 19 | } | |
| 17 | 20 | ||
| 18 | - bool V8FunctionHandler::Execute(const CefString& name, | ||
| 21 | + bool V8FunctionHandler::Execute(const CefString& funcName, | ||
| 19 | 22 | CefRefPtr<CefV8Value> object, | |
| 20 | - const CefV8ValueList& arguments, | ||
| 23 | + const CefV8ValueList& v8Arguments, | ||
| 21 | 24 | CefRefPtr<CefV8Value>& retval, | |
| 22 | 25 | CefString& exception) { | |
| 23 | - DebugLog("V8FunctionHandler::Execute()"); | ||
| 26 | + DebugLog("Renderer: V8FunctionHandler::Execute()"); | ||
| 24 | 27 | CefRefPtr<CefV8Context> context = CefV8Context::GetCurrentContext(); | |
| 25 | 28 | CefRefPtr<CefBrowser> browser = context.get()->GetBrowser(); | |
| 26 | - CefRefPtr<CefFrame> frame = context.get()->GetFrame(); | ||
| 27 | - // convert arguments to CefListValue | ||
| 28 | - // set retval to null | ||
| 29 | - // send process message: frameId, funcName, funcArgs | ||
| 30 | - return false; | ||
| 29 | + CefRefPtr<CefFrame> frame = context.get()->GetFrame(); | ||
| 30 | + if (!cefPythonApp_->BindedFunctionExists(browser, funcName)) { | ||
| 31 | + return false; | ||
| 32 | + } | ||
| 33 | + CefRefPtr<CefListValue> funcArguments = V8ValueListToCefListValue( | ||
| 34 | + v8Arguments); | ||
| 35 | + // TODO: losing int64 precision here. | ||
| 36 | + int frameId = (int)frame->GetIdentifier(); | ||
| 37 | + CefRefPtr<CefProcessMessage> processMessage = CefProcessMessage::Create( | ||
| 38 | + "V8FunctionHandler::Execute"); | ||
| 39 | + CefRefPtr<CefListValue> messageArguments = processMessage->GetArgumentList(); | ||
| 40 | + messageArguments->SetInt(0, frameId); | ||
| 41 | + messageArguments->SetString(1, funcName); | ||
| 42 | + messageArguments->SetList(2, funcArguments); | ||
| 43 | + browser->SendProcessMessage(PID_BROWSER, processMessage); | ||
| 44 | + retval = CefV8Value::CreateNull(); | ||
| 45 | + return true; | ||
| 31 | 46 | } | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -2,17 +2,26 @@ | |||
| 2 | 2 | // License: New BSD License. | |
| 3 | 3 | // Website: http://code.google.com/p/cefpython/ | |
| 4 | 4 | ||
| 5 | + #pragma once | ||
| 6 | + | ||
| 5 | 7 | #include "include/cef_v8.h" | |
| 6 | 8 | #include "util.h" | |
| 7 | 9 | ||
| 10 | + class CefPythonApp; | ||
| 11 | + | ||
| 8 | 12 | class V8FunctionHandler | |
| 9 | 13 | : public CefV8Handler { | |
| 10 | 14 | public: | |
| 15 | + V8FunctionHandler(CefRefPtr<CefPythonApp> cefPythonApp) | ||
| 16 | + : cefPythonApp_(cefPythonApp) { | ||
| 17 | + } | ||
| 11 | 18 | virtual bool Execute(const CefString& name, | |
| 12 | 19 | CefRefPtr<CefV8Value> object, | |
| 13 | 20 | const CefV8ValueList& arguments, | |
| 14 | 21 | CefRefPtr<CefV8Value>& retval, | |
| 15 | 22 | CefString& exception) OVERRIDE; | |
| 23 | + protected: | ||
| 24 | + CefRefPtr<CefPythonApp> cefPythonApp_; | ||
| 16 | 25 | private: | |
| 17 | 26 | IMPLEMENT_REFCOUNTING(V8FunctionHandler); | |
| 18 | 27 | }; | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,12 @@ | |||
| 1 | + // Copyright (c) 2012-2013 The CEF Python authors. All rights reserved. | ||
| 2 | + // License: New BSD License. | ||
| 3 | + // Website: http://code.google.com/p/cefpython/ | ||
| 4 | + | ||
| 5 | + #include "include/cef_v8.h" | ||
| 6 | + #include "include/cef_values.h" | ||
| 7 | + | ||
| 8 | + CefRefPtr<CefListValue> V8ValueListToCefListValue( | ||
| 9 | + const CefV8ValueList& v8List) { | ||
| 10 | + // TODO. | ||
| 11 | + return CefListValue::Create(); | ||
| 12 | + } | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -4,8 +4,8 @@ | |||
| 4 | 4 | ||
| 5 | 5 | cdef class JavascriptBindings: | |
| 6 | 6 | # By default binding only to top frame. | |
| 7 | - cdef public int bindToFrames | ||
| 8 | - cdef public int bindToPopups | ||
| 7 | + cdef public cpp_bool bindToFrames | ||
| 8 | + cdef public cpp_bool bindToPopups | ||
| 9 | 9 | cdef public dict functions | |
| 10 | 10 | cdef public dict properties | |
| 11 | 11 | cdef public dict objects | |
@@ -15,8 +15,8 @@ cdef class JavascriptBindings: | |||
| 15 | 15 | self.properties = {} | |
| 16 | 16 | self.objects = {} | |
| 17 | 17 | ||
| 18 | - self.bindToFrames = int(bindToFrames) | ||
| 19 | - self.bindToPopups = int(bindToPopups) | ||
| 18 | + self.bindToFrames = bool(bindToFrames) | ||
| 19 | + self.bindToPopups = bool(bindToPopups) | ||
| 20 | 20 | ||
| 21 | 21 | cpdef py_bool GetBindToFrames(self): | |
| 22 | 22 | return bool(self.bindToFrames) | |
@@ -116,23 +116,23 @@ cdef class JavascriptBindings: | |||
| 116 | 116 | # In CEF Python 3 due to its multi-process architecture | |
| 117 | 117 | # Rebind() is used for both first-time binding and rebinding. | |
| 118 | 118 | cdef PyBrowser pyBrowser | |
| 119 | - cdef list functions | ||
| 119 | + cdef dict functions | ||
| 120 | 120 | cdef dict properties | |
| 121 | 121 | cdef dict objects | |
| 122 | - cdef list methods | ||
| 122 | + cdef dict methods | ||
| 123 | 123 | global g_pyBrowsers | |
| 124 | 124 | for browserId, pyBrowser in g_pyBrowsers.iteritems(): | |
| 125 | 125 | # Send to the Renderer process: functions, properties, | |
| 126 | 126 | # objects and its methods, bindToFrames. | |
| 127 | - functions = [] | ||
| 127 | + functions = {} | ||
| 128 | 128 | for funcName in self.functions: | |
| 129 | - functions.append(funcName) | ||
| 129 | + functions[funcName] = None | ||
| 130 | 130 | properties = self.properties | |
| 131 | 131 | objects = {} | |
| 132 | 132 | for objectName in self.objects: | |
| 133 | - methods = [] | ||
| 133 | + methods = {} | ||
| 134 | 134 | for methodName in self.objects[objectName]: | |
| 135 | - methods.append(methodName) | ||
| 135 | + methods[methodName] = None | ||
| 136 | 136 | pyBrowser.SendProcessMessage(cef_types.PID_RENDERER, | |
| 137 | 137 | "DoJavascriptBindings", [{ | |
| 138 | 138 | "functions": functions, | |
| Back | FazBrowse Home | New Git URL |
0 commit comments