| 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/browser/fileapi/file_system_url_request_job.h" | 5 #include "storage/browser/fileapi/file_system_url_request_job.h" |
| 6 | 6 |
| 7 #include <vector> | 7 #include <vector> |
| 8 | 8 |
| 9 #include "base/bind.h" | 9 #include "base/bind.h" |
| 10 #include "base/compiler_specific.h" | 10 #include "base/compiler_specific.h" |
| 11 #include "base/files/file_path.h" | 11 #include "base/files/file_path.h" |
| 12 #include "base/files/file_util_proxy.h" | 12 #include "base/files/file_util_proxy.h" |
| 13 #include "base/message_loop/message_loop.h" | 13 #include "base/message_loop/message_loop.h" |
| 14 #include "base/message_loop/message_loop_proxy.h" | 14 #include "base/message_loop/message_loop_proxy.h" |
| 15 #include "base/threading/thread_restrictions.h" | 15 #include "base/threading/thread_restrictions.h" |
| 16 #include "base/time/time.h" | 16 #include "base/time/time.h" |
| 17 #include "build/build_config.h" | 17 #include "build/build_config.h" |
| 18 #include "net/base/file_stream.h" | 18 #include "net/base/file_stream.h" |
| 19 #include "net/base/io_buffer.h" | 19 #include "net/base/io_buffer.h" |
| 20 #include "net/base/mime_util.h" | 20 #include "net/base/mime_util.h" |
| 21 #include "net/base/net_errors.h" | 21 #include "net/base/net_errors.h" |
| 22 #include "net/base/net_util.h" | 22 #include "net/base/net_util.h" |
| 23 #include "net/http/http_response_headers.h" | 23 #include "net/http/http_response_headers.h" |
| 24 #include "net/http/http_response_info.h" | 24 #include "net/http/http_response_info.h" |
| 25 #include "net/http/http_util.h" | 25 #include "net/http/http_util.h" |
| 26 #include "net/url_request/url_request.h" | 26 #include "net/url_request/url_request.h" |
| 27 #include "url/gurl.h" | 27 #include "url/gurl.h" |
| 28 #include "webkit/browser/blob/file_stream_reader.h" | 28 #include "storage/browser/blob/file_stream_reader.h" |
| 29 #include "webkit/browser/fileapi/file_system_context.h" | 29 #include "storage/browser/fileapi/file_system_context.h" |
| 30 #include "webkit/browser/fileapi/file_system_operation_runner.h" | 30 #include "storage/browser/fileapi/file_system_operation_runner.h" |
| 31 #include "webkit/common/fileapi/file_system_util.h" | 31 #include "storage/common/fileapi/file_system_util.h" |
| 32 | 32 |
| 33 using net::NetworkDelegate; | 33 using net::NetworkDelegate; |
| 34 using net::URLRequest; | 34 using net::URLRequest; |
| 35 using net::URLRequestJob; | 35 using net::URLRequestJob; |
| 36 using net::URLRequestStatus; | 36 using net::URLRequestStatus; |
| 37 | 37 |
| 38 namespace fileapi { | 38 namespace storage { |
| 39 | 39 |
| 40 static net::HttpResponseHeaders* CreateHttpResponseHeaders() { | 40 static net::HttpResponseHeaders* CreateHttpResponseHeaders() { |
| 41 // HttpResponseHeaders expects its input string to be terminated by two NULs. | 41 // HttpResponseHeaders expects its input string to be terminated by two NULs. |
| 42 static const char kStatus[] = "HTTP/1.1 200 OK\0"; | 42 static const char kStatus[] = "HTTP/1.1 200 OK\0"; |
| 43 static const size_t kStatusLen = arraysize(kStatus); | 43 static const size_t kStatusLen = arraysize(kStatus); |
| 44 | 44 |
| 45 net::HttpResponseHeaders* headers = | 45 net::HttpResponseHeaders* headers = |
| 46 new net::HttpResponseHeaders(std::string(kStatus, kStatusLen)); | 46 new net::HttpResponseHeaders(std::string(kStatus, kStatusLen)); |
| 47 | 47 |
| 48 // Tell WebKit never to cache this content. | 48 // Tell WebKit never to cache this content. |
| (...skipping 10 matching lines...) Expand all Loading... |
| 59 const std::string& storage_domain, | 59 const std::string& storage_domain, |
| 60 FileSystemContext* file_system_context) | 60 FileSystemContext* file_system_context) |
| 61 : URLRequestJob(request, network_delegate), | 61 : URLRequestJob(request, network_delegate), |
| 62 storage_domain_(storage_domain), | 62 storage_domain_(storage_domain), |
| 63 file_system_context_(file_system_context), | 63 file_system_context_(file_system_context), |
| 64 is_directory_(false), | 64 is_directory_(false), |
| 65 remaining_bytes_(0), | 65 remaining_bytes_(0), |
| 66 weak_factory_(this) { | 66 weak_factory_(this) { |
| 67 } | 67 } |
| 68 | 68 |
| 69 FileSystemURLRequestJob::~FileSystemURLRequestJob() {} | 69 FileSystemURLRequestJob::~FileSystemURLRequestJob() { |
| 70 } |
| 70 | 71 |
| 71 void FileSystemURLRequestJob::Start() { | 72 void FileSystemURLRequestJob::Start() { |
| 72 base::MessageLoop::current()->PostTask( | 73 base::MessageLoop::current()->PostTask( |
| 73 FROM_HERE, | 74 FROM_HERE, |
| 74 base::Bind(&FileSystemURLRequestJob::StartAsync, | 75 base::Bind(&FileSystemURLRequestJob::StartAsync, |
| 75 weak_factory_.GetWeakPtr())); | 76 weak_factory_.GetWeakPtr())); |
| 76 } | 77 } |
| 77 | 78 |
| 78 void FileSystemURLRequestJob::Kill() { | 79 void FileSystemURLRequestJob::Kill() { |
| 79 reader_.reset(); | 80 reader_.reset(); |
| 80 URLRequestJob::Kill(); | 81 URLRequestJob::Kill(); |
| 81 weak_factory_.InvalidateWeakPtrs(); | 82 weak_factory_.InvalidateWeakPtrs(); |
| 82 } | 83 } |
| 83 | 84 |
| 84 bool FileSystemURLRequestJob::ReadRawData(net::IOBuffer* dest, int dest_size, | 85 bool FileSystemURLRequestJob::ReadRawData(net::IOBuffer* dest, |
| 85 int *bytes_read) { | 86 int dest_size, |
| 87 int* bytes_read) { |
| 86 DCHECK_NE(dest_size, 0); | 88 DCHECK_NE(dest_size, 0); |
| 87 DCHECK(bytes_read); | 89 DCHECK(bytes_read); |
| 88 DCHECK_GE(remaining_bytes_, 0); | 90 DCHECK_GE(remaining_bytes_, 0); |
| 89 | 91 |
| 90 if (reader_.get() == NULL) | 92 if (reader_.get() == NULL) |
| 91 return false; | 93 return false; |
| 92 | 94 |
| 93 if (remaining_bytes_ < dest_size) | 95 if (remaining_bytes_ < dest_size) |
| 94 dest_size = static_cast<int>(remaining_bytes_); | 96 dest_size = static_cast<int>(remaining_bytes_); |
| 95 | 97 |
| 96 if (!dest_size) { | 98 if (!dest_size) { |
| 97 *bytes_read = 0; | 99 *bytes_read = 0; |
| 98 return true; | 100 return true; |
| 99 } | 101 } |
| 100 | 102 |
| 101 const int rv = reader_->Read(dest, dest_size, | 103 const int rv = reader_->Read(dest, |
| 104 dest_size, |
| 102 base::Bind(&FileSystemURLRequestJob::DidRead, | 105 base::Bind(&FileSystemURLRequestJob::DidRead, |
| 103 weak_factory_.GetWeakPtr())); | 106 weak_factory_.GetWeakPtr())); |
| 104 if (rv >= 0) { | 107 if (rv >= 0) { |
| 105 // Data is immediately available. | 108 // Data is immediately available. |
| 106 *bytes_read = rv; | 109 *bytes_read = rv; |
| 107 remaining_bytes_ -= rv; | 110 remaining_bytes_ -= rv; |
| 108 DCHECK_GE(remaining_bytes_, 0); | 111 DCHECK_GE(remaining_bytes_, 0); |
| 109 return true; | 112 return true; |
| 110 } | 113 } |
| 111 if (rv == net::ERR_IO_PENDING) | 114 if (rv == net::ERR_IO_PENDING) |
| (...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 183 StartAsync(); | 186 StartAsync(); |
| 184 } else { | 187 } else { |
| 185 NotifyFailed(net::ERR_FILE_NOT_FOUND); | 188 NotifyFailed(net::ERR_FILE_NOT_FOUND); |
| 186 } | 189 } |
| 187 } | 190 } |
| 188 | 191 |
| 189 void FileSystemURLRequestJob::DidGetMetadata( | 192 void FileSystemURLRequestJob::DidGetMetadata( |
| 190 base::File::Error error_code, | 193 base::File::Error error_code, |
| 191 const base::File::Info& file_info) { | 194 const base::File::Info& file_info) { |
| 192 if (error_code != base::File::FILE_OK) { | 195 if (error_code != base::File::FILE_OK) { |
| 193 NotifyFailed(error_code == base::File::FILE_ERROR_INVALID_URL ? | 196 NotifyFailed(error_code == base::File::FILE_ERROR_INVALID_URL |
| 194 net::ERR_INVALID_URL : net::ERR_FILE_NOT_FOUND); | 197 ? net::ERR_INVALID_URL |
| 198 : net::ERR_FILE_NOT_FOUND); |
| 195 return; | 199 return; |
| 196 } | 200 } |
| 197 | 201 |
| 198 // We may have been orphaned... | 202 // We may have been orphaned... |
| 199 if (!request_) | 203 if (!request_) |
| 200 return; | 204 return; |
| 201 | 205 |
| 202 is_directory_ = file_info.is_directory; | 206 is_directory_ = file_info.is_directory; |
| 203 | 207 |
| 204 if (!byte_range_.ComputeBounds(file_info.size)) { | 208 if (!byte_range_.ComputeBounds(file_info.size)) { |
| 205 NotifyFailed(net::ERR_REQUEST_RANGE_NOT_SATISFIABLE); | 209 NotifyFailed(net::ERR_REQUEST_RANGE_NOT_SATISFIABLE); |
| 206 return; | 210 return; |
| 207 } | 211 } |
| 208 | 212 |
| 209 if (is_directory_) { | 213 if (is_directory_) { |
| 210 NotifyHeadersComplete(); | 214 NotifyHeadersComplete(); |
| 211 return; | 215 return; |
| 212 } | 216 } |
| 213 | 217 |
| 214 remaining_bytes_ = byte_range_.last_byte_position() - | 218 remaining_bytes_ = |
| 215 byte_range_.first_byte_position() + 1; | 219 byte_range_.last_byte_position() - byte_range_.first_byte_position() + 1; |
| 216 DCHECK_GE(remaining_bytes_, 0); | 220 DCHECK_GE(remaining_bytes_, 0); |
| 217 | 221 |
| 218 DCHECK(!reader_.get()); | 222 DCHECK(!reader_.get()); |
| 219 reader_ = file_system_context_->CreateFileStreamReader( | 223 reader_ = file_system_context_->CreateFileStreamReader( |
| 220 url_, byte_range_.first_byte_position(), base::Time()); | 224 url_, byte_range_.first_byte_position(), base::Time()); |
| 221 | 225 |
| 222 set_expected_content_size(remaining_bytes_); | 226 set_expected_content_size(remaining_bytes_); |
| 223 response_info_.reset(new net::HttpResponseInfo()); | 227 response_info_.reset(new net::HttpResponseInfo()); |
| 224 response_info_->headers = CreateHttpResponseHeaders(); | 228 response_info_->headers = CreateHttpResponseHeaders(); |
| 225 NotifyHeadersComplete(); | 229 NotifyHeadersComplete(); |
| (...skipping 27 matching lines...) Expand all Loading... |
| 253 return true; | 257 return true; |
| 254 } | 258 } |
| 255 | 259 |
| 256 return false; | 260 return false; |
| 257 } | 261 } |
| 258 | 262 |
| 259 void FileSystemURLRequestJob::NotifyFailed(int rv) { | 263 void FileSystemURLRequestJob::NotifyFailed(int rv) { |
| 260 NotifyDone(URLRequestStatus(URLRequestStatus::FAILED, rv)); | 264 NotifyDone(URLRequestStatus(URLRequestStatus::FAILED, rv)); |
| 261 } | 265 } |
| 262 | 266 |
| 263 } // namespace fileapi | 267 } // namespace storage |
| OLD | NEW |