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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
content / browser / tracing / tracing_service_controller.cc [blame]
// Copyright 2019 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "content/browser/tracing/tracing_service_controller.h"
#include <utility>
#include "base/no_destructor.h"
#include "base/task/thread_pool.h"
#include "content/public/browser/browser_task_traits.h"
#include "content/public/browser/browser_thread.h"
#include "content/public/browser/service_process_host.h"
#include "content/public/browser/tracing_service.h"
#include "mojo/public/cpp/bindings/pending_receiver.h"
#include "mojo/public/cpp/bindings/remote.h"
#include "mojo/public/cpp/bindings/self_owned_receiver.h"
#include "services/tracing/public/cpp/traced_process.h"
#include "services/tracing/public/cpp/tracing_features.h"
#include "services/tracing/public/mojom/tracing_service.mojom.h"
#include "services/tracing/tracing_service.h"
namespace content {
namespace {
void BindNewInProcessInstance(
mojo::PendingReceiver<tracing::mojom::TracingService> receiver) {
mojo::MakeSelfOwnedReceiver(std::make_unique<tracing::TracingService>(),
std::move(receiver));
}
} // namespace
TracingServiceController::ClientRegistration::ClientRegistration(
base::PassKey<TracingServiceController>,
base::OnceClosure unregister)
: unregister_(std::move(unregister)) {}
TracingServiceController::ClientRegistration::~ClientRegistration() {
std::move(unregister_).Run();
}
TracingServiceController::TracingServiceController() = default;
TracingServiceController::~TracingServiceController() = default;
// static
TracingServiceController& TracingServiceController::Get() {
static base::NoDestructor<TracingServiceController> controller;
return *controller;
}
std::unique_ptr<TracingServiceController::ClientRegistration>
TracingServiceController::RegisterClient(base::ProcessId pid,
EnableTracingCallback callback) {
base::OnceClosure unregister =
base::BindOnce(&TracingServiceController::RemoveClient,
base::Unretained(&TracingServiceController::Get()), pid);
auto registration = std::make_unique<ClientRegistration>(
base::PassKey<TracingServiceController>(), std::move(unregister));
if (!BrowserThread::CurrentlyOn(BrowserThread::UI)) {
// Force registration to happen on the UI thread.
GetUIThreadTaskRunner({})->PostTask(
FROM_HERE,
base::BindOnce(&TracingServiceController::RegisterClientOnUIThread,
base::Unretained(this), pid, std::move(callback)));
} else {
RegisterClientOnUIThread(pid, std::move(callback));
}
return registration;
}
tracing::mojom::TracingService& TracingServiceController::GetService() {
DCHECK_CURRENTLY_ON(BrowserThread::UI);
if (!service_) {
auto receiver = service_.BindNewPipeAndPassReceiver();
if (base::FeatureList::IsEnabled(features::kTracingServiceInProcess)) {
base::ThreadPool::CreateSequencedTaskRunner(
{base::MayBlock(), base::TaskShutdownBehavior::SKIP_ON_SHUTDOWN,
base::WithBaseSyncPrimitives(), base::TaskPriority::USER_BLOCKING})
->PostTask(FROM_HERE, base::BindOnce(&BindNewInProcessInstance,
std::move(receiver)));
} else {
ServiceProcessHost::Launch(
std::move(receiver),
ServiceProcessHost::Options()
.WithDisplayName("Tracing Service")
.Pass());
}
service_.reset_on_disconnect();
// Initialize the new service instance by pushing a pipe to each currently
// registered client, including the browser process itself.
std::vector<tracing::mojom::ClientInfoPtr> initial_clients;
mojo::PendingRemote<tracing::mojom::TracedProcess> browser_remote;
tracing::TracedProcess::ResetTracedProcessReceiver();
tracing::TracedProcess::OnTracedProcessRequest(
browser_remote.InitWithNewPipeAndPassReceiver());
initial_clients.push_back(tracing::mojom::ClientInfo::New(
base::GetCurrentProcId(), std::move(browser_remote)));
for (const std::pair<const base::ProcessId, EnableTracingCallback>& entry :
clients_) {
mojo::PendingRemote<tracing::mojom::TracedProcess> remote_process;
entry.second.Run(remote_process.InitWithNewPipeAndPassReceiver());
initial_clients.push_back(tracing::mojom::ClientInfo::New(
/*pid=*/entry.first, std::move(remote_process)));
}
service_->Initialize(std::move(initial_clients));
}
return *service_.get();
}
void TracingServiceController::RegisterClientOnUIThread(
base::ProcessId pid,
EnableTracingCallback callback) {
DCHECK_CURRENTLY_ON(BrowserThread::UI);
// If the service is currently running, immediately connect the new client.
if (service_) {
mojo::PendingRemote<tracing::mojom::TracedProcess> remote_process;
callback.Run(remote_process.InitWithNewPipeAndPassReceiver());
service_->AddClient(
tracing::mojom::ClientInfo::New(pid, std::move(remote_process)));
}
clients_.emplace(pid, std::move(callback));
}
void TracingServiceController::RemoveClient(base::ProcessId pid) {
if (!BrowserThread::CurrentlyOn(BrowserThread::UI)) {
GetUIThreadTaskRunner({})->PostTask(
FROM_HERE, base::BindOnce(&TracingServiceController::RemoveClient,
base::Unretained(this), pid));
return;
}
DCHECK_CURRENTLY_ON(BrowserThread::UI);
clients_.erase(pid);
}
tracing::mojom::TracingService& GetTracingService() {
DCHECK_CURRENTLY_ON(BrowserThread::UI);
return TracingServiceController::Get().GetService();
}
} // namespace content