Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2016 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. | |
| 4 | |
| 5 #ifndef CONTENT_BROWSER_RENDERER_HOST_WEBSOCKET_BLOB_SENDER_H_ | |
| 6 #define CONTENT_BROWSER_RENDERER_HOST_WEBSOCKET_BLOB_SENDER_H_ | |
| 7 | |
| 8 #include <stdint.h> | |
| 9 #include <string> | |
| 10 #include <vector> | |
| 11 | |
| 12 #include "base/macros.h" | |
| 13 #include "base/memory/ref_counted.h" | |
| 14 #include "base/memory/scoped_ptr.h" | |
| 15 #include "content/common/content_export.h" | |
| 16 #include "net/base/completion_callback.h" | |
| 17 #include "net/websockets/websocket_event_interface.h" | |
| 18 | |
| 19 namespace base { | |
| 20 class SingleThreadTaskRunner; | |
| 21 } | |
| 22 | |
| 23 namespace net { | |
| 24 class IOBuffer; | |
| 25 } | |
| 26 | |
| 27 namespace storage { | |
| 28 class BlobReader; | |
| 29 class BlobStorageContext; | |
| 30 class FileSystemContext; | |
| 31 } | |
| 32 | |
| 33 namespace content { | |
| 34 | |
| 35 class WebSocketHost; | |
| 36 | |
| 37 class CONTENT_EXPORT WebSocketBlobSender { | |
| 38 public: | |
| 39 // An abstraction of the WebSocketChannel this object will send frames to. | |
| 40 class Channel { | |
| 41 public: | |
| 42 using ChannelState = net::WebSocketEventInterface::ChannelState; | |
| 43 | |
| 44 Channel() {} | |
| 45 virtual ~Channel() {} | |
| 46 | |
| 47 // The currently available quota for sending. It must not decrease without | |
| 48 // SendFrame() being called. | |
| 49 virtual int GetSendQuota() const = 0; | |
| 50 | |
| 51 // Send a binary frame. |fin| is true for the final frame of the message. | |
| 52 // |data| is the contents of the frame. data.size() must be less than | |
| 53 // GetSendQuota(). If this call returns CHANNEL_DELETED, WebSocketBlobSender | |
| 54 // will assume that it has been deleted and return without calling any | |
| 55 // callbacks or accessing any other member data. | |
| 56 virtual ChannelState SendFrame(bool fin, const std::vector<char>& data) = 0; | |
|
dcheng
2016/01/20 08:30:06
bool is_final_frame would be more descriptive.
Adam Rice
2016/01/20 17:11:20
The names were originally based on the terminology
| |
| 57 | |
| 58 private: | |
| 59 DISALLOW_COPY_AND_ASSIGN(Channel); | |
| 60 }; | |
| 61 | |
| 62 explicit WebSocketBlobSender(scoped_ptr<Channel> channel); | |
| 63 ~WebSocketBlobSender(); | |
| 64 | |
| 65 // Checks that the blob identified by |uuid| exists, has the size | |
| 66 // |expected_size| and then starts sending it via |channel_|. Returns | |
| 67 // ERR_IO_PENDING to indicate that |callback| will be called later with the | |
| 68 // result. net::OK indicates synchronous success. Any other net error code | |
| 69 // indicates synchronous failure. This method may result in the destruction of | |
| 70 // the channel, in which case |*channel_state| will be set to CHANNEL_DELETED. | |
| 71 int Start(const std::string& uuid, | |
| 72 uint64_t expected_size, | |
| 73 storage::BlobStorageContext* context, | |
| 74 storage::FileSystemContext* file_system_context, | |
| 75 base::SingleThreadTaskRunner* file_task_runner, | |
| 76 net::WebSocketEventInterface::ChannelState* channel_state, | |
| 77 const net::CompletionCallback& callback); | |
| 78 | |
| 79 // Sends more data if the object was waiting for quota and the new value of | |
| 80 // GetSendQuota() is large enough. | |
| 81 void OnNewSendQuota(); | |
| 82 | |
| 83 uint64_t expected_size() const { return expected_size_; } | |
| 84 | |
| 85 // ActualSize() should only be called after completion: ie. Start() returned a | |
| 86 // value other than ERR_IO_PENDING or |callback_| has been called. | |
| 87 uint64_t ActualSize() const; | |
| 88 | |
| 89 private: | |
| 90 // State proceeds through STATE_READ_SIZE and STATE_READ_SIZE_COMPLETE, then | |
| 91 // loops STATE_WAIT_FOR_QUOTA -> STATE_WAIT_FOR_QUOTA_COMPLETE -> STATE_READ | |
| 92 // -> STATE_READ_COMPLETE -> STATE_WAIT_FOR_QUOTA until the Blob is completely | |
| 93 // sent. | |
| 94 enum State { | |
|
dcheng
2016/01/20 08:30:06
enum class? Then the members can just be named NON
Adam Rice
2016/01/20 17:11:20
Done.
| |
| 95 STATE_NONE, | |
| 96 STATE_READ_SIZE, | |
| 97 STATE_READ_SIZE_COMPLETE, | |
| 98 STATE_WAIT_FOR_QUOTA, | |
| 99 STATE_WAIT_FOR_QUOTA_COMPLETE, | |
| 100 STATE_READ, | |
| 101 STATE_READ_COMPLETE, | |
| 102 }; | |
| 103 | |
| 104 void OnIOComplete(int rv); | |
| 105 void OnSizeCalculated(int rv); | |
| 106 // |channel_state| should point to CHANNEL_ALIVE when called. If it is | |
| 107 // CHANNEL_DELETED on return, the object has been deleted. | |
| 108 int DoLoop(int result, Channel::ChannelState* channel_state); | |
| 109 void DoLoopAsync(int result); | |
| 110 int DoReadSize(); | |
| 111 int DoReadSizeComplete(int result); | |
| 112 int DoWaitForQuota(); | |
| 113 int DoWaitForQuotaComplete(); | |
| 114 int DoRead(); | |
| 115 int DoReadComplete(int result, Channel::ChannelState* channel_state); | |
| 116 | |
| 117 State next_state_ = STATE_NONE; | |
| 118 uint64_t expected_size_ = 0; | |
| 119 uint64_t bytes_left_ = 0; | |
| 120 net::CompletionCallback callback_; | |
| 121 scoped_refptr<net::IOBuffer> buffer_; | |
| 122 scoped_ptr<storage::BlobReader> reader_; | |
| 123 const scoped_ptr<Channel> channel_; | |
| 124 | |
| 125 DISALLOW_COPY_AND_ASSIGN(WebSocketBlobSender); | |
| 126 }; | |
| 127 | |
| 128 } // namespace content | |
| 129 | |
| 130 #endif // CONTENT_BROWSER_RENDERER_HOST_WEBSOCKET_BLOB_SENDER_H_ | |
| OLD | NEW |