| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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/file_util_proxy.h" | 5 #include "base/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/message_loop_proxy.h" | 10 #include "base/message_loop_proxy.h" |
| (...skipping 158 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 169 } | 169 } |
| 170 | 170 |
| 171 private: | 171 private: |
| 172 scoped_refptr<MessageLoopProxy> message_loop_proxy_; | 172 scoped_refptr<MessageLoopProxy> message_loop_proxy_; |
| 173 PlatformFile file_handle_; | 173 PlatformFile file_handle_; |
| 174 FilePath file_path_; | 174 FilePath file_path_; |
| 175 PlatformFileError error_; | 175 PlatformFileError error_; |
| 176 DISALLOW_COPY_AND_ASSIGN(CreateTemporaryHelper); | 176 DISALLOW_COPY_AND_ASSIGN(CreateTemporaryHelper); |
| 177 }; | 177 }; |
| 178 | 178 |
| 179 PlatformFileError DeleteHelper(const FilePath& file_path, bool recursive) { | |
| 180 if (!file_util::PathExists(file_path)) { | |
| 181 return PLATFORM_FILE_ERROR_NOT_FOUND; | |
| 182 } | |
| 183 if (!file_util::Delete(file_path, recursive)) { | |
| 184 if (!recursive && !file_util::IsDirectoryEmpty(file_path)) { | |
| 185 return PLATFORM_FILE_ERROR_NOT_EMPTY; | |
| 186 } | |
| 187 return PLATFORM_FILE_ERROR_FAILED; | |
| 188 } | |
| 189 return PLATFORM_FILE_OK; | |
| 190 } | |
| 191 | |
| 192 class GetFileInfoHelper { | 179 class GetFileInfoHelper { |
| 193 public: | 180 public: |
| 194 GetFileInfoHelper() | 181 GetFileInfoHelper() |
| 195 : error_(PLATFORM_FILE_OK) {} | 182 : error_(PLATFORM_FILE_OK) {} |
| 196 | 183 |
| 197 void RunWorkForFilePath(const FilePath& file_path) { | 184 void RunWorkForFilePath(const FilePath& file_path) { |
| 198 if (!file_util::PathExists(file_path)) { | 185 if (!file_util::PathExists(file_path)) { |
| 199 error_ = PLATFORM_FILE_ERROR_NOT_FOUND; | 186 error_ = PLATFORM_FILE_ERROR_NOT_FOUND; |
| 200 return; | 187 return; |
| 201 } | 188 } |
| (...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 290 return error; | 277 return error; |
| 291 } | 278 } |
| 292 | 279 |
| 293 PlatformFileError CloseAdapter(PlatformFile file_handle) { | 280 PlatformFileError CloseAdapter(PlatformFile file_handle) { |
| 294 if (!ClosePlatformFile(file_handle)) { | 281 if (!ClosePlatformFile(file_handle)) { |
| 295 return PLATFORM_FILE_ERROR_FAILED; | 282 return PLATFORM_FILE_ERROR_FAILED; |
| 296 } | 283 } |
| 297 return PLATFORM_FILE_OK; | 284 return PLATFORM_FILE_OK; |
| 298 } | 285 } |
| 299 | 286 |
| 287 PlatformFileError DeleteAdapter(const FilePath& file_path, bool recursive) { |
| 288 if (!file_util::PathExists(file_path)) { |
| 289 return PLATFORM_FILE_ERROR_NOT_FOUND; |
| 290 } |
| 291 if (!file_util::Delete(file_path, recursive)) { |
| 292 if (!recursive && !file_util::IsDirectoryEmpty(file_path)) { |
| 293 return PLATFORM_FILE_ERROR_NOT_EMPTY; |
| 294 } |
| 295 return PLATFORM_FILE_ERROR_FAILED; |
| 296 } |
| 297 return PLATFORM_FILE_OK; |
| 298 } |
| 299 |
| 300 } // namespace | 300 } // namespace |
| 301 | 301 |
| 302 // static | 302 // static |
| 303 bool FileUtilProxy::CreateOrOpen( | 303 bool FileUtilProxy::CreateOrOpen( |
| 304 scoped_refptr<MessageLoopProxy> message_loop_proxy, | 304 scoped_refptr<MessageLoopProxy> message_loop_proxy, |
| 305 const FilePath& file_path, int file_flags, | 305 const FilePath& file_path, int file_flags, |
| 306 const CreateOrOpenCallback& callback) { | 306 const CreateOrOpenCallback& callback) { |
| 307 return RelayCreateOrOpen( | 307 return RelayCreateOrOpen( |
| 308 message_loop_proxy, | 308 message_loop_proxy, |
| 309 base::Bind(&CreateOrOpenAdapter, file_path, file_flags), | 309 base::Bind(&CreateOrOpenAdapter, file_path, file_flags), |
| (...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 360 Bind(&GetFileInfoHelper::RunWorkForPlatformFile, | 360 Bind(&GetFileInfoHelper::RunWorkForPlatformFile, |
| 361 Unretained(helper), file), | 361 Unretained(helper), file), |
| 362 Bind(&GetFileInfoHelper::Reply, Owned(helper), callback)); | 362 Bind(&GetFileInfoHelper::Reply, Owned(helper), callback)); |
| 363 } | 363 } |
| 364 | 364 |
| 365 // static | 365 // static |
| 366 bool FileUtilProxy::Delete(scoped_refptr<MessageLoopProxy> message_loop_proxy, | 366 bool FileUtilProxy::Delete(scoped_refptr<MessageLoopProxy> message_loop_proxy, |
| 367 const FilePath& file_path, | 367 const FilePath& file_path, |
| 368 bool recursive, | 368 bool recursive, |
| 369 const StatusCallback& callback) { | 369 const StatusCallback& callback) { |
| 370 return PostTaskAndReplyWithStatus<PlatformFileError>( | 370 return RelayFileTask( |
| 371 message_loop_proxy, FROM_HERE, | 371 message_loop_proxy, FROM_HERE, |
| 372 Bind(&DeleteHelper, file_path, recursive), callback, | 372 Bind(&DeleteAdapter, file_path, recursive), |
| 373 new PlatformFileError); | 373 callback); |
| 374 } | 374 } |
| 375 | 375 |
| 376 // static | 376 // static |
| 377 bool FileUtilProxy::RecursiveDelete( | 377 bool FileUtilProxy::RecursiveDelete( |
| 378 scoped_refptr<MessageLoopProxy> message_loop_proxy, | 378 scoped_refptr<MessageLoopProxy> message_loop_proxy, |
| 379 const FilePath& file_path, | 379 const FilePath& file_path, |
| 380 const StatusCallback& callback) { | 380 const StatusCallback& callback) { |
| 381 return PostTaskAndReplyWithStatus<PlatformFileError>( | 381 return RelayFileTask( |
| 382 message_loop_proxy, FROM_HERE, | 382 message_loop_proxy, FROM_HERE, |
| 383 Bind(&DeleteHelper, file_path, true /* recursive */), callback, | 383 Bind(&DeleteAdapter, file_path, true /* recursive */), |
| 384 new PlatformFileError); | 384 callback); |
| 385 } | 385 } |
| 386 | 386 |
| 387 // static | 387 // static |
| 388 bool FileUtilProxy::Read( | 388 bool FileUtilProxy::Read( |
| 389 scoped_refptr<MessageLoopProxy> message_loop_proxy, | 389 scoped_refptr<MessageLoopProxy> message_loop_proxy, |
| 390 PlatformFile file, | 390 PlatformFile file, |
| 391 int64 offset, | 391 int64 offset, |
| 392 int bytes_to_read, | 392 int bytes_to_read, |
| 393 const ReadCallback& callback) { | 393 const ReadCallback& callback) { |
| 394 if (bytes_to_read < 0) { | 394 if (bytes_to_read < 0) { |
| (...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 465 scoped_refptr<MessageLoopProxy> message_loop_proxy, | 465 scoped_refptr<MessageLoopProxy> message_loop_proxy, |
| 466 PlatformFile file, | 466 PlatformFile file, |
| 467 const StatusCallback& callback) { | 467 const StatusCallback& callback) { |
| 468 return PostTaskAndReplyWithStatus<bool>( | 468 return PostTaskAndReplyWithStatus<bool>( |
| 469 message_loop_proxy, FROM_HERE, | 469 message_loop_proxy, FROM_HERE, |
| 470 Bind(&FlushPlatformFile, file), callback, | 470 Bind(&FlushPlatformFile, file), callback, |
| 471 new PlatformFileError); | 471 new PlatformFileError); |
| 472 } | 472 } |
| 473 | 473 |
| 474 // static | 474 // static |
| 475 bool FileUtilProxy::RelayFileTask( |
| 476 scoped_refptr<MessageLoopProxy> message_loop_proxy, |
| 477 const tracked_objects::Location& from_here, |
| 478 const FileTask& file_task, |
| 479 const StatusCallback& callback) { |
| 480 PlatformFileError* result = new PlatformFileError; |
| 481 return message_loop_proxy->PostTaskAndReply( |
| 482 from_here, |
| 483 ReturnAsParam(file_task, result), |
| 484 ReplyHelper(callback, Owned(result))); |
| 485 } |
| 486 |
| 487 // static |
| 475 bool FileUtilProxy::RelayCreateOrOpen( | 488 bool FileUtilProxy::RelayCreateOrOpen( |
| 476 scoped_refptr<MessageLoopProxy> message_loop_proxy, | 489 scoped_refptr<MessageLoopProxy> message_loop_proxy, |
| 477 const CreateOrOpenTask& open_task, | 490 const CreateOrOpenTask& open_task, |
| 478 const CloseTask& close_task, | 491 const CloseTask& close_task, |
| 479 const CreateOrOpenCallback& callback) { | 492 const CreateOrOpenCallback& callback) { |
| 480 CreateOrOpenHelper* helper = new CreateOrOpenHelper( | 493 CreateOrOpenHelper* helper = new CreateOrOpenHelper( |
| 481 message_loop_proxy, close_task); | 494 message_loop_proxy, close_task); |
| 482 return message_loop_proxy->PostTaskAndReply( | 495 return message_loop_proxy->PostTaskAndReply( |
| 483 FROM_HERE, | 496 FROM_HERE, |
| 484 Bind(&CreateOrOpenHelper::RunWork, Unretained(helper), open_task), | 497 Bind(&CreateOrOpenHelper::RunWork, Unretained(helper), open_task), |
| 485 Bind(&CreateOrOpenHelper::Reply, Owned(helper), callback)); | 498 Bind(&CreateOrOpenHelper::Reply, Owned(helper), callback)); |
| 486 } | 499 } |
| 487 | 500 |
| 488 // static | 501 // static |
| 489 bool FileUtilProxy::RelayClose( | 502 bool FileUtilProxy::RelayClose( |
| 490 scoped_refptr<MessageLoopProxy> message_loop_proxy, | 503 scoped_refptr<MessageLoopProxy> message_loop_proxy, |
| 491 const CloseTask& close_task, | 504 const CloseTask& close_task, |
| 492 PlatformFile file_handle, | 505 PlatformFile file_handle, |
| 493 const StatusCallback& callback) { | 506 const StatusCallback& callback) { |
| 494 PlatformFileError* result = new PlatformFileError; | 507 PlatformFileError* result = new PlatformFileError; |
| 495 return message_loop_proxy->PostTaskAndReply( | 508 return message_loop_proxy->PostTaskAndReply( |
| 496 FROM_HERE, | 509 FROM_HERE, |
| 497 ReturnAsParam(close_task, file_handle, result), | 510 ReturnAsParam(close_task, file_handle, result), |
| 498 ReplyHelper(callback, Owned(result))); | 511 ReplyHelper(callback, Owned(result))); |
| 499 } | 512 } |
| 500 | 513 |
| 501 } // namespace base | 514 } // namespace base |
| OLD | NEW |