Remove RenderViewImpl::GetFocusedElement.

R=esprehn,enne
BUG=612570
CQ_INCLUDE_TRYBOTS=tryserver.blink:linux_blink_rel

Review-Url: https://codereview.chromium.org/1990553002
Cr-Commit-Position: refs/heads/master@{#395166}
diff --git a/cc/trees/layer_tree_host.cc b/cc/trees/layer_tree_host.cc
index 794809f..ff28504 100644
--- a/cc/trees/layer_tree_host.cc
+++ b/cc/trees/layer_tree_host.cc
@@ -839,6 +839,10 @@
   SetNeedsCommit();
 }
 
+bool LayerTreeHost::HasPendingPageScaleAnimation() const {
+  return !!pending_page_scale_animation_.get();
+}
+
 void LayerTreeHost::NotifyInputThrottledUntilCommit() {
   proxy_->NotifyInputThrottledUntilCommit();
 }
diff --git a/cc/trees/layer_tree_host.h b/cc/trees/layer_tree_host.h
index e544e0c..1b2c4ba 100644
--- a/cc/trees/layer_tree_host.h
+++ b/cc/trees/layer_tree_host.h
@@ -273,6 +273,7 @@
                                bool use_anchor,
                                float scale,
                                base::TimeDelta duration);
+  bool HasPendingPageScaleAnimation() const;
 
   void ApplyScrollAndScale(ScrollAndScaleSet* info);
   void SetImplTransform(const gfx::Transform& transform);
diff --git a/content/renderer/gpu/render_widget_compositor.cc b/content/renderer/gpu/render_widget_compositor.cc
index 5d5a155..6304815d 100644
--- a/content/renderer/gpu/render_widget_compositor.cc
+++ b/content/renderer/gpu/render_widget_compositor.cc
@@ -657,6 +657,10 @@
       duration);
 }
 
+bool RenderWidgetCompositor::hasPendingPageScaleAnimation() const {
+  return layer_tree_host_->HasPendingPageScaleAnimation();
+}
+
 void RenderWidgetCompositor::heuristicsForGpuRasterizationUpdated(
     bool matches_heuristics) {
   layer_tree_host_->SetHasGpuRasterizationTrigger(matches_heuristics);
diff --git a/content/renderer/gpu/render_widget_compositor.h b/content/renderer/gpu/render_widget_compositor.h
index 3b8637e..ef553c5c 100644
--- a/content/renderer/gpu/render_widget_compositor.h
+++ b/content/renderer/gpu/render_widget_compositor.h
@@ -116,6 +116,7 @@
                                bool use_anchor,
                                float new_page_scale,
                                double duration_sec) override;
+  bool hasPendingPageScaleAnimation() const override;
   void heuristicsForGpuRasterizationUpdated(bool matches_heuristics) override;
   void setNeedsAnimate() override;
   void setNeedsBeginFrame() override;
diff --git a/content/renderer/render_view_impl.cc b/content/renderer/render_view_impl.cc
index 71c52a3..145ca56f 100644
--- a/content/renderer/render_view_impl.cc
+++ b/content/renderer/render_view_impl.cc
@@ -1406,15 +1406,12 @@
     return;
   }
 
-  blink::WebElement element = GetFocusedElement();
-  bool will_animate = false;
-  if (!element.isNull() && element.isEditable()) {
-    rect_for_scrolled_focused_editable_node_ = rect;
-    has_scrolled_focused_editable_node_into_rect_ = true;
-    will_animate = webview()->scrollFocusedNodeIntoRect(rect);
-  }
+  if (!webview()->scrollFocusedEditableElementIntoRect(rect))
+    return;
 
-  if (!will_animate)
+  rect_for_scrolled_focused_editable_node_ = rect;
+  has_scrolled_focused_editable_node_into_rect_ = true;
+  if (!compositor()->hasPendingPageScaleAnimation())
     GetWidget()->FocusChangeComplete();
 }
 
@@ -2323,19 +2320,6 @@
   main_render_frame_->didStopLoading();
 }
 
