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

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

Issue 10566018: Initial work to remove the notion of all extension message payloads as strings. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Review changes. Created 8 years, 6 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 #include "chrome/browser/extensions/extension_event_router.h" 5 #include "chrome/browser/extensions/extension_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"
(...skipping 11 matching lines...) Expand all
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 content::BrowserThread; 33 using content::BrowserThread;
33 using extensions::Extension; 34 using extensions::Extension;
34 using extensions::ExtensionAPI; 35 using extensions::ExtensionAPI;
35 36
36 namespace { 37 namespace {
37 38
38 const char kDispatchEvent[] = "Event.dispatchJSON"; 39 const char kDispatchEvent[] = "Event.dispatchJSON";
39 40
40 void NotifyEventListenerRemovedOnIOThread( 41 void NotifyEventListenerRemovedOnIOThread(
41 void* profile, 42 void* profile,
(...skipping 17 matching lines...) Expand all
59 if (process < that.process) 60 if (process < that.process)
60 return true; 61 return true;
61 if (process == that.process && extension_id < that.extension_id) 62 if (process == that.process && extension_id < that.extension_id)
62 return true; 63 return true;
63 return false; 64 return false;
64 } 65 }
65 }; 66 };
66 67
67 struct ExtensionEventRouter::ExtensionEvent { 68 struct ExtensionEventRouter::ExtensionEvent {
68 std::string event_name; 69 std::string event_name;
69 std::string event_args; 70 scoped_ptr<Value> event_args;
70 GURL event_url; 71 GURL event_url;
71 Profile* restrict_to_profile; 72 Profile* restrict_to_profile;
72 std::string cross_incognito_args; 73 scoped_ptr<Value> cross_incognito_args;
73 UserGestureState user_gesture; 74 UserGestureState user_gesture;
74 75
75 ExtensionEvent(const std::string& event_name, 76 ExtensionEvent(const std::string& event_name,
77 const Value& event_args,
78 const GURL& event_url,
79 Profile* restrict_to_profile,
80 const Value& cross_incognito_args,
81 UserGestureState user_gesture)
82 : event_name(event_name),
83 event_args(event_args.DeepCopy()),
84 event_url(event_url),
85 restrict_to_profile(restrict_to_profile),
86 cross_incognito_args(cross_incognito_args.DeepCopy()),
87 user_gesture(user_gesture) {}
88
89 ExtensionEvent(const std::string& event_name,
90 const Value& event_args,
91 const GURL& event_url,
92 Profile* restrict_to_profile,
93 UserGestureState user_gesture)
94 : event_name(event_name),
95 event_args(event_args.DeepCopy()),
96 event_url(event_url),
97 restrict_to_profile(restrict_to_profile),
98 cross_incognito_args(NULL),
99 user_gesture(user_gesture) {}
100
101 // TODO(gdk): This variant should be retired once the callers are switched to
102 // providing Values instead of just strings.
103 ExtensionEvent(const std::string& event_name,
76 const std::string& event_args, 104 const std::string& event_args,
77 const GURL& event_url, 105 const GURL& event_url,
78 Profile* restrict_to_profile, 106 Profile* restrict_to_profile,
79 const std::string& cross_incognito_args, 107 const std::string& cross_incognito_args,
80 UserGestureState user_gesture) 108 UserGestureState user_gesture)
81 : event_name(event_name), 109 : event_name(event_name),
82 event_args(event_args), 110 event_args(Value::CreateStringValue(event_args)),
83 event_url(event_url), 111 event_url(event_url),
84 restrict_to_profile(restrict_to_profile), 112 restrict_to_profile(restrict_to_profile),
85 cross_incognito_args(cross_incognito_args), 113 cross_incognito_args(Value::CreateStringValue(cross_incognito_args)),
86 user_gesture(user_gesture) {} 114 user_gesture(user_gesture) {}
87 }; 115 };
88 116
89 // static 117 // static
90 void ExtensionEventRouter::DispatchEvent(IPC::Sender* ipc_sender, 118 void ExtensionEventRouter::DispatchEvent(IPC::Sender* ipc_sender,
91 const std::string& extension_id, 119 const std::string& extension_id,
92 const std::string& event_name, 120 const std::string& event_name,
121 const Value& event_args,
122 const GURL& event_url,
123 UserGestureState user_gesture) {
124 // TODO(gdk): Reduce number of DeepCopy() calls throughout the event dispatch
125 // chain, starting by replacing the event_args with a Value*.
126 ListValue args;
127 args.Set(0, Value::CreateStringValue(event_name));
128 args.Set(1, event_args.DeepCopy());
129 ipc_sender->Send(new ExtensionMsg_MessageInvoke(MSG_ROUTING_CONTROL,
130 extension_id, kDispatchEvent, args, event_url,
131 user_gesture == USER_GESTURE_ENABLED));
132 }
133
134 // static
135 void ExtensionEventRouter::DispatchEvent(IPC::Sender* ipc_sender,
136 const std::string& extension_id,
137 const std::string& event_name,
93 const std::string& event_args, 138 const std::string& event_args,
94 const GURL& event_url, 139 const GURL& event_url,
95 UserGestureState user_gesture) { 140 UserGestureState user_gesture) {
96 ListValue args; 141 scoped_ptr<Value> event_args_value(Value::CreateStringValue(event_args));
97 args.Set(0, Value::CreateStringValue(event_name)); 142 DispatchEvent(ipc_sender, extension_id, event_name, *event_args_value.get(),
98 args.Set(1, Value::CreateStringValue(event_args)); 143 event_url, user_gesture);
99 ipc_sender->Send(new ExtensionMsg_MessageInvoke(MSG_ROUTING_CONTROL,
100 extension_id, kDispatchEvent, args, event_url,
101 user_gesture == USER_GESTURE_ENABLED));
102 } 144 }
103 145
104 ExtensionEventRouter::ExtensionEventRouter(Profile* profile) 146 ExtensionEventRouter::ExtensionEventRouter(Profile* profile)
105 : profile_(profile), 147 : profile_(profile),
106 extension_devtools_manager_( 148 extension_devtools_manager_(
107 ExtensionSystem::Get(profile)->devtools_manager()) { 149 ExtensionSystem::Get(profile)->devtools_manager()) {
108 registrar_.Add(this, content::NOTIFICATION_RENDERER_PROCESS_TERMINATED, 150 registrar_.Add(this, content::NOTIFICATION_RENDERER_PROCESS_TERMINATED,
109 content::NotificationService::AllSources()); 151 content::NotificationService::AllSources());
110 registrar_.Add(this, content::NOTIFICATION_RENDERER_PROCESS_CLOSED, 152 registrar_.Add(this, content::NOTIFICATION_RENDERER_PROCESS_CLOSED,
111 content::NotificationService::AllSources()); 153 content::NotificationService::AllSources());
(...skipping 124 matching lines...) Expand 10 before | Expand all | Expand 10 after
236 const GURL& event_url) { 278 const GURL& event_url) {
237 linked_ptr<ExtensionEvent> event( 279 linked_ptr<ExtensionEvent> event(
238 new ExtensionEvent(event_name, event_args, event_url, 280 new ExtensionEvent(event_name, event_args, event_url,
239 restrict_to_profile, "", USER_GESTURE_UNKNOWN)); 281 restrict_to_profile, "", USER_GESTURE_UNKNOWN));
240 DispatchEventImpl("", event); 282 DispatchEventImpl("", event);
241 } 283 }
242 284
243 void ExtensionEventRouter::DispatchEventToExtension( 285 void ExtensionEventRouter::DispatchEventToExtension(
244 const std::string& extension_id, 286 const std::string& extension_id,
245 const std::string& event_name, 287 const std::string& event_name,
246 const std::string& event_args, 288 const Value& event_args,
247 Profile* restrict_to_profile, 289 Profile* restrict_to_profile,
248 const GURL& event_url) { 290 const GURL& event_url) {
249 DCHECK(!extension_id.empty()); 291 DCHECK(!extension_id.empty());
250 linked_ptr<ExtensionEvent> event( 292 linked_ptr<ExtensionEvent> event(
251 new ExtensionEvent(event_name, event_args, event_url, 293 new ExtensionEvent(event_name, event_args, event_url,
252 restrict_to_profile, "", USER_GESTURE_UNKNOWN)); 294 restrict_to_profile, USER_GESTURE_UNKNOWN));
253 DispatchEventImpl(extension_id, event); 295 DispatchEventImpl(extension_id, event);
254 } 296 }
255 297
256 void ExtensionEventRouter::DispatchEventToExtension( 298 void ExtensionEventRouter::DispatchEventToExtension(
257 const std::string& extension_id, 299 const std::string& extension_id,
258 const std::string& event_name, 300 const std::string& event_name,
259 const std::string& event_args, 301 const std::string& event_args,
260 Profile* restrict_to_profile, 302 Profile* restrict_to_profile,
303 const GURL& event_url) {
304 scoped_ptr<Value> event_args_value(Value::CreateStringValue(event_args));
305 DispatchEventToExtension(extension_id, event_name, *event_args_value.get(),
306 restrict_to_profile, event_url);
307 }
308
309 void ExtensionEventRouter::DispatchEventToExtension(
310 const std::string& extension_id,
311 const std::string& event_name,
312 const std::string& event_args,
313 Profile* restrict_to_profile,
261 const GURL& event_url, 314 const GURL& event_url,
262 UserGestureState user_gesture) { 315 UserGestureState user_gesture) {
263 DCHECK(!extension_id.empty()); 316 DCHECK(!extension_id.empty());
264 linked_ptr<ExtensionEvent> event( 317 linked_ptr<ExtensionEvent> event(
265 new ExtensionEvent(event_name, event_args, event_url, 318 new ExtensionEvent(event_name, event_args, event_url,
266 restrict_to_profile, "", user_gesture)); 319 restrict_to_profile, "", user_gesture));
267 DispatchEventImpl(extension_id, event); 320 DispatchEventImpl(extension_id, event);
268 } 321 }
269 322
270 void ExtensionEventRouter::DispatchEventsToRenderersAcrossIncognito( 323 void ExtensionEventRouter::DispatchEventsToRenderersAcrossIncognito(
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after
319 listener.process->GetBrowserContext()); 372 listener.process->GetBrowserContext());
320 extensions::ProcessMap* process_map = 373 extensions::ProcessMap* process_map =
321 listener_profile->GetExtensionService()->process_map(); 374 listener_profile->GetExtensionService()->process_map();
322 // If the event is privileged, only send to extension processes. Otherwise, 375 // If the event is privileged, only send to extension processes. Otherwise,
323 // it's OK to send to normal renderers (e.g., for content scripts). 376 // it's OK to send to normal renderers (e.g., for content scripts).
324 if (ExtensionAPI::GetSharedInstance()->IsPrivileged(event->event_name) && 377 if (ExtensionAPI::GetSharedInstance()->IsPrivileged(event->event_name) &&
325 !process_map->Contains(extension->id(), listener.process->GetID())) { 378 !process_map->Contains(extension->id(), listener.process->GetID())) {
326 return; 379 return;
327 } 380 }
328 381
329 const std::string* event_args; 382 const Value* event_args = NULL;
330 if (!CanDispatchEventToProfile(listener_profile, extension, 383 if (!CanDispatchEventToProfile(listener_profile, extension,
331 event, &event_args)) 384 event, &event_args))
332 return; 385 return;
333 386
334 DispatchEvent(listener.process, listener.extension_id, 387 DispatchEvent(listener.process, listener.extension_id,
335 event->event_name, *event_args, 388 event->event_name, *event_args,
336 event->event_url, event->user_gesture); 389 event->event_url, event->user_gesture);
337 IncrementInFlightEvents(listener_profile, extension); 390 IncrementInFlightEvents(listener_profile, extension);
338 } 391 }
339 392
340 bool ExtensionEventRouter::CanDispatchEventToProfile( 393 bool ExtensionEventRouter::CanDispatchEventToProfile(
341 Profile* profile, 394 Profile* profile,
342 const Extension* extension, 395 const Extension* extension,
343 const linked_ptr<ExtensionEvent>& event, 396 const linked_ptr<ExtensionEvent>& event,
344 const std::string** event_args) { 397 const Value** event_args) {
345 *event_args = &event->event_args; 398 *event_args = event->event_args.get();
346 399
347 // Is this event from a different profile than the renderer (ie, an 400 // Is this event from a different profile than the renderer (ie, an
348 // incognito tab event sent to a normal process, or vice versa). 401 // incognito tab event sent to a normal process, or vice versa).
349 bool cross_incognito = event->restrict_to_profile && 402 bool cross_incognito = event->restrict_to_profile &&
350 profile != event->restrict_to_profile; 403 profile != event->restrict_to_profile;
351 if (cross_incognito && 404 if (cross_incognito &&
352 !profile->GetExtensionService()->CanCrossIncognito(extension)) { 405 !profile->GetExtensionService()->CanCrossIncognito(extension)) {
353 if (event->cross_incognito_args.empty()) 406 if (!event->cross_incognito_args.get())
354 return false; 407 return false;
355 // Send the event with different arguments to extensions that can't 408 // Send the event with different arguments to extensions that can't
356 // cross incognito. 409 // cross incognito.
357 *event_args = &event->cross_incognito_args; 410 *event_args = event->cross_incognito_args.get();
358 } 411 }
359 412
360 return true; 413 return true;
361 } 414 }
362 415
363 void ExtensionEventRouter::LoadLazyBackgroundPagesForEvent( 416 void ExtensionEventRouter::LoadLazyBackgroundPagesForEvent(
364 const std::string& extension_id, 417 const std::string& extension_id,
365 const linked_ptr<ExtensionEvent>& event) { 418 const linked_ptr<ExtensionEvent>& event) {
366 ExtensionService* service = profile_->GetExtensionService(); 419 ExtensionService* service = profile_->GetExtensionService();
367 420
(...skipping 20 matching lines...) Expand all
388 profile_->GetOffTheRecordProfile(), extension, event); 441 profile_->GetOffTheRecordProfile(), extension, event);
389 } 442 }
390 } 443 }
391 } 444 }
392 } 445 }
393 446
394 void ExtensionEventRouter::MaybeLoadLazyBackgroundPage( 447 void ExtensionEventRouter::MaybeLoadLazyBackgroundPage(
395 Profile* profile, 448 Profile* profile,
396 const Extension* extension, 449 const Extension* extension,
397 const linked_ptr<ExtensionEvent>& event) { 450 const linked_ptr<ExtensionEvent>& event) {
398 const std::string* event_args; 451 const Value* event_args = NULL;
399 if (!CanDispatchEventToProfile(profile, extension, event, &event_args)) 452 if (!CanDispatchEventToProfile(profile, extension, event, &event_args))
400 return; 453 return;
401 454
402 extensions::LazyBackgroundTaskQueue* queue = 455 extensions::LazyBackgroundTaskQueue* queue =
403 ExtensionSystem::Get(profile)->lazy_background_task_queue(); 456 ExtensionSystem::Get(profile)->lazy_background_task_queue();
404 if (queue->ShouldEnqueueTask(profile, extension)) { 457 if (queue->ShouldEnqueueTask(profile, extension)) {
405 queue->AddPendingTask( 458 queue->AddPendingTask(
406 profile, extension->id(), 459 profile, extension->id(),
407 base::Bind(&ExtensionEventRouter::DispatchPendingEvent, 460 base::Bind(&ExtensionEventRouter::DispatchPendingEvent,
408 base::Unretained(this), event)); 461 base::Unretained(this), event));
(...skipping 96 matching lines...) Expand 10 before | Expand all | Expand 10 after
505 MessageLoop::current()->PostTask(FROM_HERE, 558 MessageLoop::current()->PostTask(FROM_HERE,
506 base::Bind(&extensions::RuntimeEventRouter::DispatchOnInstalledEvent, 559 base::Bind(&extensions::RuntimeEventRouter::DispatchOnInstalledEvent,
507 profile_, extension->id())); 560 profile_, extension->id()));
508 break; 561 break;
509 } 562 }
510 default: 563 default:
511 NOTREACHED(); 564 NOTREACHED();
512 return; 565 return;
513 } 566 }
514 } 567 }
OLDNEW
« no previous file with comments | « chrome/browser/extensions/extension_event_router.h ('k') | chrome/renderer/resources/extensions/event.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698