// Copyright (c) 2013 CEF Python, see the Authors file.
// All rights reserved. Licensed under BSD 3-clause license.
// Project website: https://github.com/cztomczak/cefpython
#include "javascript_callback.h"
#include
#include
#include "v8utils.h"
#include "cefpython_app.h"
#include "include/base/cef_logging.h"
template
inline std::string AnyToString(const T& value)
{
std::ostringstream oss;
oss 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 args) {
if (g_jsCallbackMap.empty()) {
LOG(ERROR) second.second;
CefRefPtr context = frame->GetV8Context();
context->Enter();
CefV8ValueList v8Arguments = CefListValueToCefV8ValueList(args);
CefRefPtr v8ReturnValue = callback->ExecuteFunction(
NULL, v8Arguments);
if (v8ReturnValue.get()) {
context->Exit();
return true;
} else {
context->Exit();
LOG(ERROR) 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++);
LOG(INFO)