-blink::WebElement RenderViewImpl::GetFocusedElement() const {
-  if (!webview())
-    return WebElement();
-  WebFrame* focused_frame = webview()->focusedFrame();
-  if (focused_frame) {
-    WebDocument doc = focused_frame->document();
-    if (!doc.isNull())
-      return doc.focusedElement();
-  }
-
-  return WebElement();
-}
-
 void RenderViewImpl::OnSetPageScale(float page_scale_factor) {
   if (!webview())
     return;
diff --git a/content/renderer/render_view_impl.h b/content/renderer/render_view_impl.h
index 3d51a93a..755d63ab 100644
--- a/content/renderer/render_view_impl.h
+++ b/content/renderer/render_view_impl.h
@@ -689,9 +689,6 @@
   // Check whether the preferred size has changed.
   void CheckPreferredSize();
 
-  // Gets the currently focused element, if any.
-  blink::WebElement GetFocusedElement() const;
-
 #if defined(OS_ANDROID)
   // Launch an Android content intent with the given URL.
   void LaunchAndroidContentIntent(const GURL& intent_url,
diff --git a/third_party/WebKit/Source/web/WebElement.cpp b/third_party/WebKit/Source/web/WebElement.cpp
index 4822f05d..4ba64394 100644
--- a/third_party/WebKit/Source/web/WebElement.cpp
+++ b/third_party/WebKit/Source/web/WebElement.cpp
@@ -56,6 +56,8 @@
     return constUnwrap<Element>()->isTextFormControl();
 }
 
+// TODO(dglazkov): Remove. Consumers of this code should use Node:hasEditableStyle.
+// http://crbug.com/612560
 bool WebElement::isEditable() const
 {
     const Element* element = constUnwrap<Element>();
diff --git a/third_party/WebKit/Source/web/WebViewImpl.cpp b/third_party/WebKit/Source/web/WebViewImpl.cpp
index 218f70f..b8cd9a1 100644
--- a/third_party/WebKit/Source/web/WebViewImpl.cpp
+++ b/third_party/WebKit/Source/web/WebViewImpl.cpp
@@ -1460,6 +1460,9 @@
         isAnimating = startPageScaleAnimation(scroll, false, scale, doubleTapZoomAnimationDurationInSeconds);
     }
 
+    // TODO(dglazkov): The only reason why we're using isAnimating and not just checking for
+    // m_layerTreeView->hasPendingPageScaleAnimation() is because of fake page scale animation plumbing
+    // for testing, which doesn't actually initiate a page scale animation.
     if (isAnimating) {
         m_doubleTapZoomPageScaleFactor = scale;
         m_doubleTapZoomPending = true;
@@ -2950,7 +2953,23 @@
         localFrame->selection().clear();
 }
 
-bool WebViewImpl::scrollFocusedNodeIntoRect(const WebRect& rectInViewport)
+// TODO(dglazkov): Remove and replace with Node:hasEditableStyle.
+// http://crbug.com/612560
+static bool isElementEditable(const Element* element)
+{
+    if (element->isContentEditable())
+        return true;
+
+    if (element->isTextFormControl()) {
+        const HTMLTextFormControlElement* input = toHTMLTextFormControlElement(element);
+        if (!input->isDisabledOrReadOnly())
+            return true;
+    }
+
+    return equalIgnoringCase(element->getAttribute(HTMLNames::roleAttr), "textbox");
+}
+
+bool WebViewImpl::scrollFocusedEditableElementIntoRect(const WebRect& rectInViewport)
 {
     LocalFrame* frame = page()->mainFrame() && page()->mainFrame()->isLocalFrame()
         ? page()->deprecatedLocalMainFrame() : nullptr;
@@ -2958,6 +2977,9 @@
     if (!frame || !frame->view() || !element)
         return false;
 
+    if (!isElementEditable(element))
+        return false;
+
     element->document().updateStyleAndLayoutIgnorePendingStylesheets();
 
     bool zoomInToLegibleScale = m_webSettings->autoZoomFocusedNodeToLegibleScale()
@@ -2977,9 +2999,9 @@
     bool needAnimation;
     computeScaleAndScrollForFocusedNode(element, zoomInToLegibleScale, scale, scroll, needAnimation);
     if (needAnimation)
-        return startPageScaleAnimation(scroll, false, scale, scrollAndScaleAnimationDurationInSeconds);
+        startPageScaleAnimation(scroll, false, scale, scrollAndScaleAnimationDurationInSeconds);
 
-    return false;
+    return true;
 }
 
 void WebViewImpl::smoothScroll(int targetX, int targetY, long durationMs)
diff --git a/third_party/WebKit/Source/web/WebViewImpl.h b/third_party/WebKit/Source/web/WebViewImpl.h
index 3015ed1..4029fee 100644
--- a/third_party/WebKit/Source/web/WebViewImpl.h
+++ b/third_party/WebKit/Source/web/WebViewImpl.h
@@ -188,7 +188,7 @@
     void focusDocumentView(WebFrame*) override;
     void setInitialFocus(bool reverse) override;
     void clearFocusedElement() override;
-    bool scrollFocusedNodeIntoRect(const WebRect&) override;
+    bool scrollFocusedEditableElementIntoRect(const WebRect&) override;
     void smoothScroll(int targetX, int targetY, long durationMs) override;
     void zoomToFindInPageRect(const WebRect&);
     void advanceFocus(bool reverse) override;
diff --git a/third_party/WebKit/Source/web/tests/VisualViewportTest.cpp b/third_party/WebKit/Source/web/tests/VisualViewportTest.cpp
index 4d6a5ba..edb2903f 100644
--- a/third_party/WebKit/Source/web/tests/VisualViewportTest.cpp
+++ b/third_party/WebKit/Source/web/tests/VisualViewportTest.cpp
@@ -948,8 +948,8 @@
     EXPECT_EQ("t ", mainFrame->selectionAsText().utf8());
 }
 
