Base: Make FileProxy automaticaly close the file on a worker thread.

This CL removes the restriction that callers should call Close before
deleting the object if they want to make sure the file is not closed
on the current thread.

BUG=322664
TEST=base_unittests

Committed: https://src.chromium.org/viewvc/chrome?view=rev&revision=263675

Review URL: https://codereview.chromium.org/231703002

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


CrOS-Libchrome-Original-Commit: 2741d240bc6e677fd319b5145125a5e5942bbda7
diff --git a/base/files/file_proxy.cc b/base/files/file_proxy.cc
index b517761..fa04d7c 100644
--- a/base/files/file_proxy.cc
+++ b/base/files/file_proxy.cc
@@ -13,27 +13,38 @@
 #include "base/task_runner.h"
 #include "base/task_runner_util.h"
 
+namespace {
+
+void FileDeleter(base::File file) {
+}
+
+}  // namespace
+
 namespace base {
 
 class FileHelper {
  public:
    FileHelper(FileProxy* proxy, File file)
       : file_(file.Pass()),
-        proxy_(AsWeakPtr(proxy)),
-        error_(File::FILE_ERROR_FAILED) {
+        error_(File::FILE_ERROR_FAILED),
+        task_runner_(proxy->task_runner()),
+        proxy_(AsWeakPtr(proxy)) {
    }
 
    void PassFile() {
      if (proxy_)
        proxy_->SetFile(file_.Pass());
+     else if (file_.IsValid())
+       task_runner_->PostTask(FROM_HERE, Bind(&FileDeleter, Passed(&file_)));
    }
 
  protected:
   File file_;
-  WeakPtr<FileProxy> proxy_;
   File::Error error_;
 
  private:
+  scoped_refptr<TaskRunner> task_runner_;
+  WeakPtr<FileProxy> proxy_;
   DISALLOW_COPY_AND_ASSIGN(FileHelper);
 };
 
@@ -219,13 +230,12 @@
 
 }  // namespace
 
-FileProxy::FileProxy() : task_runner_(NULL) {
-}
-
 FileProxy::FileProxy(TaskRunner* task_runner) : task_runner_(task_runner) {
 }
 
 FileProxy::~FileProxy() {
+  if (file_.IsValid())
+    task_runner_->PostTask(FROM_HERE, Bind(&FileDeleter, Passed(&file_)));
 }
 
 bool FileProxy::CreateOrOpen(const FilePath& file_path,