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/loader/mojo_async_resource_handler.h" |
| 6 |
| 7 #include <utility> |
| 8 |
| 9 #include "base/command_line.h" |
| 10 #include "base/containers/hash_tables.h" |
| 11 #include "base/logging.h" |
| 12 #include "base/macros.h" |
| 13 #include "base/strings/string_number_conversions.h" |
| 14 #include "base/time/time.h" |
| 15 #include "content/browser/loader/netlog_observer.h" |
| 16 #include "content/browser/loader/resource_dispatcher_host_impl.h" |
| 17 #include "content/browser/loader/resource_request_info_impl.h" |
| 18 #include "content/common/resource_request_completion_status.h" |
| 19 #include "content/public/browser/resource_dispatcher_host_delegate.h" |
| 20 #include "content/public/common/resource_response.h" |
| 21 #include "mojo/public/c/system/data_pipe.h" |
| 22 #include "mojo/public/cpp/system/data_pipe.h" |
| 23 #include "net/base/io_buffer.h" |
| 24 #include "net/base/load_flags.h" |
| 25 #include "net/base/mime_sniffer.h" |
| 26 #include "net/log/net_log.h" |
| 27 #include "net/url_request/redirect_info.h" |
| 28 |
| 29 namespace content { |
| 30 namespace { |
| 31 |
| 32 int g_allocation_size = MojoAsyncResourceHandler::kDefaultAllocationSize; |
| 33 |
| 34 // MimeTypeResourceHandler *implicitly* requires that the buffer size |
| 35 // returned from OnWillRead should be larger than certain size. |
| 36 // TODO(yhirano): Fix MimeTypeResourceHandler. |
| 37 constexpr size_t kMinAllocationSize = 2 * net::kMaxBytesToSniff; |
| 38 |
| 39 constexpr size_t kMaxChunkSize = 32 * 1024; |
| 40 |
| 41 void GetNumericArg(const std::string& name, int* result) { |
| 42 const std::string& value = |
| 43 base::CommandLine::ForCurrentProcess()->GetSwitchValueASCII(name); |
| 44 if (!value.empty()) |
| 45 base::StringToInt(value, result); |
| 46 } |
| 47 |
| 48 void InitializeResourceBufferConstants() { |
| 49 static bool did_init = false; |
| 50 if (did_init) |
| 51 return; |
| 52 did_init = true; |
| 53 |
| 54 GetNumericArg("resource-buffer-size", &g_allocation_size); |
| 55 } |
| 56 |
| 57 } // namespace |
| 58 |
| 59 // This class is for sharing the ownership of a ScopedDataPipeProducerHandle |
| 60 // between WriterIOBuffer and MojoAsyncResourceHandler. |
| 61 class MojoAsyncResourceHandler::SharedWriter final |
| 62 : public base::RefCountedThreadSafe<SharedWriter> { |
| 63 public: |
| 64 explicit SharedWriter(mojo::ScopedDataPipeProducerHandle writer) |
| 65 : writer_(std::move(writer)) {} |
| 66 mojo::DataPipeProducerHandle writer() { return writer_.get(); } |
| 67 |
| 68 private: |
| 69 friend class base::RefCountedThreadSafe<SharedWriter>; |
| 70 ~SharedWriter() {} |
| 71 |
| 72 const mojo::ScopedDataPipeProducerHandle writer_; |
| 73 |
| 74 DISALLOW_COPY_AND_ASSIGN(SharedWriter); |
| 75 }; |
| 76 |
| 77 // This class is a IOBuffer subclass for data gotten from a |
| 78 // ScopedDataPipeProducerHandle. |
| 79 class MojoAsyncResourceHandler::WriterIOBuffer final |
| 80 : public net::IOBufferWithSize { |
| 81 public: |
| 82 // |data| and |size| should be gotten from |writer| via BeginWriteDataRaw. |
| 83 // They will be accesible via IOBuffer methods. As |writer| is stored in this |
| 84 // instance, |data| will be kept valid as long as the following conditions |
| 85 // hold: |
| 86 // 1. |data| is not invalidated via EndWriteDataRaw. |
| 87 // 2. |this| instance is alive. |
| 88 WriterIOBuffer(scoped_refptr<SharedWriter> writer, void* data, size_t size) |
| 89 : net::IOBufferWithSize(static_cast<char*>(data), size), |
| 90 writer_(std::move(writer)) {} |
| 91 |
| 92 private: |
| 93 ~WriterIOBuffer() override { |
| 94 // Avoid deleting |data_| in the IOBuffer destructor. |
| 95 data_ = nullptr; |
| 96 } |
| 97 |
| 98 // This member is for keeping the writer alive. |
| 99 scoped_refptr<SharedWriter> writer_; |
| 100 |
| 101 DISALLOW_COPY_AND_ASSIGN(WriterIOBuffer); |
| 102 }; |
| 103 |
| 104 MojoAsyncResourceHandler::MojoAsyncResourceHandler( |
| 105 net::URLRequest* request, |
| 106 ResourceDispatcherHostImpl* rdh, |
| 107 mojo::InterfaceRequest<mojom::URLLoader> mojo_request, |
| 108 mojom::URLLoaderClientPtr url_loader_client) |
| 109 : ResourceHandler(request), |
| 110 rdh_(rdh), |
| 111 binding_(this, std::move(mojo_request)), |
| 112 url_loader_client_(std::move(url_loader_client)) { |
| 113 DCHECK(url_loader_client_); |
| 114 InitializeResourceBufferConstants(); |
| 115 } |
| 116 |
| 117 MojoAsyncResourceHandler::~MojoAsyncResourceHandler() { |
| 118 if (has_checked_for_sufficient_resources_) |
| 119 rdh_->FinishedWithResourcesForRequest(request()); |
| 120 } |
| 121 |
| 122 bool MojoAsyncResourceHandler::OnRequestRedirected( |
| 123 const net::RedirectInfo& redirect_info, |
| 124 ResourceResponse* response, |
| 125 bool* defer) { |
| 126 // Not implemented. |
| 127 return false; |
| 128 } |
| 129 |
| 130 bool MojoAsyncResourceHandler::OnResponseStarted(ResourceResponse* response, |
| 131 bool* defer) { |
| 132 const ResourceRequestInfoImpl* info = GetRequestInfo(); |
| 133 |
| 134 if (rdh_->delegate()) { |
| 135 rdh_->delegate()->OnResponseStarted(request(), info->GetContext(), |
| 136 response); |
| 137 } |
| 138 |
| 139 NetLogObserver::PopulateResponseInfo(request(), response); |
| 140 |
| 141 response->head.request_start = request()->creation_time(); |
| 142 response->head.response_start = base::TimeTicks::Now(); |
| 143 sent_received_response_message_ = true; |
| 144 url_loader_client_->OnReceiveResponse(response->head); |
| 145 return true; |
| 146 } |
| 147 |
| 148 bool MojoAsyncResourceHandler::OnWillStart(const GURL& url, bool* defer) { |
| 149 return true; |
| 150 } |
| 151 |
| 152 bool MojoAsyncResourceHandler::OnWillRead(scoped_refptr<net::IOBuffer>* buf, |
| 153 int* buf_size, |
| 154 int min_size) { |
| 155 DCHECK_EQ(-1, min_size); |
| 156 |
| 157 if (!CheckForSufficientResource()) |
| 158 return false; |
| 159 |
| 160 if (!shared_writer_) { |
| 161 MojoCreateDataPipeOptions options; |
| 162 options.struct_size = sizeof(MojoCreateDataPipeOptions); |
| 163 options.flags = MOJO_CREATE_DATA_PIPE_OPTIONS_FLAG_NONE; |
| 164 options.element_num_bytes = 1; |
| 165 options.capacity_num_bytes = g_allocation_size; |
| 166 mojo::DataPipe data_pipe(options); |
| 167 |
| 168 url_loader_client_->OnStartLoadingResponseBody( |
| 169 std::move(data_pipe.consumer_handle)); |
| 170 if (!data_pipe.producer_handle.is_valid()) |
| 171 return false; |
| 172 |
| 173 shared_writer_ = new SharedWriter(std::move(data_pipe.producer_handle)); |
| 174 handle_watcher_.Start(shared_writer_->writer(), MOJO_HANDLE_SIGNAL_WRITABLE, |
| 175 base::Bind(&MojoAsyncResourceHandler::OnWritable, |
| 176 base::Unretained(this))); |
| 177 |
| 178 bool defer = false; |
| 179 scoped_refptr<net::IOBufferWithSize> buffer; |
| 180 if (!AllocateWriterIOBuffer(&buffer, &defer)) |
| 181 return false; |
| 182 if (!defer) { |
| 183 if (static_cast<size_t>(buffer->size()) >= kMinAllocationSize) { |
| 184 *buf = buffer_ = buffer; |
| 185 *buf_size = buffer_->size(); |
| 186 return true; |
| 187 } |
| 188 |
| 189 // The allocated buffer is too small. |
| 190 if (EndWrite(0) != MOJO_RESULT_OK) |
| 191 return false; |
| 192 } |
| 193 DCHECK(!is_using_io_buffer_not_from_writer_); |
| 194 is_using_io_buffer_not_from_writer_ = true; |
| 195 buffer_ = new net::IOBufferWithSize(kMinAllocationSize); |
| 196 } |
| 197 |
| 198 DCHECK_EQ(0u, buffer_offset_); |
| 199 *buf = buffer_; |
| 200 *buf_size = buffer_->size(); |
| 201 return true; |
| 202 } |
| 203 |
| 204 bool MojoAsyncResourceHandler::OnReadCompleted(int bytes_read, bool* defer) { |
| 205 DCHECK_GE(bytes_read, 0); |
| 206 DCHECK(buffer_); |
| 207 |
| 208 if (!bytes_read) |
| 209 return true; |
| 210 |
| 211 if (is_using_io_buffer_not_from_writer_) { |
| 212 // Couldn't allocate a buffer on the data pipe in OnWillRead. |
| 213 DCHECK_EQ(0u, buffer_bytes_read_); |
| 214 buffer_bytes_read_ = bytes_read; |
| 215 if (!CopyReadDataToDataPipe(defer)) |
| 216 return false; |
| 217 if (*defer) |
| 218 OnDefer(); |
| 219 return true; |
| 220 } |
| 221 |
| 222 if (EndWrite(bytes_read) != MOJO_RESULT_OK) |
| 223 return false; |
| 224 // Allocate a buffer for the next OnWillRead call here, because OnWillRead |
| 225 // doesn't have |defer| parameter. |
| 226 if (!AllocateWriterIOBuffer(&buffer_, defer)) |
| 227 return false; |
| 228 if (*defer) |
| 229 OnDefer(); |
| 230 return true; |
| 231 } |
| 232 |
| 233 void MojoAsyncResourceHandler::OnDataDownloaded(int bytes_downloaded) { |
| 234 // Not implemented. |
| 235 } |
| 236 |
| 237 void MojoAsyncResourceHandler::FollowRedirect() { |
| 238 NOTIMPLEMENTED(); |
| 239 } |
| 240 |
| 241 void MojoAsyncResourceHandler::Cancel() { |
| 242 NOTIMPLEMENTED(); |
| 243 } |
| 244 |
| 245 void MojoAsyncResourceHandler::ResumeForTesting() { |
| 246 Resume(); |
| 247 } |
| 248 |
| 249 void MojoAsyncResourceHandler::SetAllocationSizeForTesting(size_t size) { |
| 250 g_allocation_size = size; |
| 251 } |
| 252 |
| 253 MojoResult MojoAsyncResourceHandler::BeginWrite(void** data, |
| 254 uint32_t* available) { |
| 255 MojoResult result = mojo::BeginWriteDataRaw( |
| 256 shared_writer_->writer(), data, available, MOJO_WRITE_DATA_FLAG_NONE); |
| 257 if (result == MOJO_RESULT_OK) |
| 258 *available = std::min(*available, static_cast<uint32_t>(kMaxChunkSize)); |
| 259 return result; |
| 260 } |
| 261 |
| 262 MojoResult MojoAsyncResourceHandler::EndWrite(uint32_t written) { |
| 263 return mojo::EndWriteDataRaw(shared_writer_->writer(), written); |
| 264 } |
| 265 |
| 266 void MojoAsyncResourceHandler::OnResponseCompleted( |
| 267 const net::URLRequestStatus& status, |
| 268 const std::string& security_info, |
| 269 bool* defer) { |
| 270 shared_writer_ = nullptr; |
| 271 buffer_ = nullptr; |
| 272 handle_watcher_.Cancel(); |
| 273 |
| 274 const ResourceRequestInfoImpl* info = GetRequestInfo(); |
| 275 |
| 276 // TODO(gavinp): Remove this CHECK when we figure out the cause of |
| 277 // http://crbug.com/124680 . This check mirrors closely check in |
| 278 // WebURLLoaderImpl::OnCompletedRequest that routes this message to a WebCore |
| 279 // ResourceHandleInternal which asserts on its state and crashes. By crashing |
| 280 // when the message is sent, we should get better crash reports. |
| 281 CHECK(status.status() != net::URLRequestStatus::SUCCESS || |
| 282 sent_received_response_message_); |
| 283 |
| 284 int error_code = status.error(); |
| 285 bool was_ignored_by_handler = info->WasIgnoredByHandler(); |
| 286 |
| 287 DCHECK_NE(status.status(), net::URLRequestStatus::IO_PENDING); |
| 288 // If this check fails, then we're in an inconsistent state because all |
| 289 // requests ignored by the handler should be canceled (which should result in |
| 290 // the ERR_ABORTED error code). |
| 291 DCHECK(!was_ignored_by_handler || error_code == net::ERR_ABORTED); |
| 292 |
| 293 ResourceRequestCompletionStatus request_complete_data; |
| 294 request_complete_data.error_code = error_code; |
| 295 request_complete_data.was_ignored_by_handler = was_ignored_by_handler; |
| 296 request_complete_data.exists_in_cache = request()->response_info().was_cached; |
| 297 request_complete_data.security_info = security_info; |
| 298 request_complete_data.completion_time = base::TimeTicks::Now(); |
| 299 request_complete_data.encoded_data_length = |
| 300 request()->GetTotalReceivedBytes(); |
| 301 |
| 302 url_loader_client_->OnComplete(request_complete_data); |
| 303 } |
| 304 |
| 305 bool MojoAsyncResourceHandler::CopyReadDataToDataPipe(bool* defer) { |
| 306 while (true) { |
| 307 scoped_refptr<net::IOBufferWithSize> dest; |
| 308 if (!AllocateWriterIOBuffer(&dest, defer)) |
| 309 return false; |
| 310 if (*defer) |
| 311 return true; |
| 312 if (buffer_bytes_read_ == 0) { |
| 313 // All bytes are copied. Save the buffer for the next OnWillRead call. |
| 314 buffer_ = std::move(dest); |
| 315 return true; |
| 316 } |
| 317 |
| 318 size_t copied_size = |
| 319 std::min(buffer_bytes_read_, static_cast<size_t>(dest->size())); |
| 320 memcpy(dest->data(), buffer_->data() + buffer_offset_, copied_size); |
| 321 buffer_offset_ += copied_size; |
| 322 buffer_bytes_read_ -= copied_size; |
| 323 if (EndWrite(copied_size) != MOJO_RESULT_OK) |
| 324 return false; |
| 325 |
| 326 if (buffer_bytes_read_ == 0) { |
| 327 // All bytes are copied. |
| 328 buffer_offset_ = 0; |
| 329 is_using_io_buffer_not_from_writer_ = false; |
| 330 } |
| 331 } |
| 332 } |
| 333 |
| 334 bool MojoAsyncResourceHandler::AllocateWriterIOBuffer( |
| 335 scoped_refptr<net::IOBufferWithSize>* buf, |
| 336 bool* defer) { |
| 337 void* data = nullptr; |
| 338 uint32_t available = 0; |
| 339 MojoResult result = BeginWrite(&data, &available); |
| 340 if (result == MOJO_RESULT_SHOULD_WAIT) { |
| 341 *defer = true; |
| 342 return true; |
| 343 } |
| 344 if (result != MOJO_RESULT_OK) |
| 345 return false; |
| 346 *buf = new WriterIOBuffer(shared_writer_, data, available); |
| 347 return true; |
| 348 } |
| 349 |
| 350 void MojoAsyncResourceHandler::Resume() { |
| 351 if (!did_defer_) |
| 352 return; |
| 353 bool defer = false; |
| 354 if (is_using_io_buffer_not_from_writer_) { |
| 355 // |buffer_| is set to a net::IOBufferWithSize. Write the buffer contents |
| 356 // to the data pipe. |
| 357 DCHECK_GT(buffer_bytes_read_, 0u); |
| 358 if (!CopyReadDataToDataPipe(&defer)) { |
| 359 controller()->CancelWithError(net::ERR_FAILED); |
| 360 return; |
| 361 } |
| 362 } else { |
| 363 // Allocate a buffer for the next OnWillRead call here. |
| 364 if (!AllocateWriterIOBuffer(&buffer_, &defer)) { |
| 365 controller()->CancelWithError(net::ERR_FAILED); |
| 366 return; |
| 367 } |
| 368 } |
| 369 |
| 370 if (defer) { |
| 371 // Continue waiting. |
| 372 return; |
| 373 } |
| 374 did_defer_ = false; |
| 375 request()->LogUnblocked(); |
| 376 controller()->Resume(); |
| 377 } |
| 378 |
| 379 void MojoAsyncResourceHandler::OnDefer() { |
| 380 request()->LogBlockedBy("MojoAsyncResourceHandler"); |
| 381 did_defer_ = true; |
| 382 } |
| 383 |
| 384 bool MojoAsyncResourceHandler::CheckForSufficientResource() { |
| 385 if (has_checked_for_sufficient_resources_) |
| 386 return true; |
| 387 has_checked_for_sufficient_resources_ = true; |
| 388 |
| 389 if (rdh_->HasSufficientResourcesForRequest(request())) |
| 390 return true; |
| 391 |
| 392 controller()->CancelWithError(net::ERR_INSUFFICIENT_RESOURCES); |
| 393 return false; |
| 394 } |
| 395 |
| 396 void MojoAsyncResourceHandler::OnWritable(MojoResult unused) { |
| 397 Resume(); |
| 398 } |
| 399 |
| 400 } // namespace content |
OLD | NEW |