Revert "Fix broken channel icon in chrome://help on CrOS" and try again
[chromium-blink-merge.git] / components / autofill / content / renderer / page_click_tracker.cc
blob56fbb5345065030cd9750dee58128d52958f27e3
1 // Copyright 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "components/autofill/content/renderer/page_click_tracker.h"
7 #include "base/command_line.h"
8 #include "components/autofill/content/renderer/form_autofill_util.h"
9 #include "components/autofill/content/renderer/page_click_listener.h"
10 #include "components/autofill/core/common/autofill_util.h"
11 #include "content/public/renderer/render_frame.h"
12 #include "content/public/renderer/render_view.h"
13 #include "third_party/WebKit/public/platform/WebPoint.h"
14 #include "third_party/WebKit/public/platform/WebSize.h"
15 #include "third_party/WebKit/public/web/WebDocument.h"
16 #include "third_party/WebKit/public/web/WebHitTestResult.h"
17 #include "third_party/WebKit/public/web/WebInputElement.h"
18 #include "third_party/WebKit/public/web/WebInputEvent.h"
19 #include "third_party/WebKit/public/web/WebLocalFrame.h"
20 #include "third_party/WebKit/public/web/WebTextAreaElement.h"
21 #include "third_party/WebKit/public/web/WebView.h"
23 using blink::WebElement;
24 using blink::WebGestureEvent;
25 using blink::WebInputElement;
26 using blink::WebInputEvent;
27 using blink::WebMouseEvent;
28 using blink::WebNode;
29 using blink::WebPoint;
30 using blink::WebSize;
31 using blink::WebTextAreaElement;
33 namespace {
35 // Casts |node| to a WebInputElement.
36 // Returns an empty (isNull()) WebInputElement if |node| is not a text field.
37 const WebInputElement GetTextWebInputElement(const WebNode& node) {
38 if (!node.isElementNode())
39 return WebInputElement();
40 const WebElement element = node.toConst<WebElement>();
41 if (!element.hasHTMLTagName("input"))
42 return WebInputElement();
43 const WebInputElement* input = blink::toWebInputElement(&element);
44 if (!autofill::IsTextInput(input))
45 return WebInputElement();
46 return *input;
49 // Casts |node| to a WebTextAreaElement.
50 // Returns an empty (isNull()) WebTextAreaElement if |node| is not a
51 // textarea field.
52 const WebTextAreaElement GetWebTextAreaElement(const WebNode& node) {
53 if (!node.isElementNode())
54 return WebTextAreaElement();
55 const WebElement element = node.toConst<WebElement>();
56 if (!element.hasHTMLTagName("textarea"))
57 return WebTextAreaElement();
58 return element.toConst<WebTextAreaElement>();
61 } // namespace
63 namespace autofill {
65 PageClickTracker::PageClickTracker(content::RenderFrame* render_frame,
66 PageClickListener* listener)
67 : content::RenderFrameObserver(render_frame),
68 focused_node_was_last_clicked_(false),
69 was_focused_before_now_(false),
70 listener_(listener),
71 legacy_(this) {
74 PageClickTracker::~PageClickTracker() {
77 void PageClickTracker::OnMouseDown(const WebNode& mouse_down_node) {
78 focused_node_was_last_clicked_ = !mouse_down_node.isNull() &&
79 mouse_down_node.focused();
82 void PageClickTracker::FocusedNodeChanged(const WebNode& node) {
83 was_focused_before_now_ = false;
85 if (IsKeyboardAccessoryEnabled()) {
86 focused_node_was_last_clicked_ = true;
87 DoFocusChangeComplete();
91 void PageClickTracker::FocusChangeComplete() {
92 if (IsKeyboardAccessoryEnabled())
93 return;
95 DoFocusChangeComplete();
98 void PageClickTracker::DoFocusChangeComplete() {
99 WebNode focused_node = render_frame()->GetFocusedElement();
100 if (focused_node_was_last_clicked_ && !focused_node.isNull()) {
101 const WebInputElement input_element = GetTextWebInputElement(focused_node);
102 if (!input_element.isNull()) {
103 listener_->FormControlElementClicked(input_element,
104 was_focused_before_now_);
105 } else {
106 const WebTextAreaElement textarea_element =
107 GetWebTextAreaElement(focused_node);
108 if (!textarea_element.isNull()) {
109 listener_->FormControlElementClicked(textarea_element,
110 was_focused_before_now_);
115 was_focused_before_now_ = true;
116 focused_node_was_last_clicked_ = false;
119 // PageClickTracker::Legacy ----------------------------------------------------
121 PageClickTracker::Legacy::Legacy(PageClickTracker* tracker)
122 : content::RenderViewObserver(tracker->render_frame()->GetRenderView()),
123 tracker_(tracker) {
126 void PageClickTracker::Legacy::OnDestruct() {
127 // No-op. Don't delete |this|.
130 void PageClickTracker::Legacy::OnMouseDown(const WebNode& mouse_down_node) {
131 tracker_->OnMouseDown(mouse_down_node);
134 void PageClickTracker::Legacy::FocusChangeComplete() {
135 tracker_->FocusChangeComplete();
138 } // namespace autofill