| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 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 "chrome/browser/chromeos/fileapi/external_file_url_request_job.h" | 5 #include "chrome/browser/chromeos/fileapi/external_file_url_request_job.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 #include <vector> | 8 #include <vector> |
| 9 | 9 |
| 10 #include "base/bind.h" | 10 #include "base/bind.h" |
| (...skipping 145 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 156 ExternalFileURLRequestJob::IsolatedFileSystemScope::~IsolatedFileSystemScope() { | 156 ExternalFileURLRequestJob::IsolatedFileSystemScope::~IsolatedFileSystemScope() { |
| 157 storage::IsolatedContext::GetInstance()->RevokeFileSystem(file_system_id_); | 157 storage::IsolatedContext::GetInstance()->RevokeFileSystem(file_system_id_); |
| 158 } | 158 } |
| 159 | 159 |
| 160 ExternalFileURLRequestJob::ExternalFileURLRequestJob( | 160 ExternalFileURLRequestJob::ExternalFileURLRequestJob( |
| 161 void* profile_id, | 161 void* profile_id, |
| 162 net::URLRequest* request, | 162 net::URLRequest* request, |
| 163 net::NetworkDelegate* network_delegate) | 163 net::NetworkDelegate* network_delegate) |
| 164 : net::URLRequestJob(request, network_delegate), | 164 : net::URLRequestJob(request, network_delegate), |
| 165 profile_id_(profile_id), | 165 profile_id_(profile_id), |
| 166 range_parse_result_(net::OK), |
| 166 remaining_bytes_(0), | 167 remaining_bytes_(0), |
| 167 weak_ptr_factory_(this) { | 168 weak_ptr_factory_(this) {} |
| 168 } | |
| 169 | 169 |
| 170 // Extracts headers that this job cares about from the supplied request headers. |
| 171 // Currently this job only cares about the Range header. Note that validation is |
| 172 // deferred to Start, because this method may be called with a |
| 173 // net::URLRequest::Delegate call still on the stack, which means |
| 174 // NotifyStartError is not safe to call (it may reenter the delegate). |
| 170 void ExternalFileURLRequestJob::SetExtraRequestHeaders( | 175 void ExternalFileURLRequestJob::SetExtraRequestHeaders( |
| 171 const net::HttpRequestHeaders& headers) { | 176 const net::HttpRequestHeaders& headers) { |
| 172 std::string range_header; | 177 std::string range_header; |
| 173 if (headers.GetHeader(net::HttpRequestHeaders::kRange, &range_header)) { | 178 if (headers.GetHeader(net::HttpRequestHeaders::kRange, &range_header)) { |
| 174 // Note: We only support single range requests. | 179 // Note: We only support single range requests. |
| 175 std::vector<net::HttpByteRange> ranges; | 180 std::vector<net::HttpByteRange> ranges; |
| 176 if (net::HttpUtil::ParseRangeHeader(range_header, &ranges) && | 181 if (net::HttpUtil::ParseRangeHeader(range_header, &ranges) && |
| 177 ranges.size() == 1) { | 182 ranges.size() == 1) { |
| 178 byte_range_ = ranges[0]; | 183 byte_range_ = ranges[0]; |
| 179 } else { | 184 } else { |
| 180 // Failed to parse Range: header, so notify the error. | 185 // Failed to parse Range: header, so save the error whih will be reported |
| 181 NotifyDone(net::URLRequestStatus(net::URLRequestStatus::FAILED, | 186 // in Start. |
| 182 net::ERR_REQUEST_RANGE_NOT_SATISFIABLE)); | 187 range_parse_result_ = net::ERR_REQUEST_RANGE_NOT_SATISFIABLE; |
| 183 } | 188 } |
| 184 } | 189 } |
| 185 } | 190 } |
| 186 | 191 |
| 187 void ExternalFileURLRequestJob::Start() { | 192 void ExternalFileURLRequestJob::Start() { |
| 188 DVLOG(1) << "Starting request"; | 193 DVLOG(1) << "Starting request"; |
| 189 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 194 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 190 DCHECK(!stream_reader_); | 195 DCHECK(!stream_reader_); |
| 191 | 196 |
| 197 if (range_parse_result_ != net::OK) { |
| 198 NotifyStartError(net::URLRequestStatus(net::URLRequestStatus::FAILED, |
| 199 range_parse_result_)); |
| 200 return; |
| 201 } |
| 202 |
| 192 // We only support GET request. | 203 // We only support GET request. |
| 193 if (request()->method() != "GET") { | 204 if (request()->method() != "GET") { |
| 194 LOG(WARNING) << "Failed to start request: " << request()->method() | 205 LOG(WARNING) << "Failed to start request: " << request()->method() |
| 195 << " method is not supported"; | 206 << " method is not supported"; |
| 196 NotifyStartError(net::URLRequestStatus(net::URLRequestStatus::FAILED, | 207 NotifyStartError(net::URLRequestStatus(net::URLRequestStatus::FAILED, |
| 197 net::ERR_METHOD_NOT_SUPPORTED)); | 208 net::ERR_METHOD_NOT_SUPPORTED)); |
| 198 return; | 209 return; |
| 199 } | 210 } |
| 200 | 211 |
| 201 // Check if the scheme is correct. | 212 // Check if the scheme is correct. |
| (...skipping 117 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 319 if (redirect_url_.is_empty()) | 330 if (redirect_url_.is_empty()) |
| 320 return false; | 331 return false; |
| 321 | 332 |
| 322 // Redirect a hosted document. | 333 // Redirect a hosted document. |
| 323 *location = redirect_url_; | 334 *location = redirect_url_; |
| 324 const int kHttpFound = 302; | 335 const int kHttpFound = 302; |
| 325 *http_status_code = kHttpFound; | 336 *http_status_code = kHttpFound; |
| 326 return true; | 337 return true; |
| 327 } | 338 } |
| 328 | 339 |
| 329 bool ExternalFileURLRequestJob::ReadRawData(net::IOBuffer* buf, | 340 int ExternalFileURLRequestJob::ReadRawData(net::IOBuffer* buf, int buf_size) { |
| 330 int buf_size, | |
| 331 int* bytes_read) { | |
| 332 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 341 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 333 DCHECK(stream_reader_); | 342 DCHECK(stream_reader_); |
| 334 | 343 |
| 335 if (remaining_bytes_ == 0) { | 344 if (remaining_bytes_ == 0) |
| 336 *bytes_read = 0; | 345 return 0; |
| 337 return true; | |
| 338 } | |
| 339 | 346 |
| 340 const int result = stream_reader_->Read( | 347 const int result = stream_reader_->Read( |
| 341 buf, | 348 buf, std::min<int64>(buf_size, remaining_bytes_), |
| 342 std::min<int64>(buf_size, remaining_bytes_), | |
| 343 base::Bind(&ExternalFileURLRequestJob::OnReadCompleted, | 349 base::Bind(&ExternalFileURLRequestJob::OnReadCompleted, |
| 344 weak_ptr_factory_.GetWeakPtr())); | 350 weak_ptr_factory_.GetWeakPtr())); |
| 345 | 351 |
| 346 if (result == net::ERR_IO_PENDING) { | 352 if (result < 0) |
| 347 // The data is not yet available. | 353 return result; |
| 348 SetStatus(net::URLRequestStatus(net::URLRequestStatus::IO_PENDING, 0)); | |
| 349 return false; | |
| 350 } | |
| 351 if (result < 0) { | |
| 352 // An error occurs. | |
| 353 NotifyDone(net::URLRequestStatus(net::URLRequestStatus::FAILED, result)); | |
| 354 return false; | |
| 355 } | |
| 356 | 354 |
| 357 // Reading has been finished immediately. | |
| 358 *bytes_read = result; | |
| 359 remaining_bytes_ -= result; | 355 remaining_bytes_ -= result; |
| 360 return true; | 356 return result; |
| 361 } | 357 } |
| 362 | 358 |
| 363 ExternalFileURLRequestJob::~ExternalFileURLRequestJob() { | 359 ExternalFileURLRequestJob::~ExternalFileURLRequestJob() { |
| 364 } | 360 } |
| 365 | 361 |
| 366 void ExternalFileURLRequestJob::OnReadCompleted(int read_result) { | 362 void ExternalFileURLRequestJob::OnReadCompleted(int read_result) { |
| 367 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 363 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 368 | 364 |
| 369 if (read_result < 0) { | 365 if (read_result > 0) |
| 370 DCHECK_NE(read_result, net::ERR_IO_PENDING); | 366 remaining_bytes_ -= read_result; |
| 371 NotifyDone( | |
| 372 net::URLRequestStatus(net::URLRequestStatus::FAILED, read_result)); | |
| 373 } | |
| 374 | 367 |
| 375 remaining_bytes_ -= read_result; | 368 ReadRawDataComplete(read_result); |
| 376 SetStatus(net::URLRequestStatus()); // Clear the IO_PENDING status. | |
| 377 NotifyReadComplete(read_result); | |
| 378 } | 369 } |
| 379 | 370 |
| 380 } // namespace chromeos | 371 } // namespace chromeos |
| OLD | NEW |