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 "base/bind.h" | 5 #include "base/bind.h" |
6 #include "base/command_line.h" | 6 #include "base/command_line.h" |
7 #include "base/lazy_instance.h" | 7 #include "base/lazy_instance.h" |
8 #include "base/logging.h" | |
8 #include "base/message_loop.h" | 9 #include "base/message_loop.h" |
9 #include "base/metrics/histogram.h" | 10 #include "base/metrics/histogram.h" |
10 #include "base/string_number_conversions.h" | 11 #include "base/string_number_conversions.h" |
11 #include "base/time.h" | 12 #include "base/time.h" |
12 #include "chrome/browser/extensions/extension_event_router.h" | 13 #include "chrome/browser/extensions/extension_event_router.h" |
13 #include "chrome/browser/extensions/extension_process_manager.h" | 14 #include "chrome/browser/extensions/extension_process_manager.h" |
14 #include "chrome/browser/extensions/extension_host.h" | 15 #include "chrome/browser/extensions/extension_host.h" |
15 #include "chrome/browser/extensions/extension_info_map.h" | 16 #include "chrome/browser/extensions/extension_info_map.h" |
16 #include "chrome/browser/extensions/extension_service.h" | 17 #include "chrome/browser/extensions/extension_service.h" |
17 #include "chrome/browser/extensions/extension_system.h" | 18 #include "chrome/browser/extensions/extension_system.h" |
(...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
105 | 106 |
106 struct ExtensionProcessManager::BackgroundPageData { | 107 struct ExtensionProcessManager::BackgroundPageData { |
107 // The count of things keeping the lazy background page alive. | 108 // The count of things keeping the lazy background page alive. |
108 int lazy_keepalive_count; | 109 int lazy_keepalive_count; |
109 | 110 |
110 // This is used with the ShouldUnload message, to ensure that the extension | 111 // This is used with the ShouldUnload message, to ensure that the extension |
111 // remained idle between sending the message and receiving the ack. | 112 // remained idle between sending the message and receiving the ack. |
112 int close_sequence_id; | 113 int close_sequence_id; |
113 | 114 |
114 // True if the page responded to the ShouldUnload message and is currently | 115 // True if the page responded to the ShouldUnload message and is currently |
115 // dispatching the unload event. We use this to ignore any activity | 116 // dispatching the unload event. During this time any events that arrive will |
116 // generated during the unload event that would otherwise keep the | 117 // cancel the unload process and an onSuspendCanceled event will be dispatched |
117 // extension alive. | 118 // to the page. |
118 bool is_closing; | 119 bool is_closing; |
119 | 120 |
120 // Keeps track of when this page was last unloaded. Used for perf metrics. | 121 // Keeps track of when this page was last unloaded. Used for perf metrics. |
121 linked_ptr<PerfTimer> since_unloaded; | 122 linked_ptr<PerfTimer> since_unloaded; |
122 | 123 |
123 BackgroundPageData() | 124 BackgroundPageData() |
124 : lazy_keepalive_count(0), close_sequence_id(0), is_closing(false) {} | 125 : lazy_keepalive_count(0), close_sequence_id(0), is_closing(false) {} |
125 }; | 126 }; |
126 | 127 |
127 // | 128 // |
(...skipping 278 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
406 } | 407 } |
407 | 408 |
408 int ExtensionProcessManager::GetLazyKeepaliveCount(const Extension* extension) { | 409 int ExtensionProcessManager::GetLazyKeepaliveCount(const Extension* extension) { |
409 if (!extension->has_lazy_background_page()) | 410 if (!extension->has_lazy_background_page()) |
410 return 0; | 411 return 0; |
411 | 412 |
412 return background_page_data_[extension->id()].lazy_keepalive_count; | 413 return background_page_data_[extension->id()].lazy_keepalive_count; |
413 } | 414 } |
414 | 415 |
415 int ExtensionProcessManager::IncrementLazyKeepaliveCount( | 416 int ExtensionProcessManager::IncrementLazyKeepaliveCount( |
417 const Extension* extension, bool cancel_suspend) { | |
418 if (!extension->has_lazy_background_page()) | |
419 return 0; | |
420 | |
421 int& count = background_page_data_[extension->id()].lazy_keepalive_count; | |
422 bool& is_closing = background_page_data_[extension->id()].is_closing; | |
423 if (++count == 1) { | |
424 if (cancel_suspend && is_closing) { | |
425 is_closing = false; | |
426 ExtensionHost* host = GetBackgroundHostForExtension(extension->id()); | |
427 if (host) | |
428 host->render_view_host()->Send( | |
429 new ExtensionMsg_CancelUnload(extension->id())); | |
benwells
2012/07/20 10:57:00
Nit: braces around the if body as there is more th
| |
430 } | |
431 OnLazyBackgroundPageActive(extension->id()); | |
432 } | |
433 | |
434 return count; | |
435 } | |
436 | |
437 int ExtensionProcessManager::IncrementLazyKeepaliveCount( | |
416 const Extension* extension) { | 438 const Extension* extension) { |
417 if (!extension->has_lazy_background_page()) | 439 return IncrementLazyKeepaliveCount(extension, false); |
418 return 0; | |
419 | |
420 int& count = background_page_data_[extension->id()].lazy_keepalive_count; | |
421 if (++count == 1) | |
422 OnLazyBackgroundPageActive(extension->id()); | |
423 | |
424 return count; | |
425 } | 440 } |
426 | 441 |
427 int ExtensionProcessManager::DecrementLazyKeepaliveCount( | 442 int ExtensionProcessManager::DecrementLazyKeepaliveCount( |
428 const Extension* extension) { | 443 const Extension* extension) { |
429 if (!extension->has_lazy_background_page()) | 444 if (!extension->has_lazy_background_page()) |
430 return 0; | 445 return 0; |
431 | 446 |
432 int& count = background_page_data_[extension->id()].lazy_keepalive_count; | 447 int& count = background_page_data_[extension->id()].lazy_keepalive_count; |
433 DCHECK_GT(count, 0); | 448 DCHECK_GT(count, 0); |
434 if (--count == 0) { | 449 if (--count == 0) { |
435 MessageLoop::current()->PostDelayedTask( | 450 MessageLoop::current()->PostDelayedTask( |
436 FROM_HERE, | 451 FROM_HERE, |
437 base::Bind(&ExtensionProcessManager::OnLazyBackgroundPageIdle, | 452 base::Bind(&ExtensionProcessManager::OnLazyBackgroundPageIdle, |
438 weak_ptr_factory_.GetWeakPtr(), extension->id(), | 453 weak_ptr_factory_.GetWeakPtr(), extension->id(), |
439 ++background_page_data_[extension->id()].close_sequence_id), | 454 ++background_page_data_[extension->id()].close_sequence_id), |
440 event_page_idle_time_); | 455 event_page_idle_time_); |
441 } | 456 } |
442 | 457 |
443 return count; | 458 return count; |
444 } | 459 } |
460 | |
445 void ExtensionProcessManager::IncrementLazyKeepaliveCountForView( | 461 void ExtensionProcessManager::IncrementLazyKeepaliveCountForView( |
446 RenderViewHost* render_view_host) { | 462 RenderViewHost* render_view_host) { |
447 WebContents* web_contents = | 463 WebContents* web_contents = |
448 WebContents::FromRenderViewHost(render_view_host); | 464 WebContents::FromRenderViewHost(render_view_host); |
449 chrome::ViewType view_type = chrome::GetViewType(web_contents); | 465 chrome::ViewType view_type = chrome::GetViewType(web_contents); |
450 if (view_type != chrome::VIEW_TYPE_INVALID && | 466 if (view_type != chrome::VIEW_TYPE_INVALID && |
451 view_type != chrome::VIEW_TYPE_EXTENSION_BACKGROUND_PAGE) { | 467 view_type != chrome::VIEW_TYPE_EXTENSION_BACKGROUND_PAGE) { |
452 const Extension* extension = GetExtensionForRenderViewHost( | 468 const Extension* extension = GetExtensionForRenderViewHost( |
453 render_view_host); | 469 render_view_host); |
454 if (extension) | 470 if (extension) |
(...skipping 30 matching lines...) Expand all Loading... | |
485 void ExtensionProcessManager::OnShouldUnloadAck( | 501 void ExtensionProcessManager::OnShouldUnloadAck( |
486 const std::string& extension_id, int sequence_id) { | 502 const std::string& extension_id, int sequence_id) { |
487 ExtensionHost* host = GetBackgroundHostForExtension(extension_id); | 503 ExtensionHost* host = GetBackgroundHostForExtension(extension_id); |
488 if (host && | 504 if (host && |
489 sequence_id == background_page_data_[extension_id].close_sequence_id) { | 505 sequence_id == background_page_data_[extension_id].close_sequence_id) { |
490 host->render_view_host()->Send(new ExtensionMsg_Unload(extension_id)); | 506 host->render_view_host()->Send(new ExtensionMsg_Unload(extension_id)); |
491 } | 507 } |
492 } | 508 } |
493 | 509 |
494 void ExtensionProcessManager::OnUnloadAck(const std::string& extension_id) { | 510 void ExtensionProcessManager::OnUnloadAck(const std::string& extension_id) { |
511 background_page_data_[extension_id].is_closing = true; | |
512 int sequence_id = background_page_data_[extension_id].close_sequence_id; | |
495 MessageLoop::current()->PostDelayedTask( | 513 MessageLoop::current()->PostDelayedTask( |
496 FROM_HERE, | 514 FROM_HERE, |
497 base::Bind(&ExtensionProcessManager::CloseLazyBackgroundPageNow, | 515 base::Bind(&ExtensionProcessManager::CloseLazyBackgroundPageNow, |
498 weak_ptr_factory_.GetWeakPtr(), extension_id), | 516 weak_ptr_factory_.GetWeakPtr(), extension_id, sequence_id), |
499 event_page_unloading_time_); | 517 event_page_unloading_time_); |
500 } | 518 } |
501 | 519 |
502 void ExtensionProcessManager::CloseLazyBackgroundPageNow( | 520 void ExtensionProcessManager::CloseLazyBackgroundPageNow( |
503 const std::string& extension_id) { | 521 const std::string& extension_id, int sequence_id) { |
504 background_page_data_[extension_id].is_closing = true; | |
505 ExtensionHost* host = GetBackgroundHostForExtension(extension_id); | 522 ExtensionHost* host = GetBackgroundHostForExtension(extension_id); |
506 if (host) | 523 if (host && |
507 CloseBackgroundHost(host); | 524 sequence_id == background_page_data_[extension_id].close_sequence_id) { |
525 ExtensionHost* host = GetBackgroundHostForExtension(extension_id); | |
526 if (host) | |
527 CloseBackgroundHost(host); | |
528 } | |
508 } | 529 } |
509 | 530 |
510 void ExtensionProcessManager::OnNetworkRequestStarted( | 531 void ExtensionProcessManager::OnNetworkRequestStarted( |
511 RenderViewHost* render_view_host) { | 532 RenderViewHost* render_view_host) { |
512 ExtensionHost* host = GetBackgroundHostForExtension( | 533 ExtensionHost* host = GetBackgroundHostForExtension( |
513 GetExtensionID(render_view_host)); | 534 GetExtensionID(render_view_host)); |
514 if (host && host->render_view_host() == render_view_host) | 535 if (host && host->render_view_host() == render_view_host) |
515 IncrementLazyKeepaliveCount(host->extension()); | 536 IncrementLazyKeepaliveCount(host->extension()); |
516 } | 537 } |
517 | 538 |
(...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
601 content::Details<RenderViewHost>(details).ptr(); | 622 content::Details<RenderViewHost>(details).ptr(); |
602 WebContents* web_contents = | 623 WebContents* web_contents = |
603 WebContents::FromRenderViewHost(render_view_host); | 624 WebContents::FromRenderViewHost(render_view_host); |
604 // Keep the lazy background page alive while it's being inspected. | 625 // Keep the lazy background page alive while it's being inspected. |
605 // Balanced in response to the CLOSING notification. | 626 // Balanced in response to the CLOSING notification. |
606 if (chrome::GetViewType(web_contents) == | 627 if (chrome::GetViewType(web_contents) == |
607 chrome::VIEW_TYPE_EXTENSION_BACKGROUND_PAGE) { | 628 chrome::VIEW_TYPE_EXTENSION_BACKGROUND_PAGE) { |
608 const Extension* extension = GetExtensionForRenderViewHost( | 629 const Extension* extension = GetExtensionForRenderViewHost( |
609 render_view_host); | 630 render_view_host); |
610 if (extension) | 631 if (extension) |
611 IncrementLazyKeepaliveCount(extension); | 632 IncrementLazyKeepaliveCount(extension, true); |
612 } | 633 } |
613 break; | 634 break; |
614 } | 635 } |
615 | 636 |
616 case content::NOTIFICATION_DEVTOOLS_WINDOW_CLOSING: { | 637 case content::NOTIFICATION_DEVTOOLS_WINDOW_CLOSING: { |
617 RenderViewHost* render_view_host = | 638 RenderViewHost* render_view_host = |
618 content::Details<RenderViewHost>(details).ptr(); | 639 content::Details<RenderViewHost>(details).ptr(); |
619 WebContents* web_contents = | 640 WebContents* web_contents = |
620 WebContents::FromRenderViewHost(render_view_host); | 641 WebContents::FromRenderViewHost(render_view_host); |
621 // Balanced in response to the OPENING notification. | 642 // Balanced in response to the OPENING notification. |
(...skipping 157 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
779 if (service && service->is_ready()) | 800 if (service && service->is_ready()) |
780 CreateBackgroundHostsForProfileStartup(this, service->extensions()); | 801 CreateBackgroundHostsForProfileStartup(this, service->extensions()); |
781 } | 802 } |
782 break; | 803 break; |
783 } | 804 } |
784 default: | 805 default: |
785 ExtensionProcessManager::Observe(type, source, details); | 806 ExtensionProcessManager::Observe(type, source, details); |
786 break; | 807 break; |
787 } | 808 } |
788 } | 809 } |
OLD | NEW |