|
| 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 "javascript_callback.h" |
| 6 | +#include <map> |
| 7 | +#include <sstream> |
| 8 | +#include "DebugLog.h" |
| 9 | +#include "v8utils.h" |
| 10 | + |
| 11 | +template<typename T> |
| 12 | +std::string AnyToString(const T& value) |
| 13 | +{ |
| 14 | + std::ostringstream oss; |
| 15 | + oss << value; |
| 16 | + return oss.str(); |
| 17 | +} |
| 18 | + |
| 19 | +typedef std::map<int, |
| 20 | + std::pair<CefRefPtr<CefFrame>, CefRefPtr<CefV8Value> > > |
| 21 | + JavascriptCallbackMap; |
| 22 | + |
| 23 | +JavascriptCallbackMap g_jsCallbackMap; |
| 24 | +int g_jsCallbackMaxId = 0; |
| 25 | + |
| 26 | +CefString PutJavascriptCallback( |
| 27 | + CefRefPtr<CefFrame> frame, CefRefPtr<CefV8Value> jsCallback) { |
| 28 | + // Returns a "####cefpython####" string followed by json encoded data. |
| 29 | + // {"what":"javascript-callback","callbackId":123, |
| 30 | + // "frameId":123,"functionName":"xx"} |
| 31 | + int callbackId = ++g_jsCallbackMaxId; |
| 32 | + int64 frameId = frame->GetIdentifier(); |
| 33 | + CefString functionName = jsCallback->GetFunctionName(); |
| 34 | + std::string strCallbackId = "####cefpython####"; |
| 35 | + strCallbackId.append("{"); |
| 36 | + // JSON format allows only for double quotes. |
| 37 | + strCallbackId.append("\"what\":\"javascript-callback\""); |
| 38 | + strCallbackId.append(",\"callbackId\":").append(AnyToString(callbackId)); |
| 39 | + strCallbackId.append(",\"frameId\":").append(AnyToString(frameId)); |
| 40 | + strCallbackId.append(",\"functionName\":\"").append(functionName) \ |
| 41 | + .append("\""); |
| 42 | + strCallbackId.append("}"); |
| 43 | + g_jsCallbackMap.insert(std::make_pair( |
| 44 | + callbackId, |
| 45 | + std::make_pair(frame, jsCallback))); |
| 46 | + return strCallbackId; |
| 47 | +} |
| 48 | + |
| 49 | +bool ExecuteJavascriptCallback(int callbackId, CefRefPtr<CefListValue> args) { |
| 50 | + if (g_jsCallbackMap.empty()) { |
| 51 | + DebugLog("Renderer: ExecuteJavascriptCallback() FAILED: " \ |
| 52 | + "callback map is empty"); |
| 53 | + return false; |
| 54 | + } |
| 55 | + JavascriptCallbackMap::const_iterator it = g_jsCallbackMap.find( |
| 56 | + callbackId); |
| 57 | + if (it == g_jsCallbackMap.end()) { |
| 58 | + std::string logMessage = "Renderer: ExecuteJavascriptCallback() " |
| 59 | + "FAILED: callback not found, id="; |
| 60 | + logMessage.append(AnyToString(callbackId)); |
| 61 | + DebugLog(logMessage.c_str()); |
| 62 | + return false; |
| 63 | + } |
| 64 | + CefRefPtr<CefFrame> frame = it->second.first; |
| 65 | + CefRefPtr<CefV8Value> callback = it->second.second; |
| 66 | + CefRefPtr<CefV8Context> context = frame->GetV8Context(); |
| 67 | + context->Enter(); |
| 68 | + CefV8ValueList v8Arguments = CefListValueToCefV8ValueList(args); |
| 69 | + CefRefPtr<CefV8Value> v8ReturnValue = callback->ExecuteFunction( |
| 70 | + NULL, v8Arguments); |
| 71 | + if (v8ReturnValue.get()) { |
| 72 | + return true; |
| 73 | + } else { |
| 74 | + DebugLog("Renderer: ExecuteJavascriptCallback() FAILED: " \ |
| 75 | + "callback->ExecuteFunction() FAILED"); |
| 76 | + return false; |
| 77 | + } |
| 78 | +} |
| 79 | + |
| 80 | +void RemoveJavascriptCallbacksForFrame(CefRefPtr<CefFrame> frame) { |
| 81 | + if (g_jsCallbackMap.empty()) { |
| 82 | + return; |
| 83 | + } |
| 84 | + JavascriptCallbackMap::iterator it = g_jsCallbackMap.begin(); |
| 85 | + int64 frameId = frame->GetIdentifier(); |
| 86 | + while (it != g_jsCallbackMap.end()) { |
| 87 | + if (it->second.first->GetIdentifier() == frameId) { |
| 88 | + g_jsCallbackMap.erase(it); |
| 89 | + DebugLog("Renderer: RemoveJavascriptCallbacksForFrame(): " \ |
| 90 | + "removed js callback from the map"); |
| 91 | + } |
| 92 | + ++it; |
| 93 | + } |
| 94 | +} |
0 commit comments