Web Automation: Add commands to compute layout of an element
authortimothy@apple.com <timothy@apple.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Mon, 28 Mar 2016 15:57:37 +0000 (15:57 +0000)
committertimothy@apple.com <timothy@apple.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Mon, 28 Mar 2016 15:57:37 +0000 (15:57 +0000)
https://bugs.webkit.org/show_bug.cgi?id=155841
rdar://problem/25340075

Reviewed by Brian Burg.

Source/WebCore:

* dom/Element.h: Mark scrollIntoViewIfNeeded() method as exported so WK2 can use it.
* platform/ScrollView.h: Mark rootViewToContents(IntRect) method as exported so WK2 can use it.

Source/WebKit2:

* UIProcess/Automation/Automation.json: Added computeElementLayout.

* UIProcess/Automation/WebAutomationSession.cpp:
(WebKit::WebAutomationSession::computeElementLayout): Added.
(WebKit::WebAutomationSession::didComputeElementLayout): Added.
* UIProcess/Automation/WebAutomationSession.h:
* UIProcess/Automation/WebAutomationSession.messages.in:
(DidComputeElementLayout): Added.

* WebProcess/Automation/WebAutomationSessionProxy.cpp:
(WebKit::WebAutomationSessionProxy::computeElementLayout): Added.
* WebProcess/Automation/WebAutomationSessionProxy.h:
* WebProcess/Automation/WebAutomationSessionProxy.messages.in:
(ComputeElementLayout): Added.

git-svn-id: https://svn.webkit.org/repository/webkit/trunk@198739 268f45cc-cd09-0410-ab3c-d52691b4dbfc

Source/WebCore/ChangeLog
Source/WebCore/dom/Element.h
Source/WebCore/platform/ScrollView.h
Source/WebKit2/ChangeLog
Source/WebKit2/UIProcess/Automation/Automation.json
Source/WebKit2/UIProcess/Automation/WebAutomationSession.cpp
Source/WebKit2/UIProcess/Automation/WebAutomationSession.h
Source/WebKit2/UIProcess/Automation/WebAutomationSession.messages.in
Source/WebKit2/WebProcess/Automation/WebAutomationSessionProxy.cpp
Source/WebKit2/WebProcess/Automation/WebAutomationSessionProxy.h
Source/WebKit2/WebProcess/Automation/WebAutomationSessionProxy.messages.in

index 99da9df..efd51b5 100644 (file)
@@ -1,3 +1,15 @@
+2016-03-24  Timothy Hatcher  <timothy@apple.com>
+
+        Web Automation: Add commands to compute layout of an element
+
+        https://bugs.webkit.org/show_bug.cgi?id=155841
+        rdar://problem/25340075
+
+        Reviewed by Brian Burg.
+
+        * dom/Element.h: Mark scrollIntoViewIfNeeded() method as exported so WK2 can use it.
+        * platform/ScrollView.h: Mark rootViewToContents(IntRect) method as exported so WK2 can use it.
+
 2016-03-18  Timothy Hatcher  <timothy@apple.com>
 
         Web Automation: Add Automation protocol commands to resolve frames as handles
index 75f246d..e7ddf82 100644 (file)
@@ -124,7 +124,7 @@ public:
     unsigned findAttributeIndexByName(const AtomicString& name, bool shouldIgnoreAttributeCase) const { return elementData()->findAttributeIndexByName(name, shouldIgnoreAttributeCase); }
 
     void scrollIntoView(bool alignToTop = true);
-    void scrollIntoViewIfNeeded(bool centerIfNeeded = true);
+    WEBCORE_EXPORT void scrollIntoViewIfNeeded(bool centerIfNeeded = true);
     WEBCORE_EXPORT void scrollIntoViewIfNotVisible(bool centerIfNotVisible = true);
 
     void scrollByLines(int lines);
@@ -157,7 +157,7 @@ public:
 
     Ref<ClientRectList> getClientRects();
     Ref<ClientRect> getBoundingClientRect();
-    
+
     // Returns the absolute bounding box translated into client coordinates.
     WEBCORE_EXPORT IntRect clientRect() const;
     // Returns the absolute bounding box translated into screen coordinates.
index 9802aa3..d07ffce 100644 (file)
@@ -286,7 +286,7 @@ public:
 
     WEBCORE_EXPORT IntPoint rootViewToContents(const IntPoint&) const;
     WEBCORE_EXPORT IntPoint contentsToRootView(const IntPoint&) const;
