Support custom V8 bindings on WebWorker threads (issue #451).

- Add CefRenderProcessHandler callbacks for WebWorker context creation, release and uncaught exceptions.
- Add CefTaskRunner class that supports posting of tasks to standard threads and WebWorker threads.
- Organize V8 internal state information on a per-thread/per-Isolate basis.
- Add webkit_451.patch that provides the WebKit implementation.

git-svn-id: https://chromiumembedded.googlecode.com/svn/trunk@972 5089003a-bbd8-11dd-ad1f-f1f9622dbc98
This commit is contained in:
Marshall Greenblatt
2013-01-03 17:24:24 +00:00
parent f30fbb3d48
commit afe3cb1d67
44 changed files with 2434 additions and 406 deletions

View File

@@ -1,4 +1,4 @@
// Copyright (c) 2012 The Chromium Embedded Framework Authors. All rights
// Copyright (c) 2013 The Chromium Embedded Framework Authors. All rights
// reserved. Use of this source code is governed by a BSD-style license that
// can be found in the LICENSE file.
@@ -27,6 +27,8 @@ const char kV8ContextChildTestUrl[] = "http://tests/V8Test.ContextChildTest";
const char kV8NavTestUrl[] = "http://tests/V8Test.NavTest";
const char kV8OnUncaughtExceptionTestUrl[] =
"http://tests/V8Test.OnUncaughtException";
const char kV8WorkerParentTestUrl[] = "http://tests/V8Test.WorkerParent";
const char kV8WorkerTestUrl[] = "http://tests/V8Test.Worker.js";
const char kV8TestMsg[] = "V8Test.Test";
const char kV8TestCmdArg[] = "v8-test";
const char kV8DevToolsURLMsg[] = "V8Test.DevToolsURL";
@@ -68,9 +70,12 @@ enum V8TestMode {
V8TEST_CONTEXT_INVALID,
V8TEST_BINDING,
V8TEST_STACK_TRACE,
V8TEST_EXTENSION,
V8TEST_ON_UNCAUGHT_EXCEPTION,
V8TEST_ON_UNCAUGHT_EXCEPTION_DEV_TOOLS,
V8TEST_EXTENSION,
V8TEST_WORKER_BINDING,
V8TEST_WORKER_ON_UNCAUGHT_EXCEPTION,
V8TEST_WORKER_ON_UNCAUGHT_EXCEPTION_DEV_TOOLS,
};
// Set to the current test being run in the browser process. Will always be
@@ -214,11 +219,16 @@ class V8RendererTest : public ClientApp::RenderDelegate {
RunStackTraceTest();
break;
case V8TEST_ON_UNCAUGHT_EXCEPTION:
case V8TEST_WORKER_ON_UNCAUGHT_EXCEPTION:
RunOnUncaughtExceptionTest();
break;
case V8TEST_ON_UNCAUGHT_EXCEPTION_DEV_TOOLS:
case V8TEST_WORKER_ON_UNCAUGHT_EXCEPTION_DEV_TOOLS:
RunOnUncaughtExceptionDevToolsTest();
break;
case V8TEST_WORKER_BINDING:
// The test is triggered in OnWorkerContextCreated().
break;
default:
// Was a startup test.
EXPECT_TRUE(startup_test_success_);
@@ -1588,32 +1598,6 @@ class V8RendererTest : public ClientApp::RenderDelegate {
"window.open('" + devtools_url_ + "');", "about:blank", 0);
}
void OnUncaughtException(CefRefPtr<ClientApp> app,
CefRefPtr<CefBrowser> browser,
CefRefPtr<CefFrame> frame,
CefRefPtr<CefV8Context> context,
CefRefPtr<CefV8Exception> exception,
CefRefPtr<CefV8StackTrace> stackTrace) OVERRIDE {
if (test_mode_ == V8TEST_ON_UNCAUGHT_EXCEPTION ||
test_mode_ == V8TEST_ON_UNCAUGHT_EXCEPTION_DEV_TOOLS) {
EXPECT_TRUE(test_context_->IsSame(context));
EXPECT_STREQ("Uncaught ReferenceError: asd is not defined",
exception->GetMessage().ToString().c_str());
std::ostringstream stackFormatted;
for (int i = 0; i < stackTrace->GetFrameCount(); ++i) {
stackFormatted << "at "
<< stackTrace->GetFrame(i)->GetFunctionName().ToString()
<< "() in " << stackTrace->GetFrame(i)->GetScriptName().ToString()
<< " on line " << stackTrace->GetFrame(i)->GetLineNumber() << "\n";
}
const char* stackFormattedShouldBe =
"at test2() in http://tests/V8Test.OnUncaughtException on line 3\n"
"at test() in http://tests/V8Test.OnUncaughtException on line 2\n";
EXPECT_STREQ(stackFormattedShouldBe, stackFormatted.str().c_str());
DestroyTest();
}
}
// Test execution of a native function when the extension is loaded.
void RunExtensionTest() {
std::string code = "native function v8_extension_test();"
@@ -1665,7 +1649,8 @@ class V8RendererTest : public ClientApp::RenderDelegate {
if (test_mode_ == V8TEST_NONE)
return;
if (test_mode_ == V8TEST_ON_UNCAUGHT_EXCEPTION_DEV_TOOLS) {
if (test_mode_ == V8TEST_ON_UNCAUGHT_EXCEPTION_DEV_TOOLS ||
test_mode_ == V8TEST_WORKER_ON_UNCAUGHT_EXCEPTION_DEV_TOOLS) {
if (browser_.get() == NULL) {
app_ = app;
browser_ = browser;
@@ -1758,12 +1743,217 @@ class V8RendererTest : public ClientApp::RenderDelegate {
}
}
void OnUncaughtException(CefRefPtr<ClientApp> app,
CefRefPtr<CefBrowser> browser,
CefRefPtr<CefFrame> frame,
CefRefPtr<CefV8Context> context,
CefRefPtr<CefV8Exception> exception,
CefRefPtr<CefV8StackTrace> stackTrace) OVERRIDE {
if (test_mode_ == V8TEST_NONE)
return;
if (test_mode_ == V8TEST_ON_UNCAUGHT_EXCEPTION ||
test_mode_ == V8TEST_ON_UNCAUGHT_EXCEPTION_DEV_TOOLS) {
EXPECT_TRUE(test_context_->IsSame(context));
EXPECT_STREQ("Uncaught ReferenceError: asd is not defined",
exception->GetMessage().ToString().c_str());
std::ostringstream stackFormatted;
for (int i = 0; i < stackTrace->GetFrameCount(); ++i) {
stackFormatted << "at "
<< stackTrace->GetFrame(i)->GetFunctionName().ToString()
<< "() in " << stackTrace->GetFrame(i)->GetScriptName().ToString()
<< " on line " << stackTrace->GetFrame(i)->GetLineNumber() << "\n";
}
const char* stackFormattedShouldBe =
"at test2() in http://tests/V8Test.OnUncaughtException on line 3\n"
"at test() in http://tests/V8Test.OnUncaughtException on line 2\n";
EXPECT_STREQ(stackFormattedShouldBe, stackFormatted.str().c_str());
DestroyTest();
}
}
virtual void OnWorkerContextCreated(
CefRefPtr<ClientApp> app,
int worker_id,
const CefString& url,
CefRefPtr<CefV8Context> context) OVERRIDE {
if (test_mode_ == V8TEST_NONE)
return;
EXPECT_STREQ(kV8WorkerTestUrl, url.ToString().c_str());
EXPECT_TRUE(context.get());
EXPECT_TRUE(context->IsValid());
test_context_worker_ = context;
CefRefPtr<CefTaskRunner> task_runner = context->GetTaskRunner();
EXPECT_TRUE(task_runner.get());
EXPECT_TRUE(task_runner->BelongsToCurrentThread());
test_task_runner_worker_ = task_runner;
EXPECT_FALSE(context->GetBrowser().get());
EXPECT_FALSE(context->GetFrame().get());
if (test_mode_ == V8TEST_WORKER_BINDING) {
static const char test_func[] = "run_async";
class Handler : public CefV8Handler {
public:
Handler() {}
virtual bool Execute(const CefString& name,
CefRefPtr<CefV8Value> object,
const CefV8ValueList& arguments,
CefRefPtr<CefV8Value>& retval,
CefString& exception) OVERRIDE {
EXPECT_STREQ(test_func, name.ToString().c_str());
EXPECT_EQ((size_t)1, arguments.size());
callback_ = arguments[0];
EXPECT_TRUE(callback_->IsFunction());
EXPECT_TRUE(context_->IsValid());
CefRefPtr<CefV8Context> current_context =
CefV8Context::GetCurrentContext();
EXPECT_TRUE(current_context->IsSame(context_));
CefRefPtr<CefTaskRunner> task_runner =
current_context->GetTaskRunner();
EXPECT_TRUE(task_runner.get());
EXPECT_TRUE(task_runner->BelongsToCurrentThread());
EXPECT_TRUE(task_runner->IsSame(task_runner_));
// Execute the callback asynchronously.
task_runner->PostTask(
NewCefRunnableMethod(this, &Handler::ExecCallback));
retval = CefV8Value::CreateBool(true);
return true;
}
void ExecCallback() {
EXPECT_TRUE(callback_->IsValid());
EXPECT_TRUE(context_->IsValid());
CefRefPtr<CefTaskRunner> task_runner = context_->GetTaskRunner();
EXPECT_TRUE(task_runner.get());
EXPECT_TRUE(task_runner->BelongsToCurrentThread());
EXPECT_TRUE(task_runner->IsSame(task_runner_));
CefRefPtr<CefV8Value> retval =
callback_->ExecuteFunctionWithContext(context_, NULL,
CefV8ValueList());
EXPECT_TRUE(retval.get());
EXPECT_TRUE(retval->IsValid());
EXPECT_TRUE(retval->IsBool());
EXPECT_EQ(true, retval->GetBoolValue());
callback_ = NULL;
context_ = NULL;
task_runner_ = NULL;
}
CefRefPtr<CefV8Value> callback_;
CefRefPtr<CefV8Context> context_;
CefRefPtr<CefTaskRunner> task_runner_;
IMPLEMENT_REFCOUNTING(Handler);
};
Handler* handler = new Handler();
CefRefPtr<CefV8Handler> handlerPtr(handler);
handler->context_ = context;
handler->task_runner_ = task_runner;
CefRefPtr<CefV8Value> global = context->GetGlobal();
EXPECT_TRUE(global.get());
global->SetValue(test_func,
CefV8Value::CreateFunction(test_func, handlerPtr),
V8_PROPERTY_ATTRIBUTE_NONE);
}
}
virtual void OnWorkerContextReleased(
CefRefPtr<ClientApp> app,
int worker_id,
const CefString& url,
CefRefPtr<CefV8Context> context) OVERRIDE {
if (test_mode_ == V8TEST_NONE)
return;
EXPECT_STREQ(kV8WorkerTestUrl, url.ToString().c_str());
EXPECT_TRUE(context.get());
EXPECT_TRUE(context->IsValid());
CefRefPtr<CefTaskRunner> task_runner = context->GetTaskRunner();
EXPECT_TRUE(task_runner.get());
EXPECT_TRUE(task_runner->BelongsToCurrentThread());
EXPECT_TRUE(task_runner->IsSame(test_task_runner_worker_));
EXPECT_TRUE(context->IsSame(test_context_worker_));
test_context_worker_ = NULL;
test_task_runner_worker_ = NULL;
if (test_mode_ == V8TEST_WORKER_BINDING) {
// Destroy the test on the renderer thread.
CefPostTask(TID_RENDERER,
NewCefRunnableMethod(this, &V8RendererTest::DestroyTest));
}
}
void OnWorkerUncaughtException(
CefRefPtr<ClientApp> app,
int worker_id,
const CefString& url,
CefRefPtr<CefV8Context> context,
CefRefPtr<CefV8Exception> exception,
CefRefPtr<CefV8StackTrace> stackTrace) OVERRIDE {
if (test_mode_ == V8TEST_NONE)
return;
EXPECT_TRUE(context.get());
EXPECT_TRUE(context->IsValid());
CefRefPtr<CefTaskRunner> task_runner = context->GetTaskRunner();
EXPECT_TRUE(task_runner.get());
EXPECT_TRUE(task_runner->BelongsToCurrentThread());
EXPECT_TRUE(task_runner->IsSame(test_task_runner_worker_));
EXPECT_TRUE(context->IsSame(test_context_worker_));
if (test_mode_ == V8TEST_WORKER_ON_UNCAUGHT_EXCEPTION ||
test_mode_ == V8TEST_WORKER_ON_UNCAUGHT_EXCEPTION_DEV_TOOLS) {
EXPECT_STREQ("Uncaught ReferenceError: asd is not defined",
exception->GetMessage().ToString().c_str());
std::ostringstream stackFormatted;
for (int i = 0; i < stackTrace->GetFrameCount(); ++i) {
stackFormatted << "at "
<< stackTrace->GetFrame(i)->GetFunctionName().ToString()
<< "() in " << stackTrace->GetFrame(i)->GetScriptName().ToString()
<< " on line " << stackTrace->GetFrame(i)->GetLineNumber() << "\n";
}
const char* stackFormattedShouldBe =
"at test2() in http://tests/V8Test.Worker.js on line 2\n"
"at test() in http://tests/V8Test.Worker.js on line 1\n";
EXPECT_STREQ(stackFormattedShouldBe, stackFormatted.str().c_str());
// Destroy the test on the renderer thread.
CefPostTask(TID_RENDERER,
NewCefRunnableMethod(this, &V8RendererTest::DestroyTest));
}
}
virtual void OnBrowserDestroyed(CefRefPtr<ClientApp> app,
CefRefPtr<CefBrowser> browser) OVERRIDE {
if (test_mode_ == V8TEST_NONE)
return;
if (test_mode_ == V8TEST_ON_UNCAUGHT_EXCEPTION_DEV_TOOLS) {
if (test_mode_ == V8TEST_ON_UNCAUGHT_EXCEPTION_DEV_TOOLS ||
test_mode_ == V8TEST_WORKER_ON_UNCAUGHT_EXCEPTION_DEV_TOOLS) {
if (browser->IsPopup()) {
// After window destruction there is still a call to
// ScriptController::setCaptureCallStackForUncaughtExceptions(0),
@@ -1782,7 +1972,8 @@ class V8RendererTest : public ClientApp::RenderDelegate {
if (test_mode_ == V8TEST_NONE)
return false;
if (test_mode_ == V8TEST_ON_UNCAUGHT_EXCEPTION_DEV_TOOLS) {
if (test_mode_ == V8TEST_ON_UNCAUGHT_EXCEPTION_DEV_TOOLS ||
test_mode_ == V8TEST_WORKER_ON_UNCAUGHT_EXCEPTION_DEV_TOOLS) {
EXPECT_TRUE(browser.get());
EXPECT_EQ(PID_BROWSER, source_process);
EXPECT_TRUE(message.get());
@@ -1883,6 +2074,8 @@ class V8RendererTest : public ClientApp::RenderDelegate {
protected:
// Return from the test.
void DestroyTest() {
EXPECT_TRUE(CefCurrentlyOn(TID_RENDERER));
if (test_mode_ == V8TEST_CONTEXT_INVALID) {
// Verify that objects related to a particular context are not valid after
// OnContextReleased is called for that context.
@@ -1921,6 +2114,9 @@ class V8RendererTest : public ClientApp::RenderDelegate {
CefRefPtr<CefV8Context> test_context_;
CefRefPtr<CefV8Value> test_object_;
CefRefPtr<CefV8Context> test_context_worker_;
CefRefPtr<CefTaskRunner> test_task_runner_worker_;
// Used by startup tests to indicate success.
TrackCallback startup_test_success_;
@@ -1957,6 +2153,28 @@ class V8TestHandler : public TestHandler {
"</body></html>\n",
"text/html");
CreateBrowser(kV8OnUncaughtExceptionTestUrl);
} else if (test_mode_ == V8TEST_WORKER_BINDING) {
AddResource(kV8WorkerParentTestUrl, "<html><body>"
"<script>var worker = new Worker('V8Test.Worker.js');</script>"
"</body></html>", "text/html");
AddResource(kV8WorkerTestUrl,
"function cb() { self.close(); return true; }\n"
"self.run_async(cb);",
"application/javascript");
CreateBrowser(kV8WorkerParentTestUrl);
} else if (test_mode_ == V8TEST_WORKER_ON_UNCAUGHT_EXCEPTION ||
test_mode_ == V8TEST_WORKER_ON_UNCAUGHT_EXCEPTION_DEV_TOOLS) {
AddResource(kV8WorkerParentTestUrl, "<html><body>"
"<script>var worker = new Worker('V8Test.Worker.js');\n"
"function test() { worker.postMessage('test'); }\n"
"</script>"
"</body></html>", "text/html");
AddResource(kV8WorkerTestUrl,
"function test(){ test2(); }\n"
"function test2(){ asd(); }\n"
"self.onmessage = function(event) { self.setTimeout(test, 0); }",
"application/javascript");
CreateBrowser(kV8WorkerParentTestUrl);
} else {
EXPECT_TRUE(test_url_ != NULL);
AddResource(test_url_, "<html><body>"
@@ -1968,7 +2186,8 @@ class V8TestHandler : public TestHandler {
virtual void OnLoadEnd(CefRefPtr<CefBrowser> browser,
CefRefPtr<CefFrame> frame,
int httpStatusCode) OVERRIDE {
if (test_mode_ == V8TEST_ON_UNCAUGHT_EXCEPTION_DEV_TOOLS) {
if (test_mode_ == V8TEST_ON_UNCAUGHT_EXCEPTION_DEV_TOOLS ||
test_mode_ == V8TEST_WORKER_ON_UNCAUGHT_EXCEPTION_DEV_TOOLS) {
if (browser->IsPopup()) {
EXPECT_STREQ(
GetBrowser()->GetHost()->GetDevToolsURL(true).ToString().c_str(),
@@ -2083,3 +2302,7 @@ V8_TEST(StackTrace, V8TEST_STACK_TRACE);
V8_TEST(OnUncaughtException, V8TEST_ON_UNCAUGHT_EXCEPTION);
V8_TEST(OnUncaughtExceptionDevTools, V8TEST_ON_UNCAUGHT_EXCEPTION_DEV_TOOLS);
V8_TEST(Extension, V8TEST_EXTENSION);
V8_TEST(WorkerBinding, V8TEST_WORKER_BINDING);
V8_TEST(WorkerOnUncaughtException, V8TEST_WORKER_ON_UNCAUGHT_EXCEPTION);
V8_TEST(WorkerOnUncaughtExceptionDevTools,
V8TEST_WORKER_ON_UNCAUGHT_EXCEPTION_DEV_TOOLS);