Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / mojo / services / network / public / cpp / web_socket_write_queue.h
blobd0ba6b2fc73112ba66348be13880bb725cb0fce4
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 MOJO_SERVICES_NETWORK_PUBLIC_CPP_WEB_SOCKET_WRITE_QUEUE_H_
6 #define MOJO_SERVICES_NETWORK_PUBLIC_CPP_WEB_SOCKET_WRITE_QUEUE_H_
8 #include "base/callback.h"
9 #include "base/memory/scoped_vector.h"
10 #include "base/memory/weak_ptr.h"
11 #include "mojo/message_pump/handle_watcher.h"
12 #include "third_party/mojo/src/mojo/public/cpp/system/data_pipe.h"
14 namespace mojo {
16 // This class simplifies the handling of multiple Writes on a DataPipe. It
17 // writes each chunk all at once (or waits until the pipe is ready before
18 // writing), calling the callback when finished. Callbacks are owned by this
19 // class, and are guaranteed not to be called after this class is destroyed.
20 // See also: WebSocketReadQueue
21 class WebSocketWriteQueue {
22 public:
23 explicit WebSocketWriteQueue(DataPipeProducerHandle handle);
24 ~WebSocketWriteQueue();
26 void Write(const char* data,
27 uint32_t num_bytes,
28 base::Callback<void(const char*)> callback);
30 private:
31 struct Operation;
33 void TryToWrite();
34 void Wait();
35 void OnHandleReady(MojoResult result);
37 DataPipeProducerHandle handle_;
38 common::HandleWatcher handle_watcher_;
39 ScopedVector<Operation> queue_;
40 bool is_busy_;
41 base::WeakPtrFactory<WebSocketWriteQueue> weak_factory_;
44 } // namespace mojo
46 #endif // MOJO_SERVICES_NETWORK_PUBLIC_CPP_WEB_SOCKET_WRITE_QUEUE_H_