Eliminate the CefBrowser::HandleJSBinding() method to avoid memory leaks (issue #72).

git-svn-id: https://chromiumembedded.googlecode.com/svn/trunk@73 5089003a-bbd8-11dd-ad1f-f1f9622dbc98
This commit is contained in:
Marshall Greenblatt 2010-04-07 14:45:40 +00:00
parent 669244ca18
commit 6a0f9a6d40
19 changed files with 111 additions and 481 deletions

View File

@ -605,13 +605,6 @@ public:
virtual RetVal HandleTakeFocus(CefRefPtr<CefBrowser> browser,
bool reverse) =0;
// Event called for adding values to a frame's JavaScript 'window' object. The
// return value is currently ignored.
/*--cef()--*/
virtual RetVal HandleJSBinding(CefRefPtr<CefBrowser> browser,
CefRefPtr<CefFrame> frame,
CefRefPtr<CefV8Value> object) =0;
// Called when the browser component is requesting focus. |isWidget| will be
// true if the focus is requested for a child widget of the browser window.
// Return RV_CONTINUE to allow the focus to be set or RV_HANDLED to cancel
@ -898,9 +891,8 @@ class CefV8Value : public CefBase
{
public:
// Create a new CefV8Value object of the specified type. These methods
// should only be called from within the JavaScript context -- either in a
// CefV8Handler::Execute() callback or a CefHandler::HandleJSBinding()
// callback.
// should only be called from within the JavaScript context in a
// CefV8Handler::Execute() callback.
/*--cef()--*/
static CefRefPtr<CefV8Value> CreateUndefined();
/*--cef()--*/

View File

@ -468,12 +468,6 @@ typedef struct _cef_handler_t
struct _cef_handler_t* self, struct _cef_browser_t* browser,
int reverse);
// Event called for adding values to a frame's JavaScript 'window' object. The
// return value is currently ignored.
enum cef_retval_t (CEF_CALLBACK *handle_jsbinding)(
struct _cef_handler_t* self, struct _cef_browser_t* browser,
struct _cef_frame_t* frame, struct _cef_v8value_t* object);
// Called when the browser component is requesting focus. |isWidget| will be
// true (1) if the focus is requested for a child widget of the browser
// window. Return RV_CONTINUE to allow the focus to be set or RV_HANDLED to
@ -831,9 +825,8 @@ typedef struct _cef_v8value_t
// Create a new cef_v8value_t object of the specified type. These functions
// should only be called from within the JavaScript context -- either in a
// cef_v8handler_t::execute() callback or a cef_handler_t::handle_jsbinding()
// callback.
// should only be called from within the JavaScript context in a
// cef_v8handler_t::execute() callback.
CEF_EXPORT cef_v8value_t* cef_v8value_create_undefined();
CEF_EXPORT cef_v8value_t* cef_v8value_create_null();
CEF_EXPORT cef_v8value_t* cef_v8value_create_bool(int value);

View File

@ -183,13 +183,6 @@ void InitializeTextEncoding() {
WebCore::UTF8Encoding();
}
v8::Handle<v8::Context> GetV8Context(WebKit::WebFrame* frame)
{
WebFrameImpl* webFrameImpl = static_cast<WebFrameImpl*>(frame);
WebCore::Frame* core_frame = webFrameImpl->frame();
return WebCore::V8Proxy::context(core_frame);
}
void CloseIdleConnections() {
// Used in benchmarking, Ignored for CEF.
}

View File

@ -33,9 +33,6 @@ void InitializeTextEncoding();
// This is called indirectly by the network layer to access resources.
base::StringPiece NetResourceProvider(int key);
// Retrieve the V8 context associated with the frame.
v8::Handle<v8::Context> GetV8Context(WebKit::WebFrame* frame);
// Clear all cached data.
void ClearCache();

View File

@ -634,22 +634,6 @@ void BrowserWebViewDelegate::didCommitProvisionalLoad(
}
}
void BrowserWebViewDelegate::didClearWindowObject(WebFrame* frame) {
CefRefPtr<CefHandler> handler = browser_->GetHandler();
if(handler.get()) {
v8::HandleScope handle_scope;
v8::Handle<v8::Context> context = webkit_glue::GetV8Context(frame);
if(context.IsEmpty())
return;
v8::Context::Scope scope(context);
CefRefPtr<CefFrame> cframe(browser_->GetCefFrame(frame));
CefRefPtr<CefV8Value> object = new CefV8ValueImpl(context->Global());
handler->HandleJSBinding(browser_, cframe, object);
}
}
void BrowserWebViewDelegate::didReceiveTitle(
WebFrame* frame, const WebString& title) {
std::wstring wtitle = UTF16ToWideHack(title);

View File

@ -144,7 +144,6 @@ class BrowserWebViewDelegate : public WebKit::WebViewClient,
WebKit::WebFrame*, const WebKit::WebURLError&);
virtual void didCommitProvisionalLoad(
WebKit::WebFrame*, bool is_new_navigation);
virtual void didClearWindowObject(WebKit::WebFrame*);
virtual void didReceiveTitle(
WebKit::WebFrame*, const WebKit::WebString& title);
virtual void didFailLoad(

View File

@ -431,22 +431,6 @@ enum cef_retval_t CEF_CALLBACK handler_handle_take_focus(
CefBrowserCToCpp::Wrap(browser), (reverse ? true : false));
}
enum cef_retval_t CEF_CALLBACK handler_handle_jsbinding(
struct _cef_handler_t* self, cef_browser_t* browser, cef_frame_t* frame,
struct _cef_v8value_t* object)
{
DCHECK(self);
DCHECK(browser);
DCHECK(frame);
DCHECK(object);
if(!self || !browser || !frame || !object)
return RV_CONTINUE;
return CefHandlerCppToC::Get(self)->HandleJSBinding(
CefBrowserCToCpp::Wrap(browser), CefFrameCToCpp::Wrap(frame),
CefV8ValueCToCpp::Wrap(object));
}
enum cef_retval_t CEF_CALLBACK handler_handle_set_focus(
struct _cef_handler_t* self, cef_browser_t* browser, int isWidget)
{
@ -501,7 +485,6 @@ CefHandlerCppToC::CefHandlerCppToC(CefHandler* cls)
struct_.struct_.handle_before_window_close =
handler_handle_before_window_close;
struct_.struct_.handle_take_focus = handler_handle_take_focus;
struct_.struct_.handle_jsbinding = handler_handle_jsbinding;
struct_.struct_.handle_set_focus = handler_handle_set_focus;
struct_.struct_.handle_key_event = handler_handle_key_event;
}

