| 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/logging.h" | 10 #include "base/logging.h" |
| 11 #include "base/macros.h" |
| 11 #include "base/message_loop/message_loop.h" | 12 #include "base/message_loop/message_loop.h" |
| 12 #include "base/metrics/histogram_macros.h" | 13 #include "base/metrics/histogram_macros.h" |
| 13 #include "base/stl_util.h" | 14 #include "base/stl_util.h" |
| 14 #include "base/time/time.h" | 15 #include "base/time/time.h" |
| 15 #include "content/public/browser/browser_context.h" | 16 #include "content/public/browser/browser_context.h" |
| 16 #include "content/public/browser/devtools_agent_host.h" | 17 #include "content/public/browser/devtools_agent_host.h" |
| 17 #include "content/public/browser/notification_service.h" | 18 #include "content/public/browser/notification_service.h" |
| 18 #include "content/public/browser/render_frame_host.h" | 19 #include "content/public/browser/render_frame_host.h" |
| 19 #include "content/public/browser/render_process_host.h" | 20 #include "content/public/browser/render_process_host.h" |
| 20 #include "content/public/browser/render_view_host.h" | 21 #include "content/public/browser/render_view_host.h" |
| (...skipping 108 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 129 // dispatching the suspend event. During this time any events that arrive will | 130 // dispatching the suspend event. During this time any events that arrive will |
| 130 // cancel the suspend process and an onSuspendCanceled event will be | 131 // cancel the suspend process and an onSuspendCanceled event will be |
| 131 // dispatched to the page. | 132 // dispatched to the page. |
| 132 bool is_closing; | 133 bool is_closing; |
| 133 | 134 |
| 134 // Stores the value of the incremented | 135 // Stores the value of the incremented |
| 135 // ProcessManager::last_background_close_sequence_id_ whenever the extension | 136 // ProcessManager::last_background_close_sequence_id_ whenever the extension |
| 136 // is active. A copy of the ID is also passed in the callbacks and IPC | 137 // is active. A copy of the ID is also passed in the callbacks and IPC |
| 137 // messages leading up to CloseLazyBackgroundPageNow. The process is aborted | 138 // messages leading up to CloseLazyBackgroundPageNow. The process is aborted |
| 138 // if the IDs ever differ due to new activity. | 139 // if the IDs ever differ due to new activity. |
| 139 uint64 close_sequence_id; | 140 uint64_t close_sequence_id; |
| 140 | 141 |
| 141 // Keeps track of when this page was last suspended. Used for perf metrics. | 142 // Keeps track of when this page was last suspended. Used for perf metrics. |
| 142 linked_ptr<base::ElapsedTimer> since_suspended; | 143 linked_ptr<base::ElapsedTimer> since_suspended; |
| 143 | 144 |
| 144 BackgroundPageData() | 145 BackgroundPageData() |
| 145 : lazy_keepalive_count(0), | 146 : lazy_keepalive_count(0), |
| 146 keepalive_impulse(false), | 147 keepalive_impulse(false), |
| 147 previous_keepalive_impulse(false), | 148 previous_keepalive_impulse(false), |
| 148 is_closing(false), | 149 is_closing(false), |
| 149 close_sequence_id(0) {} | 150 close_sequence_id(0) {} |
| (...skipping 386 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 536 const Extension* extension = | 537 const Extension* extension = |
| 537 ExtensionRegistry::Get(browser_context)->enabled_extensions().GetByID( | 538 ExtensionRegistry::Get(browser_context)->enabled_extensions().GetByID( |
| 538 extension_id); | 539 extension_id); |
| 539 if (!extension) | 540 if (!extension) |
| 540 return; | 541 return; |
| 541 | 542 |
| 542 ProcessManager::Get(browser_context)->KeepaliveImpulse(extension); | 543 ProcessManager::Get(browser_context)->KeepaliveImpulse(extension); |
| 543 } | 544 } |
| 544 | 545 |
| 545 void ProcessManager::OnShouldSuspendAck(const std::string& extension_id, | 546 void ProcessManager::OnShouldSuspendAck(const std::string& extension_id, |
| 546 uint64 sequence_id) { | 547 uint64_t sequence_id) { |
| 547 ExtensionHost* host = GetBackgroundHostForExtension(extension_id); | 548 ExtensionHost* host = GetBackgroundHostForExtension(extension_id); |
| 548 if (host && | 549 if (host && |
| 549 sequence_id == background_page_data_[extension_id].close_sequence_id) { | 550 sequence_id == background_page_data_[extension_id].close_sequence_id) { |
| 550 host->render_process_host()->Send(new ExtensionMsg_Suspend(extension_id)); | 551 host->render_process_host()->Send(new ExtensionMsg_Suspend(extension_id)); |
| 551 } | 552 } |
| 552 } | 553 } |
| 553 | 554 |
| 554 void ProcessManager::OnSuspendAck(const std::string& extension_id) { | 555 void ProcessManager::OnSuspendAck(const std::string& extension_id) { |
| 555 background_page_data_[extension_id].is_closing = true; | 556 background_page_data_[extension_id].is_closing = true; |
| 556 uint64 sequence_id = background_page_data_[extension_id].close_sequence_id; | 557 uint64_t sequence_id = background_page_data_[extension_id].close_sequence_id; |
| 557 base::MessageLoop::current()->PostDelayedTask( | 558 base::MessageLoop::current()->PostDelayedTask( |
| 558 FROM_HERE, | 559 FROM_HERE, |
| 559 base::Bind(&ProcessManager::CloseLazyBackgroundPageNow, | 560 base::Bind(&ProcessManager::CloseLazyBackgroundPageNow, |
| 560 weak_ptr_factory_.GetWeakPtr(), | 561 weak_ptr_factory_.GetWeakPtr(), |
| 561 extension_id, | 562 extension_id, |
| 562 sequence_id), | 563 sequence_id), |
| 563 base::TimeDelta::FromMilliseconds(g_event_page_suspending_time_msec)); | 564 base::TimeDelta::FromMilliseconds(g_event_page_suspending_time_msec)); |
| 564 } | 565 } |
| 565 | 566 |
| 566 void ProcessManager::OnNetworkRequestStarted( | 567 void ProcessManager::OnNetworkRequestStarted( |
| 567 content::RenderFrameHost* render_frame_host, | 568 content::RenderFrameHost* render_frame_host, |
| 568 uint64 request_id) { | 569 uint64_t request_id) { |
| 569 ExtensionHost* host = GetBackgroundHostForExtension( | 570 ExtensionHost* host = GetBackgroundHostForExtension( |
| 570 GetExtensionID(render_frame_host)); | 571 GetExtensionID(render_frame_host)); |
| 571 auto result = pending_network_requests_.insert(request_id); | 572 auto result = pending_network_requests_.insert(request_id); |
| 572 DCHECK(result.second) << "Duplicate network request IDs."; | 573 DCHECK(result.second) << "Duplicate network request IDs."; |
| 573 if (host && IsFrameInExtensionHost(host, render_frame_host)) { | 574 if (host && IsFrameInExtensionHost(host, render_frame_host)) { |
| 574 IncrementLazyKeepaliveCount(host->extension()); | 575 IncrementLazyKeepaliveCount(host->extension()); |
| 575 host->OnNetworkRequestStarted(request_id); | 576 host->OnNetworkRequestStarted(request_id); |
| 576 } | 577 } |
| 577 } | 578 } |
| 578 | 579 |
| 579 void ProcessManager::OnNetworkRequestDone( | 580 void ProcessManager::OnNetworkRequestDone( |
| 580 content::RenderFrameHost* render_frame_host, | 581 content::RenderFrameHost* render_frame_host, |
| 581 uint64 request_id) { | 582 uint64_t request_id) { |
| 582 ExtensionHost* host = GetBackgroundHostForExtension( | 583 ExtensionHost* host = GetBackgroundHostForExtension( |
| 583 GetExtensionID(render_frame_host)); | 584 GetExtensionID(render_frame_host)); |
| 584 if (host && IsFrameInExtensionHost(host, render_frame_host)) { | 585 if (host && IsFrameInExtensionHost(host, render_frame_host)) { |
| 585 host->OnNetworkRequestDone(request_id); | 586 host->OnNetworkRequestDone(request_id); |
| 586 if (pending_network_requests_.erase(request_id)) | 587 if (pending_network_requests_.erase(request_id)) |
| 587 DecrementLazyKeepaliveCount(host->extension()); | 588 DecrementLazyKeepaliveCount(host->extension()); |
| 588 } | 589 } |
| 589 } | 590 } |
| 590 | 591 |
| 591 void ProcessManager::CancelSuspend(const Extension* extension) { | 592 void ProcessManager::CancelSuspend(const Extension* extension) { |
| (...skipping 220 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 812 if (base::MessageLoop::current()) { | 813 if (base::MessageLoop::current()) { |
| 813 base::MessageLoop::current()->PostDelayedTask( | 814 base::MessageLoop::current()->PostDelayedTask( |
| 814 FROM_HERE, | 815 FROM_HERE, |
| 815 base::Bind(&ProcessManager::OnKeepaliveImpulseCheck, | 816 base::Bind(&ProcessManager::OnKeepaliveImpulseCheck, |
| 816 weak_ptr_factory_.GetWeakPtr()), | 817 weak_ptr_factory_.GetWeakPtr()), |
| 817 base::TimeDelta::FromMilliseconds(g_event_page_idle_time_msec)); | 818 base::TimeDelta::FromMilliseconds(g_event_page_idle_time_msec)); |
| 818 } | 819 } |
| 819 } | 820 } |
| 820 | 821 |
| 821 void ProcessManager::OnLazyBackgroundPageIdle(const std::string& extension_id, | 822 void ProcessManager::OnLazyBackgroundPageIdle(const std::string& extension_id, |
| 822 uint64 sequence_id) { | 823 uint64_t sequence_id) { |
| 823 ExtensionHost* host = GetBackgroundHostForExtension(extension_id); | 824 ExtensionHost* host = GetBackgroundHostForExtension(extension_id); |
| 824 if (host && !background_page_data_[extension_id].is_closing && | 825 if (host && !background_page_data_[extension_id].is_closing && |
| 825 sequence_id == background_page_data_[extension_id].close_sequence_id) { | 826 sequence_id == background_page_data_[extension_id].close_sequence_id) { |
| 826 // Tell the renderer we are about to close. This is a simple ping that the | 827 // Tell the renderer we are about to close. This is a simple ping that the |
| 827 // renderer will respond to. The purpose is to control sequencing: if the | 828 // renderer will respond to. The purpose is to control sequencing: if the |
| 828 // extension remains idle until the renderer responds with an ACK, then we | 829 // extension remains idle until the renderer responds with an ACK, then we |
| 829 // know that the extension process is ready to shut down. If our | 830 // know that the extension process is ready to shut down. If our |
| 830 // close_sequence_id has already changed, then we would ignore the | 831 // close_sequence_id has already changed, then we would ignore the |
| 831 // ShouldSuspendAck, so we don't send the ping. | 832 // ShouldSuspendAck, so we don't send the ping. |
| 832 host->render_process_host()->Send(new ExtensionMsg_ShouldSuspend( | 833 host->render_process_host()->Send(new ExtensionMsg_ShouldSuspend( |
| 833 extension_id, sequence_id)); | 834 extension_id, sequence_id)); |
| 834 } | 835 } |
| 835 } | 836 } |
| 836 | 837 |
| 837 void ProcessManager::OnLazyBackgroundPageActive( | 838 void ProcessManager::OnLazyBackgroundPageActive( |
| 838 const std::string& extension_id) { | 839 const std::string& extension_id) { |
| 839 if (!background_page_data_[extension_id].is_closing) { | 840 if (!background_page_data_[extension_id].is_closing) { |
| 840 // Cancel the current close sequence by changing the close_sequence_id, | 841 // Cancel the current close sequence by changing the close_sequence_id, |
| 841 // which causes us to ignore the next ShouldSuspendAck. | 842 // which causes us to ignore the next ShouldSuspendAck. |
| 842 background_page_data_[extension_id].close_sequence_id = | 843 background_page_data_[extension_id].close_sequence_id = |
| 843 ++last_background_close_sequence_id_; | 844 ++last_background_close_sequence_id_; |
| 844 } | 845 } |
| 845 } | 846 } |
| 846 | 847 |
| 847 void ProcessManager::CloseLazyBackgroundPageNow(const std::string& extension_id, | 848 void ProcessManager::CloseLazyBackgroundPageNow(const std::string& extension_id, |
| 848 uint64 sequence_id) { | 849 uint64_t sequence_id) { |
| 849 ExtensionHost* host = GetBackgroundHostForExtension(extension_id); | 850 ExtensionHost* host = GetBackgroundHostForExtension(extension_id); |
| 850 if (host && | 851 if (host && |
| 851 sequence_id == background_page_data_[extension_id].close_sequence_id) { | 852 sequence_id == background_page_data_[extension_id].close_sequence_id) { |
| 852 // Handle the case where the keepalive count was increased after the | 853 // Handle the case where the keepalive count was increased after the |
| 853 // OnSuspend event was sent. | 854 // OnSuspend event was sent. |
| 854 if (background_page_data_[extension_id].lazy_keepalive_count > 0) { | 855 if (background_page_data_[extension_id].lazy_keepalive_count > 0) { |
| 855 CancelSuspend(host->extension()); | 856 CancelSuspend(host->extension()); |
| 856 return; | 857 return; |
| 857 } | 858 } |
| 858 | 859 |
| (...skipping 117 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 976 if (extension && !IncognitoInfo::IsSplitMode(extension)) { | 977 if (extension && !IncognitoInfo::IsSplitMode(extension)) { |
| 977 BrowserContext* original_context = | 978 BrowserContext* original_context = |
| 978 ExtensionsBrowserClient::Get()->GetOriginalContext(browser_context()); | 979 ExtensionsBrowserClient::Get()->GetOriginalContext(browser_context()); |
| 979 return ProcessManager::Get(original_context)->GetSiteInstanceForURL(url); | 980 return ProcessManager::Get(original_context)->GetSiteInstanceForURL(url); |
| 980 } | 981 } |
| 981 | 982 |
| 982 return ProcessManager::GetSiteInstanceForURL(url); | 983 return ProcessManager::GetSiteInstanceForURL(url); |
| 983 } | 984 } |
| 984 | 985 |
| 985 } // namespace extensions | 986 } // namespace extensions |
| OLD | NEW |