3

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.

Luboš Turek
  • 5,179
  • 6
  • 39
  • 47
Hzmy
  • 759
  • 7
  • 16

1 Answers1

0

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.

JohanTG
  • 1,304
  • 1
  • 13
  • 19