View File

@ -328,17 +328,6 @@ CefHandler::RetVal CefHandlerCToCpp::HandleTakeFocus(
reverse);
}
CefHandler::RetVal CefHandlerCToCpp::HandleJSBinding(
CefRefPtr<CefBrowser> browser, CefRefPtr<CefFrame> frame,
CefRefPtr<CefV8Value> object)
{
if(CEF_MEMBER_MISSING(struct_, handle_jsbinding))
return RV_CONTINUE;
return struct_->handle_jsbinding(struct_, CefBrowserCppToC::Wrap(browser),
CefFrameCppToC::Wrap(frame), CefV8ValueCppToC::Wrap(object));
}
CefHandler::RetVal CefHandlerCToCpp::HandleSetFocus(
CefRefPtr<CefBrowser> browser, bool isWidget)
{

View File

@ -73,8 +73,6 @@ public:
const std::wstring& defaultValue, bool& retval, std::wstring& result);
virtual RetVal HandleBeforeWindowClose(CefRefPtr<CefBrowser> browser);
virtual RetVal HandleTakeFocus(CefRefPtr<CefBrowser> browser, bool reverse);
virtual RetVal HandleJSBinding(CefRefPtr<CefBrowser> browser,
CefRefPtr<CefFrame> frame, CefRefPtr<CefV8Value> object);
virtual RetVal HandleSetFocus(CefRefPtr<CefBrowser> browser, bool isWidget);
virtual RetVal HandleKeyEvent(CefRefPtr<CefBrowser> browser,
KeyEventType type, int code, int modifiers, bool isSystemKey);

