Add CefFrame::GetV8Context() method for retrieving the V8 context of a frame (issue #344).

git-svn-id: https://chromiumembedded.googlecode.com/svn/trunk@345 5089003a-bbd8-11dd-ad1f-f1f9622dbc98
This commit is contained in:
Marshall Greenblatt
2011-10-28 20:30:19 +00:00
parent 5147a4c7a0
commit 0ec9541b78
13 changed files with 154 additions and 16 deletions

View File

@ -14,6 +14,54 @@
#include <stdio.h>
#include <string>
namespace {
void UIT_InvokeScript(CefRefPtr<CefBrowser> browser)
{
REQUIRE_UI_THREAD();
CefRefPtr<CefFrame> frame = browser->GetMainFrame();
CefRefPtr<CefV8Context> v8Context = frame->GetV8Context();
CefString url = frame->GetURL();
if (!v8Context.get()) {
frame->ExecuteJavaScript("alert('Failed to get V8 context!');", url, 0);
} else if (v8Context->Enter()) {
CefRefPtr<CefV8Value> globalObj = v8Context->GetGlobal();
CefRefPtr<CefV8Value> evalFunc = globalObj->GetValue("eval");
CefRefPtr<CefV8Value> arg0 = CefV8Value::CreateString("1+2");
CefV8ValueList args;
args.push_back(arg0);
CefRefPtr<CefV8Value> retVal;
CefString exception;
if (evalFunc->ExecuteFunctionWithContext(v8Context, globalObj, args, retVal,
exception)) {
if (retVal.get()) {
frame->ExecuteJavaScript(
std::string("alert('InvokeScript returns ") +
retVal->GetStringValue().ToString() + "!');",
url, 0);
} else {
frame->ExecuteJavaScript(
std::string("alert('InvokeScript returns exception: ") +
exception.ToString() + "!');",
url, 0);
}
} else {
frame->ExecuteJavaScript("alert('Failed to execute function!');", url, 0);
}
v8Context->Exit();
} else {
frame->ExecuteJavaScript("alert('Failed to enter into V8 context!');",
url, 0);
}
}
} // namespace
CefRefPtr<ClientHandler> g_handler;
@ -102,6 +150,16 @@ void RunJavaScriptExecuteTest(CefRefPtr<CefBrowser> browser)
"alert('JavaScript execute works!');", "about:blank", 0);
}
void RunJavaScriptInvokeTest(CefRefPtr<CefBrowser> browser)
{
if (CefCurrentlyOn(TID_UI)) {
UIT_InvokeScript(browser);
} else {
// Execute on the UI thread.
CefPostTask(TID_UI, NewCefRunnableFunction(&UIT_InvokeScript, browser));
}
}
void RunPopupTest(CefRefPtr<CefBrowser> browser)
{
browser->GetMainFrame()->ExecuteJavaScript(