OLD | NEW |
---|---|
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "components/autofill/content/renderer/page_click_tracker.h" | 5 #include "components/autofill/content/renderer/page_click_tracker.h" |
6 | 6 |
7 #include "base/bind.h" | |
8 #include "base/message_loop/message_loop.h" | |
9 #include "components/autofill/content/renderer/form_autofill_util.h" | 7 #include "components/autofill/content/renderer/form_autofill_util.h" |
10 #include "components/autofill/content/renderer/page_click_listener.h" | 8 #include "components/autofill/content/renderer/page_click_listener.h" |
11 #include "content/public/renderer/render_view.h" | 9 #include "content/public/renderer/render_view.h" |
12 #include "third_party/WebKit/public/platform/WebString.h" | |
13 #include "third_party/WebKit/public/web/WebDOMMouseEvent.h" | |
14 #include "third_party/WebKit/public/web/WebDocument.h" | 10 #include "third_party/WebKit/public/web/WebDocument.h" |
15 #include "third_party/WebKit/public/web/WebInputElement.h" | 11 #include "third_party/WebKit/public/web/WebInputElement.h" |
16 #include "third_party/WebKit/public/web/WebInputEvent.h" | 12 #include "third_party/WebKit/public/web/WebInputEvent.h" |
17 #include "third_party/WebKit/public/web/WebLocalFrame.h" | 13 #include "third_party/WebKit/public/web/WebLocalFrame.h" |
18 #include "third_party/WebKit/public/web/WebTextAreaElement.h" | 14 #include "third_party/WebKit/public/web/WebTextAreaElement.h" |
19 #include "third_party/WebKit/public/web/WebView.h" | 15 #include "third_party/WebKit/public/web/WebView.h" |
20 | 16 |
21 using blink::WebDOMEvent; | |
22 using blink::WebDOMMouseEvent; | |
23 using blink::WebElement; | 17 using blink::WebElement; |
24 using blink::WebFormControlElement; | |
25 using blink::WebFrame; | |
26 using blink::WebGestureEvent; | 18 using blink::WebGestureEvent; |
27 using blink::WebInputElement; | 19 using blink::WebInputElement; |
28 using blink::WebInputEvent; | 20 using blink::WebInputEvent; |
29 using blink::WebMouseEvent; | 21 using blink::WebMouseEvent; |
30 using blink::WebNode; | 22 using blink::WebNode; |
31 using blink::WebString; | |
32 using blink::WebTextAreaElement; | 23 using blink::WebTextAreaElement; |
33 using blink::WebView; | |
34 | 24 |
35 namespace { | 25 namespace { |
36 | 26 |
37 // Casts |node| to a WebInputElement. | 27 // Casts |node| to a WebInputElement. |
38 // Returns an empty (isNull()) WebInputElement if |node| is not a text field. | 28 // Returns an empty (isNull()) WebInputElement if |node| is not a text field. |
39 const WebInputElement GetTextWebInputElement(const WebNode& node) { | 29 const WebInputElement GetTextWebInputElement(const WebNode& node) { |
40 if (!node.isElementNode()) | 30 if (!node.isElementNode()) |
41 return WebInputElement(); | 31 return WebInputElement(); |
42 const WebElement element = node.toConst<WebElement>(); | 32 const WebElement element = node.toConst<WebElement>(); |
43 if (!element.hasHTMLTagName("input")) | 33 if (!element.hasHTMLTagName("input")) |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
81 | 71 |
82 void PageClickTracker::DidHandleMouseEvent(const WebMouseEvent& event) { | 72 void PageClickTracker::DidHandleMouseEvent(const WebMouseEvent& event) { |
83 if (event.type != WebInputEvent::MouseDown || | 73 if (event.type != WebInputEvent::MouseDown || |
84 event.button != WebMouseEvent::ButtonLeft) { | 74 event.button != WebMouseEvent::ButtonLeft) { |
85 return; | 75 return; |
86 } | 76 } |
87 | 77 |
88 PotentialActivationAt(event.x, event.y); | 78 PotentialActivationAt(event.x, event.y); |
89 } | 79 } |
90 | 80 |
91 void PageClickTracker::DidHandleGestureEvent( | 81 void PageClickTracker::DidHandleGestureEvent(const WebGestureEvent& event) { |
92 const blink::WebGestureEvent& event) { | 82 if (event.type != WebGestureEvent::GestureTap) |
93 if (event.type != blink::WebGestureEvent::GestureTap) | |
94 return; | 83 return; |
95 | 84 |
96 PotentialActivationAt(event.x, event.y); | 85 PotentialActivationAt(event.x, event.y); |
97 } | 86 } |
98 | 87 |
99 void PageClickTracker::FocusedNodeChanged(const blink::WebNode& node) { | 88 void PageClickTracker::FocusedNodeChanged(const WebNode& node) { |
100 was_focused_before_now_ = false; | 89 was_focused_before_now_ = false; |
101 // If the focus change was a result of handling a click or tap, we'll soon get | 90 } |
102 // an associated event. Reset |was_focused_before_now_| to true only after the | 91 |
103 // message loop unwinds. | 92 void PageClickTracker::FocusChangeComplete() { |
104 base::MessageLoop::current()->PostTask( | 93 if (!clicked_node_.isNull()) { |
105 FROM_HERE, | 94 const WebInputElement input_element = GetTextWebInputElement(clicked_node_); |
106 base::Bind(&PageClickTracker::SetWasFocused, | 95 if (!input_element.isNull()) { |
107 weak_ptr_factory_.GetWeakPtr())); | 96 listener_->FormControlElementClicked(input_element, |
97 was_focused_before_now_); | |
98 } else { | |
99 const WebTextAreaElement textarea_element = | |
100 GetWebTextAreaElement(clicked_node_); | |
101 if (!textarea_element.isNull()) { | |
102 listener_->FormControlElementClicked(textarea_element, | |
103 was_focused_before_now_); | |
104 } | |
105 } | |
106 } | |
107 | |
108 clicked_node_.reset(); | |
109 was_focused_before_now_ = true; | |
108 } | 110 } |
109 | 111 |
110 void PageClickTracker::PotentialActivationAt(int x, int y) { | 112 void PageClickTracker::PotentialActivationAt(int x, int y) { |
111 blink::WebNode focused_node = render_view()->GetFocusedElement(); | 113 WebElement focused_element = render_view()->GetFocusedElement(); |
112 if (focused_node.isNull()) | 114 if (focused_element.isNull()) |
113 return; | 115 return; |
114 | 116 |
115 if (!render_view()->NodeContainsPoint(focused_node, gfx::Point(x, y))) | 117 if (!GetScaledBoundingBox(render_view()->GetWebView()->pageScaleFactor(), |
116 return; | 118 &focused_element).Contains(x, y)) { |
Garrett Casto
2015/01/06 20:01:09
Was the old code incorrect when the page was zoome
please use gerrit instead
2015/01/13 02:14:55
Added a test. The old code was incorrect when the
| |
117 | |
118 const WebInputElement input_element = GetTextWebInputElement(focused_node); | |
119 if (!input_element.isNull()) { | |
120 listener_->FormControlElementClicked(input_element, | |
121 was_focused_before_now_); | |
122 return; | 119 return; |
123 } | 120 } |
124 | 121 |
125 const WebTextAreaElement textarea_element = | 122 clicked_node_ = focused_element; |
126 GetWebTextAreaElement(focused_node); | |
127 if (!textarea_element.isNull()) { | |
128 listener_->FormControlElementClicked(textarea_element, | |
129 was_focused_before_now_); | |
130 } | |
131 } | |
132 | |
133 void PageClickTracker::SetWasFocused() { | |
134 was_focused_before_now_ = true; | |
135 } | 123 } |
136 | 124 |
137 } // namespace autofill | 125 } // namespace autofill |
OLD | NEW |