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 #ifndef CHROME_BROWSER_EXTENSIONS_BROWSER_EVENT_ROUTER_H_ | |
6 #define CHROME_BROWSER_EXTENSIONS_BROWSER_EVENT_ROUTER_H_ | |
7 | |
8 #include <map> | |
9 #include <string> | |
10 | |
11 #include "base/basictypes.h" | |
12 #include "base/compiler_specific.h" | |
13 #include "chrome/browser/extensions/api/tabs/tabs_api.h" | |
14 #include "chrome/browser/extensions/event_router.h" | |
15 #include "chrome/browser/ui/browser_list_observer.h" | |
16 #include "chrome/browser/ui/tabs/tab_strip_model_observer.h" | |
17 #include "content/public/browser/notification_registrar.h" | |
18 | |
19 namespace content { | |
20 class WebContents; | |
21 } | |
22 | |
23 namespace extensions { | |
24 | |
25 // The BrowserEventRouter listens to Browser window & tab events | |
26 // and routes them to listeners inside extension process renderers. | |
27 // BrowserEventRouter listens to *all* events, but will only route | |
28 // events from windows/tabs within a profile to extension processes in the same | |
29 // profile. | |
30 class BrowserEventRouter : public TabStripModelObserver, | |
31 public chrome::BrowserListObserver, | |
32 public content::NotificationObserver { | |
33 public: | |
34 explicit BrowserEventRouter(Profile* profile); | |
35 virtual ~BrowserEventRouter(); | |
36 | |
37 // chrome::BrowserListObserver | |
38 virtual void OnBrowserAdded(Browser* browser) OVERRIDE; | |
39 virtual void OnBrowserRemoved(Browser* browser) OVERRIDE; | |
40 virtual void OnBrowserSetLastActive(Browser* browser) OVERRIDE; | |
41 | |
42 // TabStripModelObserver | |
43 virtual void TabInsertedAt(content::WebContents* contents, int index, | |
44 bool active) OVERRIDE; | |
45 virtual void TabClosingAt(TabStripModel* tab_strip_model, | |
46 content::WebContents* contents, | |
47 int index) OVERRIDE; | |
48 virtual void TabDetachedAt(content::WebContents* contents, | |
49 int index) OVERRIDE; | |
50 virtual void ActiveTabChanged(content::WebContents* old_contents, | |
51 content::WebContents* new_contents, | |
52 int index, | |
53 int reason) OVERRIDE; | |
54 virtual void TabSelectionChanged( | |
55 TabStripModel* tab_strip_model, | |
56 const ui::ListSelectionModel& old_model) OVERRIDE; | |
57 virtual void TabMoved(content::WebContents* contents, | |
58 int from_index, | |
59 int to_index) OVERRIDE; | |
60 virtual void TabChangedAt(content::WebContents* contents, | |
61 int index, | |
62 TabChangeType change_type) OVERRIDE; | |
63 virtual void TabReplacedAt(TabStripModel* tab_strip_model, | |
64 content::WebContents* old_contents, | |
65 content::WebContents* new_contents, | |
66 int index) OVERRIDE; | |
67 virtual void TabPinnedStateChanged(content::WebContents* contents, | |
68 int index) OVERRIDE; | |
69 virtual void TabStripEmpty() OVERRIDE; | |
70 | |
71 // content::NotificationObserver. | |
72 virtual void Observe(int type, | |
73 const content::NotificationSource& source, | |
74 const content::NotificationDetails& details) OVERRIDE; | |
75 private: | |
76 // "Synthetic" event. Called from TabInsertedAt if new tab is detected. | |
77 void TabCreatedAt(content::WebContents* contents, int index, bool active); | |
78 | |
79 // Internal processing of tab updated events. Is called by both TabChangedAt | |
80 // and Observe/NAV_ENTRY_COMMITTED. | |
81 void TabUpdated(content::WebContents* contents, bool did_navigate); | |
82 | |
83 // Triggers a tab updated event if the favicon URL changes. | |
84 void FaviconUrlUpdated(content::WebContents* contents, | |
85 const bool* icon_url_changed); | |
86 | |
87 // The DispatchEvent methods forward events to the |profile|'s event router. | |
88 // The BrowserEventRouter listens to events for all profiles, | |
89 // so we avoid duplication by dropping events destined for other profiles. | |
90 void DispatchEvent(Profile* profile, | |
91 const char* event_name, | |
92 scoped_ptr<base::ListValue> args, | |
93 EventRouter::UserGestureState user_gesture); | |
94 | |
95 void DispatchEventsAcrossIncognito( | |
96 Profile* profile, | |
97 const char* event_name, | |
98 scoped_ptr<base::ListValue> event_args, | |
99 scoped_ptr<base::ListValue> cross_incognito_args); | |
100 | |
101 void DispatchSimpleBrowserEvent(Profile* profile, | |
102 const int window_id, | |
103 const char* event_name); | |
104 | |
105 // Packages |changed_properties| as a tab updated event for the tab |contents| | |
106 // and dispatches the event to the extension. | |
107 void DispatchTabUpdatedEvent(content::WebContents* contents, | |
108 scoped_ptr<DictionaryValue> changed_properties); | |
109 | |
110 // Register ourselves to receive the various notifications we are interested | |
111 // in for a browser. | |
112 void RegisterForBrowserNotifications(Browser* browser); | |
113 | |
114 // Register ourselves to receive the various notifications we are interested | |
115 // in for a tab. | |
116 void RegisterForTabNotifications(content::WebContents* contents); | |
117 | |
118 // Removes notifications added in RegisterForTabNotifications. | |
119 void UnregisterForTabNotifications(content::WebContents* contents); | |
120 | |
121 content::NotificationRegistrar registrar_; | |
122 | |
123 // Maintain some information about known tabs, so we can: | |
124 // | |
125 // - distinguish between tab creation and tab insertion | |
126 // - not send tab-detached after tab-removed | |
127 // - reduce the "noise" of TabChangedAt() when sending events to extensions | |
128 class TabEntry { | |
129 public: | |
130 // Create a new tab entry whose initial state is TAB_COMPLETE. This | |
131 // constructor is required because TabEntry objects placed inside an | |
132 // std::map<> by value. | |
133 TabEntry(); | |
134 | |
135 // Update the load state of the tab based on its WebContents. Returns true | |
136 // if the state changed, false otherwise. Whether the state has changed or | |
137 // not is used to determine if events needs to be sent to extensions during | |
138 // processing of TabChangedAt(). This method will "hold" a state-change | |
139 // to "loading", until the DidNavigate() method which should always follow | |
140 // it. Returns NULL if no updates should be sent. | |
141 DictionaryValue* UpdateLoadState(const content::WebContents* contents); | |
142 | |
143 // Indicates that a tab load has resulted in a navigation and the | |
144 // destination url is available for inspection. Returns NULL if no updates | |
145 // should be sent. | |
146 DictionaryValue* DidNavigate(const content::WebContents* contents); | |
147 | |
148 private: | |
149 // Whether we are waiting to fire the 'complete' status change. This will | |
150 // occur the first time the WebContents stops loading after the | |
151 // NAV_ENTRY_COMMITTED was fired. The tab may go back into and out of the | |
152 // loading state subsequently, but we will ignore those changes. | |
153 bool complete_waiting_on_load_; | |
154 | |
155 GURL url_; | |
156 }; | |
157 | |
158 // Gets the TabEntry for the given |contents|. Returns TabEntry* if | |
159 // found, NULL if not. | |
160 TabEntry* GetTabEntry(const content::WebContents* contents); | |
161 | |
162 std::map<int, TabEntry> tab_entries_; | |
163 | |
164 // The main profile that owns this event router. | |
165 Profile* profile_; | |
166 | |
167 DISALLOW_COPY_AND_ASSIGN(BrowserEventRouter); | |
168 }; | |
169 | |
170 } // namespace extensions | |
171 | |
172 #endif // CHROME_BROWSER_EXTENSIONS_BROWSER_EVENT_ROUTER_H_ | |
OLD | NEW |