Index: base/file_util_proxy.cc |
=================================================================== |
--- base/file_util_proxy.cc (revision 56752) |
+++ base/file_util_proxy.cc (working copy) |
@@ -2,20 +2,58 @@ |
// Use of this source code is governed by a BSD-style license that can be |
// found in the LICENSE file. |
-#include "chrome/browser/file_system_proxy.h" |
+#include "base/file_util_proxy.h" |
#include "base/file_util.h" |
-#include "chrome/browser/chrome_thread_relay.h" |
+#include "base/message_loop_proxy.h" |
namespace { |
-class RelayCreateOrOpen : public ChromeThreadRelay { |
+class MessageLoopRelay |
+ : public base::RefCountedThreadSafe<MessageLoopRelay> { |
public: |
+ MessageLoopRelay() |
+ : origin_message_loop_proxy_( |
+ base::MessageLoopProxy::CreateForCurrentThread()) { |
+ } |
+ |
+ void Start(scoped_refptr<base::MessageLoopProxy> message_loop_proxy, |
+ const tracked_objects::Location& from_here) { |
+ message_loop_proxy->PostTask( |
+ from_here, |
+ NewRunnableMethod(this, &MessageLoopRelay::ProcessOnTargetThread)); |
+ } |
+ |
+ protected: |
+ friend class base::RefCountedThreadSafe<MessageLoopRelay>; |
+ virtual ~MessageLoopRelay() {} |
+ |
+ // Called to perform work on the FILE thread. |
+ virtual void RunWork() = 0; |
+ |
+ // Called to notify the callback on the origin thread. |
+ virtual void RunCallback() = 0; |
+ |
+ private: |
+ void ProcessOnTargetThread() { |
+ RunWork(); |
+ origin_message_loop_proxy_->PostTask( |
+ FROM_HERE, |
+ NewRunnableMethod(this, &MessageLoopRelay::RunCallback)); |
+ } |
+ |
+ scoped_refptr<base::MessageLoopProxy> origin_message_loop_proxy_; |
+}; |
+ |
+class RelayCreateOrOpen : public MessageLoopRelay { |
+ public: |
RelayCreateOrOpen( |
+ scoped_refptr<base::MessageLoopProxy> message_loop_proxy, |
const FilePath& file_path, |
int file_flags, |
- FileSystemProxy::CreateOrOpenCallback* callback) |
- : file_path_(file_path), |
+ base::FileUtilProxy::CreateOrOpenCallback* callback) |
+ : message_loop_proxy_(message_loop_proxy), |
+ file_path_(file_path), |
file_flags_(file_flags), |
callback_(callback), |
file_handle_(base::kInvalidPlatformFileValue), |
@@ -26,7 +64,7 @@ |
protected: |
virtual ~RelayCreateOrOpen() { |
if (file_handle_ != base::kInvalidPlatformFileValue) |
- FileSystemProxy::Close(file_handle_, NULL); |
+ base::FileUtilProxy::Close(message_loop_proxy_, file_handle_, NULL); |
} |
virtual void RunWork() { |
@@ -39,18 +77,21 @@ |
} |
private: |
+ scoped_refptr<base::MessageLoopProxy> message_loop_proxy_; |
FilePath file_path_; |
int file_flags_; |
- FileSystemProxy::CreateOrOpenCallback* callback_; |
+ base::FileUtilProxy::CreateOrOpenCallback* callback_; |
base::PlatformFile file_handle_; |
bool created_; |
}; |
-class RelayCreateTemporary : public ChromeThreadRelay { |
+class RelayCreateTemporary : public MessageLoopRelay { |
public: |
- explicit RelayCreateTemporary( |
- FileSystemProxy::CreateTemporaryCallback* callback) |
- : callback_(callback), |
+ RelayCreateTemporary( |
+ scoped_refptr<base::MessageLoopProxy> message_loop_proxy, |
+ base::FileUtilProxy::CreateTemporaryCallback* callback) |
+ : message_loop_proxy_(message_loop_proxy), |
+ callback_(callback), |
file_handle_(base::kInvalidPlatformFileValue) { |
DCHECK(callback); |
} |
@@ -58,7 +99,7 @@ |
protected: |
virtual ~RelayCreateTemporary() { |
if (file_handle_ != base::kInvalidPlatformFileValue) |
- FileSystemProxy::Close(file_handle_, NULL); |
+ base::FileUtilProxy::Close(message_loop_proxy_, file_handle_, NULL); |
} |
virtual void RunWork() { |
@@ -82,14 +123,16 @@ |
} |
private: |
- FileSystemProxy::CreateTemporaryCallback* callback_; |
+ scoped_refptr<base::MessageLoopProxy> message_loop_proxy_; |
+ base::FileUtilProxy::CreateTemporaryCallback* callback_; |
base::PlatformFile file_handle_; |
FilePath file_path_; |
}; |
-class RelayWithStatusCallback : public ChromeThreadRelay { |
+class RelayWithStatusCallback : public MessageLoopRelay { |
public: |
- explicit RelayWithStatusCallback(FileSystemProxy::StatusCallback* callback) |
+ explicit RelayWithStatusCallback( |
+ base::FileUtilProxy::StatusCallback* callback) |
: callback_(callback), |
succeeded_(false) { |
// It is OK for callback to be NULL. |
@@ -107,14 +150,14 @@ |
void SetStatus(bool succeeded) { succeeded_ = succeeded; } |
private: |
- FileSystemProxy::StatusCallback* callback_; |
+ base::FileUtilProxy::StatusCallback* callback_; |
bool succeeded_; |
}; |
class RelayClose : public RelayWithStatusCallback { |
public: |
RelayClose(base::PlatformFile file_handle, |
- FileSystemProxy::StatusCallback* callback) |
+ base::FileUtilProxy::StatusCallback* callback) |
: RelayWithStatusCallback(callback), |
file_handle_(file_handle) { |
} |
@@ -132,7 +175,7 @@ |
public: |
RelayDelete(const FilePath& file_path, |
bool recursive, |
- FileSystemProxy::StatusCallback* callback) |
+ base::FileUtilProxy::StatusCallback* callback) |
: RelayWithStatusCallback(callback), |
file_path_(file_path), |
recursive_(recursive) { |
@@ -149,32 +192,54 @@ |
}; |
void Start(const tracked_objects::Location& from_here, |
- scoped_refptr<ChromeThreadRelay> relay) { |
- relay->Start(ChromeThread::FILE, from_here); |
+ scoped_refptr<base::MessageLoopProxy> message_loop_proxy, |
+ scoped_refptr<MessageLoopRelay> relay) { |
+ relay->Start(message_loop_proxy, from_here); |
} |
} // namespace |
-void FileSystemProxy::CreateOrOpen(const FilePath& file_path, int file_flags, |
- CreateOrOpenCallback* callback) { |
- Start(FROM_HERE, new RelayCreateOrOpen(file_path, file_flags, callback)); |
+namespace base { |
+ |
+// static |
+void FileUtilProxy::CreateOrOpen( |
+ scoped_refptr<MessageLoopProxy> message_loop_proxy, |
+ const FilePath& file_path, int file_flags, |
+ CreateOrOpenCallback* callback) { |
+ Start(FROM_HERE, message_loop_proxy, new RelayCreateOrOpen( |
+ message_loop_proxy, file_path, file_flags, callback)); |
} |
-void FileSystemProxy::CreateTemporary(CreateTemporaryCallback* callback) { |
- Start(FROM_HERE, new RelayCreateTemporary(callback)); |
+// static |
+void FileUtilProxy::CreateTemporary( |
+ scoped_refptr<MessageLoopProxy> message_loop_proxy, |
+ CreateTemporaryCallback* callback) { |
+ Start(FROM_HERE, message_loop_proxy, |
+ new RelayCreateTemporary(message_loop_proxy, callback)); |
} |
-void FileSystemProxy::Close(base::PlatformFile file_handle, |
+// static |
+void FileUtilProxy::Close(scoped_refptr<MessageLoopProxy> message_loop_proxy, |
+ base::PlatformFile file_handle, |
StatusCallback* callback) { |
- Start(FROM_HERE, new RelayClose(file_handle, callback)); |
+ Start(FROM_HERE, message_loop_proxy, new RelayClose(file_handle, callback)); |
} |
-void FileSystemProxy::Delete(const FilePath& file_path, |
+// static |
+void FileUtilProxy::Delete(scoped_refptr<MessageLoopProxy> message_loop_proxy, |
+ const FilePath& file_path, |
StatusCallback* callback) { |
- Start(FROM_HERE, new RelayDelete(file_path, false, callback)); |
+ Start(FROM_HERE, message_loop_proxy, |
+ new RelayDelete(file_path, false, callback)); |
} |
-void FileSystemProxy::RecursiveDelete(const FilePath& file_path, |
- StatusCallback* callback) { |
- Start(FROM_HERE, new RelayDelete(file_path, true, callback)); |
+// static |
+void FileUtilProxy::RecursiveDelete( |
+ scoped_refptr<MessageLoopProxy> message_loop_proxy, |
+ const FilePath& file_path, |
+ StatusCallback* callback) { |
+ Start(FROM_HERE, message_loop_proxy, |
+ new RelayDelete(file_path, true, callback)); |
} |
+ |
+} // namespace base |