Change the FileUtilProxy callbacks to report error codes instead of booleans.

BUG=none
TEST=none


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

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


CrOS-Libchrome-Original-Commit: d480a3ecadc882225c2b249346dae3759e3f8fce
diff --git a/base/file_util_proxy.cc b/base/file_util_proxy.cc
index 62d150e..b08d4a7 100644
--- a/base/file_util_proxy.cc
+++ b/base/file_util_proxy.cc
@@ -14,12 +14,13 @@
  public:
   MessageLoopRelay()
       : origin_message_loop_proxy_(
-          base::MessageLoopProxy::CreateForCurrentThread()) {
+          base::MessageLoopProxy::CreateForCurrentThread()),
+        error_code_(base::PLATFORM_FILE_OK) {
   }
 
-  void Start(scoped_refptr<base::MessageLoopProxy> message_loop_proxy,
+  bool Start(scoped_refptr<base::MessageLoopProxy> message_loop_proxy,
              const tracked_objects::Location& from_here) {
-    message_loop_proxy->PostTask(
+    return message_loop_proxy->PostTask(
         from_here,
         NewRunnableMethod(this, &MessageLoopRelay::ProcessOnTargetThread));
   }
@@ -34,6 +35,14 @@
   // Called to notify the callback on the origin thread.
   virtual void RunCallback() = 0;
 
+  void set_error_code(int error_code) {
+    error_code_ = error_code;
+  }
+
+  int error_code() const {
+    return error_code_;
+  }
+
  private:
   void ProcessOnTargetThread() {
     RunWork();
@@ -43,6 +52,7 @@
   }
 
   scoped_refptr<base::MessageLoopProxy> origin_message_loop_proxy_;
+  int error_code_;
 };
 
 class RelayCreateOrOpen : public MessageLoopRelay {
@@ -69,10 +79,13 @@
 
   virtual void RunWork() {
     file_handle_ = base::CreatePlatformFile(file_path_, file_flags_, &created_);
+    if (file_handle_ == base::kInvalidPlatformFileValue)
+      set_error_code(base::PLATFORM_FILE_ERROR);
   }
 
   virtual void RunCallback() {
-    callback_->Run(base::PassPlatformFile(&file_handle_), created_);
+    callback_->Run(error_code(), base::PassPlatformFile(&file_handle_),
+                   created_);
     delete callback_;
   }
 
@@ -115,10 +128,13 @@
         base::PLATFORM_FILE_ASYNC |
         base::PLATFORM_FILE_TEMPORARY;
     file_handle_ = base::CreatePlatformFile(file_path_, file_flags, NULL);
+    if (file_handle_ == base::kInvalidPlatformFileValue)
+      set_error_code(base::PLATFORM_FILE_ERROR);
   }
 
   virtual void RunCallback() {
-    callback_->Run(base::PassPlatformFile(&file_handle_), file_path_);
+    callback_->Run(error_code(), base::PassPlatformFile(&file_handle_),
+                   file_path_);
     delete callback_;
   }
 
@@ -133,8 +149,7 @@
  public:
   explicit RelayWithStatusCallback(
       base::FileUtilProxy::StatusCallback* callback)
-      : callback_(callback),
-        succeeded_(false) {
+      : callback_(callback) {
     // It is OK for callback to be NULL.
   }
 
@@ -142,16 +157,13 @@
   virtual void RunCallback() {
     // The caller may not have been interested in the result.
     if (callback_) {
-      callback_->Run(succeeded_);
+      callback_->Run(error_code());
       delete callback_;
     }
   }
 
-  void SetStatus(bool succeeded) { succeeded_ = succeeded; }
-
  private:
   base::FileUtilProxy::StatusCallback* callback_;
-  bool succeeded_;
 };
 
 class RelayClose : public RelayWithStatusCallback {
@@ -164,7 +176,8 @@
 
  protected:
   virtual void RunWork() {
-    SetStatus(base::ClosePlatformFile(file_handle_));
+    if (!base::ClosePlatformFile(file_handle_))
+      set_error_code(base::PLATFORM_FILE_ERROR);
   }
 
  private:
@@ -183,7 +196,8 @@
 
  protected:
   virtual void RunWork() {
-    SetStatus(file_util::Delete(file_path_, recursive_));
+    if (!file_util::Delete(file_path_, recursive_))
+      set_error_code(base::PLATFORM_FILE_ERROR);
   }
 
  private:
@@ -191,10 +205,10 @@
   bool recursive_;
 };
 
-void Start(const tracked_objects::Location& from_here,
+bool 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);
+  return relay->Start(message_loop_proxy, from_here);
 }
 
 }  // namespace
