When it is safe to call CefFrame::ExecuteJavascript()

Do not post support requests, bug reports or feature requests. Discuss CEF here. Non-CEF related discussion goes in General Discussion!

When it is safe to call CefFrame::ExecuteJavascript()

Postby Mayhew » Wed Aug 13, 2014 2:24 pm

I have been looking over the JavaScript integration docs and need to clarify something.

I want to call CefFrame::ExecuteJavascript() to run some code in the main browser frame. I want to call ExecuteJavascript() from within my override of CefApp::OnProcessMessageReceived(). The docs state " The ExecuteFunction() method should only be used if V8 is already inside a context as described in the "Working with Contexts" section" and Working with Contexts states "V8 will be inside a context if the current code location has a CefV8Handler, CefV8Accessor or OnContextCreated()/OnContextReleased() callback higher in the call stack."

OnProcessMessageReceived() doesn't fall into any of the above categories so I assume it is unsafe to call CefFrame::ExecuteJavascript() there unless I
1) call GetCurrentContext() and ensure that the context is for the main frame, but I'm not sure what I do if it is not the context for the main frame.
2) Simply keep set a member boolean to true in my CefApps OnContextCreated() method when the context for the main frame is created. I set this boolean to false when OnContextReleased() is called with the main frames context. If that boolean is true, I can assume it is safe to call ExecuteJavascript() within my OnProcessMessageReceived() method.

Currently, I'm doing neither of the above and it is working, but I assume I'm just lucky. So, in order to make this bulletproof are either option above the right approach?

Thanks,
John
Mayhew
Expert
 
Posts: 303
Joined: Mon Apr 18, 2011 8:02 pm

Re: When it is safe to call CefFrame::ExecuteJavascript()

Postby magreenblatt » Thu Aug 14, 2014 4:31 am

It is safe to use ExecuteJavaScript. The context requirements only apply to CefV8* classes.
magreenblatt
Site Admin
 
Posts: 12409
Joined: Fri May 29, 2009 6:57 pm

Re: When it is safe to call CefFrame::ExecuteJavascript()

Postby Mayhew » Thu Aug 14, 2014 5:48 pm

Okay, thanks. That is good to know. It may be worth specifying that CefFrame::ExecuteJavascript() is always safe in the wiki as the wording "The ExecuteFunction() method should only be used if V8 is already inside a context" is a bit ambiguous. Should I file an issue for that?
Mayhew
Expert
 
Posts: 303
Joined: Mon Apr 18, 2011 8:02 pm

Re: When it is safe to call CefFrame::ExecuteJavascript()

Postby magreenblatt » Fri Aug 15, 2014 4:36 am

Mayhew wrote:That is good to know. It may be worth specifying that CefFrame::ExecuteJavascript() is always safe in the wiki as the wording "The ExecuteFunction() method should only be used if V8 is already inside a context" is a bit ambiguous.

Thanks for the suggestion, I've updated the documentation :).
magreenblatt
Site Admin
 
Posts: 12409
Joined: Fri May 29, 2009 6:57 pm


Return to CEF Discussion

Who is online

Users browsing this forum: No registered users and 41 guests