Title: [122034] trunk/Source/WebKit/blackberry
Revision
122034
Author
[email protected]
Date
2012-07-06 13:43:20 -0700 (Fri, 06 Jul 2012)

Log Message

Add an API to explicitly call a _javascript_ function with args.
https://bugs.webkit.org/show_bug.cgi?id=90694

Currently the Blackberry port doesn't expose the _javascript_
engine to 3rd parties so they rely upon executeJavaScript
which can be slower than necessary and unsafe as eval is used.
This new API provides a way to explicitly call a specific
_javascript_ function with a list of args preventing the case
where an argument comes from a untrusted source and tries to
escape the arg list to take control of the _javascript_ engine.

In the future if the Blackberry port introduces a formal
way to interact with the _javascript_ engine this function should
be removed.

PR 149294

Patch by Benjamin C Meyer <[email protected]> on 2012-07-06
Reviewed by Unreviewed

* Api/WebPage.cpp:
(BlackBerry::WebKit::WebPage::executeJavaScriptFunction):
(WebKit):
* Api/WebPage.h:

Modified Paths

Diff

Modified: trunk/Source/WebKit/blackberry/Api/WebPage.cpp (122033 => 122034)


--- trunk/Source/WebKit/blackberry/Api/WebPage.cpp	2012-07-06 20:41:09 UTC (rev 122033)
+++ trunk/Source/WebKit/blackberry/Api/WebPage.cpp	2012-07-06 20:43:20 UTC (rev 122034)
@@ -157,6 +157,7 @@
 #include <BlackBerryPlatformSettings.h>
 #include <_javascript_Core/APICast.h>
 #include <_javascript_Core/JSContextRef.h>
+#include <_javascript_Core/JSStringRef.h>
 #include <SharedPointer.h>
 #include <sys/keycodes.h>
 #include <unicode/ustring.h> // platform ICU
@@ -849,6 +850,93 @@
     return d->executeJavaScriptInIsolatedWorld(sourceCode, returnType, returnValue);
 }
 