@@ -202,44 +216,45 @@
 namespace base {
 
 // static
-void FileUtilProxy::CreateOrOpen(
+bool 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(
+  return Start(FROM_HERE, message_loop_proxy, new RelayCreateOrOpen(
       message_loop_proxy, file_path, file_flags, callback));
 }
 
 // static
-void FileUtilProxy::CreateTemporary(
+bool FileUtilProxy::CreateTemporary(
     scoped_refptr<MessageLoopProxy> message_loop_proxy,
     CreateTemporaryCallback* callback) {
-  Start(FROM_HERE, message_loop_proxy,
-        new RelayCreateTemporary(message_loop_proxy, callback));
+  return Start(FROM_HERE, message_loop_proxy,
+               new RelayCreateTemporary(message_loop_proxy, callback));
 }
 
 // static
-void FileUtilProxy::Close(scoped_refptr<MessageLoopProxy> message_loop_proxy,
+bool 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));
+  return Start(FROM_HERE, message_loop_proxy,
+               new RelayClose(file_handle, callback));
 }
 
 // static
-void FileUtilProxy::Delete(scoped_refptr<MessageLoopProxy> message_loop_proxy,
+bool 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));
+  return Start(FROM_HERE, message_loop_proxy,
+               new RelayDelete(file_path, false, callback));
 }
 
 // static
-void FileUtilProxy::RecursiveDelete(
+bool 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));
+  return 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
index cd6b158..87cd081 100644
--- a/base/file_util_proxy.h
+++ b/base/file_util_proxy.h
@@ -17,40 +17,40 @@
 // 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
+  // This callback is used by methods that report only an error code.  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;
+  typedef Callback1<int /* error code */>::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,
+  typedef Callback3<int /* error code */, base::PassPlatformFile,
+                    bool /* created */>::Type CreateOrOpenCallback;
+  static bool 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(
+  typedef Callback3<int /* error code */, base::PassPlatformFile,
+                    FilePath>::Type CreateTemporaryCallback;
+  static bool CreateTemporary(
       scoped_refptr<MessageLoopProxy> message_loop_proxy,
       CreateTemporaryCallback* callback);
 
   // Close the given file handle.
-  static void Close(scoped_refptr<MessageLoopProxy> message_loop_proxy,
+  static bool 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,
+  static bool Delete(scoped_refptr<MessageLoopProxy> message_loop_proxy,
                      const FilePath& file_path,
                      StatusCallback* callback);
 
   // Deletes a directory and all of its contents.
-  static void RecursiveDelete(
+  static bool RecursiveDelete(
       scoped_refptr<MessageLoopProxy> message_loop_proxy,
       const FilePath& file_path,
       StatusCallback* callback);
diff --git a/base/platform_file.h b/base/platform_file.h
index 43f8511..0216c71 100644
--- a/base/platform_file.h
+++ b/base/platform_file.h
@@ -40,6 +40,13 @@
   PLATFORM_FILE_DELETE_ON_CLOSE = 2048
 };
 
+// TODO(dumi): add more specific error codes for CreatePlatformFile().
+// TODO(dumi): add more error codes as we add new methods to FileUtilProxy.
+enum PlatformFileErrors {
+  PLATFORM_FILE_OK = 0,
+  PLATFORM_FILE_ERROR = -1
+};
+
 // Creates or opens the given file. If PLATFORM_FILE_OPEN_ALWAYS is used, and
 // |created| is provided, |created| will be set to true if the file was created
 // or to false in case the file was just opened.