diff options
author | joi@chromium.org <joi@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2011-11-08 12:47:58 +0000 |
---|---|---|
committer | joi@chromium.org <joi@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2011-11-08 12:47:58 +0000 |
commit | f3bed0141f547f49d05ea8973b88cf3511f48dcd (patch) | |
tree | 8c84089dc3abfedb8102b1f6eda60faabcf755d2 /webkit/fileapi | |
parent | f6779a644fd46a4574869610b8a7ac5a38dfc7c2 (diff) | |
download | chromium_src-f3bed0141f547f49d05ea8973b88cf3511f48dcd.zip chromium_src-f3bed0141f547f49d05ea8973b88cf3511f48dcd.tar.gz chromium_src-f3bed0141f547f49d05ea8973b88cf3511f48dcd.tar.bz2 |
Revert 109010 - Merge FileUtilProxy and FileSystemFileUtilProxy using PostTaskAndReply: Delete/Touch/Truncate/Copy/Move
BUG=none
TEST=test_shell_tests:FileSystem*
Review URL: http://codereview.chromium.org/8424007
TBR=kinuko@chromium.org
Review URL: http://codereview.chromium.org/8497012
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@109015 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'webkit/fileapi')
-rw-r--r-- | webkit/fileapi/file_system_file_util_proxy.cc | 270 | ||||
-rw-r--r-- | webkit/fileapi/file_system_file_util_proxy.h | 72 | ||||
-rw-r--r-- | webkit/fileapi/file_system_operation.cc | 183 | ||||
-rw-r--r-- | webkit/fileapi/file_system_operation.h | 29 |
4 files changed, 460 insertions, 94 deletions
diff --git a/webkit/fileapi/file_system_file_util_proxy.cc b/webkit/fileapi/file_system_file_util_proxy.cc index 9569d41..6c54cdc 100644 --- a/webkit/fileapi/file_system_file_util_proxy.cc +++ b/webkit/fileapi/file_system_file_util_proxy.cc @@ -71,6 +71,27 @@ class MessageLoopRelay fileapi::FileSystemFileUtil* file_util_; }; +class RelayWithStatusCallback : public MessageLoopRelay { + public: + RelayWithStatusCallback( + const fileapi::FileSystemOperationContext& context, + const fileapi::FileSystemFileUtilProxy::StatusCallback& callback) + : MessageLoopRelay(context), + callback_(callback) { + // It is OK for callback to be NULL. + } + + protected: + virtual void RunCallback() { + // The caller may not have been interested in the result. + if (!callback_.is_null()) + callback_.Run(error_code()); + } + + private: + fileapi::FileSystemFileUtilProxy::StatusCallback callback_; +}; + class RelayEnsureFileExists : public MessageLoopRelay { public: RelayEnsureFileExists( @@ -105,6 +126,35 @@ class RelayEnsureFileExists : public MessageLoopRelay { }; +class RelayGetLocalPath : public MessageLoopRelay { + public: + RelayGetLocalPath( + const fileapi::FileSystemOperationContext& context, + const FilePath& virtual_path, + fileapi::FileSystemFileUtilProxy::GetLocalPathCallback* callback) + : MessageLoopRelay(context), + callback_(callback), + virtual_path_(virtual_path) { + DCHECK(callback); + } + + protected: + virtual void RunWork() { + set_error_code(file_util()->GetLocalFilePath( + context(), virtual_path_, &local_path_)); + } + + virtual void RunCallback() { + callback_->Run(error_code(), local_path_); + delete callback_; + } + + private: + fileapi::FileSystemFileUtilProxy::GetLocalPathCallback* callback_; + FilePath virtual_path_; + FilePath local_path_; +}; + class RelayGetFileInfo : public MessageLoopRelay { public: RelayGetFileInfo( @@ -162,6 +212,145 @@ class RelayReadDirectory : public MessageLoopRelay { std::vector<base::FileUtilProxy::Entry> entries_; }; +class RelayCreateDirectory : public RelayWithStatusCallback { + public: + RelayCreateDirectory( + const fileapi::FileSystemOperationContext& context, + const FilePath& file_path, + bool exclusive, + bool recursive, + const fileapi::FileSystemFileUtilProxy::StatusCallback& callback) + : RelayWithStatusCallback(context, callback), + file_path_(file_path), + exclusive_(exclusive), + recursive_(recursive) { + } + + protected: + virtual void RunWork() { + set_error_code(file_util()->CreateDirectory( + context(), file_path_, exclusive_, recursive_)); + } + + private: + FilePath file_path_; + bool exclusive_; + bool recursive_; +}; + +class RelayCopy : public RelayWithStatusCallback { + public: + RelayCopy(const fileapi::FileSystemOperationContext& context, + const FilePath& src_file_path, + const FilePath& dest_file_path, + const fileapi::FileSystemFileUtilProxy::StatusCallback& callback) + : RelayWithStatusCallback(context, callback), + src_file_path_(src_file_path), + dest_file_path_(dest_file_path) { + } + + protected: + virtual void RunWork() { + set_error_code(file_util()->Copy( + context(), src_file_path_, dest_file_path_)); + } + + private: + FilePath src_file_path_; + FilePath dest_file_path_; +}; + +class RelayMove : public RelayWithStatusCallback { + public: + RelayMove(const fileapi::FileSystemOperationContext& context, + const FilePath& src_file_path, + const FilePath& dest_file_path, + const fileapi::FileSystemFileUtilProxy::StatusCallback& callback) + : RelayWithStatusCallback(context, callback), + src_file_path_(src_file_path), + dest_file_path_(dest_file_path) { + } + + protected: + virtual void RunWork() { + set_error_code(file_util()->Move( + context(), src_file_path_, dest_file_path_)); + } + + private: + FilePath src_file_path_; + FilePath dest_file_path_; +}; + +class RelayDelete : public RelayWithStatusCallback { + public: + RelayDelete(const fileapi::FileSystemOperationContext& context, + const FilePath& file_path, + bool recursive, + const fileapi::FileSystemFileUtilProxy::StatusCallback& callback) + : RelayWithStatusCallback(context, callback), + file_path_(file_path), + recursive_(recursive) { + } + + protected: + virtual void RunWork() { + set_error_code(file_util()->Delete(context(), file_path_, recursive_)); + } + + private: + FilePath file_path_; + bool recursive_; +}; + +class RelayTouchFilePath : public RelayWithStatusCallback { + public: + RelayTouchFilePath( + const fileapi::FileSystemOperationContext& context, + const FilePath& file_path, + const base::Time& last_access_time, + const base::Time& last_modified_time, + const fileapi::FileSystemFileUtilProxy::StatusCallback& callback) + : RelayWithStatusCallback(context, callback), + file_path_(file_path), + last_access_time_(last_access_time), + last_modified_time_(last_modified_time) { + } + + protected: + virtual void RunWork() { + set_error_code(file_util()->Touch( + context(), file_path_, last_access_time_, last_modified_time_)); + } + + private: + FilePath file_path_; + base::Time last_access_time_; + base::Time last_modified_time_; +}; + +class RelayTruncate : public RelayWithStatusCallback { + public: + RelayTruncate( + const fileapi::FileSystemOperationContext& context, + const FilePath& file_path, + int64 length, + const fileapi::FileSystemFileUtilProxy::StatusCallback& callback) + : RelayWithStatusCallback(context, callback), + file_path_(file_path), + length_(length) { + } + + protected: + virtual void RunWork() { + set_error_code(file_util()->Truncate(context(), file_path_, length_)); + } + + private: + FilePath file_path_; + int64 length_; +}; + bool Start(const tracked_objects::Location& from_here, scoped_refptr<base::MessageLoopProxy> message_loop_proxy, scoped_refptr<MessageLoopRelay> relay) { @@ -183,6 +372,16 @@ bool FileSystemFileUtilProxy::EnsureFileExists( } // static +bool FileSystemFileUtilProxy::GetLocalPath( + const FileSystemOperationContext& context, + scoped_refptr<MessageLoopProxy> message_loop_proxy, + const FilePath& virtual_path, + GetLocalPathCallback* callback) { + return Start(FROM_HERE, message_loop_proxy, + new RelayGetLocalPath(context, virtual_path, callback)); +} + +// static bool FileSystemFileUtilProxy::GetFileInfo( const FileSystemOperationContext& context, scoped_refptr<MessageLoopProxy> message_loop_proxy, @@ -202,4 +401,75 @@ bool FileSystemFileUtilProxy::ReadDirectory( file_path, callback)); } +// static +bool FileSystemFileUtilProxy::CreateDirectory( + const FileSystemOperationContext& context, + scoped_refptr<MessageLoopProxy> message_loop_proxy, + const FilePath& file_path, + bool exclusive, + bool recursive, + const StatusCallback& callback) { + return Start(FROM_HERE, message_loop_proxy, new RelayCreateDirectory( + context, file_path, exclusive, recursive, callback)); +} + +// static +bool FileSystemFileUtilProxy::Copy( + const FileSystemOperationContext& context, + scoped_refptr<MessageLoopProxy> message_loop_proxy, + const FilePath& src_file_path, + const FilePath& dest_file_path, + const StatusCallback& callback) { + return Start(FROM_HERE, message_loop_proxy, + new RelayCopy(context, src_file_path, dest_file_path, + callback)); +} + +// static +bool FileSystemFileUtilProxy::Move( + const FileSystemOperationContext& context, + scoped_refptr<MessageLoopProxy> message_loop_proxy, + const FilePath& src_file_path, + const FilePath& dest_file_path, + const StatusCallback& callback) { + return Start(FROM_HERE, message_loop_proxy, + new RelayMove(context, src_file_path, dest_file_path, + callback)); +} + +// static +bool FileSystemFileUtilProxy::Delete( + const FileSystemOperationContext& context, + scoped_refptr<MessageLoopProxy> message_loop_proxy, + const FilePath& file_path, + bool recursive, + const StatusCallback& callback) { + return Start(FROM_HERE, message_loop_proxy, + new RelayDelete(context, file_path, recursive, callback)); +} + +// static +bool FileSystemFileUtilProxy::Touch( + const FileSystemOperationContext& context, + scoped_refptr<MessageLoopProxy> message_loop_proxy, + const FilePath& file_path, + const base::Time& last_access_time, + const base::Time& last_modified_time, + const StatusCallback& callback) { + return Start(FROM_HERE, message_loop_proxy, + new RelayTouchFilePath(context, file_path, last_access_time, + last_modified_time, callback)); +} + +// static +bool FileSystemFileUtilProxy::Truncate( + const FileSystemOperationContext& context, + scoped_refptr<MessageLoopProxy> message_loop_proxy, + const FilePath& path, + int64 length, + const StatusCallback& callback) { + return Start(FROM_HERE, message_loop_proxy, + new RelayTruncate(context, path, length, callback)); +} + } // namespace fileapi diff --git a/webkit/fileapi/file_system_file_util_proxy.h b/webkit/fileapi/file_system_file_util_proxy.h index 48ac923..b6a9ada 100644 --- a/webkit/fileapi/file_system_file_util_proxy.h +++ b/webkit/fileapi/file_system_file_util_proxy.h @@ -35,6 +35,7 @@ class FileSystemFileUtilProxy { public: typedef base::FileUtilProxy::Entry Entry; + typedef base::FileUtilProxy::StatusCallback StatusCallback; typedef base::Callback<void(PlatformFileError, bool /* created */ )> EnsureFileExistsCallback; @@ -46,6 +47,10 @@ class FileSystemFileUtilProxy { const std::vector<Entry>& )> ReadDirectoryCallback; + typedef Callback2<PlatformFileError /* error code */, + const FilePath& /* local_path, where possible */ + >::Type GetLocalPathCallback; + // Ensures that the given |file_path| exist. This creates a empty new file // at |file_path| if the |file_path| does not exist. // If a new file han not existed and is created at the |file_path|, @@ -61,6 +66,13 @@ class FileSystemFileUtilProxy { const FilePath& file_path, const EnsureFileExistsCallback& callback); + // Maps virtual file patch to its local physical location. + static bool GetLocalPath( + const FileSystemOperationContext& context, + scoped_refptr<MessageLoopProxy> message_loop_proxy, + const FilePath& virtual_path, + GetLocalPathCallback* callback); + // Retrieves the information about a file. It is invalid to pass NULL for the // callback. static bool GetFileInfo( @@ -74,6 +86,66 @@ class FileSystemFileUtilProxy { const FilePath& file_path, const ReadDirectoryCallback& callback); + // Creates directory at given path. It's an error to create + // if |exclusive| is true and dir already exists. + static bool CreateDirectory( + const FileSystemOperationContext& context, + scoped_refptr<MessageLoopProxy> message_loop_proxy, + const FilePath& file_path, + bool exclusive, + bool recursive, + const StatusCallback& callback); + + // Copies a file or a directory from |src_file_path| to |dest_file_path| + // Error cases: + // If destination file doesn't exist or destination's parent + // doesn't exists. + // If source dir exists but destination path is an existing file. + // If source file exists but destination path is an existing directory. + // If source is a parent of destination. + // If source doesn't exists. + static bool Copy(const FileSystemOperationContext& context, + scoped_refptr<MessageLoopProxy> message_loop_proxy, + const FilePath& src_file_path, + const FilePath& dest_file_path, + const StatusCallback& callback); + + // Moves a file or a directory from src_file_path to dest_file_path. + // Error cases are similar to Copy method's error cases. + static bool Move( + const FileSystemOperationContext& context, + scoped_refptr<MessageLoopProxy> message_loop_proxy, + const FilePath& src_file_path, + const FilePath& dest_file_path, + const StatusCallback& callback); + + // Deletes a file or a directory. + // It is an error to delete a non-empty directory with recursive=false. + static bool Delete(const FileSystemOperationContext& context, + scoped_refptr<MessageLoopProxy> message_loop_proxy, + const FilePath& file_path, + bool recursive, + const StatusCallback& callback); + + // Touches a file. The callback can be NULL. + static bool Touch( + const FileSystemOperationContext& context, + scoped_refptr<MessageLoopProxy> message_loop_proxy, + const FilePath& file_path, + const base::Time& last_access_time, + const base::Time& last_modified_time, + const StatusCallback& callback); + + // Truncates a file to the given length. If |length| is greater than the + // current length of the file, the file will be extended with zeroes. + // The callback can be NULL. + static bool Truncate( + const FileSystemOperationContext& context, + scoped_refptr<MessageLoopProxy> message_loop_proxy, + const FilePath& path, + int64 length, + const StatusCallback& callback); + private: DISALLOW_IMPLICIT_CONSTRUCTORS(FileSystemFileUtilProxy); }; diff --git a/webkit/fileapi/file_system_operation.cc b/webkit/fileapi/file_system_operation.cc index f329cc1..c65aee7 100644 --- a/webkit/fileapi/file_system_operation.cc +++ b/webkit/fileapi/file_system_operation.cc @@ -63,7 +63,8 @@ FileSystemOperation::FileSystemOperation( FileSystemContext* file_system_context) : proxy_(proxy), dispatcher_(dispatcher), - operation_context_(file_system_context, NULL) { + operation_context_(file_system_context, NULL), + ALLOW_THIS_IN_INITIALIZER_LIST(weak_factory_(this)) { #ifndef NDEBUG pending_operation_ = kOperationNone; #endif @@ -79,7 +80,7 @@ FileSystemOperation::~FileSystemOperation() { base::Unretained(c->src_file_util()), base::Owned(c)), file_writer_delegate_->file(), - base::FileUtilProxy::StatusCallback()); + FileSystemFileUtilProxy::StatusCallback()); } } @@ -103,7 +104,7 @@ void FileSystemOperation::OpenFileSystem( file_system_context()->path_manager()->ValidateFileSystemRootAndGetURL( origin_url, type, create, base::Bind(&FileSystemOperation::DidGetRootPath, - base::Owned(this))); + weak_factory_.GetWeakPtr())); } void FileSystemOperation::CreateFile(const GURL& path, @@ -116,14 +117,15 @@ void FileSystemOperation::CreateFile(const GURL& path, delete this; return; } + exclusive_ = exclusive; + GetUsageAndQuotaThenCallback( operation_context_.src_origin_url(), base::Bind(&FileSystemOperation::DelayedCreateFileForQuota, - base::Unretained(this), exclusive)); + weak_factory_.GetWeakPtr())); } void FileSystemOperation::DelayedCreateFileForQuota( - bool exclusive, quota::QuotaStatusCode status, int64 usage, int64 quota) { operation_context_.set_allowed_bytes_growth(quota - usage); @@ -137,9 +139,9 @@ void FileSystemOperation::DelayedCreateFileForQuota( proxy_, src_virtual_path_, base::Bind( - exclusive ? &FileSystemOperation::DidEnsureFileExistsExclusive - : &FileSystemOperation::DidEnsureFileExistsNonExclusive, - base::Owned(this))); + exclusive_ ? &FileSystemOperation::DidEnsureFileExistsExclusive + : &FileSystemOperation::DidEnsureFileExistsNonExclusive, + weak_factory_.GetWeakPtr())); } void FileSystemOperation::CreateDirectory(const GURL& path, @@ -153,14 +155,16 @@ void FileSystemOperation::CreateDirectory(const GURL& path, delete this; return; } + exclusive_ = exclusive; + recursive_ = recursive; + GetUsageAndQuotaThenCallback( operation_context_.src_origin_url(), base::Bind(&FileSystemOperation::DelayedCreateDirectoryForQuota, - base::Unretained(this), exclusive, recursive)); + weak_factory_.GetWeakPtr())); } void FileSystemOperation::DelayedCreateDirectoryForQuota( - bool exclusive, bool recursive, quota::QuotaStatusCode status, int64 usage, int64 quota) { operation_context_.set_allowed_bytes_growth(quota - usage); @@ -169,14 +173,11 @@ void FileSystemOperation::DelayedCreateDirectoryForQuota( operation_context_.src_origin_url(), operation_context_.src_type())); - base::FileUtilProxy::RelayFileTask( - proxy_, FROM_HERE, - base::Bind(&FileSystemFileUtil::CreateDirectory, - base::Unretained(operation_context_.src_file_util()), - &operation_context_, - src_virtual_path_, exclusive, recursive), + FileSystemFileUtilProxy::CreateDirectory( + operation_context_, proxy_, src_virtual_path_, exclusive_, + recursive_, base::Bind(&FileSystemOperation::DidFinishFileOperation, - base::Owned(this))); + weak_factory_.GetWeakPtr())); } void FileSystemOperation::Copy(const GURL& src_path, @@ -194,7 +195,7 @@ void FileSystemOperation::Copy(const GURL& src_path, GetUsageAndQuotaThenCallback( operation_context_.dest_origin_url(), base::Bind(&FileSystemOperation::DelayedCopyForQuota, - base::Unretained(this))); + weak_factory_.GetWeakPtr())); } void FileSystemOperation::DelayedCopyForQuota(quota::QuotaStatusCode status, @@ -206,14 +207,11 @@ void FileSystemOperation::DelayedCopyForQuota(quota::QuotaStatusCode status, operation_context_.dest_origin_url(), operation_context_.dest_type())); - base::FileUtilProxy::RelayFileTask( - proxy_, FROM_HERE, - base::Bind(&FileSystemFileUtil::Copy, - base::Unretained(operation_context_.src_file_util()), - &operation_context_, - src_virtual_path_, dest_virtual_path_), + FileSystemFileUtilProxy::Copy( + operation_context_, proxy_, src_virtual_path_, + dest_virtual_path_, base::Bind(&FileSystemOperation::DidFinishFileOperation, - base::Owned(this))); + weak_factory_.GetWeakPtr())); } void FileSystemOperation::Move(const GURL& src_path, @@ -231,7 +229,7 @@ void FileSystemOperation::Move(const GURL& src_path, GetUsageAndQuotaThenCallback( operation_context_.dest_origin_url(), base::Bind(&FileSystemOperation::DelayedMoveForQuota, - base::Unretained(this))); + weak_factory_.GetWeakPtr())); } void FileSystemOperation::DelayedMoveForQuota(quota::QuotaStatusCode status, @@ -243,14 +241,11 @@ void FileSystemOperation::DelayedMoveForQuota(quota::QuotaStatusCode status, operation_context_.dest_origin_url(), operation_context_.dest_type())); - base::FileUtilProxy::RelayFileTask( - proxy_, FROM_HERE, - base::Bind(&FileSystemFileUtil::Move, - base::Unretained(operation_context_.src_file_util()), - &operation_context_, - src_virtual_path_, dest_virtual_path_), + FileSystemFileUtilProxy::Move( + operation_context_, proxy_, src_virtual_path_, + dest_virtual_path_, base::Bind(&FileSystemOperation::DidFinishFileOperation, - base::Owned(this))); + weak_factory_.GetWeakPtr())); } void FileSystemOperation::DirectoryExists(const GURL& path) { @@ -265,7 +260,8 @@ void FileSystemOperation::DirectoryExists(const GURL& path) { FileSystemFileUtilProxy::GetFileInfo( operation_context_, proxy_, src_virtual_path_, - base::Bind(&FileSystemOperation::DidDirectoryExists, base::Owned(this))); + base::Bind(&FileSystemOperation::DidDirectoryExists, + weak_factory_.GetWeakPtr())); } void FileSystemOperation::FileExists(const GURL& path) { @@ -280,7 +276,8 @@ void FileSystemOperation::FileExists(const GURL& path) { FileSystemFileUtilProxy::GetFileInfo( operation_context_, proxy_, src_virtual_path_, - base::Bind(&FileSystemOperation::DidFileExists, base::Owned(this))); + base::Bind(&FileSystemOperation::DidFileExists, + weak_factory_.GetWeakPtr())); } void FileSystemOperation::GetMetadata(const GURL& path) { @@ -295,7 +292,8 @@ void FileSystemOperation::GetMetadata(const GURL& path) { FileSystemFileUtilProxy::GetFileInfo( operation_context_, proxy_, src_virtual_path_, - base::Bind(&FileSystemOperation::DidGetMetadata, base::Owned(this))); + base::Bind(&FileSystemOperation::DidGetMetadata, + weak_factory_.GetWeakPtr())); } void FileSystemOperation::ReadDirectory(const GURL& path) { @@ -310,7 +308,8 @@ void FileSystemOperation::ReadDirectory(const GURL& path) { FileSystemFileUtilProxy::ReadDirectory( operation_context_, proxy_, src_virtual_path_, - base::Bind(&FileSystemOperation::DidReadDirectory, base::Owned(this))); + base::Bind(&FileSystemOperation::DidReadDirectory, + weak_factory_.GetWeakPtr())); } void FileSystemOperation::Remove(const GURL& path, bool recursive) { @@ -323,14 +322,10 @@ void FileSystemOperation::Remove(const GURL& path, bool recursive) { return; } - base::FileUtilProxy::RelayFileTask( - proxy_, FROM_HERE, - base::Bind(&FileSystemFileUtil::Delete, - base::Unretained(operation_context_.src_file_util()), - &operation_context_, - src_virtual_path_, recursive), + FileSystemFileUtilProxy::Delete( + operation_context_, proxy_, src_virtual_path_, recursive, base::Bind(&FileSystemOperation::DidFinishFileOperation, - base::Owned(this))); + weak_factory_.GetWeakPtr())); } void FileSystemOperation::Write( @@ -355,7 +350,7 @@ void FileSystemOperation::Write( GetUsageAndQuotaThenCallback( operation_context_.src_origin_url(), base::Bind(&FileSystemOperation::DelayedWriteForQuota, - base::Unretained(this))); + weak_factory_.GetWeakPtr())); } void FileSystemOperation::DelayedWriteForQuota(quota::QuotaStatusCode status, @@ -375,13 +370,13 @@ void FileSystemOperation::DelayedWriteForQuota(quota::QuotaStatusCode status, proxy_, base::Bind(&FileSystemFileUtil::CreateOrOpen, base::Unretained(operation_context_.src_file_util()), - &operation_context_, + base::Unretained(&operation_context_), src_virtual_path_, file_flags), base::Bind(&FileSystemFileUtil::Close, base::Unretained(operation_context_.src_file_util()), - &operation_context_), + base::Unretained(&operation_context_)), base::Bind(&FileSystemOperation::OnFileOpenedForWrite, - base::Unretained(this))); + weak_factory_.GetWeakPtr())); } void FileSystemOperation::Truncate(const GURL& path, int64 length) { @@ -393,14 +388,15 @@ void FileSystemOperation::Truncate(const GURL& path, int64 length) { delete this; return; } + length_ = length; + GetUsageAndQuotaThenCallback( operation_context_.src_origin_url(), base::Bind(&FileSystemOperation::DelayedTruncateForQuota, - base::Unretained(this), length)); + weak_factory_.GetWeakPtr())); } -void FileSystemOperation::DelayedTruncateForQuota(int64 length, - quota::QuotaStatusCode status, +void FileSystemOperation::DelayedTruncateForQuota(quota::QuotaStatusCode status, int64 usage, int64 quota) { operation_context_.set_allowed_bytes_growth(quota - usage); @@ -409,14 +405,10 @@ void FileSystemOperation::DelayedTruncateForQuota(int64 length, operation_context_.src_origin_url(), operation_context_.src_type())); - base::FileUtilProxy::RelayFileTask( - proxy_, FROM_HERE, - base::Bind(&FileSystemFileUtil::Truncate, - base::Unretained(operation_context_.src_file_util()), - &operation_context_, - src_virtual_path_, length), + FileSystemFileUtilProxy::Truncate( + operation_context_, proxy_, src_virtual_path_, length_, base::Bind(&FileSystemOperation::DidFinishFileOperation, - base::Owned(this))); + weak_factory_.GetWeakPtr())); } void FileSystemOperation::TouchFile(const GURL& path, @@ -431,13 +423,11 @@ void FileSystemOperation::TouchFile(const GURL& path, return; } - base::FileUtilProxy::RelayFileTask( - proxy_, FROM_HERE, - base::Bind(&FileSystemFileUtil::Touch, - base::Unretained(operation_context_.src_file_util()), - &operation_context_, - src_virtual_path_, last_access_time, last_modified_time), - base::Bind(&FileSystemOperation::DidTouchFile, base::Owned(this))); + FileSystemFileUtilProxy::Touch( + operation_context_, proxy_, src_virtual_path_, + last_access_time, last_modified_time, + base::Bind(&FileSystemOperation::DidTouchFile, + weak_factory_.GetWeakPtr())); } void FileSystemOperation::OpenFile(const GURL& path, @@ -471,14 +461,15 @@ void FileSystemOperation::OpenFile(const GURL& path, return; } } + file_flags_ = file_flags; + GetUsageAndQuotaThenCallback( operation_context_.src_origin_url(), base::Bind(&FileSystemOperation::DelayedOpenFileForQuota, - base::Unretained(this), file_flags)); + weak_factory_.GetWeakPtr())); } -void FileSystemOperation::DelayedOpenFileForQuota(int file_flags, - quota::QuotaStatusCode status, +void FileSystemOperation::DelayedOpenFileForQuota(quota::QuotaStatusCode status, int64 usage, int64 quota) { operation_context_.set_allowed_bytes_growth(quota - usage); @@ -491,12 +482,13 @@ void FileSystemOperation::DelayedOpenFileForQuota(int file_flags, proxy_, base::Bind(&FileSystemFileUtil::CreateOrOpen, base::Unretained(operation_context_.src_file_util()), - &operation_context_, - src_virtual_path_, file_flags), + base::Unretained(&operation_context_), + src_virtual_path_, file_flags_), base::Bind(&FileSystemFileUtil::Close, base::Unretained(operation_context_.src_file_util()), - &operation_context_), - base::Bind(&FileSystemOperation::DidOpenFile, base::Owned(this))); + base::Unretained(&operation_context_)), + base::Bind(&FileSystemOperation::DidOpenFile, + weak_factory_.GetWeakPtr())); } void FileSystemOperation::SyncGetPlatformPath(const GURL& path, @@ -577,8 +569,6 @@ void FileSystemOperation::DidGetRootPath( const FilePath& path, const std::string& name) { DCHECK(success || path.empty()); GURL result; - if (!dispatcher_.get()) - return; // We ignore the path, and return a URL instead. The point was just to verify // that we could create/find the path. if (success) { @@ -586,7 +576,9 @@ void FileSystemOperation::DidGetRootPath( operation_context_.src_origin_url(), operation_context_.src_type()); } - dispatcher_->DidOpenFileSystem(name, result); + if (dispatcher_.get()) + dispatcher_->DidOpenFileSystem(name, result); + delete this; } void FileSystemOperation::DidEnsureFileExistsExclusive( @@ -594,6 +586,7 @@ void FileSystemOperation::DidEnsureFileExistsExclusive( if (rv == base::PLATFORM_FILE_OK && !created) { if (dispatcher_.get()) dispatcher_->DidFail(base::PLATFORM_FILE_ERROR_EXISTS); + delete this; } else { DidFinishFileOperation(rv); } @@ -620,14 +613,17 @@ void FileSystemOperation::DidFinishFileOperation( else dispatcher_->DidFail(rv); } + delete this; } void FileSystemOperation::DidDirectoryExists( base::PlatformFileError rv, const base::PlatformFileInfo& file_info, const FilePath& unused) { - if (!dispatcher_.get()) + if (!dispatcher_.get()) { + delete this; return; + } if (rv == base::PLATFORM_FILE_OK) { if (file_info.is_directory) dispatcher_->DidSucceed(); @@ -636,14 +632,17 @@ void FileSystemOperation::DidDirectoryExists( } else { dispatcher_->DidFail(rv); } + delete this; } void FileSystemOperation::DidFileExists( base::PlatformFileError rv, const base::PlatformFileInfo& file_info, const FilePath& unused) { - if (!dispatcher_.get()) + if (!dispatcher_.get()) { + delete this; return; + } if (rv == base::PLATFORM_FILE_OK) { if (file_info.is_directory) dispatcher_->DidFail(base::PLATFORM_FILE_ERROR_NOT_A_FILE); @@ -652,30 +651,37 @@ void FileSystemOperation::DidFileExists( } else { dispatcher_->DidFail(rv); } + delete this; } void FileSystemOperation::DidGetMetadata( base::PlatformFileError rv, const base::PlatformFileInfo& file_info, const FilePath& platform_path) { - if (!dispatcher_.get()) + if (!dispatcher_.get()) { + delete this; return; + } if (rv == base::PLATFORM_FILE_OK) dispatcher_->DidReadMetadata(file_info, platform_path); else dispatcher_->DidFail(rv); + delete this; } void FileSystemOperation::DidReadDirectory( base::PlatformFileError rv, const std::vector<base::FileUtilProxy::Entry>& entries) { - if (!dispatcher_.get()) + if (!dispatcher_.get()) { + delete this; return; + } if (rv == base::PLATFORM_FILE_OK) dispatcher_->DidReadDirectory(entries, false /* has_more */); else dispatcher_->DidFail(rv); + delete this; } void FileSystemOperation::DidWrite( @@ -695,33 +701,42 @@ void FileSystemOperation::DidWrite( } void FileSystemOperation::DidTouchFile(base::PlatformFileError rv) { - if (!dispatcher_.get()) + if (!dispatcher_.get()) { + delete this; return; + } if (rv == base::PLATFORM_FILE_OK) dispatcher_->DidSucceed(); else dispatcher_->DidFail(rv); + delete this; } void FileSystemOperation::DidOpenFile( base::PlatformFileError rv, base::PassPlatformFile file, bool unused) { - if (!dispatcher_.get()) + if (!dispatcher_.get()) { + delete this; return; + } if (rv == base::PLATFORM_FILE_OK) dispatcher_->DidOpenFile(file.ReleaseValue(), peer_handle_); else dispatcher_->DidFail(rv); + delete this; } void FileSystemOperation::OnFileOpenedForWrite( base::PlatformFileError rv, base::PassPlatformFile file, bool created) { - if (base::PLATFORM_FILE_OK != rv || !dispatcher_.get()) { - if (dispatcher_.get()) - dispatcher_->DidFail(rv); + if (!dispatcher_.get()) { + delete this; + return; + } + if (base::PLATFORM_FILE_OK != rv) { + dispatcher_->DidFail(rv); delete this; return; } diff --git a/webkit/fileapi/file_system_operation.h b/webkit/fileapi/file_system_operation.h index fdf26fc..0846ca6 100644 --- a/webkit/fileapi/file_system_operation.h +++ b/webkit/fileapi/file_system_operation.h @@ -13,6 +13,7 @@ #include "base/memory/ref_counted.h" #include "base/memory/scoped_callback_factory.h" #include "base/memory/scoped_ptr.h" +#include "base/memory/weak_ptr.h" #include "base/message_loop_proxy.h" #include "base/platform_file.h" #include "base/process.h" @@ -44,8 +45,8 @@ class FileSystemQuotaUtil; // Only one method(CreateFile, CreateDirectory, Copy, Move, DirectoryExists, // GetMetadata, ReadDirectory and Remove) may be called during the lifetime of // this object and it should be called no more than once. -// This class is self-destructed, or get deleted via base::Owned() fater the -// operation finishes and completion callback is called. +// This class is self-destructed and an instance automatically gets deleted +// when its operation is finished. class FileSystemOperation { public: // |dispatcher| will be owned by this class. @@ -119,11 +120,9 @@ class FileSystemOperation { const GURL& origin_url, const quota::QuotaManager::GetUsageAndQuotaCallback& callback); - void DelayedCreateFileForQuota(bool exclusive, - quota::QuotaStatusCode status, + void DelayedCreateFileForQuota(quota::QuotaStatusCode status, int64 usage, int64 quota); - void DelayedCreateDirectoryForQuota(bool exclusive, bool recursive, - quota::QuotaStatusCode status, + void DelayedCreateDirectoryForQuota(quota::QuotaStatusCode status, int64 usage, int64 quota); void DelayedCopyForQuota(quota::QuotaStatusCode status, int64 usage, int64 quota); @@ -131,11 +130,9 @@ class FileSystemOperation { int64 usage, int64 quota); void DelayedWriteForQuota(quota::QuotaStatusCode status, int64 usage, int64 quota); - void DelayedTruncateForQuota(int64 length, - quota::QuotaStatusCode status, + void DelayedTruncateForQuota(quota::QuotaStatusCode status, int64 usage, int64 quota); - void DelayedOpenFileForQuota(int file_flags, - quota::QuotaStatusCode status, + void DelayedOpenFileForQuota(quota::QuotaStatusCode status, int64 usage, int64 quota); // A callback used for OpenFileSystem. @@ -259,6 +256,8 @@ class FileSystemOperation { FileSystemOperationContext operation_context_; + base::WeakPtrFactory<FileSystemOperation> weak_factory_; + scoped_ptr<ScopedQuotaUtilHelper> quota_util_helper_; // These are all used only by Write(). @@ -277,6 +276,16 @@ class FileSystemOperation { FilePath src_virtual_path_; FilePath dest_virtual_path_; + // Options for CreateFile and CreateDirectory. + bool exclusive_; + bool recursive_; + + // Options for OpenFile. + int file_flags_; + + // Length to be truncated. + int64 length_; + DISALLOW_COPY_AND_ASSIGN(FileSystemOperation); }; |