Commit c1b10f97 authored by Yutaka Hirano's avatar Yutaka Hirano Committed by Commit Bot

Decrement RenderProcessHost's keep alive refcount eagerly

Having a non-zero keep alive refcount prevents the task queue throttling
even when the page is in background. This CL avoids that with calling
RenderProcessHost::DecrementKeepAliveRefCount eagerly.

Bug: 831853
Change-Id: I79562153286aee08d65520de0bf48eb8087db961
Reviewed-on: https://chromium-review.googlesource.com/1023628Reviewed-by: default avatarKinuko Yasuda <kinuko@chromium.org>
Reviewed-by: default avatarShubhie Panicker <panicker@chromium.org>
Commit-Queue: Yutaka Hirano <yhirano@chromium.org>
Cr-Commit-Position: refs/heads/master@{#553477}
parent 82979c0f
...@@ -4,10 +4,8 @@ ...@@ -4,10 +4,8 @@
#include "content/browser/frame_host/keep_alive_handle_factory.h" #include "content/browser/frame_host/keep_alive_handle_factory.h"
#include "base/memory/weak_ptr.h"
#include "base/metrics/field_trial.h" #include "base/metrics/field_trial.h"
#include "base/metrics/field_trial_params.h" #include "base/metrics/field_trial_params.h"
#include "base/time/time.h"
#include "content/public/browser/browser_thread.h" #include "content/public/browser/browser_thread.h"
#include "content/public/browser/render_process_host.h" #include "content/public/browser/render_process_host.h"
#include "content/public/common/content_features.h" #include "content/public/common/content_features.h"
...@@ -18,9 +16,11 @@ namespace content { ...@@ -18,9 +16,11 @@ namespace content {
class KeepAliveHandleFactory::Context final : public base::RefCounted<Context> { class KeepAliveHandleFactory::Context final : public base::RefCounted<Context> {
public: public:
explicit Context(RenderProcessHost* process_host) explicit Context(int process_id)
: process_id_(process_host->GetID()), weak_ptr_factory_(this) { : process_id_(process_id), weak_ptr_factory_(this) {
DCHECK(!process_host->IsKeepAliveRefCountDisabled()); RenderProcessHost* process_host = RenderProcessHost::FromID(process_id_);
if (!process_host || process_host->IsKeepAliveRefCountDisabled())
return;
process_host->IncrementKeepAliveRefCount( process_host->IncrementKeepAliveRefCount(
RenderProcessHost::KeepAliveClientType::kFetch); RenderProcessHost::KeepAliveClientType::kFetch);
} }
...@@ -37,12 +37,11 @@ class KeepAliveHandleFactory::Context final : public base::RefCounted<Context> { ...@@ -37,12 +37,11 @@ class KeepAliveHandleFactory::Context final : public base::RefCounted<Context> {
RenderProcessHost::KeepAliveClientType::kFetch); RenderProcessHost::KeepAliveClientType::kFetch);
} }
void DetachLater() { void DetachLater(base::TimeDelta timeout) {
DCHECK_CURRENTLY_ON(BrowserThread::UI); DCHECK_CURRENTLY_ON(BrowserThread::UI);
BrowserThread::PostDelayedTask( BrowserThread::PostDelayedTask(
BrowserThread::UI, FROM_HERE, BrowserThread::UI, FROM_HERE,
base::BindOnce(&Context::Detach, weak_ptr_factory_.GetWeakPtr()), base::BindOnce(&Context::Detach, AsWeakPtr()), timeout);
timeout_);
} }
void AddBinding(std::unique_ptr<mojom::KeepAliveHandle> impl, void AddBinding(std::unique_ptr<mojom::KeepAliveHandle> impl,
...@@ -50,7 +49,7 @@ class KeepAliveHandleFactory::Context final : public base::RefCounted<Context> { ...@@ -50,7 +49,7 @@ class KeepAliveHandleFactory::Context final : public base::RefCounted<Context> {
binding_set_.AddBinding(std::move(impl), std::move(request)); binding_set_.AddBinding(std::move(impl), std::move(request));
} }
void set_timeout(base::TimeDelta timeout) { timeout_ = timeout; } base::WeakPtr<Context> AsWeakPtr() { return weak_ptr_factory_.GetWeakPtr(); }
private: private:
friend class base::RefCounted<Context>; friend class base::RefCounted<Context>;
...@@ -59,7 +58,6 @@ class KeepAliveHandleFactory::Context final : public base::RefCounted<Context> { ...@@ -59,7 +58,6 @@ class KeepAliveHandleFactory::Context final : public base::RefCounted<Context> {
mojo::StrongBindingSet<mojom::KeepAliveHandle> binding_set_; mojo::StrongBindingSet<mojom::KeepAliveHandle> binding_set_;
const int process_id_; const int process_id_;
bool detached_ = false; bool detached_ = false;
base::TimeDelta timeout_;
base::WeakPtrFactory<Context> weak_ptr_factory_; base::WeakPtrFactory<Context> weak_ptr_factory_;
...@@ -80,19 +78,28 @@ class KeepAliveHandleFactory::KeepAliveHandleImpl final ...@@ -80,19 +78,28 @@ class KeepAliveHandleFactory::KeepAliveHandleImpl final
}; };
KeepAliveHandleFactory::KeepAliveHandleFactory(RenderProcessHost* process_host) KeepAliveHandleFactory::KeepAliveHandleFactory(RenderProcessHost* process_host)
: context_(base::MakeRefCounted<Context>(process_host)) {} : process_id_(process_host->GetID()) {}
KeepAliveHandleFactory::~KeepAliveHandleFactory() { KeepAliveHandleFactory::~KeepAliveHandleFactory() {
context_->DetachLater(); if (context_)
context_->DetachLater(timeout_);
} }
void KeepAliveHandleFactory::Create(mojom::KeepAliveHandleRequest request) { void KeepAliveHandleFactory::Create(mojom::KeepAliveHandleRequest request) {
context_->AddBinding(std::make_unique<KeepAliveHandleImpl>(context_), scoped_refptr<Context> context;
std::move(request)); if (context_) {
context = context_.get();
} else {
context = base::MakeRefCounted<Context>(process_id_);
context_ = context->AsWeakPtr();
}
context->AddBinding(std::make_unique<KeepAliveHandleImpl>(context),
std::move(request));
} }
void KeepAliveHandleFactory::SetTimeout(base::TimeDelta timeout) { void KeepAliveHandleFactory::SetTimeout(base::TimeDelta timeout) {
context_->set_timeout(timeout); timeout_ = timeout;
} }
} // namespace content } // namespace content
...@@ -6,6 +6,8 @@ ...@@ -6,6 +6,8 @@
#define CONTENT_BROWSER_FRAME_HOST_KEEP_ALIVE_HANDLE_FACTORY_H_ #define CONTENT_BROWSER_FRAME_HOST_KEEP_ALIVE_HANDLE_FACTORY_H_
#include "base/memory/ref_counted.h" #include "base/memory/ref_counted.h"
#include "base/memory/weak_ptr.h"
#include "base/time/time.h"
#include "content/common/frame.mojom.h" #include "content/common/frame.mojom.h"
namespace content { namespace content {
...@@ -32,7 +34,9 @@ class KeepAliveHandleFactory final { ...@@ -32,7 +34,9 @@ class KeepAliveHandleFactory final {
class KeepAliveHandleImpl; class KeepAliveHandleImpl;
class Context; class Context;
scoped_refptr<Context> context_; const int process_id_;
base::TimeDelta timeout_;
base::WeakPtr<Context> context_;
DISALLOW_COPY_AND_ASSIGN(KeepAliveHandleFactory); DISALLOW_COPY_AND_ASSIGN(KeepAliveHandleFactory);
}; };
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment