1 // Copyright (c) 2012 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.
5 #include "net/url_request/url_request_simple_job.h"
8 #include "base/compiler_specific.h"
9 #include "base/message_loop/message_loop.h"
10 #include "net/base/io_buffer.h"
11 #include "net/base/net_errors.h"
12 #include "net/url_request/url_request_status.h"
16 URLRequestSimpleJob::URLRequestSimpleJob(
17 URLRequest* request, NetworkDelegate* network_delegate)
18 : URLRequestJob(request, network_delegate),
20 weak_factory_(this) {}
22 void URLRequestSimpleJob::Start() {
23 // Start reading asynchronously so that all error reporting and data
24 // callbacks happen as they would for network requests.
25 base::MessageLoop::current()->PostTask(
27 base::Bind(&URLRequestSimpleJob::StartAsync, weak_factory_.GetWeakPtr()));
30 bool URLRequestSimpleJob::GetMimeType(std::string* mime_type) const {
31 *mime_type = mime_type_;
35 bool URLRequestSimpleJob::GetCharset(std::string* charset) {
40 URLRequestSimpleJob::~URLRequestSimpleJob() {}
42 bool URLRequestSimpleJob::ReadRawData(IOBuffer* buf, int buf_size,
45 int remaining = static_cast<int>(data_.size()) - data_offset_;
46 if (buf_size > remaining)
48 memcpy(buf->data(), data_.data() + data_offset_, buf_size);
49 data_offset_ += buf_size;
50 *bytes_read = buf_size;
54 void URLRequestSimpleJob::StartAsync() {
58 int result = GetData(&mime_type_, &charset_, &data_,
59 base::Bind(&URLRequestSimpleJob::OnGetDataCompleted,
60 weak_factory_.GetWeakPtr()));
61 if (result != ERR_IO_PENDING)
62 OnGetDataCompleted(result);
65 void URLRequestSimpleJob::OnGetDataCompleted(int result) {
67 // Notify that the headers are complete
68 NotifyHeadersComplete();
70 NotifyStartError(URLRequestStatus(URLRequestStatus::FAILED, result));