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.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 ALLOW_THIS_IN_INITIALIZER_LIST(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 MessageLoop::current()->PostTask(
27 base::Bind(&URLRequestSimpleJob::StartAsync
,
28 weak_factory_
.GetWeakPtr()));
31 bool URLRequestSimpleJob::GetMimeType(std::string
* mime_type
) const {
32 *mime_type
= mime_type_
;
36 bool URLRequestSimpleJob::GetCharset(std::string
* charset
) {
41 URLRequestSimpleJob::~URLRequestSimpleJob() {}
43 bool URLRequestSimpleJob::ReadRawData(IOBuffer
* buf
, int buf_size
,
46 int remaining
= static_cast<int>(data_
.size()) - data_offset_
;
47 if (buf_size
> remaining
)
49 memcpy(buf
->data(), data_
.data() + data_offset_
, buf_size
);
50 data_offset_
+= buf_size
;
51 *bytes_read
= buf_size
;
55 void URLRequestSimpleJob::StartAsync() {
59 int result
= GetData(&mime_type_
, &charset_
, &data_
,
60 base::Bind(&URLRequestSimpleJob::OnGetDataCompleted
,
61 weak_factory_
.GetWeakPtr()));
62 if (result
!= ERR_IO_PENDING
)
63 OnGetDataCompleted(result
);
66 void URLRequestSimpleJob::OnGetDataCompleted(int result
) {
68 // Notify that the headers are complete
69 NotifyHeadersComplete();
71 NotifyStartError(URLRequestStatus(URLRequestStatus::FAILED
, result
));