1// Copyright (c) 2011 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 "net/url_request/url_request_simple_job.h" 6 7#include "base/compiler_specific.h" 8#include "base/message_loop.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 13namespace net { 14 15URLRequestSimpleJob::URLRequestSimpleJob(URLRequest* request) 16 : URLRequestJob(request), 17 data_offset_(0), 18 ALLOW_THIS_IN_INITIALIZER_LIST(method_factory_(this)) {} 19 20void URLRequestSimpleJob::Start() { 21 // Start reading asynchronously so that all error reporting and data 22 // callbacks happen as they would for network requests. 23 MessageLoop::current()->PostTask( 24 FROM_HERE, 25 method_factory_.NewRunnableMethod(&URLRequestSimpleJob::StartAsync)); 26} 27 28bool URLRequestSimpleJob::GetMimeType(std::string* mime_type) const { 29 *mime_type = mime_type_; 30 return true; 31} 32 33bool URLRequestSimpleJob::GetCharset(std::string* charset) { 34 *charset = charset_; 35 return true; 36} 37 38URLRequestSimpleJob::~URLRequestSimpleJob() {} 39 40bool URLRequestSimpleJob::ReadRawData(IOBuffer* buf, int buf_size, 41 int* bytes_read) { 42 DCHECK(bytes_read); 43 int remaining = static_cast<int>(data_.size()) - data_offset_; 44 if (buf_size > remaining) 45 buf_size = remaining; 46 memcpy(buf->data(), data_.data() + data_offset_, buf_size); 47 data_offset_ += buf_size; 48 *bytes_read = buf_size; 49 return true; 50} 51 52void URLRequestSimpleJob::StartAsync() { 53 if (!request_) 54 return; 55 56 if (GetData(&mime_type_, &charset_, &data_)) { 57 // Notify that the headers are complete 58 NotifyHeadersComplete(); 59 } else { 60 // what should the error code be? 61 NotifyStartError(URLRequestStatus(URLRequestStatus::FAILED, 62 ERR_INVALID_URL)); 63 } 64} 65 66} // namespace net 67