<!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 <timothy@apple.com>
+
+ 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 <timothy@apple.com>
</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"> "description": "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.",
</span><span class="cx"> "enum": [
</span><span class="cx"> "InternalError",
</span><ins>+ "JavaScriptError",
</ins><span class="cx"> "WindowNotFound",
</span><ins>+ "NodeNotFound",
</ins><span class="cx"> "NotImplemented"
</span><span class="cx"> ]
</span><span class="cx"> },
</span><span class="lines">@@ -95,6 +97,20 @@
</span><span class="cx"> "parameters": [
</span><span class="cx"> { "name": "handle", "$ref": "BrowsingContextHandle", "description": "The handle for the browsing context that should be reloaded." }
</span><span class="cx"> ]
</span><ins>+ },
+ {
+ "name": "evaluateJavaScriptFunction",
+ "description": "Evaluates a script function in a browsing context and calls it with the supplied arguments.",
+ "parameters": [
+ { "name": "handle", "$ref": "BrowsingContextHandle", "description": "The handle for the browsing context the script should be evaluated." },
+ { "name": "function", "type": "string", "description": "The script to evaluate in the browsing context. The script is expected to be a function declaration, or otherwise evaluate to a function result." },
+ { "name": "arguments", "type": "array", "items": { "type": "string" }, "description": "The arguments to pass to the function when called. They will be parsed as JSON before calling the function." },
+ { "name": "expectsImplicitCallbackArgument", "type": "boolean", "description": "The function expects a callback function as the last argument. It is expected to call this callback with a result." }
+ ],
+ "returns": [
+ { "name": "result", "type": "string", "description": "The result returned by the called function. It is JSON encoded after the function returns or calls the callback." }
+ ],
+ "async": 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->reload(reloadFromOrigin, contentBlockersEnabled);
</span><span class="cx"> }
</span><span class="cx">
</span><ins>+void WebAutomationSession::evaluateJavaScriptFunction(Inspector::ErrorString& errorString, const String& handle, const String& function, const Inspector::InspectorArray& arguments, bool expectsImplicitCallbackArgument, Ref<EvaluateJavaScriptFunctionCallback>&& 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<String> argumentsVector;
+ argumentsVector.reserveCapacity(arguments.length());
+
+ for (auto& argument : arguments) {
+ String argumentString;
+ argument->asString(argumentString);
+ argumentsVector.uncheckedAppend(argumentString);
+ }
+
+ uint64_t callbackID = m_nextEvaluateJavaScriptCallbackID++;
+ m_evaluateJavaScriptFunctionCallbacks.set(callbackID, WTFMove(callback));
+
+ page->process().send(Messages::WebAutomationSessionProxy::EvaluateJavaScriptFunction(page->mainFrame()->frameID(), function, argumentsVector, expectsImplicitCallbackArgument, callbackID), 0);
+}
+
+void WebAutomationSession::didEvaluateJavaScriptFunction(uint64_t callbackID, const String& result, const String& 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->sendFailure(errorType);
+ } else
+ callback->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&, const String&) override;
</span><span class="cx"> void goForwardInBrowsingContext(Inspector::ErrorString&, const String&) override;
</span><span class="cx"> void reloadBrowsingContext(Inspector::ErrorString&, const String&) override;
</span><ins>+ void evaluateJavaScriptFunction(Inspector::ErrorString&, const String& handle, const String& function, const Inspector::InspectorArray& arguments, bool expectsImplicitCallbackArgument, Ref<Inspector::AutomationBackendDispatcherHandler::EvaluateJavaScriptFunctionCallback>&&) override;
</ins><span class="cx">
</span><span class="cx"> private:
</span><span class="cx"> WebKit::WebPageProxy* webPageProxyForHandle(const String&);
</span><span class="lines">@@ -98,8 +99,7 @@
</span><span class="cx"> void didReceiveMessage(IPC::Connection&, IPC::MessageDecoder&) 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& result, const String& errorType);
</ins><span class="cx">
</span><span class="cx"> WebKit::WebProcessPool* m_processPool { nullptr };
</span><span class="cx"> std::unique_ptr<API::AutomationSessionClient> 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<uint64_t, RefPtr<Inspector::AutomationBackendDispatcherHandler::EvaluateJavaScriptFunctionCallback>> 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 -> 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 "config.h"
</span><span class="cx"> #include "WebAutomationSessionProxy.h"
</span><span class="cx">
</span><ins>+#include "InspectorProtocolObjects.h"
</ins><span class="cx"> #include "WebAutomationSessionMessages.h"
</span><span class="cx"> #include "WebAutomationSessionProxyMessages.h"
</span><span class="cx"> #include "WebAutomationSessionProxyScriptSource.h"
</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 <typename T>
+static JSObjectRef toJSArray(JSContextRef context, const Vector<T>& data, JSValueRef (*converter)(JSContextRef, const T&), JSValueRef* exception)
+{
+ ASSERT_ARG(converter, converter);
+
+ if (data.isEmpty())
+ return JSObjectMakeArray(context, 0, nullptr, exception);
+
+ Vector<JSValueRef, 8> convertedData;
+ convertedData.reserveCapacity(data.size());
+
+ for (auto& originalValue : data) {
+ JSValueRef convertedValue = converter(context, originalValue);
+ JSValueProtect(context, convertedValue);
+ convertedData.uncheckedAppend(convertedValue);
+ }
+
+ JSObjectRef array = JSObjectMakeArray(context, convertedData.size(), convertedData.data(), exception);
+
+ for (auto& convertedValue : convertedData)
+ JSValueUnprotect(context, convertedValue);
+
+ return array;
+}
+
</ins><span class="cx"> static inline JSRetainPtr<JSStringRef> toJSString(const String& string)
</span><span class="cx"> {
</span><span class="cx"> return JSRetainPtr<JSStringRef>(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& propertyName, size_t argumentCount, const JSValueRef arguments[], JSValueRef* exception)
+{
+ ASSERT_ARG(object, object);
+ ASSERT_ARG(object, JSValueIsObject(context, object));
+
+ JSObjectRef function = const_cast<JSObjectRef>(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& 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<JSStringRef> result(Adopt, JSValueToStringCopy(context, arguments[2], exception));
+
+ automationSessionProxy->didEvaluateJavaScriptFunction(frameID, callbackID, result->string(), emptyString());
+
+ return JSValueMakeUndefined(context);
+}
+
</ins><span class="cx"> JSObjectRef WebAutomationSessionProxy::scriptObjectForFrame(WebFrame& 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& 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("Callback was not called before the unload event.");
+ String errorType = Inspector::Protocol::getEnumConstantValue(Inspector::Protocol::Automation::ErrorMessage::JavaScriptError);
+
+ auto pendingFrameCallbacks = m_webFramePendingEvaluateJavaScriptCallbacksMap.take(frameID);
+ for (uint64_t callbackID : pendingFrameCallbacks)
+ WebProcess::singleton().parentProcessConnection()->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& function, Vector<String> 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->jsContext();
+
+ JSObjectRef callbackFunction = JSObjectMakeFunctionWithCallback(context, nullptr, evaluateJavaScriptCallback);
+
+ if (expectsImplicitCallbackArgument) {
+ auto result = m_webFramePendingEvaluateJavaScriptCallbacksMap.add(frameID, Vector<uint64_t>());
+ result.iterator->value.append(callbackID);
+ }
+
+ JSValueRef functionArguments[] = {
+ toJSValue(context, function),
+ toJSArray(context, arguments, toJSValue, &exception),
+ JSValueMakeBoolean(context, expectsImplicitCallbackArgument),
+ JSValueMakeNumber(context, frameID),
+ JSValueMakeNumber(context, callbackID),
+ callbackFunction
+ };
+
+ callPropertyFunction(context, scriptObject, ASCIILiteral("evaluateJavaScriptFunction"), WTF_ARRAY_LENGTH(functionArguments), functionArguments, &exception);
+
+ if (!exception)
+ return;
+
+ String errorType = Inspector::Protocol::getEnumConstantValue(Inspector::Protocol::Automation::ErrorMessage::JavaScriptError);
+
+ JSRetainPtr<JSStringRef> exceptionMessage;
+ if (JSValueIsObject(context, exception)) {
+ JSValueRef nameValue = JSObjectGetProperty(context, const_cast<JSObjectRef>(exception), toJSString(ASCIILiteral("name")).get(), nullptr);
+ JSRetainPtr<JSStringRef> exceptionName(Adopt, JSValueToStringCopy(context, nameValue, nullptr));
+ if (exceptionName->string() == "NodeNotFound")
+ errorType = Inspector::Protocol::getEnumConstantValue(Inspector::Protocol::Automation::ErrorMessage::NodeNotFound);
+
+ JSValueRef messageValue = JSObjectGetProperty(context, const_cast<JSObjectRef>(exception), toJSString(ASCIILiteral("message")).get(), nullptr);
+ exceptionMessage.adopt(JSValueToStringCopy(context, messageValue, nullptr));
+ } else
+ exceptionMessage.adopt(JSValueToStringCopy(context, exception, nullptr));
+
+ didEvaluateJavaScriptFunction(frameID, callbackID, exceptionMessage->string(), errorType);
+}
+
+void WebAutomationSessionProxy::didEvaluateJavaScriptFunction(uint64_t frameID, uint64_t callbackID, const String& result, const String& errorType)
+{
+ auto findResult = m_webFramePendingEvaluateJavaScriptCallbacksMap.find(frameID);
+ if (findResult != m_webFramePendingEvaluateJavaScriptCallbacksMap.end()) {
+ findResult->value.removeFirst(callbackID);
+ ASSERT(!findResult->value.contains(callbackID));
+ if (findResult->value.isEmpty())
+ m_webFramePendingEvaluateJavaScriptCallbacksMap.remove(findResult);
+ }
+
+ WebProcess::singleton().parentProcessConnection()->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&);
</span><span class="cx">
</span><ins>+ void didEvaluateJavaScriptFunction(uint64_t frameID, uint64_t callbackID, const String& result, const String& errorType);
+
</ins><span class="cx"> private:
</span><span class="cx"> JSObjectRef scriptObjectForFrame(WebFrame&);
</span><span class="cx">
</span><span class="lines">@@ -50,13 +52,12 @@
</span><span class="cx"> void didReceiveMessage(IPC::Connection&, IPC::MessageDecoder&);
</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& function, Vector<String> 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<uint64_t, JSObjectRef> WebFrameScriptObjectMap;
- WebFrameScriptObjectMap m_webFrameScriptObjectMap;
</del><ins>+ HashMap<uint64_t, JSObjectRef> m_webFrameScriptObjectMap;
+ HashMap<uint64_t, Vector<uint64_t>> 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 = "session-node-" + 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("(" + functionString + ")");
+ if (typeof functionValue !== "function")
+ throw new TypeError("Script did not evaluate to a function.");
+
+ let argumentValues = argumentStrings.map(this._jsonParse, this);
+ let callback = (result) => 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) => this._reviveJSONValue(key, value));
+ }
+
+ _jsonStringify(original)
+ {
+ return JSON.stringify(original, (key, value) => this._replaceJSONValue(key, value));
+ }
+
+ _reviveJSONValue(key, value)
+ {
+ if (value && typeof value === "object" && 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: "NodeNotFound", message: "Node with identifier '" + identifier + "' was not found"};
+ }
+
+ _identifierForNode(node)
+ {
+ let identifier = this._nodeToIdMap.get(node);
+ if (identifier)
+ return identifier;
+
+ identifier = "node-" + 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 -> WebAutomationSessionProxy {
</span><del>- // FIXME: Add messages here.
- Test()
</del><ins>+ EvaluateJavaScriptFunction(uint64_t frame, String function, Vector<String> arguments, bool expectsImplicitCallbackArgument, uint64_t callbackID)
</ins><span class="cx"> }
</span></span></pre>
</div>
</div>
</body>
</html>