blob: 068c8ec250dec7d8d03c6f10030bc282371aac82 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
|
// Copyright 2017 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "content/browser/frame_host/keep_alive_handle_factory.h"
#include "base/metrics/field_trial.h"
#include "base/metrics/field_trial_params.h"
#include "content/public/browser/browser_thread.h"
#include "content/public/browser/render_process_host.h"
#include "content/public/common/content_features.h"
#include "mojo/public/cpp/bindings/interface_request.h"
#include "mojo/public/cpp/bindings/strong_binding_set.h"
namespace content {
class KeepAliveHandleFactory::Context final : public base::RefCounted<Context> {
public:
explicit Context(int process_id)
: process_id_(process_id), weak_ptr_factory_(this) {
RenderProcessHost* process_host = RenderProcessHost::FromID(process_id_);
if (!process_host || process_host->IsKeepAliveRefCountDisabled())
return;
process_host->IncrementKeepAliveRefCount(
RenderProcessHost::KeepAliveClientType::kFetch);
}
void Detach() {
if (detached_)
return;
detached_ = true;
RenderProcessHost* process_host = RenderProcessHost::FromID(process_id_);
if (!process_host || process_host->IsKeepAliveRefCountDisabled())
return;
process_host->DecrementKeepAliveRefCount(
RenderProcessHost::KeepAliveClientType::kFetch);
}
void DetachLater(base::TimeDelta timeout) {
DCHECK_CURRENTLY_ON(BrowserThread::UI);
BrowserThread::PostDelayedTask(
BrowserThread::UI, FROM_HERE,
base::BindOnce(&Context::Detach, AsWeakPtr()), timeout);
}
void AddBinding(std::unique_ptr<mojom::KeepAliveHandle> impl,
mojom::KeepAliveHandleRequest request) {
binding_set_.AddBinding(std::move(impl), std::move(request));
}
base::WeakPtr<Context> AsWeakPtr() { return weak_ptr_factory_.GetWeakPtr(); }
private:
friend class base::RefCounted<Context>;
~Context() { Detach(); }
mojo::StrongBindingSet<mojom::KeepAliveHandle> binding_set_;
const int process_id_;
bool detached_ = false;
base::WeakPtrFactory<Context> weak_ptr_factory_;
DISALLOW_COPY_AND_ASSIGN(Context);
};
class KeepAliveHandleFactory::KeepAliveHandleImpl final
: public mojom::KeepAliveHandle {
public:
explicit KeepAliveHandleImpl(scoped_refptr<Context> context)
: context_(std::move(context)) {}
~KeepAliveHandleImpl() override {}
private:
scoped_refptr<Context> context_;
DISALLOW_COPY_AND_ASSIGN(KeepAliveHandleImpl);
};
KeepAliveHandleFactory::KeepAliveHandleFactory(RenderProcessHost* process_host)
: process_id_(process_host->GetID()) {}
KeepAliveHandleFactory::~KeepAliveHandleFactory() {
if (context_)
context_->DetachLater(timeout_);
}
void KeepAliveHandleFactory::Create(mojom::KeepAliveHandleRequest request) {
scoped_refptr<Context> context;
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) {
timeout_ = timeout;
}
} // namespace content
|