Index: net/url_request/url_request_file_job.cc |
=================================================================== |
--- net/url_request/url_request_file_job.cc (revision 145483) |
+++ net/url_request/url_request_file_job.cc (working copy) |
@@ -42,54 +42,25 @@ |
namespace net { |
-class URLRequestFileJob::AsyncResolver |
- : public base::RefCountedThreadSafe<URLRequestFileJob::AsyncResolver> { |
- public: |
- explicit AsyncResolver(URLRequestFileJob* owner) |
- : owner_(owner), owner_loop_(MessageLoop::current()) { |
- } |
+namespace { |
- void Resolve(const FilePath& file_path) { |
- base::PlatformFileInfo file_info; |
- bool exists = file_util::GetFileInfo(file_path, &file_info); |
- base::AutoLock locked(lock_); |
- if (owner_loop_) { |
- owner_loop_->PostTask( |
- FROM_HERE, |
- base::Bind(&AsyncResolver::ReturnResults, this, exists, file_info)); |
- } |
- } |
+void Resolve(const FilePath& file_path, |
+ bool* exists, |
+ base::PlatformFileInfo* file_info) { |
+ *exists = file_util::GetFileInfo(file_path, file_info); |
+} |
- void Cancel() { |
- owner_ = NULL; |
+} |
- base::AutoLock locked(lock_); |
- owner_loop_ = NULL; |
- } |
- private: |
- friend class base::RefCountedThreadSafe<URLRequestFileJob::AsyncResolver>; |
- |
- ~AsyncResolver() {} |
- |
- void ReturnResults(bool exists, const base::PlatformFileInfo& file_info) { |
- if (owner_) |
- owner_->DidResolve(exists, file_info); |
- } |
- |
- URLRequestFileJob* owner_; |
- |
- base::Lock lock_; |
- MessageLoop* owner_loop_; |
-}; |
- |
URLRequestFileJob::URLRequestFileJob(URLRequest* request, |
const FilePath& file_path) |
: URLRequestJob(request, request->context()->network_delegate()), |
file_path_(file_path), |
stream_(NULL), |
is_directory_(false), |
eroman
2012/07/10 23:50:56
Could you initialize file_size_ to something like
|
- remaining_bytes_(0) { |
+ remaining_bytes_(0), |
+ weak_ptr_factory_(ALLOW_THIS_IN_INITIALIZER_LIST(this)) { |
} |
// static |
@@ -119,25 +90,22 @@ |
} |
void URLRequestFileJob::Start() { |
- DCHECK(!async_resolver_); |
- async_resolver_ = new AsyncResolver(this); |
- base::WorkerPool::PostTask( |
+ bool* exists = new bool; |
+ base::PlatformFileInfo* file_info = new base::PlatformFileInfo; |
+ base::WorkerPool::PostTaskAndReply( |
FROM_HERE, |
- base::Bind(&AsyncResolver::Resolve, async_resolver_.get(), file_path_), |
+ base::Bind(&Resolve, file_path_, |
+ base::Unretained(exists), base::Unretained(file_info)), |
+ base::Bind(&URLRequestFileJob::DidResolve, |
+ weak_ptr_factory_.GetWeakPtr(), |
+ base::Owned(exists), base::Owned(file_info)), |
true); |
} |
void URLRequestFileJob::Kill() { |
- // URL requests should not block on the disk! |
- // http://code.google.com/p/chromium/issues/detail?id=59849 |
- base::ThreadRestrictions::ScopedAllowIO allow_io; |
- stream_.CloseSync(); |
+ stream_.CloseAndCancelAsync(); |
+ weak_ptr_factory_.InvalidateWeakPtrs(); |
- if (async_resolver_) { |
- async_resolver_->Cancel(); |
- async_resolver_ = NULL; |
- } |
- |
URLRequestJob::Kill(); |
} |
@@ -159,7 +127,7 @@ |
int rv = stream_.Read(dest, dest_size, |
base::Bind(&URLRequestFileJob::DidRead, |
- base::Unretained(this))); |
+ weak_ptr_factory_.GetWeakPtr())); |
if (rv >= 0) { |
// Data is immediately available. |
*bytes_read = rv; |
@@ -263,20 +231,17 @@ |
} |
URLRequestFileJob::~URLRequestFileJob() { |
- DCHECK(!async_resolver_); |
} |
-void URLRequestFileJob::DidResolve( |
- bool exists, const base::PlatformFileInfo& file_info) { |
- async_resolver_ = NULL; |
- |
+void URLRequestFileJob::DidResolve(const bool* exists, |
+ const base::PlatformFileInfo* file_info) { |
// We may have been orphaned... |
if (!request_) |
eroman
2012/07/10 23:50:56
I wander if this is still true, or if Kill() will
|
return; |
- is_directory_ = file_info.is_directory; |
+ is_directory_ = file_info->is_directory; |
+ file_size_ = file_info->size; |
- int rv = OK; |
// We use URLRequestFileJob to handle files as well as directories without |
// trailing slash. |
// If a directory does not exist, we return ERR_FILE_NOT_FOUND. Otherwise, |
@@ -285,25 +250,29 @@ |
// However, Windows resolves "\" to "C:\", thus reports it as existent. |
// So what happens is we append it with trailing slash and redirect it to |
// FileDirJob where it is resolved as invalid. |
- if (!exists) { |
- rv = ERR_FILE_NOT_FOUND; |
+ if (!(*exists)) { |
+ DidOpen(ERR_FILE_NOT_FOUND); |
eroman
2012/07/10 23:50:56
[optional] Rather than an if/else chain, I like to
|
} else if (!is_directory_) { |
- // URL requests should not block on the disk! |
- // http://code.google.com/p/chromium/issues/detail?id=59849 |
- base::ThreadRestrictions::ScopedAllowIO allow_io; |
- |
int flags = base::PLATFORM_FILE_OPEN | |
base::PLATFORM_FILE_READ | |
base::PLATFORM_FILE_ASYNC; |
- rv = stream_.OpenSync(file_path_, flags); |
+ int rv = stream_.Open(file_path_, flags, |
+ base::Bind(&URLRequestFileJob::DidOpen, |
+ weak_ptr_factory_.GetWeakPtr())); |
+ if (rv != ERR_IO_PENDING) |
+ DidOpen(rv); |
+ } else { |
+ DidOpen(OK); |
} |
+} |
- if (rv != OK) { |
- NotifyDone(URLRequestStatus(URLRequestStatus::FAILED, rv)); |
+void URLRequestFileJob::DidOpen(int result) { |
+ if (result != OK) { |
+ NotifyDone(URLRequestStatus(URLRequestStatus::FAILED, result)); |
return; |
} |
- if (!byte_range_.ComputeBounds(file_info.size)) { |
+ if (!byte_range_.ComputeBounds(file_size_)) { |
NotifyDone(URLRequestStatus(URLRequestStatus::FAILED, |
ERR_REQUEST_RANGE_NOT_SATISFIABLE)); |
return; |
@@ -313,21 +282,24 @@ |
byte_range_.first_byte_position() + 1; |
DCHECK_GE(remaining_bytes_, 0); |
- // URL requests should not block on the disk! |
- // http://code.google.com/p/chromium/issues/detail?id=59849 |
- { |
- base::ThreadRestrictions::ScopedAllowIO allow_io; |
- // Do the seek at the beginning of the request. |
- if (remaining_bytes_ > 0 && |
- byte_range_.first_byte_position() != 0 && |
- byte_range_.first_byte_position() != |
- stream_.SeekSync(FROM_BEGIN, byte_range_.first_byte_position())) { |
- NotifyDone(URLRequestStatus(URLRequestStatus::FAILED, |
- ERR_REQUEST_RANGE_NOT_SATISFIABLE)); |
- return; |
- } |
+ if (remaining_bytes_ > 0 && byte_range_.first_byte_position() != 0) { |
+ int rv = stream_.Seek(FROM_BEGIN, byte_range_.first_byte_position(), |
+ base::Bind(&URLRequestFileJob::DidSeek, |
+ weak_ptr_factory_.GetWeakPtr())); |
+ if (rv != ERR_IO_PENDING) |
+ DidSeek(-1); |
+ } else { |
+ DidSeek(byte_range_.first_byte_position()); |
} |
+} |
+void URLRequestFileJob::DidSeek(int64 result) { |
+ if (result != byte_range_.first_byte_position()) { |
+ NotifyDone(URLRequestStatus(URLRequestStatus::FAILED, |
+ ERR_REQUEST_RANGE_NOT_SATISFIABLE)); |
+ return; |
+ } |
+ |
set_expected_content_size(remaining_bytes_); |
NotifyHeadersComplete(); |
} |