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