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
|
// Copyright 2018 The Chromium Authors
// 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.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/bindings/core/v8/script_promise_resolver.h"
#include "third_party/blink/renderer/core/dom/events/event_target.h"
#include "third_party/blink/renderer/platform/bindings/script_wrappable.h"
#include "third_party/blink/renderer/platform/heap/collection_support/heap_hash_set.h"
#include "third_party/blink/renderer/platform/heap/collection_support/heap_vector.h"
#include "third_party/blink/renderer/platform/heap/garbage_collected.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"
#include "third_party/blink/renderer/platform/scheduler/public/frame_scheduler.h"
namespace base {
class UnguessableToken;
}
namespace blink {
class ReadableStream;
class ScriptState;
class Serial;
class SerialInputSignals;
class SerialOptions;
class SerialOutputSignals;
class SerialPortInfo;
class SerialPortUnderlyingSink;
class SerialPortUnderlyingSource;
class WritableStream;
class SerialPort final : public EventTarget,
public ActiveScriptWrappable<SerialPort>,
public device::mojom::blink::SerialPortClient {
DEFINE_WRAPPERTYPEINFO();
public:
explicit SerialPort(Serial* parent, mojom::blink::SerialPortInfoPtr info);
~SerialPort() override;
// Web-exposed functions
DEFINE_ATTRIBUTE_EVENT_LISTENER(connect, kConnect)
DEFINE_ATTRIBUTE_EVENT_LISTENER(disconnect, kDisconnect)
SerialPortInfo* getInfo();
ScriptPromise<IDLUndefined> open(ScriptState*,
const SerialOptions* options,
ExceptionState&);
bool connected() { return connected_; }
ReadableStream* readable(ScriptState*, ExceptionState&);
WritableStream* writable(ScriptState*, ExceptionState&);
ScriptPromise<SerialInputSignals> getSignals(ScriptState*, ExceptionState&);
ScriptPromise<IDLUndefined> setSignals(ScriptState*,
const SerialOutputSignals*,
ExceptionState&);
ScriptPromise<IDLUndefined> close(ScriptState*, ExceptionState&);
ScriptPromise<IDLUndefined> forget(ScriptState*, ExceptionState&);
const base::UnguessableToken& token() const { return info_->token; }
void set_connected(bool connected) { connected_ = connected; }
ScriptPromise<IDLUndefined> ContinueClose(ScriptState*);
void AbortClose();
void StreamsClosed();
bool IsClosing() const { return close_resolver_ != nullptr; }
void Flush(device::mojom::blink::SerialPortFlushMode mode,
device::mojom::blink::SerialPort::FlushCallback callback);
void Drain(device::mojom::blink::SerialPort::DrainCallback callback);
void UnderlyingSourceClosed();
void UnderlyingSinkClosed();
void ContextDestroyed();
void Trace(Visitor*) const override;
// ActiveScriptWrappable
bool HasPendingActivity() const override;
// EventTarget
ExecutionContext* GetExecutionContext() const override;
const AtomicString& InterfaceName() const override;
DispatchEventResult DispatchEventInternal(Event& event) 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::PendingReceiver<device::mojom::blink::SerialPortClient>,
mojo::PendingRemote<device::mojom::blink::SerialPort>);
void OnGetSignals(ScriptPromiseResolver<SerialInputSignals>*,
device::mojom::blink::SerialPortControlSignalsPtr);
void OnSetSignals(ScriptPromiseResolver<IDLUndefined>*, bool success);
void OnClose();
const mojom::blink::SerialPortInfoPtr info_;
bool connected_;
const Member<Serial> parent_;
uint32_t buffer_size_ = 0;
HeapMojoRemote<device::mojom::blink::SerialPort> port_;
HeapMojoReceiver<device::mojom::blink::SerialPortClient, SerialPort>
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;
// The port was opened with { flowControl: "hardware" }.
bool hardware_flow_control_ = false;
// Resolver for the Promise returned by open().
Member<ScriptPromiseResolver<IDLUndefined>> open_resolver_;
// Resolvers for the Promises returned by getSignals() and setSignals() to
// reject them on Mojo connection failure.
HeapHashSet<Member<ScriptPromiseResolverBase>> signal_resolvers_;
// Resolver for the Promise returned by close().
Member<ScriptPromiseResolver<IDLUndefined>> close_resolver_;
FrameScheduler::SchedulingAffectingFeatureHandle
feature_handle_for_scheduler_;
};
} // namespace blink
#endif // THIRD_PARTY_BLINK_RENDERER_MODULES_SERIAL_SERIAL_PORT_H_
|