| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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 "extensions/browser/process_manager.h" | 5 #include "extensions/browser/process_manager.h" |
| 6 | 6 |
| 7 #include <vector> | 7 #include <vector> |
| 8 | 8 |
| 9 #include "base/bind.h" | 9 #include "base/bind.h" |
| 10 #include "base/location.h" |
| 10 #include "base/logging.h" | 11 #include "base/logging.h" |
| 11 #include "base/macros.h" | 12 #include "base/macros.h" |
| 12 #include "base/message_loop/message_loop.h" | 13 #include "base/message_loop/message_loop.h" |
| 13 #include "base/metrics/histogram_macros.h" | 14 #include "base/metrics/histogram_macros.h" |
| 15 #include "base/single_thread_task_runner.h" |
| 14 #include "base/stl_util.h" | 16 #include "base/stl_util.h" |
| 17 #include "base/threading/thread_task_runner_handle.h" |
| 15 #include "base/time/time.h" | 18 #include "base/time/time.h" |
| 16 #include "content/public/browser/browser_context.h" | 19 #include "content/public/browser/browser_context.h" |
| 17 #include "content/public/browser/devtools_agent_host.h" | 20 #include "content/public/browser/devtools_agent_host.h" |
| 18 #include "content/public/browser/notification_service.h" | 21 #include "content/public/browser/notification_service.h" |
| 19 #include "content/public/browser/render_frame_host.h" | 22 #include "content/public/browser/render_frame_host.h" |
| 20 #include "content/public/browser/render_process_host.h" | 23 #include "content/public/browser/render_process_host.h" |
| 21 #include "content/public/browser/render_view_host.h" | 24 #include "content/public/browser/render_view_host.h" |
| 22 #include "content/public/browser/site_instance.h" | 25 #include "content/public/browser/site_instance.h" |
| 23 #include "content/public/browser/web_contents.h" | 26 #include "content/public/browser/web_contents.h" |
| 24 #include "content/public/common/url_constants.h" | 27 #include "content/public/common/url_constants.h" |
| (...skipping 523 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 548 ExtensionHost* host = GetBackgroundHostForExtension(extension_id); | 551 ExtensionHost* host = GetBackgroundHostForExtension(extension_id); |
| 549 if (host && | 552 if (host && |
| 550 sequence_id == background_page_data_[extension_id].close_sequence_id) { | 553 sequence_id == background_page_data_[extension_id].close_sequence_id) { |
| 551 host->render_process_host()->Send(new ExtensionMsg_Suspend(extension_id)); | 554 host->render_process_host()->Send(new ExtensionMsg_Suspend(extension_id)); |
| 552 } | 555 } |
| 553 } | 556 } |
| 554 | 557 |
| 555 void ProcessManager::OnSuspendAck(const std::string& extension_id) { | 558 void ProcessManager::OnSuspendAck(const std::string& extension_id) { |
| 556 background_page_data_[extension_id].is_closing = true; | 559 background_page_data_[extension_id].is_closing = true; |
| 557 uint64_t sequence_id = background_page_data_[extension_id].close_sequence_id; | 560 uint64_t sequence_id = background_page_data_[extension_id].close_sequence_id; |
| 558 base::MessageLoop::current()->PostDelayedTask( | 561 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask( |
| 559 FROM_HERE, | 562 FROM_HERE, |
| 560 base::Bind(&ProcessManager::CloseLazyBackgroundPageNow, | 563 base::Bind(&ProcessManager::CloseLazyBackgroundPageNow, |
| 561 weak_ptr_factory_.GetWeakPtr(), | 564 weak_ptr_factory_.GetWeakPtr(), extension_id, sequence_id), |
| 562 extension_id, | |
| 563 sequence_id), | |
| 564 base::TimeDelta::FromMilliseconds(g_event_page_suspending_time_msec)); | 565 base::TimeDelta::FromMilliseconds(g_event_page_suspending_time_msec)); |
| 565 } | 566 } |
| 566 | 567 |
| 567 void ProcessManager::OnNetworkRequestStarted( | 568 void ProcessManager::OnNetworkRequestStarted( |
| 568 content::RenderFrameHost* render_frame_host, | 569 content::RenderFrameHost* render_frame_host, |
| 569 uint64_t request_id) { | 570 uint64_t request_id) { |
| 570 ExtensionHost* host = GetBackgroundHostForExtension( | 571 ExtensionHost* host = GetBackgroundHostForExtension( |
| 571 GetExtensionID(render_frame_host)); | 572 GetExtensionID(render_frame_host)); |
| 572 if (!host || !IsFrameInExtensionHost(host, render_frame_host)) | 573 if (!host || !IsFrameInExtensionHost(host, render_frame_host)) |
| 573 return; | 574 return; |
| (...skipping 206 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 780 DCHECK(count > 0 || | 781 DCHECK(count > 0 || |
| 781 !extension_registry_->enabled_extensions().Contains(extension_id)); | 782 !extension_registry_->enabled_extensions().Contains(extension_id)); |
| 782 | 783 |
| 783 // If we reach a zero keepalive count when the lazy background page is about | 784 // If we reach a zero keepalive count when the lazy background page is about |
| 784 // to be closed, incrementing close_sequence_id will cancel the close | 785 // to be closed, incrementing close_sequence_id will cancel the close |
| 785 // sequence and cause the background page to linger. So check is_closing | 786 // sequence and cause the background page to linger. So check is_closing |
| 786 // before initiating another close sequence. | 787 // before initiating another close sequence. |
| 787 if (--count == 0 && !background_page_data_[extension_id].is_closing) { | 788 if (--count == 0 && !background_page_data_[extension_id].is_closing) { |
| 788 background_page_data_[extension_id].close_sequence_id = | 789 background_page_data_[extension_id].close_sequence_id = |
| 789 ++last_background_close_sequence_id_; | 790 ++last_background_close_sequence_id_; |
| 790 base::MessageLoop::current()->PostDelayedTask( | 791 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask( |
| 791 FROM_HERE, | 792 FROM_HERE, base::Bind(&ProcessManager::OnLazyBackgroundPageIdle, |
| 792 base::Bind(&ProcessManager::OnLazyBackgroundPageIdle, | 793 weak_ptr_factory_.GetWeakPtr(), extension_id, |
| 793 weak_ptr_factory_.GetWeakPtr(), | 794 last_background_close_sequence_id_), |
| 794 extension_id, | |
| 795 last_background_close_sequence_id_), | |
| 796 base::TimeDelta::FromMilliseconds(g_event_page_idle_time_msec)); | 795 base::TimeDelta::FromMilliseconds(g_event_page_idle_time_msec)); |
| 797 } | 796 } |
| 798 } | 797 } |
| 799 | 798 |
| 800 // DecrementLazyKeepaliveCount is called when no calls to KeepaliveImpulse | 799 // DecrementLazyKeepaliveCount is called when no calls to KeepaliveImpulse |
| 801 // have been made for at least g_event_page_idle_time_msec. In the best case an | 800 // have been made for at least g_event_page_idle_time_msec. In the best case an |
| 802 // impulse was made just before being cleared, and the decrement will occur | 801 // impulse was made just before being cleared, and the decrement will occur |
| 803 // g_event_page_idle_time_msec later, causing a 2 * g_event_page_idle_time_msec | 802 // g_event_page_idle_time_msec later, causing a 2 * g_event_page_idle_time_msec |
| 804 // total time for extension to be shut down based on impulses. Worst case is | 803 // total time for extension to be shut down based on impulses. Worst case is |
| 805 // an impulse just after a clear, adding one check cycle and resulting in 3x | 804 // an impulse just after a clear, adding one check cycle and resulting in 3x |
| (...skipping 11 matching lines...) Expand all Loading... |
| 817 } | 816 } |
| 818 } | 817 } |
| 819 | 818 |
| 820 i->second.previous_keepalive_impulse = i->second.keepalive_impulse; | 819 i->second.previous_keepalive_impulse = i->second.keepalive_impulse; |
| 821 i->second.keepalive_impulse = false; | 820 i->second.keepalive_impulse = false; |
| 822 } | 821 } |
| 823 | 822 |
| 824 // OnKeepaliveImpulseCheck() is always called in constructor, but in unit | 823 // OnKeepaliveImpulseCheck() is always called in constructor, but in unit |
| 825 // tests there will be no message loop. In that event don't schedule tasks. | 824 // tests there will be no message loop. In that event don't schedule tasks. |
| 826 if (base::MessageLoop::current()) { | 825 if (base::MessageLoop::current()) { |
| 827 base::MessageLoop::current()->PostDelayedTask( | 826 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask( |
| 828 FROM_HERE, | 827 FROM_HERE, base::Bind(&ProcessManager::OnKeepaliveImpulseCheck, |
| 829 base::Bind(&ProcessManager::OnKeepaliveImpulseCheck, | 828 weak_ptr_factory_.GetWeakPtr()), |
| 830 weak_ptr_factory_.GetWeakPtr()), | |
| 831 base::TimeDelta::FromMilliseconds(g_event_page_idle_time_msec)); | 829 base::TimeDelta::FromMilliseconds(g_event_page_idle_time_msec)); |
| 832 } | 830 } |
| 833 } | 831 } |
| 834 | 832 |
| 835 void ProcessManager::OnLazyBackgroundPageIdle(const std::string& extension_id, | 833 void ProcessManager::OnLazyBackgroundPageIdle(const std::string& extension_id, |
| 836 uint64_t sequence_id) { | 834 uint64_t sequence_id) { |
| 837 ExtensionHost* host = GetBackgroundHostForExtension(extension_id); | 835 ExtensionHost* host = GetBackgroundHostForExtension(extension_id); |
| 838 if (host && !background_page_data_[extension_id].is_closing && | 836 if (host && !background_page_data_[extension_id].is_closing && |
| 839 sequence_id == background_page_data_[extension_id].close_sequence_id) { | 837 sequence_id == background_page_data_[extension_id].close_sequence_id) { |
| 840 // Tell the renderer we are about to close. This is a simple ping that the | 838 // Tell the renderer we are about to close. This is a simple ping that the |
| (...skipping 149 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 990 if (extension && !IncognitoInfo::IsSplitMode(extension)) { | 988 if (extension && !IncognitoInfo::IsSplitMode(extension)) { |
| 991 BrowserContext* original_context = | 989 BrowserContext* original_context = |
| 992 ExtensionsBrowserClient::Get()->GetOriginalContext(browser_context()); | 990 ExtensionsBrowserClient::Get()->GetOriginalContext(browser_context()); |
| 993 return ProcessManager::Get(original_context)->GetSiteInstanceForURL(url); | 991 return ProcessManager::Get(original_context)->GetSiteInstanceForURL(url); |
| 994 } | 992 } |
| 995 | 993 |
| 996 return ProcessManager::GetSiteInstanceForURL(url); | 994 return ProcessManager::GetSiteInstanceForURL(url); |
| 997 } | 995 } |
| 998 | 996 |
| 999 } // namespace extensions | 997 } // namespace extensions |
| OLD | NEW |