Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #ifndef WEBKIT_BLOB_SCOPED_FILE_H_ | |
| 6 #define WEBKIT_BLOB_SCOPED_FILE_H_ | |
| 7 | |
| 8 #include <map> | |
| 9 | |
| 10 #include "base/callback_forward.h" | |
| 11 #include "base/files/file_path.h" | |
| 12 #include "base/memory/ref_counted.h" | |
| 13 #include "base/move.h" | |
| 14 #include "webkit/storage/webkit_storage_export.h" | |
| 15 | |
| 16 namespace base { | |
| 17 class TaskRunner; | |
| 18 } | |
| 19 | |
| 20 namespace webkit_blob { | |
| 21 | |
| 22 // A scoped reference for a FilePath that can optionally schedule the file | |
| 23 // to be deleted and/or to notify a consumer when it is going to be scoped out. | |
| 24 // This class supports move semantics, i.e. consumers can call Pass() to | |
| 25 // pass the ownership of ScopedFile. | |
| 26 // | |
| 27 // TODO(kinuko): Probably this can be moved under base or somewhere more | |
| 28 // common place. | |
| 29 class WEBKIT_STORAGE_EXPORT ScopedFile { | |
| 30 MOVE_ONLY_TYPE_FOR_CPP_03(ScopedFile, RValue) | |
|
michaeln
2013/04/22 21:07:03
geeezz... macro voodoo, i'll have to dig up what t
kinuko
2013/04/23 06:31:29
It's to support move only semantics, the macro aut
| |
| 31 | |
| 32 public: | |
| 33 typedef base::Callback<void(const base::FilePath&)> ScopeOutCallback; | |
| 34 typedef std::pair<ScopeOutCallback, scoped_refptr<base::TaskRunner> > | |
| 35 ScopeOutCallbackPair; | |
| 36 typedef std::vector<ScopeOutCallbackPair> ScopeOutCallbackList; | |
| 37 | |
| 38 enum ScopeOutPolicy { | |
| 39 DELETE_ON_SCOPE_OUT, | |
| 40 DONT_DELETE_ON_SCOPE_OUT, | |
| 41 }; | |
| 42 | |
| 43 ScopedFile(); | |
| 44 | |
| 45 // |file_task_runner| is used to schedule a file deletion if |policy| | |
| 46 // is DELETE_ON_SCOPE_OUT. | |
| 47 ScopedFile(const base::FilePath& path, | |
| 48 ScopeOutPolicy policy, | |
| 49 base::TaskRunner* file_task_runner); | |
| 50 | |
| 51 ScopedFile(RValue other); | |
| 52 ScopedFile& operator=(RValue rhs) { | |
| 53 Swap(*rhs.object); | |
|
michaeln
2013/04/22 21:07:03
assignment mutates the rhs, is that right? and is
kinuko
2013/04/23 06:31:29
Yes. I've added comments about that.
| |
| 54 return *this; | |
| 55 } | |
| 56 | |
| 57 ~ScopedFile(); | |
| 58 | |
| 59 // The |callback| is fired on |callback_runner| when the final reference | |
| 60 // of this instance is released. | |
| 61 // If release policy is DELETE_ON_SCOPE_OUT the | |
| 62 // callback(s) is/are called before the deletion is scheduled. | |
| 63 void AddScopeOutCallback(const ScopeOutCallback& callback, | |
| 64 base::TaskRunner* callback_runner); | |
| 65 | |
| 66 // The full file path. | |
| 67 const base::FilePath& path() const { return path_; } | |
| 68 | |
| 69 bool empty() const { return path_.empty(); } | |
| 70 | |
| 71 // Releases the file. After this operation, this operation will hold | |
| 72 // an empty file path and scoping out won't make any file deletion | |
| 73 // or callback dispatch. (If an owned pointer is attached to the callback | |
| 74 // the pointer will be deleted when the callback is reset.) | |
| 75 base::FilePath Release(); | |
| 76 | |
| 77 void Swap(ScopedFile& other); | |
| 78 | |
| 79 private: | |
| 80 void ScopeOut(); | |
| 81 | |
| 82 base::FilePath path_; | |
| 83 ScopeOutPolicy scope_out_policy_; | |
| 84 scoped_refptr<base::TaskRunner> file_task_runner_; | |
| 85 ScopeOutCallbackList scope_out_callbacks_; | |
| 86 }; | |
| 87 | |
| 88 } // namespace webkit_blob | |
| 89 | |
| 90 #endif // WEBKIT_BLOB_SCOPED_FILE_H_ | |
| OLD | NEW |