View File

@ -1,173 +0,0 @@
// Copyright (c) 2008-2009 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.
#include "binding_test.h"
#include <sstream>
// Implementation of the V8 handler class for the "window.cef_test.Dump"
// function.
class ClientV8FunctionHandler : public CefThreadSafeBase<CefV8Handler>
{
public:
ClientV8FunctionHandler() {}
virtual ~ClientV8FunctionHandler() {}
// Execute with the specified argument list and return value. Return true if
// the method was handled.
virtual bool Execute(const std::wstring& name,
CefRefPtr<CefV8Value> object,
const CefV8ValueList& arguments,
CefRefPtr<CefV8Value>& retval,
std::wstring& exception)
{
if(name == L"Dump")
{
// The "Dump" function will return a human-readable dump of the input
// arguments.
std::wstringstream stream;
size_t i;
for(i = 0; i < arguments.size(); ++i)
{
stream << L"arg[" << i << L"] = ";
PrintValue(arguments[i], stream, 0);
stream << L"\n";
}
retval = CefV8Value::CreateString(stream.str());
return true;
}
else if(name == L"Call")
{
// The "Call" function will execute a function to get an object and then
// return the result of calling a function belonging to that object. The
// first arument is the function that will return an object and the second
// argument is the function that will be called on that returned object.
int argSize = arguments.size();
if(argSize < 2 || !arguments[0]->IsFunction()
|| !arguments[1]->IsString())
return false;
CefV8ValueList argList;
// Execute the function stored in the first argument to retrieve an
// object.
CefRefPtr<CefV8Value> objectPtr;
if(!arguments[0]->ExecuteFunction(object, argList, objectPtr, exception))
return false;
// Verify that the returned value is an object.
if(!objectPtr.get() || !objectPtr->IsObject())
return false;
// Retrieve the member function specified by name in the second argument
// from the object.
CefRefPtr<CefV8Value> funcPtr =
objectPtr->GetValue(arguments[1]->GetStringValue());
// Verify that the returned value is a function.
if(!funcPtr.get() || !funcPtr->IsFunction())
return false;
// Pass any additional arguments on to the member function.
for(int i = 2; i < argSize; ++i)
argList.push_back(arguments[i]);
// Execute the member function.
return funcPtr->ExecuteFunction(arguments[0], argList, retval, exception);
}
return false;
}
// Simple function for formatted output of a V8 value.
void PrintValue(CefRefPtr<CefV8Value> value, std::wstringstream &stream,
int indent)
{
std::wstringstream indent_stream;
for(int i = 0; i < indent; ++i)
indent_stream << L" ";
std::wstring indent_str = indent_stream.str();
if(value->IsUndefined())
stream << L"(undefined)";
else if(value->IsNull())
stream << L"(null)";
else if(value->IsBool())
stream << L"(bool) " << (value->GetBoolValue() ? L"true" : L"false");
else if(value->IsInt())
stream << L"(int) " << value->GetIntValue();
else if(value->IsDouble())
stream << L"(double) " << value->GetDoubleValue();
else if(value->IsString())
stream << L"(string) " << value->GetStringValue().c_str();
else if(value->IsFunction())
stream << L"(function) " << value->GetFunctionName().c_str();
else if(value->IsArray()) {
stream << L"(array) [";
int len = value->GetArrayLength();
for(int i = 0; i < len; ++i) {
stream << L"\n " << indent_str.c_str() << i << L" = ";
PrintValue(value->GetValue(i), stream, indent+1);
}
stream << L"\n" << indent_str.c_str() << L"]";
} else if(value->IsObject()) {
stream << L"(object) [";
std::vector<std::wstring> keys;
if(value->GetKeys(keys)) {
for(size_t i = 0; i < keys.size(); ++i) {
stream << L"\n " << indent_str.c_str() << keys[i].c_str() << L" = ";
PrintValue(value->GetValue(keys[i]), stream, indent+1);
}
}
stream << L"\n" << indent_str.c_str() << L"]";
}
}
};
void InitBindingTest(CefRefPtr<CefBrowser> browser,
CefRefPtr<CefFrame> frame,
CefRefPtr<CefV8Value> object)
{
// Create the new V8 object.
CefRefPtr<CefV8Value> testObjPtr = CefV8Value::CreateObject(NULL);
// Add the new V8 object to the global window object with the name
// "cef_test".
object->SetValue(L"cef_test", testObjPtr);
// Create an instance of ClientV8FunctionHandler as the V8 handler.
CefRefPtr<CefV8Handler> handlerPtr = new ClientV8FunctionHandler();
// Add a new V8 function to the cef_test object with the name "Dump".
testObjPtr->SetValue(L"Dump",
CefV8Value::CreateFunction(L"Dump", handlerPtr));
// Add a new V8 function to the cef_test object with the name "Call".
testObjPtr->SetValue(L"Call",
CefV8Value::CreateFunction(L"Call", handlerPtr));
}
void RunBindingTest(CefRefPtr<CefBrowser> browser)
{
std::wstring html =
L"<html><body>ClientV8FunctionHandler says:<br><pre>"
L"<script language=\"JavaScript\">"
L"document.writeln(window.cef_test.Dump(false, 1, 7.6654,'bar',"
L" [false,true],[5, 7.654, 1, 'foo', [true, 'bar'], 8]));"
L"document.writeln(window.cef_test.Dump(cef));"
L"document.writeln("
L" window.cef_test.Call(cef.test.test_object, 'GetMessage'));"
L"function my_object() {"
L" var obj = {};"
L" (function() {"
L" obj.GetMessage = function(a) {"
L" return 'Calling a function with value '+a+' on a user object succeeded.';"
L" };"
L" })();"
L" return obj;"
L"};"
L"document.writeln("
L" window.cef_test.Call(my_object, 'GetMessage', 'foobar'));"
L"</script>"
L"</pre></body></html>";
browser->GetMainFrame()->LoadString(html, L"about:blank");
}

