[email protected] | 65ec8db | 2011-11-04 14:50:44 | [diff] [blame] | 1 | // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
[email protected] | 6a8ddba5 | 2010-09-05 04:38:06 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #ifndef CHROME_RENDERER_PAGE_CLICK_TRACKER_H_ |
| 6 | #define CHROME_RENDERER_PAGE_CLICK_TRACKER_H_ |
| 7 | |
| 8 | #include <vector> |
| 9 | |
| 10 | #include "base/basictypes.h" |
| 11 | #include "base/observer_list.h" |
[email protected] | 3a034ebb | 2011-10-03 19:19:44 | [diff] [blame] | 12 | #include "content/public/renderer/render_view_observer.h" |
[email protected] | 8bd0fe6 | 2011-01-17 06:44:37 | [diff] [blame] | 13 | #include "third_party/WebKit/Source/WebKit/chromium/public/WebDOMEventListener.h" |
| 14 | #include "third_party/WebKit/Source/WebKit/chromium/public/WebNode.h" |
[email protected] | 6a8ddba5 | 2010-09-05 04:38:06 | [diff] [blame] | 15 | |
| 16 | class PageClickListener; |
[email protected] | 6a8ddba5 | 2010-09-05 04:38:06 | [diff] [blame] | 17 | |
[email protected] | 6a8ddba5 | 2010-09-05 04:38:06 | [diff] [blame] | 18 | |
| 19 | // This class is responsible for tracking clicks on elements in web pages and |
| 20 | // notifiying the associated listener when a node is clicked. |
| 21 | // Compared to a simple WebDOMEventListener, it offers the added capability of |
| 22 | // notifying the listeners of whether the clicked node was already focused |
| 23 | // before it was clicked. |
| 24 | // |
| 25 | // This is useful for password/form autofill where we want to trigger a |
| 26 | // suggestion popup when a text input is clicked. |
| 27 | // It only notifies of WebInputElement that are text inputs being clicked, but |
| 28 | // could easily be changed to report click on any type of WebNode. |
| 29 | // |
| 30 | // There is one PageClickTracker per RenderView. |
[email protected] | 3a034ebb | 2011-10-03 19:19:44 | [diff] [blame] | 31 | class PageClickTracker : public content::RenderViewObserver, |
[email protected] | 676126f7 | 2011-01-15 00:03:51 | [diff] [blame] | 32 | public WebKit::WebDOMEventListener { |
[email protected] | 6a8ddba5 | 2010-09-05 04:38:06 | [diff] [blame] | 33 | public: |
[email protected] | a2ef54c | 2011-10-10 16:20:31 | [diff] [blame] | 34 | explicit PageClickTracker(content::RenderView* render_view); |
[email protected] | 6a8ddba5 | 2010-09-05 04:38:06 | [diff] [blame] | 35 | virtual ~PageClickTracker(); |
| 36 | |
[email protected] | 6a8ddba5 | 2010-09-05 04:38:06 | [diff] [blame] | 37 | // Adds/removes a listener for getting notification when an element is |
| 38 | // clicked. Note that the order of insertion is important as a listener when |
| 39 | // notified can decide to stop the propagation of the event (so that listeners |
| 40 | // inserted after don't get the notification). |
| 41 | void AddListener(PageClickListener* listener); |
| 42 | void RemoveListener(PageClickListener* listener); |
| 43 | |
| 44 | private: |
[email protected] | 676126f7 | 2011-01-15 00:03:51 | [diff] [blame] | 45 | // RenderView::Observer implementation. |
[email protected] | 77fc9b9 | 2011-10-15 16:20:37 | [diff] [blame] | 46 | virtual void DidFinishDocumentLoad(WebKit::WebFrame* frame) OVERRIDE; |
| 47 | virtual void FrameDetached(WebKit::WebFrame* frame) OVERRIDE; |
| 48 | virtual void DidHandleMouseEvent(const WebKit::WebMouseEvent& event) OVERRIDE; |
[email protected] | 676126f7 | 2011-01-15 00:03:51 | [diff] [blame] | 49 | |
[email protected] | 6a8ddba5 | 2010-09-05 04:38:06 | [diff] [blame] | 50 | // WebKit::WebDOMEventListener implementation. |
| 51 | virtual void handleEvent(const WebKit::WebDOMEvent& event); |
| 52 | |
[email protected] | 65ec8db | 2011-11-04 14:50:44 | [diff] [blame] | 53 | // Checks to see if a text field is losing focus and inform listeners if |
| 54 | // it is. |
| 55 | void HandleTextFieldMaybeLosingFocus( |
| 56 | const WebKit::WebNode& newly_clicked_node); |
| 57 | |
[email protected] | 6a8ddba5 | 2010-09-05 04:38:06 | [diff] [blame] | 58 | // The last node that was clicked and had focus. |
| 59 | WebKit::WebNode last_node_clicked_; |
| 60 | |
[email protected] | 6a8ddba5 | 2010-09-05 04:38:06 | [diff] [blame] | 61 | // Whether the last clicked node had focused before it was clicked. |
| 62 | bool was_focused_; |
| 63 | |
| 64 | // The frames we are listening to for mouse events. |
| 65 | typedef std::vector<WebKit::WebFrame*> FrameList; |
| 66 | FrameList tracked_frames_; |
| 67 | |
| 68 | // The listener getting the actual notifications. |
| 69 | ObserverList<PageClickListener> listeners_; |
| 70 | |
| 71 | DISALLOW_COPY_AND_ASSIGN(PageClickTracker); |
| 72 | }; |
| 73 | |
| 74 | #endif // CHROME_RENDERER_PAGE_CLICK_TRACKER_H_ |