<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>[198737] trunk/Source/WebKit2</title>
</head>
<body>

<style type="text/css"><!--
#msg dl.meta { border: 1px #006 solid; background: #369; padding: 6px; color: #fff; }
#msg dl.meta dt { float: left; width: 6em; font-weight: bold; }
#msg dt:after { content:':';}
#msg dl, #msg dt, #msg ul, #msg li, #header, #footer, #logmsg { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt;  }
#msg dl a { font-weight: bold}
#msg dl a:link    { color:#fc3; }
#msg dl a:active  { color:#ff0; }
#msg dl a:visited { color:#cc6; }
h3 { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; font-weight: bold; }
#msg pre { overflow: auto; background: #ffc; border: 1px #fa0 solid; padding: 6px; }
#logmsg { background: #ffc; border: 1px #fa0 solid; padding: 1em 1em 0 1em; }
#logmsg p, #logmsg pre, #logmsg blockquote { margin: 0 0 1em 0; }
#logmsg p, #logmsg li, #logmsg dt, #logmsg dd { line-height: 14pt; }
#logmsg h1, #logmsg h2, #logmsg h3, #logmsg h4, #logmsg h5, #logmsg h6 { margin: .5em 0; }
#logmsg h1:first-child, #logmsg h2:first-child, #logmsg h3:first-child, #logmsg h4:first-child, #logmsg h5:first-child, #logmsg h6:first-child { margin-top: 0; }
#logmsg ul, #logmsg ol { padding: 0; list-style-position: inside; margin: 0 0 0 1em; }
#logmsg ul { text-indent: -1em; padding-left: 1em; }#logmsg ol { text-indent: -1.5em; padding-left: 1.5em; }
#logmsg > ul, #logmsg > ol { margin: 0 0 1em 0; }
#logmsg pre { background: #eee; padding: 1em; }
#logmsg blockquote { border: 1px solid #fa0; border-left-width: 10px; padding: 1em 1em 0 1em; background: white;}
#logmsg dl { margin: 0; }
#logmsg dt { font-weight: bold; }
#logmsg dd { margin: 0; padding: 0 0 0.5em 0; }
#logmsg dd:before { content:'\00bb';}
#logmsg table { border-spacing: 0px; border-collapse: collapse; border-top: 4px solid #fa0; border-bottom: 1px solid #fa0; background: #fff; }
#logmsg table th { text-align: left; font-weight: normal; padding: 0.2em 0.5em; border-top: 1px dotted #fa0; }
#logmsg table td { text-align: right; border-top: 1px dotted #fa0; padding: 0.2em 0.5em; }
#logmsg table thead th { text-align: center; border-bottom: 1px solid #fa0; }
#logmsg table th.Corner { text-align: left; }
#logmsg hr { border: none 0; border-top: 2px dashed #fa0; height: 1px; }
#header, #footer { color: #fff; background: #636; border: 1px #300 solid; padding: 6px; }
#patch { width: 100%; }
#patch h4 {font-family: verdana,arial,helvetica,sans-serif;font-size:10pt;padding:8px;background:#369;color:#fff;margin:0;}
#patch .propset h4, #patch .binary h4 {margin:0;}
#patch pre {padding:0;line-height:1.2em;margin:0;}
#patch .diff {width:100%;background:#eee;padding: 0 0 10px 0;overflow:auto;}
#patch .propset .diff, #patch .binary .diff  {padding:10px 0;}
#patch span {display:block;padding:0 10px;}
#patch .modfile, #patch .addfile, #patch .delfile, #patch .propset, #patch .binary, #patch .copfile {border:1px solid #ccc;margin:10px 0;}
#patch ins {background:#dfd;text-decoration:none;display:block;padding:0 10px;}
#patch del {background:#fdd;text-decoration:none;display:block;padding:0 10px;}
#patch .lines, .info {color:#888;background:#fff;}
--></style>
<div id="msg">
<dl class="meta">
<dt>Revision</dt> <dd><a href="http://trac.webkit.org/projects/webkit/changeset/198737">198737</a></dd>
<dt>Author</dt> <dd>timothy@apple.com</dd>
<dt>Date</dt> <dd>2016-03-28 08:57:06 -0700 (Mon, 28 Mar 2016)</dd>
</dl>

<h3>Log Message</h3>
<pre>Web Automation: Add Automation.evaluateJavaScriptFunction

https://bugs.webkit.org/show_bug.cgi?id=155524
rdar://problem/25181888

Reviewed by Joseph Pecoraro.

* UIProcess/Automation/Automation.json: Added evaluateJavaScriptFunction command.

* UIProcess/Automation/WebAutomationSession.cpp:
(WebKit::WebAutomationSession::evaluateJavaScriptFunction): Added.
(WebKit::WebAutomationSession::didEvaluateJavaScriptFunction): Added.
* UIProcess/Automation/WebAutomationSession.h:
* UIProcess/Automation/WebAutomationSession.messages.in: Added didEvaluateJavaScriptFunction.

* WebProcess/Automation/WebAutomationSessionProxy.cpp:
(WebKit::toJSArray): Added.
(WebKit::callPropertyFunction): Added.
(WebKit::evaluateJavaScriptCallback): Added.
(WebKit::WebAutomationSessionProxy::didClearWindowObjectForFrame): Dispatch pending callbacks as errors.
(WebKit::WebAutomationSessionProxy::evaluateJavaScriptFunction): Added.
(WebKit::WebAutomationSessionProxy::didEvaluateJavaScriptFunction): Added.
* WebProcess/Automation/WebAutomationSessionProxy.h:

* WebProcess/Automation/WebAutomationSessionProxy.js:
(AutomationSessionProxy): Added maps for node handles.
(AutomationSessionProxy.prototype.evaluateJavaScriptFunction): Added.
(AutomationSessionProxy.prototype._jsonParse): Added.
(AutomationSessionProxy.prototype._jsonStringify): Added.
(AutomationSessionProxy.prototype._reviveJSONValue): Added.
(AutomationSessionProxy.prototype._replaceJSONValue): Added.
(AutomationSessionProxy.prototype._createNodeHandle): Added.
(AutomationSessionProxy.prototype._nodeForIdentifier): Added.
(AutomationSessionProxy.prototype._identifierForNode): Added.

