2011-09-20 22:41:54 +02:00
|
|
|
// Copyright (c) 2011 The Chromium Embedded Framework Authors. All rights
|
2010-10-03 23:04:50 +02:00
|
|
|
// reserved. Use of this source code is governed by a BSD-style license that
|
|
|
|
// can be found in the LICENSE file.
|
|
|
|
|
2012-01-10 00:46:23 +01:00
|
|
|
#include "cefclient/extension_test.h"
|
|
|
|
#include <string>
|
2011-12-23 18:36:30 +01:00
|
|
|
#include "include/cef_browser.h"
|
|
|
|
#include "include/cef_frame.h"
|
|
|
|
#include "include/cef_stream.h"
|
|
|
|
#include "include/cef_v8.h"
|
2012-01-10 00:46:23 +01:00
|
|
|
#include "cefclient/resource_util.h"
|
2010-10-03 23:04:50 +02:00
|
|
|
|
|
|
|
// Implementation of the V8 handler class for the "cef.test" extension.
|
2012-01-10 00:46:23 +01:00
|
|
|
class ClientV8ExtensionHandler : public CefV8Handler {
|
|
|
|
public:
|
2010-11-22 18:49:46 +01:00
|
|
|
ClientV8ExtensionHandler() : test_param_("An initial string value.") {}
|
2010-10-03 23:04:50 +02:00
|
|
|
virtual ~ClientV8ExtensionHandler() {}
|
|
|
|
|
|
|
|
// Execute with the specified argument list and return value. Return true if
|
|
|
|
// the method was handled.
|
2010-11-22 18:49:46 +01:00
|
|
|
virtual bool Execute(const CefString& name,
|
2010-10-03 23:04:50 +02:00
|
|
|
CefRefPtr<CefV8Value> object,
|
|
|
|
const CefV8ValueList& arguments,
|
|
|
|
CefRefPtr<CefV8Value>& retval,
|
2012-01-10 00:46:23 +01:00
|
|
|
CefString& exception) {
|
2012-10-26 22:39:24 +02:00
|
|
|
if (name == "SetTestParam") {
|
2010-10-03 23:04:50 +02:00
|
|
|
// Handle the SetTestParam native function by saving the string argument
|
|
|
|
// into the local member.
|
2012-01-10 00:46:23 +01:00
|
|
|
if (arguments.size() != 1 || !arguments[0]->IsString())
|
2010-10-03 23:04:50 +02:00
|
|
|
return false;
|
2012-01-10 00:46:23 +01:00
|
|
|
|
2010-10-03 23:04:50 +02:00
|
|
|
test_param_ = arguments[0]->GetStringValue();
|
|
|
|
return true;
|
2012-01-10 00:46:23 +01:00
|
|
|
} else if (name == "GetTestParam") {
|
2010-10-03 23:04:50 +02:00
|
|
|
// Handle the GetTestParam native function by returning the local member
|
|
|
|
// value.
|
|
|
|
retval = CefV8Value::CreateString(test_param_);
|
|
|
|
return true;
|
2012-01-10 00:46:23 +01:00
|
|
|
} else if (name == "GetTestObject") {
|
2010-10-03 23:04:50 +02:00
|
|
|
// Handle the GetTestObject native function by creating and returning a
|
|
|
|
// new V8 object.
|
2012-05-23 21:01:04 +02:00
|
|
|
retval = CefV8Value::CreateObject(NULL);
|
2010-10-03 23:04:50 +02:00
|
|
|
// Add a string parameter to the new V8 object.
|
2010-11-22 18:49:46 +01:00
|
|
|
retval->SetValue("param", CefV8Value::CreateString(
|
2011-11-04 20:34:14 +01:00
|
|
|
"Retrieving a parameter on a native object succeeded."),
|
|
|
|
V8_PROPERTY_ATTRIBUTE_NONE);
|
2010-10-03 23:04:50 +02:00
|
|
|
// Add a function to the new V8 object.
|
2010-11-22 18:49:46 +01:00
|
|
|
retval->SetValue("GetMessage",
|
2011-11-04 20:34:14 +01:00
|
|
|
CefV8Value::CreateFunction("GetMessage", this),
|
|
|
|
V8_PROPERTY_ATTRIBUTE_NONE);
|
2010-10-03 23:04:50 +02:00
|
|
|
return true;
|
2012-01-10 00:46:23 +01:00
|
|
|
} else if (name == "GetMessage") {
|
2010-10-03 23:04:50 +02:00
|
|
|
// Handle the GetMessage object function by returning a string.
|
|
|
|
retval = CefV8Value::CreateString(
|
2010-11-22 18:49:46 +01:00
|
|
|
"Calling a function on a native object succeeded.");
|
2010-10-03 23:04:50 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2012-01-10 00:46:23 +01:00
|
|
|
private:
|
2010-11-22 18:49:46 +01:00
|
|
|
CefString test_param_;
|
2011-05-20 16:42:25 +02:00
|
|
|
|
|
|
|
IMPLEMENT_REFCOUNTING(ClientV8ExtensionHandler);
|
2010-10-03 23:04:50 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2012-01-10 00:46:23 +01:00
|
|
|
void InitExtensionTest() {
|
2010-10-03 23:04:50 +02:00
|
|
|
// Register a V8 extension with the below JavaScript code that calls native
|
|
|
|
// methods implemented in ClientV8ExtensionHandler.
|
2010-11-22 18:49:46 +01:00
|
|
|
std::string code = "var cef;"
|
|
|
|
"if (!cef)"
|
|
|
|
" cef = {};"
|
|
|
|
"if (!cef.test)"
|
|
|
|
" cef.test = {};"
|
|
|
|
"(function() {"
|
|
|
|
" cef.test.__defineGetter__('test_param', function() {"
|
|
|
|
" native function GetTestParam();"
|
|
|
|
" return GetTestParam();"
|
|
|
|
" });"
|
|
|
|
" cef.test.__defineSetter__('test_param', function(b) {"
|
|
|
|
" native function SetTestParam();"
|
2012-01-10 00:46:23 +01:00
|
|
|
" if (b) SetTestParam(b);"
|
2010-11-22 18:49:46 +01:00
|
|
|
" });"
|
|
|
|
" cef.test.test_object = function() {"
|
|
|
|
" native function GetTestObject();"
|
|
|
|
" return GetTestObject();"
|
|
|
|
" };"
|
|
|
|
"})();";
|
|
|
|
CefRegisterExtension("v8/test", code, new ClientV8ExtensionHandler());
|
2010-10-03 23:04:50 +02:00
|
|
|
}
|
|
|
|
|
2012-01-10 00:46:23 +01:00
|
|
|
void RunExtensionTest(CefRefPtr<CefBrowser> browser) {
|
2010-11-22 18:49:46 +01:00
|
|
|
std::string html =
|
|
|
|
"<html><body>ClientV8ExtensionHandler says:<br><pre>"
|
|
|
|
"<script language=\"JavaScript\">"
|
|
|
|
"cef.test.test_param ="
|
|
|
|
" 'Assign and retrieve a value succeeded the first time.';"
|
|
|
|
"document.writeln(cef.test.test_param);"
|
|
|
|
"cef.test.test_param ="
|
|
|
|
" 'Assign and retrieve a value succeeded the second time.';"
|
|
|
|
"document.writeln(cef.test.test_param);"
|
|
|
|
"var obj = cef.test.test_object();"
|
|
|
|
"document.writeln(obj.param);"
|
|
|
|
"document.writeln(obj.GetMessage());"
|
|
|
|
"</script>"
|
|
|
|
"</pre></body></html>";
|
|
|
|
browser->GetMainFrame()->LoadString(html, "about:blank");
|
2010-10-03 23:04:50 +02:00
|
|
|
}
|