File: web_message_port.cc

package info (click to toggle)
chromium 138.0.7204.183-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 6,071,908 kB
  • sloc: cpp: 34,937,088; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,953; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,806; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (266 lines) | stat: -rw-r--r-- 8,260 bytes parent folder | download | duplicates (5)
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
// Copyright 2020 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "third_party/blink/public/common/messaging/web_message_port.h"

#include <variant>

#include "base/memory/ptr_util.h"
#include "base/task/sequenced_task_runner.h"
#include "third_party/blink/public/common/messaging/message_port_channel.h"
#include "third_party/blink/public/common/messaging/string_message_codec.h"
#include "third_party/blink/public/common/messaging/transferable_message.h"
#include "third_party/blink/public/common/messaging/transferable_message_mojom_traits.h"
#include "third_party/blink/public/mojom/blob/blob.mojom.h"
#include "third_party/blink/public/mojom/messaging/transferable_message.mojom.h"

namespace blink {

WebMessagePort::Message::Message() = default;
WebMessagePort::Message::Message(Message&&) = default;
WebMessagePort::Message& WebMessagePort::Message::operator=(Message&&) =
    default;
WebMessagePort::Message::~Message() = default;

WebMessagePort::Message::Message(const std::u16string& data) : data(data) {}

WebMessagePort::Message::Message(std::vector<WebMessagePort> ports)
    : ports(std::move(ports)) {}

WebMessagePort::Message::Message(WebMessagePort&& port) {
  ports.emplace_back(std::move(port));
}

WebMessagePort::Message::Message(const std::u16string& data,
                                 std::vector<WebMessagePort> ports)
    : data(data), ports(std::move(ports)) {}

WebMessagePort::Message::Message(const std::u16string& data,
                                 WebMessagePort port)
    : data(data) {
  ports.emplace_back(std::move(port));
}

WebMessagePort::MessageReceiver::MessageReceiver() = default;
WebMessagePort::MessageReceiver::~MessageReceiver() = default;

bool WebMessagePort::MessageReceiver::OnMessage(Message) {
  return false;
}

WebMessagePort::WebMessagePort() = default;

WebMessagePort::WebMessagePort(WebMessagePort&& other) {
  Take(std::move(other));
}

WebMessagePort& WebMessagePort::operator=(WebMessagePort&& other) {
  CloseIfNecessary();
  Take(std::move(other));
  return *this;
}

WebMessagePort::~WebMessagePort() {
  CloseIfNecessary();
}

// static
std::pair<WebMessagePort, WebMessagePort> WebMessagePort::CreatePair() {
  MessagePortDescriptorPair port_pair;
  return std::make_pair(WebMessagePort(port_pair.TakePort0()),
                        WebMessagePort(port_pair.TakePort1()));
}

// static
WebMessagePort WebMessagePort::Create(MessagePortDescriptor port) {
  DCHECK(port.IsValid());
  DCHECK(!port.IsEntangled());

  return WebMessagePort(std::move(port));
}

void WebMessagePort::SetReceiver(
    MessageReceiver* receiver,
    scoped_refptr<base::SequencedTaskRunner> runner) {
  DCHECK(receiver);
  DCHECK(runner.get());

  DCHECK(port_.IsValid());
  DCHECK(!connector_);
  DCHECK(!is_closed_);
  DCHECK(!is_errored_);
  DCHECK(is_transferable_);

  is_transferable_ = false;
  receiver_ = receiver;
  connector_ = std::make_unique<mojo::Connector>(
      port_.TakeHandleToEntangleWithEmbedder(),
      mojo::Connector::SINGLE_THREADED_SEND, std::move(runner));
  connector_->set_incoming_receiver(this);
  connector_->set_connection_error_handler(
      base::BindOnce(&WebMessagePort::OnPipeError, base::Unretained(this)));
}

void WebMessagePort::ClearReceiver() {
  if (!connector_)
    return;
  port_.GiveDisentangledHandle(connector_->PassMessagePipe());
  connector_.reset();
  receiver_ = nullptr;
}

base::SequencedTaskRunner* WebMessagePort::GetTaskRunner() const {
  if (!connector_)
    return nullptr;
  return connector_->task_runner();
}

MessagePortDescriptor WebMessagePort::PassPort() {
  DCHECK(is_transferable_);

  // Clear the receiver, which takes the handle out of the connector if it
  // exists, and puts it back in |port_|.
  ClearReceiver();
  MessagePortDescriptor port = std::move(port_);
  Reset();
  return port;
}

const base::UnguessableToken& WebMessagePort::GetEmbedderAgentClusterID() {
  // This is creating a single agent cluster ID that would represent the
  // embedder in MessagePort IPCs. While we could create a new ID on each call,
  // providing a consistent one saves RNG work and could be useful in the future
  // if we'd want to consistently identify messages from the embedder.
  static const auto agent_cluster_id = base::UnguessableToken::Create();
  return agent_cluster_id;
}

WebMessagePort::WebMessagePort(MessagePortDescriptor&& port)
    : port_(std::move(port)), is_closed_(false), is_transferable_(true) {
  DCHECK(port_.IsValid());
}

bool WebMessagePort::CanPostMessage() const {
  return connector_ && connector_->is_valid() && !is_closed_ && !is_errored_ &&
         receiver_;
}

bool WebMessagePort::PostMessage(Message&& message) {
  if (!CanPostMessage())
    return false;

  // Extract the underlying handles for transport in a
  // blink::TransferableMessage.
  std::vector<MessagePortDescriptor> ports;
  for (auto& port : message.ports) {
    // We should not be trying to send ourselves in a message. Mojo prevents
    // this at a deeper level, but we can also check here.
    DCHECK_NE(this, &port);

    ports.emplace_back(port.PassPort());
  }

  // Build the message.
  // TODO(chrisha): Finally kill off MessagePortChannel, once
  // MessagePortDescriptor more thoroughly plays that role.
  blink::TransferableMessage transferable_message =
      blink::EncodeWebMessagePayload(std::move(message.data));
  transferable_message.ports =
      blink::MessagePortChannel::CreateFromHandles(std::move(ports));

  // Get the embedder assigned cluster ID, as these messages originate from the
  // embedder.
  transferable_message.sender_agent_cluster_id = GetEmbedderAgentClusterID();

  // TODO(chrisha): Notify the instrumentation delegate of a message being sent!

  // Send via Mojo. The message should never be malformed so should always be
  // accepted.
  mojo::Message mojo_message =
      blink::mojom::TransferableMessage::SerializeAsMessage(
          &transferable_message);
  CHECK(connector_->Accept(&mojo_message));

  return true;
}

bool WebMessagePort::IsValid() const {
  if (connector_)
    return connector_->is_valid();
  return port_.IsValid();
}

void WebMessagePort::Close() {
  CloseIfNecessary();
}

void WebMessagePort::Reset() {
  CloseIfNecessary();
  is_closed_ = true;
  is_errored_ = false;
  is_transferable_ = false;
}

void WebMessagePort::Take(WebMessagePort&& other) {
  port_ = std::move(other.port_);
  connector_ = std::move(other.connector_);
  is_closed_ = std::exchange(other.is_closed_, true);
  is_errored_ = std::exchange(other.is_errored_, false);
  is_transferable_ = std::exchange(other.is_transferable_, false);
  receiver_ = std::exchange(other.receiver_, nullptr);
}

void WebMessagePort::OnPipeError() {
  DCHECK(!is_transferable_);
  if (is_errored_)
    return;
  is_errored_ = true;
  if (receiver_)
    receiver_->OnPipeError();
}

void WebMessagePort::CloseIfNecessary() {
  if (is_closed_)
    return;
  is_closed_ = true;
  ClearReceiver();
  port_.Reset();
}

bool WebMessagePort::Accept(mojo::Message* mojo_message) {
  DCHECK(receiver_);
  DCHECK(!is_transferable_);

  // Deserialize the message.
  blink::TransferableMessage transferable_message;
  if (!blink::mojom::TransferableMessage::DeserializeFromMessage(
          std::move(*mojo_message), &transferable_message)) {
    return false;
  }
  auto ports = std::move(transferable_message.ports);
  // Decode the string portion of the message.
  Message message;
  std::optional<WebMessagePayload> optional_payload =
      blink::DecodeToWebMessagePayload(std::move(transferable_message));
  if (!optional_payload)
    return false;
  auto& payload = optional_payload.value();
  if (auto* str = std::get_if<std::u16string>(&payload)) {
    message.data = std::move(*str);
  } else {
    return false;
  }

  // Convert raw handles to MessagePorts.
  // TODO(chrisha): Kill off MessagePortChannel entirely!
  auto handles = blink::MessagePortChannel::ReleaseHandles(ports);
  for (auto& handle : handles) {
    message.ports.emplace_back(WebMessagePort(std::move(handle)));
  }

  // Pass the message on to the receiver.
  return receiver_->OnMessage(std::move(message));
}

}  // namespace blink