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 "headless/public/util/in_memory_request_job.h" |
| 6 |
| 7 #include "base/threading/thread_task_runner_handle.h" |
| 8 #include "headless/public/util/in_memory_protocol_handler.h" |
| 9 #include "net/base/io_buffer.h" |
| 10 #include "net/base/net_errors.h" |
| 11 #include "net/url_request/url_request_status.h" |
| 12 |
| 13 namespace headless { |
| 14 |
| 15 InMemoryRequestJob::InMemoryRequestJob( |
| 16 net::URLRequest* request, |
| 17 net::NetworkDelegate* network_delegate, |
| 18 const InMemoryProtocolHandler::Response* response) |
| 19 : net::URLRequestJob(request, network_delegate), |
| 20 response_(response), |
| 21 data_offset_(0), |
| 22 weak_factory_(this) {} |
| 23 |
| 24 InMemoryRequestJob::~InMemoryRequestJob() {} |
| 25 |
| 26 void InMemoryRequestJob::Start() { |
| 27 // Start reading asynchronously so that all error reporting and data |
| 28 // callbacks happen as they would for network requests. |
| 29 base::ThreadTaskRunnerHandle::Get()->PostTask( |
| 30 FROM_HERE, |
| 31 base::Bind(&InMemoryRequestJob::StartAsync, weak_factory_.GetWeakPtr())); |
| 32 } |
| 33 |
| 34 void InMemoryRequestJob::StartAsync() { |
| 35 if (!response_) { |
| 36 NotifyStartError(net::URLRequestStatus(net::URLRequestStatus::FAILED, |
| 37 net::ERR_FILE_NOT_FOUND)); |
| 38 return; |
| 39 } |
| 40 NotifyHeadersComplete(); |
| 41 } |
| 42 |
| 43 void InMemoryRequestJob::Kill() { |
| 44 weak_factory_.InvalidateWeakPtrs(); |
| 45 URLRequestJob::Kill(); |
| 46 } |
| 47 |
| 48 int InMemoryRequestJob::ReadRawData(net::IOBuffer* buf, int buf_size) { |
| 49 DCHECK(response_); |
| 50 DCHECK_LE(data_offset_, response_->data.size()); |
| 51 int remaining = |
| 52 base::checked_cast<int>(response_->data.size() - data_offset_); |
| 53 if (buf_size > remaining) |
| 54 buf_size = remaining; |
| 55 memcpy(buf->data(), response_->data.data() + data_offset_, buf_size); |
| 56 data_offset_ += buf_size; |
| 57 return buf_size; |
| 58 } |
| 59 |
| 60 bool InMemoryRequestJob::GetMimeType(std::string* mime_type) const { |
| 61 *mime_type = response_->mime_type; |
| 62 return true; |
| 63 } |
| 64 |
| 65 } // namespace headless |
OLD | NEW |