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"
10 #include "base/compiler_specific.h"
11 #include "base/message_loop/message_loop.h"
12 #include "base/profiler/scoped_tracker.h"
13 #include "net/base/io_buffer.h"
14 #include "net/base/net_errors.h"
15 #include "net/http/http_request_headers.h"
16 #include "net/http/http_util.h"
17 #include "net/url_request/url_request_status.h"
21 URLRequestSimpleJob::URLRequestSimpleJob(
22 URLRequest* request, NetworkDelegate* network_delegate)
23 : URLRangeRequestJob(request, network_delegate),
25 weak_factory_(this) {}
27 void URLRequestSimpleJob::Start() {
28 // Start reading asynchronously so that all error reporting and data
29 // callbacks happen as they would for network requests.
30 base::MessageLoop::current()->PostTask(
32 base::Bind(&URLRequestSimpleJob::StartAsync, weak_factory_.GetWeakPtr()));
35 bool URLRequestSimpleJob::GetMimeType(std::string* mime_type) const {
36 *mime_type = mime_type_;
40 bool URLRequestSimpleJob::GetCharset(std::string* charset) {
45 URLRequestSimpleJob::~URLRequestSimpleJob() {}
47 bool URLRequestSimpleJob::ReadRawData(IOBuffer* buf, int buf_size,
50 int remaining = byte_range_.last_byte_position() - data_offset_ + 1;
51 if (buf_size > remaining)
53 memcpy(buf->data(), data_.data() + data_offset_, buf_size);
54 data_offset_ += buf_size;
55 *bytes_read = buf_size;
59 void URLRequestSimpleJob::StartAsync() {
63 if (ranges().size() > 1) {
64 // TODO(vadimt): Remove ScopedTracker below once crbug.com/422489 is fixed.
65 tracked_objects::ScopedTracker tracking_profile(
66 FROM_HERE_WITH_EXPLICIT_FUNCTION(
67 "422489 URLRequestSimpleJob::StartAsync 1"));
69 NotifyDone(URLRequestStatus(URLRequestStatus::FAILED,
70 ERR_REQUEST_RANGE_NOT_SATISFIABLE));
74 if (!ranges().empty() && range_parse_result() == OK)
75 byte_range_ = ranges().front();
79 // TODO(vadimt): Remove ScopedTracker below once crbug.com/422489 is fixed.
80 // Remove the block and assign 'result' in its declaration.
81 tracked_objects::ScopedTracker tracking_profile(
82 FROM_HERE_WITH_EXPLICIT_FUNCTION(
83 "422489 URLRequestSimpleJob::StartAsync 2"));
85 result = GetData(&mime_type_,
88 base::Bind(&URLRequestSimpleJob::OnGetDataCompleted,
89 weak_factory_.GetWeakPtr()));
92 if (result != ERR_IO_PENDING) {
93 // TODO(vadimt): Remove ScopedTracker below once crbug.com/422489 is fixed.
94 tracked_objects::ScopedTracker tracking_profile(
95 FROM_HERE_WITH_EXPLICIT_FUNCTION(
96 "422489 URLRequestSimpleJob::StartAsync 3"));
98 OnGetDataCompleted(result);
102 void URLRequestSimpleJob::OnGetDataCompleted(int result) {
103 // TODO(vadimt): Remove ScopedTracker below once crbug.com/422489 is fixed.
104 tracked_objects::ScopedTracker tracking_profile(
105 FROM_HERE_WITH_EXPLICIT_FUNCTION(
106 "422489 URLRequestSimpleJob::OnGetDataCompleted"));
109 // Notify that the headers are complete
110 if (!byte_range_.ComputeBounds(data_.size())) {
111 NotifyDone(URLRequestStatus(URLRequestStatus::FAILED,
112 ERR_REQUEST_RANGE_NOT_SATISFIABLE));
116 data_offset_ = byte_range_.first_byte_position();
117 int remaining_bytes = byte_range_.last_byte_position() -
118 byte_range_.first_byte_position() + 1;
119 set_expected_content_size(remaining_bytes);
120 NotifyHeadersComplete();
122 NotifyStartError(URLRequestStatus(URLRequestStatus::FAILED, result));