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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
fuchsia_web / webengine / browser / message_port.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 "fuchsia_web/webengine/browser/message_port.h"
#include <fuchsia/mem/cpp/fidl.h>
#include <lib/fidl/cpp/binding.h>
#include <lib/fit/function.h>
#include <lib/fpromise/result.h>
#include <stdint.h>
#include <memory>
#include <string>
#include <utility>
#include <vector>
#include "base/check.h"
#include "base/containers/circular_deque.h"
#include "base/fuchsia/fuchsia_logging.h"
#include "base/fuchsia/mem_buffer_util.h"
#include "base/functional/bind.h"
#include "base/stl_util.h"
#include "base/strings/utf_string_conversions.h"
#include "base/task/single_thread_task_runner.h"
namespace {
using BlinkMessage = blink::WebMessagePort::Message;
// Converts a fuchsia::web::WebMessage to a BlinkMessage.
// An empty result indicates that conversion was successful.
// Data validation errors are returned as a FrameError.
std::optional<fuchsia::web::FrameError> BlinkMessageFromFidl(
fuchsia::web::WebMessage fidl_message,
BlinkMessage* blink_message) {
if (!fidl_message.has_data()) {
return fuchsia::web::FrameError::NO_DATA_IN_MESSAGE;
}
std::optional<std::u16string> data_utf16 =
base::ReadUTF8FromVMOAsUTF16(fidl_message.data());
if (!data_utf16) {
return fuchsia::web::FrameError::BUFFER_NOT_UTF8;
}
blink_message->data = std::move(*data_utf16);
if (fidl_message.has_outgoing_transfer() &&
fidl_message.has_incoming_transfer()) {
DLOG(WARNING) << "WebMessage may only have incoming or outgoing transfer.";
return fuchsia::web::FrameError::INTERNAL_ERROR;
}
if (fidl_message.has_outgoing_transfer()) {
for (fuchsia::web::OutgoingTransferable& transferrable :
*fidl_message.mutable_outgoing_transfer()) {
if (!transferrable.is_message_port())
return fuchsia::web::FrameError::INTERNAL_ERROR;
blink_message->ports.push_back(
BlinkMessagePortFromFidl(std::move(transferrable.message_port())));
}
} else if (fidl_message.has_incoming_transfer()) {
for (fuchsia::web::IncomingTransferable& transferrable :
*fidl_message.mutable_incoming_transfer()) {
if (!transferrable.is_message_port())
return fuchsia::web::FrameError::INTERNAL_ERROR;
blink_message->ports.push_back(
BlinkMessagePortFromFidl(std::move(transferrable.message_port())));
}
}
return std::nullopt;
}
// Defines a MessagePortAdapter, which translates and routes messages between a
// FIDL MessagePort and a blink::WebMessagePort. Every MessagePortAdapter has
// exactly one FIDL MessagePort and one blink::WebMessagePort.
//
// MessagePortAdapter instances are self-managed; they destroy themselves when
// the connection is terminated from either the Blink or FIDL side.
class MessagePortAdapter : public blink::WebMessagePort::MessageReceiver {
public:
MessagePortAdapter(const MessagePortAdapter&) = delete;
MessagePortAdapter& operator=(const MessagePortAdapter&) = delete;
protected:
explicit MessagePortAdapter(blink::WebMessagePort blink_port)
: blink_port_(std::move(blink_port)) {
blink_port_.SetReceiver(this,
base::SingleThreadTaskRunner::GetCurrentDefault());
}
~MessagePortAdapter() override = default;
// Deletes |this|, implicitly disconnecting the FIDL and Blink ports.
void Destroy() { delete this; }
// Sends a message to |blink_port_|.
void SendBlinkMessage(BlinkMessage message) {
CHECK(blink_port_.PostMessage(std::move(message)));
}
// Called when a Blink message was received through |blink_port_|.
virtual void DeliverMessageToFidl() = 0;
// Returns the next messagefrom Blink, or an empty value if there
// are no more messages in the incoming queue.
std::optional<fuchsia::web::WebMessage> GetNextBlinkMessage() {
if (message_queue_.empty())
return std::nullopt;
return std::move(message_queue_.front());
}
void OnDeliverMessageToFidlComplete() {
DCHECK(!message_queue_.empty());
message_queue_.pop_front();
}
private:
// blink::WebMessagePort::MessageReceiver implementation:
bool OnMessage(BlinkMessage message) override {
std::optional<fuchsia::web::WebMessage> message_converted =
FidlWebMessageFromBlink(std::move(message),
TransferableHostType::kLocal);
if (!message_converted) {
DLOG(ERROR) << "Couldn't decode WebMessage from blink::WebMessagePort.";
Destroy();
return false;
}
message_queue_.emplace_back(std::move(*message_converted));
// Start draining the queue if it was empty beforehand.
if (message_queue_.size() == 1u)
DeliverMessageToFidl();
return true;
}
// blink::WebMessagePort::MessageReceiver implementation:
void OnPipeError() override { Destroy(); }
base::circular_deque<fuchsia::web::WebMessage> message_queue_;
blink::WebMessagePort blink_port_;
};
// Binds a handle to a remote MessagePort to a blink::WebMessagePort.
class FidlMessagePortClientAdapter : public MessagePortAdapter {
public:
FidlMessagePortClientAdapter(
blink::WebMessagePort blink_port,
fidl::InterfaceHandle<fuchsia::web::MessagePort> fidl_port)
: MessagePortAdapter(std::move(blink_port)), port_(fidl_port.Bind()) {
ReadMessageFromFidl();
port_.set_error_handler([this](zx_status_t status) {
ZX_LOG_IF(ERROR,
status != ZX_ERR_PEER_CLOSED && status != ZX_ERR_CANCELED,
status)
<< " MessagePort disconnected.";
Destroy();
});
}
FidlMessagePortClientAdapter(const FidlMessagePortClientAdapter&) = delete;
FidlMessagePortClientAdapter& operator=(const FidlMessagePortClientAdapter&) =
delete;
fidl::InterfaceRequest<fuchsia::web::MessagePort> NewRequest() {
return port_.NewRequest();
}
private:
~FidlMessagePortClientAdapter() override = default;
void ReadMessageFromFidl() {
port_->ReceiveMessage(fit::bind_member(
this, &FidlMessagePortClientAdapter::OnMessageReceived));
}
void OnMessageReceived(fuchsia::web::WebMessage message) {
BlinkMessage blink_message;
std::optional<fuchsia::web::FrameError> result =
BlinkMessageFromFidl(std::move(message), &blink_message);
if (result) {
LOG(WARNING) << "Received bad message, error: "
<< static_cast<int32_t>(*result);
Destroy();
return;
}
SendBlinkMessage(std::move(blink_message));
ReadMessageFromFidl();
}
void OnMessagePosted(fuchsia::web::MessagePort_PostMessage_Result result) {
if (result.is_err()) {
LOG(ERROR) << "PostMessage failed, reason: "
<< static_cast<int32_t>(result.err());
Destroy();
return;
}
DeliverMessageToFidl();
}
// MessagePortAdapter implementation.
void DeliverMessageToFidl() override {
std::optional<fuchsia::web::WebMessage> message = GetNextBlinkMessage();
if (!message)
return;
port_->PostMessage(
std::move(*message),
fit::bind_member(this, &FidlMessagePortClientAdapter::OnMessagePosted));
OnDeliverMessageToFidlComplete();
}
fuchsia::web::MessagePortPtr port_;
};
// Binds a MessagePort FIDL service from a blink::WebMessagePort.
class FidlMessagePortServerAdapter : public fuchsia::web::MessagePort,
public MessagePortAdapter {
public:
explicit FidlMessagePortServerAdapter(blink::WebMessagePort blink_port)
: MessagePortAdapter(std::move(blink_port)), binding_(this) {
binding_.set_error_handler([this](zx_status_t status) {
ZX_LOG_IF(ERROR,
status != ZX_ERR_PEER_CLOSED && status != ZX_ERR_CANCELED,
status)
<< " MessagePort disconnected.";
Destroy();
});
}
FidlMessagePortServerAdapter(
blink::WebMessagePort blink_port,
fidl::InterfaceRequest<fuchsia::web::MessagePort> request)
: FidlMessagePortServerAdapter(std::move(blink_port)) {
binding_.Bind(std::move(request));
}
FidlMessagePortServerAdapter(const FidlMessagePortServerAdapter&) = delete;
FidlMessagePortServerAdapter& operator=(const FidlMessagePortServerAdapter&) =
delete;
fidl::InterfaceHandle<fuchsia::web::MessagePort> NewBinding() {
return binding_.NewBinding();
}
private:
~FidlMessagePortServerAdapter() override = default;
// MessagePortAdapter implementation.
void DeliverMessageToFidl() override {
// Do nothing if the client hasn't requested a read, or if there's nothing
// to read.
if (!pending_receive_message_callback_)
return;
std::optional<fuchsia::web::WebMessage> message = GetNextBlinkMessage();
if (!message)
return;
pending_receive_message_callback_(std::move(*message));
pending_receive_message_callback_ = {};
OnDeliverMessageToFidlComplete();
}
// fuchsia::web::MessagePort implementation.
void PostMessage(fuchsia::web::WebMessage message,
PostMessageCallback callback) override {
BlinkMessage blink_message;
std::optional<fuchsia::web::FrameError> status =
BlinkMessageFromFidl(std::move(message), &blink_message);
if (status) {
LOG(ERROR) << "Error when reading message from FIDL: "
<< static_cast<int32_t>(*status);
Destroy();
return;
}
SendBlinkMessage(std::move(blink_message));
callback(fpromise::ok());
}
void ReceiveMessage(ReceiveMessageCallback callback) override {
if (pending_receive_message_callback_) {
LOG(WARNING)
<< "ReceiveMessage called multiple times without acknowledgement.";
Destroy();
return;
}
pending_receive_message_callback_ = std::move(callback);
DeliverMessageToFidl();
}
PostMessageCallback post_message_ack_;
ReceiveMessageCallback pending_receive_message_callback_;
fidl::Binding<fuchsia::web::MessagePort> binding_;
};
fidl::InterfaceRequest<fuchsia::web::MessagePort>
RemoteFidlMessagePortFromBlink(blink::WebMessagePort blink_port) {
fidl::InterfaceHandle<fuchsia::web::MessagePort> fidl_handle;
auto request = fidl_handle.NewRequest();
new FidlMessagePortClientAdapter(std::move(blink_port),
std::move(fidl_handle));
return request;
}
} // namespace
// Methods for constructing MessagePortAdapters for various port types and
// origins. The adapters manage their own lifetimes and will self-delete when
// either endpoint of their channels are disconnected.
blink::WebMessagePort BlinkMessagePortFromFidl(
fidl::InterfaceRequest<fuchsia::web::MessagePort> fidl_port) {
auto port_pair = blink::WebMessagePort::CreatePair();
new FidlMessagePortServerAdapter(std::move(port_pair.first),
std::move(fidl_port));
return std::move(port_pair.second);
}
blink::WebMessagePort BlinkMessagePortFromFidl(
fidl::InterfaceHandle<fuchsia::web::MessagePort> fidl_port) {
auto port_pair = blink::WebMessagePort::CreatePair();
new FidlMessagePortClientAdapter(std::move(port_pair.first),
std::move(fidl_port));
return std::move(port_pair.second);
}
fidl::InterfaceHandle<fuchsia::web::MessagePort> FidlMessagePortFromBlink(
blink::WebMessagePort blink_port) {
auto* adapter = new FidlMessagePortServerAdapter(std::move(blink_port));
return adapter->NewBinding();
}
std::optional<fuchsia::web::WebMessage> FidlWebMessageFromBlink(
BlinkMessage blink_message,
TransferableHostType port_type) {
fuchsia::web::WebMessage fidl_message;
if (!blink_message.ports.empty()) {
switch (port_type) {
case TransferableHostType::kLocal:
for (blink::WebMessagePort& port : blink_message.ports) {
fuchsia::web::IncomingTransferable incoming;
incoming.set_message_port(FidlMessagePortFromBlink(std::move(port)));
fidl_message.mutable_incoming_transfer()->push_back(
std::move(incoming));
}
break;
case TransferableHostType::kRemote:
for (blink::WebMessagePort& port : blink_message.ports) {
fuchsia::web::OutgoingTransferable outgoing;
outgoing.set_message_port(
RemoteFidlMessagePortFromBlink(std::move(port)));
fidl_message.mutable_outgoing_transfer()->push_back(
std::move(outgoing));
}
break;
}
blink_message.ports.clear();
}
std::u16string data_utf16 = std::move(blink_message.data);
std::string data_utf8;
if (!base::UTF16ToUTF8(data_utf16.data(), data_utf16.size(), &data_utf8))
return std::nullopt;
base::STLClearObject(&data_utf16);
constexpr char kBufferVmoName[] = "cr-web-message-from-blink";
fuchsia::mem::Buffer data_buffer =
base::MemBufferFromString(data_utf8, kBufferVmoName);
if (!data_buffer.vmo)
return std::nullopt;
fidl_message.set_data(std::move(data_buffer));
return fidl_message;
}