license.bot | bf09a50 | 2008-08-24 00:55:55 | [diff] [blame] | 1 | // Copyright (c) 2006-2008 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. |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 4 | |
| 5 | #include "net/url_request/url_request_simple_job.h" |
| 6 | |
| 7 | #include "base/message_loop.h" |
[email protected] | 597cf6e | 2009-05-29 09:43:26 | [diff] [blame] | 8 | #include "net/base/io_buffer.h" |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 9 | #include "net/base/net_errors.h" |
[email protected] | 319d9e6f | 2009-02-18 19:47:21 | [diff] [blame] | 10 | #include "net/url_request/url_request_status.h" |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 11 | |
[email protected] | 8697755 | 2010-12-15 01:56:03 | [diff] [blame] | 12 | namespace net { |
| 13 | |
| 14 | URLRequestSimpleJob::URLRequestSimpleJob(URLRequest* request) |
| 15 | : URLRequestJob(request), |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 16 | data_offset_(0) { |
| 17 | } |
| 18 | |
| 19 | void URLRequestSimpleJob::Start() { |
| 20 | // Start reading asynchronously so that all error reporting and data |
| 21 | // callbacks happen as they would for network requests. |
| 22 | MessageLoop::current()->PostTask(FROM_HERE, NewRunnableMethod( |
| 23 | this, &URLRequestSimpleJob::StartAsync)); |
| 24 | } |
| 25 | |
[email protected] | 60c413c9 | 2009-03-09 16:53:31 | [diff] [blame] | 26 | bool URLRequestSimpleJob::GetMimeType(std::string* mime_type) const { |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 27 | *mime_type = mime_type_; |
| 28 | return true; |
| 29 | } |
| 30 | |
| 31 | bool URLRequestSimpleJob::GetCharset(std::string* charset) { |
| 32 | *charset = charset_; |
| 33 | return true; |
| 34 | } |
| 35 | |
[email protected] | 8697755 | 2010-12-15 01:56:03 | [diff] [blame] | 36 | bool URLRequestSimpleJob::ReadRawData(IOBuffer* buf, int buf_size, |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 37 | int* bytes_read) { |
| 38 | DCHECK(bytes_read); |
| 39 | int remaining = static_cast<int>(data_.size()) - data_offset_; |
| 40 | if (buf_size > remaining) |
| 41 | buf_size = remaining; |
[email protected] | 9dea9e1f | 2009-01-29 00:30:47 | [diff] [blame] | 42 | memcpy(buf->data(), data_.data() + data_offset_, buf_size); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 43 | data_offset_ += buf_size; |
| 44 | *bytes_read = buf_size; |
| 45 | return true; |
| 46 | } |
| 47 | |
| 48 | void URLRequestSimpleJob::StartAsync() { |
| 49 | if (!request_) |
| 50 | return; |
| 51 | |
| 52 | if (GetData(&mime_type_, &charset_, &data_)) { |
| 53 | // Notify that the headers are complete |
| 54 | NotifyHeadersComplete(); |
| 55 | } else { |
| 56 | // what should the error code be? |
| 57 | NotifyStartError(URLRequestStatus(URLRequestStatus::FAILED, |
[email protected] | 8697755 | 2010-12-15 01:56:03 | [diff] [blame] | 58 | ERR_INVALID_URL)); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 59 | } |
| 60 | } |
[email protected] | 8697755 | 2010-12-15 01:56:03 | [diff] [blame] | 61 | |
| 62 | } // namespace net |