Introduce CefString and cef_string_t implementations that support string type conversions and customization of the API string type (issue #146).

git-svn-id: https://chromiumembedded.googlecode.com/svn/trunk@145 5089003a-bbd8-11dd-ad1f-f1f9622dbc98
This commit is contained in:
Marshall Greenblatt
2010-11-22 17:49:46 +00:00
parent 1e1c2ad8d7
commit 7d60642638
121 changed files with 2598 additions and 3209 deletions

View File

@@ -9,18 +9,18 @@
class ClientV8ExtensionHandler : public CefThreadSafeBase<CefV8Handler>
{
public:
ClientV8ExtensionHandler() : test_param_(L"An initial string value.") {}
ClientV8ExtensionHandler() : test_param_("An initial string value.") {}
virtual ~ClientV8ExtensionHandler() {}
// Execute with the specified argument list and return value. Return true if
// the method was handled.
virtual bool Execute(const std::wstring& name,
virtual bool Execute(const CefString& name,
CefRefPtr<CefV8Value> object,
const CefV8ValueList& arguments,
CefRefPtr<CefV8Value>& retval,
std::wstring& exception)
CefString& exception)
{
if(name == L"SetTestParam")
if(name == "SetTestParam")
{
// Handle the SetTestParam native function by saving the string argument
// into the local member.
@@ -30,38 +30,38 @@ public:
test_param_ = arguments[0]->GetStringValue();
return true;
}
else if(name == L"GetTestParam")
else if(name == "GetTestParam")
{
// Handle the GetTestParam native function by returning the local member
// value.
retval = CefV8Value::CreateString(test_param_);
return true;
}
else if(name == L"GetTestObject")
else if(name == "GetTestObject")
{
// Handle the GetTestObject native function by creating and returning a
// new V8 object.
retval = CefV8Value::CreateObject(NULL);
// Add a string parameter to the new V8 object.
retval->SetValue(L"param", CefV8Value::CreateString(
L"Retrieving a parameter on a native object succeeded."));
retval->SetValue("param", CefV8Value::CreateString(
"Retrieving a parameter on a native object succeeded."));
// Add a function to the new V8 object.
retval->SetValue(L"GetMessage",
CefV8Value::CreateFunction(L"GetMessage", this));
retval->SetValue("GetMessage",
CefV8Value::CreateFunction("GetMessage", this));
return true;
}
else if(name == L"GetMessage")
else if(name == "GetMessage")
{
// Handle the GetMessage object function by returning a string.
retval = CefV8Value::CreateString(
L"Calling a function on a native object succeeded.");
"Calling a function on a native object succeeded.");
return true;
}
return false;
}
private:
std::wstring test_param_;
CefString test_param_;
};
@@ -69,43 +69,43 @@ void InitExtensionTest()
{
// Register a V8 extension with the below JavaScript code that calls native
// methods implemented in ClientV8ExtensionHandler.
std::wstring code = L"var cef;"
L"if (!cef)"
L" cef = {};"
L"if (!cef.test)"
L" cef.test = {};"
L"(function() {"
L" cef.test.__defineGetter__('test_param', function() {"
L" native function GetTestParam();"
L" return GetTestParam();"
L" });"
L" cef.test.__defineSetter__('test_param', function(b) {"
L" native function SetTestParam();"
L" if(b) SetTestParam(b);"
L" });"
L" cef.test.test_object = function() {"
L" native function GetTestObject();"
L" return GetTestObject();"
L" };"
L"})();";
CefRegisterExtension(L"v8/test", code, new ClientV8ExtensionHandler());
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();"
" if(b) SetTestParam(b);"
" });"
" cef.test.test_object = function() {"
" native function GetTestObject();"
" return GetTestObject();"
" };"
"})();";
CefRegisterExtension("v8/test", code, new ClientV8ExtensionHandler());
}
void RunExtensionTest(CefRefPtr<CefBrowser> browser)
{
std::wstring html =
L"<html><body>ClientV8ExtensionHandler says:<br><pre>"
L"<script language=\"JavaScript\">"
L"cef.test.test_param ="
L" 'Assign and retrieve a value succeeded the first time.';"
L"document.writeln(cef.test.test_param);"
L"cef.test.test_param ="
L" 'Assign and retrieve a value succeeded the second time.';"
L"document.writeln(cef.test.test_param);"
L"var obj = cef.test.test_object();"
L"document.writeln(obj.param);"
L"document.writeln(obj.GetMessage());"
L"</script>"
L"</pre></body></html>";
browser->GetMainFrame()->LoadString(html, L"about:blank");
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");
}