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" |
11 #include "base/logging.h" | 11 #include "base/logging.h" |
12 #include "base/memory/ref_counted.h" | 12 #include "base/memory/ref_counted.h" |
| 13 #include "base/thread_task_runner_handle.h" |
13 #include "chrome/browser/browser_process.h" | 14 #include "chrome/browser/browser_process.h" |
14 #include "chrome/browser/chromeos/file_manager/fileapi_util.h" | 15 #include "chrome/browser/chromeos/file_manager/fileapi_util.h" |
15 #include "chrome/browser/chromeos/fileapi/external_file_url_util.h" | 16 #include "chrome/browser/chromeos/fileapi/external_file_url_util.h" |
16 #include "chrome/browser/extensions/api/file_handlers/mime_util.h" | 17 #include "chrome/browser/extensions/api/file_handlers/mime_util.h" |
17 #include "chrome/browser/profiles/profile_manager.h" | 18 #include "chrome/browser/profiles/profile_manager.h" |
18 #include "components/drive/file_system_core_util.h" | 19 #include "components/drive/file_system_core_util.h" |
19 #include "content/public/browser/browser_thread.h" | 20 #include "content/public/browser/browser_thread.h" |
20 #include "content/public/browser/storage_partition.h" | 21 #include "content/public/browser/storage_partition.h" |
21 #include "content/public/common/url_constants.h" | 22 #include "content/public/common/url_constants.h" |
22 #include "net/base/net_errors.h" | |
23 #include "net/http/http_byte_range.h" | 23 #include "net/http/http_byte_range.h" |
24 #include "net/http/http_request_headers.h" | 24 #include "net/http/http_request_headers.h" |
25 #include "net/http/http_response_info.h" | 25 #include "net/http/http_response_info.h" |
26 #include "net/http/http_util.h" | 26 #include "net/http/http_util.h" |
27 #include "net/url_request/url_request.h" | 27 #include "net/url_request/url_request.h" |
28 #include "net/url_request/url_request_status.h" | 28 #include "net/url_request/url_request_status.h" |
29 #include "storage/browser/fileapi/external_mount_points.h" | 29 #include "storage/browser/fileapi/external_mount_points.h" |
30 #include "storage/browser/fileapi/file_system_backend.h" | 30 #include "storage/browser/fileapi/file_system_backend.h" |
31 #include "storage/browser/fileapi/file_system_context.h" | 31 #include "storage/browser/fileapi/file_system_context.h" |
32 #include "storage/browser/fileapi/file_system_operation_runner.h" | 32 #include "storage/browser/fileapi/file_system_operation_runner.h" |
(...skipping 123 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 void ExternalFileURLRequestJob::SetExtraRequestHeaders( | 170 void ExternalFileURLRequestJob::SetExtraRequestHeaders( |
170 const net::HttpRequestHeaders& headers) { | 171 const net::HttpRequestHeaders& headers) { |
171 std::string range_header; | 172 std::string range_header; |
172 if (headers.GetHeader(net::HttpRequestHeaders::kRange, &range_header)) { | 173 if (headers.GetHeader(net::HttpRequestHeaders::kRange, &range_header)) { |
173 // Note: We only support single range requests. | 174 // Currently this job only cares about the Range header, and only supports |
| 175 // single range requests. Note that validation is deferred to Start, |
| 176 // because NotifyStartError is not legal to call since the job has not |
| 177 // started. |
174 std::vector<net::HttpByteRange> ranges; | 178 std::vector<net::HttpByteRange> ranges; |
175 if (net::HttpUtil::ParseRangeHeader(range_header, &ranges) && | 179 if (net::HttpUtil::ParseRangeHeader(range_header, &ranges) && |
176 ranges.size() == 1) { | 180 ranges.size() == 1) { |
177 byte_range_ = ranges[0]; | 181 byte_range_ = ranges[0]; |
178 } else { | 182 } else { |
179 // Failed to parse Range: header, so notify the error. | 183 range_parse_result_ = net::ERR_REQUEST_RANGE_NOT_SATISFIABLE; |
180 NotifyDone(net::URLRequestStatus(net::URLRequestStatus::FAILED, | |
181 net::ERR_REQUEST_RANGE_NOT_SATISFIABLE)); | |
182 } | 184 } |
183 } | 185 } |
184 } | 186 } |
185 | 187 |
186 void ExternalFileURLRequestJob::Start() { | 188 void ExternalFileURLRequestJob::Start() { |
| 189 // Post a task to invoke StartAsync asynchronously to avoid re-entering the |
| 190 // delegate, because NotifyStartError is not legal to call synchronously in |
| 191 // Start(). |
| 192 base::ThreadTaskRunnerHandle::Get()->PostTask( |
| 193 FROM_HERE, base::Bind(&ExternalFileURLRequestJob::StartAsync, |
| 194 weak_ptr_factory_.GetWeakPtr())); |
| 195 } |
| 196 |
| 197 void ExternalFileURLRequestJob::StartAsync() { |
187 DVLOG(1) << "Starting request"; | 198 DVLOG(1) << "Starting request"; |
188 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 199 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
189 DCHECK(!stream_reader_); | 200 DCHECK(!stream_reader_); |
190 | 201 |
| 202 if (range_parse_result_ != net::OK) { |
| 203 NotifyStartError(net::URLRequestStatus(net::URLRequestStatus::FAILED, |
| 204 range_parse_result_)); |
| 205 return; |
| 206 } |
| 207 |
191 // We only support GET request. | 208 // We only support GET request. |
192 if (request()->method() != "GET") { | 209 if (request()->method() != "GET") { |
193 LOG(WARNING) << "Failed to start request: " << request()->method() | 210 LOG(WARNING) << "Failed to start request: " << request()->method() |
194 << " method is not supported"; | 211 << " method is not supported"; |
195 NotifyStartError(net::URLRequestStatus(net::URLRequestStatus::FAILED, | 212 NotifyStartError(net::URLRequestStatus(net::URLRequestStatus::FAILED, |
196 net::ERR_METHOD_NOT_SUPPORTED)); | 213 net::ERR_METHOD_NOT_SUPPORTED)); |
197 return; | 214 return; |
198 } | 215 } |
199 | 216 |
200 // Check if the scheme is correct. | 217 // Check if the scheme is correct. |
(...skipping 117 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
318 if (redirect_url_.is_empty()) | 335 if (redirect_url_.is_empty()) |
319 return false; | 336 return false; |
320 | 337 |
321 // Redirect a hosted document. | 338 // Redirect a hosted document. |
322 *location = redirect_url_; | 339 *location = redirect_url_; |
323 const int kHttpFound = 302; | 340 const int kHttpFound = 302; |
324 *http_status_code = kHttpFound; | 341 *http_status_code = kHttpFound; |
325 return true; | 342 return true; |
326 } | 343 } |
327 | 344 |
328 bool ExternalFileURLRequestJob::ReadRawData(net::IOBuffer* buf, | 345 int ExternalFileURLRequestJob::ReadRawData(net::IOBuffer* buf, int buf_size) { |
329 int buf_size, | |
330 int* bytes_read) { | |
331 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 346 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
332 DCHECK(stream_reader_); | 347 DCHECK(stream_reader_); |
333 | 348 |
334 if (remaining_bytes_ == 0) { | 349 if (remaining_bytes_ == 0) |
335 *bytes_read = 0; | 350 return 0; |
336 return true; | |
337 } | |
338 | 351 |
339 const int result = stream_reader_->Read( | 352 const int result = stream_reader_->Read( |
340 buf, std::min<int64>(buf_size, remaining_bytes_), | 353 buf, std::min<int64>(buf_size, remaining_bytes_), |
341 base::Bind(&ExternalFileURLRequestJob::OnReadCompleted, | 354 base::Bind(&ExternalFileURLRequestJob::OnReadCompleted, |
342 weak_ptr_factory_.GetWeakPtr())); | 355 weak_ptr_factory_.GetWeakPtr())); |
343 | 356 |
344 if (result == net::ERR_IO_PENDING) { | 357 if (result < 0) |
345 // The data is not yet available. | 358 return result; |
346 SetStatus(net::URLRequestStatus(net::URLRequestStatus::IO_PENDING, 0)); | |
347 return false; | |
348 } | |
349 if (result < 0) { | |
350 // An error occurs. | |
351 NotifyDone(net::URLRequestStatus(net::URLRequestStatus::FAILED, result)); | |
352 return false; | |
353 } | |
354 | 359 |
355 // Reading has been finished immediately. | |
356 *bytes_read = result; | |
357 remaining_bytes_ -= result; | 360 remaining_bytes_ -= result; |
358 return true; | 361 return result; |
359 } | 362 } |
360 | 363 |
361 ExternalFileURLRequestJob::~ExternalFileURLRequestJob() { | 364 ExternalFileURLRequestJob::~ExternalFileURLRequestJob() { |
362 } | 365 } |
363 | 366 |
364 void ExternalFileURLRequestJob::OnReadCompleted(int read_result) { | 367 void ExternalFileURLRequestJob::OnReadCompleted(int read_result) { |
365 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 368 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
366 | 369 |
367 if (read_result < 0) { | 370 if (read_result > 0) |
368 DCHECK_NE(read_result, net::ERR_IO_PENDING); | 371 remaining_bytes_ -= read_result; |
369 NotifyDone( | |
370 net::URLRequestStatus(net::URLRequestStatus::FAILED, read_result)); | |
371 } | |
372 | 372 |
373 remaining_bytes_ -= read_result; | 373 ReadRawDataComplete(read_result); |
374 SetStatus(net::URLRequestStatus()); // Clear the IO_PENDING status. | |
375 NotifyReadComplete(read_result); | |
376 } | 374 } |
377 | 375 |
378 } // namespace chromeos | 376 } // namespace chromeos |
OLD | NEW |