| 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 WebFormControlElement& controlElement = |
| 58 element.toConst<WebFormControlElement>(); |
| 59 if (!autofill::IsTextAreaElement(controlElement)) |
| 60 return WebTextAreaElement(); |
| 61 const WebTextAreaElement text_area = |
| 62 controlElement.toConst<WebTextAreaElement>(); |
| 63 return text_area; |
| 64 } |
| 65 |
| 46 // Checks to see if a text field was the previously selected node and is now | 66 // Checks to see if a text field was the previously selected node and is now |
| 47 // losing its focus. | 67 // losing its focus. |
| 48 bool DidSelectedTextFieldLoseFocus(const WebNode& newly_clicked_node) { | 68 bool DidSelectedTextFieldLoseFocus(const WebNode& newly_clicked_node) { |
| 49 blink::WebNode focused_node = newly_clicked_node.document().focusedNode(); | 69 blink::WebNode focused_node = newly_clicked_node.document().focusedNode(); |
| 50 | 70 |
| 51 if (focused_node.isNull() || GetTextWebInputElement(focused_node).isNull()) | 71 if (focused_node.isNull() || |
| 72 (GetTextWebInputElement(focused_node).isNull() && |
| 73 GetTextWebTextAreaElement(focused_node).isNull())) |
| 52 return false; | 74 return false; |
| 53 | 75 |
| 54 return focused_node != newly_clicked_node; | 76 return focused_node != newly_clicked_node; |
| 55 } | 77 } |
| 56 | 78 |
| 57 } // namespace | 79 } // namespace |
| 58 | 80 |
| 59 namespace autofill { | 81 namespace autofill { |
| 60 | 82 |
| 61 PageClickTracker::PageClickTracker(content::RenderView* render_view, | 83 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 | 95 // unregister listeners in frames remaining in tracked_frames_ as they might |
| 74 // be invalid. | 96 // be invalid. |
| 75 } | 97 } |
| 76 | 98 |
| 77 void PageClickTracker::DidHandleMouseEvent(const WebMouseEvent& event) { | 99 void PageClickTracker::DidHandleMouseEvent(const WebMouseEvent& event) { |
| 78 if (event.type != WebInputEvent::MouseDown || | 100 if (event.type != WebInputEvent::MouseDown || |
| 79 last_node_clicked_.isNull()) { | 101 last_node_clicked_.isNull()) { |
| 80 return; | 102 return; |
| 81 } | 103 } |
| 82 | 104 |
| 83 // We are only interested in text field clicks. | 105 // We are only interested in text field and textarea field clicks. |
| 84 const WebInputElement input_element = | 106 const WebInputElement input_element = |
| 85 GetTextWebInputElement(last_node_clicked_); | 107 GetTextWebInputElement(last_node_clicked_); |
| 86 if (input_element.isNull()) | 108 const WebTextAreaElement textarea_element = |
| 109 GetTextWebTextAreaElement(last_node_clicked_); |
| 110 if (input_element.isNull() && textarea_element.isNull()) |
| 87 return; | 111 return; |
| 88 | 112 |
| 89 bool is_focused = (last_node_clicked_ == render_view()->GetFocusedNode()); | 113 bool is_focused = (last_node_clicked_ == render_view()->GetFocusedNode()); |
| 90 listener_->InputElementClicked(input_element, was_focused_, is_focused); | 114 if (!input_element.isNull()) |
| 115 listener_->InputElementClicked(input_element, was_focused_, is_focused); |
| 116 if (!textarea_element.isNull()) |
| 117 listener_->TextAreaElementClicked(textarea_element, |
| 118 was_focused_, is_focused); |
| 91 } | 119 } |
| 92 | 120 |
| 93 void PageClickTracker::DidFinishDocumentLoad(blink::WebFrame* frame) { | 121 void PageClickTracker::DidFinishDocumentLoad(blink::WebFrame* frame) { |
| 94 tracked_frames_.push_back(frame); | 122 tracked_frames_.push_back(frame); |
| 95 frame->document().addEventListener("mousedown", this, false); | 123 frame->document().addEventListener("mousedown", this, false); |
| 96 } | 124 } |
| 97 | 125 |
| 98 void PageClickTracker::FrameDetached(blink::WebFrame* frame) { | 126 void PageClickTracker::FrameDetached(blink::WebFrame* frame) { |
| 99 std::vector<blink::WebFrame*>::iterator iter = | 127 std::vector<blink::WebFrame*>::iterator iter = |
| 100 std::find(tracked_frames_.begin(), tracked_frames_.end(), frame); | 128 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. | 151 // (DidHandleMouseEvent), we'll notify the listener at that point. |
| 124 WebNode node = mouse_event.target(); | 152 WebNode node = mouse_event.target(); |
| 125 if (node.isNull()) | 153 if (node.isNull()) |
| 126 // Node may be null if the target was an SVG instance element from a <use> | 154 // 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. | 155 // tree and the tree has been rebuilt due to an earlier event. |
| 128 return; | 156 return; |
| 129 | 157 |
| 130 HandleTextFieldMaybeLosingFocus(node); | 158 HandleTextFieldMaybeLosingFocus(node); |
| 131 | 159 |
| 132 // We are only interested in text field clicks. | 160 // We are only interested in text field clicks. |
| 133 if (GetTextWebInputElement(node).isNull()) | 161 if (GetTextWebInputElement(node).isNull() && |
| 162 GetTextWebTextAreaElement(node).isNull()) |
| 134 return; | 163 return; |
| 135 | 164 |
| 136 last_node_clicked_ = node; | 165 last_node_clicked_ = node; |
| 137 was_focused_ = (node.document().focusedNode() == last_node_clicked_); | 166 was_focused_ = (node.document().focusedNode() == last_node_clicked_); |
| 138 } | 167 } |
| 139 | 168 |
| 140 void PageClickTracker::HandleTextFieldMaybeLosingFocus( | 169 void PageClickTracker::HandleTextFieldMaybeLosingFocus( |
| 141 const WebNode& newly_clicked_node) { | 170 const WebNode& newly_clicked_node) { |
| 142 if (DidSelectedTextFieldLoseFocus(newly_clicked_node)) | 171 if (DidSelectedTextFieldLoseFocus(newly_clicked_node)) |
| 143 listener_->InputElementLostFocus(); | 172 listener_->FormControlElementLostFocus(); |
| 144 } | 173 } |
| 145 | 174 |
| 146 } // namespace autofill | 175 } // namespace autofill |
| OLD | NEW |