-    IntRect rootViewToContents(const IntRect&) const;
+    WEBCORE_EXPORT IntRect rootViewToContents(const IntRect&) const;
     WEBCORE_EXPORT IntRect contentsToRootView(const IntRect&) const;
 
     IntPoint viewToContents(const IntPoint&) const;
index 9a5edc8..632a378 100644 (file)
@@ -1,3 +1,27 @@
+2016-03-24  Timothy Hatcher  <timothy@apple.com>
+
+        Web Automation: Add commands to compute layout of an element
+
+        https://bugs.webkit.org/show_bug.cgi?id=155841
+        rdar://problem/25340075
+
+        Reviewed by Brian Burg.
+
+        * UIProcess/Automation/Automation.json: Added computeElementLayout.
+
+        * UIProcess/Automation/WebAutomationSession.cpp:
+        (WebKit::WebAutomationSession::computeElementLayout): Added.
+        (WebKit::WebAutomationSession::didComputeElementLayout): Added.
+        * UIProcess/Automation/WebAutomationSession.h:
+        * UIProcess/Automation/WebAutomationSession.messages.in:
+        (DidComputeElementLayout): Added.
+
+        * WebProcess/Automation/WebAutomationSessionProxy.cpp:
+        (WebKit::WebAutomationSessionProxy::computeElementLayout): Added.
+        * WebProcess/Automation/WebAutomationSessionProxy.h:
+        * WebProcess/Automation/WebAutomationSessionProxy.messages.in:
+        (ComputeElementLayout): Added.
+
 2016-03-18  Timothy Hatcher  <timothy@apple.com>
 
         Web Automation: Add Automation protocol commands to resolve frames as handles
index 1fa1f4e..54491de 100644 (file)
@@ -3,6 +3,30 @@
     "description": "Automation domain exposes commands for automating user interactions with the browser.",
     "types": [
         {
+            "id": "Point",
+            "type": "object",
+            "properties": [
+                { "name": "x", "type": "number" },
+                { "name": "y", "type": "number" }
+            ]
+        },
+        {
+            "id": "Size",
+            "type": "object",
+            "properties": [
+                { "name": "width", "type": "number" },
+                { "name": "height", "type": "number" }
+            ]
+        },
+        {
+            "id": "Rect",
+            "type": "object",
+            "properties": [
+                { "name": "origin", "$ref": "Point" },
+                { "name": "size", "$ref": "Size" }
+            ]
+        },
+        {
             "id": "BrowsingContextHandle",
             "type": "string",
             "description": "An opaque identifier for a browsing context."
                 { "name": "result", "$ref": "FrameHandle", "description": "The <code>FrameHandle</code> for the requested frame." }
             ],
             "async": true
+        },
+        {
+            "name": "computeElementLayout",
+            "description": "Computes the layout for an element.",
+            "parameters": [
+                { "name": "browsingContextHandle", "$ref": "BrowsingContextHandle", "description": "The handle for the browsing context the frame is located." },
+                { "name": "frameHandle", "$ref": "FrameHandle", "description": "The handle for the frame that contains the element." },
+                { "name": "nodeHandle", "$ref": "NodeHandle", "description": "The handle of the element to use." },
+                { "name": "scrollIntoViewIfNeeded", "optional": true, "type": "boolean", "description": "If the element should be scrolled into view before computing its layout." },
+                { "name": "useViewportCoordinates", "optional": true, "type": "boolean", "description": "If the result coordinates should be represented as viewport coordinates or not. Defaults to false, which means coordinates should be represented as page coordinates." }
+            ],
+            "returns": [
+                { "name": "rect", "$ref": "Rect", "description": "The layout rect for the requested element. Specified in page or viewport coordinates based on the useViewportCoordinates parameter." }
+            ],
+            "async": true
         }
     ]
 }
index 6ddef05..aaa451b 100644 (file)
@@ -417,4 +417,52 @@ void WebAutomationSession::didResolveParentFrame(uint64_t callbackID, uint64_t f
         callback->sendSuccess(handleForWebFrameID(frameID));
 }
 
