mirror of
https://bitbucket.org/chromiumembedded/cef
synced 2025-06-05 21:39:12 +02:00
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:
@ -16,30 +16,30 @@ public:
|
||||
|
||||
// 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"Dump")
|
||||
if(name == "Dump")
|
||||
{
|
||||
// The "Dump" function will return a human-readable dump of the input
|
||||
// arguments.
|
||||
std::wstringstream stream;
|
||||
std::stringstream stream;
|
||||
size_t i;
|
||||
|
||||
for(i = 0; i < arguments.size(); ++i)
|
||||
{
|
||||
stream << L"arg[" << i << L"] = ";
|
||||
stream << "arg[" << i << "] = ";
|
||||
PrintValue(arguments[i], stream, 0);
|
||||
stream << L"\n";
|
||||
stream << "\n";
|
||||
}
|
||||
|
||||
retval = CefV8Value::CreateString(stream.str());
|
||||
return true;
|
||||
}
|
||||
else if(name == L"Call")
|
||||
else if(name == "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
|
||||
@ -80,46 +80,46 @@ public:
|
||||
}
|
||||
|
||||
// Simple function for formatted output of a V8 value.
|
||||
void PrintValue(CefRefPtr<CefV8Value> value, std::wstringstream &stream,
|
||||
void PrintValue(CefRefPtr<CefV8Value> value, std::stringstream &stream,
|
||||
int indent)
|
||||
{
|
||||
std::wstringstream indent_stream;
|
||||
std::stringstream indent_stream;
|
||||
for(int i = 0; i < indent; ++i)
|
||||
indent_stream << L" ";
|
||||
std::wstring indent_str = indent_stream.str();
|
||||
indent_stream << " ";
|
||||
std::string indent_str = indent_stream.str();
|
||||
|
||||
if(value->IsUndefined())
|
||||
stream << L"(undefined)";
|
||||
stream << "(undefined)";
|
||||
else if(value->IsNull())
|
||||
stream << L"(null)";
|
||||
stream << "(null)";
|
||||
else if(value->IsBool())
|
||||
stream << L"(bool) " << (value->GetBoolValue() ? L"true" : L"false");
|
||||
stream << "(bool) " << (value->GetBoolValue() ? "true" : "false");
|
||||
else if(value->IsInt())
|
||||
stream << L"(int) " << value->GetIntValue();
|
||||
stream << "(int) " << value->GetIntValue();
|
||||
else if(value->IsDouble())
|
||||
stream << L"(double) " << value->GetDoubleValue();
|
||||
stream << "(double) " << value->GetDoubleValue();
|
||||
else if(value->IsString())
|
||||
stream << L"(string) " << value->GetStringValue().c_str();
|
||||
stream << "(string) " << std::string(value->GetStringValue());
|
||||
else if(value->IsFunction())
|
||||
stream << L"(function) " << value->GetFunctionName().c_str();
|
||||
stream << "(function) " << std::string(value->GetFunctionName());
|
||||
else if(value->IsArray()) {
|
||||
stream << L"(array) [";
|
||||
stream << "(array) [";
|
||||
int len = value->GetArrayLength();
|
||||
for(int i = 0; i < len; ++i) {
|
||||
stream << L"\n " << indent_str.c_str() << i << L" = ";
|
||||
stream << "\n " << indent_str.c_str() << i << " = ";
|
||||
PrintValue(value->GetValue(i), stream, indent+1);
|
||||
}
|
||||
stream << L"\n" << indent_str.c_str() << L"]";
|
||||
stream << "\n" << indent_str.c_str() << "]";
|
||||
} else if(value->IsObject()) {
|
||||
stream << L"(object) [";
|
||||
std::vector<std::wstring> keys;
|
||||
stream << "(object) [";
|
||||
std::vector<CefString> 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" = ";
|
||||
stream << "\n " << indent_str.c_str() << keys[i].c_str() << " = ";
|
||||
PrintValue(value->GetValue(keys[i]), stream, indent+1);
|
||||
}
|
||||
}
|
||||
stream << L"\n" << indent_str.c_str() << L"]";
|
||||
stream << "\n" << indent_str.c_str() << "]";
|
||||
}
|
||||
}
|
||||
};
|
||||
@ -133,41 +133,41 @@ void InitBindingTest(CefRefPtr<CefBrowser> browser,
|
||||
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);
|
||||
object->SetValue("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));
|
||||
testObjPtr->SetValue("Dump",
|
||||
CefV8Value::CreateFunction("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));
|
||||
testObjPtr->SetValue("Call",
|
||||
CefV8Value::CreateFunction("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");
|
||||
std::string html =
|
||||
"<html><body>ClientV8FunctionHandler says:<br><pre>"
|
||||
"<script language=\"JavaScript\">"
|
||||
"document.writeln(window.cef_test.Dump(false, 1, 7.6654,'bar',"
|
||||
" [false,true],[5, 7.654, 1, 'foo', [true, 'bar'], 8]));"
|
||||
"document.writeln(window.cef_test.Dump(cef));"
|
||||
"document.writeln("
|
||||
" window.cef_test.Call(cef.test.test_object, 'GetMessage'));"
|
||||
"function my_object() {"
|
||||
" var obj = {};"
|
||||
" (function() {"
|
||||
" obj.GetMessage = function(a) {"
|
||||
" return 'Calling a function with value '+a+' on a user object succeeded.';"
|
||||
" };"
|
||||
" })();"
|
||||
" return obj;"
|
||||
"};"
|
||||
"document.writeln("
|
||||
" window.cef_test.Call(my_object, 'GetMessage', 'foobar'));"
|
||||
"</script>"
|
||||
"</pre></body></html>";
|
||||
browser->GetMainFrame()->LoadString(html, "about:blank");
|
||||
}
|
||||
|
Reference in New Issue
Block a user