OLD | NEW |
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 356 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
367 | 367 |
368 void ExtensionEventRouter::DispatchEventImpl( | 368 void ExtensionEventRouter::DispatchEventImpl( |
369 const std::string& restrict_to_extension_id, | 369 const std::string& restrict_to_extension_id, |
370 const linked_ptr<ExtensionEvent>& event) { | 370 const linked_ptr<ExtensionEvent>& event) { |
371 // We don't expect to get events from a completely different profile. | 371 // We don't expect to get events from a completely different profile. |
372 DCHECK(!event->restrict_to_profile || | 372 DCHECK(!event->restrict_to_profile || |
373 profile_->IsSameProfile(event->restrict_to_profile)); | 373 profile_->IsSameProfile(event->restrict_to_profile)); |
374 | 374 |
375 std::set<const EventListener*> listeners( | 375 std::set<const EventListener*> listeners( |
376 listeners_.GetEventListeners(*event)); | 376 listeners_.GetEventListeners(*event)); |
| 377 |
| 378 // We dispatch events for lazy background pages first because attempting to do |
| 379 // so will cause those that are being suspended to cancel that suspension. |
| 380 // As canceling a suspension entails sending an event to the affected |
| 381 // background page, and as that event needs to be delivered before we dispatch |
| 382 // the event we are dispatching here, we dispatch to the lazy listeners here |
| 383 // first. |
377 for (std::set<const EventListener*>::iterator it = listeners.begin(); | 384 for (std::set<const EventListener*>::iterator it = listeners.begin(); |
378 it != listeners.end(); it++) { | 385 it != listeners.end(); it++) { |
379 const EventListener* listener = *it; | 386 const EventListener* listener = *it; |
| 387 if (restrict_to_extension_id.empty() || |
| 388 restrict_to_extension_id == listener->extension_id) { |
| 389 if (!listener->process) |
| 390 DispatchLazyEvent(listener->extension_id, event); |
| 391 } |
| 392 } |
| 393 |
| 394 for (std::set<const EventListener*>::iterator it = listeners.begin(); |
| 395 it != listeners.end(); it++) { |
| 396 const EventListener* listener = *it; |
380 if (restrict_to_extension_id.empty() || | 397 if (restrict_to_extension_id.empty() || |
381 restrict_to_extension_id == listener->extension_id) { | 398 restrict_to_extension_id == listener->extension_id) { |
382 if (listener->process) { | 399 if (listener->process) { |
383 DispatchEventToProcess(listener->extension_id, listener->process, | 400 DispatchEventToProcess(listener->extension_id, listener->process, |
384 event); | 401 event); |
385 } else { | |
386 DispatchLazyEvent(listener->extension_id, event); | |
387 } | 402 } |
388 } | 403 } |
389 } | 404 } |
390 } | 405 } |
391 | 406 |
392 void ExtensionEventRouter::DispatchLazyEvent( | 407 void ExtensionEventRouter::DispatchLazyEvent( |
393 const std::string& extension_id, | 408 const std::string& extension_id, |
394 const linked_ptr<ExtensionEvent>& event) { | 409 const linked_ptr<ExtensionEvent>& event) { |
395 ExtensionService* service = profile_->GetExtensionService(); | 410 ExtensionService* service = profile_->GetExtensionService(); |
396 // Check both the original and the incognito profile to see if we | 411 // Check both the original and the incognito profile to see if we |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
429 !process_map->Contains(extension->id(), process->GetID())) { | 444 !process_map->Contains(extension->id(), process->GetID())) { |
430 return; | 445 return; |
431 } | 446 } |
432 | 447 |
433 const Value* event_args = NULL; | 448 const Value* event_args = NULL; |
434 if (!CanDispatchEventToProfile(listener_profile, extension, | 449 if (!CanDispatchEventToProfile(listener_profile, extension, |
435 event, &event_args)) { | 450 event, &event_args)) { |
436 return; | 451 return; |
437 } | 452 } |
438 | 453 |
439 // We need to call IncrementInFlightEvents() before DispatchEvent() because | |
440 // the former may prevent this extension from getting suspended and so send | |
441 // the onSuspendCanceled() to the extension, which needs to arrive before the | |
442 // event that DispatchEvent() is dispatching. | |
443 IncrementInFlightEvents(listener_profile, extension); | |
444 | |
445 DispatchEvent(process, extension_id, | 454 DispatchEvent(process, extension_id, |
446 event->event_name, *event_args, | 455 event->event_name, *event_args, |
447 event->event_url, event->user_gesture, | 456 event->event_url, event->user_gesture, |
448 event->info); | 457 event->info); |
| 458 IncrementInFlightEvents(listener_profile, extension); |
449 } | 459 } |
450 | 460 |
451 bool ExtensionEventRouter::CanDispatchEventToProfile( | 461 bool ExtensionEventRouter::CanDispatchEventToProfile( |
452 Profile* profile, | 462 Profile* profile, |
453 const Extension* extension, | 463 const Extension* extension, |
454 const linked_ptr<ExtensionEvent>& event, | 464 const linked_ptr<ExtensionEvent>& event, |
455 const Value** event_args) { | 465 const Value** event_args) { |
456 *event_args = event->event_args.get(); | 466 *event_args = event->event_args.get(); |
457 | 467 |
458 // Is this event from a different profile than the renderer (ie, an | 468 // Is this event from a different profile than the renderer (ie, an |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
491 | 501 |
492 void ExtensionEventRouter::IncrementInFlightEvents( | 502 void ExtensionEventRouter::IncrementInFlightEvents( |
493 Profile* profile, const Extension* extension) { | 503 Profile* profile, const Extension* extension) { |
494 // Only increment in-flight events if the lazy background page is active, | 504 // Only increment in-flight events if the lazy background page is active, |
495 // because that's the only time we'll get an ACK. | 505 // because that's the only time we'll get an ACK. |
496 if (extension->has_lazy_background_page()) { | 506 if (extension->has_lazy_background_page()) { |
497 ExtensionProcessManager* pm = | 507 ExtensionProcessManager* pm = |
498 extensions::ExtensionSystem::Get(profile)->process_manager(); | 508 extensions::ExtensionSystem::Get(profile)->process_manager(); |
499 ExtensionHost* host = pm->GetBackgroundHostForExtension(extension->id()); | 509 ExtensionHost* host = pm->GetBackgroundHostForExtension(extension->id()); |
500 if (host) | 510 if (host) |
501 pm->IncrementLazyKeepaliveCount( | 511 pm->IncrementLazyKeepaliveCount(extension); |
502 extension, ExtensionProcessManager::CANCEL_SUSPEND); | |
503 } | 512 } |
504 } | 513 } |
505 | 514 |
506 void ExtensionEventRouter::OnEventAck( | 515 void ExtensionEventRouter::OnEventAck( |
507 Profile* profile, const std::string& extension_id) { | 516 Profile* profile, const std::string& extension_id) { |
508 ExtensionProcessManager* pm = | 517 ExtensionProcessManager* pm = |
509 extensions::ExtensionSystem::Get(profile)->process_manager(); | 518 extensions::ExtensionSystem::Get(profile)->process_manager(); |
510 ExtensionHost* host = pm->GetBackgroundHostForExtension(extension_id); | 519 ExtensionHost* host = pm->GetBackgroundHostForExtension(extension_id); |
511 // The event ACK is routed to the background host, so this should never be | 520 // The event ACK is routed to the background host, so this should never be |
512 // NULL. | 521 // NULL. |
(...skipping 112 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
625 event_args(event_args.DeepCopy()), | 634 event_args(event_args.DeepCopy()), |
626 event_url(event_url), | 635 event_url(event_url), |
627 restrict_to_profile(restrict_to_profile), | 636 restrict_to_profile(restrict_to_profile), |
628 cross_incognito_args(NULL), | 637 cross_incognito_args(NULL), |
629 user_gesture(user_gesture), | 638 user_gesture(user_gesture), |
630 info(info) { | 639 info(info) { |
631 } | 640 } |
632 | 641 |
633 ExtensionEvent::~ExtensionEvent() { | 642 ExtensionEvent::~ExtensionEvent() { |
634 } | 643 } |
OLD | NEW |