forked from chromium/chromium
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserial_service.cc
214 lines (177 loc) · 7.17 KB
/
serial_service.cc
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
// Copyright 2018 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/serial/serial_service.h"
#include <utility>
#include "base/bind.h"
#include "base/callback.h"
#include "content/browser/renderer_host/back_forward_cache_disable.h"
#include "content/browser/web_contents/web_contents_impl.h"
#include "content/public/browser/back_forward_cache.h"
#include "content/public/browser/content_browser_client.h"
#include "content/public/browser/render_frame_host.h"
#include "content/public/browser/serial_chooser.h"
#include "content/public/browser/serial_delegate.h"
#include "content/public/browser/web_contents.h"
#include "content/public/common/content_client.h"
#include "mojo/public/cpp/bindings/pending_remote.h"
#include "third_party/blink/public/mojom/permissions_policy/permissions_policy.mojom.h"
namespace content {
namespace {
blink::mojom::SerialPortInfoPtr ToBlinkType(
const device::mojom::SerialPortInfo& port) {
auto info = blink::mojom::SerialPortInfo::New();
info->token = port.token;
info->has_usb_vendor_id = port.has_vendor_id;
if (port.has_vendor_id)
info->usb_vendor_id = port.vendor_id;
info->has_usb_product_id = port.has_product_id;
if (port.has_product_id)
info->usb_product_id = port.product_id;
return info;
}
} // namespace
SerialService::SerialService(RenderFrameHost* rfh)
: DocumentUserData<SerialService>(rfh) {
DCHECK(render_frame_host().IsFeatureEnabled(
blink::mojom::PermissionsPolicyFeature::kSerial));
// Serial API is not supported for back-forward cache for now because we
// don't have support for closing/freezing ports when the frame is added to
// the back-forward cache, so we mark frames that use this API as disabled
// for back-forward cache.
BackForwardCache::DisableForRenderFrameHost(
&render_frame_host(),
BackForwardCacheDisable::DisabledReason(
BackForwardCacheDisable::DisabledReasonId::kSerial));
watchers_.set_disconnect_handler(base::BindRepeating(
&SerialService::OnWatcherConnectionError, base::Unretained(this)));
SerialDelegate* delegate = GetContentClient()->browser()->GetSerialDelegate();
if (delegate)
delegate->AddObserver(&render_frame_host(), this);
}
SerialService::~SerialService() {
SerialDelegate* delegate = GetContentClient()->browser()->GetSerialDelegate();
if (delegate)
delegate->RemoveObserver(&render_frame_host(), this);
// The remaining watchers will be closed from this end.
if (!watchers_.empty())
DecrementActiveFrameCount();
}
void SerialService::Bind(
mojo::PendingReceiver<blink::mojom::SerialService> receiver) {
receivers_.Add(this, std::move(receiver));
}
void SerialService::SetClient(
mojo::PendingRemote<blink::mojom::SerialServiceClient> client) {
clients_.Add(std::move(client));
}
void SerialService::GetPorts(GetPortsCallback callback) {
SerialDelegate* delegate = GetContentClient()->browser()->GetSerialDelegate();
if (!delegate) {
std::move(callback).Run(std::vector<blink::mojom::SerialPortInfoPtr>());
return;
}
delegate->GetPortManager(&render_frame_host())
->GetDevices(base::BindOnce(&SerialService::FinishGetPorts,
weak_factory_.GetWeakPtr(),
std::move(callback)));
}
void SerialService::RequestPort(
std::vector<blink::mojom::SerialPortFilterPtr> filters,
RequestPortCallback callback) {
SerialDelegate* delegate = GetContentClient()->browser()->GetSerialDelegate();
if (!delegate) {
std::move(callback).Run(nullptr);
return;
}
if (!delegate->CanRequestPortPermission(&render_frame_host())) {
std::move(callback).Run(nullptr);
return;
}
chooser_ = delegate->RunChooser(
&render_frame_host(), std::move(filters),
base::BindOnce(&SerialService::FinishRequestPort,
weak_factory_.GetWeakPtr(), std::move(callback)));
}
void SerialService::OpenPort(
const base::UnguessableToken& token,
device::mojom::SerialConnectionOptionsPtr options,
mojo::PendingRemote<device::mojom::SerialPortClient> client,
OpenPortCallback callback) {
SerialDelegate* delegate = GetContentClient()->browser()->GetSerialDelegate();
if (!delegate) {
std::move(callback).Run(mojo::NullRemote());
return;
}
if (watchers_.empty()) {
auto* web_contents_impl = static_cast<WebContentsImpl*>(
WebContents::FromRenderFrameHost(&render_frame_host()));
web_contents_impl->IncrementSerialActiveFrameCount();
}
mojo::PendingRemote<device::mojom::SerialPortConnectionWatcher> watcher;
watchers_.Add(this, watcher.InitWithNewPipeAndPassReceiver());
delegate->GetPortManager(&render_frame_host())
->OpenPort(token, /*use_alternate_path=*/false, std::move(options),
std::move(client), std::move(watcher), std::move(callback));
}
void SerialService::OnPortAdded(const device::mojom::SerialPortInfo& port) {
SerialDelegate* delegate = GetContentClient()->browser()->GetSerialDelegate();
if (!delegate || !delegate->HasPortPermission(&render_frame_host(), port))
return;
for (const auto& client : clients_)
client->OnPortAdded(ToBlinkType(port));
}
void SerialService::OnPortRemoved(const device::mojom::SerialPortInfo& port) {
SerialDelegate* delegate = GetContentClient()->browser()->GetSerialDelegate();
if (!delegate || !delegate->HasPortPermission(&render_frame_host(), port))
return;
for (const auto& client : clients_)
client->OnPortRemoved(ToBlinkType(port));
}
void SerialService::OnPortManagerConnectionError() {
// Reflect the loss of the SerialPortManager connection into the renderer
// in order to force caches to be cleared and connections to be
// re-established.
receivers_.Clear();
clients_.Clear();
chooser_.reset();
if (!watchers_.empty()) {
watchers_.Clear();
DecrementActiveFrameCount();
}
}
void SerialService::FinishGetPorts(
GetPortsCallback callback,
std::vector<device::mojom::SerialPortInfoPtr> ports) {
std::vector<blink::mojom::SerialPortInfoPtr> result;
SerialDelegate* delegate = GetContentClient()->browser()->GetSerialDelegate();
if (!delegate) {
std::move(callback).Run(std::move(result));
return;
}
for (const auto& port : ports) {
if (delegate->HasPortPermission(&render_frame_host(), *port))
result.push_back(ToBlinkType(*port));
}
std::move(callback).Run(std::move(result));
}
void SerialService::FinishRequestPort(RequestPortCallback callback,
device::mojom::SerialPortInfoPtr port) {
SerialDelegate* delegate = GetContentClient()->browser()->GetSerialDelegate();
if (!delegate || !port) {
std::move(callback).Run(nullptr);
return;
}
std::move(callback).Run(ToBlinkType(*port));
}
void SerialService::OnWatcherConnectionError() {
if (watchers_.empty())
DecrementActiveFrameCount();
}
void SerialService::DecrementActiveFrameCount() {
auto* web_contents_impl = static_cast<WebContentsImpl*>(
WebContents::FromRenderFrameHost(&render_frame_host()));
web_contents_impl->DecrementSerialActiveFrameCount();
}
DOCUMENT_USER_DATA_KEY_IMPL(SerialService);
} // namespace content