blob: 0b95d38317a3a162df39f99c22471e092075b340 [file] [log] [blame]
license.botbf09a502008-08-24 00:55:551// 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.commit586acc5fe2008-07-26 22:42:524
5#include "net/url_request/url_request_simple_job.h"
6
7#include "base/message_loop.h"
[email protected]597cf6e2009-05-29 09:43:268#include "net/base/io_buffer.h"
initial.commit586acc5fe2008-07-26 22:42:529#include "net/base/net_errors.h"
[email protected]319d9e6f2009-02-18 19:47:2110#include "net/url_request/url_request_status.h"
initial.commit586acc5fe2008-07-26 22:42:5211
[email protected]86977552010-12-15 01:56:0312namespace net {
13
14URLRequestSimpleJob::URLRequestSimpleJob(URLRequest* request)
15 : URLRequestJob(request),
initial.commit586acc5fe2008-07-26 22:42:5216 data_offset_(0) {
17}
18
19void 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]60c413c92009-03-09 16:53:3126bool URLRequestSimpleJob::GetMimeType(std::string* mime_type) const {
initial.commit586acc5fe2008-07-26 22:42:5227 *mime_type = mime_type_;
28 return true;
29}
30
31bool URLRequestSimpleJob::GetCharset(std::string* charset) {
32 *charset = charset_;
33 return true;
34}
35
[email protected]86977552010-12-15 01:56:0336bool URLRequestSimpleJob::ReadRawData(IOBuffer* buf, int buf_size,
initial.commit586acc5fe2008-07-26 22:42:5237 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]9dea9e1f2009-01-29 00:30:4742 memcpy(buf->data(), data_.data() + data_offset_, buf_size);
initial.commit586acc5fe2008-07-26 22:42:5243 data_offset_ += buf_size;
44 *bytes_read = buf_size;
45 return true;
46}
47
48void 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]86977552010-12-15 01:56:0358 ERR_INVALID_URL));
initial.commit586acc5fe2008-07-26 22:42:5259 }
60}
[email protected]86977552010-12-15 01:56:0361
62} // namespace net