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/message_loop.h" | 8 #include "base/message_loop.h" |
9 #include "base/metrics/histogram.h" | 9 #include "base/metrics/histogram.h" |
10 #include "base/string_number_conversions.h" | 10 #include "base/string_number_conversions.h" |
(...skipping 94 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
105 | 105 |
106 struct ExtensionProcessManager::BackgroundPageData { | 106 struct ExtensionProcessManager::BackgroundPageData { |
107 // The count of things keeping the lazy background page alive. | 107 // The count of things keeping the lazy background page alive. |
108 int lazy_keepalive_count; | 108 int lazy_keepalive_count; |
109 | 109 |
110 // This is used with the ShouldUnload message, to ensure that the extension | 110 // This is used with the ShouldUnload message, to ensure that the extension |
111 // remained idle between sending the message and receiving the ack. | 111 // remained idle between sending the message and receiving the ack. |
112 int close_sequence_id; | 112 int close_sequence_id; |
113 | 113 |
114 // True if the page responded to the ShouldUnload message and is currently | 114 // True if the page responded to the ShouldUnload message and is currently |
115 // dispatching the unload event. We use this to ignore any activity | 115 // dispatching the unload event. During this time any events that arrive will |
116 // generated during the unload event that would otherwise keep the | 116 // cancel the unload process and an onSuspendCanceled event will be dispatched |
117 // extension alive. | 117 // to the page. |
118 bool is_closing; | 118 bool is_closing; |
119 | 119 |
120 // Keeps track of when this page was last unloaded. Used for perf metrics. | 120 // Keeps track of when this page was last unloaded. Used for perf metrics. |
121 linked_ptr<PerfTimer> since_unloaded; | 121 linked_ptr<PerfTimer> since_unloaded; |
122 | 122 |
123 BackgroundPageData() | 123 BackgroundPageData() |
124 : lazy_keepalive_count(0), close_sequence_id(0), is_closing(false) {} | 124 : lazy_keepalive_count(0), close_sequence_id(0), is_closing(false) {} |
125 }; | 125 }; |
126 | 126 |
127 // | 127 // |
(...skipping 307 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
435 MessageLoop::current()->PostDelayedTask( | 435 MessageLoop::current()->PostDelayedTask( |
436 FROM_HERE, | 436 FROM_HERE, |
437 base::Bind(&ExtensionProcessManager::OnLazyBackgroundPageIdle, | 437 base::Bind(&ExtensionProcessManager::OnLazyBackgroundPageIdle, |
438 weak_ptr_factory_.GetWeakPtr(), extension->id(), | 438 weak_ptr_factory_.GetWeakPtr(), extension->id(), |
439 ++background_page_data_[extension->id()].close_sequence_id), | 439 ++background_page_data_[extension->id()].close_sequence_id), |
440 event_page_idle_time_); | 440 event_page_idle_time_); |
441 } | 441 } |
442 | 442 |
443 return count; | 443 return count; |
444 } | 444 } |
445 | |
445 void ExtensionProcessManager::IncrementLazyKeepaliveCountForView( | 446 void ExtensionProcessManager::IncrementLazyKeepaliveCountForView( |
446 RenderViewHost* render_view_host) { | 447 RenderViewHost* render_view_host) { |
447 WebContents* web_contents = | 448 WebContents* web_contents = |
448 WebContents::FromRenderViewHost(render_view_host); | 449 WebContents::FromRenderViewHost(render_view_host); |
449 chrome::ViewType view_type = chrome::GetViewType(web_contents); | 450 chrome::ViewType view_type = chrome::GetViewType(web_contents); |
450 if (view_type != chrome::VIEW_TYPE_INVALID && | 451 if (view_type != chrome::VIEW_TYPE_INVALID && |
451 view_type != chrome::VIEW_TYPE_EXTENSION_BACKGROUND_PAGE) { | 452 view_type != chrome::VIEW_TYPE_EXTENSION_BACKGROUND_PAGE) { |
452 const Extension* extension = GetExtensionForRenderViewHost( | 453 const Extension* extension = GetExtensionForRenderViewHost( |
453 render_view_host); | 454 render_view_host); |
454 if (extension) | 455 if (extension) |
455 IncrementLazyKeepaliveCount(extension); | 456 IncrementLazyKeepaliveCount(extension); |
456 } | 457 } |
457 } | 458 } |
458 | 459 |
460 void ExtensionProcessManager::CancelSuspend( | |
461 const Extension* extension) { | |
462 bool& is_closing = background_page_data_[extension->id()].is_closing; | |
463 if (is_closing) { | |
464 is_closing = false; | |
Matt Perry
2012/07/19 21:36:22
Could you add a DCHECK that the lazy_keepalive_cou
koz (OOO until 15th September)
2012/07/20 06:08:09
That's a good idea. In fact, it's probably a good
| |
465 OnLazyBackgroundPageActive(extension->id()); | |
466 ExtensionHost* host = GetBackgroundHostForExtension(extension->id()); | |
467 if (host) | |
468 host->render_view_host()->Send( | |
469 new ExtensionMsg_CancelUnload(extension->id())); | |
470 } | |
471 } | |
472 | |
459 void ExtensionProcessManager::OnLazyBackgroundPageIdle( | 473 void ExtensionProcessManager::OnLazyBackgroundPageIdle( |
460 const std::string& extension_id, int sequence_id) { | 474 const std::string& extension_id, int sequence_id) { |
461 ExtensionHost* host = GetBackgroundHostForExtension(extension_id); | 475 ExtensionHost* host = GetBackgroundHostForExtension(extension_id); |
462 if (host && !background_page_data_[extension_id].is_closing && | 476 if (host && !background_page_data_[extension_id].is_closing && |
463 sequence_id == background_page_data_[extension_id].close_sequence_id) { | 477 sequence_id == background_page_data_[extension_id].close_sequence_id) { |
464 // Tell the renderer we are about to close. This is a simple ping that the | 478 // Tell the renderer we are about to close. This is a simple ping that the |
465 // renderer will respond to. The purpose is to control sequencing: if the | 479 // renderer will respond to. The purpose is to control sequencing: if the |
466 // extension remains idle until the renderer responds with an ACK, then we | 480 // extension remains idle until the renderer responds with an ACK, then we |
467 // know that the extension process is ready to shut down. If our | 481 // know that the extension process is ready to shut down. If our |
468 // close_sequence_id has already changed, then we would ignore the | 482 // close_sequence_id has already changed, then we would ignore the |
(...skipping 16 matching lines...) Expand all Loading... | |
485 void ExtensionProcessManager::OnShouldUnloadAck( | 499 void ExtensionProcessManager::OnShouldUnloadAck( |
486 const std::string& extension_id, int sequence_id) { | 500 const std::string& extension_id, int sequence_id) { |
487 ExtensionHost* host = GetBackgroundHostForExtension(extension_id); | 501 ExtensionHost* host = GetBackgroundHostForExtension(extension_id); |
488 if (host && | 502 if (host && |
489 sequence_id == background_page_data_[extension_id].close_sequence_id) { | 503 sequence_id == background_page_data_[extension_id].close_sequence_id) { |
490 host->render_view_host()->Send(new ExtensionMsg_Unload(extension_id)); | 504 host->render_view_host()->Send(new ExtensionMsg_Unload(extension_id)); |
491 } | 505 } |
492 } | 506 } |
493 | 507 |
494 void ExtensionProcessManager::OnUnloadAck(const std::string& extension_id) { | 508 void ExtensionProcessManager::OnUnloadAck(const std::string& extension_id) { |
509 background_page_data_[extension_id].is_closing = true; | |
510 int sequence_id = background_page_data_[extension_id].close_sequence_id; | |
495 MessageLoop::current()->PostDelayedTask( | 511 MessageLoop::current()->PostDelayedTask( |
496 FROM_HERE, | 512 FROM_HERE, |
497 base::Bind(&ExtensionProcessManager::CloseLazyBackgroundPageNow, | 513 base::Bind(&ExtensionProcessManager::CloseLazyBackgroundPageNow, |
498 weak_ptr_factory_.GetWeakPtr(), extension_id), | 514 weak_ptr_factory_.GetWeakPtr(), extension_id, sequence_id), |
499 event_page_unloading_time_); | 515 event_page_unloading_time_); |
500 } | 516 } |
501 | 517 |
502 void ExtensionProcessManager::CloseLazyBackgroundPageNow( | 518 void ExtensionProcessManager::CloseLazyBackgroundPageNow( |
503 const std::string& extension_id) { | 519 const std::string& extension_id, int sequence_id) { |
504 background_page_data_[extension_id].is_closing = true; | |
505 ExtensionHost* host = GetBackgroundHostForExtension(extension_id); | 520 ExtensionHost* host = GetBackgroundHostForExtension(extension_id); |
506 if (host) | 521 if (host && |
507 CloseBackgroundHost(host); | 522 sequence_id == background_page_data_[extension_id].close_sequence_id) { |
523 ExtensionHost* host = GetBackgroundHostForExtension(extension_id); | |
524 if (host) | |
525 CloseBackgroundHost(host); | |
526 } | |
508 } | 527 } |
509 | 528 |
510 void ExtensionProcessManager::OnNetworkRequestStarted( | 529 void ExtensionProcessManager::OnNetworkRequestStarted( |
511 RenderViewHost* render_view_host) { | 530 RenderViewHost* render_view_host) { |
512 ExtensionHost* host = GetBackgroundHostForExtension( | 531 ExtensionHost* host = GetBackgroundHostForExtension( |
513 GetExtensionID(render_view_host)); | 532 GetExtensionID(render_view_host)); |
514 if (host && host->render_view_host() == render_view_host) | 533 if (host && host->render_view_host() == render_view_host) |
515 IncrementLazyKeepaliveCount(host->extension()); | 534 IncrementLazyKeepaliveCount(host->extension()); |
516 } | 535 } |
517 | 536 |
(...skipping 261 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
779 if (service && service->is_ready()) | 798 if (service && service->is_ready()) |
780 CreateBackgroundHostsForProfileStartup(this, service->extensions()); | 799 CreateBackgroundHostsForProfileStartup(this, service->extensions()); |
781 } | 800 } |
782 break; | 801 break; |
783 } | 802 } |
784 default: | 803 default: |
785 ExtensionProcessManager::Observe(type, source, details); | 804 ExtensionProcessManager::Observe(type, source, details); |
786 break; | 805 break; |
787 } | 806 } |
788 } | 807 } |
OLD | NEW |