-
Notifications
You must be signed in to change notification settings - Fork 6.6k
/
serial_port.h
128 lines (106 loc) · 4.8 KB
/
serial_port.h
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
// 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.
#ifndef THIRD_PARTY_BLINK_RENDERER_MODULES_SERIAL_SERIAL_PORT_H_
#define THIRD_PARTY_BLINK_RENDERER_MODULES_SERIAL_SERIAL_PORT_H_
#include "mojo/public/cpp/bindings/pending_receiver.h"
#include "services/device/public/mojom/serial.mojom-blink-forward.h"
#include "third_party/blink/public/mojom/serial/serial.mojom-blink.h"
#include "third_party/blink/renderer/bindings/core/v8/active_script_wrappable.h"
#include "third_party/blink/renderer/bindings/core/v8/script_promise.h"
#include "third_party/blink/renderer/platform/bindings/script_wrappable.h"
#include "third_party/blink/renderer/platform/heap/handle.h"
#include "third_party/blink/renderer/platform/heap/heap_allocator.h"
#include "third_party/blink/renderer/platform/mojo/heap_mojo_receiver.h"
#include "third_party/blink/renderer/platform/mojo/heap_mojo_remote.h"
#include "third_party/blink/renderer/platform/mojo/heap_mojo_wrapper_mode.h"
namespace base {
class UnguessableToken;
}
namespace blink {
class ReadableStream;
class ScriptPromiseResolver;
class ScriptState;
class Serial;
class SerialOptions;
class SerialOutputSignals;
class SerialPortUnderlyingSink;
class SerialPortUnderlyingSource;
class WritableStream;
class SerialPort final : public ScriptWrappable,
public ActiveScriptWrappable<SerialPort>,
public device::mojom::blink::SerialPortClient {
DEFINE_WRAPPERTYPEINFO();
USING_GARBAGE_COLLECTED_MIXIN(SerialPort);
public:
explicit SerialPort(Serial* parent, mojom::blink::SerialPortInfoPtr info);
~SerialPort() override;
// Web-exposed functions
ScriptPromise open(ScriptState*,
const SerialOptions* options,
ExceptionState&);
ReadableStream* readable(ScriptState*, ExceptionState&);
WritableStream* writable(ScriptState*, ExceptionState&);
ScriptPromise getSignals(ScriptState*, ExceptionState&);
ScriptPromise setSignals(ScriptState*,
const SerialOutputSignals*,
ExceptionState&);
ScriptPromise close(ScriptState*, ExceptionState&);
const base::UnguessableToken& token() const { return info_->token; }
void UnderlyingSourceClosed();
void UnderlyingSinkClosed();
ScriptPromise ContinueClose(ScriptState*);
void AbortClose();
void ContextDestroyed();
void Trace(Visitor*) override;
// ActiveScriptWrappable
ExecutionContext* GetExecutionContext() const;
bool HasPendingActivity() const override;
// SerialPortClient
void OnReadError(device::mojom::blink::SerialReceiveError) override;
void OnSendError(device::mojom::blink::SerialSendError) override;
private:
bool CreateDataPipe(mojo::ScopedDataPipeProducerHandle* producer,
mojo::ScopedDataPipeConsumerHandle* consumer);
void OnConnectionError();
void OnOpen(mojo::ScopedDataPipeConsumerHandle,
mojo::ScopedDataPipeProducerHandle,
mojo::PendingReceiver<device::mojom::blink::SerialPortClient>,
bool success);
void InitializeReadableStream(ScriptState*,
mojo::ScopedDataPipeConsumerHandle);
void InitializeWritableStream(ScriptState*,
mojo::ScopedDataPipeProducerHandle);
void OnGetSignals(ScriptPromiseResolver*,
device::mojom::blink::SerialPortControlSignalsPtr);
void OnSetSignals(ScriptPromiseResolver*, bool success);
void OnClose();
const mojom::blink::SerialPortInfoPtr info_;
const Member<Serial> parent_;
uint32_t buffer_size_ = 0;
HeapMojoRemote<device::mojom::blink::SerialPort> port_;
HeapMojoReceiver<device::mojom::blink::SerialPortClient,
SerialPort,
HeapMojoWrapperMode::kWithoutContextObserver>
client_receiver_;
Member<ReadableStream> readable_;
Member<SerialPortUnderlyingSource> underlying_source_;
Member<WritableStream> writable_;
Member<SerialPortUnderlyingSink> underlying_sink_;
// Indicates that the read or write streams have encountered a fatal error and
// should not be reopened.
bool read_fatal_ = false;
bool write_fatal_ = false;
// Indicates that the port is being closed and so the streams should not be
// reopened on demand.
bool closing_ = false;
// Resolver for the Promise returned by open().
Member<ScriptPromiseResolver> open_resolver_;
// Resolvers for the Promises returned by getSignals() and setSignals() to
// reject them on Mojo connection failure.
HeapHashSet<Member<ScriptPromiseResolver>> signal_resolvers_;
// Resolver for the Promise returned by ClosePort().
Member<ScriptPromiseResolver> close_resolver_;
};
} // namespace blink
#endif // THIRD_PARTY_BLINK_RENDERER_MODULES_SERIAL_SERIAL_PORT_H_