View File

@ -1,14 +0,0 @@
// Copyright (c) 2009 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.
#pragma once
#include "include/cef.h"
// Add the V8 bindings.
void InitBindingTest(CefRefPtr<CefBrowser> browser,
CefRefPtr<CefFrame> frame,
CefRefPtr<CefV8Value> object);
// Run the test.
void RunBindingTest(CefRefPtr<CefBrowser> browser);

View File

@ -4,7 +4,6 @@
#include "include/cef.h"
#include "cefclient.h"
#include "binding_test.h"
#include "extension_test.h"
#include "plugin_test.h"
#include "resource_util.h"
@ -495,21 +494,6 @@ public:
return RV_CONTINUE;
}
// Event called for binding to a frame's JavaScript global object. The
// return value is currently ignored.
virtual RetVal HandleJSBinding(CefRefPtr<CefBrowser> browser,
CefRefPtr<CefFrame> frame,
CefRefPtr<CefV8Value> object)
{
// Add the V8 bindings.
InitBindingTest(browser, frame, object);
// Add the UI app V8 bindings.
InitUIBindingTest(browser, frame, object);
return RV_HANDLED;
}
// Called when the browser component is requesting focus. |isWidget| will be
// true if the focus is requested for a child widget of the browser window.
// Return RV_CONTINUE to allow the focus to be set or RV_HANDLED to cancel
@ -773,11 +757,7 @@ LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
if(browser.get())
browser->StopLoad();
return 0;
case ID_TESTS_JAVASCRIPT_HANDLER: // Test the V8 function handler
if(browser.get())
RunBindingTest(browser);
return 0;
case ID_TESTS_JAVASCRIPT_HANDLER2: // Test the V8 extension handler
case ID_TESTS_JAVASCRIPT_HANDLER: // Test the V8 extension handler
if(browser.get())
RunExtensionTest(browser);
return 0;

