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

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

Issue 10514013: Filtered events. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: rebase, reland 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 | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 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 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 #ifndef CHROME_BROWSER_EXTENSIONS_EXTENSION_EVENT_ROUTER_H_ 5 #ifndef CHROME_BROWSER_EXTENSIONS_EXTENSION_EVENT_ROUTER_H_
6 #define CHROME_BROWSER_EXTENSIONS_EXTENSION_EVENT_ROUTER_H_ 6 #define CHROME_BROWSER_EXTENSIONS_EXTENSION_EVENT_ROUTER_H_
7 #pragma once 7 #pragma once
8 8
9 #include <map> 9 #include <map>
10 #include <set> 10 #include <set>
11 #include <string> 11 #include <string>
12 12
13 #include "base/compiler_specific.h" 13 #include "base/compiler_specific.h"
14 #include "base/memory/linked_ptr.h" 14 #include "base/memory/linked_ptr.h"
15 #include "base/memory/ref_counted.h" 15 #include "base/memory/ref_counted.h"
16 #include "base/values.h" 16 #include "base/values.h"
17 #include "chrome/browser/extensions/event_listener_map.h"
18 #include "chrome/common/extensions/event_filtering_info.h"
17 #include "content/public/browser/notification_observer.h" 19 #include "content/public/browser/notification_observer.h"
18 #include "content/public/browser/notification_registrar.h" 20 #include "content/public/browser/notification_registrar.h"
19 #include "ipc/ipc_sender.h" 21 #include "ipc/ipc_sender.h"
20 22
21 class GURL; 23 class GURL;
22 class ExtensionHost; 24 class ExtensionHost;
23 class ExtensionDevToolsManager; 25 class ExtensionDevToolsManager;
24 class Profile; 26 class Profile;
25 27
26 namespace content { 28 namespace content {
27 class RenderProcessHost; 29 class RenderProcessHost;
28 } 30 }
29 31
30 namespace extensions { 32 namespace extensions {
31 class Extension; 33 class Extension;
32 } 34 }
33 35
34 class ExtensionEventRouter : public content::NotificationObserver { 36 struct ExtensionEvent;
37
38 using extensions::EventFilteringInfo;
39 using extensions::EventListener;
40 using extensions::EventListenerMap;
41
42 class ExtensionEventRouter : public content::NotificationObserver,
43 public extensions::EventListenerMap::Delegate {
35 public: 44 public:
36 // These constants convey the state of our knowledge of whether we're in 45 // These constants convey the state of our knowledge of whether we're in
37 // a user-caused gesture as part of DispatchEvent. 46 // a user-caused gesture as part of DispatchEvent.
38 enum UserGestureState { 47 enum UserGestureState {
39 USER_GESTURE_UNKNOWN = 0, 48 USER_GESTURE_UNKNOWN = 0,
40 USER_GESTURE_ENABLED = 1, 49 USER_GESTURE_ENABLED = 1,
41 USER_GESTURE_NOT_ENABLED = 2, 50 USER_GESTURE_NOT_ENABLED = 2,
42 }; 51 };
43 52
44 // Sends an event via ipc_sender to the given extension. Can be called on 53 // Sends an event via ipc_sender to the given extension. Can be called on
45 // any thread. 54 // any thread.
46 static void DispatchEvent(IPC::Sender* ipc_sender, 55 static void DispatchEvent(IPC::Sender* ipc_sender,
47 const std::string& extension_id, 56 const std::string& extension_id,
48 const std::string& event_name, 57 const std::string& event_name,
49 const base::Value& event_args, 58 const base::Value& event_args,
50 const GURL& event_url, 59 const GURL& event_url,
51 UserGestureState user_gesture); 60 UserGestureState user_gesture,
61 const extensions::EventFilteringInfo& info);
52 62
53 // This invocation is deprecated. All future consumers of this API should be 63 // This invocation is deprecated. All future consumers of this API should be
54 // sending Values as event arguments, using the above version. 64 // sending Values as event arguments, using the above version.
55 static void DispatchEvent(IPC::Sender* ipc_sender, 65 static void DispatchEvent(IPC::Sender* ipc_sender,
56 const std::string& extension_id, 66 const std::string& extension_id,
57 const std::string& event_name, 67 const std::string& event_name,
58 const std::string& event_args, 68 const std::string& event_args,
59 const GURL& event_url, 69 const GURL& event_url,
60 UserGestureState user_gesture); 70 UserGestureState user_gesture,
71 const extensions::EventFilteringInfo& info);
61 72
62 explicit ExtensionEventRouter(Profile* profile); 73 explicit ExtensionEventRouter(Profile* profile);
63 virtual ~ExtensionEventRouter(); 74 virtual ~ExtensionEventRouter();
64 75
65 // Add or remove the process/extension pair as a listener for |event_name|. 76 // Add or remove the process/extension pair as a listener for |event_name|.
66 // Note that multiple extensions can share a process due to process 77 // Note that multiple extensions can share a process due to process
67 // collapsing. Also, a single extension can have 2 processes if it is a split 78 // collapsing. Also, a single extension can have 2 processes if it is a split
68 // mode extension. 79 // mode extension.
69 void AddEventListener(const std::string& event_name, 80 void AddEventListener(const std::string& event_name,
70 content::RenderProcessHost* process, 81 content::RenderProcessHost* process,
71 const std::string& extension_id); 82 const std::string& extension_id);
72 void RemoveEventListener(const std::string& event_name, 83 void RemoveEventListener(const std::string& event_name,
73 content::RenderProcessHost* process, 84 content::RenderProcessHost* process,
74 const std::string& extension_id); 85 const std::string& extension_id);
75 86
76 // Add or remove the extension as having a lazy background page that listens 87 // Add or remove the extension as having a lazy background page that listens
77 // to the event. The difference from the above methods is that these will be 88 // to the event. The difference from the above methods is that these will be
78 // remembered even after the process goes away. We use this list to decide 89 // remembered even after the process goes away. We use this list to decide
79 // which extension pages to load when dispatching an event. 90 // which extension pages to load when dispatching an event.
80 void AddLazyEventListener(const std::string& event_name, 91 void AddLazyEventListener(const std::string& event_name,
81 const std::string& extension_id); 92 const std::string& extension_id);
82 void RemoveLazyEventListener(const std::string& event_name, 93 void RemoveLazyEventListener(const std::string& event_name,
83 const std::string& extension_id); 94 const std::string& extension_id);
84 95
96 // If |add_lazy_listener| is true also add the lazy version of this listener.
97 void AddFilteredEventListener(const std::string& event_name,
98 content::RenderProcessHost* process,
99 const std::string& extension_id,
100 const base::DictionaryValue& filter,
101 bool add_lazy_listener);
102
103 // If |remove_lazy_listener| is true also remove the lazy version of this
104 // listener.
105 void RemoveFilteredEventListener(const std::string& event_name,
106 content::RenderProcessHost* process,
107 const std::string& extension_id,
108 const base::DictionaryValue& filter,
109 bool remove_lazy_listener);
110
85 // Returns true if there is at least one listener for the given event. 111 // Returns true if there is at least one listener for the given event.
86 bool HasEventListener(const std::string& event_name); 112 bool HasEventListener(const std::string& event_name);
87 113
88 // Returns true if the extension is listening to the given event. 114 // Returns true if the extension is listening to the given event.
89 bool ExtensionHasEventListener(const std::string& extension_id, 115 bool ExtensionHasEventListener(const std::string& extension_id,
90 const std::string& event_name); 116 const std::string& event_name);
91 117
92 // Send an event to every registered extension renderer. If 118 // Send an event to every registered extension renderer. If
93 // |restrict_to_profile| is non-NULL, then the event will not be sent to other 119 // |restrict_to_profile| is non-NULL, then the event will not be sent to other
94 // profiles unless the extension has permission (e.g. incognito tab update -> 120 // profiles unless the extension has permission (e.g. incognito tab update ->
95 // normal profile only works if extension is allowed incognito access). If 121 // normal profile only works if extension is allowed incognito access). If
96 // |event_url| is not empty, the event is only sent to extension with host 122 // |event_url| is not empty, the event is only sent to extension with host
97 // permissions for this url. 123 // permissions for this url.
98 void DispatchEventToRenderers( 124 void DispatchEventToRenderers(
99 const std::string& event_name, 125 const std::string& event_name,
100 const std::string& event_args, 126 const std::string& event_args,
101 Profile* restrict_to_profile, 127 Profile* restrict_to_profile,
128 const GURL& event_url,
129 extensions::EventFilteringInfo info);
130
131 // As above, but defaults |info| to EventFilteringInfo().
132 void DispatchEventToRenderers(
133 const std::string& event_name,
134 const std::string& event_args,
135 Profile* restrict_to_profile,
102 const GURL& event_url); 136 const GURL& event_url);
103 137
104 // Same as above, except only send the event to the given extension. 138 // Same as above, except only send the event to the given extension.
105 virtual void DispatchEventToExtension( 139 virtual void DispatchEventToExtension(
106 const std::string& extension_id, 140 const std::string& extension_id,
107 const std::string& event_name, 141 const std::string& event_name,
108 const base::Value& event_args, 142 const base::Value& event_args,
109 Profile* restrict_to_profile, 143 Profile* restrict_to_profile,
110 const GURL& event_url); 144 const GURL& event_url);
111 145
(...skipping 27 matching lines...) Expand all
139 const std::string& event_name, 173 const std::string& event_name,
140 const std::string& event_args, 174 const std::string& event_args,
141 Profile* restrict_to_profile, 175 Profile* restrict_to_profile,
142 const std::string& cross_incognito_args, 176 const std::string& cross_incognito_args,
143 const GURL& event_url); 177 const GURL& event_url);
144 178
145 // Record the Event Ack from the renderer. (One less event in-flight.) 179 // Record the Event Ack from the renderer. (One less event in-flight.)
146 void OnEventAck(Profile* profile, const std::string& extension_id); 180 void OnEventAck(Profile* profile, const std::string& extension_id);
147 181
148 private: 182 private:
149 // The details of an event to be dispatched.
150 struct ExtensionEvent;
151
152 // The extension and process that contains the event listener for a given 183 // The extension and process that contains the event listener for a given
153 // event. 184 // event.
154 struct ListenerProcess; 185 struct ListenerProcess;
155 186
156 // A map between an event name and a set of extensions that are listening 187 // A map between an event name and a set of extensions that are listening
157 // to that event. 188 // to that event.
158 typedef std::map<std::string, std::set<ListenerProcess> > ListenerMap; 189 typedef std::map<std::string, std::set<ListenerProcess> > ListenerMap;
159 190
160 virtual void Observe(int type, 191 virtual void Observe(int type,
161 const content::NotificationSource& source, 192 const content::NotificationSource& source,
162 const content::NotificationDetails& details) OVERRIDE; 193 const content::NotificationDetails& details) OVERRIDE;
163 194
164 // Returns true if the given listener map contains a event listeners for 195 // Returns true if the given listener map contains a event listeners for
165 // the given event. If |extension_id| is non-empty, we also check that that 196 // the given event. If |extension_id| is non-empty, we also check that that
166 // extension is one of the listeners. 197 // extension is one of the listeners.
167 bool HasEventListenerImpl(const ListenerMap& listeners, 198 bool HasEventListenerImpl(const ListenerMap& listeners,
168 const std::string& extension_id, 199 const std::string& extension_id,
169 const std::string& event_name); 200 const std::string& event_name);
170 201
171 // Shared by DispatchEvent*. If |extension_id| is empty, the event is 202 // Shared by DispatchEvent*. If |restrict_to_extension_id| is empty, the
172 // broadcast. If |process| is non-NULL, the event is only dispatched to that 203 // event is broadcast.
173 // particular process.
174 // An event that just came off the pending list may not be delayed again. 204 // An event that just came off the pending list may not be delayed again.
175 void DispatchEventImpl(const std::string& extension_id, 205 void DispatchEventImpl(const std::string& restrict_to_extension_id,
176 const linked_ptr<ExtensionEvent>& event); 206 const linked_ptr<ExtensionEvent>& event);
177 207
178 // Dispatches the event to a single listener process. 208 // Ensures that all lazy background pages that are interested in the given
179 void DispatchEventToListener(const ListenerProcess& listener, 209 // event are loaded, and queues the event if the page is not ready yet.
180 const linked_ptr<ExtensionEvent>& event); 210 void DispatchLazyEvent(const std::string& extension_id,
211 const linked_ptr<ExtensionEvent>& event);
212
213 // Dispatches the event to the specified extension running in |process|.
214 void DispatchEventToProcess(const std::string& extension_id,
215 content::RenderProcessHost* process,
216 const linked_ptr<ExtensionEvent>& event);
181 217
182 // Returns false when the event is scoped to a profile and the listening 218 // Returns false when the event is scoped to a profile and the listening
183 // extension does not have access to events from that profile. Also fills 219 // extension does not have access to events from that profile. Also fills
184 // |event_args| with the proper arguments to send, which may differ if 220 // |event_args| with the proper arguments to send, which may differ if
185 // the event crosses the incognito boundary. 221 // the event crosses the incognito boundary.
186 bool CanDispatchEventToProfile( 222 bool CanDispatchEventToProfile(
187 Profile* profile, 223 Profile* profile,
188 const extensions::Extension* extension, 224 const extensions::Extension* extension,
189 const linked_ptr<ExtensionEvent>& event, 225 const linked_ptr<ExtensionEvent>& event,
190 const base::Value** event_args); 226 const base::Value** event_args);
191 227
192 // Ensures that all lazy background pages that are interested in the given
193 // event are loaded, and queues the event if the page is not ready yet.
194 // If |extension_id| is non-empty, we load only that extension's page
195 // (assuming it is interested in the event).
196 void LoadLazyBackgroundPagesForEvent(
197 const std::string& extension_id,
198 const linked_ptr<ExtensionEvent>& event);
199
200 // Possibly loads given extension's background page in preparation to 228 // Possibly loads given extension's background page in preparation to
201 // dispatch an event. 229 // dispatch an event.
202 void MaybeLoadLazyBackgroundPage( 230 void MaybeLoadLazyBackgroundPageToDispatchEvent(
203 Profile* profile, 231 Profile* profile,
204 const extensions::Extension* extension, 232 const extensions::Extension* extension,
205 const linked_ptr<ExtensionEvent>& event); 233 const linked_ptr<ExtensionEvent>& event);
206 234
207 // Track of the number of dispatched events that have not yet sent an 235 // Track of the number of dispatched events that have not yet sent an
208 // ACK from the renderer. 236 // ACK from the renderer.
209 void IncrementInFlightEvents(Profile* profile, 237 void IncrementInFlightEvents(Profile* profile,
210 const extensions::Extension* extension); 238 const extensions::Extension* extension);
211 239
212 void DispatchPendingEvent(const linked_ptr<ExtensionEvent>& event, 240 void DispatchPendingEvent(const linked_ptr<ExtensionEvent>& event,
213 ExtensionHost* host); 241 ExtensionHost* host);
214 242
243 // Implementation of extensions::EventListenerMap::Delegate.
244 virtual void OnListenerAdded(const EventListener* listener) OVERRIDE;
245 virtual void OnListenerRemoved(const EventListener* listener) OVERRIDE;
246
215 Profile* profile_; 247 Profile* profile_;
216 248
217 content::NotificationRegistrar registrar_; 249 content::NotificationRegistrar registrar_;
218 250
219 scoped_refptr<ExtensionDevToolsManager> extension_devtools_manager_; 251 scoped_refptr<ExtensionDevToolsManager> extension_devtools_manager_;
220 252
221 // The list of active extension processes that are listening to events. 253 EventListenerMap listeners_;
222 ListenerMap listeners_;
223
224 // The list of all the lazy (non-persistent) background pages that are
225 // listening to events. This is just a cache of the real list, which is
226 // stored on disk in the extension prefs.
227 ListenerMap lazy_listeners_;
228 254
229 DISALLOW_COPY_AND_ASSIGN(ExtensionEventRouter); 255 DISALLOW_COPY_AND_ASSIGN(ExtensionEventRouter);
230 }; 256 };
231 257
258 struct ExtensionEvent {
259 std::string event_name;
260 scoped_ptr<Value> event_args;
261 GURL event_url;
262 Profile* restrict_to_profile;
263 scoped_ptr<Value> cross_incognito_args;
264 ExtensionEventRouter::UserGestureState user_gesture;
265 extensions::EventFilteringInfo info;
266
267 ExtensionEvent(const std::string& event_name,
268 const Value& event_args,
269 const GURL& event_url,
270 Profile* restrict_to_profile,
271 const Value& cross_incognito_args,
272 ExtensionEventRouter::UserGestureState user_gesture,
273 const extensions::EventFilteringInfo& info);
274
275 // TODO(gdk): This variant should be retired once the callers are switched to
276 // providing Values instead of just strings.
277 ExtensionEvent(const std::string& event_name,
278 const std::string& event_args,
279 const GURL& event_url,
280 Profile* restrict_to_profile,
281 const std::string& cross_incognito_args,
282 ExtensionEventRouter::UserGestureState user_gesture,
283 const extensions::EventFilteringInfo& info);
284
285 ExtensionEvent(const std::string& event_name,
286 const Value& event_args,
287 const GURL& event_url,
288 Profile* restrict_to_profile,
289 ExtensionEventRouter::UserGestureState user_gesture,
290 const extensions::EventFilteringInfo& info);
291
292 ~ExtensionEvent();
293 };
294
295
232 #endif // CHROME_BROWSER_EXTENSIONS_EXTENSION_EVENT_ROUTER_H_ 296 #endif // CHROME_BROWSER_EXTENSIONS_EXTENSION_EVENT_ROUTER_H_
OLDNEW
« no previous file with comments | « chrome/browser/extensions/extension_devtools_bridge.cc ('k') | chrome/browser/extensions/extension_event_router.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698