Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(386)

Side by Side Diff: content/browser/android/url_request_content_job.cc

Issue 1410643007: URLRequestJob: change ReadRawData contract (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Address Comments Created 5 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright (c) 2014 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "content/browser/android/url_request_content_job.h" 5 #include "content/browser/android/url_request_content_job.h"
6 6
7 #include "base/android/content_uri_utils.h" 7 #include "base/android/content_uri_utils.h"
8 #include "base/bind.h" 8 #include "base/bind.h"
9 #include "base/files/file_util.h" 9 #include "base/files/file_util.h"
10 #include "base/message_loop/message_loop.h" 10 #include "base/message_loop/message_loop.h"
11 #include "base/task_runner.h" 11 #include "base/task_runner.h"
12 #include "net/base/file_stream.h" 12 #include "net/base/file_stream.h"
13 #include "net/base/io_buffer.h" 13 #include "net/base/io_buffer.h"
14 #include "net/base/net_errors.h"
15 #include "net/http/http_util.h" 14 #include "net/http/http_util.h"
16 #include "net/url_request/url_request_error_job.h" 15 #include "net/url_request/url_request_error_job.h"
17 #include "url/gurl.h" 16 #include "url/gurl.h"
18 17
19 namespace content { 18 namespace content {
20 19
21 // TODO(qinmin): Refactor this class to reuse the common code in 20 // TODO(qinmin): Refactor this class to reuse the common code in
22 // url_request_file_job.cc. 21 // url_request_file_job.cc.
23 URLRequestContentJob::ContentMetaInfo::ContentMetaInfo() 22 URLRequestContentJob::ContentMetaInfo::ContentMetaInfo()
24 : content_exists(false), 23 : content_exists(false),
25 content_size(0) { 24 content_size(0) {
26 } 25 }
27 26
28 URLRequestContentJob::URLRequestContentJob( 27 URLRequestContentJob::URLRequestContentJob(
29 net::URLRequest* request, 28 net::URLRequest* request,
30 net::NetworkDelegate* network_delegate, 29 net::NetworkDelegate* network_delegate,
31 const base::FilePath& content_path, 30 const base::FilePath& content_path,
32 const scoped_refptr<base::TaskRunner>& content_task_runner) 31 const scoped_refptr<base::TaskRunner>& content_task_runner)
33 : net::URLRequestJob(request, network_delegate), 32 : net::URLRequestJob(request, network_delegate),
34 content_path_(content_path), 33 content_path_(content_path),
35 stream_(new net::FileStream(content_task_runner)), 34 stream_(new net::FileStream(content_task_runner)),
36 content_task_runner_(content_task_runner), 35 content_task_runner_(content_task_runner),
36 range_parse_result_(net::OK),
37 remaining_bytes_(0), 37 remaining_bytes_(0),
38 io_pending_(false), 38 io_pending_(false),
39 weak_ptr_factory_(this) {} 39 weak_ptr_factory_(this) {}
40 40
41 void URLRequestContentJob::Start() { 41 void URLRequestContentJob::Start() {
42 ContentMetaInfo* meta_info = new ContentMetaInfo(); 42 ContentMetaInfo* meta_info = new ContentMetaInfo();
43 content_task_runner_->PostTaskAndReply( 43 content_task_runner_->PostTaskAndReply(
44 FROM_HERE, 44 FROM_HERE,
45 base::Bind(&URLRequestContentJob::FetchMetaInfo, content_path_, 45 base::Bind(&URLRequestContentJob::FetchMetaInfo, content_path_,
46 base::Unretained(meta_info)), 46 base::Unretained(meta_info)),
47 base::Bind(&URLRequestContentJob::DidFetchMetaInfo, 47 base::Bind(&URLRequestContentJob::DidFetchMetaInfo,
48 weak_ptr_factory_.GetWeakPtr(), 48 weak_ptr_factory_.GetWeakPtr(),
49 base::Owned(meta_info))); 49 base::Owned(meta_info)));
50 } 50 }
51 51
52 void URLRequestContentJob::Kill() { 52 void URLRequestContentJob::Kill() {
53 stream_.reset(); 53 stream_.reset();
54 weak_ptr_factory_.InvalidateWeakPtrs(); 54 weak_ptr_factory_.InvalidateWeakPtrs();
55 55
56 net::URLRequestJob::Kill(); 56 net::URLRequestJob::Kill();
57 } 57 }
58 58
59 bool URLRequestContentJob::ReadRawData(net::IOBuffer* dest, 59 int URLRequestContentJob::ReadRawData(net::IOBuffer* dest, int dest_size) {
60 int dest_size,
61 int* bytes_read) {
62 DCHECK_GT(dest_size, 0); 60 DCHECK_GT(dest_size, 0);
63 DCHECK(bytes_read);
64 DCHECK_GE(remaining_bytes_, 0); 61 DCHECK_GE(remaining_bytes_, 0);
65 62
66 if (remaining_bytes_ < dest_size) 63 if (remaining_bytes_ < dest_size)
67 dest_size = static_cast<int>(remaining_bytes_); 64 dest_size = remaining_bytes_;
68 65
69 // If we should copy zero bytes because |remaining_bytes_| is zero, short 66 // If we should copy zero bytes because |remaining_bytes_| is zero, short
70 // circuit here. 67 // circuit here.
71 if (!dest_size) { 68 if (!dest_size)
72 *bytes_read = 0; 69 return 0;
73 return true;
74 }
75 70
76 int rv = stream_->Read(dest, 71 int rv =
77 dest_size, 72 stream_->Read(dest, dest_size, base::Bind(&URLRequestContentJob::DidRead,
78 base::Bind(&URLRequestContentJob::DidRead, 73 weak_ptr_factory_.GetWeakPtr(),
79 weak_ptr_factory_.GetWeakPtr(), 74 make_scoped_refptr(dest)));
80 make_scoped_refptr(dest)));
81 if (rv >= 0) {
82 // Data is immediately available.
83 *bytes_read = rv;
84 remaining_bytes_ -= rv;
85 DCHECK_GE(remaining_bytes_, 0);
86 return true;
87 }
88
89 // Otherwise, a read error occured. We may just need to wait...
90 if (rv == net::ERR_IO_PENDING) { 75 if (rv == net::ERR_IO_PENDING) {
91 io_pending_ = true; 76 io_pending_ = true;
92 SetStatus(net::URLRequestStatus(net::URLRequestStatus::IO_PENDING, 0)); 77 } else if (rv > 0) {
93 } else { 78 remaining_bytes_ -= rv;
94 NotifyDone(net::URLRequestStatus(net::URLRequestStatus::FAILED, rv));
95 } 79 }
96 return false; 80 DCHECK_GE(remaining_bytes_, 0);
81 return rv;
97 } 82 }
98 83
99 bool URLRequestContentJob::IsRedirectResponse(GURL* location, 84 bool URLRequestContentJob::IsRedirectResponse(GURL* location,
100 int* http_status_code) { 85 int* http_status_code) {
101 return false; 86 return false;
102 } 87 }
103 88
104 bool URLRequestContentJob::GetMimeType(std::string* mime_type) const { 89 bool URLRequestContentJob::GetMimeType(std::string* mime_type) const {
105 DCHECK(request_); 90 DCHECK(request_);
106 if (!meta_info_.mime_type.empty()) { 91 if (!meta_info_.mime_type.empty()) {
107 *mime_type = meta_info_.mime_type; 92 *mime_type = meta_info_.mime_type;
108 return true; 93 return true;
109 } 94 }
110 return false; 95 return false;
111 } 96 }
112 97
113 void URLRequestContentJob::SetExtraRequestHeaders( 98 void URLRequestContentJob::SetExtraRequestHeaders(
davidben 2015/11/03 22:50:05 [Not for this CL, but //net folks on this CL: this
mmenke 2015/11/03 22:54:16 I really like the idea of making them an argument
114 const net::HttpRequestHeaders& headers) { 99 const net::HttpRequestHeaders& headers) {
115 std::string range_header; 100 std::string range_header;
116 if (!headers.GetHeader(net::HttpRequestHeaders::kRange, &range_header)) 101 if (!headers.GetHeader(net::HttpRequestHeaders::kRange, &range_header))
117 return; 102 return;
118 103
119 // We only care about "Range" header here. 104 // Currently this job only cares about the Range header. Note that validation
105 // is deferred to DidOpen(), because NotifyStartError is not legal to call
106 // since the job has not started.
120 std::vector<net::HttpByteRange> ranges; 107 std::vector<net::HttpByteRange> ranges;
121 if (net::HttpUtil::ParseRangeHeader(range_header, &ranges)) { 108 if (net::HttpUtil::ParseRangeHeader(range_header, &ranges)) {
122 if (ranges.size() == 1) { 109 if (ranges.size() == 1) {
123 byte_range_ = ranges[0]; 110 byte_range_ = ranges[0];
124 } else { 111 } else {
125 // We don't support multiple range requests. 112 // We don't support multiple range requests.
126 NotifyDone(net::URLRequestStatus( 113 range_parse_result_ = net::ERR_REQUEST_RANGE_NOT_SATISFIABLE;
127 net::URLRequestStatus::FAILED,
128 net::ERR_REQUEST_RANGE_NOT_SATISFIABLE));
129 } 114 }
130 } 115 }
131 } 116 }
132 117
133 URLRequestContentJob::~URLRequestContentJob() {} 118 URLRequestContentJob::~URLRequestContentJob() {}
134 119
135 void URLRequestContentJob::FetchMetaInfo(const base::FilePath& content_path, 120 void URLRequestContentJob::FetchMetaInfo(const base::FilePath& content_path,
136 ContentMetaInfo* meta_info) { 121 ContentMetaInfo* meta_info) {
137 base::File::Info file_info; 122 base::File::Info file_info;
138 meta_info->content_exists = base::GetFileInfo(content_path, &file_info); 123 meta_info->content_exists = base::GetFileInfo(content_path, &file_info);
(...skipping 16 matching lines...) Expand all
155 base::File::FLAG_ASYNC; 140 base::File::FLAG_ASYNC;
156 int rv = stream_->Open(content_path_, flags, 141 int rv = stream_->Open(content_path_, flags,
157 base::Bind(&URLRequestContentJob::DidOpen, 142 base::Bind(&URLRequestContentJob::DidOpen,
158 weak_ptr_factory_.GetWeakPtr())); 143 weak_ptr_factory_.GetWeakPtr()));
159 if (rv != net::ERR_IO_PENDING) 144 if (rv != net::ERR_IO_PENDING)
160 DidOpen(rv); 145 DidOpen(rv);
161 } 146 }
162 147
163 void URLRequestContentJob::DidOpen(int result) { 148 void URLRequestContentJob::DidOpen(int result) {
164 if (result != net::OK) { 149 if (result != net::OK) {
165 NotifyDone(net::URLRequestStatus(net::URLRequestStatus::FAILED, result)); 150 NotifyStartError(
151 net::URLRequestStatus(net::URLRequestStatus::FAILED, result));
152 return;
153 }
154
155 if (range_parse_result_ != net::OK) {
156 NotifyStartError(net::URLRequestStatus(net::URLRequestStatus::FAILED,
157 range_parse_result_));
166 return; 158 return;
167 } 159 }
168 160
169 if (!byte_range_.ComputeBounds(meta_info_.content_size)) { 161 if (!byte_range_.ComputeBounds(meta_info_.content_size)) {
170 NotifyDone(net::URLRequestStatus(net::URLRequestStatus::FAILED, 162 NotifyStartError(net::URLRequestStatus(
171 net::ERR_REQUEST_RANGE_NOT_SATISFIABLE)); 163 net::URLRequestStatus::FAILED, net::ERR_REQUEST_RANGE_NOT_SATISFIABLE));
172 return; 164 return;
173 } 165 }
174 166
175 remaining_bytes_ = byte_range_.last_byte_position() - 167 remaining_bytes_ = byte_range_.last_byte_position() -
176 byte_range_.first_byte_position() + 1; 168 byte_range_.first_byte_position() + 1;
177 DCHECK_GE(remaining_bytes_, 0); 169 DCHECK_GE(remaining_bytes_, 0);
178 170
179 if (remaining_bytes_ > 0 && byte_range_.first_byte_position() != 0) { 171 if (remaining_bytes_ > 0 && byte_range_.first_byte_position() != 0) {
180 int rv = stream_->Seek(byte_range_.first_byte_position(), 172 int rv = stream_->Seek(byte_range_.first_byte_position(),
181 base::Bind(&URLRequestContentJob::DidSeek, 173 base::Bind(&URLRequestContentJob::DidSeek,
182 weak_ptr_factory_.GetWeakPtr())); 174 weak_ptr_factory_.GetWeakPtr()));
183 if (rv != net::ERR_IO_PENDING) { 175 if (rv != net::ERR_IO_PENDING) {
184 // stream_->Seek() failed, so pass an intentionally erroneous value 176 // stream_->Seek() failed, so pass an intentionally erroneous value
185 // into DidSeek(). 177 // into DidSeek().
186 DidSeek(-1); 178 DidSeek(-1);
187 } 179 }
188 } else { 180 } else {
189 // We didn't need to call stream_->Seek() at all, so we pass to DidSeek() 181 // We didn't need to call stream_->Seek() at all, so we pass to DidSeek()
190 // the value that would mean seek success. This way we skip the code 182 // the value that would mean seek success. This way we skip the code
191 // handling seek failure. 183 // handling seek failure.
192 DidSeek(byte_range_.first_byte_position()); 184 DidSeek(byte_range_.first_byte_position());
193 } 185 }
194 } 186 }
195 187
196 void URLRequestContentJob::DidSeek(int64 result) { 188 void URLRequestContentJob::DidSeek(int64 result) {
197 if (result != byte_range_.first_byte_position()) { 189 if (result != byte_range_.first_byte_position()) {
198 NotifyDone(net::URLRequestStatus(net::URLRequestStatus::FAILED, 190 NotifyStartError(net::URLRequestStatus(
199 net::ERR_REQUEST_RANGE_NOT_SATISFIABLE)); 191 net::URLRequestStatus::FAILED, net::ERR_REQUEST_RANGE_NOT_SATISFIABLE));
200 return; 192 return;
201 } 193 }
202 194
203 set_expected_content_size(remaining_bytes_); 195 set_expected_content_size(remaining_bytes_);
204 NotifyHeadersComplete(); 196 NotifyHeadersComplete();
205 } 197 }
206 198
207 void URLRequestContentJob::DidRead( 199 void URLRequestContentJob::DidRead(scoped_refptr<net::IOBuffer> buf,
davidben 2015/11/03 22:50:05 Why does this hold onto buf at all? It doesn't use
xunjieli 2015/11/04 01:26:51 Done.
208 scoped_refptr<net::IOBuffer> buf, int result) { 200 int result) {
201 DCHECK(io_pending_);
202 io_pending_ = false;
203
209 if (result > 0) { 204 if (result > 0) {
210 SetStatus(net::URLRequestStatus()); // Clear the IO_PENDING status
211 remaining_bytes_ -= result; 205 remaining_bytes_ -= result;
212 DCHECK_GE(remaining_bytes_, 0); 206 DCHECK_GE(remaining_bytes_, 0);
213 } 207 }
214 208
215 DCHECK(io_pending_); 209 ReadRawDataComplete(result);
216 io_pending_ = false;
217
218 if (result == 0) {
219 NotifyDone(net::URLRequestStatus());
220 } else if (result < 0) {
221 NotifyDone(net::URLRequestStatus(net::URLRequestStatus::FAILED, result));
222 }
223
224 NotifyReadComplete(result);
225 } 210 }
226 211
227 } // namespace content 212 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698