| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 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 "webkit/blob/blob_url_request_job.h" | 5 #include "webkit/blob/blob_url_request_job.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/compiler_specific.h" | 8 #include "base/compiler_specific.h" |
| 9 #include "base/file_util_proxy.h" | 9 #include "base/file_util_proxy.h" |
| 10 #include "base/message_loop.h" | 10 #include "base/message_loop.h" |
| 11 #include "base/message_loop_proxy.h" | 11 #include "base/message_loop_proxy.h" |
| 12 #include "base/stl_util.h" | 12 #include "base/stl_util.h" |
| 13 #include "base/string_number_conversions.h" | 13 #include "base/string_number_conversions.h" |
| 14 #include "net/base/io_buffer.h" | 14 #include "net/base/io_buffer.h" |
| 15 #include "net/base/net_errors.h" | 15 #include "net/base/net_errors.h" |
| 16 #include "net/http/http_request_headers.h" | 16 #include "net/http/http_request_headers.h" |
| 17 #include "net/http/http_response_headers.h" | 17 #include "net/http/http_response_headers.h" |
| 18 #include "net/http/http_response_info.h" | 18 #include "net/http/http_response_info.h" |
| 19 #include "net/http/http_util.h" | 19 #include "net/http/http_util.h" |
| 20 #include "net/url_request/url_request.h" | 20 #include "net/url_request/url_request.h" |
| 21 #include "net/url_request/url_request_error_job.h" | 21 #include "net/url_request/url_request_error_job.h" |
| 22 #include "net/url_request/url_request_status.h" | 22 #include "net/url_request/url_request_status.h" |
| 23 #include "webkit/blob/local_file_reader.h" | 23 #include "webkit/blob/local_file_stream_reader.h" |
| 24 | 24 |
| 25 namespace webkit_blob { | 25 namespace webkit_blob { |
| 26 | 26 |
| 27 namespace { | 27 namespace { |
| 28 | 28 |
| 29 const int kHTTPOk = 200; | 29 const int kHTTPOk = 200; |
| 30 const int kHTTPPartialContent = 206; | 30 const int kHTTPPartialContent = 206; |
| 31 const int kHTTPNotAllowed = 403; | 31 const int kHTTPNotAllowed = 403; |
| 32 const int kHTTPNotFound = 404; | 32 const int kHTTPNotFound = 404; |
| 33 const int kHTTPMethodNotAllow = 405; | 33 const int kHTTPMethodNotAllow = 405; |
| (...skipping 136 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 170 void BlobURLRequestJob::CountSize() { | 170 void BlobURLRequestJob::CountSize() { |
| 171 error_ = false; | 171 error_ = false; |
| 172 pending_get_file_info_count_ = 0; | 172 pending_get_file_info_count_ = 0; |
| 173 total_size_ = 0; | 173 total_size_ = 0; |
| 174 item_length_list_.resize(blob_data_->items().size()); | 174 item_length_list_.resize(blob_data_->items().size()); |
| 175 | 175 |
| 176 for (size_t i = 0; i < blob_data_->items().size(); ++i) { | 176 for (size_t i = 0; i < blob_data_->items().size(); ++i) { |
| 177 const BlobData::Item& item = blob_data_->items().at(i); | 177 const BlobData::Item& item = blob_data_->items().at(i); |
| 178 if (item.type == BlobData::TYPE_FILE) { | 178 if (item.type == BlobData::TYPE_FILE) { |
| 179 ++pending_get_file_info_count_; | 179 ++pending_get_file_info_count_; |
| 180 GetFileReader(i)->GetLength( | 180 GetFileStreamReader(i)->GetLength( |
| 181 base::Bind(&BlobURLRequestJob::DidGetFileItemLength, | 181 base::Bind(&BlobURLRequestJob::DidGetFileItemLength, |
| 182 weak_factory_.GetWeakPtr(), i)); | 182 weak_factory_.GetWeakPtr(), i)); |
| 183 continue; | 183 continue; |
| 184 } | 184 } |
| 185 // Cache the size and add it to the total size. | 185 // Cache the size and add it to the total size. |
| 186 int64 item_length = static_cast<int64>(item.length); | 186 int64 item_length = static_cast<int64>(item.length); |
| 187 item_length_list_[i] = item_length; | 187 item_length_list_[i] = item_length; |
| 188 total_size_ += item_length; | 188 total_size_ += item_length; |
| 189 } | 189 } |
| 190 | 190 |
| (...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 262 // Set the offset that need to jump to for the first item in the range. | 262 // Set the offset that need to jump to for the first item in the range. |
| 263 current_item_offset_ = offset; | 263 current_item_offset_ = offset; |
| 264 | 264 |
| 265 if (offset == 0) | 265 if (offset == 0) |
| 266 return; | 266 return; |
| 267 | 267 |
| 268 // Adjust the offset of the first stream if it is of file type. | 268 // Adjust the offset of the first stream if it is of file type. |
| 269 const BlobData::Item& item = blob_data_->items().at(current_item_index_); | 269 const BlobData::Item& item = blob_data_->items().at(current_item_index_); |
| 270 if (item.type == BlobData::TYPE_FILE) { | 270 if (item.type == BlobData::TYPE_FILE) { |
| 271 DeleteCurrentFileReader(); | 271 DeleteCurrentFileReader(); |
| 272 index_to_reader_[current_item_index_] = new LocalFileReader( | 272 index_to_reader_[current_item_index_] = new LocalFileStreamReader( |
| 273 file_thread_proxy_, | 273 file_thread_proxy_, |
| 274 item.file_path, | 274 item.file_path, |
| 275 item.offset + offset, | 275 item.offset + offset, |
| 276 item.expected_modification_time); | 276 item.expected_modification_time); |
| 277 } | 277 } |
| 278 } | 278 } |
| 279 | 279 |
| 280 bool BlobURLRequestJob::ReadItem() { | 280 bool BlobURLRequestJob::ReadItem() { |
| 281 // Are we done with reading all the blob data? | 281 // Are we done with reading all the blob data? |
| 282 if (remaining_bytes_ == 0) | 282 if (remaining_bytes_ == 0) |
| (...skipping 14 matching lines...) Expand all Loading... |
| 297 AdvanceItem(); | 297 AdvanceItem(); |
| 298 return ReadItem(); | 298 return ReadItem(); |
| 299 } | 299 } |
| 300 | 300 |
| 301 // Do the reading. | 301 // Do the reading. |
| 302 const BlobData::Item& item = blob_data_->items().at(current_item_index_); | 302 const BlobData::Item& item = blob_data_->items().at(current_item_index_); |
| 303 switch (item.type) { | 303 switch (item.type) { |
| 304 case BlobData::TYPE_DATA: | 304 case BlobData::TYPE_DATA: |
| 305 return ReadBytesItem(item, bytes_to_read); | 305 return ReadBytesItem(item, bytes_to_read); |
| 306 case BlobData::TYPE_FILE: | 306 case BlobData::TYPE_FILE: |
| 307 return ReadFileItem(GetFileReader(current_item_index_), bytes_to_read); | 307 return ReadFileItem(GetFileStreamReader(current_item_index_), |
| 308 bytes_to_read); |
| 308 default: | 309 default: |
| 309 DCHECK(false); | 310 DCHECK(false); |
| 310 return false; | 311 return false; |
| 311 } | 312 } |
| 312 } | 313 } |
| 313 | 314 |
| 314 void BlobURLRequestJob::AdvanceItem() { | 315 void BlobURLRequestJob::AdvanceItem() { |
| 315 // Close the file if the current item is a file. | 316 // Close the file if the current item is a file. |
| 316 DeleteCurrentFileReader(); | 317 DeleteCurrentFileReader(); |
| 317 | 318 |
| (...skipping 24 matching lines...) Expand all Loading... |
| 342 DCHECK_GE(read_buf_->BytesRemaining(), bytes_to_read); | 343 DCHECK_GE(read_buf_->BytesRemaining(), bytes_to_read); |
| 343 | 344 |
| 344 memcpy(read_buf_->data(), | 345 memcpy(read_buf_->data(), |
| 345 &item.data.at(0) + item.offset + current_item_offset_, | 346 &item.data.at(0) + item.offset + current_item_offset_, |
| 346 bytes_to_read); | 347 bytes_to_read); |
| 347 | 348 |
| 348 AdvanceBytesRead(bytes_to_read); | 349 AdvanceBytesRead(bytes_to_read); |
| 349 return true; | 350 return true; |
| 350 } | 351 } |
| 351 | 352 |
| 352 bool BlobURLRequestJob::ReadFileItem(LocalFileReader* reader, | 353 bool BlobURLRequestJob::ReadFileItem(LocalFileStreamReader* reader, |
| 353 int bytes_to_read) { | 354 int bytes_to_read) { |
| 354 DCHECK_GE(read_buf_->BytesRemaining(), bytes_to_read); | 355 DCHECK_GE(read_buf_->BytesRemaining(), bytes_to_read); |
| 355 DCHECK(reader); | 356 DCHECK(reader); |
| 356 const int result = reader->Read( | 357 const int result = reader->Read( |
| 357 read_buf_, bytes_to_read, | 358 read_buf_, bytes_to_read, |
| 358 base::Bind(&BlobURLRequestJob::DidReadFile, | 359 base::Bind(&BlobURLRequestJob::DidReadFile, |
| 359 base::Unretained(this))); | 360 base::Unretained(this))); |
| 360 if (result >= 0) { | 361 if (result >= 0) { |
| 361 // Data is immediately available. | 362 // Data is immediately available. |
| 362 if (GetStatus().is_io_pending()) | 363 if (GetStatus().is_io_pending()) |
| (...skipping 150 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 513 | 514 |
| 514 response_info_.reset(new net::HttpResponseInfo()); | 515 response_info_.reset(new net::HttpResponseInfo()); |
| 515 response_info_->headers = headers; | 516 response_info_->headers = headers; |
| 516 | 517 |
| 517 set_expected_content_size(remaining_bytes_); | 518 set_expected_content_size(remaining_bytes_); |
| 518 headers_set_ = true; | 519 headers_set_ = true; |
| 519 | 520 |
| 520 NotifyHeadersComplete(); | 521 NotifyHeadersComplete(); |
| 521 } | 522 } |
| 522 | 523 |
| 523 LocalFileReader* BlobURLRequestJob::GetFileReader(size_t index) { | 524 LocalFileStreamReader* BlobURLRequestJob::GetFileStreamReader(size_t index) { |
| 524 DCHECK_LT(index, blob_data_->items().size()); | 525 DCHECK_LT(index, blob_data_->items().size()); |
| 525 const BlobData::Item& item = blob_data_->items().at(index); | 526 const BlobData::Item& item = blob_data_->items().at(index); |
| 526 if (item.type != BlobData::TYPE_FILE) | 527 if (item.type != BlobData::TYPE_FILE) |
| 527 return NULL; | 528 return NULL; |
| 528 if (index_to_reader_.find(index) == index_to_reader_.end()) { | 529 if (index_to_reader_.find(index) == index_to_reader_.end()) { |
| 529 index_to_reader_[index] = new LocalFileReader( | 530 index_to_reader_[index] = new LocalFileStreamReader( |
| 530 file_thread_proxy_, | 531 file_thread_proxy_, |
| 531 item.file_path, | 532 item.file_path, |
| 532 item.offset, | 533 item.offset, |
| 533 item.expected_modification_time); | 534 item.expected_modification_time); |
| 534 } | 535 } |
| 535 DCHECK(index_to_reader_[index]); | 536 DCHECK(index_to_reader_[index]); |
| 536 return index_to_reader_[index]; | 537 return index_to_reader_[index]; |
| 537 } | 538 } |
| 538 | 539 |
| 539 } // namespace webkit_blob | 540 } // namespace webkit_blob |
| OLD | NEW |