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

Side by Side Diff: webkit/blob/blob_url_request_job.cc

Issue 15746017: Move webkit/blob to new locations. (Closed) Base URL: svn://chrome-svn/chrome/trunk/src/
Patch Set: Created 7 years, 6 months 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 | Annotate | Revision Log
« no previous file with comments | « webkit/blob/blob_url_request_job.h ('k') | webkit/blob/blob_url_request_job_factory.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
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.
4
5 #include "webkit/blob/blob_url_request_job.h"
6
7 #include <limits>
8
9 #include "base/basictypes.h"
10 #include "base/bind.h"
11 #include "base/compiler_specific.h"
12 #include "base/files/file_util_proxy.h"
13 #include "base/message_loop.h"
14 #include "base/message_loop_proxy.h"
15 #include "base/stl_util.h"
16 #include "base/strings/string_number_conversions.h"
17 #include "net/base/io_buffer.h"
18 #include "net/base/net_errors.h"
19 #include "net/http/http_request_headers.h"
20 #include "net/http/http_response_headers.h"
21 #include "net/http/http_response_info.h"
22 #include "net/http/http_util.h"
23 #include "net/url_request/url_request.h"
24 #include "net/url_request/url_request_context.h"
25 #include "net/url_request/url_request_error_job.h"
26 #include "net/url_request/url_request_status.h"
27 #include "webkit/blob/local_file_stream_reader.h"
28 #include "webkit/browser/fileapi/file_system_context.h"
29 #include "webkit/browser/fileapi/file_system_url.h"
30
31 namespace webkit_blob {
32
33 namespace {
34
35 const int kHTTPOk = 200;
36 const int kHTTPPartialContent = 206;
37 const int kHTTPNotAllowed = 403;
38 const int kHTTPNotFound = 404;
39 const int kHTTPMethodNotAllow = 405;
40 const int kHTTPRequestedRangeNotSatisfiable = 416;
41 const int kHTTPInternalError = 500;
42
43 const char kHTTPOKText[] = "OK";
44 const char kHTTPPartialContentText[] = "Partial Content";
45 const char kHTTPNotAllowedText[] = "Not Allowed";
46 const char kHTTPNotFoundText[] = "Not Found";
47 const char kHTTPMethodNotAllowText[] = "Method Not Allowed";
48 const char kHTTPRequestedRangeNotSatisfiableText[] =
49 "Requested Range Not Satisfiable";
50 const char kHTTPInternalErrorText[] = "Internal Server Error";
51
52 bool IsFileType(BlobData::Item::Type type) {
53 switch (type) {
54 case BlobData::Item::TYPE_FILE:
55 case BlobData::Item::TYPE_FILE_FILESYSTEM:
56 return true;
57 default:
58 return false;
59 }
60 }
61
62 } // namespace
63
64 BlobURLRequestJob::BlobURLRequestJob(
65 net::URLRequest* request,
66 net::NetworkDelegate* network_delegate,
67 BlobData* blob_data,
68 fileapi::FileSystemContext* file_system_context,
69 base::MessageLoopProxy* file_thread_proxy)
70 : net::URLRequestJob(request, network_delegate),
71 weak_factory_(this),
72 blob_data_(blob_data),
73 file_system_context_(file_system_context),
74 file_thread_proxy_(file_thread_proxy),
75 total_size_(0),
76 remaining_bytes_(0),
77 pending_get_file_info_count_(0),
78 current_item_index_(0),
79 current_item_offset_(0),
80 error_(false),
81 headers_set_(false),
82 byte_range_set_(false) {
83 DCHECK(file_thread_proxy_);
84 }
85
86 void BlobURLRequestJob::Start() {
87 // Continue asynchronously.
88 base::MessageLoop::current()->PostTask(
89 FROM_HERE,
90 base::Bind(&BlobURLRequestJob::DidStart, weak_factory_.GetWeakPtr()));
91 }
92
93 void BlobURLRequestJob::Kill() {
94 DeleteCurrentFileReader();
95
96 net::URLRequestJob::Kill();
97 weak_factory_.InvalidateWeakPtrs();
98 }
99
100 bool BlobURLRequestJob::ReadRawData(net::IOBuffer* dest,
101 int dest_size,
102 int* bytes_read) {
103 DCHECK_NE(dest_size, 0);
104 DCHECK(bytes_read);
105 DCHECK_GE(remaining_bytes_, 0);
106
107 // Bail out immediately if we encounter an error.
108 if (error_) {
109 *bytes_read = 0;
110 return true;
111 }
112
113 if (remaining_bytes_ < dest_size)
114 dest_size = static_cast<int>(remaining_bytes_);
115
116 // If we should copy zero bytes because |remaining_bytes_| is zero, short
117 // circuit here.
118 if (!dest_size) {
119 *bytes_read = 0;
120 return true;
121 }
122
123 // Keep track of the buffer.
124 DCHECK(!read_buf_);
125 read_buf_ = new net::DrainableIOBuffer(dest, dest_size);
126
127 return ReadLoop(bytes_read);
128 }
129
130 bool BlobURLRequestJob::GetMimeType(std::string* mime_type) const {
131 if (!response_info_)
132 return false;
133
134 return response_info_->headers->GetMimeType(mime_type);
135 }
136
137 void BlobURLRequestJob::GetResponseInfo(net::HttpResponseInfo* info) {
138 if (response_info_)
139 *info = *response_info_;
140 }
141
142 int BlobURLRequestJob::GetResponseCode() const {
143 if (!response_info_)
144 return -1;
145
146 return response_info_->headers->response_code();
147 }
148
149 void BlobURLRequestJob::SetExtraRequestHeaders(
150 const net::HttpRequestHeaders& headers) {
151 std::string range_header;
152 if (headers.GetHeader(net::HttpRequestHeaders::kRange, &range_header)) {
153 // We only care about "Range" header here.
154 std::vector<net::HttpByteRange> ranges;
155 if (net::HttpUtil::ParseRangeHeader(range_header, &ranges)) {
156 if (ranges.size() == 1) {
157 byte_range_set_ = true;
158 byte_range_ = ranges[0];
159 } else {
160 // We don't support multiple range requests in one single URL request,
161 // because we need to do multipart encoding here.
162 // TODO(jianli): Support multipart byte range requests.
163 NotifyFailure(net::ERR_REQUEST_RANGE_NOT_SATISFIABLE);
164 }
165 }
166 }
167 }
168
169 BlobURLRequestJob::~BlobURLRequestJob() {
170 STLDeleteValues(&index_to_reader_);
171 }
172
173 void BlobURLRequestJob::DidStart() {
174 // We only support GET request per the spec.
175 if (request()->method() != "GET") {
176 NotifyFailure(net::ERR_METHOD_NOT_SUPPORTED);
177 return;
178 }
179
180 // If the blob data is not present, bail out.
181 if (!blob_data_) {
182 NotifyFailure(net::ERR_FILE_NOT_FOUND);
183 return;
184 }
185
186 CountSize();
187 }
188
189 bool BlobURLRequestJob::AddItemLength(size_t index, int64 item_length) {
190 if (item_length > kint64max - total_size_) {
191 NotifyFailure(net::ERR_FAILED);
192 return false;
193 }
194
195 // Cache the size and add it to the total size.
196 DCHECK_LT(index, item_length_list_.size());
197 item_length_list_[index] = item_length;
198 total_size_ += item_length;
199 return true;
200 }
201
202 void BlobURLRequestJob::CountSize() {
203 error_ = false;
204 pending_get_file_info_count_ = 0;
205 total_size_ = 0;
206 item_length_list_.resize(blob_data_->items().size());
207
208 for (size_t i = 0; i < blob_data_->items().size(); ++i) {
209 const BlobData::Item& item = blob_data_->items().at(i);
210 if (IsFileType(item.type())) {
211 ++pending_get_file_info_count_;
212 GetFileStreamReader(i)->GetLength(
213 base::Bind(&BlobURLRequestJob::DidGetFileItemLength,
214 weak_factory_.GetWeakPtr(), i));
215 continue;
216 }
217
218 if (!AddItemLength(i, item.length()))
219 return;
220 }
221
222 if (pending_get_file_info_count_ == 0)
223 DidCountSize(net::OK);
224 }
225
226 void BlobURLRequestJob::DidCountSize(int error) {
227 DCHECK(!error_);
228
229 // If an error occured, bail out.
230 if (error != net::OK) {
231 NotifyFailure(error);
232 return;
233 }
234
235 // Apply the range requirement.
236 if (!byte_range_.ComputeBounds(total_size_)) {
237 NotifyFailure(net::ERR_REQUEST_RANGE_NOT_SATISFIABLE);
238 return;
239 }
240
241 remaining_bytes_ = byte_range_.last_byte_position() -
242 byte_range_.first_byte_position() + 1;
243 DCHECK_GE(remaining_bytes_, 0);
244
245 // Do the seek at the beginning of the request.
246 if (byte_range_.first_byte_position())
247 Seek(byte_range_.first_byte_position());
248
249 NotifySuccess();
250 }
251
252 void BlobURLRequestJob::DidGetFileItemLength(size_t index, int64 result) {
253 // Do nothing if we have encountered an error.
254 if (error_)
255 return;
256
257 if (result == net::ERR_UPLOAD_FILE_CHANGED) {
258 NotifyFailure(net::ERR_FILE_NOT_FOUND);
259 return;
260 } else if (result < 0) {
261 NotifyFailure(result);
262 return;
263 }
264
265 DCHECK_LT(index, blob_data_->items().size());
266 const BlobData::Item& item = blob_data_->items().at(index);
267 DCHECK(IsFileType(item.type()));
268
269 uint64 file_length = result;
270 uint64 item_offset = item.offset();
271 uint64 item_length = item.length();
272
273 if (item_offset > file_length) {
274 NotifyFailure(net::ERR_FILE_NOT_FOUND);
275 return;
276 }
277
278 uint64 max_length = file_length - item_offset;
279
280 // If item length is -1, we need to use the file size being resolved
281 // in the real time.
282 if (item_length == static_cast<uint64>(-1)) {
283 item_length = max_length;
284 } else if (item_length > max_length) {
285 NotifyFailure(net::ERR_FILE_NOT_FOUND);
286 return;
287 }
288
289 if (!AddItemLength(index, item_length))
290 return;
291
292 if (--pending_get_file_info_count_ == 0)
293 DidCountSize(net::OK);
294 }
295
296 void BlobURLRequestJob::Seek(int64 offset) {
297 // Skip the initial items that are not in the range.
298 for (current_item_index_ = 0;
299 current_item_index_ < blob_data_->items().size() &&
300 offset >= item_length_list_[current_item_index_];
301 ++current_item_index_) {
302 offset -= item_length_list_[current_item_index_];
303 }
304
305 // Set the offset that need to jump to for the first item in the range.
306 current_item_offset_ = offset;
307
308 if (offset == 0)
309 return;
310
311 // Adjust the offset of the first stream if it is of file type.
312 const BlobData::Item& item = blob_data_->items().at(current_item_index_);
313 if (IsFileType(item.type())) {
314 DeleteCurrentFileReader();
315 CreateFileStreamReader(current_item_index_, offset);
316 }
317 }
318
319 bool BlobURLRequestJob::ReadItem() {
320 // Are we done with reading all the blob data?
321 if (remaining_bytes_ == 0)
322 return true;
323
324 // If we get to the last item but still expect something to read, bail out
325 // since something is wrong.
326 if (current_item_index_ >= blob_data_->items().size()) {
327 NotifyFailure(net::ERR_FAILED);
328 return false;
329 }
330
331 // Compute the bytes to read for current item.
332 int bytes_to_read = ComputeBytesToRead();
333
334 // If nothing to read for current item, advance to next item.
335 if (bytes_to_read == 0) {
336 AdvanceItem();
337 return ReadItem();
338 }
339
340 // Do the reading.
341 const BlobData::Item& item = blob_data_->items().at(current_item_index_);
342 if (item.type() == BlobData::Item::TYPE_BYTES)
343 return ReadBytesItem(item, bytes_to_read);
344 if (IsFileType(item.type())) {
345 return ReadFileItem(GetFileStreamReader(current_item_index_),
346 bytes_to_read);
347 }
348 NOTREACHED();
349 return false;
350 }
351
352 void BlobURLRequestJob::AdvanceItem() {
353 // Close the file if the current item is a file.
354 DeleteCurrentFileReader();
355
356 // Advance to the next item.
357 current_item_index_++;
358 current_item_offset_ = 0;
359 }
360
361 void BlobURLRequestJob::AdvanceBytesRead(int result) {
362 DCHECK_GT(result, 0);
363
364 // Do we finish reading the current item?
365 current_item_offset_ += result;
366 if (current_item_offset_ == item_length_list_[current_item_index_])
367 AdvanceItem();
368
369 // Subtract the remaining bytes.
370 remaining_bytes_ -= result;
371 DCHECK_GE(remaining_bytes_, 0);
372
373 // Adjust the read buffer.
374 read_buf_->DidConsume(result);
375 DCHECK_GE(read_buf_->BytesRemaining(), 0);
376 }
377
378 bool BlobURLRequestJob::ReadBytesItem(const BlobData::Item& item,
379 int bytes_to_read) {
380 DCHECK_GE(read_buf_->BytesRemaining(), bytes_to_read);
381
382 memcpy(read_buf_->data(),
383 item.bytes() + item.offset() + current_item_offset_,
384 bytes_to_read);
385
386 AdvanceBytesRead(bytes_to_read);
387 return true;
388 }
389
390 bool BlobURLRequestJob::ReadFileItem(FileStreamReader* reader,
391 int bytes_to_read) {
392 DCHECK_GE(read_buf_->BytesRemaining(), bytes_to_read);
393 DCHECK(reader);
394 const int result = reader->Read(
395 read_buf_, bytes_to_read,
396 base::Bind(&BlobURLRequestJob::DidReadFile,
397 base::Unretained(this)));
398 if (result >= 0) {
399 // Data is immediately available.
400 if (GetStatus().is_io_pending())
401 DidReadFile(result);
402 else
403 AdvanceBytesRead(result);
404 return true;
405 }
406 if (result == net::ERR_IO_PENDING)
407 SetStatus(net::URLRequestStatus(net::URLRequestStatus::IO_PENDING, 0));
408 else
409 NotifyFailure(result);
410 return false;
411 }
412
413 void BlobURLRequestJob::DidReadFile(int result) {
414 if (result <= 0) {
415 NotifyFailure(net::ERR_FAILED);
416 return;
417 }
418 SetStatus(net::URLRequestStatus()); // Clear the IO_PENDING status
419
420 AdvanceBytesRead(result);
421
422 // If the read buffer is completely filled, we're done.
423 if (!read_buf_->BytesRemaining()) {
424 int bytes_read = BytesReadCompleted();
425 NotifyReadComplete(bytes_read);
426 return;
427 }
428
429 // Otherwise, continue the reading.
430 int bytes_read = 0;
431 if (ReadLoop(&bytes_read))
432 NotifyReadComplete(bytes_read);
433 }
434
435 void BlobURLRequestJob::DeleteCurrentFileReader() {
436 IndexToReaderMap::iterator found = index_to_reader_.find(current_item_index_);
437 if (found != index_to_reader_.end() && found->second) {
438 delete found->second;
439 index_to_reader_.erase(found);
440 }
441 }
442
443 int BlobURLRequestJob::BytesReadCompleted() {
444 int bytes_read = read_buf_->BytesConsumed();
445 read_buf_ = NULL;
446 return bytes_read;
447 }
448
449 int BlobURLRequestJob::ComputeBytesToRead() const {
450 int64 current_item_length = item_length_list_[current_item_index_];
451
452 int64 item_remaining = current_item_length - current_item_offset_;
453 int64 buf_remaining = read_buf_->BytesRemaining();
454 int64 max_remaining = std::numeric_limits<int>::max();
455
456 int64 min = std::min(std::min(std::min(item_remaining,
457 buf_remaining),
458 remaining_bytes_),
459 max_remaining);
460
461 return static_cast<int>(min);
462 }
463
464 bool BlobURLRequestJob::ReadLoop(int* bytes_read) {
465 // Read until we encounter an error or could not get the data immediately.
466 while (remaining_bytes_ > 0 && read_buf_->BytesRemaining() > 0) {
467 if (!ReadItem())
468 return false;
469 }
470
471 *bytes_read = BytesReadCompleted();
472 return true;
473 }
474
475 void BlobURLRequestJob::NotifySuccess() {
476 int status_code = 0;
477 std::string status_text;
478 if (byte_range_set_ && byte_range_.IsValid()) {
479 status_code = kHTTPPartialContent;
480 status_text += kHTTPPartialContentText;
481 } else {
482 status_code = kHTTPOk;
483 status_text = kHTTPOKText;
484 }
485 HeadersCompleted(status_code, status_text);
486 }
487
488 void BlobURLRequestJob::NotifyFailure(int error_code) {
489 error_ = true;
490
491 // If we already return the headers on success, we can't change the headers
492 // now. Instead, we just error out.
493 if (headers_set_) {
494 NotifyDone(net::URLRequestStatus(net::URLRequestStatus::FAILED,
495 error_code));
496 return;
497 }
498
499 int status_code = 0;
500 std::string status_txt;
501 switch (error_code) {
502 case net::ERR_ACCESS_DENIED:
503 status_code = kHTTPNotAllowed;
504 status_txt = kHTTPNotAllowedText;
505 break;
506 case net::ERR_FILE_NOT_FOUND:
507 status_code = kHTTPNotFound;
508 status_txt = kHTTPNotFoundText;
509 break;
510 case net::ERR_METHOD_NOT_SUPPORTED:
511 status_code = kHTTPMethodNotAllow;
512 status_txt = kHTTPMethodNotAllowText;
513 break;
514 case net::ERR_REQUEST_RANGE_NOT_SATISFIABLE:
515 status_code = kHTTPRequestedRangeNotSatisfiable;
516 status_txt = kHTTPRequestedRangeNotSatisfiableText;
517 break;
518 case net::ERR_FAILED:
519 status_code = kHTTPInternalError;
520 status_txt = kHTTPInternalErrorText;
521 break;
522 default:
523 DCHECK(false);
524 status_code = kHTTPInternalError;
525 status_txt = kHTTPInternalErrorText;
526 break;
527 }
528 HeadersCompleted(status_code, status_txt);
529 }
530
531 void BlobURLRequestJob::HeadersCompleted(int status_code,
532 const std::string& status_text) {
533 std::string status("HTTP/1.1 ");
534 status.append(base::IntToString(status_code));
535 status.append(" ");
536 status.append(status_text);
537 status.append("\0\0", 2);
538 net::HttpResponseHeaders* headers = new net::HttpResponseHeaders(status);
539
540 if (status_code == kHTTPOk || status_code == kHTTPPartialContent) {
541 std::string content_length_header(net::HttpRequestHeaders::kContentLength);
542 content_length_header.append(": ");
543 content_length_header.append(base::Int64ToString(remaining_bytes_));
544 headers->AddHeader(content_length_header);
545 if (!blob_data_->content_type().empty()) {
546 std::string content_type_header(net::HttpRequestHeaders::kContentType);
547 content_type_header.append(": ");
548 content_type_header.append(blob_data_->content_type());
549 headers->AddHeader(content_type_header);
550 }
551 if (!blob_data_->content_disposition().empty()) {
552 std::string content_disposition_header("Content-Disposition: ");
553 content_disposition_header.append(blob_data_->content_disposition());
554 headers->AddHeader(content_disposition_header);
555 }
556 }
557
558 response_info_.reset(new net::HttpResponseInfo());
559 response_info_->headers = headers;
560
561 set_expected_content_size(remaining_bytes_);
562 headers_set_ = true;
563
564 NotifyHeadersComplete();
565 }
566
567 FileStreamReader* BlobURLRequestJob::GetFileStreamReader(size_t index) {
568 DCHECK_LT(index, blob_data_->items().size());
569 const BlobData::Item& item = blob_data_->items().at(index);
570 if (!IsFileType(item.type()))
571 return NULL;
572 if (index_to_reader_.find(index) == index_to_reader_.end())
573 CreateFileStreamReader(index, 0);
574 DCHECK(index_to_reader_[index]);
575 return index_to_reader_[index];
576 }
577
578 void BlobURLRequestJob::CreateFileStreamReader(size_t index,
579 int64 additional_offset) {
580 DCHECK_LT(index, blob_data_->items().size());
581 const BlobData::Item& item = blob_data_->items().at(index);
582 DCHECK(IsFileType(item.type()));
583 DCHECK_EQ(0U, index_to_reader_.count(index));
584
585 FileStreamReader* reader = NULL;
586 switch (item.type()) {
587 case BlobData::Item::TYPE_FILE:
588 reader = new LocalFileStreamReader(
589 file_thread_proxy_,
590 item.path(),
591 item.offset() + additional_offset,
592 item.expected_modification_time());
593 break;
594 case BlobData::Item::TYPE_FILE_FILESYSTEM:
595 reader = file_system_context_->CreateFileStreamReader(
596 fileapi::FileSystemURL(file_system_context_->CrackURL(item.url())),
597 item.offset() + additional_offset,
598 item.expected_modification_time()).release();
599 break;
600 default:
601 NOTREACHED();
602 }
603 DCHECK(reader);
604 index_to_reader_[index] = reader;
605 }
606
607 } // namespace webkit_blob
OLDNEW
« no previous file with comments | « webkit/blob/blob_url_request_job.h ('k') | webkit/blob/blob_url_request_job_factory.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698