Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(89)

Side by Side Diff: components/autofill/content/renderer/page_click_tracker.cc

Issue 140093005: Add supports that allow Autofill to be initiated from textarea field (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Update code as per review comments Created 6 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 WebFormControlElement& controlElement =
58 element.toConst<WebFormControlElement>();
Ilya Sherman 2014/02/22 06:24:20 Can we skip this cast, and cast directly to a WebT
59 const WebTextAreaElement text_area =
60 controlElement.toConst<WebTextAreaElement>();
61 return text_area;
62 }
63
46 // Checks to see if a text field was the previously selected node and is now 64 // Checks to see if a text field was the previously selected node and is now
47 // losing its focus. 65 // losing its focus.
48 bool DidSelectedTextFieldLoseFocus(const WebNode& newly_clicked_node) { 66 bool DidSelectedTextFieldLoseFocus(const WebNode& newly_clicked_node) {
49 blink::WebNode focused_node = newly_clicked_node.document().focusedNode(); 67 blink::WebNode focused_node = newly_clicked_node.document().focusedNode();
50 68
51 if (focused_node.isNull() || GetTextWebInputElement(focused_node).isNull()) 69 if (focused_node.isNull() ||
70 (GetTextWebInputElement(focused_node).isNull() &&
71 GetTextWebTextAreaElement(focused_node).isNull()))
52 return false; 72 return false;
53 73
54 return focused_node != newly_clicked_node; 74 return focused_node != newly_clicked_node;
55 } 75 }
56 76
57 } // namespace 77 } // namespace
58 78
59 namespace autofill { 79 namespace autofill {
60 80
61 PageClickTracker::PageClickTracker(content::RenderView* render_view, 81 PageClickTracker::PageClickTracker(content::RenderView* render_view,
(...skipping 11 matching lines...) Expand all
73 // unregister listeners in frames remaining in tracked_frames_ as they might 93 // unregister listeners in frames remaining in tracked_frames_ as they might
74 // be invalid. 94 // be invalid.
75 } 95 }
76 96
77 void PageClickTracker::DidHandleMouseEvent(const WebMouseEvent& event) { 97 void PageClickTracker::DidHandleMouseEvent(const WebMouseEvent& event) {
78 if (event.type != WebInputEvent::MouseDown || 98 if (event.type != WebInputEvent::MouseDown ||
79 last_node_clicked_.isNull()) { 99 last_node_clicked_.isNull()) {
80 return; 100 return;
81 } 101 }
82 102
83 // We are only interested in text field clicks. 103 // We are only interested in text field and textarea field clicks.
84 const WebInputElement input_element = 104 const WebInputElement input_element =
85 GetTextWebInputElement(last_node_clicked_); 105 GetTextWebInputElement(last_node_clicked_);
86 if (input_element.isNull()) 106 const WebTextAreaElement textarea_element =
107 GetTextWebTextAreaElement(last_node_clicked_);
108 if (input_element.isNull() && textarea_element.isNull())
87 return; 109 return;
88 110
89 bool is_focused = (last_node_clicked_ == render_view()->GetFocusedNode()); 111 bool is_focused = (last_node_clicked_ == render_view()->GetFocusedNode());
90 listener_->InputElementClicked(input_element, was_focused_, is_focused); 112 if (!input_element.isNull())
Ilya Sherman 2014/02/22 06:24:20 nit: This if-stmt's body spans multiple lines of c
113 listener_->FormControlElementClicked(input_element,
114 was_focused_,
115 is_focused);
116 if (!textarea_element.isNull()) {
Ilya Sherman 2014/02/22 06:24:20 nit: This can be an else-if.
117 listener_->FormControlElementClicked(textarea_element,
118 was_focused_,
119 is_focused);
120 }
91 } 121 }
92 122
93 void PageClickTracker::DidFinishDocumentLoad(blink::WebFrame* frame) { 123 void PageClickTracker::DidFinishDocumentLoad(blink::WebFrame* frame) {
94 tracked_frames_.push_back(frame); 124 tracked_frames_.push_back(frame);
95 frame->document().addEventListener("mousedown", this, false); 125 frame->document().addEventListener("mousedown", this, false);
96 } 126 }
97 127
98 void PageClickTracker::FrameDetached(blink::WebFrame* frame) { 128 void PageClickTracker::FrameDetached(blink::WebFrame* frame) {
99 std::vector<blink::WebFrame*>::iterator iter = 129 std::vector<blink::WebFrame*>::iterator iter =
100 std::find(tracked_frames_.begin(), tracked_frames_.end(), frame); 130 std::find(tracked_frames_.begin(), tracked_frames_.end(), frame);
(...skipping 22 matching lines...) Expand all
123 // (DidHandleMouseEvent), we'll notify the listener at that point. 153 // (DidHandleMouseEvent), we'll notify the listener at that point.
124 WebNode node = mouse_event.target(); 154 WebNode node = mouse_event.target();
125 if (node.isNull()) 155 if (node.isNull())
126 // Node may be null if the target was an SVG instance element from a <use> 156 // 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. 157 // tree and the tree has been rebuilt due to an earlier event.
128 return; 158 return;
129 159
130 HandleTextFieldMaybeLosingFocus(node); 160 HandleTextFieldMaybeLosingFocus(node);
131 161
132 // We are only interested in text field clicks. 162 // We are only interested in text field clicks.
133 if (GetTextWebInputElement(node).isNull()) 163 if (GetTextWebInputElement(node).isNull() &&
164 GetTextWebTextAreaElement(node).isNull())
134 return; 165 return;
135 166
136 last_node_clicked_ = node; 167 last_node_clicked_ = node;
137 was_focused_ = (node.document().focusedNode() == last_node_clicked_); 168 was_focused_ = (node.document().focusedNode() == last_node_clicked_);
138 } 169 }
139 170
140 void PageClickTracker::HandleTextFieldMaybeLosingFocus( 171 void PageClickTracker::HandleTextFieldMaybeLosingFocus(
141 const WebNode& newly_clicked_node) { 172 const WebNode& newly_clicked_node) {
142 if (DidSelectedTextFieldLoseFocus(newly_clicked_node)) 173 if (DidSelectedTextFieldLoseFocus(newly_clicked_node))
143 listener_->InputElementLostFocus(); 174 listener_->FormControlElementLostFocus();
144 } 175 }
145 176
146 } // namespace autofill 177 } // namespace autofill
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698