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

Side by Side Diff: chrome/renderer/page_click_tracker.cc

Issue 6151011: Introduce RenderView::Observer interface so that RenderView doesn't have to k... (Closed) Base URL: svn://chrome-svn/chrome/trunk/src/
Patch Set: '' Created 9 years, 11 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 | Annotate | Revision Log
OLDNEW
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/renderer/page_click_listener.h" 7 #include "chrome/renderer/page_click_listener.h"
8 #include "chrome/renderer/render_view.h"
9 #include "third_party/WebKit/WebKit/chromium/public/WebDocument.h" 8 #include "third_party/WebKit/WebKit/chromium/public/WebDocument.h"
10 #include "third_party/WebKit/WebKit/chromium/public/WebDOMMouseEvent.h" 9 #include "third_party/WebKit/WebKit/chromium/public/WebDOMMouseEvent.h"
11 #include "third_party/WebKit/WebKit/chromium/public/WebFrame.h" 10 #include "third_party/WebKit/WebKit/chromium/public/WebFrame.h"
12 #include "third_party/WebKit/WebKit/chromium/public/WebInputElement.h" 11 #include "third_party/WebKit/WebKit/chromium/public/WebInputElement.h"
13 #include "third_party/WebKit/WebKit/chromium/public/WebString.h" 12 #include "third_party/WebKit/WebKit/chromium/public/WebString.h"
14 #include "third_party/WebKit/WebKit/chromium/public/WebView.h" 13 #include "third_party/WebKit/WebKit/chromium/public/WebView.h"
15 14
16 using WebKit::WebDOMEvent; 15 using WebKit::WebDOMEvent;
17 using WebKit::WebDOMMouseEvent; 16 using WebKit::WebDOMMouseEvent;
18 using WebKit::WebElement; 17 using WebKit::WebElement;
19 using WebKit::WebFormControlElement; 18 using WebKit::WebFormControlElement;
20 using WebKit::WebFrame; 19 using WebKit::WebFrame;
21 using WebKit::WebInputElement; 20 using WebKit::WebInputElement;
22 using WebKit::WebInputEvent; 21 using WebKit::WebInputEvent;
23 using WebKit::WebMouseEvent; 22 using WebKit::WebMouseEvent;
24 using WebKit::WebNode; 23 using WebKit::WebNode;
25 using WebKit::WebString; 24 using WebKit::WebString;
26 using WebKit::WebView; 25 using WebKit::WebView;
27 26
28 PageClickTracker::PageClickTracker(RenderView* render_view) 27 PageClickTracker::PageClickTracker(RenderView* render_view)
29 : render_view_(render_view), 28 : RenderView::Observer(render_view),
30 was_focused_(false) { 29 was_focused_(false) {
31 } 30 }
32 31
33 PageClickTracker::~PageClickTracker() { 32 PageClickTracker::~PageClickTracker() {
34 // Note that even though RenderView calls StopTrackingFrame when notified that 33 // Note that even though RenderView calls FrameDetached when notified that
35 // a frame was closed, it might not always get that notification from WebKit 34 // a frame was closed, it might not always get that notification from WebKit
36 // for all frames. 35 // for all frames.
37 // By the time we get here, the frame could have been destroyed so we cannot 36 // By the time we get here, the frame could have been destroyed so we cannot
38 // unregister listeners in frames remaining in tracked_frames_ as they might 37 // unregister listeners in frames remaining in tracked_frames_ as they might
39 // be invalid. 38 // be invalid.
40 } 39 }
41 40
42 void PageClickTracker::StartTrackingFrame(WebFrame* frame) {
43 tracked_frames_.push_back(frame);
44 frame->document().addEventListener("mousedown", this, false);
45 }
46
47 void PageClickTracker::StopTrackingFrame(WebFrame* frame, bool frame_detached) {
48 FrameList::iterator iter =
49 std::find(tracked_frames_.begin(), tracked_frames_.end(), frame);
50 if (iter == tracked_frames_.end()) {
51 // Some frames might never load contents so we may not have a listener on
52 // them. Calling removeEventListener() on them would trigger an assert, so
53 // we need to keep track of which frames we are listening to.
54 return;
55 }
56 tracked_frames_.erase(iter);
57 // If the frame has been detached, all event listeners have already been
58 // removed.
59 if (!frame_detached)
60 frame->document().removeEventListener("mousedown", this, false);
61 }
62
63 void PageClickTracker::DidHandleMouseEvent(const WebMouseEvent& event) { 41 void PageClickTracker::DidHandleMouseEvent(const WebMouseEvent& event) {
64 if (event.type != WebInputEvent::MouseDown || 42 if (event.type != WebInputEvent::MouseDown ||
65 last_node_clicked_.isNull()) { 43 last_node_clicked_.isNull()) {
66 return; 44 return;
67 } 45 }
68 46
69 // We are only interested in text field clicks. 47 // We are only interested in text field clicks.
70 if (!last_node_clicked_.isElementNode()) 48 if (!last_node_clicked_.isElementNode())
71 return; 49 return;
72 const WebElement& element = last_node_clicked_.toConst<WebElement>(); 50 const WebElement& element = last_node_clicked_.toConst<WebElement>();
(...skipping 18 matching lines...) Expand all
91 } 69 }
92 70
93 void PageClickTracker::AddListener(PageClickListener* listener) { 71 void PageClickTracker::AddListener(PageClickListener* listener) {
94 listeners_.AddObserver(listener); 72 listeners_.AddObserver(listener);
95 } 73 }
96 74
97 void PageClickTracker::RemoveListener(PageClickListener* listener) { 75 void PageClickTracker::RemoveListener(PageClickListener* listener) {
98 listeners_.RemoveObserver(listener); 76 listeners_.RemoveObserver(listener);
99 } 77 }
100 78
79 bool PageClickTracker::OnMessageReceived(const IPC::Message& message) {
80 if (message.type() == ViewMsg_HandleInputEvent::ID) {
81 void* iter = NULL;
82 const char* data;
83 int data_length;
84 if (message.ReadData(&iter, &data, &data_length)) {
85 const WebInputEvent* input_event =
86 reinterpret_cast<const WebInputEvent*>(data);
87 if (WebInputEvent::isMouseEventType(input_event->type))
88 DidHandleMouseEvent(*(static_cast<const WebMouseEvent*>(input_event)));
89 }
90 }
91 return false;
92 }
93
94 void PageClickTracker::DidFinishDocumentLoad(WebKit::WebFrame* frame) {
95 tracked_frames_.push_back(frame);
96 frame->document().addEventListener("mousedown", this, false);
97 }
98
99 void PageClickTracker::FrameDetached(WebKit::WebFrame* frame) {
100 FrameList::iterator iter =
101 std::find(tracked_frames_.begin(), tracked_frames_.end(), frame);
102 if (iter == tracked_frames_.end()) {
103 // Some frames might never load contents so we may not have a listener on
104 // them. Calling removeEventListener() on them would trigger an assert, so
105 // we need to keep track of which frames we are listening to.
106 return;
107 }
108 tracked_frames_.erase(iter);
109 }
110
101 void PageClickTracker::handleEvent(const WebDOMEvent& event) { 111 void PageClickTracker::handleEvent(const WebDOMEvent& event) {
102 last_node_clicked_.reset(); 112 last_node_clicked_.reset();
103 113
104 if (!event.isMouseEvent()) 114 if (!event.isMouseEvent())
105 return; 115 return;
106 116
107 const WebDOMMouseEvent mouse_event = event.toConst<WebDOMMouseEvent>(); 117 const WebDOMMouseEvent mouse_event = event.toConst<WebDOMMouseEvent>();
108 DCHECK(mouse_event.buttonDown()); 118 DCHECK(mouse_event.buttonDown());
109 if (mouse_event.button() != 0) 119 if (mouse_event.button() != 0)
110 return; // We are only interested in left clicks. 120 return; // We are only interested in left clicks.
111 121
112 // Remember which node has focus before the click is processed. 122 // Remember which node has focus before the click is processed.
113 // We'll get a notification once the mouse event has been processed 123 // We'll get a notification once the mouse event has been processed
114 // (DidHandleMouseEvent), we'll notify the listener at that point. 124 // (DidHandleMouseEvent), we'll notify the listener at that point.
115 last_node_clicked_ = mouse_event.target(); 125 last_node_clicked_ = mouse_event.target();
116 was_focused_ = (GetFocusedNode() == last_node_clicked_); 126 was_focused_ = (GetFocusedNode() == last_node_clicked_);
117 } 127 }
118 128
119 WebNode PageClickTracker::GetFocusedNode() { 129 WebNode PageClickTracker::GetFocusedNode() {
120 WebView* web_view = render_view_->webview(); 130 WebView* web_view = render_view()->webview();
121 if (!web_view) 131 if (!web_view)
122 return WebNode(); 132 return WebNode();
123 133
124 WebFrame* web_frame = web_view->focusedFrame(); 134 WebFrame* web_frame = web_view->focusedFrame();
125 if (!web_frame) 135 if (!web_frame)
126 return WebNode(); 136 return WebNode();
127 137
128 return web_frame->document().focusedNode(); 138 return web_frame->document().focusedNode();
129 } 139 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698