Title: [185554] trunk
Revision
185554
Author
[email protected]
Date
2015-06-15 09:57:01 -0700 (Mon, 15 Jun 2015)

Log Message

-[WKWebView evaluateJavaScript] provides a misleading error when the return cannot be serialized
https://bugs.webkit.org/show_bug.cgi?id=145900

Reviewed by Sam Weinig.

Source/WebCore:

* English.lproj/Localizable.strings:

Source/WebKit2:

We used to conflate to cases: (1) when there was a _javascript_ exception, and
(2) when the result could not be serialized for IPC. Turns out that it was quite
misleading!

* UIProcess/API/C/WKPage.cpp:
(WKPageRunJavaScriptInMainFrame):
* UIProcess/API/Cocoa/WKError.h:
* UIProcess/API/Cocoa/WKError.mm:
(localizedDescriptionForErrorCode):
* UIProcess/API/Cocoa/WKWebView.mm:
(-[WKWebView evaluateJavaScript:completionHandler:]):
* UIProcess/WebPageProxy.cpp:
(WebKit::WebPageProxy::runJavaScriptInMainFrame):
(WebKit::WebPageProxy::scriptValueCallback):
* UIProcess/WebPageProxy.h:
* UIProcess/WebPageProxy.messages.in:
* WebProcess/WebPage/WebPage.cpp:
(WebKit::WebPage::runJavaScriptInMainFrame):
* UIProcess/API/gtk/WebKitWebView.cpp:
(webkit_web_view_run_javascript):
(resourcesStreamReadCallback):

Tools:

* TestWebKitAPI/Tests/WebKit2Cocoa/WKWebViewEvaluateJavaScript.mm:
(TEST): Added tests.

Modified Paths

Diff

Modified: trunk/Source/WebCore/ChangeLog (185553 => 185554)


--- trunk/Source/WebCore/ChangeLog	2015-06-15 16:36:24 UTC (rev 185553)
+++ trunk/Source/WebCore/ChangeLog	2015-06-15 16:57:01 UTC (rev 185554)
@@ -1,3 +1,12 @@
+2015-06-12  Alexey Proskuryakov  <[email protected]>
+
+        -[WKWebView evaluateJavaScript] provides a misleading error when the return cannot be serialized
+        https://bugs.webkit.org/show_bug.cgi?id=145900
+
+        Reviewed by Sam Weinig.
+
+        * English.lproj/Localizable.strings:
+
 2015-06-15  Carlos Garcia Campos  <[email protected]>
 
         [SOUP] Custom URI schemes don't work for requests containing a fragment identifier

Modified: trunk/Source/WebCore/English.lproj/Localizable.strings (185553 => 185554)


--- trunk/Source/WebCore/English.lproj/Localizable.strings	2015-06-15 16:36:24 UTC (rev 185553)
+++ trunk/Source/WebCore/English.lproj/Localizable.strings	2015-06-15 16:57:01 UTC (rev 185554)
@@ -91,6 +91,9 @@
 /* WKErrorJavaScriptExceptionOccurred description */
 "A _javascript_ exception occurred" = "A _javascript_ exception occurred";
 
+/* WKErrorJavaScriptResultTypeIsUnsupported */
+"_javascript_ execution returned a result of an unsupported type" = "_javascript_ execution returned a result of an unsupported type";
+
 /* action menu item */
 "Add to Photos (action menu item)" = "Add to Photos";
 

Modified: trunk/Source/WebKit2/ChangeLog (185553 => 185554)