-// Test that the scrollFocusedNodeIntoRect method works with the visual viewport.
-TEST_P(ParameterizedVisualViewportTest, DISABLED_TestScrollFocusedNodeIntoRect)
+// Test that the scrollFocusedEditableElementIntoRect method works with the visual viewport.
+TEST_P(ParameterizedVisualViewportTest, DISABLED_TestScrollFocusedEditableElementIntoRect)
 {
     initializeWithDesktopSettings();
     webViewImpl()->resize(IntSize(500, 300));
@@ -961,7 +961,7 @@
     webViewImpl()->resizeVisualViewport(IntSize(200, 100));
     webViewImpl()->setInitialFocus(false);
     visualViewport.setLocation(FloatPoint());
-    webViewImpl()->scrollFocusedNodeIntoRect(IntRect(0, 0, 500, 200));
+    webViewImpl()->scrollFocusedEditableElementIntoRect(IntRect(0, 0, 500, 200));
 
     EXPECT_POINT_EQ(IntPoint(0, frame()->view()->maximumScrollPosition().y()),
         frame()->view()->scrollPosition());
@@ -973,7 +973,7 @@
     visualViewport.setLocation(FloatPoint(0, 0));
 
     webViewImpl()->setPageScaleFactor(2);
-    webViewImpl()->scrollFocusedNodeIntoRect(IntRect(0, 0, 500, 200));
+    webViewImpl()->scrollFocusedEditableElementIntoRect(IntRect(0, 0, 500, 200));
     EXPECT_POINT_EQ(IntPoint(0, frame()->view()->maximumScrollPosition().y()),
         frame()->view()->scrollPosition());
     EXPECT_FLOAT_POINT_EQ(FloatPoint(125, 150), visualViewport.visibleRect().location());
@@ -988,7 +988,7 @@
     visualViewport.setLocation(FloatPoint(30, 50));
 
     webViewImpl()->setPageScaleFactor(2);
-    webViewImpl()->scrollFocusedNodeIntoRect(IntRect(0, 0, 500, 200));
+    webViewImpl()->scrollFocusedEditableElementIntoRect(IntRect(0, 0, 500, 200));
     EXPECT_POINT_EQ(IntPoint(200-30-75, 600-50-65), frame()->view()->scrollPosition());
     EXPECT_FLOAT_POINT_EQ(FloatPoint(30, 50), visualViewport.visibleRect().location());
 }