View File

@ -59,8 +59,7 @@ BEGIN
END
POPUP "Tests"
BEGIN
MENUITEM "JavaScript Function Handler", ID_TESTS_JAVASCRIPT_HANDLER
MENUITEM "JavaScript Extension Handler",ID_TESTS_JAVASCRIPT_HANDLER2
MENUITEM "JavaScript Extension Handler",ID_TESTS_JAVASCRIPT_HANDLER
MENUITEM "JavaScript Execute", ID_TESTS_JAVASCRIPT_EXECUTE
MENUITEM "Plugin", ID_TESTS_PLUGIN
MENUITEM "Popup Window", ID_TESTS_POPUP

View File

@ -13,7 +13,7 @@
</tr>
<tr>
<td colspan="2" style="font-size: 8pt;"><i>An embedded OpenGL plugin window that communicates with the Chromium browser control via JavaScript calls.</i>
<a href="#" onclick="window.uiapp.viewSource(); return false;">View Page Source</a></td>
<a href="#" onclick="cef.uiapp.viewSource(); return false;">View Page Source</a></td>
</tr>
<tr>
<td colspan="2" height="10"></td>
@ -36,9 +36,9 @@
<form name="control">
Rotation Value: <input type="text" name="rotation_field" value="0" size="5" readonly>
<br/><input type="button" value="Decrement Rotation" onclick="window.uiapp.modifyRotation(-2.0);">
<input type="button" value="Increment Rotation" onclick="window.uiapp.modifyRotation(2.0);">
<input type="button" value="Reset Rotation" name="reset_button" onclick="window.uiapp.resetRotation();" disabled>
<br/><input type="button" value="Decrement Rotation" onclick="cef.uiapp.modifyRotation(-2.0);">
<input type="button" value="Increment Rotation" onclick="cef.uiapp.modifyRotation(2.0);">
<input type="button" value="Reset Rotation" name="reset_button" onclick="cef.uiapp.resetRotation();" disabled>
</form>
</td>
</tr>

View File

@ -22,13 +22,12 @@
#define IDC_NAV_RELOAD 202
#define IDC_NAV_STOP 203
#define ID_TESTS_JAVASCRIPT_HANDLER 32771
#define ID_TESTS_JAVASCRIPT_HANDLER2 32772
#define ID_TESTS_JAVASCRIPT_EXECUTE 32773
#define ID_TESTS_PLUGIN 32774
#define ID_TESTS_POPUP 32775
#define ID_TESTS_REQUEST 32776
#define ID_TESTS_SCHEME_HANDLER 32777
#define ID_TESTS_UIAPP 32778
#define ID_TESTS_JAVASCRIPT_EXECUTE 32772
#define ID_TESTS_PLUGIN 32773
#define ID_TESTS_POPUP 32774
#define ID_TESTS_REQUEST 32775
#define ID_TESTS_SCHEME_HANDLER 32776
#define ID_TESTS_UIAPP 32777
#define IDC_STATIC -1
#define IDS_LOGO 1000
#define IDS_UIPLUGIN 1001

View File

