铬嵌入式框架:使用“ ExecuteFunctionWithContext”时创建对象失败。 [英] Chromium embedded framework: Creating an object fails when using "ExecuteFunctionWithContext"

查看:170
本文介绍了铬嵌入式框架:使用“ ExecuteFunctionWithContext”时创建对象失败。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

概述



我正在Delphi 2009上使用铬嵌入式框架(cef),这是最新版本。



错误



我可以使用 ExecuteFunctionWithContext 成功执行JavaScript回调例程,我可以为其提供参数。
但是,当我尝试使用 TCefv8ValueRef.CreateObject(nil); 时, libcef中会发生访问冲突 em>库。



假设


  1. 推动JavaScript回调的参数适用于 TCefv8ValueRef.CreateString 和所有其他 Cefv8Value 类型。

  2. TCefv8ValueRef.CreateObject(nil)函数在用作铬扩展的返回值时可以正常工作。 (如Delphi CEF的 / demos / guiclient 演示中所述)。

  3. TChromium 对象保留在主窗体上。

可能的解决方案和缪斯


  1. 我尝试使用 TCefv8ValueRef.CreateObject(nil); 通过主窗体上的 OnClick 事件,这也会产生访问冲突。但是, TCefv8ValueRef.CreateString('test'); 可以正常工作。

任何帮助将不胜感激。

解决方案

我在C ++上遇到了同样的问题!我已经用以下代码解决了它:

  CefRefPtr< CefFrame>框架=浏览器-> GetMainFrame(); 
CefRefPtr< CefV8Context> v8Context =框架-> GetV8Context();
if(v8Context.get()&& v8Context-> Enter())
{
CefRefPtr< CefV8Value>对象= CefV8Value :: CreateObject(NULL);
// ExecuteFunctionWithContext和其他操作

v8Context-> Exit();
}

嵌入铬的文档包含以下内容:


因此,在使用javascript模型进行操作之前,应打开正确的连接。
如果V8当前不在上下文中,或者需要检索和存储对上下文的引用,则可以使用两个可用的CefV8Context静态方法之一。 GetCurrentContext()返回当前正在执行JS的框架的上下文。 GetEnteredContext()返回JS执行开始的框架的上下文。例如,如果frame1中的函数调用frame2中的函数,则当前上下文将为frame2,输入的上下文将为frame1。



数组,对象和函数只能如果V8在上下文中,则可以创建,修改和执行(就功能而言)执行。如果V8不在上下文中,则应用程序需要通过调用Enter()进入上下文并通过调用Exit()退出上下文。仅应使用Enter()和Exit()方法:


  1. 在外部创建V8对象,函数或数组时现有环境。例如,当创建JS对象以响应本机菜单回调时。


  2. 在创建V8对象,函数或数组时,除当前上下文外的上下文中上下文。例如,如果源自frame1的呼叫需要修改frame2的上下文。



所以这就是为什么您无法创建对象但能够创建js字符串的原因。
您还可以看到常规用法示例。 p>

Overview

I am using chromium embedded framework (cef) on Delphi 2009, it is the latest release.

Error

I can use ExecuteFunctionWithContext to successfully execute a JavaScript callback routine and I can supply it with arguments. However, when I try and use TCefv8ValueRef.CreateObject(nil); an access violation occurs in the libcef library.

Assumptions

  1. Pushing an argument to the JavaScript callback works for TCefv8ValueRef.CreateString and all the other Cefv8Value types.
  2. The TCefv8ValueRef.CreateObject(nil) function works fine when it is used as a return value for a chromium extension. (As detailed in the /demos/guiclient demo for the Delphi CEF).
  3. The TChromium object is held on the main form.

Possible Solutions and Musings

  1. I have tried using TCefv8ValueRef.CreateObject(nil); via an OnClick event on the main form, this also produced a access violation. However, TCefv8ValueRef.CreateString('test'); will work fine.

Any help would be greatly appreciated.

解决方案

I had the same problem on C++! And I have solved it with following code:

CefRefPtr<CefFrame> frame = browser->GetMainFrame();
CefRefPtr<CefV8Context> v8Context = frame->GetV8Context();
if (v8Context.get() && v8Context->Enter())
{
    CefRefPtr<CefV8Value> object = CefV8Value::CreateObject(NULL);
    // ExecuteFunctionWithContext and other actions

    v8Context->Exit();
}

The chromiumembedded documentation contains following:

So you should switch on the right contect before your actions with javascript model. If V8 is not currently inside a context, or if you need to retrieve and store a reference to a context, you can use one of two available CefV8Context static methods. GetCurrentContext() returns the context for the frame that is currently executing JS. GetEnteredContext() returns the context for the frame where JS execution began. For example, if a function in frame1 calls a function in frame2 then the current context will be frame2 and the entered context will be frame1.

Arrays, objects and functions may only be created, modified and, in the case of functions, executed, if V8 is inside a context. If V8 is not inside a context then the application needs to enter a context by calling Enter() and exit the context by calling Exit(). The Enter() and Exit() methods should only be used:

  1. When creating a V8 object, function or array outside of an existing context. For example, when creating a JS object in response to a native menu callback.

  2. When creating a V8 object, function or array in a context other than the current context. For example, if a call originating from frame1 needs to modify the context of frame2.

So that's why you couldn't create an object but was able to create js strings. Also you could see the general usage example.

这篇关于铬嵌入式框架:使用“ ExecuteFunctionWithContext”时创建对象失败。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