OLD | NEW |
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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 "chrome/renderer/page_click_tracker.h" | 5 #include "chrome/renderer/page_click_tracker.h" |
6 | 6 |
7 #include "chrome/common/render_messages.h" | 7 #include "chrome/common/render_messages.h" |
8 #include "chrome/renderer/page_click_listener.h" | 8 #include "chrome/renderer/page_click_listener.h" |
9 #include "content/common/view_messages.h" | 9 #include "content/common/view_messages.h" |
10 #include "content/renderer/render_view.h" | 10 #include "content/renderer/render_view.h" |
11 #include "third_party/WebKit/Source/WebKit/chromium/public/WebDocument.h" | 11 #include "third_party/WebKit/Source/WebKit/chromium/public/WebDocument.h" |
12 #include "third_party/WebKit/Source/WebKit/chromium/public/WebDOMMouseEvent.h" | 12 #include "third_party/WebKit/Source/WebKit/chromium/public/WebDOMMouseEvent.h" |
13 #include "third_party/WebKit/Source/WebKit/chromium/public/WebFrame.h" | 13 #include "third_party/WebKit/Source/WebKit/chromium/public/WebFrame.h" |
14 #include "third_party/WebKit/Source/WebKit/chromium/public/WebInputElement.h" | 14 #include "third_party/WebKit/Source/WebKit/chromium/public/WebInputElement.h" |
15 #include "third_party/WebKit/Source/WebKit/chromium/public/WebString.h" | 15 #include "third_party/WebKit/Source/WebKit/chromium/public/WebString.h" |
16 #include "third_party/WebKit/Source/WebKit/chromium/public/WebView.h" | 16 #include "third_party/WebKit/Source/WebKit/chromium/public/WebView.h" |
17 | 17 |
18 using WebKit::WebDOMEvent; | 18 using WebKit::WebDOMEvent; |
19 using WebKit::WebDOMMouseEvent; | 19 using WebKit::WebDOMMouseEvent; |
20 using WebKit::WebElement; | 20 using WebKit::WebElement; |
21 using WebKit::WebFormControlElement; | 21 using WebKit::WebFormControlElement; |
22 using WebKit::WebFrame; | 22 using WebKit::WebFrame; |
23 using WebKit::WebInputElement; | 23 using WebKit::WebInputElement; |
24 using WebKit::WebInputEvent; | 24 using WebKit::WebInputEvent; |
25 using WebKit::WebMouseEvent; | 25 using WebKit::WebMouseEvent; |
26 using WebKit::WebNode; | 26 using WebKit::WebNode; |
27 using WebKit::WebString; | 27 using WebKit::WebString; |
28 using WebKit::WebView; | 28 using WebKit::WebView; |
29 | 29 |
| 30 namespace { |
| 31 |
| 32 // Casts |node| to a WebInputElement. |
| 33 // Returns an empty (isNull()) WebInputElement if |node| is not a text |
| 34 // WebInputElement. |
| 35 const WebInputElement GetTextWebInputElement(const WebNode& node) { |
| 36 if (!node.isElementNode()) |
| 37 return WebInputElement(); |
| 38 const WebElement element = node.toConst<WebElement>(); |
| 39 if (!element.isFormControlElement()) |
| 40 return WebInputElement(); |
| 41 const WebFormControlElement control = |
| 42 element.toConst<WebFormControlElement>(); |
| 43 if (control.formControlType() != WebString::fromUTF8("text")) |
| 44 return WebInputElement(); |
| 45 return element.toConst<WebInputElement>(); |
| 46 } |
| 47 |
| 48 } // namespace |
| 49 |
30 PageClickTracker::PageClickTracker(RenderView* render_view) | 50 PageClickTracker::PageClickTracker(RenderView* render_view) |
31 : RenderViewObserver(render_view), | 51 : RenderViewObserver(render_view), |
32 was_focused_(false) { | 52 was_focused_(false) { |
33 } | 53 } |
34 | 54 |
35 PageClickTracker::~PageClickTracker() { | 55 PageClickTracker::~PageClickTracker() { |
36 // Note that even though RenderView calls FrameDetached when notified that | 56 // Note that even though RenderView calls FrameDetached when notified that |
37 // a frame was closed, it might not always get that notification from WebKit | 57 // a frame was closed, it might not always get that notification from WebKit |
38 // for all frames. | 58 // for all frames. |
39 // By the time we get here, the frame could have been destroyed so we cannot | 59 // By the time we get here, the frame could have been destroyed so we cannot |
40 // unregister listeners in frames remaining in tracked_frames_ as they might | 60 // unregister listeners in frames remaining in tracked_frames_ as they might |
41 // be invalid. | 61 // be invalid. |
42 } | 62 } |
43 | 63 |
44 void PageClickTracker::DidHandleMouseEvent(const WebMouseEvent& event) { | 64 void PageClickTracker::DidHandleMouseEvent(const WebMouseEvent& event) { |
45 if (event.type != WebInputEvent::MouseDown || | 65 if (event.type != WebInputEvent::MouseDown || |
46 last_node_clicked_.isNull()) { | 66 last_node_clicked_.isNull()) { |
47 return; | 67 return; |
48 } | 68 } |
49 | 69 |
50 // We are only interested in text field clicks. | 70 // We are only interested in text field clicks. |
51 if (!last_node_clicked_.isElementNode()) | 71 const WebInputElement input_element = |
| 72 GetTextWebInputElement(last_node_clicked_); |
| 73 if (input_element.isNull()) |
52 return; | 74 return; |
53 const WebElement& element = last_node_clicked_.toConst<WebElement>(); | |
54 if (!element.isFormControlElement()) | |
55 return; | |
56 const WebFormControlElement& control = | |
57 element.toConst<WebFormControlElement>(); | |
58 if (control.formControlType() != WebString::fromUTF8("text")) | |
59 return; | |
60 | |
61 const WebInputElement& input_element = element.toConst<WebInputElement>(); | |
62 | 75 |
63 bool is_focused = (last_node_clicked_ == GetFocusedNode()); | 76 bool is_focused = (last_node_clicked_ == GetFocusedNode()); |
64 ObserverListBase<PageClickListener>::Iterator it(listeners_); | 77 ObserverListBase<PageClickListener>::Iterator it(listeners_); |
65 PageClickListener* listener; | 78 PageClickListener* listener; |
66 while ((listener = it.GetNext()) != NULL) { | 79 while ((listener = it.GetNext()) != NULL) { |
67 if (listener->InputElementClicked(input_element, was_focused_, is_focused)) | 80 if (listener->InputElementClicked(input_element, was_focused_, is_focused)) |
68 break; | 81 break; |
69 } | 82 } |
70 | 83 |
71 last_node_clicked_.reset(); | 84 last_node_clicked_.reset(); |
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
118 return; | 131 return; |
119 | 132 |
120 const WebDOMMouseEvent mouse_event = event.toConst<WebDOMMouseEvent>(); | 133 const WebDOMMouseEvent mouse_event = event.toConst<WebDOMMouseEvent>(); |
121 DCHECK(mouse_event.buttonDown()); | 134 DCHECK(mouse_event.buttonDown()); |
122 if (mouse_event.button() != 0) | 135 if (mouse_event.button() != 0) |
123 return; // We are only interested in left clicks. | 136 return; // We are only interested in left clicks. |
124 | 137 |
125 // Remember which node has focus before the click is processed. | 138 // Remember which node has focus before the click is processed. |
126 // We'll get a notification once the mouse event has been processed | 139 // We'll get a notification once the mouse event has been processed |
127 // (DidHandleMouseEvent), we'll notify the listener at that point. | 140 // (DidHandleMouseEvent), we'll notify the listener at that point. |
128 last_node_clicked_ = mouse_event.target(); | 141 WebNode node = mouse_event.target(); |
| 142 // We are only interested in text field clicks. |
| 143 if (GetTextWebInputElement(node).isNull()) |
| 144 return; |
| 145 |
| 146 last_node_clicked_ = node; |
129 was_focused_ = (GetFocusedNode() == last_node_clicked_); | 147 was_focused_ = (GetFocusedNode() == last_node_clicked_); |
130 } | 148 } |
131 | 149 |
132 WebNode PageClickTracker::GetFocusedNode() { | 150 WebNode PageClickTracker::GetFocusedNode() { |
133 WebView* web_view = render_view()->webview(); | 151 WebView* web_view = render_view()->webview(); |
134 if (!web_view) | 152 if (!web_view) |
135 return WebNode(); | 153 return WebNode(); |
136 | 154 |
137 WebFrame* web_frame = web_view->focusedFrame(); | 155 WebFrame* web_frame = web_view->focusedFrame(); |
138 if (!web_frame) | 156 if (!web_frame) |
139 return WebNode(); | 157 return WebNode(); |
140 | 158 |
141 return web_frame->document().focusedNode(); | 159 return web_frame->document().focusedNode(); |
142 } | 160 } |
OLD | NEW |