Explicitly add python-numpy dependency to install-build-deps.
[chromium-blink-merge.git] / net / url_request / url_request_simple_job.cc
blob060a90e6a44518d7c86abefef53e58ecbaa099c1
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"
7 #include <vector>
9 #include "base/bind.h"
10 #include "base/compiler_specific.h"
11 #include "base/memory/ref_counted_memory.h"
12 #include "base/message_loop/message_loop.h"
13 #include "base/profiler/scoped_tracker.h"
14 #include "net/base/io_buffer.h"
15 #include "net/base/net_errors.h"
16 #include "net/http/http_request_headers.h"
17 #include "net/http/http_util.h"
18 #include "net/url_request/url_request_status.h"
20 namespace net {
22 URLRequestSimpleJob::URLRequestSimpleJob(
23 URLRequest* request, NetworkDelegate* network_delegate)
24 : URLRangeRequestJob(request, network_delegate),
25 data_offset_(0),
26 weak_factory_(this) {}
28 void URLRequestSimpleJob::Start() {
29 // Start reading asynchronously so that all error reporting and data
30 // callbacks happen as they would for network requests.
31 base::MessageLoop::current()->PostTask(
32 FROM_HERE,
33 base::Bind(&URLRequestSimpleJob::StartAsync, weak_factory_.GetWeakPtr()));
36 bool URLRequestSimpleJob::GetMimeType(std::string* mime_type) const {
37 *mime_type = mime_type_;
38 return true;
41 bool URLRequestSimpleJob::GetCharset(std::string* charset) {
42 *charset = charset_;
43 return true;
46 URLRequestSimpleJob::~URLRequestSimpleJob() {}
48 bool URLRequestSimpleJob::ReadRawData(IOBuffer* buf, int buf_size,
49 int* bytes_read) {
50 // TODO(vadimt): Remove ScopedTracker below once crbug.com/422489 is fixed.
51 tracked_objects::ScopedTracker tracking_profile(
52 FROM_HERE_WITH_EXPLICIT_FUNCTION(
53 "422489 URLRequestSimpleJob::ReadRawData"));
55 DCHECK(bytes_read);
56 int remaining = byte_range_.last_byte_position() - data_offset_ + 1;
57 if (buf_size > remaining)
58 buf_size = remaining;
59 memcpy(buf->data(), data_->front() + data_offset_, buf_size);
60 data_offset_ += buf_size;
61 *bytes_read = buf_size;
62 return true;
65 int URLRequestSimpleJob::GetData(std::string* mime_type,
66 std::string* charset,
67 std::string* data,
68 const CompletionCallback& callback) const {
69 NOTREACHED();
70 return ERR_UNEXPECTED;
73 int URLRequestSimpleJob::GetRefCountedData(
74 std::string* mime_type,
75 std::string* charset,
76 scoped_refptr<base::RefCountedMemory>* data,
77 const CompletionCallback& callback) const {
78 scoped_refptr<base::RefCountedString> str_data(new base::RefCountedString());
79 int result = GetData(mime_type, charset, &str_data->data(), callback);
80 *data = str_data;
81 return result;
84 void URLRequestSimpleJob::StartAsync() {
85 if (!request_)
86 return;
88 if (ranges().size() > 1) {
89 // TODO(vadimt): Remove ScopedTracker below once crbug.com/422489 is fixed.
90 tracked_objects::ScopedTracker tracking_profile(
91 FROM_HERE_WITH_EXPLICIT_FUNCTION(
92 "422489 URLRequestSimpleJob::StartAsync 1"));
94 NotifyDone(URLRequestStatus(URLRequestStatus::FAILED,
95 ERR_REQUEST_RANGE_NOT_SATISFIABLE));
96 return;
99 if (!ranges().empty() && range_parse_result() == OK)
100 byte_range_ = ranges().front();
102 int result;
104 // TODO(vadimt): Remove ScopedTracker below once crbug.com/422489 is fixed.
105 // Remove the block and assign 'result' in its declaration.
106 tracked_objects::ScopedTracker tracking_profile(
107 FROM_HERE_WITH_EXPLICIT_FUNCTION(
108 "422489 URLRequestSimpleJob::StartAsync 2"));
110 result =
111 GetRefCountedData(&mime_type_, &charset_, &data_,
112 base::Bind(&URLRequestSimpleJob::OnGetDataCompleted,
113 weak_factory_.GetWeakPtr()));
116 if (result != ERR_IO_PENDING) {
117 // TODO(vadimt): Remove ScopedTracker below once crbug.com/422489 is fixed.
118 tracked_objects::ScopedTracker tracking_profile(
119 FROM_HERE_WITH_EXPLICIT_FUNCTION(
120 "422489 URLRequestSimpleJob::StartAsync 3"));
122 OnGetDataCompleted(result);
126 void URLRequestSimpleJob::OnGetDataCompleted(int result) {
127 // TODO(vadimt): Remove ScopedTracker below once crbug.com/422489 is fixed.
128 tracked_objects::ScopedTracker tracking_profile(
129 FROM_HERE_WITH_EXPLICIT_FUNCTION(
130 "422489 URLRequestSimpleJob::OnGetDataCompleted"));
132 if (result == OK) {
133 // Notify that the headers are complete
134 if (!byte_range_.ComputeBounds(data_->size())) {
135 NotifyDone(URLRequestStatus(URLRequestStatus::FAILED,
136 ERR_REQUEST_RANGE_NOT_SATISFIABLE));
137 return;
140 data_offset_ = byte_range_.first_byte_position();
141 int remaining_bytes = byte_range_.last_byte_position() -
142 byte_range_.first_byte_position() + 1;
143 set_expected_content_size(remaining_bytes);
144 NotifyHeadersComplete();
145 } else {
146 NotifyStartError(URLRequestStatus(URLRequestStatus::FAILED, result));
150 } // namespace net