--- trunk/Source/WebKit2/ChangeLog	2015-06-15 16:36:24 UTC (rev 185553)
+++ trunk/Source/WebKit2/ChangeLog	2015-06-15 16:57:01 UTC (rev 185554)
@@ -1,3 +1,32 @@
+2015-06-12  Alexey Proskuryakov  <[email protected]>
+
+        -[WKWebView evaluateJavaScript] provides a misleading error when the return cannot be serialized
+        https://bugs.webkit.org/show_bug.cgi?id=145900
+
+        Reviewed by Sam Weinig.
+
+        We used to conflate to cases: (1) when there was a _javascript_ exception, and
+        (2) when the result could not be serialized for IPC. Turns out that it was quite
+        misleading!
+
+        * UIProcess/API/C/WKPage.cpp:
+        (WKPageRunJavaScriptInMainFrame):
+        * UIProcess/API/Cocoa/WKError.h:
+        * UIProcess/API/Cocoa/WKError.mm:
+        (localizedDescriptionForErrorCode):
+        * UIProcess/API/Cocoa/WKWebView.mm:
+        (-[WKWebView evaluateJavaScript:completionHandler:]):
+        * UIProcess/WebPageProxy.cpp:
+        (WebKit::WebPageProxy::runJavaScriptInMainFrame):
+        (WebKit::WebPageProxy::scriptValueCallback):
+        * UIProcess/WebPageProxy.h:
+        * UIProcess/WebPageProxy.messages.in:
+        * WebProcess/WebPage/WebPage.cpp:
+        (WebKit::WebPage::runJavaScriptInMainFrame):
+        * UIProcess/API/gtk/WebKitWebView.cpp:
+        (webkit_web_view_run_javascript):
+        (resourcesStreamReadCallback):
+
 2015-06-15  Carlos Garcia Campos  <[email protected]>
 
         [SOUP] Custom URI schemes don't work for requests containing a fragment identifier

Modified: trunk/Source/WebKit2/UIProcess/API/C/WKPage.cpp (185553 => 185554)


--- trunk/Source/WebKit2/UIProcess/API/C/WKPage.cpp	2015-06-15 16:36:24 UTC (rev 185553)
+++ trunk/Source/WebKit2/UIProcess/API/C/WKPage.cpp	2015-06-15 16:57:01 UTC (rev 185554)
@@ -1944,7 +1944,9 @@
 
 void WKPageRunJavaScriptInMainFrame(WKPageRef pageRef, WKStringRef scriptRef, void* context, WKPageRunJavaScriptFunction callback)
 {
-    toImpl(pageRef)->runJavaScriptInMainFrame(toImpl(scriptRef)->string(), toGenericCallbackFunction(context, callback));
+    toImpl(pageRef)->runJavaScriptInMainFrame(toImpl(scriptRef)->string(), [context, callback](API::SerializedScriptValue* returnValue, bool, CallbackBase::Error error) {
+        callback(toAPI(returnValue), (error != CallbackBase::Error::None) ? toAPI(API::Error::create().ptr()) : 0, context);
+    });
 }
 
 #ifdef __BLOCKS__

Modified: trunk/Source/WebKit2/UIProcess/API/Cocoa/WKError.h (185553 => 185554)


--- trunk/Source/WebKit2/UIProcess/API/Cocoa/WKError.h	2015-06-15 16:36:24 UTC (rev 185553)
+++ trunk/Source/WebKit2/UIProcess/API/Cocoa/WKError.h	2015-06-15 16:57:01 UTC (rev 185554)
@@ -36,16 +36,18 @@
 
 /*! @enum WKErrorCode
  @abstract Constants used by NSError to indicate errors in the WebKit domain.
- @constant WKErrorUnknown                       Indicates that an unknown error occurred.
- @constant WKErrorWebContentProcessTerminated   Indicates that the Web Content process was terminated.
- @constant WKErrorWebViewInvalidated            Indicates that the WKWebView was invalidated.
- @constant WKErrorJavaScriptExceptionOccurred   Indicates that a _javascript_ exception occurred.
+ @constant WKErrorUnknown                           Indicates that an unknown error occurred.
+ @constant WKErrorWebContentProcessTerminated       Indicates that the Web Content process was terminated.
+ @constant WKErrorWebViewInvalidated                Indicates that the WKWebView was invalidated.
+ @constant WKErrorJavaScriptExceptionOccurred       Indicates that a _javascript_ exception occurred.
+ @constant WKErrorJavaScriptResultTypeIsUnsupported Indicates that the result of _javascript_ execution could not be returned.
  */
 typedef NS_ENUM(NSInteger, WKErrorCode) {
     WKErrorUnknown = 1,
     WKErrorWebContentProcessTerminated,
     WKErrorWebViewInvalidated,
     WKErrorJavaScriptExceptionOccurred,
+    WKErrorJavaScriptResultTypeIsUnsupported WK_ENUM_AVAILABLE(WK_MAC_TBA, WK_IOS_TBA),
 } WK_ENUM_AVAILABLE(10_10, 8_0);
 
 WK_ASSUME_NONNULL_END