+void WebAutomationSession::computeElementLayout(Inspector::ErrorString& errorString, const String& browsingContextHandle, const String& frameHandle, const String& nodeHandle, const bool* optionalScrollIntoViewIfNeeded, const bool* optionalUseViewportCoordinates, Ref<ComputeElementLayoutCallback>&& callback)
+{
+    WebPageProxy* page = webPageProxyForHandle(browsingContextHandle);
+    if (!page)
+        FAIL_WITH_PREDEFINED_ERROR_MESSAGE(WindowNotFound);
+
+    WebFrameProxy* frame = webFrameProxyForHandle(frameHandle, *page);
+    if (!frame)
+        FAIL_WITH_PREDEFINED_ERROR_MESSAGE(FrameNotFound);
+
+    uint64_t callbackID = m_nextComputeElementLayoutCallbackID++;
+    m_computeElementLayoutCallbacks.set(callbackID, WTFMove(callback));
+
+    bool scrollIntoViewIfNeeded = optionalScrollIntoViewIfNeeded ? *optionalScrollIntoViewIfNeeded : false;
+    bool useViewportCoordinates = optionalUseViewportCoordinates ? *optionalUseViewportCoordinates : false;
+
+    page->process().send(Messages::WebAutomationSessionProxy::ComputeElementLayout(frame->frameID(), nodeHandle, scrollIntoViewIfNeeded, useViewportCoordinates, callbackID), 0);
+}
+
+void WebAutomationSession::didComputeElementLayout(uint64_t callbackID, WebCore::IntRect rect, const String& errorType)
+{
+    auto callback = m_computeElementLayoutCallbacks.take(callbackID);
+    if (!callback)
+        return;
+
+    if (!errorType.isEmpty()) {
+        callback->sendFailure(errorType);
+        return;
+    }
+
+    auto originObject = Inspector::Protocol::Automation::Point::create()
+        .setX(rect.x())
+        .setY(rect.y())
+        .release();
+
+    auto sizeObject = Inspector::Protocol::Automation::Size::create()
+        .setWidth(rect.width())
+        .setHeight(rect.height())
+        .release();
+
+    auto rectObject = Inspector::Protocol::Automation::Rect::create()
+        .setOrigin(WTFMove(originObject))
+        .setSize(WTFMove(sizeObject))
+        .release();
+
+    callback->sendSuccess(WTFMove(rectObject));
+}
+
 } // namespace WebKit
index 95b7705..9544aec 100644 (file)
@@ -44,6 +44,10 @@ class BackendDispatcher;
 class FrontendRouter;
 }
 
+namespace WebCore {
+class IntRect;
+}
+
 namespace WebKit {
 
 class WebAutomationSessionClient;
@@ -90,6 +94,7 @@ public:
     void evaluateJavaScriptFunction(Inspector::ErrorString&, const String& browsingContextHandle, const String* optionalFrameHandle, const String& function, const Inspector::InspectorArray& arguments, bool expectsImplicitCallbackArgument, Ref<Inspector::AutomationBackendDispatcherHandler::EvaluateJavaScriptFunctionCallback>&&) override;
     void resolveChildFrameHandle(Inspector::ErrorString&, const String& browsingContextHandle, const String* optionalFrameHandle, const int* optionalOrdinal, const String* optionalName, const String* optionalNodeHandle, Ref<ResolveChildFrameHandleCallback>&&) override;
     void resolveParentFrameHandle(Inspector::ErrorString&, const String& browsingContextHandle, const String& frameHandle, Ref<ResolveParentFrameHandleCallback>&&) override;
+    void computeElementLayout(Inspector::ErrorString&, const String& browsingContextHandle, const String& frameHandle, const String& nodeHandle, const bool* optionalScrollIntoViewIfNeeded, const bool* useViewportCoordinates, Ref<Inspector::AutomationBackendDispatcherHandler::ComputeElementLayoutCallback>&&) override;
 
 private:
     WebPageProxy* webPageProxyForHandle(const String&);
@@ -106,6 +111,7 @@ private:
     void didEvaluateJavaScriptFunction(uint64_t callbackID, const String& result, const String& errorType);
     void didResolveChildFrame(uint64_t callbackID, uint64_t frameID, const String& errorType);
     void didResolveParentFrame(uint64_t callbackID, uint64_t frameID, const String& errorType);
+    void didComputeElementLayout(uint64_t callbackID, WebCore::IntRect, const String& errorType);
 
     WebProcessPool* m_processPool { nullptr };
 
@@ -131,6 +137,9 @@ private:
     uint64_t m_nextResolveParentFrameCallbackID { 1 };
     HashMap<uint64_t, RefPtr<Inspector::AutomationBackendDispatcherHandler::ResolveParentFrameHandleCallback>> m_resolveParentFrameHandleCallbacks;
 
+    uint64_t m_nextComputeElementLayoutCallbackID { 1 };
+    HashMap<uint64_t, RefPtr<Inspector::AutomationBackendDispatcherHandler::ComputeElementLayoutCallback>> m_computeElementLayoutCallbacks;
+
 #if ENABLE(REMOTE_INSPECTOR)
     Inspector::FrontendChannel* m_remoteChannel { nullptr };
 #endif
index 852ff85..72d9a93 100644 (file)
@@ -25,4 +25,6 @@ messages -> WebAutomationSession {
 
     DidResolveChildFrame(uint64_t callbackID, uint64_t frameID, String errorType)
     DidResolveParentFrame(uint64_t callbackID, uint64_t frameID, String errorType)
+
+    DidComputeElementLayout(uint64_t callbackID, WebCore::IntRect rect, String errorType)
 }
index 8a7a2da..95edf53 100644 (file)
@@ -41,6 +41,7 @@
 #include <WebCore/DOMWindow.h>
 #include <WebCore/Frame.h>
 #include <WebCore/FrameTree.h>
+#include <WebCore/FrameView.h>
 #include <WebCore/HTMLFrameElementBase.h>
 #include <WebCore/JSElement.h>
 #include <WebCore/MainFrame.h>
@@ -407,4 +408,45 @@ void WebAutomationSessionProxy::focusFrame(uint64_t frameID)
     coreDOMWindow->focus(true);
 }
 
