| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 2012 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. | |
| 4 | |
| 5 #include "chrome/renderer/page_click_tracker.h" | |
| 6 | |
| 7 #include "chrome/renderer/autofill/form_autofill_util.h" | |
| 8 #include "chrome/renderer/page_click_listener.h" | |
| 9 #include "content/public/renderer/render_view.h" | |
| 10 #include "third_party/WebKit/Source/Platform/chromium/public/WebString.h" | |
| 11 #include "third_party/WebKit/Source/WebKit/chromium/public/WebDocument.h" | |
| 12 #include "third_party/WebKit/Source/WebKit/chromium/public/WebDOMMouseEvent.h" | |
| 13 #include "third_party/WebKit/Source/WebKit/chromium/public/WebFrame.h" | |
| 14 #include "third_party/WebKit/Source/WebKit/chromium/public/WebInputElement.h" | |
| 15 #include "third_party/WebKit/Source/WebKit/chromium/public/WebInputEvent.h" | |
| 16 #include "third_party/WebKit/Source/WebKit/chromium/public/WebView.h" | |
| 17 | |
| 18 using WebKit::WebDOMEvent; | |
| 19 using WebKit::WebDOMMouseEvent; | |
| 20 using WebKit::WebElement; | |
| 21 using WebKit::WebFormControlElement; | |
| 22 using WebKit::WebFrame; | |
| 23 using WebKit::WebInputElement; | |
| 24 using WebKit::WebInputEvent; | |
| 25 using WebKit::WebMouseEvent; | |
| 26 using WebKit::WebNode; | |
| 27 using WebKit::WebString; | |
| 28 using WebKit::WebView; | |
| 29 | |
| 30 namespace { | |
| 31 | |
| 32 // Casts |node| to a WebInputElement. | |
| 33 // Returns an empty (isNull()) WebInputElement if |node| is not a text field. | |
| 34 const WebInputElement GetTextWebInputElement(const WebNode& node) { | |
| 35 if (!node.isElementNode()) | |
| 36 return WebInputElement(); | |
| 37 const WebElement element = node.toConst<WebElement>(); | |
| 38 if (!element.hasTagName("input")) | |
| 39 return WebInputElement(); | |
| 40 const WebInputElement* input = WebKit::toWebInputElement(&element); | |
| 41 if (!autofill::IsTextInput(input)) | |
| 42 return WebInputElement(); | |
| 43 return *input; | |
| 44 } | |
| 45 | |
| 46 // Checks to see if a text field was the previously selected node and is now | |
| 47 // losing its focus. | |
| 48 bool DidSelectedTextFieldLoseFocus(const WebNode& newly_clicked_node) { | |
| 49 WebKit::WebNode focused_node = newly_clicked_node.document().focusedNode(); | |
| 50 | |
| 51 if (focused_node.isNull() || GetTextWebInputElement(focused_node).isNull()) | |
| 52 return false; | |
| 53 | |
| 54 return focused_node != newly_clicked_node; | |
| 55 } | |
| 56 | |
| 57 } // namespace | |
| 58 | |
| 59 PageClickTracker::PageClickTracker(content::RenderView* render_view) | |
| 60 : content::RenderViewObserver(render_view), | |
| 61 was_focused_(false) { | |
| 62 } | |
| 63 | |
| 64 PageClickTracker::~PageClickTracker() { | |
| 65 // Note that even though RenderView calls FrameDetached when notified that | |
| 66 // a frame was closed, it might not always get that notification from WebKit | |
| 67 // for all frames. | |
| 68 // By the time we get here, the frame could have been destroyed so we cannot | |
| 69 // unregister listeners in frames remaining in tracked_frames_ as they might | |
| 70 // be invalid. | |
| 71 } | |
| 72 | |
| 73 void PageClickTracker::DidHandleMouseEvent(const WebMouseEvent& event) { | |
| 74 if (event.type != WebInputEvent::MouseDown || | |
| 75 last_node_clicked_.isNull()) { | |
| 76 return; | |
| 77 } | |
| 78 | |
| 79 // We are only interested in text field clicks. | |
| 80 const WebInputElement input_element = | |
| 81 GetTextWebInputElement(last_node_clicked_); | |
| 82 if (input_element.isNull()) | |
| 83 return; | |
| 84 | |
| 85 bool is_focused = (last_node_clicked_ == render_view()->GetFocusedNode()); | |
| 86 ObserverListBase<PageClickListener>::Iterator it(listeners_); | |
| 87 PageClickListener* listener; | |
| 88 while ((listener = it.GetNext()) != NULL) { | |
| 89 if (listener->InputElementClicked(input_element, was_focused_, is_focused)) | |
| 90 break; | |
| 91 } | |
| 92 } | |
| 93 | |
| 94 void PageClickTracker::AddListener(PageClickListener* listener) { | |
| 95 listeners_.AddObserver(listener); | |
| 96 } | |
| 97 | |
| 98 void PageClickTracker::RemoveListener(PageClickListener* listener) { | |
| 99 listeners_.RemoveObserver(listener); | |
| 100 } | |
| 101 | |
| 102 void PageClickTracker::DidFinishDocumentLoad(WebKit::WebFrame* frame) { | |
| 103 tracked_frames_.push_back(frame); | |
| 104 frame->document().addEventListener("mousedown", this, false); | |
| 105 } | |
| 106 | |
| 107 void PageClickTracker::FrameDetached(WebKit::WebFrame* frame) { | |
| 108 FrameList::iterator iter = | |
| 109 std::find(tracked_frames_.begin(), tracked_frames_.end(), frame); | |
| 110 if (iter == tracked_frames_.end()) { | |
| 111 // Some frames might never load contents so we may not have a listener on | |
| 112 // them. Calling removeEventListener() on them would trigger an assert, so | |
| 113 // we need to keep track of which frames we are listening to. | |
| 114 return; | |
| 115 } | |
| 116 tracked_frames_.erase(iter); | |
| 117 } | |
| 118 | |
| 119 void PageClickTracker::handleEvent(const WebDOMEvent& event) { | |
| 120 last_node_clicked_.reset(); | |
| 121 | |
| 122 if (!event.isMouseEvent()) | |
| 123 return; | |
| 124 | |
| 125 const WebDOMMouseEvent mouse_event = event.toConst<WebDOMMouseEvent>(); | |
| 126 DCHECK(mouse_event.buttonDown()); | |
| 127 if (mouse_event.button() != 0) | |
| 128 return; // We are only interested in left clicks. | |
| 129 | |
| 130 // Remember which node has focus before the click is processed. | |
| 131 // We'll get a notification once the mouse event has been processed | |
| 132 // (DidHandleMouseEvent), we'll notify the listener at that point. | |
| 133 WebNode node = mouse_event.target(); | |
| 134 if (node.isNull()) | |
| 135 // Node may be null if the target was an SVG instance element from a <use> | |
| 136 // tree and the tree has been rebuilt due to an earlier event. | |
| 137 return; | |
| 138 | |
| 139 HandleTextFieldMaybeLosingFocus(node); | |
| 140 | |
| 141 // We are only interested in text field clicks. | |
| 142 if (GetTextWebInputElement(node).isNull()) | |
| 143 return; | |
| 144 | |
| 145 last_node_clicked_ = node; | |
| 146 was_focused_ = (node.document().focusedNode() == last_node_clicked_); | |
| 147 } | |
| 148 | |
| 149 void PageClickTracker::HandleTextFieldMaybeLosingFocus( | |
| 150 const WebNode& newly_clicked_node) { | |
| 151 if (!DidSelectedTextFieldLoseFocus(newly_clicked_node)) | |
| 152 return; | |
| 153 | |
| 154 ObserverListBase<PageClickListener>::Iterator it(listeners_); | |
| 155 PageClickListener* listener; | |
| 156 while ((listener = it.GetNext()) != NULL) { | |
| 157 if (listener->InputElementLostFocus()) | |
| 158 break; | |
| 159 } | |
| 160 } | |
| OLD | NEW |