GPU workaround to simulate Out of Memory errors with large textures
[chromium-blink-merge.git] / device / serial / serial_io_handler.h
blob5f93d63270b0ffcc8f4a31db94d21054a90a1a2c
1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #ifndef DEVICE_SERIAL_SERIAL_IO_HANDLER_H_
6 #define DEVICE_SERIAL_SERIAL_IO_HANDLER_H_
8 #include "base/callback.h"
9 #include "base/files/file.h"
10 #include "base/memory/ref_counted.h"
11 #include "base/message_loop/message_loop_proxy.h"
12 #include "base/threading/non_thread_safe.h"
13 #include "device/serial/buffer.h"
14 #include "device/serial/serial.mojom.h"
16 namespace device {
18 // Provides a simplified interface for performing asynchronous I/O on serial
19 // devices by hiding platform-specific MessageLoop interfaces. Pending I/O
20 // operations hold a reference to this object until completion so that memory
21 // doesn't disappear out from under the OS.
22 class SerialIoHandler : public base::NonThreadSafe,
23 public base::RefCounted<SerialIoHandler> {
24 public:
25 // Constructs an instance of some platform-specific subclass.
26 static scoped_refptr<SerialIoHandler> Create(
27 scoped_refptr<base::MessageLoopProxy> file_thread_message_loop,
28 scoped_refptr<base::MessageLoopProxy> ui_thread_message_loop);
30 typedef base::Callback<void(bool success)> OpenCompleteCallback;
32 // Initiates an asynchronous Open of the device.
33 virtual void Open(const std::string& port,
34 const serial::ConnectionOptions& options,
35 const OpenCompleteCallback& callback);
37 // Signals that the access request for |port| is complete.
38 void OnRequestAccessComplete(const std::string& port, bool success);
40 // Performs an async Read operation. Behavior is undefined if this is called
41 // while a Read is already pending. Otherwise, the Done or DoneWithError
42 // method on |buffer| will eventually be called with a result.
43 void Read(scoped_ptr<WritableBuffer> buffer);
45 // Performs an async Write operation. Behavior is undefined if this is called
46 // while a Write is already pending. Otherwise, the Done or DoneWithError
47 // method on |buffer| will eventually be called with a result.
48 void Write(scoped_ptr<ReadOnlyBuffer> buffer);
50 // Indicates whether or not a read is currently pending.
51 bool IsReadPending() const;
53 // Indicates whether or not a write is currently pending.
54 bool IsWritePending() const;
56 // Attempts to cancel a pending read operation.
57 void CancelRead(serial::ReceiveError reason);
59 // Attempts to cancel a pending write operation.
60 void CancelWrite(serial::SendError reason);
62 // Flushes input and output buffers.
63 virtual bool Flush() const = 0;
65 // Reads current control signals (DCD, CTS, etc.) into an existing
66 // DeviceControlSignals structure. Returns |true| iff the signals were
67 // successfully read.
68 virtual serial::DeviceControlSignalsPtr GetControlSignals() const = 0;
70 // Sets one or more control signals (DTR and/or RTS). Returns |true| iff
71 // the signals were successfully set. Unininitialized flags in the
72 // HostControlSignals structure are left unchanged.
73 virtual bool SetControlSignals(
74 const serial::HostControlSignals& control_signals) = 0;
76 // Performs platform-specific port configuration. Returns |true| iff
77 // configuration was successful.
78 bool ConfigurePort(const serial::ConnectionOptions& options);
80 // Performs a platform-specific port configuration query. Fills values in an
81 // existing ConnectionInfo. Returns |true| iff port configuration was
82 // successfully retrieved.
83 virtual serial::ConnectionInfoPtr GetPortInfo() const = 0;
85 protected:
86 explicit SerialIoHandler(
87 scoped_refptr<base::MessageLoopProxy> file_thread_message_loop,
88 scoped_refptr<base::MessageLoopProxy> ui_thread_message_loop);
89 virtual ~SerialIoHandler();
91 // Performs a platform-specific read operation. This must guarantee that
92 // ReadCompleted is called when the underlying async operation is completed
93 // or the SerialIoHandler instance will leak.
94 // NOTE: Implementations of ReadImpl should never call ReadCompleted directly.
95 // Use QueueReadCompleted instead to avoid reentrancy.
96 virtual void ReadImpl() = 0;
98 // Performs a platform-specific write operation. This must guarantee that
99 // WriteCompleted is called when the underlying async operation is completed
100 // or the SerialIoHandler instance will leak.
101 // NOTE: Implementations of WriteImpl should never call WriteCompleted
102 // directly. Use QueueWriteCompleted instead to avoid reentrancy.
103 virtual void WriteImpl() = 0;
105 // Platform-specific read cancelation.
106 virtual void CancelReadImpl() = 0;
108 // Platform-specific write cancelation.
109 virtual void CancelWriteImpl() = 0;
111 // Platform-specific port configuration applies options_ to the device.
112 virtual bool ConfigurePortImpl() = 0;
114 // Requests access to the underlying serial device, if needed.
115 virtual void RequestAccess(
116 const std::string& port,
117 scoped_refptr<base::MessageLoopProxy> file_message_loop,
118 scoped_refptr<base::MessageLoopProxy> ui_message_loop);
120 // Performs platform-specific, one-time port configuration on open.
121 virtual bool PostOpen();
123 // Called by the implementation to signal that the active read has completed.
124 // WARNING: Calling this method can destroy the SerialIoHandler instance
125 // if the associated I/O operation was the only thing keeping it alive.
126 void ReadCompleted(int bytes_read, serial::ReceiveError error);
128 // Called by the implementation to signal that the active write has completed.
129 // WARNING: Calling this method may destroy the SerialIoHandler instance
130 // if the associated I/O operation was the only thing keeping it alive.
131 void WriteCompleted(int bytes_written, serial::SendError error);
133 // Queues a ReadCompleted call on the current thread. This is used to allow
134 // ReadImpl to immediately signal completion with 0 bytes and an error,
135 // without being reentrant.
136 void QueueReadCompleted(int bytes_read, serial::ReceiveError error);
138 // Queues a WriteCompleted call on the current thread. This is used to allow
139 // WriteImpl to immediately signal completion with 0 bytes and an error,
140 // without being reentrant.
141 void QueueWriteCompleted(int bytes_written, serial::SendError error);
143 const base::File& file() const { return file_; }
145 char* pending_read_buffer() const {
146 return pending_read_buffer_ ? pending_read_buffer_->GetData() : NULL;
149 uint32_t pending_read_buffer_len() const {
150 return pending_read_buffer_ ? pending_read_buffer_->GetSize() : 0;
153 serial::ReceiveError read_cancel_reason() const {
154 return read_cancel_reason_;
157 bool read_canceled() const { return read_canceled_; }
159 const char* pending_write_buffer() const {
160 return pending_write_buffer_ ? pending_write_buffer_->GetData() : NULL;
163 uint32_t pending_write_buffer_len() const {
164 return pending_write_buffer_ ? pending_write_buffer_->GetSize() : 0;
167 serial::SendError write_cancel_reason() const { return write_cancel_reason_; }
169 bool write_canceled() const { return write_canceled_; }
171 const serial::ConnectionOptions& options() const { return options_; }
173 // Possibly fixes up a serial port path name in a platform-specific manner.
174 static std::string MaybeFixUpPortName(const std::string& port_name);
176 private:
177 friend class base::RefCounted<SerialIoHandler>;
179 void MergeConnectionOptions(const serial::ConnectionOptions& options);
181 // Continues an Open operation on the FILE thread.
182 void StartOpen(const std::string& port,
183 scoped_refptr<base::MessageLoopProxy> io_message_loop);
185 // Finalizes an Open operation (continued from StartOpen) on the IO thread.
186 void FinishOpen(base::File file);
188 void Close();
190 // Continues a Close operation on the FILE thread.
191 static void DoClose(base::File port);
193 // File for the opened serial device. This value is only modified from the IO
194 // thread.
195 base::File file_;
197 // Currently applied connection options.
198 serial::ConnectionOptions options_;
200 scoped_ptr<WritableBuffer> pending_read_buffer_;
201 serial::ReceiveError read_cancel_reason_;
202 bool read_canceled_;
204 scoped_ptr<ReadOnlyBuffer> pending_write_buffer_;
205 serial::SendError write_cancel_reason_;
206 bool write_canceled_;
208 // Callback to handle the completion of a pending Open() request.
209 OpenCompleteCallback open_complete_;
211 scoped_refptr<base::MessageLoopProxy> file_thread_message_loop_;
212 // On Chrome OS, PermissionBrokerClient should be called on the UI thread.
213 scoped_refptr<base::MessageLoopProxy> ui_thread_message_loop_;
215 DISALLOW_COPY_AND_ASSIGN(SerialIoHandler);
218 } // namespace device
220 #endif // DEVICE_SERIAL_SERIAL_IO_HANDLER_H_