+bool WebPage::executeJavaScriptFunction(const std::vector<std::string> &function, const std::vector<std::string> &args, _javascript_DataType& returnType, WebString& returnValue)
+{
+    if (!d->m_mainFrame)
+        return false;
+    JSC::Bindings::RootObject* root = d->m_mainFrame->script()->bindingRootObject();
+    if (!root)
+        return false;
+    JSC::ExecState* exec = root->globalObject()->globalExec();
+    JSGlobalContextRef ctx = toGlobalRef(exec);
+
+    WTF::Vector<JSStringRef> argList(args.size());
+    WTF::Vector<JSValueRef> argListRef(args.size());
+    for (unsigned i = 0; i < args.size(); ++i) {
+        JSStringRef str = JSStringCreateWithUTF8CString(args[i].c_str());
+        argList[i] = str;
+        JSValueRef strRef = JSValueMakeString(ctx, str);
+        argListRef[i] = strRef;
+    }
+
+    JSValueRef windowObjectValue = windowObject();
+    JSObjectRef obj = JSValueToObject(ctx, windowObjectValue, 0);
+    JSObjectRef thisObject = obj;
+    for (unsigned i = 0; i < function.size(); ++i) {
+        JSStringRef str = JSStringCreateWithUTF8CString(function[i].c_str());
+        thisObject = obj;
+        obj = JSValueToObject(ctx, JSObjectGetProperty(ctx, obj, str, 0), 0);
+        JSStringRelease(str);
+        if (!obj)
+            break;
+    }
+
+    JSObjectRef functionObject = obj;
+    JSValueRef result = 0;
+    JSValueRef exception;
+    if (functionObject && thisObject)
+        result = JSObjectCallAsFunction(ctx, functionObject, thisObject, args.size(), argListRef.data(), &exception);
+
+    for (unsigned i = 0; i < args.size(); ++i)
+        JSStringRelease(argList[i]);
+
+    JSC::JSValue value = toJS(exec, result);
+
+    if (!value) {
+        returnType = JSException;
+        JSStringRef stringRef = JSValueToStringCopy(ctx, exception, 0);
+        size_t bufferSize = JSStringGetMaximumUTF8CStringSize(stringRef);
+        WTF::Vector<char> buffer(bufferSize);
+        JSStringGetUTF8CString(stringRef, buffer.data(), bufferSize);
+        returnValue = WebString::fromUtf8(buffer.data());
+        return false;
+    }
+
+    JSType type = JSValueGetType(ctx, result);
+
+    switch (type) {
+    case kJSTypeNull:
+        returnType = JSNull;
+        break;
+    case kJSTypeBoolean:
+        returnType = JSBoolean;
+        break;
+    case kJSTypeNumber:
+        returnType = JSNumber;
+        break;
+    case kJSTypeString:
+        returnType = JSString;
+        break;
+    case kJSTypeObject:
+        returnType = JSObject;
+        break;
+    case kJSTypeUndefined:
+    default:
+        returnType = JSUndefined;
+        break;
+    }
+
+    if (returnType == JSBoolean || returnType == JSNumber || returnType == JSString || returnType == JSObject) {
+        JSStringRef stringRef = JSValueToStringCopy(ctx, result, 0);
+        size_t bufferSize = JSStringGetMaximumUTF8CStringSize(stringRef);
+        WTF::Vector<char> buffer(bufferSize);
+        JSStringGetUTF8CString(stringRef, buffer.data(), bufferSize);
+        returnValue = WebString::fromUtf8(buffer.data());
+    }
+
+    return true;
+}
+
 void WebPagePrivate::stopCurrentLoad()
 {
     // This function should contain all common code triggered by WebPage::load

Modified: trunk/Source/WebKit/blackberry/Api/WebPage.h (122033 => 122034)


--- trunk/Source/WebKit/blackberry/Api/WebPage.h	2012-07-06 20:41:09 UTC (rev 122033)
+++ trunk/Source/WebKit/blackberry/Api/WebPage.h	2012-07-06 20:43:20 UTC (rev 122034)
@@ -27,6 +27,8 @@
 #include <BlackBerryPlatformWebContext.h>
 #include <imf/input_data.h>
 #include <network/NetworkRequest.h>
+#include <string>
+#include <vector>
 
 struct OpaqueJSContext;
 typedef const struct OpaqueJSContext* JSContextRef;
@@ -103,6 +105,8 @@
     // Takes a UTF16 encoded script that is used explicitly by the pattern matching code
     bool executeJavaScriptInIsolatedWorld(const std::wstring& script, _javascript_DataType& returnType, WebString& returnValue);
 
+    bool executeJavaScriptFunction(const std::vector<std::string> &script, const std::vector<std::string> &args, _javascript_DataType& returnType, WebString& returnValue);
+
     void initializeIconDataBase();
 
     void stopLoading();

Modified: trunk/Source/WebKit/blackberry/ChangeLog (122033 => 122034)


--- trunk/Source/WebKit/blackberry/ChangeLog	2012-07-06 20:41:09 UTC (rev 122033)
+++ trunk/Source/WebKit/blackberry/ChangeLog	2012-07-06 20:43:20 UTC (rev 122034)
@@ -1,3 +1,29 @@
+2012-07-06  Benjamin C Meyer  <[email protected]>
+
+        Add an API to explicitly call a _javascript_ function with args.
+        https://bugs.webkit.org/show_bug.cgi?id=90694
+
+        Currently the Blackberry port doesn't expose the _javascript_
+        engine to 3rd parties so they rely upon executeJavaScript
+        which can be slower than necessary and unsafe as eval is used.
+        This new API provides a way to explicitly call a specific
+        _javascript_ function with a list of args preventing the case
+        where an argument comes from a untrusted source and tries to
+        escape the arg list to take control of the _javascript_ engine.
+
+        In the future if the Blackberry port introduces a formal
+        way to interact with the _javascript_ engine this function should
+        be removed.
+
+        PR 149294
+
+        Reviewed by Unreviewed
+
+        * Api/WebPage.cpp:
+        (BlackBerry::WebKit::WebPage::executeJavaScriptFunction):
+        (WebKit):
+        * Api/WebPage.h:
+
 2012-07-05  Charles Wei  <[email protected]>
 
         [BlackBerry] Fix the build error introduced by enabling CUSTOM_THEME_HANDLER
_______________________________________________
webkit-changes mailing list
[email protected]
http://lists.webkit.org/mailman/listinfo.cgi/webkit-changes

Reply via email to