@ -7,28 +7,6 @@
#include "cefclient.h"
void InitUIPluginTest()
{
// Structure providing information about the client plugin.
CefPluginInfo plugin_info;
plugin_info.display_name = L"Client UI Plugin";
plugin_info.unique_name = L"client_ui_plugin";
plugin_info.version = L"1, 0, 0, 1";
plugin_info.description = L"My Example Client UI Plugin";
CefPluginMimeType mime_type;
mime_type.mime_type = L"application/x-client-ui-plugin";
mime_type.file_extensions.push_back(L"*");
plugin_info.mime_types.push_back(mime_type);
plugin_info.np_getentrypoints = NP_UIGetEntryPoints;
plugin_info.np_initialize = NP_UIInitialize;
plugin_info.np_shutdown = NP_UIShutdown;
// Register the internal client plugin
CefRegisterPlugin(plugin_info);
}
// Implementation of the V8 handler class for the "window.uiapp" functions.
class ClientV8UIHandler : public CefThreadSafeBase<CefV8Handler>
{
@ -76,30 +54,49 @@ public:
}
};
void InitUIBindingTest(CefRefPtr<CefBrowser> browser,
CefRefPtr<CefFrame> frame,
CefRefPtr<CefV8Value> object)
void InitUIPluginTest()
{
if(frame->GetURL().find(L"http://tests/uiapp") != 0)
return;
// Create the new V8 object.
CefRefPtr<CefV8Value> testObjPtr = CefV8Value::CreateObject(NULL);
// Add the new V8 object to the global window object with the name "uiapp".
object->SetValue(L"uiapp", testObjPtr);
// Structure providing information about the client plugin.
CefPluginInfo plugin_info;
plugin_info.display_name = L"Client UI Plugin";
plugin_info.unique_name = L"client_ui_plugin";
plugin_info.version = L"1, 0, 0, 1";
plugin_info.description = L"My Example Client UI Plugin";
// Create an instance of ClientV8UIHandler as the V8 handler.
CefRefPtr<CefV8Handler> handlerPtr = new ClientV8UIHandler();
CefPluginMimeType mime_type;
mime_type.mime_type = L"application/x-client-ui-plugin";
mime_type.file_extensions.push_back(L"*");
plugin_info.mime_types.push_back(mime_type);
// Add a new V8 function to the uiapp object with the name "modifyRotation".
testObjPtr->SetValue(L"modifyRotation",
CefV8Value::CreateFunction(L"modifyRotation", handlerPtr));
// Add a new V8 function to the uiapp object with the name "resetRotation".
testObjPtr->SetValue(L"resetRotation",
CefV8Value::CreateFunction(L"resetRotation", handlerPtr));
// Add a new V8 function to the uiapp object with the name "viewSource".
testObjPtr->SetValue(L"viewSource",
CefV8Value::CreateFunction(L"viewSource", handlerPtr));
plugin_info.np_getentrypoints = NP_UIGetEntryPoints;
plugin_info.np_initialize = NP_UIInitialize;
plugin_info.np_shutdown = NP_UIShutdown;
// Register the internal client plugin
CefRegisterPlugin(plugin_info);
// Register a V8 extension with the below JavaScript code that calls native
// methods implemented in ClientV8UIHandler.
std::wstring code = L"var cef;"
L"if (!cef)"
L" cef = {};"
L"if (!cef.uiapp)"
L" cef.uiapp = {};"
L"(function() {"
L" cef.uiapp.modifyRotation = function(val) {"
L" native function modifyRotation();"
L" return modifyRotation(val);"
L" };"
L" cef.uiapp.resetRotation = function() {"
L" native function resetRotation();"
L" return resetRotation();"
L" };"
L" cef.uiapp.viewSource = function() {"
L" native function viewSource();"
L" return viewSource();"
L" };"
L"})();";
CefRegisterExtension(L"uiplugin/test", code, new ClientV8UIHandler());
}
void RunUIPluginTest(CefRefPtr<CefBrowser> browser)

View File

@ -5,14 +5,8 @@
#pragma once
#include "include/cef.h"
// Register the internal client plugin.
// Register the internal client plugin and V8 extension.
void InitUIPluginTest();
// Add the V8 bindings.
void InitUIBindingTest(CefRefPtr<CefBrowser> browser,
CefRefPtr<CefFrame> frame,
CefRefPtr<CefV8Value> object);
// Run the test.
void RunUIPluginTest(CefRefPtr<CefBrowser> browser);

