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

Side by Side Diff: chrome/browser/extensions/extension_browser_event_router.h

Issue 10696208: Move ExtensionEventRouter and related into extensions namespace (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: Fixed bug + latest master Created 8 years, 5 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
OLDNEW
(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_EXTENSION_BROWSER_EVENT_ROUTER_H_
6 #define CHROME_BROWSER_EXTENSIONS_EXTENSION_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.h"
14 #include "chrome/browser/extensions/extension_toolbar_model.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 #if defined(TOOLKIT_VIEWS)
19 #include "ui/views/focus/widget_focus_manager.h"
20 #elif defined(TOOLKIT_GTK)
21 #include "ui/base/x/active_window_watcher_x_observer.h"
22 #endif
23
24 namespace content {
25 class WebContents;
26 }
27
28 // The ExtensionBrowserEventRouter listens to Browser window & tab events
29 // and routes them to listeners inside extension process renderers.
30 // ExtensionBrowserEventRouter listens to *all* events, but will only route
31 // events from windows/tabs within a profile to extension processes in the same
32 // profile.
33 class ExtensionBrowserEventRouter : public TabStripModelObserver,
34 #if defined(TOOLKIT_VIEWS)
35 public views::WidgetFocusChangeListener,
36 #elif defined(TOOLKIT_GTK)
37 public ui::ActiveWindowWatcherXObserver,
38 #endif
39 public chrome::BrowserListObserver,
40 public content::NotificationObserver {
41 public:
42 explicit ExtensionBrowserEventRouter(Profile* profile);
43 virtual ~ExtensionBrowserEventRouter();
44
45 // Must be called once. Subsequent calls have no effect.
46 void Init();
47
48 // chrome::BrowserListObserver
49 virtual void OnBrowserAdded(Browser* browser) OVERRIDE;
50 virtual void OnBrowserRemoved(Browser* browser) OVERRIDE;
51 virtual void OnBrowserSetLastActive(Browser* browser) OVERRIDE;
52
53 #if defined(TOOLKIT_VIEWS)
54 virtual void OnNativeFocusChange(gfx::NativeView focused_before,
55 gfx::NativeView focused_now) OVERRIDE;
56 #elif defined(TOOLKIT_GTK)
57 virtual void ActiveWindowChanged(GdkWindow* active_window) OVERRIDE;
58 #endif
59
60 // Called from Observe() on BROWSER_WINDOW_READY (not a part of
61 // chrome::BrowserListObserver).
62 void OnBrowserWindowReady(Browser* browser);
63
64 // TabStripModelObserver
65 virtual void TabInsertedAt(TabContents* contents, int index,
66 bool active) OVERRIDE;
67 virtual void TabClosingAt(TabStripModel* tab_strip_model,
68 TabContents* contents,
69 int index) OVERRIDE;
70 virtual void TabDetachedAt(TabContents* contents, int index) OVERRIDE;
71 virtual void ActiveTabChanged(TabContents* old_contents,
72 TabContents* new_contents,
73 int index,
74 bool user_gesture) OVERRIDE;
75 virtual void TabSelectionChanged(
76 TabStripModel* tab_strip_model,
77 const TabStripSelectionModel& old_model) OVERRIDE;
78 virtual void TabMoved(TabContents* contents, int from_index,
79 int to_index) OVERRIDE;
80 virtual void TabChangedAt(TabContents* contents, int index,
81 TabChangeType change_type) OVERRIDE;
82 virtual void TabReplacedAt(TabStripModel* tab_strip_model,
83 TabContents* old_contents,
84 TabContents* new_contents,
85 int index) OVERRIDE;
86 virtual void TabPinnedStateChanged(TabContents* contents,
87 int index) OVERRIDE;
88 virtual void TabStripEmpty() OVERRIDE;
89
90 // Fires the onClicked event for page_action.
91 void PageActionExecuted(Profile* profile,
92 const ExtensionAction& page_action,
93 int tab_id,
94 const std::string& url,
95 int button);
96
97 // Fires the onClicked event for script_badge.
98 void ScriptBadgeExecuted(Profile* profile,
99 const ExtensionAction& script_badge,
100 int tab_id);
101
102 // Fires the onClicked event for browser_action.
103 void BrowserActionExecuted(const ExtensionAction& browser_action,
104 Browser* browser);
105
106 // A keyboard shortcut resulted in an extension command.
107 void CommandExecuted(Profile* profile,
108 const std::string& extension_id,
109 const std::string& command);
110
111 // content::NotificationObserver.
112 virtual void Observe(int type,
113 const content::NotificationSource& source,
114 const content::NotificationDetails& details) OVERRIDE;
115 private:
116 // "Synthetic" event. Called from TabInsertedAt if new tab is detected.
117 void TabCreatedAt(content::WebContents* contents, int index, bool active);
118
119 // Internal processing of tab updated events. Is called by both TabChangedAt
120 // and Observe/NAV_ENTRY_COMMITTED.
121 void TabUpdated(content::WebContents* contents, bool did_navigate);
122
123 // The DispatchEvent methods forward events to the |profile|'s event router.
124 // The ExtensionBrowserEventRouter listens to events for all profiles,
125 // so we avoid duplication by dropping events destined for other profiles.
126 void DispatchEvent(Profile* profile,
127 const char* event_name,
128 const std::string& json_args);
129
130 void DispatchEventToExtension(Profile* profile,
131 const std::string& extension_id,
132 const char* event_name,
133 const std::string& json_args);
134
135 void DispatchEventsAcrossIncognito(Profile* profile,
136 const char* event_name,
137 const std::string& json_args,
138 const std::string& cross_incognito_args);
139
140 void DispatchEventWithTab(Profile* profile,
141 const std::string& extension_id,
142 const char* event_name,
143 const content::WebContents* web_contents,
144 bool active);
145
146 void DispatchSimpleBrowserEvent(Profile* profile,
147 const int window_id,
148 const char* event_name);
149
150 // Packages |changed_properties| as a tab updated event for the tab |contents|
151 // and dispatches the event to the extension.
152 void DispatchTabUpdatedEvent(content::WebContents* contents,
153 DictionaryValue* changed_properties);
154
155 // Called to dispatch a deprecated style page action click event that was
156 // registered like:
157 // chrome.pageActions["name"].addListener(function(actionId, info){})
158 void DispatchOldPageActionEvent(Profile* profile,
159 const std::string& extension_id,
160 const std::string& page_action_id,
161 int tab_id,
162 const std::string& url,
163 int button);
164
165 // Register ourselves to receive the various notifications we are interested
166 // in for a browser.
167 void RegisterForBrowserNotifications(Browser* browser);
168
169 // Register ourselves to receive the various notifications we are interested
170 // in for a tab.
171 void RegisterForTabNotifications(content::WebContents* contents);
172
173 // Removes notifications added in RegisterForTabNotifications.
174 void UnregisterForTabNotifications(content::WebContents* contents);
175
176 content::NotificationRegistrar registrar_;
177
178 bool initialized_;
179
180 // Maintain some information about known tabs, so we can:
181 //
182 // - distinguish between tab creation and tab insertion
183 // - not send tab-detached after tab-removed
184 // - reduce the "noise" of TabChangedAt() when sending events to extensions
185 class TabEntry {
186 public:
187 // Create a new tab entry whose initial state is TAB_COMPLETE. This
188 // constructor is required because TabEntry objects placed inside an
189 // std::map<> by value.
190 TabEntry();
191
192 // Update the load state of the tab based on its WebContents. Returns true
193 // if the state changed, false otherwise. Whether the state has changed or
194 // not is used to determine if events needs to be sent to extensions during
195 // processing of TabChangedAt(). This method will "hold" a state-change
196 // to "loading", until the DidNavigate() method which should always follow
197 // it. Returns NULL if no updates should be sent.
198 DictionaryValue* UpdateLoadState(const content::WebContents* contents);
199
200 // Indicates that a tab load has resulted in a navigation and the
201 // destination url is available for inspection. Returns NULL if no updates
202 // should be sent.
203 DictionaryValue* DidNavigate(const content::WebContents* contents);
204
205 private:
206 // Whether we are waiting to fire the 'complete' status change. This will
207 // occur the first time the WebContents stops loading after the
208 // NAV_ENTRY_COMMITTED was fired. The tab may go back into and out of the
209 // loading state subsequently, but we will ignore those changes.
210 bool complete_waiting_on_load_;
211
212 GURL url_;
213 };
214
215 // Gets the TabEntry for the given |contents|. Returns TabEntry* if
216 // found, NULL if not.
217 TabEntry* GetTabEntry(const content::WebContents* contents);
218
219 // Called when either a browser or page action is executed. Figures out which
220 // event to send based on what the extension wants.
221 void ExtensionActionExecuted(Profile* profile,
222 const ExtensionAction& extension_action,
223 TabContents* tab_contents);
224
225 std::map<int, TabEntry> tab_entries_;
226
227 // The main profile that owns this event router.
228 Profile* profile_;
229
230 // The profile the currently focused window belongs to; either the main or
231 // incognito profile or NULL (none of the above). We remember this in order
232 // to correctly handle focus changes between non-OTR and OTR windows.
233 Profile* focused_profile_;
234
235 // The currently focused window. We keep this so as to avoid sending multiple
236 // windows.onFocusChanged events with the same windowId.
237 int focused_window_id_;
238
239 DISALLOW_COPY_AND_ASSIGN(ExtensionBrowserEventRouter);
240 };
241
242 #endif // CHROME_BROWSER_EXTENSIONS_EXTENSION_BROWSER_EVENT_ROUTER_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698