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 #include "content/browser/renderer_host/websocket_blob_sender.h" |
| 6 |
| 7 #include <algorithm> |
| 8 #include <ostream> |
| 9 #include <utility> |
| 10 |
| 11 #include "base/bind.h" |
| 12 #include "base/bind_helpers.h" |
| 13 #include "base/callback_helpers.h" |
| 14 #include "base/logging.h" |
| 15 #include "base/numerics/safe_conversions.h" |
| 16 #include "content/browser/renderer_host/websocket_dispatcher_host.h" |
| 17 #include "content/browser/renderer_host/websocket_host.h" |
| 18 #include "net/base/io_buffer.h" |
| 19 #include "net/base/net_errors.h" |
| 20 #include "net/websockets/websocket_channel.h" |
| 21 #include "net/websockets/websocket_frame.h" |
| 22 #include "storage/browser/blob/blob_data_handle.h" |
| 23 #include "storage/browser/blob/blob_reader.h" |
| 24 #include "storage/browser/blob/blob_storage_context.h" |
| 25 |
| 26 namespace content { |
| 27 |
| 28 namespace { |
| 29 |
| 30 using storage::BlobReader; |
| 31 using storage::BlobDataHandle; |
| 32 using storage::BlobStorageContext; |
| 33 |
| 34 // This must be smaller than the send quota high water mark or this class will |
| 35 // never send anything. |
| 36 const int kMinimumNonFinalFrameSize = 8 * 1024; |
| 37 |
| 38 // The IOBuffer has a fixed size for simplicity. |
| 39 const size_t kBufferSize = 128 * 1024; |
| 40 |
| 41 } // namespace |
| 42 |
| 43 // This is needed to make DCHECK_EQ(), etc. compile. |
| 44 std::ostream& operator<<(std::ostream& os, WebSocketBlobSender::State state) { |
| 45 static const char* const kStateStrings[] = { |
| 46 "NONE", |
| 47 "READ_SIZE", |
| 48 "READ_SIZE_COMPLETE", |
| 49 "WAIT_FOR_QUOTA", |
| 50 "WAIT_FOR_QUOTA_COMPLETE", |
| 51 "READ", |
| 52 "READ_COMPLETE", |
| 53 }; |
| 54 if (state < WebSocketBlobSender::State::NONE || |
| 55 state > WebSocketBlobSender::State::READ_COMPLETE) { |
| 56 return os << "Bad State (" << static_cast<int>(state) << ")"; |
| 57 } |
| 58 return os << kStateStrings[static_cast<int>(state)]; |
| 59 } |
| 60 |
| 61 WebSocketBlobSender::WebSocketBlobSender(scoped_ptr<Channel> channel) |
| 62 : channel_(std::move(channel)) {} |
| 63 |
| 64 WebSocketBlobSender::~WebSocketBlobSender() {} |
| 65 |
| 66 int WebSocketBlobSender::Start( |
| 67 const std::string& uuid, |
| 68 uint64_t expected_size, |
| 69 BlobStorageContext* context, |
| 70 storage::FileSystemContext* file_system_context, |
| 71 base::SingleThreadTaskRunner* file_task_runner, |
| 72 net::WebSocketEventInterface::ChannelState* channel_state, |
| 73 const net::CompletionCallback& callback) { |
| 74 DCHECK(context); |
| 75 DCHECK(channel_state); |
| 76 scoped_ptr<storage::BlobDataHandle> data_handle( |
| 77 context->GetBlobDataFromUUID(uuid)); |
| 78 if (!data_handle) |
| 79 return net::ERR_INVALID_HANDLE; |
| 80 reader_ = data_handle->CreateReader(file_system_context, file_task_runner); |
| 81 expected_size_ = expected_size; |
| 82 next_state_ = State::READ_SIZE; |
| 83 int rv = DoLoop(net::OK, channel_state); |
| 84 if (*channel_state == net::WebSocketEventInterface::CHANNEL_ALIVE && |
| 85 rv == net::ERR_IO_PENDING) { |
| 86 callback_ = callback; |
| 87 } |
| 88 return rv; |
| 89 } |
| 90 |
| 91 void WebSocketBlobSender::OnNewSendQuota() { |
| 92 if (next_state_ == State::WAIT_FOR_QUOTA) |
| 93 DoLoopAsync(net::OK); |
| 94 // |this| may be deleted. |
| 95 } |
| 96 |
| 97 uint64_t WebSocketBlobSender::ActualSize() const { |
| 98 return reader_->total_size(); |
| 99 } |
| 100 |
| 101 void WebSocketBlobSender::OnReadComplete(int rv) { |
| 102 CHECK_EQ(State::READ_COMPLETE, next_state_); |
| 103 DoLoopAsync(rv); |
| 104 // |this| may be deleted. |
| 105 } |
| 106 |
| 107 void WebSocketBlobSender::OnSizeCalculated(int rv) { |
| 108 CHECK_EQ(State::READ_SIZE_COMPLETE, next_state_); |
| 109 DoLoopAsync(rv); |
| 110 // |this| may be deleted. |
| 111 } |
| 112 |
| 113 int WebSocketBlobSender::DoLoop(int result, |
| 114 Channel::ChannelState* channel_state) { |
| 115 CHECK_NE(State::NONE, next_state_); |
| 116 int rv = result; |
| 117 do { |
| 118 State state = next_state_; |
| 119 next_state_ = State::NONE; |
| 120 switch (state) { |
| 121 case State::READ_SIZE: |
| 122 DCHECK_EQ(net::OK, rv); |
| 123 rv = DoReadSize(); |
| 124 break; |
| 125 |
| 126 case State::READ_SIZE_COMPLETE: |
| 127 rv = DoReadSizeComplete(rv); |
| 128 break; |
| 129 |
| 130 case State::WAIT_FOR_QUOTA: |
| 131 DCHECK_EQ(net::OK, rv); |
| 132 rv = DoWaitForQuota(); |
| 133 break; |
| 134 |
| 135 case State::WAIT_FOR_QUOTA_COMPLETE: |
| 136 DCHECK_EQ(net::OK, rv); |
| 137 rv = DoWaitForQuotaComplete(); |
| 138 break; |
| 139 |
| 140 case State::READ: |
| 141 DCHECK_EQ(net::OK, rv); |
| 142 rv = DoRead(); |
| 143 break; |
| 144 |
| 145 case State::READ_COMPLETE: |
| 146 rv = DoReadComplete(rv, channel_state); |
| 147 break; |
| 148 |
| 149 default: |
| 150 NOTREACHED(); |
| 151 break; |
| 152 } |
| 153 } while (*channel_state != net::WebSocketEventInterface::CHANNEL_DELETED && |
| 154 rv != net::ERR_IO_PENDING && next_state_ != State::NONE); |
| 155 return rv; |
| 156 } |
| 157 |
| 158 void WebSocketBlobSender::DoLoopAsync(int result) { |
| 159 Channel::ChannelState channel_state = |
| 160 net::WebSocketEventInterface::CHANNEL_ALIVE; |
| 161 int rv = DoLoop(result, &channel_state); |
| 162 if (channel_state == net::WebSocketEventInterface::CHANNEL_ALIVE && |
| 163 rv != net::ERR_IO_PENDING) { |
| 164 ResetAndReturn(&callback_).Run(rv); |
| 165 } |
| 166 // |this| may be deleted. |
| 167 } |
| 168 |
| 169 int WebSocketBlobSender::DoReadSize() { |
| 170 next_state_ = State::READ_SIZE_COMPLETE; |
| 171 // This use of base::Unretained() is safe because BlobReader cannot call the |
| 172 // callback after it has been destroyed, and it is owned by this object. |
| 173 BlobReader::Status status = reader_->CalculateSize(base::Bind( |
| 174 &WebSocketBlobSender::OnSizeCalculated, base::Unretained(this))); |
| 175 switch (status) { |
| 176 case BlobReader::Status::NET_ERROR: |
| 177 return reader_->net_error(); |
| 178 |
| 179 case BlobReader::Status::IO_PENDING: |
| 180 return net::ERR_IO_PENDING; |
| 181 |
| 182 case BlobReader::Status::DONE: |
| 183 return net::OK; |
| 184 } |
| 185 NOTREACHED(); |
| 186 return net::ERR_UNEXPECTED; |
| 187 } |
| 188 |
| 189 int WebSocketBlobSender::DoReadSizeComplete(int result) { |
| 190 if (result < 0) |
| 191 return result; |
| 192 if (reader_->total_size() != expected_size_) |
| 193 return net::ERR_UPLOAD_FILE_CHANGED; |
| 194 bytes_left_ = expected_size_; |
| 195 // The result of the call to std::min() must fit inside a size_t because |
| 196 // kBufferSize is type size_t. |
| 197 size_t buffer_size = static_cast<size_t>( |
| 198 std::min(bytes_left_, base::strict_cast<uint64_t>(kBufferSize))); |
| 199 buffer_ = new net::IOBuffer(buffer_size); |
| 200 next_state_ = State::WAIT_FOR_QUOTA; |
| 201 return net::OK; |
| 202 } |
| 203 |
| 204 // The WAIT_FOR_QUOTA state has a self-edge; it will wait in this state until |
| 205 // there is enough quota to send some data. |
| 206 int WebSocketBlobSender::DoWaitForQuota() { |
| 207 int quota = channel_->GetSendQuota(); |
| 208 if (kMinimumNonFinalFrameSize <= quota || |
| 209 bytes_left_ <= base::checked_cast<uint64_t>(quota)) { |
| 210 next_state_ = State::WAIT_FOR_QUOTA_COMPLETE; |
| 211 return net::OK; |
| 212 } |
| 213 next_state_ = State::WAIT_FOR_QUOTA; |
| 214 return net::ERR_IO_PENDING; |
| 215 } |
| 216 |
| 217 // State::WAIT_FOR_QUOTA_COMPLETE exists just to give the state machine the |
| 218 // expected shape. It should be mostly optimised out. |
| 219 int WebSocketBlobSender::DoWaitForQuotaComplete() { |
| 220 next_state_ = State::READ; |
| 221 return net::OK; |
| 222 } |
| 223 |
| 224 int WebSocketBlobSender::DoRead() { |
| 225 next_state_ = State::READ_COMPLETE; |
| 226 uint64_t quota = base::checked_cast<uint64_t>(channel_->GetSendQuota()); |
| 227 uint64_t desired_bytes = std::min(bytes_left_, quota); |
| 228 |
| 229 // |bytes_to_read| must fit in a size_t because |kBufferSize| is of type |
| 230 // size_t and so cannot be larger than its maximum value. For simplicity this |
| 231 // method only reads as many bytes as are currently needed. |
| 232 size_t bytes_to_read = static_cast<size_t>( |
| 233 std::min(desired_bytes, base::strict_cast<uint64_t>(kBufferSize))); |
| 234 int bytes_read = 0; |
| 235 DCHECK(reader_); |
| 236 DCHECK(buffer_); |
| 237 |
| 238 // This use of base::Unretained is safe because the BlobReader object won't |
| 239 // call the callback after it has been destroyed, and it belongs to this |
| 240 // object. |
| 241 BlobReader::Status status = reader_->Read( |
| 242 buffer_.get(), bytes_to_read, &bytes_read, |
| 243 base::Bind(&WebSocketBlobSender::OnReadComplete, base::Unretained(this))); |
| 244 |
| 245 switch (status) { |
| 246 case BlobReader::Status::NET_ERROR: |
| 247 return reader_->net_error(); |
| 248 |
| 249 case BlobReader::Status::IO_PENDING: |
| 250 return net::ERR_IO_PENDING; |
| 251 |
| 252 case BlobReader::Status::DONE: |
| 253 return bytes_read; |
| 254 } |
| 255 NOTREACHED(); |
| 256 return net::ERR_UNEXPECTED; |
| 257 } |
| 258 |
| 259 int WebSocketBlobSender::DoReadComplete(int result, |
| 260 Channel::ChannelState* channel_state) { |
| 261 if (result < 0) |
| 262 return result; |
| 263 DCHECK_GE(channel_->GetSendQuota(), result); |
| 264 uint64_t bytes_read = base::checked_cast<uint64_t>(result); |
| 265 CHECK_GE(bytes_left_, bytes_read); |
| 266 bytes_left_ -= bytes_read; |
| 267 bool fin = bytes_left_ == 0; |
| 268 std::vector<char> data(buffer_->data(), buffer_->data() + bytes_read); |
| 269 DCHECK(fin || data.size() > 0u) << "Non-final frames should be non-empty"; |
| 270 *channel_state = channel_->SendFrame(fin, data); |
| 271 if (*channel_state == net::WebSocketEventInterface::CHANNEL_DELETED) { |
| 272 // |this| is deleted. |
| 273 return net::ERR_CONNECTION_RESET; |
| 274 } |
| 275 |
| 276 // It is important not to set next_state_ until after the call to SendFrame() |
| 277 // because SendFrame() will sometimes call OnNewSendQuota() synchronously. |
| 278 if (!fin) |
| 279 next_state_ = State::WAIT_FOR_QUOTA; |
| 280 return net::OK; |
| 281 } |
| 282 |
| 283 } // namespace content |
OLD | NEW |