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

Side by Side Diff: chrome/browser/extensions/event_router.cc

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
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 #include "chrome/browser/extensions/extension_event_router.h" 5 #include "chrome/browser/extensions/event_router.h"
6 6
7 #include "base/bind.h" 7 #include "base/bind.h"
8 #include "base/command_line.h" 8 #include "base/command_line.h"
9 #include "base/message_loop.h" 9 #include "base/message_loop.h"
10 #include "base/values.h" 10 #include "base/values.h"
11 #include "chrome/browser/extensions/api/runtime/runtime_api.h" 11 #include "chrome/browser/extensions/api/runtime/runtime_api.h"
12 #include "chrome/browser/extensions/api/web_request/web_request_api.h" 12 #include "chrome/browser/extensions/api/web_request/web_request_api.h"
13 #include "chrome/browser/extensions/extension_devtools_manager.h" 13 #include "chrome/browser/extensions/extension_devtools_manager.h"
14 #include "chrome/browser/extensions/extension_host.h" 14 #include "chrome/browser/extensions/extension_host.h"
15 #include "chrome/browser/extensions/extension_process_manager.h" 15 #include "chrome/browser/extensions/extension_process_manager.h"
16 #include "chrome/browser/extensions/extension_processes_api.h" 16 #include "chrome/browser/extensions/extension_processes_api.h"
17 #include "chrome/browser/extensions/extension_processes_api_constants.h" 17 #include "chrome/browser/extensions/extension_processes_api_constants.h"
18 #include "chrome/browser/extensions/extension_service.h" 18 #include "chrome/browser/extensions/extension_service.h"
19 #include "chrome/browser/extensions/extension_system.h" 19 #include "chrome/browser/extensions/extension_system.h"
20 #include "chrome/browser/extensions/lazy_background_task_queue.h" 20 #include "chrome/browser/extensions/lazy_background_task_queue.h"
21 #include "chrome/browser/extensions/process_map.h" 21 #include "chrome/browser/extensions/process_map.h"
22 #include "chrome/browser/profiles/profile.h" 22 #include "chrome/browser/profiles/profile.h"
23 #include "chrome/common/chrome_notification_types.h" 23 #include "chrome/common/chrome_notification_types.h"
24 #include "chrome/common/chrome_switches.h" 24 #include "chrome/common/chrome_switches.h"
25 #include "chrome/common/extensions/extension.h" 25 #include "chrome/common/extensions/extension.h"
26 #include "chrome/common/extensions/extension_messages.h" 26 #include "chrome/common/extensions/extension_messages.h"
27 #include "chrome/common/extensions/api/extension_api.h" 27 #include "chrome/common/extensions/api/extension_api.h"
28 #include "chrome/common/view_type.h" 28 #include "chrome/common/view_type.h"
29 #include "content/public/browser/notification_service.h" 29 #include "content/public/browser/notification_service.h"
30 #include "content/public/browser/render_process_host.h" 30 #include "content/public/browser/render_process_host.h"
31 31
32 using base::Value; 32 using base::Value;
33 using content::BrowserThread; 33 using content::BrowserThread;
34 using extensions::Extension;
35 using extensions::ExtensionAPI;
36 34
37 namespace { 35 namespace {
38 36
39 const char kDispatchEvent[] = "Event.dispatchJSON"; 37 const char kDispatchEvent[] = "Event.dispatchJSON";
40 38
41 void NotifyEventListenerRemovedOnIOThread( 39 void NotifyEventListenerRemovedOnIOThread(
42 void* profile, 40 void* profile,
43 const std::string& extension_id, 41 const std::string& extension_id,
44 const std::string& sub_event_name) { 42 const std::string& sub_event_name) {
45 ExtensionWebRequestEventRouter::GetInstance()->RemoveEventListener( 43 ExtensionWebRequestEventRouter::GetInstance()->RemoveEventListener(
46 profile, extension_id, sub_event_name); 44 profile, extension_id, sub_event_name);
47 } 45 }
48 46
49 } // namespace 47 } // namespace
50 48
51 struct ExtensionEventRouter::ListenerProcess { 49 namespace extensions {
50
51 struct EventRouter::ListenerProcess {
52 content::RenderProcessHost* process; 52 content::RenderProcessHost* process;
53 std::string extension_id; 53 std::string extension_id;
54 54
55 ListenerProcess(content::RenderProcessHost* process, 55 ListenerProcess(content::RenderProcessHost* process,
56 const std::string& extension_id) 56 const std::string& extension_id)
57 : process(process), extension_id(extension_id) {} 57 : process(process), extension_id(extension_id) {}
58 58
59 bool operator<(const ListenerProcess& that) const { 59 bool operator<(const ListenerProcess& that) const {
60 if (process < that.process) 60 if (process < that.process)
61 return true; 61 return true;
62 if (process == that.process && extension_id < that.extension_id) 62 if (process == that.process && extension_id < that.extension_id)
63 return true; 63 return true;
64 return false; 64 return false;
65 } 65 }
66 }; 66 };
67 67
68 // static 68 // static
69 void ExtensionEventRouter::DispatchEvent( 69 void EventRouter::DispatchEvent(IPC::Sender* ipc_sender,
70 IPC::Sender* ipc_sender, 70 const std::string& extension_id,
71 const std::string& extension_id, 71 const std::string& event_name,
72 const std::string& event_name, 72 const Value& event_args,
73 const Value& event_args, 73 const GURL& event_url,
74 const GURL& event_url, 74 UserGestureState user_gesture,
75 UserGestureState user_gesture, 75 const EventFilteringInfo& info) {
76 const extensions::EventFilteringInfo& info) {
77 // TODO(gdk): Reduce number of DeepCopy() calls throughout the event dispatch 76 // TODO(gdk): Reduce number of DeepCopy() calls throughout the event dispatch
78 // chain, starting by replacing the event_args with a Value*. 77 // chain, starting by replacing the event_args with a Value*.
79 ListValue args; 78 ListValue args;
80 args.Set(0, Value::CreateStringValue(event_name)); 79 args.Set(0, Value::CreateStringValue(event_name));
81 args.Set(1, event_args.DeepCopy()); 80 args.Set(1, event_args.DeepCopy());
82 args.Set(2, info.AsValue().release()); 81 args.Set(2, info.AsValue().release());
83 82
84 ipc_sender->Send(new ExtensionMsg_MessageInvoke(MSG_ROUTING_CONTROL, 83 ipc_sender->Send(new ExtensionMsg_MessageInvoke(MSG_ROUTING_CONTROL,
85 extension_id, kDispatchEvent, args, event_url, 84 extension_id, kDispatchEvent, args, event_url,
86 user_gesture == USER_GESTURE_ENABLED)); 85 user_gesture == USER_GESTURE_ENABLED));
87 } 86 }
88 87
89 // static 88 // static
90 void ExtensionEventRouter::DispatchEvent( 89 void EventRouter::DispatchEvent(IPC::Sender* ipc_sender,
91 IPC::Sender* ipc_sender, 90 const std::string& extension_id,
92 const std::string& extension_id, 91 const std::string& event_name,
93 const std::string& event_name, 92 const std::string& event_args,
94 const std::string& event_args, 93 const GURL& event_url,
95 const GURL& event_url, 94 UserGestureState user_gesture,
96 UserGestureState user_gesture, 95 const EventFilteringInfo& info) {
97 const extensions::EventFilteringInfo& info) {
98 scoped_ptr<Value> event_args_value(Value::CreateStringValue(event_args)); 96 scoped_ptr<Value> event_args_value(Value::CreateStringValue(event_args));
99 DispatchEvent(ipc_sender, extension_id, event_name, *event_args_value.get(), 97 DispatchEvent(ipc_sender, extension_id, event_name, *event_args_value.get(),
100 event_url, user_gesture, info); 98 event_url, user_gesture, info);
101 } 99 }
102 100
103 ExtensionEventRouter::ExtensionEventRouter(Profile* profile) 101 EventRouter::EventRouter(Profile* profile)
104 : profile_(profile), 102 : profile_(profile),
105 extension_devtools_manager_( 103 extension_devtools_manager_(
106 extensions::ExtensionSystem::Get(profile)->devtools_manager()), 104 ExtensionSystem::Get(profile)->devtools_manager()),
107 listeners_(ALLOW_THIS_IN_INITIALIZER_LIST(this)) { 105 listeners_(ALLOW_THIS_IN_INITIALIZER_LIST(this)) {
108 registrar_.Add(this, content::NOTIFICATION_RENDERER_PROCESS_TERMINATED, 106 registrar_.Add(this, content::NOTIFICATION_RENDERER_PROCESS_TERMINATED,
109 content::NotificationService::AllSources()); 107 content::NotificationService::AllSources());
110 registrar_.Add(this, content::NOTIFICATION_RENDERER_PROCESS_CLOSED, 108 registrar_.Add(this, content::NOTIFICATION_RENDERER_PROCESS_CLOSED,
111 content::NotificationService::AllSources()); 109 content::NotificationService::AllSources());
112 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_LOADED, 110 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_LOADED,
113 content::Source<Profile>(profile_)); 111 content::Source<Profile>(profile_));
114 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_UNLOADED, 112 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_UNLOADED,
115 content::Source<Profile>(profile_)); 113 content::Source<Profile>(profile_));
116 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_INSTALLED, 114 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_INSTALLED,
117 content::Source<Profile>(profile_)); 115 content::Source<Profile>(profile_));
118 } 116 }
119 117
120 ExtensionEventRouter::~ExtensionEventRouter() {} 118 EventRouter::~EventRouter() {}
121 119
122 void ExtensionEventRouter::AddEventListener( 120 void EventRouter::AddEventListener(const std::string& event_name,
123 const std::string& event_name, 121 content::RenderProcessHost* process,
124 content::RenderProcessHost* process, 122 const std::string& extension_id) {
125 const std::string& extension_id) {
126 listeners_.AddListener(scoped_ptr<EventListener>(new EventListener( 123 listeners_.AddListener(scoped_ptr<EventListener>(new EventListener(
127 event_name, extension_id, process, scoped_ptr<DictionaryValue>()))); 124 event_name, extension_id, process, scoped_ptr<DictionaryValue>())));
128 } 125 }
129 126
130 void ExtensionEventRouter::RemoveEventListener( 127 void EventRouter::RemoveEventListener(const std::string& event_name,
131 const std::string& event_name, 128 content::RenderProcessHost* process,
132 content::RenderProcessHost* process, 129 const std::string& extension_id) {
133 const std::string& extension_id) {
134 EventListener listener(event_name, extension_id, process, 130 EventListener listener(event_name, extension_id, process,
135 scoped_ptr<DictionaryValue>()); 131 scoped_ptr<DictionaryValue>());
136 listeners_.RemoveListener(&listener); 132 listeners_.RemoveListener(&listener);
137 } 133 }
138 134
139 void ExtensionEventRouter::OnListenerAdded(const EventListener* listener) { 135 void EventRouter::OnListenerAdded(const EventListener* listener) {
140 // We don't care about lazy events being added. 136 // We don't care about lazy events being added.
141 if (!listener->process) 137 if (!listener->process)
142 return; 138 return;
143 139
144 if (extension_devtools_manager_.get()) 140 if (extension_devtools_manager_.get())
145 extension_devtools_manager_->AddEventListener(listener->event_name, 141 extension_devtools_manager_->AddEventListener(listener->event_name,
146 listener->process->GetID()); 142 listener->process->GetID());
147 143
148 // We lazily tell the TaskManager to start updating when listeners to the 144 // We lazily tell the TaskManager to start updating when listeners to the
149 // processes.onUpdated or processes.onUpdatedWithMemory events arrive. 145 // processes.onUpdated or processes.onUpdatedWithMemory events arrive.
150 const std::string& event_name = listener->event_name; 146 const std::string& event_name = listener->event_name;
151 if (event_name.compare(extension_processes_api_constants::kOnUpdated) == 0 || 147 if (event_name.compare(extension_processes_api_constants::kOnUpdated) == 0 ||
152 event_name.compare( 148 event_name.compare(
153 extension_processes_api_constants::kOnUpdatedWithMemory) == 0) 149 extension_processes_api_constants::kOnUpdatedWithMemory) == 0)
154 ExtensionProcessesEventRouter::GetInstance()->ListenerAdded(); 150 ExtensionProcessesEventRouter::GetInstance()->ListenerAdded();
155 } 151 }
156 152
157 void ExtensionEventRouter::OnListenerRemoved(const EventListener* listener) { 153 void EventRouter::OnListenerRemoved(const EventListener* listener) {
158 // We don't care about lazy events being removed. 154 // We don't care about lazy events being removed.
159 if (!listener->process) 155 if (!listener->process)
160 return; 156 return;
161 157
162 const std::string& event_name = listener->event_name; 158 const std::string& event_name = listener->event_name;
163 if (extension_devtools_manager_.get()) 159 if (extension_devtools_manager_.get())
164 extension_devtools_manager_->RemoveEventListener( 160 extension_devtools_manager_->RemoveEventListener(
165 event_name, listener->process->GetID()); 161 event_name, listener->process->GetID());
166 162
167 // If a processes.onUpdated or processes.onUpdatedWithMemory event listener 163 // If a processes.onUpdated or processes.onUpdatedWithMemory event listener
168 // is removed (or a process with one exits), then we let the extension API 164 // is removed (or a process with one exits), then we let the extension API
169 // know that it has one fewer listener. 165 // know that it has one fewer listener.
170 if (event_name.compare(extension_processes_api_constants::kOnUpdated) == 0 || 166 if (event_name.compare(extension_processes_api_constants::kOnUpdated) == 0 ||
171 event_name.compare( 167 event_name.compare(
172 extension_processes_api_constants::kOnUpdatedWithMemory) == 0) 168 extension_processes_api_constants::kOnUpdatedWithMemory) == 0)
173 ExtensionProcessesEventRouter::GetInstance()->ListenerRemoved(); 169 ExtensionProcessesEventRouter::GetInstance()->ListenerRemoved();
174 170
175 BrowserThread::PostTask( 171 BrowserThread::PostTask(
176 BrowserThread::IO, FROM_HERE, 172 BrowserThread::IO, FROM_HERE,
177 base::Bind( 173 base::Bind(&NotifyEventListenerRemovedOnIOThread,
178 &NotifyEventListenerRemovedOnIOThread, 174 profile_, listener->extension_id, listener->event_name));
179 profile_, listener->extension_id, listener->event_name));
180 } 175 }
181 176
182 void ExtensionEventRouter::AddLazyEventListener( 177 void EventRouter::AddLazyEventListener(const std::string& event_name,
183 const std::string& event_name, 178 const std::string& extension_id) {
184 const std::string& extension_id) {
185 scoped_ptr<EventListener> listener(new EventListener( 179 scoped_ptr<EventListener> listener(new EventListener(
186 event_name, extension_id, NULL, scoped_ptr<DictionaryValue>())); 180 event_name, extension_id, NULL, scoped_ptr<DictionaryValue>()));
187 bool is_new = listeners_.AddListener(listener.Pass()); 181 bool is_new = listeners_.AddListener(listener.Pass());
188 182
189 if (is_new) { 183 if (is_new) {
190 extensions::ExtensionPrefs* prefs = 184 ExtensionPrefs* prefs =
191 profile_->GetExtensionService()->extension_prefs(); 185 profile_->GetExtensionService()->extension_prefs();
192 std::set<std::string> events = prefs->GetRegisteredEvents(extension_id); 186 std::set<std::string> events = prefs->GetRegisteredEvents(extension_id);
193 bool prefs_is_new = events.insert(event_name).second; 187 bool prefs_is_new = events.insert(event_name).second;
194 if (prefs_is_new) 188 if (prefs_is_new)
195 prefs->SetRegisteredEvents(extension_id, events); 189 prefs->SetRegisteredEvents(extension_id, events);
196 } 190 }
197 } 191 }
198 192
199 void ExtensionEventRouter::RemoveLazyEventListener( 193 void EventRouter::RemoveLazyEventListener(const std::string& event_name,
200 const std::string& event_name, 194 const std::string& extension_id) {
201 const std::string& extension_id) {
202 EventListener listener(event_name, extension_id, NULL, 195 EventListener listener(event_name, extension_id, NULL,
203 scoped_ptr<DictionaryValue>()); 196 scoped_ptr<DictionaryValue>());
204 bool did_exist = listeners_.RemoveListener(&listener); 197 bool did_exist = listeners_.RemoveListener(&listener);
205 198
206 if (did_exist) { 199 if (did_exist) {
207 extensions::ExtensionPrefs* prefs = 200 ExtensionPrefs* prefs =
208 profile_->GetExtensionService()->extension_prefs(); 201 profile_->GetExtensionService()->extension_prefs();
209 std::set<std::string> events = prefs->GetRegisteredEvents(extension_id); 202 std::set<std::string> events = prefs->GetRegisteredEvents(extension_id);
210 bool prefs_did_exist = events.erase(event_name) > 0; 203 bool prefs_did_exist = events.erase(event_name) > 0;
211 DCHECK(prefs_did_exist); 204 DCHECK(prefs_did_exist);
212 prefs->SetRegisteredEvents(extension_id, events); 205 prefs->SetRegisteredEvents(extension_id, events);
213 } 206 }
214 } 207 }
215 208
216 void ExtensionEventRouter::AddFilteredEventListener( 209 void EventRouter::AddFilteredEventListener(const std::string& event_name,
217 const std::string& event_name, 210 content::RenderProcessHost* process,
218 content::RenderProcessHost* process, 211 const std::string& extension_id,
219 const std::string& extension_id, 212 const base::DictionaryValue& filter,
220 const base::DictionaryValue& filter, 213 bool add_lazy_listener) {
221 bool add_lazy_listener) {
222 listeners_.AddListener(scoped_ptr<EventListener>(new EventListener( 214 listeners_.AddListener(scoped_ptr<EventListener>(new EventListener(
223 event_name, extension_id, process, 215 event_name, extension_id, process,
224 scoped_ptr<DictionaryValue>(filter.DeepCopy())))); 216 scoped_ptr<DictionaryValue>(filter.DeepCopy()))));
225 217
226 if (add_lazy_listener) { 218 if (add_lazy_listener) {
227 bool added = listeners_.AddListener(scoped_ptr<EventListener>( 219 bool added = listeners_.AddListener(scoped_ptr<EventListener>(
228 new EventListener(event_name, extension_id, NULL, 220 new EventListener(event_name, extension_id, NULL,
229 scoped_ptr<DictionaryValue>(filter.DeepCopy())))); 221 scoped_ptr<DictionaryValue>(filter.DeepCopy()))));
230 222
231 if (added) { 223 if (added) {
232 extensions::ExtensionPrefs* prefs = 224 ExtensionPrefs* prefs =
233 profile_->GetExtensionService()->extension_prefs(); 225 profile_->GetExtensionService()->extension_prefs();
234 prefs->AddFilterToEvent(event_name, extension_id, &filter); 226 prefs->AddFilterToEvent(event_name, extension_id, &filter);
235 } 227 }
236 } 228 }
237 } 229 }
238 230
239 void ExtensionEventRouter::RemoveFilteredEventListener( 231 void EventRouter::RemoveFilteredEventListener(
240 const std::string& event_name, 232 const std::string& event_name,
241 content::RenderProcessHost* process, 233 content::RenderProcessHost* process,
242 const std::string& extension_id, 234 const std::string& extension_id,
243 const base::DictionaryValue& filter, 235 const base::DictionaryValue& filter,
244 bool remove_lazy_listener) { 236 bool remove_lazy_listener) {
245 EventListener listener(event_name, extension_id, process, 237 EventListener listener(event_name, extension_id, process,
246 scoped_ptr<DictionaryValue>(filter.DeepCopy())); 238 scoped_ptr<DictionaryValue>(filter.DeepCopy()));
247 239
248 listeners_.RemoveListener(&listener); 240 listeners_.RemoveListener(&listener);
249 241
250 if (remove_lazy_listener) { 242 if (remove_lazy_listener) {
251 listener.process = NULL; 243 listener.process = NULL;
252 bool removed = listeners_.RemoveListener(&listener); 244 bool removed = listeners_.RemoveListener(&listener);
253 245
254 if (removed) { 246 if (removed) {
255 extensions::ExtensionPrefs* prefs = 247 ExtensionPrefs* prefs =
256 profile_->GetExtensionService()->extension_prefs(); 248 profile_->GetExtensionService()->extension_prefs();
257 prefs->RemoveFilterFromEvent(event_name, extension_id, &filter); 249 prefs->RemoveFilterFromEvent(event_name, extension_id, &filter);
258 } 250 }
259 } 251 }
260 } 252 }
261 253
262 bool ExtensionEventRouter::HasEventListener(const std::string& event_name) { 254 bool EventRouter::HasEventListener(const std::string& event_name) {
263 return listeners_.HasListenerForEvent(event_name); 255 return listeners_.HasListenerForEvent(event_name);
264 } 256 }
265 257
266 bool ExtensionEventRouter::ExtensionHasEventListener( 258 bool EventRouter::ExtensionHasEventListener(const std::string& extension_id,
267 const std::string& extension_id, const std::string& event_name) { 259 const std::string& event_name) {
268 return listeners_.HasListenerForExtension(extension_id, event_name); 260 return listeners_.HasListenerForExtension(extension_id, event_name);
269 } 261 }
270 262
271 bool ExtensionEventRouter::HasEventListenerImpl( 263 bool EventRouter::HasEventListenerImpl(const ListenerMap& listener_map,
272 const ListenerMap& listener_map, 264 const std::string& extension_id,
273 const std::string& extension_id, 265 const std::string& event_name) {
274 const std::string& event_name) {
275 ListenerMap::const_iterator it = listener_map.find(event_name); 266 ListenerMap::const_iterator it = listener_map.find(event_name);
276 if (it == listener_map.end()) 267 if (it == listener_map.end())
277 return false; 268 return false;
278 269
279 const std::set<ListenerProcess>& listeners = it->second; 270 const std::set<ListenerProcess>& listeners = it->second;
280 if (extension_id.empty()) 271 if (extension_id.empty())
281 return !listeners.empty(); 272 return !listeners.empty();
282 273
283 for (std::set<ListenerProcess>::const_iterator listener = listeners.begin(); 274 for (std::set<ListenerProcess>::const_iterator listener = listeners.begin();
284 listener != listeners.end(); ++listener) { 275 listener != listeners.end(); ++listener) {
285 if (listener->extension_id == extension_id) 276 if (listener->extension_id == extension_id)
286 return true; 277 return true;
287 } 278 }
288 return false; 279 return false;
289 } 280 }
290 281
291 void ExtensionEventRouter::DispatchEventToRenderers( 282 void EventRouter::DispatchEventToRenderers(const std::string& event_name,
292 const std::string& event_name, 283 const std::string& event_args,
293 const std::string& event_args, 284 Profile* restrict_to_profile,
294 Profile* restrict_to_profile, 285 const GURL& event_url,
295 const GURL& event_url, 286 EventFilteringInfo info) {
296 extensions::EventFilteringInfo info) {
297 DCHECK(!event_args.empty()); 287 DCHECK(!event_args.empty());
298 StringValue event_args_value(event_args); 288 StringValue event_args_value(event_args);
299 linked_ptr<ExtensionEvent> event( 289 linked_ptr<Event> event(new Event(event_name, event_args_value,
300 new ExtensionEvent(event_name, event_args_value, event_url, 290 event_url, restrict_to_profile,
301 restrict_to_profile, USER_GESTURE_UNKNOWN, info)); 291 USER_GESTURE_UNKNOWN, info));
302 DispatchEventImpl("", event); 292 DispatchEventImpl("", event);
303 } 293 }
304 294
305 void ExtensionEventRouter::DispatchEventToRenderers( 295 void EventRouter::DispatchEventToRenderers(const std::string& event_name,
306 const std::string& event_name, 296 const std::string& event_args,
307 const std::string& event_args, 297 Profile* restrict_to_profile,
308 Profile* restrict_to_profile, 298 const GURL& event_url) {
309 const GURL& event_url) {
310 DispatchEventToRenderers(event_name, event_args, restrict_to_profile, 299 DispatchEventToRenderers(event_name, event_args, restrict_to_profile,
311 event_url, extensions::EventFilteringInfo()); 300 event_url, EventFilteringInfo());
312 } 301 }
313 302
314 void ExtensionEventRouter::DispatchEventToExtension( 303 void EventRouter::DispatchEventToExtension(const std::string& extension_id,
315 const std::string& extension_id, 304 const std::string& event_name,
316 const std::string& event_name, 305 const Value& event_args,
317 const Value& event_args, 306 Profile* restrict_to_profile,
318 Profile* restrict_to_profile, 307 const GURL& event_url) {
319 const GURL& event_url) {
320 DCHECK(!extension_id.empty()); 308 DCHECK(!extension_id.empty());
321 linked_ptr<ExtensionEvent> event( 309 linked_ptr<Event> event(new Event(event_name, event_args, event_url,
322 new ExtensionEvent(event_name, event_args, event_url, 310 restrict_to_profile, USER_GESTURE_UNKNOWN,
323 restrict_to_profile, USER_GESTURE_UNKNOWN, 311 EventFilteringInfo()));
324 EventFilteringInfo()));
325 DispatchEventImpl(extension_id, event); 312 DispatchEventImpl(extension_id, event);
326 } 313 }
327 314
328 void ExtensionEventRouter::DispatchEventToExtension( 315 void EventRouter::DispatchEventToExtension(const std::string& extension_id,
329 const std::string& extension_id, 316 const std::string& event_name,
330 const std::string& event_name, 317 const std::string& event_args,
331 const std::string& event_args, 318 Profile* restrict_to_profile,
332 Profile* restrict_to_profile, 319 const GURL& event_url) {
333 const GURL& event_url) {
334 StringValue event_args_value(event_args); 320 StringValue event_args_value(event_args);
335 DispatchEventToExtension(extension_id, event_name, event_args_value, 321 DispatchEventToExtension(extension_id, event_name, event_args_value,
336 restrict_to_profile, event_url); 322 restrict_to_profile, event_url);
337 } 323 }
338 324
339 void ExtensionEventRouter::DispatchEventToExtension( 325 void EventRouter::DispatchEventToExtension(const std::string& extension_id,
340 const std::string& extension_id, 326 const std::string& event_name,
341 const std::string& event_name, 327 const std::string& event_args,
342 const std::string& event_args, 328 Profile* restrict_to_profile,
343 Profile* restrict_to_profile, 329 const GURL& event_url,
344 const GURL& event_url, 330 UserGestureState user_gesture) {
345 UserGestureState user_gesture) {
346 DCHECK(!extension_id.empty()); 331 DCHECK(!extension_id.empty());
347 StringValue event_args_value(event_args); 332 StringValue event_args_value(event_args);
348 linked_ptr<ExtensionEvent> event( 333 linked_ptr<Event> event(new Event(event_name, event_args_value, event_url,
349 new ExtensionEvent(event_name, event_args_value, event_url, 334 restrict_to_profile, user_gesture,
350 restrict_to_profile, user_gesture, 335 EventFilteringInfo()));
351 EventFilteringInfo()));
352 DispatchEventImpl(extension_id, event); 336 DispatchEventImpl(extension_id, event);
353 } 337 }
354 338
355 void ExtensionEventRouter::DispatchEventsToRenderersAcrossIncognito( 339 void EventRouter::DispatchEventsToRenderersAcrossIncognito(
356 const std::string& event_name, 340 const std::string& event_name,
357 const std::string& event_args, 341 const std::string& event_args,
358 Profile* restrict_to_profile, 342 Profile* restrict_to_profile,
359 const std::string& cross_incognito_args, 343 const std::string& cross_incognito_args,
360 const GURL& event_url) { 344 const GURL& event_url) {
361 linked_ptr<ExtensionEvent> event( 345 linked_ptr<Event> event(new Event(event_name, event_args,
362 new ExtensionEvent(event_name, event_args, event_url, 346 event_url, restrict_to_profile,
363 restrict_to_profile, cross_incognito_args, 347 cross_incognito_args, USER_GESTURE_UNKNOWN,
364 USER_GESTURE_UNKNOWN, EventFilteringInfo())); 348 EventFilteringInfo()));
365 DispatchEventImpl("", event); 349 DispatchEventImpl("", event);
366 } 350 }
367 351
368 void ExtensionEventRouter::DispatchEventImpl( 352 void EventRouter::DispatchEventImpl(const std::string& restrict_to_extension_id,
369 const std::string& restrict_to_extension_id, 353 const linked_ptr<Event>& event) {
370 const linked_ptr<ExtensionEvent>& event) {
371 // We don't expect to get events from a completely different profile. 354 // We don't expect to get events from a completely different profile.
372 DCHECK(!event->restrict_to_profile || 355 DCHECK(!event->restrict_to_profile ||
373 profile_->IsSameProfile(event->restrict_to_profile)); 356 profile_->IsSameProfile(event->restrict_to_profile));
374 357
375 std::set<const EventListener*> listeners( 358 std::set<const EventListener*> listeners(
376 listeners_.GetEventListeners(*event)); 359 listeners_.GetEventListeners(*event));
377 for (std::set<const EventListener*>::iterator it = listeners.begin(); 360 for (std::set<const EventListener*>::iterator it = listeners.begin();
378 it != listeners.end(); it++) { 361 it != listeners.end(); it++) {
379 const EventListener* listener = *it; 362 const EventListener* listener = *it;
380 if (restrict_to_extension_id.empty() || 363 if (restrict_to_extension_id.empty() ||
381 restrict_to_extension_id == listener->extension_id) { 364 restrict_to_extension_id == listener->extension_id) {
382 if (listener->process) { 365 if (listener->process) {
383 DispatchEventToProcess(listener->extension_id, listener->process, 366 DispatchEventToProcess(listener->extension_id, listener->process,
384 event); 367 event);
385 } else { 368 } else {
386 DispatchLazyEvent(listener->extension_id, event); 369 DispatchLazyEvent(listener->extension_id, event);
387 } 370 }
388 } 371 }
389 } 372 }
390 } 373 }
391 374
392 void ExtensionEventRouter::DispatchLazyEvent( 375 void EventRouter::DispatchLazyEvent(const std::string& extension_id,
393 const std::string& extension_id, 376 const linked_ptr<Event>& event) {
394 const linked_ptr<ExtensionEvent>& event) {
395 ExtensionService* service = profile_->GetExtensionService(); 377 ExtensionService* service = profile_->GetExtensionService();
396 // Check both the original and the incognito profile to see if we 378 // Check both the original and the incognito profile to see if we
397 // should load a lazy bg page to handle the event. The latter case 379 // should load a lazy bg page to handle the event. The latter case
398 // occurs in the case of split-mode extensions. 380 // occurs in the case of split-mode extensions.
399 const Extension* extension = service->extensions()->GetByID(extension_id); 381 const Extension* extension = service->extensions()->GetByID(extension_id);
400 if (extension) { 382 if (extension) {
401 MaybeLoadLazyBackgroundPageToDispatchEvent(profile_, extension, event); 383 MaybeLoadLazyBackgroundPageToDispatchEvent(profile_, extension, event);
402 if (profile_->HasOffTheRecordProfile() && 384 if (profile_->HasOffTheRecordProfile() &&
403 extension->incognito_split_mode()) { 385 extension->incognito_split_mode()) {
404 MaybeLoadLazyBackgroundPageToDispatchEvent( 386 MaybeLoadLazyBackgroundPageToDispatchEvent(
405 profile_->GetOffTheRecordProfile(), extension, event); 387 profile_->GetOffTheRecordProfile(), extension, event);
406 } 388 }
407 } 389 }
408 } 390 }
409 391
410 void ExtensionEventRouter::DispatchEventToProcess( 392 void EventRouter::DispatchEventToProcess(const std::string& extension_id,
411 const std::string& extension_id, 393 content::RenderProcessHost* process,
412 content::RenderProcessHost* process, 394 const linked_ptr<Event>& event) {
413 const linked_ptr<ExtensionEvent>& event) {
414 ExtensionService* service = profile_->GetExtensionService(); 395 ExtensionService* service = profile_->GetExtensionService();
415 const Extension* extension = service->extensions()->GetByID(extension_id); 396 const Extension* extension = service->extensions()->GetByID(extension_id);
416 397
417 // The extension could have been removed, but we do not unregister it until 398 // The extension could have been removed, but we do not unregister it until
418 // the extension process is unloaded. 399 // the extension process is unloaded.
419 if (!extension) 400 if (!extension)
420 return; 401 return;
421 402
422 Profile* listener_profile = Profile::FromBrowserContext( 403 Profile* listener_profile = Profile::FromBrowserContext(
423 process->GetBrowserContext()); 404 process->GetBrowserContext());
424 extensions::ProcessMap* process_map = 405 ProcessMap* process_map =
425 listener_profile->GetExtensionService()->process_map(); 406 listener_profile->GetExtensionService()->process_map();
426 // If the event is privileged, only send to extension processes. Otherwise, 407 // If the event is privileged, only send to extension processes. Otherwise,
427 // it's OK to send to normal renderers (e.g., for content scripts). 408 // it's OK to send to normal renderers (e.g., for content scripts).
428 if (ExtensionAPI::GetSharedInstance()->IsPrivileged(event->event_name) && 409 if (ExtensionAPI::GetSharedInstance()->IsPrivileged(event->event_name) &&
429 !process_map->Contains(extension->id(), process->GetID())) { 410 !process_map->Contains(extension->id(), process->GetID())) {
430 return; 411 return;
431 } 412 }
432 413
433 const Value* event_args = NULL; 414 const Value* event_args = NULL;
434 if (!CanDispatchEventToProfile(listener_profile, extension, 415 if (!CanDispatchEventToProfile(listener_profile, extension,
435 event, &event_args)) { 416 event, &event_args)) {
436 return; 417 return;
437 } 418 }
438 419
439 DispatchEvent(process, extension_id, 420 DispatchEvent(process, extension_id,
440 event->event_name, *event_args, 421 event->event_name, *event_args,
441 event->event_url, event->user_gesture, 422 event->event_url, event->user_gesture,
442 event->info); 423 event->info);
443 IncrementInFlightEvents(listener_profile, extension); 424 IncrementInFlightEvents(listener_profile, extension);
444 } 425 }
445 426
446 bool ExtensionEventRouter::CanDispatchEventToProfile( 427 bool EventRouter::CanDispatchEventToProfile(Profile* profile,
447 Profile* profile, 428 const Extension* extension,
448 const Extension* extension, 429 const linked_ptr<Event>& event,
449 const linked_ptr<ExtensionEvent>& event, 430 const Value** event_args) {
450 const Value** event_args) {
451 *event_args = event->event_args.get(); 431 *event_args = event->event_args.get();
452 432
453 // Is this event from a different profile than the renderer (ie, an 433 // Is this event from a different profile than the renderer (ie, an
454 // incognito tab event sent to a normal process, or vice versa). 434 // incognito tab event sent to a normal process, or vice versa).
455 bool cross_incognito = event->restrict_to_profile && 435 bool cross_incognito =
456 profile != event->restrict_to_profile; 436 event->restrict_to_profile && profile != event->restrict_to_profile;
457 if (cross_incognito && 437 if (cross_incognito &&
458 !profile->GetExtensionService()->CanCrossIncognito(extension)) { 438 !profile->GetExtensionService()->CanCrossIncognito(extension)) {
459 if (!event->cross_incognito_args.get()) 439 if (!event->cross_incognito_args.get())
460 return false; 440 return false;
461 // Send the event with different arguments to extensions that can't 441 // Send the event with different arguments to extensions that can't
462 // cross incognito. 442 // cross incognito.
463 *event_args = event->cross_incognito_args.get(); 443 *event_args = event->cross_incognito_args.get();
464 } 444 }
465 445
466 return true; 446 return true;
467 } 447 }
468 448
469 void ExtensionEventRouter::MaybeLoadLazyBackgroundPageToDispatchEvent( 449 void EventRouter::MaybeLoadLazyBackgroundPageToDispatchEvent(
470 Profile* profile, 450 Profile* profile,
471 const Extension* extension, 451 const Extension* extension,
472 const linked_ptr<ExtensionEvent>& event) { 452 const linked_ptr<Event>& event) {
473 const Value* event_args = NULL; 453 const Value* event_args = NULL;
474 if (!CanDispatchEventToProfile(profile, extension, event, &event_args)) 454 if (!CanDispatchEventToProfile(profile, extension, event, &event_args))
475 return; 455 return;
476 456
477 extensions::LazyBackgroundTaskQueue* queue = 457 LazyBackgroundTaskQueue* queue =
478 extensions::ExtensionSystem::Get(profile)->lazy_background_task_queue(); 458 ExtensionSystem::Get(profile)->lazy_background_task_queue();
479 if (queue->ShouldEnqueueTask(profile, extension)) { 459 if (queue->ShouldEnqueueTask(profile, extension)) {
480 queue->AddPendingTask( 460 queue->AddPendingTask(profile, extension->id(),
481 profile, extension->id(), 461 base::Bind(&EventRouter::DispatchPendingEvent,
482 base::Bind(&ExtensionEventRouter::DispatchPendingEvent, 462 base::Unretained(this), event));
483 base::Unretained(this), event));
484 } 463 }
485 } 464 }
486 465
487 void ExtensionEventRouter::IncrementInFlightEvents( 466 void EventRouter::IncrementInFlightEvents(Profile* profile,
488 Profile* profile, const Extension* extension) { 467 const Extension* extension) {
489 // Only increment in-flight events if the lazy background page is active, 468 // Only increment in-flight events if the lazy background page is active,
490 // because that's the only time we'll get an ACK. 469 // because that's the only time we'll get an ACK.
491 if (extension->has_lazy_background_page()) { 470 if (extension->has_lazy_background_page()) {
492 ExtensionProcessManager* pm = 471 ExtensionProcessManager* pm =
493 extensions::ExtensionSystem::Get(profile)->process_manager(); 472 ExtensionSystem::Get(profile)->process_manager();
494 ExtensionHost* host = pm->GetBackgroundHostForExtension(extension->id()); 473 ExtensionHost* host = pm->GetBackgroundHostForExtension(extension->id());
495 if (host) 474 if (host)
496 pm->IncrementLazyKeepaliveCount(extension); 475 pm->IncrementLazyKeepaliveCount(extension);
497 } 476 }
498 } 477 }
499 478
500 void ExtensionEventRouter::OnEventAck( 479 void EventRouter::OnEventAck(Profile* profile,
501 Profile* profile, const std::string& extension_id) { 480 const std::string& extension_id) {
502 ExtensionProcessManager* pm = 481 ExtensionProcessManager* pm =
503 extensions::ExtensionSystem::Get(profile)->process_manager(); 482 ExtensionSystem::Get(profile)->process_manager();
504 ExtensionHost* host = pm->GetBackgroundHostForExtension(extension_id); 483 ExtensionHost* host = pm->GetBackgroundHostForExtension(extension_id);
505 // The event ACK is routed to the background host, so this should never be 484 // The event ACK is routed to the background host, so this should never be
506 // NULL. 485 // NULL.
507 CHECK(host); 486 CHECK(host);
508 // TODO(mpcomplete): We should never get this message unless 487 // TODO(mpcomplete): We should never get this message unless
509 // has_lazy_background_page is true. Find out why we're getting it anyway. 488 // has_lazy_background_page is true. Find out why we're getting it anyway.
510 if (host->extension() && host->extension()->has_lazy_background_page()) 489 if (host->extension() && host->extension()->has_lazy_background_page())
511 pm->DecrementLazyKeepaliveCount(host->extension()); 490 pm->DecrementLazyKeepaliveCount(host->extension());
512 } 491 }
513 492
514 void ExtensionEventRouter::DispatchPendingEvent( 493 void EventRouter::DispatchPendingEvent(const linked_ptr<Event>& event,
515 const linked_ptr<ExtensionEvent>& event, 494 ExtensionHost* host) {
516 ExtensionHost* host) {
517 if (!host) 495 if (!host)
518 return; 496 return;
519 497
520 if (listeners_.HasProcessListener(host->render_process_host(), 498 if (listeners_.HasProcessListener(host->render_process_host(),
521 host->extension()->id())) 499 host->extension()->id()))
522 DispatchEventToProcess(host->extension()->id(), 500 DispatchEventToProcess(host->extension()->id(),
523 host->render_process_host(), event); 501 host->render_process_host(), event);
524 } 502 }
525 503
526 void ExtensionEventRouter::Observe( 504 void EventRouter::Observe(int type,
527 int type, 505 const content::NotificationSource& source,
528 const content::NotificationSource& source, 506 const content::NotificationDetails& details) {
529 const content::NotificationDetails& details) {
530 switch (type) { 507 switch (type) {
531 case content::NOTIFICATION_RENDERER_PROCESS_TERMINATED: 508 case content::NOTIFICATION_RENDERER_PROCESS_TERMINATED:
532 case content::NOTIFICATION_RENDERER_PROCESS_CLOSED: { 509 case content::NOTIFICATION_RENDERER_PROCESS_CLOSED: {
533 content::RenderProcessHost* renderer = 510 content::RenderProcessHost* renderer =
534 content::Source<content::RenderProcessHost>(source).ptr(); 511 content::Source<content::RenderProcessHost>(source).ptr();
535 // Remove all event listeners associated with this renderer. 512 // Remove all event listeners associated with this renderer.
536 listeners_.RemoveListenersForProcess(renderer); 513 listeners_.RemoveListenersForProcess(renderer);
537 break; 514 break;
538 } 515 }
539 case chrome::NOTIFICATION_EXTENSION_LOADED: { 516 case chrome::NOTIFICATION_EXTENSION_LOADED: {
540 // Add all registered lazy listeners to our cache. 517 // Add all registered lazy listeners to our cache.
541 const Extension* extension = 518 const Extension* extension =
542 content::Details<const Extension>(details).ptr(); 519 content::Details<const Extension>(details).ptr();
543 extensions::ExtensionPrefs* prefs = 520 ExtensionPrefs* prefs =
544 profile_->GetExtensionService()->extension_prefs(); 521 profile_->GetExtensionService()->extension_prefs();
545 std::set<std::string> registered_events = 522 std::set<std::string> registered_events =
546 prefs->GetRegisteredEvents(extension->id()); 523 prefs->GetRegisteredEvents(extension->id());
547 listeners_.LoadUnfilteredLazyListeners(extension->id(), 524 listeners_.LoadUnfilteredLazyListeners(extension->id(),
548 registered_events); 525 registered_events);
549 const DictionaryValue* filtered_events = 526 const DictionaryValue* filtered_events =
550 prefs->GetFilteredEvents(extension->id()); 527 prefs->GetFilteredEvents(extension->id());
551 if (filtered_events) 528 if (filtered_events)
552 listeners_.LoadFilteredLazyListeners(extension->id(), *filtered_events); 529 listeners_.LoadFilteredLazyListeners(extension->id(), *filtered_events);
553 break; 530 break;
554 } 531 }
555 case chrome::NOTIFICATION_EXTENSION_UNLOADED: { 532 case chrome::NOTIFICATION_EXTENSION_UNLOADED: {
556 // Remove all registered lazy listeners from our cache. 533 // Remove all registered lazy listeners from our cache.
557 extensions::UnloadedExtensionInfo* unloaded = 534 UnloadedExtensionInfo* unloaded =
558 content::Details<extensions::UnloadedExtensionInfo>(details).ptr(); 535 content::Details<UnloadedExtensionInfo>(details).ptr();
559 listeners_.RemoveLazyListenersForExtension(unloaded->extension->id()); 536 listeners_.RemoveLazyListenersForExtension(unloaded->extension->id());
560 break; 537 break;
561 } 538 }
562 case chrome::NOTIFICATION_EXTENSION_INSTALLED: { 539 case chrome::NOTIFICATION_EXTENSION_INSTALLED: {
563 // Dispatch the onInstalled event. 540 // Dispatch the onInstalled event.
564 const Extension* extension = 541 const Extension* extension =
565 content::Details<const Extension>(details).ptr(); 542 content::Details<const Extension>(details).ptr();
566 MessageLoop::current()->PostTask(FROM_HERE, 543 MessageLoop::current()->PostTask(FROM_HERE,
567 base::Bind(&extensions::RuntimeEventRouter::DispatchOnInstalledEvent, 544 base::Bind(&RuntimeEventRouter::DispatchOnInstalledEvent,
568 profile_, extension->id())); 545 profile_, extension->id()));
569 break; 546 break;
570 } 547 }
571 default: 548 default:
572 NOTREACHED(); 549 NOTREACHED();
573 return; 550 return;
574 } 551 }
575 } 552 }
576 553
577 ExtensionEvent::ExtensionEvent( 554 Event::Event(const std::string& event_name,
578 const std::string& event_name, 555 const Value& event_args,
579 const Value& event_args, 556 const GURL& event_url,
580 const GURL& event_url, 557 Profile* restrict_to_profile,
581 Profile* restrict_to_profile, 558 const Value& cross_incognito_args,
582 const Value& cross_incognito_args, 559 EventRouter::UserGestureState user_gesture,
583 ExtensionEventRouter::UserGestureState user_gesture, 560 const EventFilteringInfo& info)
584 const extensions::EventFilteringInfo& info)
585 : event_name(event_name), 561 : event_name(event_name),
586 event_args(event_args.DeepCopy()), 562 event_args(event_args.DeepCopy()),
587 event_url(event_url), 563 event_url(event_url),
588 restrict_to_profile(restrict_to_profile), 564 restrict_to_profile(restrict_to_profile),
589 cross_incognito_args(cross_incognito_args.DeepCopy()), 565 cross_incognito_args(cross_incognito_args.DeepCopy()),
590 user_gesture(user_gesture), 566 user_gesture(user_gesture),
591 info(info) { 567 info(info) {
592 } 568 }
593 569
594 ExtensionEvent::ExtensionEvent( 570 Event::Event(const std::string& event_name,
595 const std::string& event_name, 571 const std::string& event_args,
596 const std::string& event_args, 572 const GURL& event_url,
597 const GURL& event_url, 573 Profile* restrict_to_profile,
598 Profile* restrict_to_profile, 574 const std::string& cross_incognito_args,
599 const std::string& cross_incognito_args, 575 EventRouter::UserGestureState user_gesture,
600 ExtensionEventRouter::UserGestureState user_gesture, 576 const EventFilteringInfo& info)
601 const extensions::EventFilteringInfo& info)
602 : event_name(event_name), 577 : event_name(event_name),
603 event_args(Value::CreateStringValue(event_args)), 578 event_args(Value::CreateStringValue(event_args)),
604 event_url(event_url), 579 event_url(event_url),
605 restrict_to_profile(restrict_to_profile), 580 restrict_to_profile(restrict_to_profile),
606 cross_incognito_args(Value::CreateStringValue(cross_incognito_args)), 581 cross_incognito_args(Value::CreateStringValue(cross_incognito_args)),
607 user_gesture(user_gesture), 582 user_gesture(user_gesture),
608 info(info) { 583 info(info) {
609 } 584 }
610 585
611 ExtensionEvent::ExtensionEvent( 586 Event::Event(const std::string& event_name,
612 const std::string& event_name, 587 const Value& event_args,
613 const Value& event_args, 588 const GURL& event_url,
614 const GURL& event_url, 589 Profile* restrict_to_profile,
615 Profile* restrict_to_profile, 590 EventRouter::UserGestureState user_gesture,
616 ExtensionEventRouter::UserGestureState user_gesture, 591 const EventFilteringInfo& info)
617 const extensions::EventFilteringInfo& info)
618 : event_name(event_name), 592 : event_name(event_name),
619 event_args(event_args.DeepCopy()), 593 event_args(event_args.DeepCopy()),
620 event_url(event_url), 594 event_url(event_url),
621 restrict_to_profile(restrict_to_profile), 595 restrict_to_profile(restrict_to_profile),
622 cross_incognito_args(NULL), 596 cross_incognito_args(NULL),
623 user_gesture(user_gesture), 597 user_gesture(user_gesture),
624 info(info) { 598 info(info) {
625 } 599 }
626 600
627 ExtensionEvent::~ExtensionEvent() { 601 Event::~Event() {
628 } 602 }
603
604 } // namespace extensions
OLDNEW
« no previous file with comments | « chrome/browser/extensions/event_router.h ('k') | chrome/browser/extensions/event_router_forwarder.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698