File: message_port_descriptor.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 (288 lines) | stat: -rw-r--r-- 9,674 bytes parent folder | download | duplicates (11)
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
// 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 "third_party/blink/public/common/messaging/message_port_descriptor.h"

#include <utility>

namespace blink {

namespace {

MessagePortDescriptor::InstrumentationDelegate* g_instrumentation_delegate =
    nullptr;

}  // namespace

// static
const size_t MessagePortDescriptor::kInvalidSequenceNumber;

// static
const size_t MessagePortDescriptor::kFirstValidSequenceNumber;

// static
void MessagePortDescriptor::SetInstrumentationDelegate(
    InstrumentationDelegate* delegate) {
  // There should only ever be one delegate, and this only should toggle from
  // being set to not being set and vice-versa. The toggling only ever occurs
  // during tests; in production a single instrumentation delegate is installed
  // early during Blink startup and left in place forever afterwards.
  DCHECK(!delegate ^ !g_instrumentation_delegate);
  g_instrumentation_delegate = delegate;
}

MessagePortDescriptor::MessagePortDescriptor() = default;

MessagePortDescriptor::MessagePortDescriptor(
    MessagePortDescriptor&& message_port)
    : handle_(std::move(message_port.handle_)),
      id_(std::exchange(message_port.id_, base::UnguessableToken::Null())),
      sequence_number_(std::exchange(message_port.sequence_number_,
                                     kInvalidSequenceNumber)) {}

MessagePortDescriptor& MessagePortDescriptor::operator=(
    MessagePortDescriptor&& message_port) {
  Reset();

  handle_ = std::move(message_port.handle_);
  id_ = std::exchange(message_port.id_, base::UnguessableToken::Null());
  sequence_number_ =
      std::exchange(message_port.sequence_number_, kInvalidSequenceNumber);

  return *this;
}

MessagePortDescriptor::~MessagePortDescriptor() {
  Reset();
}

MojoHandle MessagePortDescriptor::GetMojoHandleForTesting() const {
  if (!handle_.get())
    return MOJO_HANDLE_INVALID;
  return handle_.get().value();
}

bool MessagePortDescriptor::IsValid() const {
  // |handle_| can be valid or invalid, depending on if we're entangled or
  // not. But everything else should be consistent.
  EnsureValidSerializationState();
  DCHECK_EQ(id_.is_empty(), sequence_number_ == kInvalidSequenceNumber);
  return !id_.is_empty() && sequence_number_ != kInvalidSequenceNumber;
}

bool MessagePortDescriptor::IsEntangled() const {
  EnsureNotSerialized();
  // This descriptor is entangled if it's valid, but its handle has been loaned
  // out.
  return IsValid() && !handle_.is_valid();
}

bool MessagePortDescriptor::IsDefault() const {
  EnsureValidSerializationState();
  if (IsValid())
    return false;

  // This is almost the converse of IsValid, except that we additionally expect
  // the |handle_| to be empty as well (which IsValid doesn't verify).
  DCHECK(!handle_.is_valid());
  return true;
}

void MessagePortDescriptor::Reset() {
#if DCHECK_IS_ON()
  EnsureValidSerializationState();
  serialization_state_ = {};
#endif

  if (IsValid()) {
    // Call NotifyDestroyed before clearing members, as the notification needs
    // to access them.
    NotifyDestroyed();

    // Ensure that MessagePipeDescriptor-wrapped handles are fully accounted for
    // over their entire lifetime.
    DCHECK(handle_.is_valid());

    handle_.reset();
    id_ = base::UnguessableToken::Null();
    sequence_number_ = kInvalidSequenceNumber;
  }
}

void MessagePortDescriptor::InitializeFromSerializedValues(
    mojo::ScopedMessagePipeHandle handle,
    const base::UnguessableToken& id,
    uint64_t sequence_number) {
#if DCHECK_IS_ON()
  EnsureValidSerializationState();
  serialization_state_ = {};

  // This is only called by deserialization code and thus should only be called
  // on a default initialized descriptor.
  DCHECK(IsDefault());
#endif

  handle_ = std::move(handle);
  id_ = id;
  sequence_number_ = sequence_number;

  // Init should only create a valid not-entangled descriptor, or a default
  // descriptor.
  DCHECK((IsValid() && !IsEntangled()) || IsDefault());
}

mojo::ScopedMessagePipeHandle
MessagePortDescriptor::TakeHandleForSerialization() {
#if DCHECK_IS_ON()
  DCHECK(handle_.is_valid());  // Ensures not entangled.
  DCHECK(!serialization_state_.took_handle_for_serialization_);
  serialization_state_.took_handle_for_serialization_ = true;
#endif
  return std::move(handle_);
}

base::UnguessableToken MessagePortDescriptor::TakeIdForSerialization() {
#if DCHECK_IS_ON()
  DCHECK(!id_.is_empty());
  DCHECK(serialization_state_.took_handle_for_serialization_ ||
         handle_.is_valid());  // Ensures not entangled.
  DCHECK(!serialization_state_.took_id_for_serialization_);
  serialization_state_.took_id_for_serialization_ = true;
#endif
  return std::exchange(id_, base::UnguessableToken::Null());
}

uint64_t MessagePortDescriptor::TakeSequenceNumberForSerialization() {
#if DCHECK_IS_ON()
  DCHECK_NE(kInvalidSequenceNumber, sequence_number_);
  DCHECK(serialization_state_.took_handle_for_serialization_ ||
         handle_.is_valid());  // Ensures not entangled.
  DCHECK(!serialization_state_.took_sequence_number_for_serialization_);
  serialization_state_.took_sequence_number_for_serialization_ = true;
#endif
  return std::exchange(sequence_number_, kInvalidSequenceNumber);
}

mojo::ScopedMessagePipeHandle MessagePortDescriptor::TakeHandleToEntangle(
    ExecutionContext* execution_context) {
  EnsureNotSerialized();
  DCHECK(handle_.is_valid());
  NotifyAttached(execution_context);
  return std::move(handle_);
}

mojo::ScopedMessagePipeHandle
MessagePortDescriptor::TakeHandleToEntangleWithEmbedder() {
  EnsureNotSerialized();
  DCHECK(handle_.is_valid());
  NotifyAttachedToEmbedder();
  return std::move(handle_);
}

void MessagePortDescriptor::GiveDisentangledHandle(
    mojo::ScopedMessagePipeHandle handle) {
  EnsureNotSerialized();
  // Ideally, we should only ever be given back the same handle that was taken
  // from us.
  // NOTE: It is possible that this can happen if the handle is bound to a
  // Connector, and the Connector subsequently encounters an error, force closes
  // the pipe, and the transparently binds another dangling pipe. This can be
  // caught by having the descriptor own the connector and observer connection
  // errors, but this can only occur once descriptors are being used everywhere.
  handle_ = std::move(handle);

  // If we've been given back a null handle, then the handle we vended out was
  // closed due to error (this can happen in Java code). For now, simply create
  // a dangling handle to replace it. This allows the IsEntangled() and
  // IsValid() logic to work as is.
  // TODO(chrisha): Clean this up once we make this own a connector, and endow
  // it with knowledge of the connector error state. There's no need for us to
  // hold on to a dangling pipe endpoint, and we can send a NotifyClosed()
  // earlier.
  if (!handle_.is_valid()) {
    mojo::MessagePipe pipe;
    handle_ = std::move(pipe.handle0);
  }

  NotifyDetached();
}

MessagePortDescriptor::MessagePortDescriptor(
    mojo::ScopedMessagePipeHandle handle)
    : handle_(std::move(handle)),
      id_(base::UnguessableToken::Create()),
      sequence_number_(kFirstValidSequenceNumber) {
}

void MessagePortDescriptor::NotifyAttached(
    ExecutionContext* execution_context) {
  EnsureNotSerialized();
  DCHECK(!id_.is_empty());
  if (g_instrumentation_delegate) {
    g_instrumentation_delegate->NotifyMessagePortAttached(
        id_, sequence_number_++, execution_context);
  }
}

void MessagePortDescriptor::NotifyAttachedToEmbedder() {
  EnsureNotSerialized();
  DCHECK(!id_.is_empty());
  if (g_instrumentation_delegate) {
    g_instrumentation_delegate->NotifyMessagePortAttachedToEmbedder(
        id_, sequence_number_++);
  }
}

void MessagePortDescriptor::NotifyDetached() {
  EnsureNotSerialized();
  DCHECK(!id_.is_empty());
  if (g_instrumentation_delegate) {
    g_instrumentation_delegate->NotifyMessagePortDetached(id_,
                                                          sequence_number_++);
  }
}

void MessagePortDescriptor::NotifyDestroyed() {
  EnsureNotSerialized();
  DCHECK(!id_.is_empty());
  if (g_instrumentation_delegate) {
    g_instrumentation_delegate->NotifyMessagePortDestroyed(id_,
                                                           sequence_number_++);
  }
}

void MessagePortDescriptor::EnsureNotSerialized() const {
#if DCHECK_IS_ON()
  DCHECK(!serialization_state_.took_handle_for_serialization_ &&
         !serialization_state_.took_id_for_serialization_ &&
         !serialization_state_.took_sequence_number_for_serialization_);
#endif
}

void MessagePortDescriptor::EnsureValidSerializationState() const {
#if DCHECK_IS_ON()
  // Either everything was serialized, or nothing was.
  DCHECK((serialization_state_.took_handle_for_serialization_ ==
          serialization_state_.took_id_for_serialization_) &&
         (serialization_state_.took_handle_for_serialization_ ==
          serialization_state_.took_sequence_number_for_serialization_));
#endif
}

MessagePortDescriptorPair::MessagePortDescriptorPair() {
  mojo::MessagePipe pipe;
  port0_ = MessagePortDescriptor(std::move(pipe.handle0));
  port1_ = MessagePortDescriptor(std::move(pipe.handle1));

  // Notify the instrumentation that these ports are newly created and peers of
  // each other.
  if (g_instrumentation_delegate) {
    g_instrumentation_delegate->NotifyMessagePortPairCreated(port0_.id(),
                                                             port1_.id());
  }
}

MessagePortDescriptorPair::~MessagePortDescriptorPair() = default;

}  // namespace blink