diff --git a/third_party/WebKit/Source/web/tests/WebFrameTest.cpp b/third_party/WebKit/Source/web/tests/WebFrameTest.cpp
index 11ef88d3..b792dd8 100644
--- a/third_party/WebKit/Source/web/tests/WebFrameTest.cpp
+++ b/third_party/WebKit/Source/web/tests/WebFrameTest.cpp
@@ -2961,7 +2961,7 @@
     // Focus the first textbox that's in a touch-action: pan-x ancestor, this
     // shouldn't cause an autozoom since pan-x disables pinch-zoom.
     webViewHelper.webViewImpl()->advanceFocus(false);
-    webViewHelper.webViewImpl()->scrollFocusedNodeIntoRect(WebRect());
+    webViewHelper.webViewImpl()->scrollFocusedEditableElementIntoRect(WebRect());
     EXPECT_EQ(webViewHelper.webViewImpl()->fakePageScaleAnimationPageScaleForTesting(), 0);
 
     setScaleAndScrollAndLayout(webViewHelper.webViewImpl(), WebPoint(0, 0), initialScale);
@@ -2970,7 +2970,7 @@
     // Focus the second textbox that's in a touch-action: manipulation ancestor,
     // this should cause an autozoom since it allows pinch-zoom.
     webViewHelper.webViewImpl()->advanceFocus(false);
-    webViewHelper.webViewImpl()->scrollFocusedNodeIntoRect(WebRect());
+    webViewHelper.webViewImpl()->scrollFocusedEditableElementIntoRect(WebRect());
     EXPECT_GT(webViewHelper.webViewImpl()->fakePageScaleAnimationPageScaleForTesting(), initialScale);
 
     setScaleAndScrollAndLayout(webViewHelper.webViewImpl(), WebPoint(0, 0), initialScale);
@@ -2980,7 +2980,7 @@
     // should cause an autozoom since it's seperated from the node with the
     // touch-action by an overflow:scroll element.
     webViewHelper.webView()->advanceFocus(false);
-    webViewHelper.webViewImpl()->scrollFocusedNodeIntoRect(WebRect());
+    webViewHelper.webViewImpl()->scrollFocusedEditableElementIntoRect(WebRect());
     EXPECT_GT(webViewHelper.webViewImpl()->fakePageScaleAnimationPageScaleForTesting(), initialScale);
 }
 
diff --git a/third_party/WebKit/public/platform/WebLayerTreeView.h b/third_party/WebKit/public/platform/WebLayerTreeView.h
index c902c88..a66bb7b 100644
--- a/third_party/WebKit/public/platform/WebLayerTreeView.h
+++ b/third_party/WebKit/public/platform/WebLayerTreeView.h
@@ -89,6 +89,9 @@
     // If useAnchor is false, destination is the final top-left scroll position.
     virtual void startPageScaleAnimation(const WebPoint& destination, bool useAnchor, float newPageScale, double durationSec) { }
 
+    // Returns true if the page scale animation had started.
+    virtual bool hasPendingPageScaleAnimation() const { return false; }
+
     virtual void heuristicsForGpuRasterizationUpdated(bool) { }
 
     // Sets the amount that the top controls are showing, from 0 (hidden) to 1
diff --git a/third_party/WebKit/public/web/WebView.h b/third_party/WebKit/public/web/WebView.h
index d25040c..c8a6244 100644
--- a/third_party/WebKit/public/web/WebView.h
+++ b/third_party/WebKit/public/web/WebView.h
@@ -210,9 +210,10 @@
     // send it.
     virtual void clearFocusedElement() = 0;
 
-    // Scrolls the node currently in focus into |rect|, where |rect| is in
-    // viewport space. Returns true if an animation was started.
-    virtual bool scrollFocusedNodeIntoRect(const WebRect&) { return false; }
+    // If it is editable, scrolls the element currently in focus into |rect|,
+    // where |rect| is in viewport space.
+    // Returns false if there is currently no currently focused element.
+    virtual bool scrollFocusedEditableElementIntoRect(const WebRect&) { return false; }
 
     // Smooth scroll the root layer to |targetX|, |targetY| in |durationMs|.
     virtual void smoothScroll(int targetX, int targetY, long durationMs) { }