Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1215)

Side by Side Diff: chrome/browser/chromeos/fileapi/external_file_url_request_job.cc

Issue 1410643007: URLRequestJob: change ReadRawData contract (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Address Matt's comments Created 5 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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
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 void ExternalFileURLRequestJob::SetExtraRequestHeaders( 170 void ExternalFileURLRequestJob::SetExtraRequestHeaders(
171 const net::HttpRequestHeaders& headers) { 171 const net::HttpRequestHeaders& headers) {
172 std::string range_header; 172 std::string range_header;
173 if (headers.GetHeader(net::HttpRequestHeaders::kRange, &range_header)) { 173 if (headers.GetHeader(net::HttpRequestHeaders::kRange, &range_header)) {
174 // 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.
175 std::vector<net::HttpByteRange> ranges; 178 std::vector<net::HttpByteRange> ranges;
176 if (net::HttpUtil::ParseRangeHeader(range_header, &ranges) && 179 if (net::HttpUtil::ParseRangeHeader(range_header, &ranges) &&
177 ranges.size() == 1) { 180 ranges.size() == 1) {
178 byte_range_ = ranges[0]; 181 byte_range_ = ranges[0];
179 } else { 182 } else {
180 // Failed to parse Range: header, so notify the error. 183 range_parse_result_ = net::ERR_REQUEST_RANGE_NOT_SATISFIABLE;
181 NotifyDone(net::URLRequestStatus(net::URLRequestStatus::FAILED,
182 net::ERR_REQUEST_RANGE_NOT_SATISFIABLE));
183 } 184 }
184 } 185 }
185 } 186 }
186 187
187 void ExternalFileURLRequestJob::Start() { 188 void ExternalFileURLRequestJob::Start() {
189 // Post a task to invoke DoStart asynchronously to avoid re-entering the
190 // delegate.
191 base::ThreadTaskRunnerHandle::Get()->PostTask(
192 FROM_HERE, base::Bind(&ExternalFileURLRequestJob::DoStart,
193 weak_ptr_factory_.GetWeakPtr()));
194 }
195
196 void ExternalFileURLRequestJob::DoStart() {
mmenke 2015/10/30 20:06:43 Does make reviewing harder, but this should be in
xunjieli 2015/10/30 20:33:28 I considered doing that, but it involves re-orderi
188 DVLOG(1) << "Starting request"; 197 DVLOG(1) << "Starting request";
189 DCHECK_CURRENTLY_ON(BrowserThread::IO); 198 DCHECK_CURRENTLY_ON(BrowserThread::IO);
190 DCHECK(!stream_reader_); 199 DCHECK(!stream_reader_);
191 200
201 if (range_parse_result_ != net::OK) {
202 NotifyStartError(net::URLRequestStatus(net::URLRequestStatus::FAILED,
203 range_parse_result_));
204 return;
205 }
206
192 // We only support GET request. 207 // We only support GET request.
193 if (request()->method() != "GET") { 208 if (request()->method() != "GET") {
194 LOG(WARNING) << "Failed to start request: " << request()->method() 209 LOG(WARNING) << "Failed to start request: " << request()->method()
195 << " method is not supported"; 210 << " method is not supported";
196 NotifyStartError(net::URLRequestStatus(net::URLRequestStatus::FAILED, 211 NotifyStartError(net::URLRequestStatus(net::URLRequestStatus::FAILED,
197 net::ERR_METHOD_NOT_SUPPORTED)); 212 net::ERR_METHOD_NOT_SUPPORTED));
198 return; 213 return;
199 } 214 }
200 215
201 // Check if the scheme is correct. 216 // Check if the scheme is correct.
(...skipping 117 matching lines...) Expand 10 before | Expand all | Expand 10 after
319 if (redirect_url_.is_empty()) 334 if (redirect_url_.is_empty())
320 return false; 335 return false;
321 336
322 // Redirect a hosted document. 337 // Redirect a hosted document.
323 *location = redirect_url_; 338 *location = redirect_url_;
324 const int kHttpFound = 302; 339 const int kHttpFound = 302;
325 *http_status_code = kHttpFound; 340 *http_status_code = kHttpFound;
326 return true; 341 return true;
327 } 342 }
328 343
329 bool ExternalFileURLRequestJob::ReadRawData(net::IOBuffer* buf, 344 int ExternalFileURLRequestJob::ReadRawData(net::IOBuffer* buf, int buf_size) {
330 int buf_size,
331 int* bytes_read) {
332 DCHECK_CURRENTLY_ON(BrowserThread::IO); 345 DCHECK_CURRENTLY_ON(BrowserThread::IO);
333 DCHECK(stream_reader_); 346 DCHECK(stream_reader_);
334 347
335 if (remaining_bytes_ == 0) { 348 if (remaining_bytes_ == 0)
336 *bytes_read = 0; 349 return 0;
337 return true;
338 }
339 350
340 const int result = stream_reader_->Read( 351 const int result = stream_reader_->Read(
341 buf, 352 buf, std::min<int64>(buf_size, remaining_bytes_),
342 std::min<int64>(buf_size, remaining_bytes_),
343 base::Bind(&ExternalFileURLRequestJob::OnReadCompleted, 353 base::Bind(&ExternalFileURLRequestJob::OnReadCompleted,
344 weak_ptr_factory_.GetWeakPtr())); 354 weak_ptr_factory_.GetWeakPtr()));
345 355
346 if (result == net::ERR_IO_PENDING) { 356 if (result < 0)
347 // The data is not yet available. 357 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 358
357 // Reading has been finished immediately.
358 *bytes_read = result;
359 remaining_bytes_ -= result; 359 remaining_bytes_ -= result;
360 return true; 360 return result;
361 } 361 }
362 362
363 ExternalFileURLRequestJob::~ExternalFileURLRequestJob() { 363 ExternalFileURLRequestJob::~ExternalFileURLRequestJob() {
364 } 364 }
365 365
366 void ExternalFileURLRequestJob::OnReadCompleted(int read_result) { 366 void ExternalFileURLRequestJob::OnReadCompleted(int read_result) {
367 DCHECK_CURRENTLY_ON(BrowserThread::IO); 367 DCHECK_CURRENTLY_ON(BrowserThread::IO);
368 368
369 if (read_result < 0) { 369 if (read_result > 0)
370 DCHECK_NE(read_result, net::ERR_IO_PENDING); 370 remaining_bytes_ -= read_result;
371 NotifyDone(
372 net::URLRequestStatus(net::URLRequestStatus::FAILED, read_result));
373 }
374 371
375 remaining_bytes_ -= read_result; 372 ReadRawDataComplete(read_result);
376 SetStatus(net::URLRequestStatus()); // Clear the IO_PENDING status.
377 NotifyReadComplete(read_result);
378 } 373 }
379 374
380 } // namespace chromeos 375 } // namespace chromeos
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698