I hope you can help me.
At first some code:
- Code: Select all
var app;
if (!app)
app = {};
(function() {
/* removed some unnecessary code */
app.setMessageCallback = function(name, callback)
{
var gwtRequest = name + "||" + callback;
window.cefQuery({
request: 'setMessageCallback:' + gwtRequest,
onSuccess: function(response)
{
//callback(response);
},
onFailure: function(error_code, error_message)
{
console.log("setMessageCallback: " + error_message);
}
});
How the function will be called:
- Code: Select all
function xyzAdapter(name, params, callback)
{
app.sendMessage(name, [params]);
//console.log("xyzAdapter: " + name + " ; " + params + " ; " + callback);
var func = function(name, returnValue)
{
if(callback && typeof(callback) === "function")
{
console.log( name +" = "+ returnValue );
callback( returnValue );
}
else
{
console.log("callback !== function : "+callback);
}
};
app.setMessageCallback(name, func);
}
My problem is, I can't execute this function from Java (cefbrowser.executeJavascript(...))
The result from
- Code: Select all
app.sendMessage()
With sendMessage I send the name of a function and parameters for this function. This function will be executed and the result will be saved (for setMessageCallback).
With setMessageCallback I send the name of the same function (for identification) and a function:
- Code: Select all
var func = function(name, returnValue)
{
if(callback && typeof(callback) === "function")
{
console.log( name +" = "+ returnValue );
callback( returnValue );
}
else
{
console.log("callback !== function : "+callback);
}
};
callback is a parameter of xyzAdapter and returnValue is result of sendMessage.
I hope, you understand what I mean. If not, feel free to ask
Thanks
Edit:
With CEF3 (not java) we solved it that way:
- Code: Select all
} else if (name == "setMessageCallback") {
// Set a message callback.
if (arguments.size() == 2 && arguments[0]->IsString() &&
arguments[1]->IsFunction()) {
std::string name = arguments[0]->GetStringValue();
CefRefPtr<CefV8Context> context = CefV8Context::GetCurrentContext();
int browser_id = context->GetBrowser()->GetIdentifier();
client_app_->SetMessageCallback(name, browser_id, context,
arguments[1]);
handled = true;
}
and
- Code: Select all
// Execute the registered JavaScript callback if any.
if (!callback_map_.empty()) {
CefString message_name = message->GetName();
CallbackMap::const_iterator it = callback_map_.find(
std::make_pair(message_name.ToString(),
browser->GetIdentifier()));
if (it != callback_map_.end()) {
// Keep a local reference to the objects. The callback may remove itself
// from the callback map.
CefRefPtr<CefV8Context> context = it->second.first;
CefRefPtr<CefV8Value> callback = it->second.second;
// Enter the context.
context->Enter();
CefV8ValueList arguments;
// First argument is the message name.
arguments.push_back(CefV8Value::CreateString(message_name));
// Second argument is the list of message arguments.
CefRefPtr<CefListValue> list = message->GetArgumentList();
CefRefPtr<CefV8Value> args = CefV8Value::CreateArray(list->GetSize());
SetList(list, args);
arguments.push_back(args);
// Execute the callback.
CefRefPtr<CefV8Value> retval = callback->ExecuteFunction(NULL, arguments);
if (retval.get()) {
if (retval->IsBool())
handled = retval->GetBoolValue();
}
// Exit the context.
context->Exit();
}
}