我有一个嵌入式框架(CEF(应用程序,在该应用程序中,我们希望在client-javascript侧与浏览器侧进行通信。因此,我们有可能使用通用消息路由器GenericMessagerouter:然后,客户端执行类似的操作以将消息发送到浏览器线程:
var request_id = window.cefQuery({
request: 'my_request',
persistent: false,
onSuccess: function(response) {},
onFailure: function(error_code, error_message) {}
});
// which will be receiven by the browser with
bool OnQuery(CefRefPtr<CefBrowser> browser,
CefRefPtr<CefFrame> frame,
int64 query_id,
const CefString& request,
bool persistent,
CefRefPtr<Callback> callback)
我想知道,如果我们想发送双向二进制数据(例如,使用Google协议缓冲区序列化数据(,哪种解决方案将是最好的。据我所知,ArrayBuffer
在CEF中没有真正支持,这有点不幸。我们还可以滥用CefString
还是这是一个坏主意?
这是完美的手册https://bitbucket.org/chromiumiumembedded/cef/wiki/javascriptintegration
扩展就像窗口绑定一样,除了将它们加载到每个帧的上下文中,并且一旦加载就无法修改。加载扩展时不存在DOM,并且在扩展加载过程中尝试访问DOM的尝试将导致崩溃。使用CefregisterExtension((函数注册扩展名,该函数应从CefrenderProcessHandler :: onwebkitinitialized((方法。
。JS功能
CEF支持使用本机实现的JS函数的创建。使用CEFV8VALUE :: CreateFunction((静态方法创建功能,该方法接受名称和CEFV8HANDLER参数。函数只能在上下文中创建和使用(有关更多信息,请参见"使用上下文"部分。
。CefRefPtr<CefV8Handler> handler = …;
CefRefPtr<CefV8Value> func = CefV8Value::CreateFunction("myfunc", handler);
客户应用程序必须提供的CEFV8HANDLER接口的实现。
class MyV8Handler : public CefV8Handler {
public:
MyV8Handler() {}
virtual bool Execute(const CefString& name,
CefRefPtr<CefV8Value> object,
const CefV8ValueList& arguments,
CefRefPtr<CefV8Value>& retval,
CefString& exception) OVERRIDE {
if (name == "myfunc") {
// Extract argument values
// ...
// Do work
// ...
// Return my string value.
retval = CefV8Value::CreateString("My Value!");
return true;
}
// Function does not exist.
return false;
}
// Provide the reference counting implementation for this class.
IMPLEMENT_REFCOUNTING(MyV8Handler);
};
功能和窗口绑定
函数可用于创建复杂的窗口绑定。
void MyRenderProcessHandler::OnContextCreated(
CefRefPtr<CefBrowser> browser,
CefRefPtr<CefFrame> frame,
CefRefPtr<CefV8Context> context) {
// Retrieve the context's window object.
CefRefPtr<CefV8Value> object = context->GetGlobal();
// Create an instance of my CefV8Handler object.
CefRefPtr<CefV8Handler> handler = new MyV8Handler();
// Create the "myfunc" function.
CefRefPtr<CefV8Value> func = CefV8Value::CreateFunction("myfunc", handler);
// Add the "myfunc" function to the "window" object.
object->SetValue("myfunc", func, V8_PROPERTY_ATTRIBUTE_NONE);
}
在JavaScript中致电
<script language="JavaScript">
alert(window.myfunc(myJSON)); // Calls the function "myFunc" and passes the JSON through function argument
</script>
功能和扩展
函数可用于创建复杂的扩展。请注意使用"本地函数"正向声明的使用。
void MyRenderProcessHandler::OnWebKitInitialized() {
// Define the extension contents.
std::string extensionCode =
"var test;"
"if (!test)"
" test = {};"
"(function() {"
" test.myfunc = function(json) {"
" native function myfunc();"
" return myfunc(json);"
" };"
"})();";
// Create an instance of my CefV8Handler object.
CefRefPtr<CefV8Handler> handler = new MyV8Handler();
// Register the extension.
CefRegisterExtension("v8/test", extensionCode, handler);
}
在JavaScript中致电
<script language="JavaScript">
alert(test.myfunc(myJSON)); // Calls the function "myFunc" and passes the JSON through function argument
</script>
Google Protobuf不适合JS和本机功能之间的交易。使用JSON。可以将JS对象透明地序列化为JSON,反之亦然。