+void WebAutomationSessionProxy::computeElementLayout(uint64_t frameID, String nodeHandle, bool scrollIntoViewIfNeeded, bool useViewportCoordinates, uint64_t callbackID)
+{
+    String frameNotFoundErrorType = Inspector::Protocol::getEnumConstantValue(Inspector::Protocol::Automation::ErrorMessage::FrameNotFound);
+    String nodeNotFoundErrorType = Inspector::Protocol::getEnumConstantValue(Inspector::Protocol::Automation::ErrorMessage::NodeNotFound);
+
+    WebFrame* frame = WebProcess::singleton().webFrame(frameID);
+    if (!frame) {
+        WebProcess::singleton().parentProcessConnection()->send(Messages::WebAutomationSession::DidComputeElementLayout(callbackID, WebCore::IntRect(), frameNotFoundErrorType), 0);
+        return;
+    }
+
+    WebCore::Element* coreElement = elementForNodeHandle(*frame, nodeHandle);
+    if (!coreElement) {
+        WebProcess::singleton().parentProcessConnection()->send(Messages::WebAutomationSession::DidComputeElementLayout(callbackID, WebCore::IntRect(), nodeNotFoundErrorType), 0);
+        return;
+    }
+
+    if (scrollIntoViewIfNeeded)
+        coreElement->scrollIntoViewIfNeeded(false);
+
+    WebCore::IntRect rect = coreElement->clientRect();
+
+    if (!useViewportCoordinates) {
+        WebCore::Frame* coreFrame = frame->coreFrame();
+        if (!coreFrame) {
+            WebProcess::singleton().parentProcessConnection()->send(Messages::WebAutomationSession::DidComputeElementLayout(callbackID, WebCore::IntRect(), frameNotFoundErrorType), 0);
+            return;
+        }
+
+        WebCore::FrameView *coreFrameView = coreFrame->view();
+        if (!coreFrameView) {
+            WebProcess::singleton().parentProcessConnection()->send(Messages::WebAutomationSession::DidComputeElementLayout(callbackID, WebCore::IntRect(), frameNotFoundErrorType), 0);
+            return;
+        }
+
+        rect = coreFrameView->rootViewToContents(rect);
+    }
+
+    WebProcess::singleton().parentProcessConnection()->send(Messages::WebAutomationSession::DidComputeElementLayout(callbackID, rect, emptyString()), 0);
+}
+
 } // namespace WebKit
index 4142c91..36961f2 100644 (file)
@@ -63,6 +63,7 @@ private:
     void resolveChildFrameWithName(uint64_t frameID, const String& name, uint64_t callbackID);
     void resolveParentFrame(uint64_t frameID, uint64_t callbackID);
     void focusFrame(uint64_t frameID);
+    void computeElementLayout(uint64_t frameID, String nodeHandle, bool scrollIntoViewIfNeeded, bool useViewportCoordinates, uint64_t callbackID);
 
     String m_sessionIdentifier;
 
index 2a1642d..f7231e7 100644 (file)
@@ -29,4 +29,6 @@ messages -> WebAutomationSessionProxy {
     ResolveParentFrame(uint64_t frameID, uint64_t callbackID)
 
     FocusFrame(uint64_t frameID)
+
+    ComputeElementLayout(uint64_t frameID, String nodeHandle, bool scrollIntoViewIfNeeded, bool useViewportCoordinates, uint64_t callbackID)
 }