Moving file_system_proxy to base/ and changing it to work with MessageLoopProxies instead of ChromeThreads.

BUG=none
TEST=none

Review URL: http://codereview.chromium.org/3131026

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@56923 0039d316-1c4b-4281-b951-d872f2087c98


CrOS-Libchrome-Original-Commit: b90c93fff90803a61d25a71a64c6bb06a73de358
diff --git a/base/base.gypi b/base/base.gypi
index 1d4fc21..e355172 100644
--- a/base/base.gypi
+++ b/base/base.gypi
@@ -81,6 +81,8 @@
           'file_util_mac.mm',
           'file_util_posix.cc',
           'file_util_win.cc',
+          'file_util_proxy.cc',
+          'file_util_proxy.h',
           'file_version_info.h',
           'file_version_info_mac.h',
           'file_version_info_mac.mm',
diff --git a/base/file_util_proxy.cc b/base/file_util_proxy.cc
new file mode 100644
index 0000000..62d150e
--- /dev/null
+++ b/base/file_util_proxy.cc
@@ -0,0 +1,245 @@
+// Copyright (c) 2010 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "base/file_util_proxy.h"
+
+#include "base/file_util.h"
+#include "base/message_loop_proxy.h"
+
+namespace {
+
+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,
+      base::FileUtilProxy::CreateOrOpenCallback* callback)
+      : message_loop_proxy_(message_loop_proxy),
+        file_path_(file_path),
+        file_flags_(file_flags),
+        callback_(callback),
+        file_handle_(base::kInvalidPlatformFileValue),
+        created_(false) {
+    DCHECK(callback);
+  }
+
+ protected:
+  virtual ~RelayCreateOrOpen() {
+    if (file_handle_ != base::kInvalidPlatformFileValue)
+      base::FileUtilProxy::Close(message_loop_proxy_, file_handle_, NULL);
+  }
+
+  virtual void RunWork() {
+    file_handle_ = base::CreatePlatformFile(file_path_, file_flags_, &created_);
+  }
+
+  virtual void RunCallback() {
+    callback_->Run(base::PassPlatformFile(&file_handle_), created_);
+    delete callback_;
+  }
+
+ private:
+  scoped_refptr<base::MessageLoopProxy> message_loop_proxy_;
+  FilePath file_path_;
+  int file_flags_;
+  base::FileUtilProxy::CreateOrOpenCallback* callback_;
+  base::PlatformFile file_handle_;
+  bool created_;
+};
+
+class RelayCreateTemporary : public MessageLoopRelay {
+ public:
+  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);
+  }
+
+ protected:
+  virtual ~RelayCreateTemporary() {
+    if (file_handle_ != base::kInvalidPlatformFileValue)
+      base::FileUtilProxy::Close(message_loop_proxy_, file_handle_, NULL);
+  }
+
+  virtual void RunWork() {
+    // TODO(darin): file_util should have a variant of CreateTemporaryFile
+    // that returns a FilePath and a PlatformFile.
+    file_util::CreateTemporaryFile(&file_path_);
+
+    // Use a fixed set of flags that are appropriate for writing to a temporary
+    // file from the IO thread using a net::FileStream.
+    int file_flags =
+        base::PLATFORM_FILE_CREATE_ALWAYS |
+        base::PLATFORM_FILE_WRITE |
+        base::PLATFORM_FILE_ASYNC |
+        base::PLATFORM_FILE_TEMPORARY;
+    file_handle_ = base::CreatePlatformFile(file_path_, file_flags, NULL);
+  }
+
+  virtual void RunCallback() {
+    callback_->Run(base::PassPlatformFile(&file_handle_), file_path_);
+    delete callback_;
+  }
+
+ private:
+  scoped_refptr<base::MessageLoopProxy> message_loop_proxy_;
+  base::FileUtilProxy::CreateTemporaryCallback* callback_;
+  base::PlatformFile file_handle_;
+  FilePath file_path_;
+};
+
+class RelayWithStatusCallback : public MessageLoopRelay {
+ public:
+  explicit RelayWithStatusCallback(
+      base::FileUtilProxy::StatusCallback* callback)
+      : callback_(callback),
+        succeeded_(false) {
+    // It is OK for callback to be NULL.
+  }
+
+ protected:
+  virtual void RunCallback() {
+    // The caller may not have been interested in the result.
+    if (callback_) {
+      callback_->Run(succeeded_);
+      delete callback_;
+    }
+  }
+
+  void SetStatus(bool succeeded) { succeeded_ = succeeded; }
+
+ private:
+  base::FileUtilProxy::StatusCallback* callback_;
+  bool succeeded_;
+};
+
+class RelayClose : public RelayWithStatusCallback {
+ public:
+  RelayClose(base::PlatformFile file_handle,
+             base::FileUtilProxy::StatusCallback* callback)
+      : RelayWithStatusCallback(callback),
+        file_handle_(file_handle) {
+  }
+
+ protected:
+  virtual void RunWork() {
+    SetStatus(base::ClosePlatformFile(file_handle_));
+  }
+
+ private:
+  base::PlatformFile file_handle_;
+};
+
+class RelayDelete : public RelayWithStatusCallback {
+ public:
+  RelayDelete(const FilePath& file_path,
+              bool recursive,
+              base::FileUtilProxy::StatusCallback* callback)
+      : RelayWithStatusCallback(callback),
+        file_path_(file_path),
+        recursive_(recursive) {
+  }
+
+ protected:
+  virtual void RunWork() {
+    SetStatus(file_util::Delete(file_path_, recursive_));
+  }
+
+ private:
+  FilePath file_path_;
+  bool recursive_;
+};
+
+void Start(const tracked_objects::Location& from_here,
+           scoped_refptr<base::MessageLoopProxy> message_loop_proxy,
+           scoped_refptr<MessageLoopRelay> relay) {
+  relay->Start(message_loop_proxy, from_here);
+}
+
+}  // namespace
+
+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));
+}
+
+// static
+void FileUtilProxy::CreateTemporary(
+    scoped_refptr<MessageLoopProxy> message_loop_proxy,
+    CreateTemporaryCallback* callback) {
+  Start(FROM_HERE, message_loop_proxy,
+        new RelayCreateTemporary(message_loop_proxy, callback));
+}
+
+// static
+void FileUtilProxy::Close(scoped_refptr<MessageLoopProxy> message_loop_proxy,
+                          base::PlatformFile file_handle,
+                          StatusCallback* callback) {
+  Start(FROM_HERE, message_loop_proxy, new RelayClose(file_handle, callback));
+}
+
+// static
+void FileUtilProxy::Delete(scoped_refptr<MessageLoopProxy> message_loop_proxy,
+                           const FilePath& file_path,
+                           StatusCallback* callback) {
+  Start(FROM_HERE, message_loop_proxy,
+        new RelayDelete(file_path, false, 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
diff --git a/base/file_util_proxy.h b/base/file_util_proxy.h
new file mode 100644
index 0000000..cd6b158
--- /dev/null
+++ b/base/file_util_proxy.h
@@ -0,0 +1,64 @@
+// Copyright (c) 2010 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#ifndef BASE_FILE_SYSTEM_PROXY_H_
+#define BASE_FILE_SYSTEM_PROXY_H_
+
+#include "base/callback.h"
+#include "base/platform_file.h"
+#include "base/ref_counted.h"
+#include "base/tracked_objects.h"
+
+namespace base {
+
+class MessageLoopProxy;
+
+// This class provides asynchronous access to common file routines.
+class FileUtilProxy {
+ public:
+  // This callback is used by methods that report success with a bool.  It is
+  // valid to pass NULL as the callback parameter to any function that takes a
+  // StatusCallback, in which case the operation will complete silently.
+  typedef Callback1<bool /* succeeded */>::Type StatusCallback;
+
+  // Creates or opens a file with the given flags.  It is invalid to pass NULL
+  // for the callback.
+  typedef Callback2<base::PassPlatformFile, bool /* created */>::Type
+      CreateOrOpenCallback;
+  static void CreateOrOpen(scoped_refptr<MessageLoopProxy> message_loop_proxy,
+                           const FilePath& file_path,
+                           int file_flags,
+                           CreateOrOpenCallback* callback);
+
+  // Creates a temporary file for writing.  The path and an open file handle
+  // are returned.  It is invalid to pass NULL for the callback.
+  typedef Callback2<base::PassPlatformFile, FilePath>::Type
+      CreateTemporaryCallback;
+  static void CreateTemporary(
+      scoped_refptr<MessageLoopProxy> message_loop_proxy,
+      CreateTemporaryCallback* callback);
+
+  // Close the given file handle.
+  static void Close(scoped_refptr<MessageLoopProxy> message_loop_proxy,
+                    base::PlatformFile,
+                    StatusCallback* callback);
+
+  // Deletes a file or empty directory.
+  static void Delete(scoped_refptr<MessageLoopProxy> message_loop_proxy,
+                     const FilePath& file_path,
+                     StatusCallback* callback);
+
+  // Deletes a directory and all of its contents.
+  static void RecursiveDelete(
+      scoped_refptr<MessageLoopProxy> message_loop_proxy,
+      const FilePath& file_path,
+      StatusCallback* callback);
+
+ private:
+  DISALLOW_IMPLICIT_CONSTRUCTORS(FileUtilProxy);
+};
+
+} // namespace base
+
+#endif  // BASE_FILE_SYSTEM_PROXY_H_