| 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 "storage/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" |
| (...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 55 FileSystemURLRequestJob::FileSystemURLRequestJob( | 55 FileSystemURLRequestJob::FileSystemURLRequestJob( |
| 56 URLRequest* request, | 56 URLRequest* request, |
| 57 NetworkDelegate* network_delegate, | 57 NetworkDelegate* network_delegate, |
| 58 const std::string& storage_domain, | 58 const std::string& storage_domain, |
| 59 FileSystemContext* file_system_context) | 59 FileSystemContext* file_system_context) |
| 60 : URLRequestJob(request, network_delegate), | 60 : URLRequestJob(request, network_delegate), |
| 61 storage_domain_(storage_domain), | 61 storage_domain_(storage_domain), |
| 62 file_system_context_(file_system_context), | 62 file_system_context_(file_system_context), |
| 63 is_directory_(false), | 63 is_directory_(false), |
| 64 remaining_bytes_(0), | 64 remaining_bytes_(0), |
| 65 range_parse_result_(net::OK), | |
| 66 weak_factory_(this) {} | 65 weak_factory_(this) {} |
| 67 | 66 |
| 68 FileSystemURLRequestJob::~FileSystemURLRequestJob() {} | 67 FileSystemURLRequestJob::~FileSystemURLRequestJob() {} |
| 69 | 68 |
| 70 void FileSystemURLRequestJob::Start() { | 69 void FileSystemURLRequestJob::Start() { |
| 71 base::MessageLoop::current()->PostTask( | 70 base::MessageLoop::current()->PostTask( |
| 72 FROM_HERE, | 71 FROM_HERE, |
| 73 base::Bind(&FileSystemURLRequestJob::StartAsync, | 72 base::Bind(&FileSystemURLRequestJob::StartAsync, |
| 74 weak_factory_.GetWeakPtr())); | 73 weak_factory_.GetWeakPtr())); |
| 75 } | 74 } |
| 76 | 75 |
| 77 void FileSystemURLRequestJob::Kill() { | 76 void FileSystemURLRequestJob::Kill() { |
| 78 reader_.reset(); | 77 reader_.reset(); |
| 79 URLRequestJob::Kill(); | 78 URLRequestJob::Kill(); |
| 80 weak_factory_.InvalidateWeakPtrs(); | 79 weak_factory_.InvalidateWeakPtrs(); |
| 81 } | 80 } |
| 82 | 81 |
| 83 int FileSystemURLRequestJob::ReadRawData(net::IOBuffer* dest, int dest_size) { | 82 bool FileSystemURLRequestJob::ReadRawData(net::IOBuffer* dest, |
| 83 int dest_size, |
| 84 int* bytes_read) { |
| 84 DCHECK_NE(dest_size, 0); | 85 DCHECK_NE(dest_size, 0); |
| 86 DCHECK(bytes_read); |
| 85 DCHECK_GE(remaining_bytes_, 0); | 87 DCHECK_GE(remaining_bytes_, 0); |
| 86 | 88 |
| 87 if (reader_.get() == NULL) | 89 if (reader_.get() == NULL) |
| 88 return net::ERR_FAILED; | 90 return false; |
| 89 | 91 |
| 90 if (remaining_bytes_ < dest_size) | 92 if (remaining_bytes_ < dest_size) |
| 91 dest_size = remaining_bytes_; | 93 dest_size = static_cast<int>(remaining_bytes_); |
| 92 | 94 |
| 93 if (!dest_size) | 95 if (!dest_size) { |
| 94 return 0; | 96 *bytes_read = 0; |
| 97 return true; |
| 98 } |
| 95 | 99 |
| 96 const int rv = reader_->Read(dest, dest_size, | 100 const int rv = reader_->Read(dest, dest_size, |
| 97 base::Bind(&FileSystemURLRequestJob::DidRead, | 101 base::Bind(&FileSystemURLRequestJob::DidRead, |
| 98 weak_factory_.GetWeakPtr())); | 102 weak_factory_.GetWeakPtr())); |
| 99 if (rv >= 0) { | 103 if (rv >= 0) { |
| 104 // Data is immediately available. |
| 105 *bytes_read = rv; |
| 100 remaining_bytes_ -= rv; | 106 remaining_bytes_ -= rv; |
| 101 DCHECK_GE(remaining_bytes_, 0); | 107 DCHECK_GE(remaining_bytes_, 0); |
| 108 return true; |
| 102 } | 109 } |
| 103 | 110 if (rv == net::ERR_IO_PENDING) |
| 104 return rv; | 111 SetStatus(URLRequestStatus(URLRequestStatus::IO_PENDING, 0)); |
| 112 else |
| 113 NotifyFailed(rv); |
| 114 return false; |
| 105 } | 115 } |
| 106 | 116 |
| 107 bool FileSystemURLRequestJob::GetMimeType(std::string* mime_type) const { | 117 bool FileSystemURLRequestJob::GetMimeType(std::string* mime_type) const { |
| 108 DCHECK(request_); | 118 DCHECK(request_); |
| 109 DCHECK(url_.is_valid()); | 119 DCHECK(url_.is_valid()); |
| 110 base::FilePath::StringType extension = url_.path().Extension(); | 120 base::FilePath::StringType extension = url_.path().Extension(); |
| 111 if (!extension.empty()) | 121 if (!extension.empty()) |
| 112 extension = extension.substr(1); | 122 extension = extension.substr(1); |
| 113 return net::GetWellKnownMimeTypeFromExtension(extension, mime_type); | 123 return net::GetWellKnownMimeTypeFromExtension(extension, mime_type); |
| 114 } | 124 } |
| 115 | 125 |
| 116 void FileSystemURLRequestJob::SetExtraRequestHeaders( | 126 void FileSystemURLRequestJob::SetExtraRequestHeaders( |
| 117 const net::HttpRequestHeaders& headers) { | 127 const net::HttpRequestHeaders& headers) { |
| 118 std::string range_header; | 128 std::string range_header; |
| 119 // Currently this job only cares about the Range header. Note that validation | |
| 120 // is deferred to DidGetMetaData(), because NotifyStartError is not legal to | |
| 121 // call since the job has not started. | |
| 122 if (headers.GetHeader(net::HttpRequestHeaders::kRange, &range_header)) { | 129 if (headers.GetHeader(net::HttpRequestHeaders::kRange, &range_header)) { |
| 123 std::vector<net::HttpByteRange> ranges; | 130 std::vector<net::HttpByteRange> ranges; |
| 124 | |
| 125 if (net::HttpUtil::ParseRangeHeader(range_header, &ranges)) { | 131 if (net::HttpUtil::ParseRangeHeader(range_header, &ranges)) { |
| 126 if (ranges.size() == 1) { | 132 if (ranges.size() == 1) { |
| 127 byte_range_ = ranges[0]; | 133 byte_range_ = ranges[0]; |
| 128 } else { | 134 } else { |
| 129 // We don't support multiple range requests in one single URL request. | 135 // We don't support multiple range requests in one single URL request. |
| 130 // TODO(adamk): decide whether we want to support multiple range | 136 // TODO(adamk): decide whether we want to support multiple range |
| 131 // requests. | 137 // requests. |
| 132 range_parse_result_ = net::ERR_REQUEST_RANGE_NOT_SATISFIABLE; | 138 NotifyFailed(net::ERR_REQUEST_RANGE_NOT_SATISFIABLE); |
| 133 } | 139 } |
| 134 } | 140 } |
| 135 } | 141 } |
| 136 } | 142 } |
| 137 | 143 |
| 138 void FileSystemURLRequestJob::GetResponseInfo(net::HttpResponseInfo* info) { | 144 void FileSystemURLRequestJob::GetResponseInfo(net::HttpResponseInfo* info) { |
| 139 if (response_info_) | 145 if (response_info_) |
| 140 *info = *response_info_; | 146 *info = *response_info_; |
| 141 } | 147 } |
| 142 | 148 |
| (...skipping 11 matching lines...) Expand all Loading... |
| 154 if (!url_.is_valid()) { | 160 if (!url_.is_valid()) { |
| 155 file_system_context_->AttemptAutoMountForURLRequest( | 161 file_system_context_->AttemptAutoMountForURLRequest( |
| 156 request_, | 162 request_, |
| 157 storage_domain_, | 163 storage_domain_, |
| 158 base::Bind(&FileSystemURLRequestJob::DidAttemptAutoMount, | 164 base::Bind(&FileSystemURLRequestJob::DidAttemptAutoMount, |
| 159 weak_factory_.GetWeakPtr())); | 165 weak_factory_.GetWeakPtr())); |
| 160 return; | 166 return; |
| 161 } | 167 } |
| 162 if (!file_system_context_->CanServeURLRequest(url_)) { | 168 if (!file_system_context_->CanServeURLRequest(url_)) { |
| 163 // In incognito mode the API is not usable and there should be no data. | 169 // In incognito mode the API is not usable and there should be no data. |
| 164 NotifyStartError(URLRequestStatus::FromError(net::ERR_FILE_NOT_FOUND)); | 170 NotifyFailed(net::ERR_FILE_NOT_FOUND); |
| 165 return; | 171 return; |
| 166 } | 172 } |
| 167 file_system_context_->operation_runner()->GetMetadata( | 173 file_system_context_->operation_runner()->GetMetadata( |
| 168 url_, FileSystemOperation::GET_METADATA_FIELD_IS_DIRECTORY | | 174 url_, FileSystemOperation::GET_METADATA_FIELD_IS_DIRECTORY | |
| 169 FileSystemOperation::GET_METADATA_FIELD_SIZE, | 175 FileSystemOperation::GET_METADATA_FIELD_SIZE, |
| 170 base::Bind(&FileSystemURLRequestJob::DidGetMetadata, | 176 base::Bind(&FileSystemURLRequestJob::DidGetMetadata, |
| 171 weak_factory_.GetWeakPtr())); | 177 weak_factory_.GetWeakPtr())); |
| 172 } | 178 } |
| 173 | 179 |
| 174 void FileSystemURLRequestJob::DidAttemptAutoMount(base::File::Error result) { | 180 void FileSystemURLRequestJob::DidAttemptAutoMount(base::File::Error result) { |
| 175 if (result >= 0 && | 181 if (result >= 0 && |
| 176 file_system_context_->CrackURL(request_->url()).is_valid()) { | 182 file_system_context_->CrackURL(request_->url()).is_valid()) { |
| 177 StartAsync(); | 183 StartAsync(); |
| 178 } else { | 184 } else { |
| 179 NotifyStartError(URLRequestStatus::FromError(net::ERR_FILE_NOT_FOUND)); | 185 NotifyFailed(net::ERR_FILE_NOT_FOUND); |
| 180 } | 186 } |
| 181 } | 187 } |
| 182 | 188 |
| 183 void FileSystemURLRequestJob::DidGetMetadata( | 189 void FileSystemURLRequestJob::DidGetMetadata( |
| 184 base::File::Error error_code, | 190 base::File::Error error_code, |
| 185 const base::File::Info& file_info) { | 191 const base::File::Info& file_info) { |
| 186 if (error_code != base::File::FILE_OK) { | 192 if (error_code != base::File::FILE_OK) { |
| 187 NotifyStartError(URLRequestStatus::FromError( | 193 NotifyFailed(error_code == base::File::FILE_ERROR_INVALID_URL |
| 188 error_code == base::File::FILE_ERROR_INVALID_URL | 194 ? net::ERR_INVALID_URL |
| 189 ? net::ERR_INVALID_URL | 195 : net::ERR_FILE_NOT_FOUND); |
| 190 : net::ERR_FILE_NOT_FOUND)); | |
| 191 return; | 196 return; |
| 192 } | 197 } |
| 193 | 198 |
| 194 // We may have been orphaned... | 199 // We may have been orphaned... |
| 195 if (!request_) | 200 if (!request_) |
| 196 return; | 201 return; |
| 197 | 202 |
| 198 is_directory_ = file_info.is_directory; | 203 is_directory_ = file_info.is_directory; |
| 199 | 204 |
| 200 if (range_parse_result_ != net::OK) { | 205 if (!byte_range_.ComputeBounds(file_info.size)) { |
| 201 NotifyStartError(URLRequestStatus::FromError(range_parse_result_)); | 206 NotifyFailed(net::ERR_REQUEST_RANGE_NOT_SATISFIABLE); |
| 202 return; | 207 return; |
| 203 } | 208 } |
| 204 | 209 |
| 205 if (!byte_range_.ComputeBounds(file_info.size)) { | |
| 206 NotifyStartError( | |
| 207 URLRequestStatus::FromError(net::ERR_REQUEST_RANGE_NOT_SATISFIABLE)); | |
| 208 return; | |
| 209 } | |
| 210 | |
| 211 if (is_directory_) { | 210 if (is_directory_) { |
| 212 NotifyHeadersComplete(); | 211 NotifyHeadersComplete(); |
| 213 return; | 212 return; |
| 214 } | 213 } |
| 215 | 214 |
| 216 remaining_bytes_ = byte_range_.last_byte_position() - | 215 remaining_bytes_ = byte_range_.last_byte_position() - |
| 217 byte_range_.first_byte_position() + 1; | 216 byte_range_.first_byte_position() + 1; |
| 218 DCHECK_GE(remaining_bytes_, 0); | 217 DCHECK_GE(remaining_bytes_, 0); |
| 219 | 218 |
| 220 DCHECK(!reader_.get()); | 219 DCHECK(!reader_.get()); |
| 221 reader_ = file_system_context_->CreateFileStreamReader( | 220 reader_ = file_system_context_->CreateFileStreamReader( |
| 222 url_, byte_range_.first_byte_position(), remaining_bytes_, base::Time()); | 221 url_, byte_range_.first_byte_position(), remaining_bytes_, base::Time()); |
| 223 | 222 |
| 224 set_expected_content_size(remaining_bytes_); | 223 set_expected_content_size(remaining_bytes_); |
| 225 response_info_.reset(new net::HttpResponseInfo()); | 224 response_info_.reset(new net::HttpResponseInfo()); |
| 226 response_info_->headers = CreateHttpResponseHeaders(); | 225 response_info_->headers = CreateHttpResponseHeaders(); |
| 227 NotifyHeadersComplete(); | 226 NotifyHeadersComplete(); |
| 228 } | 227 } |
| 229 | 228 |
| 230 void FileSystemURLRequestJob::DidRead(int result) { | 229 void FileSystemURLRequestJob::DidRead(int result) { |
| 231 if (result >= 0) { | 230 if (result > 0) |
| 232 remaining_bytes_ -= result; | 231 SetStatus(URLRequestStatus()); // Clear the IO_PENDING status |
| 233 DCHECK_GE(remaining_bytes_, 0); | 232 else if (result == 0) |
| 234 } | 233 NotifyDone(URLRequestStatus()); |
| 234 else |
| 235 NotifyFailed(result); |
| 235 | 236 |
| 236 ReadRawDataComplete(result); | 237 remaining_bytes_ -= result; |
| 238 DCHECK_GE(remaining_bytes_, 0); |
| 239 |
| 240 NotifyReadComplete(result); |
| 237 } | 241 } |
| 238 | 242 |
| 239 bool FileSystemURLRequestJob::IsRedirectResponse(GURL* location, | 243 bool FileSystemURLRequestJob::IsRedirectResponse(GURL* location, |
| 240 int* http_status_code) { | 244 int* http_status_code) { |
| 241 if (is_directory_) { | 245 if (is_directory_) { |
| 242 // This happens when we discovered the file is a directory, so needs a | 246 // This happens when we discovered the file is a directory, so needs a |
| 243 // slash at the end of the path. | 247 // slash at the end of the path. |
| 244 std::string new_path = request_->url().path(); | 248 std::string new_path = request_->url().path(); |
| 245 new_path.push_back('/'); | 249 new_path.push_back('/'); |
| 246 GURL::Replacements replacements; | 250 GURL::Replacements replacements; |
| 247 replacements.SetPathStr(new_path); | 251 replacements.SetPathStr(new_path); |
| 248 *location = request_->url().ReplaceComponents(replacements); | 252 *location = request_->url().ReplaceComponents(replacements); |
| 249 *http_status_code = 301; // simulate a permanent redirect | 253 *http_status_code = 301; // simulate a permanent redirect |
| 250 return true; | 254 return true; |
| 251 } | 255 } |
| 252 | 256 |
| 253 return false; | 257 return false; |
| 254 } | 258 } |
| 255 | 259 |
| 260 void FileSystemURLRequestJob::NotifyFailed(int rv) { |
| 261 NotifyDone(URLRequestStatus(URLRequestStatus::FAILED, rv)); |
| 262 } |
| 263 |
| 256 } // namespace storage | 264 } // namespace storage |
| OLD | NEW |