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/values.h" | 9 #include "base/values.h" |
10 #include "chrome/browser/extensions/extension_devtools_manager.h" | 10 #include "chrome/browser/extensions/extension_devtools_manager.h" |
(...skipping 364 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
375 for (std::set<ListenerProcess>::iterator listener = listeners.begin(); | 375 for (std::set<ListenerProcess>::iterator listener = listeners.begin(); |
376 listener != listeners.end(); ++listener) { | 376 listener != listeners.end(); ++listener) { |
377 if (!extension_id.empty() && extension_id != listener->extension_id) | 377 if (!extension_id.empty() && extension_id != listener->extension_id) |
378 continue; | 378 continue; |
379 | 379 |
380 const Extension* extension = service->extensions()->GetByID( | 380 const Extension* extension = service->extensions()->GetByID( |
381 listener->extension_id); | 381 listener->extension_id); |
382 | 382 |
383 if (extension && !CanDispatchEventNow(extension)) { | 383 if (extension && !CanDispatchEventNow(extension)) { |
384 AppendEvent(extension->id(), event); | 384 AppendEvent(extension->id(), event); |
385 pm->CreateBackgroundHost(extension, extension->GetBackgroundURL()); | 385 if (!pm->GetBackgroundHostForExtension(extension->id())) { |
| 386 // Balanced in DispatchPendingEvents, after the page has loaded. |
| 387 pm->IncrementLazyKeepaliveCount(extension); |
| 388 pm->CreateBackgroundHost(extension, extension->GetBackgroundURL()); |
| 389 } |
386 } | 390 } |
387 } | 391 } |
388 } | 392 } |
389 | 393 |
390 void ExtensionEventRouter::IncrementInFlightEvents(const Extension* extension) { | 394 void ExtensionEventRouter::IncrementInFlightEvents(const Extension* extension) { |
391 if (!extension->background_page_persists()) { | 395 if (!extension->background_page_persists()) { |
392 profile_->GetExtensionProcessManager()->IncrementLazyKeepaliveCount( | 396 profile_->GetExtensionProcessManager()->IncrementLazyKeepaliveCount( |
393 extension); | 397 extension); |
394 } | 398 } |
395 } | 399 } |
(...skipping 26 matching lines...) Expand all Loading... |
422 void ExtensionEventRouter::DispatchPendingEvents( | 426 void ExtensionEventRouter::DispatchPendingEvents( |
423 const std::string& extension_id) { | 427 const std::string& extension_id) { |
424 CHECK(!extension_id.empty()); | 428 CHECK(!extension_id.empty()); |
425 PendingEventsPerExtMap::const_iterator map_it = | 429 PendingEventsPerExtMap::const_iterator map_it = |
426 pending_events_.find(extension_id); | 430 pending_events_.find(extension_id); |
427 if (map_it == pending_events_.end()) { | 431 if (map_it == pending_events_.end()) { |
428 NOTREACHED(); // lazy page should not load without any pending events | 432 NOTREACHED(); // lazy page should not load without any pending events |
429 return; | 433 return; |
430 } | 434 } |
431 | 435 |
432 // Temporarily increment the keepalive count while dispatching the events. | |
433 // This also ensures that if no events were dispatched, the extension returns | |
434 // to "idle" and is shut down. | |
435 const Extension* extension = | |
436 profile_->GetExtensionService()->extensions()->GetByID(extension_id); | |
437 ExtensionProcessManager* pm = profile_->GetExtensionProcessManager(); | |
438 pm->IncrementLazyKeepaliveCount(extension); | |
439 | |
440 PendingEventsList* events_list = map_it->second.get(); | 436 PendingEventsList* events_list = map_it->second.get(); |
441 for (PendingEventsList::const_iterator it = events_list->begin(); | 437 for (PendingEventsList::const_iterator it = events_list->begin(); |
442 it != events_list->end(); ++it) | 438 it != events_list->end(); ++it) |
443 DispatchEventImpl(extension_id, *it, true); | 439 DispatchEventImpl(extension_id, *it, true); |
444 | 440 |
445 events_list->clear(); | 441 events_list->clear(); |
446 pending_events_.erase(extension_id); | 442 pending_events_.erase(extension_id); |
447 | 443 |
| 444 // Balance the keepalive addref in LoadLazyBackgroundPagesForEvent. |
| 445 const Extension* extension = |
| 446 profile_->GetExtensionService()->extensions()->GetByID(extension_id); |
| 447 ExtensionProcessManager* pm = profile_->GetExtensionProcessManager(); |
448 pm->DecrementLazyKeepaliveCount(extension); | 448 pm->DecrementLazyKeepaliveCount(extension); |
449 } | 449 } |
450 | 450 |
451 void ExtensionEventRouter::Observe( | 451 void ExtensionEventRouter::Observe( |
452 int type, | 452 int type, |
453 const content::NotificationSource& source, | 453 const content::NotificationSource& source, |
454 const content::NotificationDetails& details) { | 454 const content::NotificationDetails& details) { |
455 switch (type) { | 455 switch (type) { |
456 case content::NOTIFICATION_RENDERER_PROCESS_TERMINATED: | 456 case content::NOTIFICATION_RENDERER_PROCESS_TERMINATED: |
457 case content::NOTIFICATION_RENDERER_PROCESS_CLOSED: { | 457 case content::NOTIFICATION_RENDERER_PROCESS_CLOSED: { |
(...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
520 extension->id(), kOnInstalledEvent, "[]", NULL, GURL()); | 520 extension->id(), kOnInstalledEvent, "[]", NULL, GURL()); |
521 break; | 521 break; |
522 } | 522 } |
523 | 523 |
524 // TODO(tessamac): if background page crashed/failed clear queue. | 524 // TODO(tessamac): if background page crashed/failed clear queue. |
525 default: | 525 default: |
526 NOTREACHED(); | 526 NOTREACHED(); |
527 return; | 527 return; |
528 } | 528 } |
529 } | 529 } |
OLD | NEW |