Modified: trunk/Source/WebKit2/UIProcess/API/Cocoa/WKError.mm (185553 => 185554)


--- trunk/Source/WebKit2/UIProcess/API/Cocoa/WKError.mm	2015-06-15 16:36:24 UTC (rev 185553)
+++ trunk/Source/WebKit2/UIProcess/API/Cocoa/WKError.mm	2015-06-15 16:57:01 UTC (rev 185554)
@@ -49,6 +49,9 @@
 
     case WKErrorJavaScriptExceptionOccurred:
         return WEB_UI_STRING("A _javascript_ exception occurred", "WKErrorJavaScriptExceptionOccurred description");
+
+    case WKErrorJavaScriptResultTypeIsUnsupported:
+        return WEB_UI_STRING("_javascript_ execution returned a result of an unsupported type", "WKErrorJavaScriptResultTypeIsUnsupported description");
     }
 }
 

Modified: trunk/Source/WebKit2/UIProcess/API/Cocoa/WKWebView.mm (185553 => 185554)


--- trunk/Source/WebKit2/UIProcess/API/Cocoa/WKWebView.mm	2015-06-15 16:36:24 UTC (rev 185553)
+++ trunk/Source/WebKit2/UIProcess/API/Cocoa/WKWebView.mm	2015-06-15 16:57:01 UTC (rev 185554)
@@ -590,7 +590,7 @@
 {
     auto handler = adoptNS([completionHandler copy]);
 
-    _page->runJavaScriptInMainFrame(_javascript_String, [handler](API::SerializedScriptValue* serializedScriptValue, WebKit::ScriptValueCallback::Error errorCode) {
+    _page->runJavaScriptInMainFrame(_javascript_String, [handler](API::SerializedScriptValue* serializedScriptValue, bool hadException, WebKit::ScriptValueCallback::Error errorCode) {
         if (!handler)
             return;
 
@@ -613,11 +613,17 @@
         }
 
         auto rawHandler = (void (^)(id, NSError *))handler.get();
-        if (!serializedScriptValue) {
+        if (hadException) {
+            ASSERT(!serializedScriptValue);
             rawHandler(nil, createNSError(WKErrorJavaScriptExceptionOccurred).get());
             return;
         }
 
+        if (!serializedScriptValue) {
+            rawHandler(nil, createNSError(WKErrorJavaScriptResultTypeIsUnsupported).get());
+            return;
+        }
+
         auto context = adoptNS([[JSContext alloc] init]);
         JSValueRef valueRef = serializedScriptValue->deserialize([context JSGlobalContextRef], 0);
         JSValue *value = [JSValue valueWithJSValueRef:valueRef inContext:context.get()];

Modified: trunk/Source/WebKit2/UIProcess/API/gtk/WebKitWebView.cpp (185553 => 185554)


--- trunk/Source/WebKit2/UIProcess/API/gtk/WebKitWebView.cpp	2015-06-15 16:36:24 UTC (rev 185553)
+++ trunk/Source/WebKit2/UIProcess/API/gtk/WebKitWebView.cpp	2015-06-15 16:57:01 UTC (rev 185554)
@@ -3008,7 +3008,7 @@
     g_return_if_fail(script);
 
     GTask* task = g_task_new(webView, cancellable, callback, userData);
-    getPage(webView)->runJavaScriptInMainFrame(String::fromUTF8(script), [task](API::SerializedScriptValue* serializedScriptValue, WebKit::CallbackBase::Error) {
+    getPage(webView)->runJavaScriptInMainFrame(String::fromUTF8(script), [task](API::SerializedScriptValue* serializedScriptValue, bool, WebKit::CallbackBase::Error) {
         webkitWebViewRunJavaScriptCallback(serializedScriptValue, adoptGRef(task).get());
     });
 }
@@ -3099,7 +3099,7 @@
     WebKitWebView* webView = WEBKIT_WEB_VIEW(g_task_get_source_object(task.get()));
     gpointer outputStreamData = g_memory_output_stream_get_data(G_MEMORY_OUTPUT_STREAM(object));
     getPage(webView)->runJavaScriptInMainFrame(String::fromUTF8(reinterpret_cast<const gchar*>(outputStreamData)),
-        [task](API::SerializedScriptValue* serializedScriptValue, WebKit::CallbackBase::Error) {
+        [task](API::SerializedScriptValue* serializedScriptValue, bool, WebKit::CallbackBase::Error) {
             webkitWebViewRunJavaScriptCallback(serializedScriptValue, task.get());
         });
 }

Modified: trunk/Source/WebKit2/UIProcess/WebPageProxy.cpp (185553 => 185554)


--- trunk/Source/WebKit2/UIProcess/WebPageProxy.cpp	2015-06-15 16:36:24 UTC (rev 185553)
+++ trunk/Source/WebKit2/UIProcess/WebPageProxy.cpp	2015-06-15 16:57:01 UTC (rev 185554)
@@ -2574,10 +2574,10 @@
     m_process->send(Messages::WebPage::CountStringMatches(string, options, maxMatchCount), m_pageID);
 }
 
-void WebPageProxy::runJavaScriptInMainFrame(const String& script, std::function<void (API::SerializedScriptValue*, CallbackBase::Error)> callbackFunction)
+void WebPageProxy::runJavaScriptInMainFrame(const String& script, std::function<void (API::SerializedScriptValue*, bool hadException, CallbackBase::Error)> callbackFunction)
 {
     if (!isValid()) {
-        callbackFunction(nullptr, CallbackBase::Error::Unknown);
+        callbackFunction(nullptr, false, CallbackBase::Error::Unknown);
         return;
     }
 
@@ -4644,7 +4644,7 @@
     callback->performCallbackWithReturnValue(resultString.impl());
 }
 
-void WebPageProxy::scriptValueCallback(const IPC::DataReference& dataReference, uint64_t callbackID)
+void WebPageProxy::scriptValueCallback(const IPC::DataReference& dataReference, bool hadException, uint64_t callbackID)
 {
     auto callback = m_callbacks.take<ScriptValueCallback>(callbackID);
     if (!callback) {
@@ -4656,7 +4656,7 @@
     data.reserveInitialCapacity(dataReference.size());
     data.append(dataReference.data(), dataReference.size());
 
-    callback->performCallbackWithReturnValue(data.size() ? API::SerializedScriptValue::adopt(data).get() : 0);
+    callback->performCallbackWithReturnValue(data.size() ? API::SerializedScriptValue::adopt(data).get() : 0, hadException);
 }
 
 void WebPageProxy::computedPagesCallback(const Vector<IntRect>& pageRects, double totalScaleFactorForPrinting, uint64_t callbackID)

Modified: trunk/Source/WebKit2/UIProcess/WebPageProxy.h (185553 => 185554)


--- trunk/Source/WebKit2/UIProcess/WebPageProxy.h	2015-06-15 16:36:24 UTC (rev 185553)
+++ trunk/Source/WebKit2/UIProcess/WebPageProxy.h	2015-06-15 16:57:01 UTC (rev 185554)
@@ -213,7 +213,7 @@
 typedef GenericCallback<uint64_t> UnsignedCallback;
 typedef GenericCallback<EditingRange> EditingRangeCallback;
 typedef GenericCallback<const String&> StringCallback;
-typedef GenericCallback<API::SerializedScriptValue*> ScriptValueCallback;
+typedef GenericCallback<API::SerializedScriptValue*, bool> ScriptValueCallback;
 
 #if PLATFORM(GTK)
 typedef GenericCallback<API::Error*> PrintFinishedCallback;
@@ -763,7 +763,7 @@
     void getSelectionAsWebArchiveData(std::function<void (API::Data*, CallbackBase::Error)>);
     void getSourceForFrame(WebFrameProxy*, std::function<void (const String&, CallbackBase::Error)>);
     void getWebArchiveOfFrame(WebFrameProxy*, std::function<void (API::Data*, CallbackBase::Error)>);
-    void runJavaScriptInMainFrame(const String&, std::function<void (API::SerializedScriptValue*, CallbackBase::Error)> callbackFunction);
+    void runJavaScriptInMainFrame(const String&, std::function<void (API::SerializedScriptValue*, bool hadException, CallbackBase::Error)> callbackFunction);
     void forceRepaint(PassRefPtr<VoidCallback>);
 
     float headerHeight(WebFrameProxy*);
@@ -1322,7 +1322,7 @@
     void dataCallback(const IPC::DataReference&, uint64_t);
     void imageCallback(const ShareableBitmap::Handle&, uint64_t);
     void stringCallback(const String&, uint64_t);
-    void scriptValueCallback(const IPC::DataReference&, uint64_t);
+    void scriptValueCallback(const IPC::DataReference&, bool hadException, uint64_t);
     void computedPagesCallback(const Vector<WebCore::IntRect>&, double totalScaleFactorForPrinting, uint64_t);
     void validateCommandCallback(const String&, bool, int, uint64_t);
     void unsignedCallback(uint64_t, uint64_t);

Modified: trunk/Source/WebKit2/UIProcess/WebPageProxy.messages.in (185553 => 185554)


--- trunk/Source/WebKit2/UIProcess/WebPageProxy.messages.in	2015-06-15 16:36:24 UTC (rev 185553)
+++ trunk/Source/WebKit2/UIProcess/WebPageProxy.messages.in	2015-06-15 16:57:01 UTC (rev 185554)
@@ -165,7 +165,7 @@
     DataCallback(IPC::DataReference resultData, uint64_t callbackID)
     ImageCallback(WebKit::ShareableBitmap::Handle bitmapHandle, uint64_t callbackID)
     StringCallback(String resultString, uint64_t callbackID)
-    ScriptValueCallback(IPC::DataReference resultData, uint64_t callbackID)
+    ScriptValueCallback(IPC::DataReference resultData, bool hadException, uint64_t callbackID)
     ComputedPagesCallback(Vector<WebCore::IntRect> pageRects, double totalScaleFactorForPrinting, uint64_t callbackID)
     ValidateCommandCallback(String command, bool isEnabled, int32_t state, uint64_t callbackID)
     EditingRangeCallback(struct WebKit::EditingRange range, uint64_t callbackID)

Modified: trunk/Source/WebKit2/WebProcess/WebPage/WebPage.cpp (185553 => 185554)


--- trunk/Source/WebKit2/WebProcess/WebPage/WebPage.cpp	2015-06-15 16:36:24 UTC (rev 185553)
+++ trunk/Source/WebKit2/WebProcess/WebPage/WebPage.cpp	2015-06-15 16:57:01 UTC (rev 185554)
@@ -2529,7 +2529,9 @@
 
     RefPtr<SerializedScriptValue> serializedResultValue;
     JSLockHolder lock(JSDOMWindow::commonVM());
+    bool hadException = true;
     if (JSValue resultValue = m_mainFrame->coreFrame()->script().executeScript(script, true).jsValue()) {
+        hadException = false;
         serializedResultValue = SerializedScriptValue::create(m_mainFrame->jsContext(),
             toRef(m_mainFrame->coreFrame()->script().globalObject(mainThreadNormalWorld())->globalExec(), resultValue), nullptr);
     }
@@ -2537,7 +2539,7 @@
     IPC::DataReference dataReference;
     if (serializedResultValue)
         dataReference = serializedResultValue->data();
-    send(Messages::WebPageProxy::ScriptValueCallback(dataReference, callbackID));
+    send(Messages::WebPageProxy::ScriptValueCallback(dataReference, hadException, callbackID));
 }
 
 void WebPage::getContentsAsString(uint64_t callbackID)

Modified: trunk/Tools/ChangeLog (185553 => 185554)


--- trunk/Tools/ChangeLog	2015-06-15 16:36:24 UTC (rev 185553)
+++ trunk/Tools/ChangeLog	2015-06-15 16:57:01 UTC (rev 185554)
@@ -1,3 +1,13 @@
+2015-06-12  Alexey Proskuryakov  <[email protected]>
+
+        -[WKWebView evaluateJavaScript] provides a misleading error when the return cannot be serialized
+        https://bugs.webkit.org/show_bug.cgi?id=145900
+
+        Reviewed by Sam Weinig.
+
+        * TestWebKitAPI/Tests/WebKit2Cocoa/WKWebViewEvaluateJavaScript.mm:
+        (TEST): Added tests.
+
 2015-06-15  Carlos Garcia Campos  <[email protected]>
 
         [SOUP] Custom URI schemes don't work for requests containing a fragment identifier

Modified: trunk/Tools/TestWebKitAPI/Tests/WebKit2Cocoa/WKWebViewEvaluateJavaScript.mm (185553 => 185554)


--- trunk/Tools/TestWebKitAPI/Tests/WebKit2Cocoa/WKWebViewEvaluateJavaScript.mm	2015-06-15 16:36:24 UTC (rev 185553)
+++ trunk/Tools/TestWebKitAPI/Tests/WebKit2Cocoa/WKWebViewEvaluateJavaScript.mm	2015-06-15 16:57:01 UTC (rev 185554)
@@ -60,4 +60,34 @@
     TestWebKitAPI::Util::run(&isDone);
 }
 
+TEST(WKWebView, EvaluateJavaScriptErrorCases)
+{
+    RetainPtr<WKWebView> webView = adoptNS([[WKWebView alloc] initWithFrame:NSMakeRect(0, 0, 800, 600)]);
+
+    NSURLRequest *request = [NSURLRequest requestWithURL:[[NSBundle mainBundle] URLForResource:@"simple" withExtension:@"html" subdirectory:@"TestWebKitAPI.resources"]];
+    [webView loadRequest:request];
+
+    [webView evaluateJavaScript:@"document.body" completionHandler:^(id result, NSError *error) {
+        EXPECT_NULL(result);
+        EXPECT_WK_STREQ(@"WKErrorDomain", [error domain]);
+        EXPECT_EQ(5, [error code]);
+
+        isDone = true;
+    }];
+
+    isDone = false;
+    TestWebKitAPI::Util::run(&isDone);
+
+    [webView evaluateJavaScript:@"document.body.insertBefore(document, document)" completionHandler:^(id result, NSError *error) {
+        EXPECT_NULL(result);
+        EXPECT_WK_STREQ(@"WKErrorDomain", [error domain]);
+        EXPECT_EQ(4, [error code]);
+
+        isDone = true;
+    }];
+
+    isDone = false;
+    TestWebKitAPI::Util::run(&isDone);
+}
+
 #endif
_______________________________________________
webkit-changes mailing list
[email protected]
https://lists.webkit.org/mailman/listinfo/webkit-changes

Reply via email to