Try backing SkPicture with SkRecord in Chromium.
[chromium-blink-merge.git] / device / serial / data_sink_receiver.h
blob2e63d7a50a6fd2519bb74fd829edfd11e8096749
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_DATA_SINK_RECEIVER_H_
6 #define DEVICE_SERIAL_DATA_SINK_RECEIVER_H_
8 #include <queue>
10 #include "base/callback.h"
11 #include "base/memory/linked_ptr.h"
12 #include "base/memory/ref_counted.h"
13 #include "base/memory/weak_ptr.h"
14 #include "device/serial/buffer.h"
15 #include "device/serial/data_stream.mojom.h"
16 #include "mojo/public/cpp/system/data_pipe.h"
18 namespace device {
20 class AsyncWaiter;
22 class DataSinkReceiver : public base::RefCounted<DataSinkReceiver>,
23 public mojo::InterfaceImpl<serial::DataSink> {
24 public:
25 typedef base::Callback<void(scoped_ptr<ReadOnlyBuffer>)> ReadyCallback;
26 typedef base::Callback<void(int32_t error)> CancelCallback;
27 typedef base::Callback<void()> ErrorCallback;
29 // Constructs a DataSinkReceiver. Whenever the pipe is ready for reading, the
30 // |ready_callback| will be called with the ReadOnlyBuffer to read.
31 // |ready_callback| will not be called again until the previous ReadOnlyBuffer
32 // is destroyed. If a connection error occurs, |error_callback| will be called
33 // and the DataSinkReceiver will act as if ShutDown() had been called. If
34 // |cancel_callback| is valid, it will be called when the DataSinkClient
35 // requests cancellation of the in-progress read.
36 DataSinkReceiver(const ReadyCallback& ready_callback,
37 const CancelCallback& cancel_callback,
38 const ErrorCallback& error_callback);
40 // Shuts down this DataSinkReceiver. After shut down, |ready_callback|,
41 // |cancel_callback| and |error_callback| will never be called.
42 void ShutDown();
44 private:
45 class Buffer;
46 class PendingFlush;
47 friend class base::RefCounted<DataSinkReceiver>;
49 virtual ~DataSinkReceiver();
51 // mojo::InterfaceImpl<serial::DataSink> overrides.
52 virtual void Init(mojo::ScopedDataPipeConsumerHandle handle) OVERRIDE;
53 virtual void Cancel(int32_t error) OVERRIDE;
54 virtual void OnConnectionError() OVERRIDE;
56 // Starts waiting for |handle_| to be ready for reads.
57 void StartWaiting();
59 // Invoked when |handle_| is ready for reads.
60 void OnDoneWaiting(MojoResult result);
62 // Reports a successful read of |bytes_read|.
63 void Done(uint32_t bytes_read);
65 // Reports a partially successful or unsuccessful read of |bytes_read|
66 // with an error of |error|.
67 void DoneWithError(uint32_t bytes_read, int32_t error);
69 // Finishes the two-phase data pipe read.
70 bool DoneInternal(uint32_t bytes_read);
72 // Sends an ReportBytesSentAndError message to the client.
73 void ReportBytesSentAndError(uint32_t bytes_read, int32_t error);
75 // Invoked in response to an ReportBytesSentAndError call to the client with
76 // the number of bytes to flush.
77 void SetNumBytesToFlush(uint32_t bytes_to_flush);
79 // Reports a fatal error to the client and shuts down.
80 void DispatchFatalError();
82 // The data connection to the data sender.
83 mojo::ScopedDataPipeConsumerHandle handle_;
85 // The callback to call when |handle_| has data ready to read.
86 const ReadyCallback ready_callback_;
88 // The callback to call when the client has requested cancellation.
89 const CancelCallback cancel_callback_;
91 // The callback to call if a fatal error occurs.
92 const ErrorCallback error_callback_;
94 // The queue of pending flushes.
95 std::queue<linked_ptr<PendingFlush> > pending_flushes_;
97 // A waiter used to wait until |handle_| is readable if we are waiting.
98 scoped_ptr<AsyncWaiter> waiter_;
100 // The buffer passed to |ready_callback_| if one exists. This is not owned,
101 // but the Buffer will call Done or DoneWithError before being deleted.
102 Buffer* buffer_in_use_;
104 // Whether we have encountered a fatal error and shut down.
105 bool shut_down_;
107 base::WeakPtrFactory<DataSinkReceiver> weak_factory_;
109 DISALLOW_COPY_AND_ASSIGN(DataSinkReceiver);
112 } // namespace device
114 #endif // DEVICE_SERIAL_DATA_SINK_RECEIVER_H_