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 "components/autofill/content/renderer/form_autofill_util.h" | 7 #include "components/autofill/content/renderer/form_autofill_util.h" |
8 #include "components/autofill/content/renderer/page_click_listener.h" | 8 #include "components/autofill/content/renderer/page_click_listener.h" |
9 #include "content/public/renderer/render_view.h" | 9 #include "content/public/renderer/render_view.h" |
10 #include "third_party/WebKit/public/platform/WebString.h" | 10 #include "third_party/WebKit/public/platform/WebString.h" |
11 #include "third_party/WebKit/public/web/WebDOMMouseEvent.h" | 11 #include "third_party/WebKit/public/web/WebDOMMouseEvent.h" |
12 #include "third_party/WebKit/public/web/WebDocument.h" | 12 #include "third_party/WebKit/public/web/WebDocument.h" |
13 #include "third_party/WebKit/public/web/WebFrame.h" | 13 #include "third_party/WebKit/public/web/WebFrame.h" |
14 #include "third_party/WebKit/public/web/WebInputElement.h" | 14 #include "third_party/WebKit/public/web/WebInputElement.h" |
15 #include "third_party/WebKit/public/web/WebInputEvent.h" | 15 #include "third_party/WebKit/public/web/WebInputEvent.h" |
16 #include "third_party/WebKit/public/web/WebTextAreaElement.h" | |
16 #include "third_party/WebKit/public/web/WebView.h" | 17 #include "third_party/WebKit/public/web/WebView.h" |
17 | 18 |
18 using blink::WebDOMEvent; | 19 using blink::WebDOMEvent; |
19 using blink::WebDOMMouseEvent; | 20 using blink::WebDOMMouseEvent; |
20 using blink::WebElement; | 21 using blink::WebElement; |
21 using blink::WebFormControlElement; | 22 using blink::WebFormControlElement; |
22 using blink::WebFrame; | 23 using blink::WebFrame; |
23 using blink::WebInputElement; | 24 using blink::WebInputElement; |
24 using blink::WebInputEvent; | 25 using blink::WebInputEvent; |
25 using blink::WebMouseEvent; | 26 using blink::WebMouseEvent; |
26 using blink::WebNode; | 27 using blink::WebNode; |
27 using blink::WebString; | 28 using blink::WebString; |
29 using blink::WebTextAreaElement; | |
28 using blink::WebView; | 30 using blink::WebView; |
29 | 31 |
30 namespace { | 32 namespace { |
31 | 33 |
32 // Casts |node| to a WebInputElement. | 34 // Casts |node| to a WebInputElement. |
33 // Returns an empty (isNull()) WebInputElement if |node| is not a text field. | 35 // Returns an empty (isNull()) WebInputElement if |node| is not a text field. |
34 const WebInputElement GetTextWebInputElement(const WebNode& node) { | 36 const WebInputElement GetTextWebInputElement(const WebNode& node) { |
35 if (!node.isElementNode()) | 37 if (!node.isElementNode()) |
36 return WebInputElement(); | 38 return WebInputElement(); |
37 const WebElement element = node.toConst<WebElement>(); | 39 const WebElement element = node.toConst<WebElement>(); |
38 if (!element.hasTagName("input")) | 40 if (!element.hasTagName("input")) |
39 return WebInputElement(); | 41 return WebInputElement(); |
40 const WebInputElement* input = blink::toWebInputElement(&element); | 42 const WebInputElement* input = blink::toWebInputElement(&element); |
41 if (!autofill::IsTextInput(input)) | 43 if (!autofill::IsTextInput(input)) |
42 return WebInputElement(); | 44 return WebInputElement(); |
43 return *input; | 45 return *input; |
44 } | 46 } |
45 | 47 |
48 // Casts |node| to a WebTextAreaElement. | |
49 // Returns an empty (isNull()) WebTextAreaElement if |node| is not a | |
50 // textarea field. | |
51 const WebTextAreaElement GetTextWebTextAreaElement(const WebNode& node) { | |
52 if (!node.isElementNode()) | |
53 return WebTextAreaElement(); | |
54 const WebElement element = node.toConst<WebElement>(); | |
55 if (!element.hasTagName("textarea")) | |
56 return WebTextAreaElement(); | |
57 const WebTextAreaElement text_area = | |
58 element.toConst<WebTextAreaElement>(); | |
59 return text_area; | |
Ilya Sherman
2014/02/26 00:12:07
nit: Lines 57 through 59 can be shortened to just
ziran.sun
2014/02/26 18:11:50
Done.
| |
60 } | |
61 | |
46 // Checks to see if a text field was the previously selected node and is now | 62 // Checks to see if a text field was the previously selected node and is now |
47 // losing its focus. | 63 // losing its focus. |
48 bool DidSelectedTextFieldLoseFocus(const WebNode& newly_clicked_node) { | 64 bool DidSelectedTextFieldLoseFocus(const WebNode& newly_clicked_node) { |
49 blink::WebNode focused_node = newly_clicked_node.document().focusedNode(); | 65 blink::WebNode focused_node = newly_clicked_node.document().focusedNode(); |
50 | 66 |
51 if (focused_node.isNull() || GetTextWebInputElement(focused_node).isNull()) | 67 if (focused_node.isNull() || |
68 (GetTextWebInputElement(focused_node).isNull() && | |
69 GetTextWebTextAreaElement(focused_node).isNull())) | |
52 return false; | 70 return false; |
53 | 71 |
54 return focused_node != newly_clicked_node; | 72 return focused_node != newly_clicked_node; |
55 } | 73 } |
56 | 74 |
57 } // namespace | 75 } // namespace |
58 | 76 |
59 namespace autofill { | 77 namespace autofill { |
60 | 78 |
61 PageClickTracker::PageClickTracker(content::RenderView* render_view, | 79 PageClickTracker::PageClickTracker(content::RenderView* render_view, |
(...skipping 11 matching lines...) Expand all Loading... | |
73 // unregister listeners in frames remaining in tracked_frames_ as they might | 91 // unregister listeners in frames remaining in tracked_frames_ as they might |
74 // be invalid. | 92 // be invalid. |
75 } | 93 } |
76 | 94 |
77 void PageClickTracker::DidHandleMouseEvent(const WebMouseEvent& event) { | 95 void PageClickTracker::DidHandleMouseEvent(const WebMouseEvent& event) { |
78 if (event.type != WebInputEvent::MouseDown || | 96 if (event.type != WebInputEvent::MouseDown || |
79 last_node_clicked_.isNull()) { | 97 last_node_clicked_.isNull()) { |
80 return; | 98 return; |
81 } | 99 } |
82 | 100 |
83 // We are only interested in text field clicks. | 101 // We are only interested in text field and textarea field clicks. |
84 const WebInputElement input_element = | 102 const WebInputElement input_element = |
85 GetTextWebInputElement(last_node_clicked_); | 103 GetTextWebInputElement(last_node_clicked_); |
86 if (input_element.isNull()) | 104 const WebTextAreaElement textarea_element = |
105 GetTextWebTextAreaElement(last_node_clicked_); | |
106 if (input_element.isNull() && textarea_element.isNull()) | |
87 return; | 107 return; |
88 | 108 |
89 bool is_focused = (last_node_clicked_ == render_view()->GetFocusedNode()); | 109 if (!input_element.isNull()) { |
90 listener_->InputElementClicked(input_element, was_focused_, is_focused); | 110 listener_->FormControlElementClicked(input_element, |
111 was_focused_); | |
Ilya Sherman
2014/02/26 00:12:07
nit: Doesn't look like this line needs to wrap any
ziran.sun
2014/02/26 18:11:50
Done.
| |
112 } else if (!textarea_element.isNull()) { | |
113 listener_->FormControlElementClicked(textarea_element, | |
114 was_focused_); | |
Ilya Sherman
2014/02/26 00:12:07
nit: Doesn't look like this line needs to wrap any
ziran.sun
2014/02/26 18:11:50
Done.
| |
115 } | |
91 } | 116 } |
92 | 117 |
93 void PageClickTracker::DidFinishDocumentLoad(blink::WebFrame* frame) { | 118 void PageClickTracker::DidFinishDocumentLoad(blink::WebFrame* frame) { |
94 tracked_frames_.push_back(frame); | 119 tracked_frames_.push_back(frame); |
95 frame->document().addEventListener("mousedown", this, false); | 120 frame->document().addEventListener("mousedown", this, false); |
96 } | 121 } |
97 | 122 |
98 void PageClickTracker::FrameDetached(blink::WebFrame* frame) { | 123 void PageClickTracker::FrameDetached(blink::WebFrame* frame) { |
99 std::vector<blink::WebFrame*>::iterator iter = | 124 std::vector<blink::WebFrame*>::iterator iter = |
100 std::find(tracked_frames_.begin(), tracked_frames_.end(), frame); | 125 std::find(tracked_frames_.begin(), tracked_frames_.end(), frame); |
(...skipping 22 matching lines...) Expand all Loading... | |
123 // (DidHandleMouseEvent), we'll notify the listener at that point. | 148 // (DidHandleMouseEvent), we'll notify the listener at that point. |
124 WebNode node = mouse_event.target(); | 149 WebNode node = mouse_event.target(); |
125 if (node.isNull()) | 150 if (node.isNull()) |
126 // Node may be null if the target was an SVG instance element from a <use> | 151 // Node may be null if the target was an SVG instance element from a <use> |
127 // tree and the tree has been rebuilt due to an earlier event. | 152 // tree and the tree has been rebuilt due to an earlier event. |
128 return; | 153 return; |
129 | 154 |
130 HandleTextFieldMaybeLosingFocus(node); | 155 HandleTextFieldMaybeLosingFocus(node); |
131 | 156 |
132 // We are only interested in text field clicks. | 157 // We are only interested in text field clicks. |
133 if (GetTextWebInputElement(node).isNull()) | 158 if (GetTextWebInputElement(node).isNull() && |
159 GetTextWebTextAreaElement(node).isNull()) | |
134 return; | 160 return; |
135 | 161 |
136 last_node_clicked_ = node; | 162 last_node_clicked_ = node; |
137 was_focused_ = (node.document().focusedNode() == last_node_clicked_); | 163 was_focused_ = (node.document().focusedNode() == last_node_clicked_); |
138 } | 164 } |
139 | 165 |
140 void PageClickTracker::HandleTextFieldMaybeLosingFocus( | 166 void PageClickTracker::HandleTextFieldMaybeLosingFocus( |
141 const WebNode& newly_clicked_node) { | 167 const WebNode& newly_clicked_node) { |
142 if (DidSelectedTextFieldLoseFocus(newly_clicked_node)) | 168 if (DidSelectedTextFieldLoseFocus(newly_clicked_node)) |
143 listener_->InputElementLostFocus(); | 169 listener_->FormControlElementLostFocus(); |
144 } | 170 } |
145 | 171 |
146 } // namespace autofill | 172 } // namespace autofill |
OLD | NEW |