| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "base/files/file_proxy.h" | 5 #include "base/files/file_proxy.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/bind_helpers.h" | 8 #include "base/bind_helpers.h" |
| 9 #include "base/file_util.h" | 9 #include "base/file_util.h" |
| 10 #include "base/files/file.h" | 10 #include "base/files/file.h" |
| 11 #include "base/location.h" | 11 #include "base/location.h" |
| 12 #include "base/message_loop/message_loop_proxy.h" | 12 #include "base/message_loop/message_loop_proxy.h" |
| 13 #include "base/task_runner.h" | 13 #include "base/task_runner.h" |
| 14 #include "base/task_runner_util.h" | 14 #include "base/task_runner_util.h" |
| 15 | 15 |
| 16 namespace { |
| 17 |
| 18 void FileDeleter(base::File file) { |
| 19 } |
| 20 |
| 21 } // namespace |
| 22 |
| 16 namespace base { | 23 namespace base { |
| 17 | 24 |
| 18 class FileHelper { | 25 class FileHelper { |
| 19 public: | 26 public: |
| 20 FileHelper(FileProxy* proxy, File file) | 27 FileHelper(FileProxy* proxy, File file) |
| 21 : file_(file.Pass()), | 28 : file_(file.Pass()), |
| 22 proxy_(AsWeakPtr(proxy)), | 29 error_(File::FILE_ERROR_FAILED), |
| 23 error_(File::FILE_ERROR_FAILED) { | 30 task_runner_(proxy->task_runner()), |
| 31 proxy_(AsWeakPtr(proxy)) { |
| 24 } | 32 } |
| 25 | 33 |
| 26 void PassFile() { | 34 void PassFile() { |
| 27 if (proxy_) | 35 if (proxy_) |
| 28 proxy_->SetFile(file_.Pass()); | 36 proxy_->SetFile(file_.Pass()); |
| 37 else if (file_.IsValid()) |
| 38 task_runner_->PostTask(FROM_HERE, Bind(&FileDeleter, Passed(&file_))); |
| 29 } | 39 } |
| 30 | 40 |
| 31 protected: | 41 protected: |
| 32 File file_; | 42 File file_; |
| 33 WeakPtr<FileProxy> proxy_; | |
| 34 File::Error error_; | 43 File::Error error_; |
| 35 | 44 |
| 36 private: | 45 private: |
| 46 scoped_refptr<TaskRunner> task_runner_; |
| 47 WeakPtr<FileProxy> proxy_; |
| 37 DISALLOW_COPY_AND_ASSIGN(FileHelper); | 48 DISALLOW_COPY_AND_ASSIGN(FileHelper); |
| 38 }; | 49 }; |
| 39 | 50 |
| 40 namespace { | 51 namespace { |
| 41 | 52 |
| 42 class GenericFileHelper : public FileHelper { | 53 class GenericFileHelper : public FileHelper { |
| 43 public: | 54 public: |
| 44 GenericFileHelper(FileProxy* proxy, File file) | 55 GenericFileHelper(FileProxy* proxy, File file) |
| 45 : FileHelper(proxy, file.Pass()) { | 56 : FileHelper(proxy, file.Pass()) { |
| 46 } | 57 } |
| (...skipping 165 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 212 | 223 |
| 213 private: | 224 private: |
| 214 scoped_ptr<char[]> buffer_; | 225 scoped_ptr<char[]> buffer_; |
| 215 int bytes_to_write_; | 226 int bytes_to_write_; |
| 216 int bytes_written_; | 227 int bytes_written_; |
| 217 DISALLOW_COPY_AND_ASSIGN(WriteHelper); | 228 DISALLOW_COPY_AND_ASSIGN(WriteHelper); |
| 218 }; | 229 }; |
| 219 | 230 |
| 220 } // namespace | 231 } // namespace |
| 221 | 232 |
| 222 FileProxy::FileProxy() : task_runner_(NULL) { | |
| 223 } | |
| 224 | |
| 225 FileProxy::FileProxy(TaskRunner* task_runner) : task_runner_(task_runner) { | 233 FileProxy::FileProxy(TaskRunner* task_runner) : task_runner_(task_runner) { |
| 226 } | 234 } |
| 227 | 235 |
| 228 FileProxy::~FileProxy() { | 236 FileProxy::~FileProxy() { |
| 237 if (file_.IsValid()) |
| 238 task_runner_->PostTask(FROM_HERE, Bind(&FileDeleter, Passed(&file_))); |
| 229 } | 239 } |
| 230 | 240 |
| 231 bool FileProxy::CreateOrOpen(const FilePath& file_path, | 241 bool FileProxy::CreateOrOpen(const FilePath& file_path, |
| 232 uint32 file_flags, | 242 uint32 file_flags, |
| 233 const StatusCallback& callback) { | 243 const StatusCallback& callback) { |
| 234 DCHECK(!file_.IsValid()); | 244 DCHECK(!file_.IsValid()); |
| 235 CreateOrOpenHelper* helper = new CreateOrOpenHelper(this, File()); | 245 CreateOrOpenHelper* helper = new CreateOrOpenHelper(this, File()); |
| 236 return task_runner_->PostTaskAndReply( | 246 return task_runner_->PostTaskAndReply( |
| 237 FROM_HERE, | 247 FROM_HERE, |
| 238 Bind(&CreateOrOpenHelper::RunWork, Unretained(helper), file_path, | 248 Bind(&CreateOrOpenHelper::RunWork, Unretained(helper), file_path, |
| (...skipping 97 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 336 Bind(&GenericFileHelper::Flush, Unretained(helper)), | 346 Bind(&GenericFileHelper::Flush, Unretained(helper)), |
| 337 Bind(&GenericFileHelper::Reply, Owned(helper), callback)); | 347 Bind(&GenericFileHelper::Reply, Owned(helper), callback)); |
| 338 } | 348 } |
| 339 | 349 |
| 340 void FileProxy::SetFile(File file) { | 350 void FileProxy::SetFile(File file) { |
| 341 DCHECK(!file_.IsValid()); | 351 DCHECK(!file_.IsValid()); |
| 342 file_ = file.Pass(); | 352 file_ = file.Pass(); |
| 343 } | 353 } |
| 344 | 354 |
| 345 } // namespace base | 355 } // namespace base |
| OLD | NEW |