| 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(std::unique_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 DCHECK(!reader_); | |
| 77 std::unique_ptr<storage::BlobDataHandle> data_handle( | |
| 78 context->GetBlobDataFromUUID(uuid)); | |
| 79 if (!data_handle) | |
| 80 return net::ERR_INVALID_HANDLE; | |
| 81 reader_ = data_handle->CreateReader(file_system_context, file_task_runner); | |
| 82 expected_size_ = expected_size; | |
| 83 next_state_ = State::READ_SIZE; | |
| 84 int rv = DoLoop(net::OK, channel_state); | |
| 85 if (*channel_state == net::WebSocketEventInterface::CHANNEL_ALIVE && | |
| 86 rv == net::ERR_IO_PENDING) { | |
| 87 callback_ = callback; | |
| 88 } | |
| 89 return rv; | |
| 90 } | |
| 91 | |
| 92 void WebSocketBlobSender::OnNewSendQuota() { | |
| 93 if (next_state_ == State::WAIT_FOR_QUOTA) | |
| 94 DoLoopAsync(net::OK); | |
| 95 // |this| may be deleted. | |
| 96 } | |
| 97 | |
| 98 uint64_t WebSocketBlobSender::ActualSize() const { | |
| 99 return reader_->total_size(); | |
| 100 } | |
| 101 | |
| 102 void WebSocketBlobSender::OnReadComplete(int rv) { | |
| 103 DCHECK_EQ(State::READ_COMPLETE, next_state_); | |
| 104 DoLoopAsync(rv); | |
| 105 // |this| may be deleted. | |
| 106 } | |
| 107 | |
| 108 void WebSocketBlobSender::OnSizeCalculated(int rv) { | |
| 109 DCHECK_EQ(State::READ_SIZE_COMPLETE, next_state_); | |
| 110 DoLoopAsync(rv); | |
| 111 // |this| may be deleted. | |
| 112 } | |
| 113 | |
| 114 int WebSocketBlobSender::DoLoop(int result, | |
| 115 Channel::ChannelState* channel_state) { | |
| 116 DCHECK_NE(State::NONE, next_state_); | |
| 117 int rv = result; | |
| 118 do { | |
| 119 State state = next_state_; | |
| 120 next_state_ = State::NONE; | |
| 121 switch (state) { | |
| 122 case State::READ_SIZE: | |
| 123 DCHECK_EQ(net::OK, rv); | |
| 124 rv = DoReadSize(); | |
| 125 break; | |
| 126 | |
| 127 case State::READ_SIZE_COMPLETE: | |
| 128 rv = DoReadSizeComplete(rv); | |
| 129 break; | |
| 130 | |
| 131 case State::WAIT_FOR_QUOTA: | |
| 132 DCHECK_EQ(net::OK, rv); | |
| 133 rv = DoWaitForQuota(); | |
| 134 break; | |
| 135 | |
| 136 case State::WAIT_FOR_QUOTA_COMPLETE: | |
| 137 DCHECK_EQ(net::OK, rv); | |
| 138 rv = DoWaitForQuotaComplete(); | |
| 139 break; | |
| 140 | |
| 141 case State::READ: | |
| 142 DCHECK_EQ(net::OK, rv); | |
| 143 rv = DoRead(); | |
| 144 break; | |
| 145 | |
| 146 case State::READ_COMPLETE: | |
| 147 rv = DoReadComplete(rv, channel_state); | |
| 148 break; | |
| 149 | |
| 150 default: | |
| 151 NOTREACHED(); | |
| 152 break; | |
| 153 } | |
| 154 } while (*channel_state != net::WebSocketEventInterface::CHANNEL_DELETED && | |
| 155 rv != net::ERR_IO_PENDING && next_state_ != State::NONE); | |
| 156 return rv; | |
| 157 } | |
| 158 | |
| 159 void WebSocketBlobSender::DoLoopAsync(int result) { | |
| 160 Channel::ChannelState channel_state = | |
| 161 net::WebSocketEventInterface::CHANNEL_ALIVE; | |
| 162 int rv = DoLoop(result, &channel_state); | |
| 163 if (channel_state == net::WebSocketEventInterface::CHANNEL_ALIVE && | |
| 164 rv != net::ERR_IO_PENDING) { | |
| 165 ResetAndReturn(&callback_).Run(rv); | |
| 166 } | |
| 167 // |this| may be deleted. | |
| 168 } | |
| 169 | |
| 170 int WebSocketBlobSender::DoReadSize() { | |
| 171 next_state_ = State::READ_SIZE_COMPLETE; | |
| 172 // This use of base::Unretained() is safe because BlobReader cannot call the | |
| 173 // callback after it has been destroyed, and it is owned by this object. | |
| 174 BlobReader::Status status = reader_->CalculateSize(base::Bind( | |
| 175 &WebSocketBlobSender::OnSizeCalculated, base::Unretained(this))); | |
| 176 switch (status) { | |
| 177 case BlobReader::Status::NET_ERROR: | |
| 178 return reader_->net_error(); | |
| 179 | |
| 180 case BlobReader::Status::IO_PENDING: | |
| 181 return net::ERR_IO_PENDING; | |
| 182 | |
| 183 case BlobReader::Status::DONE: | |
| 184 return net::OK; | |
| 185 } | |
| 186 NOTREACHED(); | |
| 187 return net::ERR_UNEXPECTED; | |
| 188 } | |
| 189 | |
| 190 int WebSocketBlobSender::DoReadSizeComplete(int result) { | |
| 191 if (result < 0) | |
| 192 return result; | |
| 193 if (reader_->total_size() != expected_size_) | |
| 194 return net::ERR_UPLOAD_FILE_CHANGED; | |
| 195 bytes_left_ = expected_size_; | |
| 196 // The result of the call to std::min() must fit inside a size_t because | |
| 197 // kBufferSize is type size_t. | |
| 198 size_t buffer_size = static_cast<size_t>( | |
| 199 std::min(bytes_left_, base::strict_cast<uint64_t>(kBufferSize))); | |
| 200 buffer_ = new net::IOBuffer(buffer_size); | |
| 201 next_state_ = State::WAIT_FOR_QUOTA; | |
| 202 return net::OK; | |
| 203 } | |
| 204 | |
| 205 // The WAIT_FOR_QUOTA state has a self-edge; it will wait in this state until | |
| 206 // there is enough quota to send some data. | |
| 207 int WebSocketBlobSender::DoWaitForQuota() { | |
| 208 size_t quota = channel_->GetSendQuota(); | |
| 209 if (kMinimumNonFinalFrameSize <= quota || bytes_left_ <= 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 size_t quota = channel_->GetSendQuota(); | |
| 227 // |desired_bytes| must fit in a size_t because |quota| is of type | |
| 228 // size_t and so cannot be larger than its maximum value. | |
| 229 size_t desired_bytes = | |
| 230 static_cast<size_t>(std::min(bytes_left_, static_cast<uint64_t>(quota))); | |
| 231 | |
| 232 // For simplicity this method only reads as many bytes as are currently | |
| 233 // needed. | |
| 234 size_t bytes_to_read = std::min(desired_bytes, kBufferSize); | |
| 235 int bytes_read = 0; | |
| 236 DCHECK(reader_); | |
| 237 DCHECK(buffer_); | |
| 238 | |
| 239 // This use of base::Unretained is safe because the BlobReader object won't | |
| 240 // call the callback after it has been destroyed, and it belongs to this | |
| 241 // object. | |
| 242 BlobReader::Status status = reader_->Read( | |
| 243 buffer_.get(), bytes_to_read, &bytes_read, | |
| 244 base::Bind(&WebSocketBlobSender::OnReadComplete, base::Unretained(this))); | |
| 245 | |
| 246 switch (status) { | |
| 247 case BlobReader::Status::NET_ERROR: | |
| 248 return reader_->net_error(); | |
| 249 | |
| 250 case BlobReader::Status::IO_PENDING: | |
| 251 return net::ERR_IO_PENDING; | |
| 252 | |
| 253 case BlobReader::Status::DONE: | |
| 254 return bytes_read; | |
| 255 } | |
| 256 NOTREACHED(); | |
| 257 return net::ERR_UNEXPECTED; | |
| 258 } | |
| 259 | |
| 260 int WebSocketBlobSender::DoReadComplete(int result, | |
| 261 Channel::ChannelState* channel_state) { | |
| 262 if (result < 0) | |
| 263 return result; | |
| 264 DCHECK_GE(channel_->GetSendQuota(), static_cast<size_t>(result)); | |
| 265 uint64_t bytes_read = static_cast<uint64_t>(result); | |
| 266 DCHECK_GE(bytes_left_, bytes_read); | |
| 267 bytes_left_ -= bytes_read; | |
| 268 bool fin = bytes_left_ == 0; | |
| 269 std::vector<char> data(buffer_->data(), buffer_->data() + bytes_read); | |
| 270 DCHECK(fin || data.size() > 0u) << "Non-final frames should be non-empty"; | |
| 271 *channel_state = channel_->SendFrame(fin, data); | |
| 272 if (*channel_state == net::WebSocketEventInterface::CHANNEL_DELETED) { | |
| 273 // |this| is deleted. | |
| 274 return net::ERR_CONNECTION_RESET; | |
| 275 } | |
| 276 | |
| 277 // It is important not to set next_state_ until after the call to SendFrame() | |
| 278 // because SendFrame() will sometimes call OnNewSendQuota() synchronously. | |
| 279 if (!fin) | |
| 280 next_state_ = State::WAIT_FOR_QUOTA; | |
| 281 return net::OK; | |
| 282 } | |
| 283 | |
| 284 } // namespace content | |
| OLD | NEW |