ozone: evdev: Sync caps lock LED state to evdev
[chromium-blink-merge.git] / components / autofill / content / renderer / page_click_tracker.cc
blob6297bc01989b31e98d8592ef5cb055ebf4c88846
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 "components/autofill/content/renderer/form_autofill_util.h"
8 #include "components/autofill/content/renderer/page_click_listener.h"
9 #include "content/public/renderer/render_frame.h"
10 #include "content/public/renderer/render_view.h"
11 #include "third_party/WebKit/public/platform/WebPoint.h"
12 #include "third_party/WebKit/public/platform/WebSize.h"
13 #include "third_party/WebKit/public/web/WebDocument.h"
14 #include "third_party/WebKit/public/web/WebHitTestResult.h"
15 #include "third_party/WebKit/public/web/WebInputElement.h"
16 #include "third_party/WebKit/public/web/WebInputEvent.h"
17 #include "third_party/WebKit/public/web/WebLocalFrame.h"
18 #include "third_party/WebKit/public/web/WebTextAreaElement.h"
19 #include "third_party/WebKit/public/web/WebView.h"
21 using blink::WebElement;
22 using blink::WebGestureEvent;
23 using blink::WebInputElement;
24 using blink::WebInputEvent;
25 using blink::WebMouseEvent;
26 using blink::WebNode;
27 using blink::WebPoint;
28 using blink::WebSize;
29 using blink::WebTextAreaElement;
31 namespace {
33 // Casts |node| to a WebInputElement.
34 // Returns an empty (isNull()) WebInputElement if |node| is not a text field.
35 const WebInputElement GetTextWebInputElement(const WebNode& node) {
36 if (!node.isElementNode())
37 return WebInputElement();
38 const WebElement element = node.toConst<WebElement>();
39 if (!element.hasHTMLTagName("input"))
40 return WebInputElement();
41 const WebInputElement* input = blink::toWebInputElement(&element);
42 if (!autofill::IsTextInput(input))
43 return WebInputElement();
44 return *input;
47 // Casts |node| to a WebTextAreaElement.
48 // Returns an empty (isNull()) WebTextAreaElement if |node| is not a
49 // textarea field.
50 const WebTextAreaElement GetWebTextAreaElement(const WebNode& node) {
51 if (!node.isElementNode())
52 return WebTextAreaElement();
53 const WebElement element = node.toConst<WebElement>();
54 if (!element.hasHTMLTagName("textarea"))
55 return WebTextAreaElement();
56 return element.toConst<WebTextAreaElement>();
59 } // namespace
61 namespace autofill {
63 PageClickTracker::PageClickTracker(content::RenderFrame* render_frame,
64 PageClickListener* listener)
65 : content::RenderFrameObserver(render_frame),
66 focused_node_was_last_clicked_(false),
67 was_focused_before_now_(false),
68 listener_(listener),
69 legacy_(this) {
72 PageClickTracker::~PageClickTracker() {
75 void PageClickTracker::DidHandleMouseEvent(const WebMouseEvent& event) {
76 if (event.type != WebInputEvent::MouseDown ||
77 event.button != WebMouseEvent::ButtonLeft) {
78 return;
81 WebNode clicked_node = render_frame()->GetRenderView()->GetWebView()
82 ->hitTestResultAt(WebPoint(event.x, event.y)).node();
83 focused_node_was_last_clicked_ = !clicked_node.isNull() &&
84 clicked_node.focused();
87 void PageClickTracker::DidHandleGestureEvent(const WebGestureEvent& event) {
88 if (event.type != WebGestureEvent::GestureTap)
89 return;
91 WebNode tapped_node = render_frame()->GetRenderView()->GetWebView()
92 ->hitTestResultForTap(
93 WebPoint(event.x, event.y),
94 WebSize(event.data.tap.width, event.data.tap.height)).node();
95 focused_node_was_last_clicked_ = !tapped_node.isNull() &&
96 tapped_node.focused();
99 void PageClickTracker::FocusedNodeChanged(const WebNode& node) {
100 was_focused_before_now_ = false;
103 void PageClickTracker::FocusChangeComplete() {
104 WebNode focused_node = render_frame()->GetFocusedElement();
105 if (focused_node_was_last_clicked_ && !focused_node.isNull()) {
106 const WebInputElement input_element = GetTextWebInputElement(focused_node);
107 if (!input_element.isNull()) {
108 listener_->FormControlElementClicked(input_element,
109 was_focused_before_now_);
110 } else {
111 const WebTextAreaElement textarea_element =
112 GetWebTextAreaElement(focused_node);
113 if (!textarea_element.isNull()) {
114 listener_->FormControlElementClicked(textarea_element,
115 was_focused_before_now_);
120 was_focused_before_now_ = true;
121 focused_node_was_last_clicked_ = false;
124 // PageClickTracker::Legacy ----------------------------------------------------
126 PageClickTracker::Legacy::Legacy(PageClickTracker* tracker)
127 : content::RenderViewObserver(tracker->render_frame()->GetRenderView()),
128 tracker_(tracker) {
131 void PageClickTracker::Legacy::OnDestruct() {
132 // No-op. Don't delete |this|.
135 void PageClickTracker::Legacy::DidHandleMouseEvent(const WebMouseEvent& event) {
136 tracker_->DidHandleMouseEvent(event);
139 void PageClickTracker::Legacy::DidHandleGestureEvent(
140 const WebGestureEvent& event) {
141 tracker_->DidHandleGestureEvent(event);
144 void PageClickTracker::Legacy::FocusChangeComplete() {
145 tracker_->FocusChangeComplete();
148 } // namespace autofill