Diff
Modified: trunk/Source/WebKit2/ChangeLog (198736 => 198737)
--- 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)
@@ -1,3 +1,42 @@
+2016-03-17 Timothy Hatcher <timo...@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.
+
2016-03-14 Timothy Hatcher <timo...@apple.com>
Add WebAutomationSessionProxy for WebProcess side automation tasks
Modified: trunk/Source/WebKit2/UIProcess/Automation/Automation.json (198736 => 198737)
--- 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)
@@ -13,7 +13,9 @@
"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.",
"enum": [
"InternalError",
+ "_javascript_Error",
"WindowNotFound",
+ "NodeNotFound",
"NotImplemented"
]
},
@@ -95,6 +97,20 @@
"parameters": [
{ "name": "handle", "$ref": "BrowsingContextHandle", "description": "The handle for the browsing context that should be reloaded." }
]
+ },
+ {
+ "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
}
]
}
Modified: trunk/Source/WebKit2/UIProcess/Automation/WebAutomationSession.cpp (198736 => 198737)
--- 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)
@@ -255,4 +255,39 @@
page->reload(reloadFromOrigin, contentBlockersEnabled);
}
+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);
+}
+
} // namespace WebKit
Modified: trunk/Source/WebKit2/UIProcess/Automation/WebAutomationSession.h (198736 => 198737)
--- 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)
@@ -89,6 +89,7 @@
void goBackInBrowsingContext(Inspector::ErrorString&, const String&) override;
void goForwardInBrowsingContext(Inspector::ErrorString&, const String&) override;
void reloadBrowsingContext(Inspector::ErrorString&, const String&) override;
+ void evaluateJavaScriptFunction(Inspector::ErrorString&, const String& handle, const String& function, const Inspector::InspectorArray& arguments, bool expectsImplicitCallbackArgument, Ref<Inspector::AutomationBackendDispatcherHandler::EvaluateJavaScriptFunctionCallback>&&) override;
private:
WebKit::WebPageProxy* webPageProxyForHandle(const String&);
@@ -98,8 +99,7 @@
void didReceiveMessage(IPC::Connection&, IPC::MessageDecoder&) override;
// Called by WebAutomationSession messages
- // FIXME: Add message functions here.
- void test() { };
+ void didEvaluateJavaScriptFunction(uint64_t callbackID, const String& result, const String& errorType);
WebKit::WebProcessPool* m_processPool { nullptr };
std::unique_ptr<API::AutomationSessionClient> m_client;
@@ -112,6 +112,9 @@
HandleWebPageMap m_handleWebPageMap;
String m_activeBrowsingContextHandle;
+ uint64_t m_nextEvaluateJavaScriptCallbackID { 1 };
+ HashMap<uint64_t, RefPtr<Inspector::AutomationBackendDispatcherHandler::EvaluateJavaScriptFunctionCallback>> m_evaluateJavaScriptFunctionCallbacks;
+
#if ENABLE(REMOTE_INSPECTOR)
Inspector::FrontendChannel* m_remoteChannel { nullptr };
#endif
Modified: trunk/Source/WebKit2/UIProcess/Automation/WebAutomationSession.messages.in (198736 => 198737)
--- 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)
@@ -21,6 +21,5 @@
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
messages -> WebAutomationSession {
- // FIXME: Add messages here.
- Test()
+ DidEvaluateJavaScriptFunction(uint64_t callbackID, String result, String errorType)
}
Modified: trunk/Source/WebKit2/WebProcess/Automation/WebAutomationSessionProxy.cpp (198736 => 198737)
--- 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)
@@ -26,6 +26,7 @@
#include "config.h"
#include "WebAutomationSessionProxy.h"
+#include "InspectorProtocolObjects.h"
#include "WebAutomationSessionMessages.h"
#include "WebAutomationSessionProxyMessages.h"
#include "WebAutomationSessionProxyScriptSource.h"
@@ -39,6 +40,31 @@
namespace WebKit {
+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;
+}
+
static inline JSRetainPtr<JSStringRef> toJSString(const String& string)
{
return JSRetainPtr<JSStringRef>(Adopt, OpaqueJSString::create(string).leakRef());
@@ -49,6 +75,17 @@
return JSValueMakeString(context, toJSString(string).get());
}
+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);
+}
+
WebAutomationSessionProxy::WebAutomationSessionProxy(const String& sessionIdentifier)
: m_sessionIdentifier(sessionIdentifier)
{
@@ -77,6 +114,26 @@
return toJSValue(context, WebCore::createCanonicalUUIDString().convertToASCIIUppercase());
}
+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);
+}
+
JSObjectRef WebAutomationSessionProxy::scriptObjectForFrame(WebFrame& frame)
{
if (JSObjectRef scriptObject = m_webFrameScriptObjectMap.get(frame.frameID()))
@@ -106,8 +163,80 @@
void WebAutomationSessionProxy::didClearWindowObjectForFrame(WebFrame& frame)
{
- if (JSObjectRef scriptObject = m_webFrameScriptObjectMap.take(frame.frameID()))
+ uint64_t frameID = frame.frameID();
+ if (JSObjectRef scriptObject = m_webFrameScriptObjectMap.take(frameID))
JSValueUnprotect(frame.jsContext(), scriptObject);
+
+ String errorMessage = ASCIILiteral("Callback was not called before the unload event.");
+ String errorType = Inspector::Protocol::getEnumConstantValue(Inspector::Protocol::Automation::ErrorMessage::_javascript_Error);
+
+ auto pendingFrameCallbacks = m_webFramePendingEvaluateJavaScriptCallbacksMap.take(frameID);
+ for (uint64_t callbackID : pendingFrameCallbacks)
+ WebProcess::singleton().parentProcessConnection()->send(Messages::WebAutomationSession::DidEvaluateJavaScriptFunction(callbackID, emptyString(), errorType), 0);
}
+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::_javascript_Error);
+
+ 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);
+}
+
} // namespace WebKit
Modified: trunk/Source/WebKit2/WebProcess/Automation/WebAutomationSessionProxy.h (198736 => 198737)
--- 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)
@@ -43,6 +43,8 @@
void didClearWindowObjectForFrame(WebFrame&);
+ void didEvaluateJavaScriptFunction(uint64_t frameID, uint64_t callbackID, const String& result, const String& errorType);
+
private:
JSObjectRef scriptObjectForFrame(WebFrame&);
@@ -50,13 +52,12 @@
void didReceiveMessage(IPC::Connection&, IPC::MessageDecoder&);
// Called by WebAutomationSessionProxy messages
- // FIXME: Add message functions here.
- void test() { };
+ void evaluateJavaScriptFunction(uint64_t frameID, const String& function, Vector<String> arguments, bool expectsImplicitCallbackArgument, uint64_t callbackID);
String m_sessionIdentifier;
- typedef HashMap<uint64_t, JSObjectRef> WebFrameScriptObjectMap;
- WebFrameScriptObjectMap m_webFrameScriptObjectMap;
+ HashMap<uint64_t, JSObjectRef> m_webFrameScriptObjectMap;
+ HashMap<uint64_t, Vector<uint64_t>> m_webFramePendingEvaluateJavaScriptCallbacksMap;
};
} // namespace WebKit
Modified: trunk/Source/WebKit2/WebProcess/Automation/WebAutomationSessionProxy.js (198736 => 198737)
--- 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)
@@ -27,14 +27,91 @@
(function (sessionIdentifier, evaluate, createUUID) {
-// Protect against Object overwritten by the page.
-let Object = {}.constructor;
+const sessionNodePropertyName = "session-node-" + sessionIdentifier;
let AutomationSessionProxy = class AutomationSessionProxy
{
+ constructor()
+ {
+ this._nodeToIdMap = new Map;
+ this._idToNodeMap = new Map;
+ }
+
// Public
- // FIXME: Add functions here.
+ 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;
+ }
};
return new AutomationSessionProxy;
Modified: trunk/Source/WebKit2/WebProcess/Automation/WebAutomationSessionProxy.messages.in (198736 => 198737)
--- 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)
@@ -21,6 +21,5 @@
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
messages -> WebAutomationSessionProxy {
- // FIXME: Add messages here.
- Test()
+ EvaluateJavaScriptFunction(uint64_t frame, String function, Vector<String> arguments, bool expectsImplicitCallbackArgument, uint64_t callbackID)
}