View File

@ -6,7 +6,7 @@
#define _TEST_HANDLER_H
#include "include/cef.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "testing/gtest/include/gtest/gtest.h"
// Base implementation of CefHandler for unit tests.
class TestHandler : public CefThreadSafeBase<CefHandler>
@ -189,13 +189,6 @@ public:
return RV_CONTINUE;
}
virtual RetVal HandleJSBinding(CefRefPtr<CefBrowser> browser,
CefRefPtr<CefFrame> frame,
CefRefPtr<CefV8Value> object)
{
return RV_CONTINUE;
}
virtual RetVal HandleSetFocus(CefRefPtr<CefBrowser> browser,
bool isWidget)
{

View File

@ -1,14 +1,14 @@
// Copyright (c) 2009 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.
#include "include/cef.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "test_handler.h"
bool g_V8TestV8HandlerExecuteCalled;
bool g_V8TestV8HandlerExecute2Called;
#include "include/cef.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "test_handler.h"
bool g_V8TestV8HandlerExecuteCalled;
bool g_V8TestV8HandlerExecute2Called;
class V8TestV8Handler : public CefThreadSafeBase<CefV8Handler>
{
public:
@ -210,124 +210,51 @@ public:
}
bool binding_test_;
};
class V8TestHandler : public TestHandler
{
public:
V8TestHandler(bool bindingTest) { binding_test_ = bindingTest; }
virtual void RunTest()
{
std::string object;
if(binding_test_) {
// binding uses the window object
object = "window.test";
} else {
// extension uses a global object
object = "test";
}
std::stringstream testHtml;
testHtml <<
"<html><body>"
"<script language=\"JavaScript\">"
"function func(a,b,c,d) { return a+b+(c?1:0)+parseFloat(d); }"
"function func2(a,b) { throw('My Exception'); }"
<< object << ".execute2("
" " << object << ".execute(5, 6.543, true, \"test string\","
" [7, 5.432, false, \"another string\"],"
" {arg0:2, arg1:3.433, arg2:true, arg3:\"some string\"}, func, func2)"
");"
"</script>"
"</body></html>";
AddResource(L"http://tests/run.html", testHtml.str(), L"text/html");
CreateBrowser(L"http://tests/run.html");
}
};
class V8TestHandler : public TestHandler
{
public:
V8TestHandler() {}
virtual void RunTest()
{
// extension uses a global object
std::string object = "test";
std::stringstream testHtml;
testHtml <<
"<html><body>"
"<script language=\"JavaScript\">"
"function func(a,b,c,d) { return a+b+(c?1:0)+parseFloat(d); }"
"function func2(a,b) { throw('My Exception'); }"
<< object << ".execute2("
" " << object << ".execute(5, 6.543, true, \"test string\","
" [7, 5.432, false, \"another string\"],"
" {arg0:2, arg1:3.433, arg2:true, arg3:\"some string\"}, func, func2)"
");"
"</script>"
"</body></html>";
AddResource(L"http://tests/run.html", testHtml.str(), L"text/html");
CreateBrowser(L"http://tests/run.html");
}
virtual RetVal HandleLoadEnd(CefRefPtr<CefBrowser> browser,
CefRefPtr<CefFrame> frame)
{
if(!browser->IsPopup() && !frame.get())
NotifyTestComplete();
return RV_CONTINUE;
}
virtual RetVal HandleJSBinding(CefRefPtr<CefBrowser> browser,
CefRefPtr<CefFrame> frame,
CefRefPtr<CefV8Value> object)
{
if(binding_test_) {
TestHandleJSBinding(browser, frame, object);
return RV_HANDLED;
}
return RV_CONTINUE;
}
void TestHandleJSBinding(CefRefPtr<CefBrowser> browser,
CefRefPtr<CefFrame> frame,
CefRefPtr<CefV8Value> object)
{
// Create the new V8 object
CefRefPtr<CefV8Value> testObj = CefV8Value::CreateObject(NULL);
ASSERT_TRUE(testObj.get() != NULL);
ASSERT_TRUE(object->SetValue(L"test", testObj));
}
};
// Create an instance of V8ExecuteV8Handler
CefRefPtr<CefV8Handler> testHandler(new V8TestV8Handler(true));
ASSERT_TRUE(testHandler.get() != NULL);
// Verify extensions
TEST(V8Test, Extension)
{
g_V8TestV8HandlerExecuteCalled = false;
g_V8TestV8HandlerExecute2Called = false;
// Add the functions
CefRefPtr<CefV8Value> testFunc;
testFunc = CefV8Value::CreateFunction(L"execute", testHandler);
ASSERT_TRUE(testFunc.get() != NULL);
ASSERT_TRUE(testObj->SetValue(L"execute", testFunc));
testFunc = CefV8Value::CreateFunction(L"execute2", testHandler);
ASSERT_TRUE(testFunc.get() != NULL);
ASSERT_TRUE(testObj->SetValue(L"execute2", testFunc));
// Add the values
ASSERT_TRUE(testObj->SetValue(L"intVal",
CefV8Value::CreateInt(12)));
ASSERT_TRUE(testObj->SetValue(L"doubleVal",
CefV8Value::CreateDouble(5.432)));
ASSERT_TRUE(testObj->SetValue(L"boolVal",
CefV8Value::CreateBool(true)));
ASSERT_TRUE(testObj->SetValue(L"stringVal",
CefV8Value::CreateString(L"the string")));
CefRefPtr<CefV8Value> testArray(CefV8Value::CreateArray());
ASSERT_TRUE(testArray.get() != NULL);
ASSERT_TRUE(testObj->SetValue(L"arrayVal", testArray));
ASSERT_TRUE(testArray->SetValue(0, CefV8Value::CreateInt(4)));
ASSERT_TRUE(testArray->SetValue(1, CefV8Value::CreateDouble(120.43)));
ASSERT_TRUE(testArray->SetValue(2, CefV8Value::CreateBool(true)));
ASSERT_TRUE(testArray->SetValue(3, CefV8Value::CreateString(L"a string")));
}
bool binding_test_;
};
// Verify window binding
TEST(V8Test, Binding)
{
g_V8TestV8HandlerExecuteCalled = false;
g_V8TestV8HandlerExecute2Called = false;
V8TestHandler* handler = new V8TestHandler(true);
handler->ExecuteTest();
ASSERT_TRUE(g_V8TestV8HandlerExecuteCalled);
ASSERT_TRUE(g_V8TestV8HandlerExecute2Called);
}
// Verify extensions
TEST(V8Test, Extension)
{
g_V8TestV8HandlerExecuteCalled = false;
g_V8TestV8HandlerExecute2Called = false;
std::wstring extensionCode =
L"var test;"
L"if (!test)"
@ -342,11 +269,11 @@ TEST(V8Test, Extension)
L" return execute2(a);"
L" };"
L"})();";
CefRegisterExtension(L"v8/test", extensionCode, new V8TestV8Handler(false));
V8TestHandler* handler = new V8TestHandler(false);
handler->ExecuteTest();
ASSERT_TRUE(g_V8TestV8HandlerExecuteCalled);
ASSERT_TRUE(g_V8TestV8HandlerExecute2Called);
}
CefRegisterExtension(L"v8/test", extensionCode, new V8TestV8Handler(false));
V8TestHandler* handler = new V8TestHandler();
handler->ExecuteTest();
ASSERT_TRUE(g_V8TestV8HandlerExecuteCalled);
ASSERT_TRUE(g_V8TestV8HandlerExecute2Called);
}