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

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

Issue 180243015: Base: Introduce a new version of FileUtilProxy that works with File. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Add some times to GetInfo test Created 6 years, 9 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 (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 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 BASE_FILES_FILE_UTIL_PROXY_H_ 5 #ifndef BASE_FILES_FILE_PROXY_H_
6 #define BASE_FILES_FILE_UTIL_PROXY_H_ 6 #define BASE_FILES_FILE_PROXY_H_
7 7
8 #include "base/base_export.h" 8 #include "base/base_export.h"
9 #include "base/callback_forward.h" 9 #include "base/callback_forward.h"
10 #include "base/files/file.h" 10 #include "base/files/file.h"
11 #include "base/files/file_path.h" 11 #include "base/files/file_path.h"
12 #include "base/memory/ref_counted.h" 12 #include "base/memory/ref_counted.h"
13 #include "base/platform_file.h" 13 #include "base/memory/weak_ptr.h"
14 14
15 namespace tracked_objects { 15 namespace tracked_objects {
16 class Location; 16 class Location;
17 }; 17 };
18 18
19 namespace base { 19 namespace base {
20 20
21 class TaskRunner; 21 class TaskRunner;
22 class Time; 22 class Time;
23 23
24 // This class provides asynchronous access to common file routines. 24 // This class provides asynchronous access to a File. All methods follow the
25 class BASE_EXPORT FileUtilProxy { 25 // same rules of the equivalent File method, as they are implemented by bouncing
26 // the operation to File using a TaskRunner.
27 //
28 // This class does NOT perform automatic proxying to close the underlying file
29 // at destruction, which means that it may potentially close the file in the
30 // wrong thread (the current thread). If that is not appropriate, the caller
31 // must ensure that Close() is called, so that the operation happens on the
32 // desired thread.
33 //
34 // The TaskRunner is in charge of any sequencing of the operations, but a single
35 // operation can be proxied at a time, regardless of the use of a callback.
36 // In other words, having a sequence like
37 //
38 // proxy.Write(...);
39 // delete proxy;
40 //
41 // will keep the file valid during the Write operation but will cause the file
42 // to be closed in the current thread, when the operation finishes. If Close is
43 // called right away after Write, the second call will fail because there is an
44 // operation in progress.
45 class BASE_EXPORT FileProxy : public SupportsWeakPtr<FileProxy> {
26 public: 46 public:
27 // This callback is used by methods that report only an error code. It is 47 // This callback is used by methods that report only an error code. It is
28 // valid to pass a null callback to any function that takes a StatusCallback, 48 // valid to pass a null callback to some functions that takes a
29 // in which case the operation will complete silently. 49 // StatusCallback, in which case the operation will complete silently.
30 typedef Callback<void(File::Error)> StatusCallback; 50 typedef Callback<void(File::Error)> StatusCallback;
31 51
32 typedef Callback<void(File::Error, 52 typedef Callback<void(File::Error,
33 PassPlatformFile,
34 bool /* created */)> CreateOrOpenCallback;
35 typedef Callback<void(File::Error,
36 PassPlatformFile,
37 const FilePath&)> CreateTemporaryCallback; 53 const FilePath&)> CreateTemporaryCallback;
38 typedef Callback<void(File::Error, 54 typedef Callback<void(File::Error,
39 const File::Info&)> GetFileInfoCallback; 55 const File::Info&)> GetFileInfoCallback;
40 typedef Callback<void(File::Error, 56 typedef Callback<void(File::Error,
41 const char* /* data */, 57 const char* data,
42 int /* bytes read */)> ReadCallback; 58 int bytes_read)> ReadCallback;
43 typedef Callback<void(File::Error, 59 typedef Callback<void(File::Error,
44 int /* bytes written */)> WriteCallback; 60 int bytes_written)> WriteCallback;
45 61
46 typedef Callback<File::Error(PlatformFile*, bool*)> CreateOrOpenTask; 62 FileProxy();
47 typedef Callback<File::Error(PlatformFile)> CloseTask; 63 explicit FileProxy(TaskRunner* task_runner);
48 typedef Callback<File::Error(void)> FileTask; 64 ~FileProxy();
49 65
50 // Creates or opens a file with the given flags. It is invalid to pass a null 66 // Creates or opens a file with the given flags. It is invalid to pass a null
51 // callback. If PLATFORM_FILE_CREATE is set in |file_flags| it always tries to 67 // callback. If File::FLAG_CREATE is set in |file_flags| it always tries to
52 // create a new file at the given |file_path| and calls back with 68 // create a new file at the given |file_path| and fails if the file already
53 // PLATFORM_FILE_ERROR_FILE_EXISTS if the |file_path| already exists. 69 // exists.
54 // 70 //
55 // This returns false if task posting to |task_runner| has failed. 71 // This returns false if task posting to |task_runner| has failed.
56 static bool CreateOrOpen(TaskRunner* task_runner, 72 bool CreateOrOpen(const FilePath& file_path,
57 const FilePath& file_path, 73 uint32 file_flags,
58 int file_flags, 74 const StatusCallback& callback);
59 const CreateOrOpenCallback& callback);
60 75
61 // Creates a temporary file for writing. The path and an open file handle are 76 // Creates a temporary file for writing. The path and an open file are
62 // returned. It is invalid to pass a null callback. The additional file flags 77 // returned. It is invalid to pass a null callback. The additional file flags
63 // will be added on top of the default file flags which are: 78 // will be added on top of the default file flags which are:
64 // base::PLATFORM_FILE_CREATE_ALWAYS 79 // File::FLAG_CREATE_ALWAYS
65 // base::PLATFORM_FILE_WRITE 80 // File::FLAG_WRITE
66 // base::PLATFORM_FILE_TEMPORARY. 81 // File::FLAG_TEMPORARY.
67 // Set |additional_file_flags| to 0 for synchronous writes and set to
68 // base::PLATFORM_FILE_ASYNC to support asynchronous file operations.
69 // 82 //
70 // This returns false if task posting to |task_runner| has failed. 83 // This returns false if task posting to |task_runner| has failed.
71 static bool CreateTemporary( 84 bool CreateTemporary(uint32 additional_file_flags,
72 TaskRunner* task_runner, 85 const CreateTemporaryCallback& callback);
73 int additional_file_flags,
74 const CreateTemporaryCallback& callback);
75 86
76 // Close the given file handle. 87 // Returns true if the underlying |file_| is valid.
88 bool IsValid() const;
89
90 // Returns true if a new file was created (or an old one truncated to zero
91 // length to simulate a new file), and false otherwise.
92 bool created() const { return file_.created(); }
93
94 File TakeFile();
95
96 // Proxies File::Close. The callback can be null.
77 // This returns false if task posting to |task_runner| has failed. 97 // This returns false if task posting to |task_runner| has failed.
78 static bool Close(TaskRunner* task_runner, 98 bool Close(const StatusCallback& callback);
79 PlatformFile,
80 const StatusCallback& callback);
81 99
82 // Retrieves the information about a file. It is invalid to pass a null 100 // Proxies File::GetInfo. The callback can't be null.
83 // callback.
84 // This returns false if task posting to |task_runner| has failed. 101 // This returns false if task posting to |task_runner| has failed.
85 static bool GetFileInfo( 102 bool GetInfo(const GetFileInfoCallback& callback);
86 TaskRunner* task_runner,
87 const FilePath& file_path,
88 const GetFileInfoCallback& callback);
89 103
90 // Does the same as GetFileInfo but takes PlatformFile instead of FilePath. 104 // Proxies File::Read. The callback can't be null.
91 // This returns false if task posting to |task_runner| has failed.
92 static bool GetFileInfoFromPlatformFile(
93 TaskRunner* task_runner,
94 PlatformFile file,
95 const GetFileInfoCallback& callback);
96
97 // Deletes a file or a directory.
98 // It is an error to delete a non-empty directory with recursive=false.
99 // This returns false if task posting to |task_runner| has failed.
100 static bool DeleteFile(TaskRunner* task_runner,
101 const FilePath& file_path,
102 bool recursive,
103 const StatusCallback& callback);
104
105 // Reads from a file. On success, the file pointer is moved to position
106 // |offset + bytes_to_read| in the file. The callback can be null.
107 //
108 // This returns false if |bytes_to_read| is less than zero, or 105 // This returns false if |bytes_to_read| is less than zero, or
109 // if task posting to |task_runner| has failed. 106 // if task posting to |task_runner| has failed.
110 static bool Read( 107 bool Read(int64 offset, int bytes_to_read, const ReadCallback& callback);
111 TaskRunner* task_runner,
112 PlatformFile file,
113 int64 offset,
114 int bytes_to_read,
115 const ReadCallback& callback);
116 108
117 // Writes to a file. If |offset| is greater than the length of the file, 109 // Proxies File::Write. The callback can be null.
118 // |false| is returned. On success, the file pointer is moved to position
119 // |offset + bytes_to_write| in the file. The callback can be null.
120 // |bytes_to_write| must be greater than zero.
121 //
122 // This returns false if |bytes_to_write| is less than or equal to zero, 110 // This returns false if |bytes_to_write| is less than or equal to zero,
123 // if |buffer| is NULL, or if task posting to |task_runner| has failed. 111 // if |buffer| is NULL, or if task posting to |task_runner| has failed.
124 static bool Write( 112 bool Write(int64 offset,
125 TaskRunner* task_runner, 113 const char* buffer,
126 PlatformFile file, 114 int bytes_to_write,
127 int64 offset, 115 const WriteCallback& callback);
128 const char* buffer,
129 int bytes_to_write,
130 const WriteCallback& callback);
131 116
132 // Touches a file. The callback can be null. 117 // Proxies File::SetTimes. The callback can be null.
133 // This returns false if task posting to |task_runner| has failed. 118 // This returns false if task posting to |task_runner| has failed.
134 static bool Touch( 119 bool SetTimes(Time last_access_time,
135 TaskRunner* task_runner, 120 Time last_modified_time,
136 PlatformFile file, 121 const StatusCallback& callback);
137 const Time& last_access_time,
138 const Time& last_modified_time,
139 const StatusCallback& callback);
140 122
141 // Touches a file. The callback can be null. 123 // Proxies File::SetLength. The callback can be null.
142 // This returns false if task posting to |task_runner| has failed. 124 // This returns false if task posting to |task_runner| has failed.
143 static bool Touch( 125 bool SetLength(int64 length, const StatusCallback& callback);
144 TaskRunner* task_runner,
145 const FilePath& file_path,
146 const Time& last_access_time,
147 const Time& last_modified_time,
148 const StatusCallback& callback);
149 126
150 // Truncates a file to the given length. If |length| is greater than the 127 // Proxies File::Flush. The callback can be null.
151 // current length of the file, the file will be extended with zeroes.
152 // The callback can be null.
153 // This returns false if task posting to |task_runner| has failed. 128 // This returns false if task posting to |task_runner| has failed.
154 static bool Truncate( 129 bool Flush(const StatusCallback& callback);
155 TaskRunner* task_runner,
156 PlatformFile file,
157 int64 length,
158 const StatusCallback& callback);
159
160 // Truncates a file to the given length. If |length| is greater than the
161 // current length of the file, the file will be extended with zeroes.
162 // The callback can be null.
163 // This returns false if task posting to |task_runner| has failed.
164 static bool Truncate(
165 TaskRunner* task_runner,
166 const FilePath& path,
167 int64 length,
168 const StatusCallback& callback);
169
170 // Flushes a file. The callback can be null.
171 // This returns false if task posting to |task_runner| has failed.
172 static bool Flush(
173 TaskRunner* task_runner,
174 PlatformFile file,
175 const StatusCallback& callback);
176
177 // Relay helpers.
178 // They return false if posting a given task to |task_runner| has failed.
179 static bool RelayCreateOrOpen(
180 TaskRunner* task_runner,
181 const CreateOrOpenTask& open_task,
182 const CloseTask& close_task,
183 const CreateOrOpenCallback& callback);
184 static bool RelayClose(
185 TaskRunner* task_runner,
186 const CloseTask& close_task,
187 PlatformFile,
188 const StatusCallback& callback);
189 130
190 private: 131 private:
191 DISALLOW_IMPLICIT_CONSTRUCTORS(FileUtilProxy); 132 friend class FileHelper;
133 void SetFile(File file);
134
135 scoped_refptr<TaskRunner> task_runner_;
136 File file_;
137 DISALLOW_COPY_AND_ASSIGN(FileProxy);
192 }; 138 };
193 139
194 } // namespace base 140 } // namespace base
195 141
196 #endif // BASE_FILES_FILE_UTIL_PROXY_H_ 142 #endif // BASE_FILES_FILE_PROXY_H_
OLDNEW
« no previous file with comments | « base/base.gypi ('k') | base/files/file_proxy.cc » ('j') | base/files/file_proxy_unittest.cc » ('J')

Powered by Google App Engine
This is Rietveld 408576698