OLD | NEW |
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 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 | 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 #ifndef STORAGE_BROWSER_BLOB_SCOPED_FILE_H_ | 5 #ifndef STORAGE_BROWSER_BLOB_SCOPED_FILE_H_ |
6 #define STORAGE_BROWSER_BLOB_SCOPED_FILE_H_ | 6 #define STORAGE_BROWSER_BLOB_SCOPED_FILE_H_ |
7 | 7 |
8 #include <map> | 8 #include <map> |
9 | 9 |
10 #include "base/callback_forward.h" | 10 #include "base/callback_forward.h" |
11 #include "base/files/file_path.h" | 11 #include "base/files/file_path.h" |
| 12 #include "base/macros.h" |
12 #include "base/memory/ref_counted.h" | 13 #include "base/memory/ref_counted.h" |
13 #include "base/move.h" | |
14 #include "storage/browser/storage_browser_export.h" | 14 #include "storage/browser/storage_browser_export.h" |
15 | 15 |
16 namespace base { | 16 namespace base { |
17 class TaskRunner; | 17 class TaskRunner; |
18 } | 18 } |
19 | 19 |
20 namespace storage { | 20 namespace storage { |
21 | 21 |
22 // A scoped reference for a FilePath that can optionally schedule the file | 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. | 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 | 24 // This class supports move semantics, i.e. consumers can call Pass() to |
25 // pass the ownership of ScopedFile. | 25 // pass the ownership of ScopedFile. |
26 // | 26 // |
27 // TODO(kinuko): Probably this can be moved under base or somewhere more | 27 // TODO(kinuko): Probably this can be moved under base or somewhere more |
28 // common place. | 28 // common place. |
29 class STORAGE_EXPORT ScopedFile { | 29 class STORAGE_EXPORT ScopedFile { |
30 MOVE_ONLY_TYPE_FOR_CPP_03(ScopedFile) | |
31 | |
32 public: | 30 public: |
33 typedef base::Callback<void(const base::FilePath&)> ScopeOutCallback; | 31 typedef base::Callback<void(const base::FilePath&)> ScopeOutCallback; |
34 typedef std::pair<ScopeOutCallback, scoped_refptr<base::TaskRunner> > | 32 typedef std::pair<ScopeOutCallback, scoped_refptr<base::TaskRunner> > |
35 ScopeOutCallbackPair; | 33 ScopeOutCallbackPair; |
36 typedef std::vector<ScopeOutCallbackPair> ScopeOutCallbackList; | 34 typedef std::vector<ScopeOutCallbackPair> ScopeOutCallbackList; |
37 | 35 |
38 enum ScopeOutPolicy { | 36 enum ScopeOutPolicy { |
39 DELETE_ON_SCOPE_OUT, | 37 DELETE_ON_SCOPE_OUT, |
40 DONT_DELETE_ON_SCOPE_OUT, | 38 DONT_DELETE_ON_SCOPE_OUT, |
41 }; | 39 }; |
42 | 40 |
43 ScopedFile(); | 41 ScopedFile(); |
44 | 42 |
45 // |file_task_runner| is used to schedule a file deletion if |policy| | 43 // |file_task_runner| is used to schedule a file deletion if |policy| |
46 // is DELETE_ON_SCOPE_OUT. | 44 // is DELETE_ON_SCOPE_OUT. |
47 ScopedFile(const base::FilePath& path, | 45 ScopedFile(const base::FilePath& path, |
48 ScopeOutPolicy policy, | 46 ScopeOutPolicy policy, |
49 const scoped_refptr<base::TaskRunner>& file_task_runner); | 47 const scoped_refptr<base::TaskRunner>& file_task_runner); |
50 | 48 |
51 // Move constructor and operator. The data of r-value will be transfered | |
52 // in a destructive way. (See base/move.h) | |
53 ScopedFile(ScopedFile&& other); | 49 ScopedFile(ScopedFile&& other); |
54 ScopedFile& operator=(ScopedFile&& rhs) { | 50 ScopedFile& operator=(ScopedFile&& rhs) { |
55 MoveFrom(rhs); | 51 MoveFrom(rhs); |
56 return *this; | 52 return *this; |
57 } | 53 } |
58 | 54 |
59 ~ScopedFile(); | 55 ~ScopedFile(); |
60 | 56 |
61 // The |callback| is fired on |callback_runner| when the final reference | 57 // The |callback| is fired on |callback_runner| when the final reference |
62 // of this instance is released. | 58 // of this instance is released. |
(...skipping 14 matching lines...) Expand all Loading... |
77 void Reset(); | 73 void Reset(); |
78 | 74 |
79 private: | 75 private: |
80 // Performs destructive move from |other| to this. | 76 // Performs destructive move from |other| to this. |
81 void MoveFrom(ScopedFile& other); | 77 void MoveFrom(ScopedFile& other); |
82 | 78 |
83 base::FilePath path_; | 79 base::FilePath path_; |
84 ScopeOutPolicy scope_out_policy_; | 80 ScopeOutPolicy scope_out_policy_; |
85 scoped_refptr<base::TaskRunner> file_task_runner_; | 81 scoped_refptr<base::TaskRunner> file_task_runner_; |
86 ScopeOutCallbackList scope_out_callbacks_; | 82 ScopeOutCallbackList scope_out_callbacks_; |
| 83 |
| 84 DISALLOW_COPY_AND_ASSIGN(ScopedFile); |
87 }; | 85 }; |
88 | 86 |
89 } // namespace storage | 87 } // namespace storage |
90 | 88 |
91 #endif // STORAGE_BROWSER_BLOB_SCOPED_FILE_H_ | 89 #endif // STORAGE_BROWSER_BLOB_SCOPED_FILE_H_ |
OLD | NEW |