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