| 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 | |
| 23 namespace base { | 16 namespace base { |
| 24 | 17 |
| 25 class FileHelper { | 18 class FileHelper { |
| 26 public: | 19 public: |
| 27 FileHelper(FileProxy* proxy, File file) | 20 FileHelper(FileProxy* proxy, File file) |
| 28 : file_(file.Pass()), | 21 : file_(file.Pass()), |
| 29 error_(File::FILE_ERROR_FAILED), | 22 proxy_(AsWeakPtr(proxy)), |
| 30 task_runner_(proxy->task_runner()), | 23 error_(File::FILE_ERROR_FAILED) { |
| 31 proxy_(AsWeakPtr(proxy)) { | |
| 32 } | 24 } |
| 33 | 25 |
| 34 void PassFile() { | 26 void PassFile() { |
| 35 if (proxy_) | 27 if (proxy_) |
| 36 proxy_->SetFile(file_.Pass()); | 28 proxy_->SetFile(file_.Pass()); |
| 37 else if (file_.IsValid()) | |
| 38 task_runner_->PostTask(FROM_HERE, Bind(&FileDeleter, Passed(&file_))); | |
| 39 } | 29 } |
| 40 | 30 |
| 41 protected: | 31 protected: |
| 42 File file_; | 32 File file_; |
| 33 WeakPtr<FileProxy> proxy_; |
| 43 File::Error error_; | 34 File::Error error_; |
| 44 | 35 |
| 45 private: | 36 private: |
| 46 scoped_refptr<TaskRunner> task_runner_; | |
| 47 WeakPtr<FileProxy> proxy_; | |
| 48 DISALLOW_COPY_AND_ASSIGN(FileHelper); | 37 DISALLOW_COPY_AND_ASSIGN(FileHelper); |
| 49 }; | 38 }; |
| 50 | 39 |
| 51 namespace { | 40 namespace { |
| 52 | 41 |
| 53 class GenericFileHelper : public FileHelper { | 42 class GenericFileHelper : public FileHelper { |
| 54 public: | 43 public: |
| 55 GenericFileHelper(FileProxy* proxy, File file) | 44 GenericFileHelper(FileProxy* proxy, File file) |
| 56 : FileHelper(proxy, file.Pass()) { | 45 : FileHelper(proxy, file.Pass()) { |
| 57 } | 46 } |
| (...skipping 165 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 223 | 212 |
| 224 private: | 213 private: |
| 225 scoped_ptr<char[]> buffer_; | 214 scoped_ptr<char[]> buffer_; |
| 226 int bytes_to_write_; | 215 int bytes_to_write_; |
| 227 int bytes_written_; | 216 int bytes_written_; |
| 228 DISALLOW_COPY_AND_ASSIGN(WriteHelper); | 217 DISALLOW_COPY_AND_ASSIGN(WriteHelper); |
| 229 }; | 218 }; |
| 230 | 219 |
| 231 } // namespace | 220 } // namespace |
| 232 | 221 |
| 222 FileProxy::FileProxy() : task_runner_(NULL) { |
| 223 } |
| 224 |
| 233 FileProxy::FileProxy(TaskRunner* task_runner) : task_runner_(task_runner) { | 225 FileProxy::FileProxy(TaskRunner* task_runner) : task_runner_(task_runner) { |
| 234 } | 226 } |
| 235 | 227 |
| 236 FileProxy::~FileProxy() { | 228 FileProxy::~FileProxy() { |
| 237 if (file_.IsValid()) | |
| 238 task_runner_->PostTask(FROM_HERE, Bind(&FileDeleter, Passed(&file_))); | |
| 239 } | 229 } |
| 240 | 230 |
| 241 bool FileProxy::CreateOrOpen(const FilePath& file_path, | 231 bool FileProxy::CreateOrOpen(const FilePath& file_path, |
| 242 uint32 file_flags, | 232 uint32 file_flags, |
| 243 const StatusCallback& callback) { | 233 const StatusCallback& callback) { |
| 244 DCHECK(!file_.IsValid()); | 234 DCHECK(!file_.IsValid()); |
| 245 CreateOrOpenHelper* helper = new CreateOrOpenHelper(this, File()); | 235 CreateOrOpenHelper* helper = new CreateOrOpenHelper(this, File()); |
| 246 return task_runner_->PostTaskAndReply( | 236 return task_runner_->PostTaskAndReply( |
| 247 FROM_HERE, | 237 FROM_HERE, |
| 248 Bind(&CreateOrOpenHelper::RunWork, Unretained(helper), file_path, | 238 Bind(&CreateOrOpenHelper::RunWork, Unretained(helper), file_path, |
| (...skipping 97 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 346 Bind(&GenericFileHelper::Flush, Unretained(helper)), | 336 Bind(&GenericFileHelper::Flush, Unretained(helper)), |
| 347 Bind(&GenericFileHelper::Reply, Owned(helper), callback)); | 337 Bind(&GenericFileHelper::Reply, Owned(helper), callback)); |
| 348 } | 338 } |
| 349 | 339 |
| 350 void FileProxy::SetFile(File file) { | 340 void FileProxy::SetFile(File file) { |
| 351 DCHECK(!file_.IsValid()); | 341 DCHECK(!file_.IsValid()); |
| 352 file_ = file.Pass(); | 342 file_ = file.Pass(); |
| 353 } | 343 } |
| 354 | 344 |
| 355 } // namespace base | 345 } // namespace base |
| OLD | NEW |