* WebProcess/Automation/WebAutomationSessionProxy.messages.in: Added evaluateJavaScriptFunction.</pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#trunkSourceWebKit2ChangeLog">trunk/Source/WebKit2/ChangeLog</a></li>
<li><a href="#trunkSourceWebKit2UIProcessAutomationAutomationjson">trunk/Source/WebKit2/UIProcess/Automation/Automation.json</a></li>
<li><a href="#trunkSourceWebKit2UIProcessAutomationWebAutomationSessioncpp">trunk/Source/WebKit2/UIProcess/Automation/WebAutomationSession.cpp</a></li>
<li><a href="#trunkSourceWebKit2UIProcessAutomationWebAutomationSessionh">trunk/Source/WebKit2/UIProcess/Automation/WebAutomationSession.h</a></li>
<li><a href="#trunkSourceWebKit2UIProcessAutomationWebAutomationSessionmessagesin">trunk/Source/WebKit2/UIProcess/Automation/WebAutomationSession.messages.in</a></li>
<li><a href="#trunkSourceWebKit2WebProcessAutomationWebAutomationSessionProxycpp">trunk/Source/WebKit2/WebProcess/Automation/WebAutomationSessionProxy.cpp</a></li>
<li><a href="#trunkSourceWebKit2WebProcessAutomationWebAutomationSessionProxyh">trunk/Source/WebKit2/WebProcess/Automation/WebAutomationSessionProxy.h</a></li>
<li><a href="#trunkSourceWebKit2WebProcessAutomationWebAutomationSessionProxyjs">trunk/Source/WebKit2/WebProcess/Automation/WebAutomationSessionProxy.js</a></li>
<li><a href="#trunkSourceWebKit2WebProcessAutomationWebAutomationSessionProxymessagesin">trunk/Source/WebKit2/WebProcess/Automation/WebAutomationSessionProxy.messages.in</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunkSourceWebKit2ChangeLog"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebKit2/ChangeLog (198736 => 198737)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebKit2/ChangeLog        2016-03-28 15:56:53 UTC (rev 198736)
+++ trunk/Source/WebKit2/ChangeLog        2016-03-28 15:57:06 UTC (rev 198737)
</span><span class="lines">@@ -1,3 +1,42 @@
</span><ins>+2016-03-17  Timothy Hatcher  &lt;timothy@apple.com&gt;
+
+        Web Automation: Add Automation.evaluateJavaScriptFunction
+
+        https://bugs.webkit.org/show_bug.cgi?id=155524
+        rdar://problem/25181888
+
+        Reviewed by Joseph Pecoraro.
+
+        * UIProcess/Automation/Automation.json: Added evaluateJavaScriptFunction command.
+
+        * UIProcess/Automation/WebAutomationSession.cpp:
+        (WebKit::WebAutomationSession::evaluateJavaScriptFunction): Added.
+        (WebKit::WebAutomationSession::didEvaluateJavaScriptFunction): Added.
+        * UIProcess/Automation/WebAutomationSession.h:
+        * UIProcess/Automation/WebAutomationSession.messages.in: Added didEvaluateJavaScriptFunction.
+
+        * WebProcess/Automation/WebAutomationSessionProxy.cpp:
+        (WebKit::toJSArray): Added.
+        (WebKit::callPropertyFunction): Added.
+        (WebKit::evaluateJavaScriptCallback): Added.
+        (WebKit::WebAutomationSessionProxy::didClearWindowObjectForFrame): Dispatch pending callbacks as errors.
+        (WebKit::WebAutomationSessionProxy::evaluateJavaScriptFunction): Added.
+        (WebKit::WebAutomationSessionProxy::didEvaluateJavaScriptFunction): Added.
+        * WebProcess/Automation/WebAutomationSessionProxy.h:
+
+        * WebProcess/Automation/WebAutomationSessionProxy.js:
+        (AutomationSessionProxy): Added maps for node handles.
+        (AutomationSessionProxy.prototype.evaluateJavaScriptFunction): Added.
+        (AutomationSessionProxy.prototype._jsonParse): Added.
+        (AutomationSessionProxy.prototype._jsonStringify): Added.
+        (AutomationSessionProxy.prototype._reviveJSONValue): Added.
+        (AutomationSessionProxy.prototype._replaceJSONValue): Added.
+        (AutomationSessionProxy.prototype._createNodeHandle): Added.
+        (AutomationSessionProxy.prototype._nodeForIdentifier): Added.
+        (AutomationSessionProxy.prototype._identifierForNode): Added.
+
+        * WebProcess/Automation/WebAutomationSessionProxy.messages.in: Added evaluateJavaScriptFunction.
+
</ins><span class="cx"> 2016-03-14  Timothy Hatcher  &lt;timothy@apple.com&gt;
</span><span class="cx"> 
</span><span class="cx">         Add WebAutomationSessionProxy for WebProcess side automation tasks
</span></span></pre></div>
<a id="trunkSourceWebKit2UIProcessAutomationAutomationjson"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebKit2/UIProcess/Automation/Automation.json (198736 => 198737)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebKit2/UIProcess/Automation/Automation.json        2016-03-28 15:56:53 UTC (rev 198736)
+++ trunk/Source/WebKit2/UIProcess/Automation/Automation.json        2016-03-28 15:57:06 UTC (rev 198737)
</span><span class="lines">@@ -13,7 +13,9 @@
</span><span class="cx">             &quot;description&quot;: &quot;This enum contains predefined error messages that can be used to signal a well-defined error condition, such as a missing implementation, unknown window handle, and so forth. The backend signals one of these errors by using it as a prefix of the commands's error message (the errorString argument in generated C++ backend dispatchers). This will be reported to the frontend as a protocol error with a JSON-RPC error code of 'ServerError'. It is up to the frontend whether and how to deal with errors.&quot;,
</span><span class="cx">             &quot;enum&quot;: [
</span><span class="cx">                 &quot;InternalError&quot;,
</span><ins>+                &quot;JavaScriptError&quot;,
</ins><span class="cx">                 &quot;WindowNotFound&quot;,
</span><ins>+                &quot;NodeNotFound&quot;,
</ins><span class="cx">                 &quot;NotImplemented&quot;
</span><span class="cx">             ]
</span><span class="cx">         },
</span><span class="lines">@@ -95,6 +97,20 @@
</span><span class="cx">             &quot;parameters&quot;: [
</span><span class="cx">                 { &quot;name&quot;: &quot;handle&quot;, &quot;$ref&quot;: &quot;BrowsingContextHandle&quot;, &quot;description&quot;: &quot;The handle for the browsing context that should be reloaded.&quot; }
</span><span class="cx">             ]
</span><ins>+        },
+        {
+            &quot;name&quot;: &quot;evaluateJavaScriptFunction&quot;,
+            &quot;description&quot;: &quot;Evaluates a script function in a browsing context and calls it with the supplied arguments.&quot;,
+            &quot;parameters&quot;: [
+                { &quot;name&quot;: &quot;handle&quot;, &quot;$ref&quot;: &quot;BrowsingContextHandle&quot;, &quot;description&quot;: &quot;The handle for the browsing context the script should be evaluated.&quot; },
+                { &quot;name&quot;: &quot;function&quot;, &quot;type&quot;: &quot;string&quot;, &quot;description&quot;: &quot;The script to evaluate in the browsing context. The script is expected to be a function declaration, or otherwise evaluate to a function result.&quot; },
+                { &quot;name&quot;: &quot;arguments&quot;, &quot;type&quot;: &quot;array&quot;, &quot;items&quot;: { &quot;type&quot;: &quot;string&quot; }, &quot;description&quot;: &quot;The arguments to pass to the function when called. They will be parsed as JSON before calling the function.&quot; },
+                { &quot;name&quot;: &quot;expectsImplicitCallbackArgument&quot;, &quot;type&quot;: &quot;boolean&quot;, &quot;description&quot;: &quot;The function expects a callback function as the last argument. It is expected to call this callback with a result.&quot; }
+            ],
+            &quot;returns&quot;: [
+                { &quot;name&quot;: &quot;result&quot;, &quot;type&quot;: &quot;string&quot;, &quot;description&quot;: &quot;The result returned by the called function. It is JSON encoded after the function returns or calls the callback.&quot; }
+            ],
+            &quot;async&quot;: true
</ins><span class="cx">         }
</span><span class="cx">     ]
</span><span class="cx"> }
</span></span></pre></div>
<a id="trunkSourceWebKit2UIProcessAutomationWebAutomationSessioncpp"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebKit2/UIProcess/Automation/WebAutomationSession.cpp (198736 => 198737)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebKit2/UIProcess/Automation/WebAutomationSession.cpp        2016-03-28 15:56:53 UTC (rev 198736)
+++ trunk/Source/WebKit2/UIProcess/Automation/WebAutomationSession.cpp        2016-03-28 15:57:06 UTC (rev 198737)
</span><span class="lines">@@ -255,4 +255,39 @@
</span><span class="cx">     page-&gt;reload(reloadFromOrigin, contentBlockersEnabled);
</span><span class="cx"> }
</span><span class="cx"> 
</span><ins>+void WebAutomationSession::evaluateJavaScriptFunction(Inspector::ErrorString&amp; errorString, const String&amp; handle, const String&amp; function, const Inspector::InspectorArray&amp; arguments, bool expectsImplicitCallbackArgument, Ref&lt;EvaluateJavaScriptFunctionCallback&gt;&amp;&amp; callback)
+{
+    // FIXME 24172439: This should be a frame handle, not a page handle. Change this once we have frame support.
+    WebPageProxy* page = webPageProxyForHandle(handle);
+    if (!page)
+        FAIL_WITH_PREDEFINED_ERROR_MESSAGE(WindowNotFound);
+
+    Vector&lt;String&gt; argumentsVector;
+    argumentsVector.reserveCapacity(arguments.length());
+
+    for (auto&amp; argument : arguments) {
+        String argumentString;
+        argument-&gt;asString(argumentString);
+        argumentsVector.uncheckedAppend(argumentString);
+    }
+
+    uint64_t callbackID = m_nextEvaluateJavaScriptCallbackID++;
+    m_evaluateJavaScriptFunctionCallbacks.set(callbackID, WTFMove(callback));
+
+    page-&gt;process().send(Messages::WebAutomationSessionProxy::EvaluateJavaScriptFunction(page-&gt;mainFrame()-&gt;frameID(), function, argumentsVector, expectsImplicitCallbackArgument, callbackID), 0);
+}
+
+void WebAutomationSession::didEvaluateJavaScriptFunction(uint64_t callbackID, const String&amp; result, const String&amp; errorType)
+{
+    auto callback = m_evaluateJavaScriptFunctionCallbacks.take(callbackID);
+    if (!callback)
+        return;
+
+    if (!errorType.isEmpty()) {
+        // FIXME: We should send both the errorType and result, since result has the specific exception message.
+        callback-&gt;sendFailure(errorType);
+    } else
+        callback-&gt;sendSuccess(result);
+}
+
</ins><span class="cx"> } // namespace WebKit
</span></span></pre></div>
<a id="trunkSourceWebKit2UIProcessAutomationWebAutomationSessionh"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebKit2/UIProcess/Automation/WebAutomationSession.h (198736 => 198737)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebKit2/UIProcess/Automation/WebAutomationSession.h        2016-03-28 15:56:53 UTC (rev 198736)
+++ trunk/Source/WebKit2/UIProcess/Automation/WebAutomationSession.h        2016-03-28 15:57:06 UTC (rev 198737)
</span><span class="lines">@@ -89,6 +89,7 @@
</span><span class="cx">     void goBackInBrowsingContext(Inspector::ErrorString&amp;, const String&amp;) override;
</span><span class="cx">     void goForwardInBrowsingContext(Inspector::ErrorString&amp;, const String&amp;) override;
</span><span class="cx">     void reloadBrowsingContext(Inspector::ErrorString&amp;, const String&amp;) override;
</span><ins>+    void evaluateJavaScriptFunction(Inspector::ErrorString&amp;, const String&amp; handle, const String&amp; function, const Inspector::InspectorArray&amp; arguments, bool expectsImplicitCallbackArgument, Ref&lt;Inspector::AutomationBackendDispatcherHandler::EvaluateJavaScriptFunctionCallback&gt;&amp;&amp;) override;
</ins><span class="cx"> 
</span><span class="cx"> private:
</span><span class="cx">     WebKit::WebPageProxy* webPageProxyForHandle(const String&amp;);
</span><span class="lines">@@ -98,8 +99,7 @@
</span><span class="cx">     void didReceiveMessage(IPC::Connection&amp;, IPC::MessageDecoder&amp;) override;
</span><span class="cx"> 
</span><span class="cx">     // Called by WebAutomationSession messages
</span><del>-    // FIXME: Add message functions here.
-    void test() { };
</del><ins>+    void didEvaluateJavaScriptFunction(uint64_t callbackID, const String&amp; result, const String&amp; errorType);
</ins><span class="cx"> 
</span><span class="cx">     WebKit::WebProcessPool* m_processPool { nullptr };
</span><span class="cx">     std::unique_ptr&lt;API::AutomationSessionClient&gt; m_client;
</span><span class="lines">@@ -112,6 +112,9 @@
</span><span class="cx">     HandleWebPageMap m_handleWebPageMap;
</span><span class="cx">     String m_activeBrowsingContextHandle;
</span><span class="cx"> 
</span><ins>+    uint64_t m_nextEvaluateJavaScriptCallbackID { 1 };
+    HashMap&lt;uint64_t, RefPtr&lt;Inspector::AutomationBackendDispatcherHandler::EvaluateJavaScriptFunctionCallback&gt;&gt; m_evaluateJavaScriptFunctionCallbacks;
+
</ins><span class="cx"> #if ENABLE(REMOTE_INSPECTOR)
</span><span class="cx">     Inspector::FrontendChannel* m_remoteChannel { nullptr };
</span><span class="cx"> #endif
</span></span></pre></div>
<a id="trunkSourceWebKit2UIProcessAutomationWebAutomationSessionmessagesin"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebKit2/UIProcess/Automation/WebAutomationSession.messages.in (198736 => 198737)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebKit2/UIProcess/Automation/WebAutomationSession.messages.in        2016-03-28 15:56:53 UTC (rev 198736)
+++ trunk/Source/WebKit2/UIProcess/Automation/WebAutomationSession.messages.in        2016-03-28 15:57:06 UTC (rev 198737)
</span><span class="lines">@@ -21,6 +21,5 @@
</span><span class="cx"> # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
</span><span class="cx"> 
</span><span class="cx"> messages -&gt; WebAutomationSession {
</span><del>-    // FIXME: Add messages here.
-    Test()
</del><ins>+    DidEvaluateJavaScriptFunction(uint64_t callbackID, String result, String errorType)
</ins><span class="cx"> }
</span></span></pre></div>
<a id="trunkSourceWebKit2WebProcessAutomationWebAutomationSessionProxycpp"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebKit2/WebProcess/Automation/WebAutomationSessionProxy.cpp (198736 => 198737)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebKit2/WebProcess/Automation/WebAutomationSessionProxy.cpp        2016-03-28 15:56:53 UTC (rev 198736)
+++ trunk/Source/WebKit2/WebProcess/Automation/WebAutomationSessionProxy.cpp        2016-03-28 15:57:06 UTC (rev 198737)
</span><span class="lines">@@ -26,6 +26,7 @@
</span><span class="cx"> #include &quot;config.h&quot;
</span><span class="cx"> #include &quot;WebAutomationSessionProxy.h&quot;
</span><span class="cx"> 
</span><ins>+#include &quot;InspectorProtocolObjects.h&quot;
</ins><span class="cx"> #include &quot;WebAutomationSessionMessages.h&quot;
</span><span class="cx"> #include &quot;WebAutomationSessionProxyMessages.h&quot;
</span><span class="cx"> #include &quot;WebAutomationSessionProxyScriptSource.h&quot;
</span><span class="lines">@@ -39,6 +40,31 @@
</span><span class="cx"> 
</span><span class="cx"> namespace WebKit {
</span><span class="cx"> 
</span><ins>+template &lt;typename T&gt;
+static JSObjectRef toJSArray(JSContextRef context, const Vector&lt;T&gt;&amp; data, JSValueRef (*converter)(JSContextRef, const T&amp;), JSValueRef* exception)
+{
+    ASSERT_ARG(converter, converter);
+
+    if (data.isEmpty())
+        return JSObjectMakeArray(context, 0, nullptr, exception);
+
+    Vector&lt;JSValueRef, 8&gt; convertedData;
+    convertedData.reserveCapacity(data.size());
+
+    for (auto&amp; originalValue : data) {
+        JSValueRef convertedValue = converter(context, originalValue);
+        JSValueProtect(context, convertedValue);
+        convertedData.uncheckedAppend(convertedValue);
+    }
+
+    JSObjectRef array = JSObjectMakeArray(context, convertedData.size(), convertedData.data(), exception);
+
+    for (auto&amp; convertedValue : convertedData)
+        JSValueUnprotect(context, convertedValue);
+
+    return array;
+}
+
</ins><span class="cx"> static inline JSRetainPtr&lt;JSStringRef&gt; toJSString(const String&amp; string)
</span><span class="cx"> {
</span><span class="cx">     return JSRetainPtr&lt;JSStringRef&gt;(Adopt, OpaqueJSString::create(string).leakRef());
</span><span class="lines">@@ -49,6 +75,17 @@
</span><span class="cx">     return JSValueMakeString(context, toJSString(string).get());
</span><span class="cx"> }
</span><span class="cx"> 
</span><ins>+static inline JSValueRef callPropertyFunction(JSContextRef context, JSObjectRef object, const String&amp; propertyName, size_t argumentCount, const JSValueRef arguments[], JSValueRef* exception)
+{
+    ASSERT_ARG(object, object);
+    ASSERT_ARG(object, JSValueIsObject(context, object));
+
+    JSObjectRef function = const_cast&lt;JSObjectRef&gt;(JSObjectGetProperty(context, object, toJSString(propertyName).get(), exception));
+    ASSERT(JSObjectIsFunction(context, function));
+
+    return JSObjectCallAsFunction(context, function, object, argumentCount, arguments, exception);
+}
+
</ins><span class="cx"> WebAutomationSessionProxy::WebAutomationSessionProxy(const String&amp; sessionIdentifier)
</span><span class="cx">     : m_sessionIdentifier(sessionIdentifier)
</span><span class="cx"> {
</span><span class="lines">@@ -77,6 +114,26 @@
</span><span class="cx">     return toJSValue(context, WebCore::createCanonicalUUIDString().convertToASCIIUppercase());
</span><span class="cx"> }
</span><span class="cx"> 
</span><ins>+static JSValueRef evaluateJavaScriptCallback(JSContextRef context, JSObjectRef function, JSObjectRef thisObject, size_t argumentCount, const JSValueRef arguments[], JSValueRef* exception)
+{
+    ASSERT_ARG(argumentCount, argumentCount == 3);
+    ASSERT_ARG(arguments, JSValueIsNumber(context, arguments[0]));
+    ASSERT_ARG(arguments, JSValueIsNumber(context, arguments[1]));
+    ASSERT_ARG(arguments, JSValueIsString(context, arguments[2]));
+
+    auto automationSessionProxy = WebProcess::singleton().automationSessionProxy();
+    if (!automationSessionProxy)
+        return JSValueMakeUndefined(context);
+
+    uint64_t frameID = JSValueToNumber(context, arguments[0], exception);
+    uint64_t callbackID = JSValueToNumber(context, arguments[1], exception);
+    JSRetainPtr&lt;JSStringRef&gt; result(Adopt, JSValueToStringCopy(context, arguments[2], exception));
+
+    automationSessionProxy-&gt;didEvaluateJavaScriptFunction(frameID, callbackID, result-&gt;string(), emptyString());
+
+    return JSValueMakeUndefined(context);
+}
+
</ins><span class="cx"> JSObjectRef WebAutomationSessionProxy::scriptObjectForFrame(WebFrame&amp; frame)
</span><span class="cx"> {
</span><span class="cx">     if (JSObjectRef scriptObject = m_webFrameScriptObjectMap.get(frame.frameID()))
</span><span class="lines">@@ -106,8 +163,80 @@
</span><span class="cx"> 
</span><span class="cx"> void WebAutomationSessionProxy::didClearWindowObjectForFrame(WebFrame&amp; frame)
</span><span class="cx"> {
</span><del>-    if (JSObjectRef scriptObject = m_webFrameScriptObjectMap.take(frame.frameID()))
</del><ins>+    uint64_t frameID = frame.frameID();
+    if (JSObjectRef scriptObject = m_webFrameScriptObjectMap.take(frameID))
</ins><span class="cx">         JSValueUnprotect(frame.jsContext(), scriptObject);
</span><ins>+
+    String errorMessage = ASCIILiteral(&quot;Callback was not called before the unload event.&quot;);
+    String errorType = Inspector::Protocol::getEnumConstantValue(Inspector::Protocol::Automation::ErrorMessage::JavaScriptError);
+
+    auto pendingFrameCallbacks = m_webFramePendingEvaluateJavaScriptCallbacksMap.take(frameID);
+    for (uint64_t callbackID : pendingFrameCallbacks)
+        WebProcess::singleton().parentProcessConnection()-&gt;send(Messages::WebAutomationSession::DidEvaluateJavaScriptFunction(callbackID, emptyString(), errorType), 0);
</ins><span class="cx"> }
</span><span class="cx"> 
</span><ins>+void WebAutomationSessionProxy::evaluateJavaScriptFunction(uint64_t frameID, const String&amp; function, Vector&lt;String&gt; arguments, bool expectsImplicitCallbackArgument, uint64_t callbackID)
+{
+    WebFrame* frame = WebProcess::singleton().webFrame(frameID);
+    if (!frame)
+        return;
+
+    JSObjectRef scriptObject = scriptObjectForFrame(*frame);
+    if (!scriptObject)
+        return;
+
+    JSValueRef exception = nullptr;
+    JSGlobalContextRef context = frame-&gt;jsContext();
+
+    JSObjectRef callbackFunction = JSObjectMakeFunctionWithCallback(context, nullptr, evaluateJavaScriptCallback);
+
+    if (expectsImplicitCallbackArgument) {
+        auto result = m_webFramePendingEvaluateJavaScriptCallbacksMap.add(frameID, Vector&lt;uint64_t&gt;());
+        result.iterator-&gt;value.append(callbackID);
+    }
+
+    JSValueRef functionArguments[] = {
+        toJSValue(context, function),
+        toJSArray(context, arguments, toJSValue, &amp;exception),
+        JSValueMakeBoolean(context, expectsImplicitCallbackArgument),
+        JSValueMakeNumber(context, frameID),
+        JSValueMakeNumber(context, callbackID),
+        callbackFunction
+    };
+
+    callPropertyFunction(context, scriptObject, ASCIILiteral(&quot;evaluateJavaScriptFunction&quot;), WTF_ARRAY_LENGTH(functionArguments), functionArguments, &amp;exception);
+
+    if (!exception)
+        return;
+
+    String errorType = Inspector::Protocol::getEnumConstantValue(Inspector::Protocol::Automation::ErrorMessage::JavaScriptError);
+
+    JSRetainPtr&lt;JSStringRef&gt; exceptionMessage;
+    if (JSValueIsObject(context, exception)) {
+        JSValueRef nameValue = JSObjectGetProperty(context, const_cast&lt;JSObjectRef&gt;(exception), toJSString(ASCIILiteral(&quot;name&quot;)).get(), nullptr);
+        JSRetainPtr&lt;JSStringRef&gt; exceptionName(Adopt, JSValueToStringCopy(context, nameValue, nullptr));
+        if (exceptionName-&gt;string() == &quot;NodeNotFound&quot;)
+            errorType = Inspector::Protocol::getEnumConstantValue(Inspector::Protocol::Automation::ErrorMessage::NodeNotFound);
+
+        JSValueRef messageValue = JSObjectGetProperty(context, const_cast&lt;JSObjectRef&gt;(exception), toJSString(ASCIILiteral(&quot;message&quot;)).get(), nullptr);
+        exceptionMessage.adopt(JSValueToStringCopy(context, messageValue, nullptr));
+    } else
+        exceptionMessage.adopt(JSValueToStringCopy(context, exception, nullptr));
+
+    didEvaluateJavaScriptFunction(frameID, callbackID, exceptionMessage-&gt;string(), errorType);
+}
+
+void WebAutomationSessionProxy::didEvaluateJavaScriptFunction(uint64_t frameID, uint64_t callbackID, const String&amp; result, const String&amp; errorType)
+{
+    auto findResult = m_webFramePendingEvaluateJavaScriptCallbacksMap.find(frameID);
+    if (findResult != m_webFramePendingEvaluateJavaScriptCallbacksMap.end()) {
+        findResult-&gt;value.removeFirst(callbackID);
+        ASSERT(!findResult-&gt;value.contains(callbackID));
+        if (findResult-&gt;value.isEmpty())
+            m_webFramePendingEvaluateJavaScriptCallbacksMap.remove(findResult);
+    }
+
+    WebProcess::singleton().parentProcessConnection()-&gt;send(Messages::WebAutomationSession::DidEvaluateJavaScriptFunction(callbackID, result, errorType), 0);
+}
+
</ins><span class="cx"> } // namespace WebKit
</span></span></pre></div>
<a id="trunkSourceWebKit2WebProcessAutomationWebAutomationSessionProxyh"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebKit2/WebProcess/Automation/WebAutomationSessionProxy.h (198736 => 198737)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebKit2/WebProcess/Automation/WebAutomationSessionProxy.h        2016-03-28 15:56:53 UTC (rev 198736)
+++ trunk/Source/WebKit2/WebProcess/Automation/WebAutomationSessionProxy.h        2016-03-28 15:57:06 UTC (rev 198737)
</span><span class="lines">@@ -43,6 +43,8 @@
</span><span class="cx"> 
</span><span class="cx">     void didClearWindowObjectForFrame(WebFrame&amp;);
</span><span class="cx"> 
</span><ins>+    void didEvaluateJavaScriptFunction(uint64_t frameID, uint64_t callbackID, const String&amp; result, const String&amp; errorType);
+
</ins><span class="cx"> private:
</span><span class="cx">     JSObjectRef scriptObjectForFrame(WebFrame&amp;);
</span><span class="cx"> 
</span><span class="lines">@@ -50,13 +52,12 @@
</span><span class="cx">     void didReceiveMessage(IPC::Connection&amp;, IPC::MessageDecoder&amp;);
</span><span class="cx"> 
</span><span class="cx">     // Called by WebAutomationSessionProxy messages
</span><del>-    // FIXME: Add message functions here.
-    void test() { };
</del><ins>+    void evaluateJavaScriptFunction(uint64_t frameID, const String&amp; function, Vector&lt;String&gt; arguments, bool expectsImplicitCallbackArgument, uint64_t callbackID);
</ins><span class="cx"> 
</span><span class="cx">     String m_sessionIdentifier;
</span><span class="cx"> 
</span><del>-    typedef HashMap&lt;uint64_t, JSObjectRef&gt; WebFrameScriptObjectMap;
-    WebFrameScriptObjectMap m_webFrameScriptObjectMap;
</del><ins>+    HashMap&lt;uint64_t, JSObjectRef&gt; m_webFrameScriptObjectMap;
+    HashMap&lt;uint64_t, Vector&lt;uint64_t&gt;&gt; m_webFramePendingEvaluateJavaScriptCallbacksMap;
</ins><span class="cx"> };
</span><span class="cx"> 
</span><span class="cx"> } // namespace WebKit
</span></span></pre></div>
<a id="trunkSourceWebKit2WebProcessAutomationWebAutomationSessionProxyjs"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebKit2/WebProcess/Automation/WebAutomationSessionProxy.js (198736 => 198737)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebKit2/WebProcess/Automation/WebAutomationSessionProxy.js        2016-03-28 15:56:53 UTC (rev 198736)
+++ trunk/Source/WebKit2/WebProcess/Automation/WebAutomationSessionProxy.js        2016-03-28 15:57:06 UTC (rev 198737)
</span><span class="lines">@@ -27,14 +27,91 @@
</span><span class="cx"> 
</span><span class="cx"> (function (sessionIdentifier, evaluate, createUUID) {
</span><span class="cx"> 
</span><del>-// Protect against Object overwritten by the page.
-let Object = {}.constructor;
</del><ins>+const sessionNodePropertyName = &quot;session-node-&quot; + sessionIdentifier;
</ins><span class="cx"> 
</span><span class="cx"> let AutomationSessionProxy = class AutomationSessionProxy
</span><span class="cx"> {
</span><ins>+    constructor()
+    {
+        this._nodeToIdMap = new Map;
+        this._idToNodeMap = new Map;
+    }
+
</ins><span class="cx">     // Public
</span><span class="cx"> 
</span><del>-    // FIXME: Add functions here.
</del><ins>+    evaluateJavaScriptFunction(functionString, argumentStrings, expectsImplicitCallbackArgument, frameID, callbackID, resultCallback)
+    {
+        // The script is expected to be a function declaration. Evaluate it inside parenthesis to get the function value.
+        let functionValue = evaluate(&quot;(&quot; + functionString + &quot;)&quot;);
+        if (typeof functionValue !== &quot;function&quot;)
+            throw new TypeError(&quot;Script did not evaluate to a function.&quot;);
+
+        let argumentValues = argumentStrings.map(this._jsonParse, this);
+        let callback = (result) =&gt; resultCallback(frameID, callbackID, this._jsonStringify(result));
+
+        if (expectsImplicitCallbackArgument) {
+            argumentValues.push(callback);
+            functionValue.apply(null, argumentValues);
+        } else
+            callback(functionValue.apply(null, argumentValues));
+    }
+
+    // Private
+
+    _jsonParse(string)
+    {
+        return JSON.parse(string, (key, value) =&gt; this._reviveJSONValue(key, value));
+    }
+
+    _jsonStringify(original)
+    {
+        return JSON.stringify(original, (key, value) =&gt; this._replaceJSONValue(key, value));
+    }
+
+    _reviveJSONValue(key, value)
+    {
+        if (value &amp;&amp; typeof value === &quot;object&quot; &amp;&amp; value[sessionNodePropertyName])
+            return this._nodeForIdentifier(value[sessionNodePropertyName]);
+        return value;
+    }
+
+    _replaceJSONValue(key, value)
+    {
+        if (value instanceof Node)
+            return this._createNodeHandle(value);
+
+        if (value instanceof NodeList || value instanceof HTMLCollection)
+            value = Array.from(value).map(this._createNodeHandle, this);
+
+        return value;
+    }
+
+    _createNodeHandle(node)
+    {
+        return {[sessionNodePropertyName]: this._identifierForNode(node)};
+    }
+
+    _nodeForIdentifier(identifier)
+    {
+        let node = this._idToNodeMap.get(identifier);
+        if (node)
+            return node;
+        throw {name: &quot;NodeNotFound&quot;, message: &quot;Node with identifier '&quot; + identifier + &quot;' was not found&quot;};
+    }
+
+    _identifierForNode(node)
+    {
+        let identifier = this._nodeToIdMap.get(node);
+        if (identifier)
+            return identifier;
+
+        identifier = &quot;node-&quot; + createUUID();
+
+        this._nodeToIdMap.set(node, identifier);
+        this._idToNodeMap.set(identifier, node);
+
+        return identifier;
+    }
</ins><span class="cx"> };
</span><span class="cx"> 
</span><span class="cx"> return new AutomationSessionProxy;
</span></span></pre></div>
<a id="trunkSourceWebKit2WebProcessAutomationWebAutomationSessionProxymessagesin"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebKit2/WebProcess/Automation/WebAutomationSessionProxy.messages.in (198736 => 198737)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebKit2/WebProcess/Automation/WebAutomationSessionProxy.messages.in        2016-03-28 15:56:53 UTC (rev 198736)
+++ trunk/Source/WebKit2/WebProcess/Automation/WebAutomationSessionProxy.messages.in        2016-03-28 15:57:06 UTC (rev 198737)
</span><span class="lines">@@ -21,6 +21,5 @@
</span><span class="cx"> # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
</span><span class="cx"> 
</span><span class="cx"> messages -&gt; WebAutomationSessionProxy {
</span><del>-    // FIXME: Add messages here.
-    Test()
</del><ins>+    EvaluateJavaScriptFunction(uint64_t frame, String function, Vector&lt;String&gt; arguments, bool expectsImplicitCallbackArgument, uint64_t callbackID)
</ins><span class="cx"> }
</span></span></pre>
</div>
</div>

</body>
</html>