Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(5)

Side by Side Diff: base/files/file_proxy.cc

Issue 231703002: Base: Make FileProxy automaticaly close the file on a worker thread. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 6 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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()),
29 task_runner_(proxy->task_runner()),
22 proxy_(AsWeakPtr(proxy)), 30 proxy_(AsWeakPtr(proxy)),
23 error_(File::FILE_ERROR_FAILED) { 31 error_(File::FILE_ERROR_FAILED) {
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_;
43 scoped_refptr<TaskRunner> task_runner_;
willchan no longer on Chromium 2014/04/10 20:27:47 Can this be private? Does it need to be protected?
rvargas (doing something else) 2014/04/10 22:03:51 yes. I made it private (along with proxy_)
33 WeakPtr<FileProxy> proxy_; 44 WeakPtr<FileProxy> proxy_;
34 File::Error error_; 45 File::Error error_;
35 46
36 private: 47 private:
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 {
(...skipping 169 matching lines...) Expand 10 before | Expand all | Expand 10 after
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
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
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698