OLD | NEW |
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 #include "base/files/file_util_proxy.h" | 5 #include "base/files/file_util_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/location.h" | 10 #include "base/location.h" |
11 #include "base/message_loop/message_loop_proxy.h" | 11 #include "base/message_loop/message_loop_proxy.h" |
12 #include "base/task_runner.h" | 12 #include "base/task_runner.h" |
13 #include "base/task_runner_util.h" | 13 #include "base/task_runner_util.h" |
14 | 14 |
15 namespace base { | 15 namespace base { |
16 | 16 |
17 namespace { | 17 namespace { |
18 | 18 |
19 void CallWithTranslatedParameter(const FileUtilProxy::StatusCallback& callback, | 19 void CallWithTranslatedParameter(const FileUtilProxy::StatusCallback& callback, |
20 bool value) { | 20 bool value) { |
21 DCHECK(!callback.is_null()); | 21 DCHECK(!callback.is_null()); |
22 callback.Run(value ? PLATFORM_FILE_OK : PLATFORM_FILE_ERROR_FAILED); | 22 callback.Run(value ? PLATFORM_FILE_OK : PLATFORM_FILE_ERROR_FAILED); |
23 } | 23 } |
24 | 24 |
25 #if !defined(OS_NACL) | |
26 // Helper classes or routines for individual methods. | 25 // Helper classes or routines for individual methods. |
27 class CreateOrOpenHelper { | 26 class CreateOrOpenHelper { |
28 public: | 27 public: |
29 CreateOrOpenHelper(TaskRunner* task_runner, | 28 CreateOrOpenHelper(TaskRunner* task_runner, |
30 const FileUtilProxy::CloseTask& close_task) | 29 const FileUtilProxy::CloseTask& close_task) |
31 : task_runner_(task_runner), | 30 : task_runner_(task_runner), |
32 close_task_(close_task), | 31 close_task_(close_task), |
33 file_handle_(kInvalidPlatformFileValue), | 32 file_handle_(kInvalidPlatformFileValue), |
34 created_(false), | 33 created_(false), |
35 error_(PLATFORM_FILE_OK) {} | 34 error_(PLATFORM_FILE_OK) {} |
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
94 callback.Run(error_, PassPlatformFile(&file_handle_), file_path_); | 93 callback.Run(error_, PassPlatformFile(&file_handle_), file_path_); |
95 } | 94 } |
96 | 95 |
97 private: | 96 private: |
98 scoped_refptr<TaskRunner> task_runner_; | 97 scoped_refptr<TaskRunner> task_runner_; |
99 PlatformFile file_handle_; | 98 PlatformFile file_handle_; |
100 FilePath file_path_; | 99 FilePath file_path_; |
101 PlatformFileError error_; | 100 PlatformFileError error_; |
102 DISALLOW_COPY_AND_ASSIGN(CreateTemporaryHelper); | 101 DISALLOW_COPY_AND_ASSIGN(CreateTemporaryHelper); |
103 }; | 102 }; |
104 #endif // !defined(OS_NACL) | |
105 | 103 |
106 class GetFileInfoHelper { | 104 class GetFileInfoHelper { |
107 public: | 105 public: |
108 GetFileInfoHelper() | 106 GetFileInfoHelper() |
109 : error_(PLATFORM_FILE_OK) {} | 107 : error_(PLATFORM_FILE_OK) {} |
110 | 108 |
111 #if !defined(OS_NACL) | |
112 void RunWorkForFilePath(const FilePath& file_path) { | 109 void RunWorkForFilePath(const FilePath& file_path) { |
113 if (!PathExists(file_path)) { | 110 if (!PathExists(file_path)) { |
114 error_ = PLATFORM_FILE_ERROR_NOT_FOUND; | 111 error_ = PLATFORM_FILE_ERROR_NOT_FOUND; |
115 return; | 112 return; |
116 } | 113 } |
117 if (!file_util::GetFileInfo(file_path, &file_info_)) | 114 if (!file_util::GetFileInfo(file_path, &file_info_)) |
118 error_ = PLATFORM_FILE_ERROR_FAILED; | 115 error_ = PLATFORM_FILE_ERROR_FAILED; |
119 } | 116 } |
120 #endif // !defined(OS_NACL) | |
121 | 117 |
122 void RunWorkForPlatformFile(PlatformFile file) { | 118 void RunWorkForPlatformFile(PlatformFile file) { |
123 if (!GetPlatformFileInfo(file, &file_info_)) | 119 if (!GetPlatformFileInfo(file, &file_info_)) |
124 error_ = PLATFORM_FILE_ERROR_FAILED; | 120 error_ = PLATFORM_FILE_ERROR_FAILED; |
125 } | 121 } |
126 | 122 |
127 void Reply(const FileUtilProxy::GetFileInfoCallback& callback) { | 123 void Reply(const FileUtilProxy::GetFileInfoCallback& callback) { |
128 if (!callback.is_null()) { | 124 if (!callback.is_null()) { |
129 callback.Run(error_, file_info_); | 125 callback.Run(error_, file_info_); |
130 } | 126 } |
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
184 } | 180 } |
185 } | 181 } |
186 | 182 |
187 private: | 183 private: |
188 scoped_ptr<char[]> buffer_; | 184 scoped_ptr<char[]> buffer_; |
189 int bytes_to_write_; | 185 int bytes_to_write_; |
190 int bytes_written_; | 186 int bytes_written_; |
191 DISALLOW_COPY_AND_ASSIGN(WriteHelper); | 187 DISALLOW_COPY_AND_ASSIGN(WriteHelper); |
192 }; | 188 }; |
193 | 189 |
194 #if !defined(OS_NACL) | |
195 PlatformFileError CreateOrOpenAdapter( | 190 PlatformFileError CreateOrOpenAdapter( |
196 const FilePath& file_path, int file_flags, | 191 const FilePath& file_path, int file_flags, |
197 PlatformFile* file_handle, bool* created) { | 192 PlatformFile* file_handle, bool* created) { |
198 DCHECK(file_handle); | 193 DCHECK(file_handle); |
199 DCHECK(created); | 194 DCHECK(created); |
200 if (!DirectoryExists(file_path.DirName())) { | 195 if (!DirectoryExists(file_path.DirName())) { |
201 // If its parent does not exist, should return NOT_FOUND error. | 196 // If its parent does not exist, should return NOT_FOUND error. |
202 return PLATFORM_FILE_ERROR_NOT_FOUND; | 197 return PLATFORM_FILE_ERROR_NOT_FOUND; |
203 } | 198 } |
204 PlatformFileError error = PLATFORM_FILE_OK; | 199 PlatformFileError error = PLATFORM_FILE_OK; |
205 *file_handle = CreatePlatformFile(file_path, file_flags, created, &error); | 200 *file_handle = CreatePlatformFile(file_path, file_flags, created, &error); |
206 return error; | 201 return error; |
207 } | 202 } |
208 #endif // !defined(OS_NACL) | |
209 | 203 |
210 PlatformFileError CloseAdapter(PlatformFile file_handle) { | 204 PlatformFileError CloseAdapter(PlatformFile file_handle) { |
211 if (!ClosePlatformFile(file_handle)) { | 205 if (!ClosePlatformFile(file_handle)) { |
212 return PLATFORM_FILE_ERROR_FAILED; | 206 return PLATFORM_FILE_ERROR_FAILED; |
213 } | 207 } |
214 return PLATFORM_FILE_OK; | 208 return PLATFORM_FILE_OK; |
215 } | 209 } |
216 | 210 |
217 #if !defined(OS_NACL) | |
218 PlatformFileError DeleteAdapter(const FilePath& file_path, bool recursive) { | 211 PlatformFileError DeleteAdapter(const FilePath& file_path, bool recursive) { |
219 if (!PathExists(file_path)) { | 212 if (!PathExists(file_path)) { |
220 return PLATFORM_FILE_ERROR_NOT_FOUND; | 213 return PLATFORM_FILE_ERROR_NOT_FOUND; |
221 } | 214 } |
222 if (!base::DeleteFile(file_path, recursive)) { | 215 if (!base::DeleteFile(file_path, recursive)) { |
223 if (!recursive && !file_util::IsDirectoryEmpty(file_path)) { | 216 if (!recursive && !file_util::IsDirectoryEmpty(file_path)) { |
224 return PLATFORM_FILE_ERROR_NOT_EMPTY; | 217 return PLATFORM_FILE_ERROR_NOT_EMPTY; |
225 } | 218 } |
226 return PLATFORM_FILE_ERROR_FAILED; | 219 return PLATFORM_FILE_ERROR_FAILED; |
227 } | 220 } |
228 return PLATFORM_FILE_OK; | 221 return PLATFORM_FILE_OK; |
229 } | 222 } |
230 #endif // !defined(OS_NACL) | |
231 | 223 |
232 } // namespace | 224 } // namespace |
233 | 225 |
234 #if !defined(OS_NACL) | |
235 // static | 226 // static |
236 bool FileUtilProxy::CreateOrOpen( | 227 bool FileUtilProxy::CreateOrOpen( |
237 TaskRunner* task_runner, | 228 TaskRunner* task_runner, |
238 const FilePath& file_path, int file_flags, | 229 const FilePath& file_path, int file_flags, |
239 const CreateOrOpenCallback& callback) { | 230 const CreateOrOpenCallback& callback) { |
240 return RelayCreateOrOpen( | 231 return RelayCreateOrOpen( |
241 task_runner, | 232 task_runner, |
242 base::Bind(&CreateOrOpenAdapter, file_path, file_flags), | 233 base::Bind(&CreateOrOpenAdapter, file_path, file_flags), |
243 base::Bind(&CloseAdapter), | 234 base::Bind(&CloseAdapter), |
244 callback); | 235 callback); |
245 } | 236 } |
246 | 237 |
247 // static | 238 // static |
248 bool FileUtilProxy::CreateTemporary( | 239 bool FileUtilProxy::CreateTemporary( |
249 TaskRunner* task_runner, | 240 TaskRunner* task_runner, |
250 int additional_file_flags, | 241 int additional_file_flags, |
251 const CreateTemporaryCallback& callback) { | 242 const CreateTemporaryCallback& callback) { |
252 CreateTemporaryHelper* helper = new CreateTemporaryHelper(task_runner); | 243 CreateTemporaryHelper* helper = new CreateTemporaryHelper(task_runner); |
253 return task_runner->PostTaskAndReply( | 244 return task_runner->PostTaskAndReply( |
254 FROM_HERE, | 245 FROM_HERE, |
255 Bind(&CreateTemporaryHelper::RunWork, Unretained(helper), | 246 Bind(&CreateTemporaryHelper::RunWork, Unretained(helper), |
256 additional_file_flags), | 247 additional_file_flags), |
257 Bind(&CreateTemporaryHelper::Reply, Owned(helper), callback)); | 248 Bind(&CreateTemporaryHelper::Reply, Owned(helper), callback)); |
258 } | 249 } |
259 #endif // !defined(OS_NACL) | |
260 | 250 |
261 // static | 251 // static |
262 bool FileUtilProxy::Close( | 252 bool FileUtilProxy::Close( |
263 TaskRunner* task_runner, | 253 TaskRunner* task_runner, |
264 base::PlatformFile file_handle, | 254 base::PlatformFile file_handle, |
265 const StatusCallback& callback) { | 255 const StatusCallback& callback) { |
266 return RelayClose( | 256 return RelayClose( |
267 task_runner, | 257 task_runner, |
268 base::Bind(&CloseAdapter), | 258 base::Bind(&CloseAdapter), |
269 file_handle, callback); | 259 file_handle, callback); |
270 } | 260 } |
271 | 261 |
272 #if !defined(OS_NACL) | |
273 // Retrieves the information about a file. It is invalid to pass NULL for the | 262 // Retrieves the information about a file. It is invalid to pass NULL for the |
274 // callback. | 263 // callback. |
275 bool FileUtilProxy::GetFileInfo( | 264 bool FileUtilProxy::GetFileInfo( |
276 TaskRunner* task_runner, | 265 TaskRunner* task_runner, |
277 const FilePath& file_path, | 266 const FilePath& file_path, |
278 const GetFileInfoCallback& callback) { | 267 const GetFileInfoCallback& callback) { |
279 GetFileInfoHelper* helper = new GetFileInfoHelper; | 268 GetFileInfoHelper* helper = new GetFileInfoHelper; |
280 return task_runner->PostTaskAndReply( | 269 return task_runner->PostTaskAndReply( |
281 FROM_HERE, | 270 FROM_HERE, |
282 Bind(&GetFileInfoHelper::RunWorkForFilePath, | 271 Bind(&GetFileInfoHelper::RunWorkForFilePath, |
283 Unretained(helper), file_path), | 272 Unretained(helper), file_path), |
284 Bind(&GetFileInfoHelper::Reply, Owned(helper), callback)); | 273 Bind(&GetFileInfoHelper::Reply, Owned(helper), callback)); |
285 } | 274 } |
286 #endif // !defined(OS_NACL) | |
287 | 275 |
288 // static | 276 // static |
289 bool FileUtilProxy::GetFileInfoFromPlatformFile( | 277 bool FileUtilProxy::GetFileInfoFromPlatformFile( |
290 TaskRunner* task_runner, | 278 TaskRunner* task_runner, |
291 PlatformFile file, | 279 PlatformFile file, |
292 const GetFileInfoCallback& callback) { | 280 const GetFileInfoCallback& callback) { |
293 GetFileInfoHelper* helper = new GetFileInfoHelper; | 281 GetFileInfoHelper* helper = new GetFileInfoHelper; |
294 return task_runner->PostTaskAndReply( | 282 return task_runner->PostTaskAndReply( |
295 FROM_HERE, | 283 FROM_HERE, |
296 Bind(&GetFileInfoHelper::RunWorkForPlatformFile, | 284 Bind(&GetFileInfoHelper::RunWorkForPlatformFile, |
297 Unretained(helper), file), | 285 Unretained(helper), file), |
298 Bind(&GetFileInfoHelper::Reply, Owned(helper), callback)); | 286 Bind(&GetFileInfoHelper::Reply, Owned(helper), callback)); |
299 } | 287 } |
300 | 288 |
301 #if !defined(OS_NACL) | |
302 // static | 289 // static |
303 bool FileUtilProxy::DeleteFile(TaskRunner* task_runner, | 290 bool FileUtilProxy::DeleteFile(TaskRunner* task_runner, |
304 const FilePath& file_path, | 291 const FilePath& file_path, |
305 bool recursive, | 292 bool recursive, |
306 const StatusCallback& callback) { | 293 const StatusCallback& callback) { |
307 return base::PostTaskAndReplyWithResult( | 294 return base::PostTaskAndReplyWithResult( |
308 task_runner, FROM_HERE, | 295 task_runner, FROM_HERE, |
309 Bind(&DeleteAdapter, file_path, recursive), | 296 Bind(&DeleteAdapter, file_path, recursive), |
310 callback); | 297 callback); |
311 } | 298 } |
312 #endif // !defined(OS_NACL) | |
313 | 299 |
314 // static | 300 // static |
315 bool FileUtilProxy::Read( | 301 bool FileUtilProxy::Read( |
316 TaskRunner* task_runner, | 302 TaskRunner* task_runner, |
317 PlatformFile file, | 303 PlatformFile file, |
318 int64 offset, | 304 int64 offset, |
319 int bytes_to_read, | 305 int bytes_to_read, |
320 const ReadCallback& callback) { | 306 const ReadCallback& callback) { |
321 if (bytes_to_read < 0) { | 307 if (bytes_to_read < 0) { |
322 return false; | 308 return false; |
(...skipping 16 matching lines...) Expand all Loading... |
339 if (bytes_to_write <= 0 || buffer == NULL) { | 325 if (bytes_to_write <= 0 || buffer == NULL) { |
340 return false; | 326 return false; |
341 } | 327 } |
342 WriteHelper* helper = new WriteHelper(buffer, bytes_to_write); | 328 WriteHelper* helper = new WriteHelper(buffer, bytes_to_write); |
343 return task_runner->PostTaskAndReply( | 329 return task_runner->PostTaskAndReply( |
344 FROM_HERE, | 330 FROM_HERE, |
345 Bind(&WriteHelper::RunWork, Unretained(helper), file, offset), | 331 Bind(&WriteHelper::RunWork, Unretained(helper), file, offset), |
346 Bind(&WriteHelper::Reply, Owned(helper), callback)); | 332 Bind(&WriteHelper::Reply, Owned(helper), callback)); |
347 } | 333 } |
348 | 334 |
349 #if !defined(OS_NACL) | |
350 // static | 335 // static |
351 bool FileUtilProxy::Touch( | 336 bool FileUtilProxy::Touch( |
352 TaskRunner* task_runner, | 337 TaskRunner* task_runner, |
353 PlatformFile file, | 338 PlatformFile file, |
354 const Time& last_access_time, | 339 const Time& last_access_time, |
355 const Time& last_modified_time, | 340 const Time& last_modified_time, |
356 const StatusCallback& callback) { | 341 const StatusCallback& callback) { |
357 return base::PostTaskAndReplyWithResult( | 342 return base::PostTaskAndReplyWithResult( |
358 task_runner, | 343 task_runner, |
359 FROM_HERE, | 344 FROM_HERE, |
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
408 const CreateOrOpenTask& open_task, | 393 const CreateOrOpenTask& open_task, |
409 const CloseTask& close_task, | 394 const CloseTask& close_task, |
410 const CreateOrOpenCallback& callback) { | 395 const CreateOrOpenCallback& callback) { |
411 CreateOrOpenHelper* helper = new CreateOrOpenHelper( | 396 CreateOrOpenHelper* helper = new CreateOrOpenHelper( |
412 task_runner, close_task); | 397 task_runner, close_task); |
413 return task_runner->PostTaskAndReply( | 398 return task_runner->PostTaskAndReply( |
414 FROM_HERE, | 399 FROM_HERE, |
415 Bind(&CreateOrOpenHelper::RunWork, Unretained(helper), open_task), | 400 Bind(&CreateOrOpenHelper::RunWork, Unretained(helper), open_task), |
416 Bind(&CreateOrOpenHelper::Reply, Owned(helper), callback)); | 401 Bind(&CreateOrOpenHelper::Reply, Owned(helper), callback)); |
417 } | 402 } |
418 #endif // !defined(OS_NACL) | |
419 | 403 |
420 // static | 404 // static |
421 bool FileUtilProxy::RelayClose( | 405 bool FileUtilProxy::RelayClose( |
422 TaskRunner* task_runner, | 406 TaskRunner* task_runner, |
423 const CloseTask& close_task, | 407 const CloseTask& close_task, |
424 PlatformFile file_handle, | 408 PlatformFile file_handle, |
425 const StatusCallback& callback) { | 409 const StatusCallback& callback) { |
426 return base::PostTaskAndReplyWithResult( | 410 return base::PostTaskAndReplyWithResult( |
427 task_runner, FROM_HERE, Bind(close_task, file_handle), callback); | 411 task_runner, FROM_HERE, Bind(close_task, file_handle), callback); |
428 } | 412 } |
429 | 413 |
430 } // namespace base | 414 } // namespace base |
OLD | NEW |