[email protected] | 92b80880 | 2013-01-28 05:10:51 | [diff] [blame] | 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 | |
[email protected] | c6f9203a | 2013-05-28 02:08:07 | [diff] [blame] | 5 | #include "webkit/browser/fileapi/remove_operation_delegate.h" |
[email protected] | 92b80880 | 2013-01-28 05:10:51 | [diff] [blame] | 6 | |
| 7 | #include "base/bind.h" |
[email protected] | c6f9203a | 2013-05-28 02:08:07 | [diff] [blame] | 8 | #include "webkit/browser/fileapi/file_system_context.h" |
[email protected] | 3c631b2 | 2013-06-05 16:13:34 | [diff] [blame] | 9 | #include "webkit/browser/fileapi/file_system_operation_runner.h" |
[email protected] | 92b80880 | 2013-01-28 05:10:51 | [diff] [blame] | 10 | |
| 11 | namespace fileapi { |
| 12 | |
| 13 | RemoveOperationDelegate::RemoveOperationDelegate( |
[email protected] | a85efc3 | 2013-03-04 10:45:51 | [diff] [blame] | 14 | FileSystemContext* file_system_context, |
[email protected] | aeba9c1 | 2013-01-28 10:44:42 | [diff] [blame] | 15 | const FileSystemURL& url, |
[email protected] | 92b80880 | 2013-01-28 05:10:51 | [diff] [blame] | 16 | const StatusCallback& callback) |
[email protected] | 3c631b2 | 2013-06-05 16:13:34 | [diff] [blame] | 17 | : RecursiveOperationDelegate(file_system_context), |
[email protected] | aeba9c1 | 2013-01-28 10:44:42 | [diff] [blame] | 18 | url_(url), |
[email protected] | 53a9741 | 2013-08-09 03:57:14 | [diff] [blame^] | 19 | callback_(callback), |
| 20 | weak_factory_(this) { |
[email protected] | 92b80880 | 2013-01-28 05:10:51 | [diff] [blame] | 21 | } |
| 22 | |
| 23 | RemoveOperationDelegate::~RemoveOperationDelegate() {} |
| 24 | |
[email protected] | aeba9c1 | 2013-01-28 10:44:42 | [diff] [blame] | 25 | void RemoveOperationDelegate::Run() { |
[email protected] | 3c631b2 | 2013-06-05 16:13:34 | [diff] [blame] | 26 | operation_runner()->RemoveFile(url_, base::Bind( |
[email protected] | 53a9741 | 2013-08-09 03:57:14 | [diff] [blame^] | 27 | &RemoveOperationDelegate::DidTryRemoveFile, weak_factory_.GetWeakPtr())); |
[email protected] | 92b80880 | 2013-01-28 05:10:51 | [diff] [blame] | 28 | } |
| 29 | |
[email protected] | aeba9c1 | 2013-01-28 10:44:42 | [diff] [blame] | 30 | void RemoveOperationDelegate::RunRecursively() { |
| 31 | StartRecursiveOperation( |
| 32 | url_, |
[email protected] | 53a9741 | 2013-08-09 03:57:14 | [diff] [blame^] | 33 | base::Bind(&RemoveOperationDelegate::RemoveNextDirectory, |
| 34 | weak_factory_.GetWeakPtr())); |
[email protected] | aeba9c1 | 2013-01-28 10:44:42 | [diff] [blame] | 35 | } |
| 36 | |
| 37 | void RemoveOperationDelegate::ProcessFile(const FileSystemURL& url, |
| 38 | const StatusCallback& callback) { |
[email protected] | aeba9c1 | 2013-01-28 10:44:42 | [diff] [blame] | 39 | if (to_remove_directories_.size() == 1u && |
| 40 | to_remove_directories_.top() == url) { |
| 41 | // We seem to have been re-directed from ProcessDirectory. |
| 42 | to_remove_directories_.pop(); |
| 43 | } |
[email protected] | 3c631b2 | 2013-06-05 16:13:34 | [diff] [blame] | 44 | operation_runner()->RemoveFile(url, base::Bind( |
[email protected] | 53a9741 | 2013-08-09 03:57:14 | [diff] [blame^] | 45 | &RemoveOperationDelegate::DidRemoveFile, |
| 46 | weak_factory_.GetWeakPtr(), callback)); |
[email protected] | aeba9c1 | 2013-01-28 10:44:42 | [diff] [blame] | 47 | } |
| 48 | |
| 49 | void RemoveOperationDelegate::ProcessDirectory(const FileSystemURL& url, |
| 50 | const StatusCallback& callback) { |
| 51 | to_remove_directories_.push(url); |
| 52 | callback.Run(base::PLATFORM_FILE_OK); |
[email protected] | 92b80880 | 2013-01-28 05:10:51 | [diff] [blame] | 53 | } |
| 54 | |
| 55 | void RemoveOperationDelegate::DidTryRemoveFile( |
[email protected] | 92b80880 | 2013-01-28 05:10:51 | [diff] [blame] | 56 | base::PlatformFileError error) { |
| 57 | if (error == base::PLATFORM_FILE_OK || |
| 58 | error != base::PLATFORM_FILE_ERROR_NOT_A_FILE) { |
| 59 | callback_.Run(error); |
| 60 | return; |
| 61 | } |
[email protected] | 3c631b2 | 2013-06-05 16:13:34 | [diff] [blame] | 62 | operation_runner()->RemoveDirectory(url_, callback_); |
[email protected] | 92b80880 | 2013-01-28 05:10:51 | [diff] [blame] | 63 | } |
| 64 | |
[email protected] | aeba9c1 | 2013-01-28 10:44:42 | [diff] [blame] | 65 | void RemoveOperationDelegate::DidRemoveFile(const StatusCallback& callback, |
| 66 | base::PlatformFileError error) { |
| 67 | if (error == base::PLATFORM_FILE_ERROR_NOT_FOUND) { |
| 68 | callback.Run(base::PLATFORM_FILE_OK); |
[email protected] | 92b80880 | 2013-01-28 05:10:51 | [diff] [blame] | 69 | return; |
| 70 | } |
[email protected] | aeba9c1 | 2013-01-28 10:44:42 | [diff] [blame] | 71 | callback.Run(error); |
[email protected] | 92b80880 | 2013-01-28 05:10:51 | [diff] [blame] | 72 | } |
| 73 | |
| 74 | void RemoveOperationDelegate::RemoveNextDirectory( |
| 75 | base::PlatformFileError error) { |
[email protected] | 92b80880 | 2013-01-28 05:10:51 | [diff] [blame] | 76 | if (error != base::PLATFORM_FILE_OK || |
| 77 | to_remove_directories_.empty()) { |
| 78 | callback_.Run(error); |
| 79 | return; |
| 80 | } |
| 81 | FileSystemURL url = to_remove_directories_.top(); |
| 82 | to_remove_directories_.pop(); |
[email protected] | 3c631b2 | 2013-06-05 16:13:34 | [diff] [blame] | 83 | operation_runner()->RemoveDirectory(url, base::Bind( |
[email protected] | 92b80880 | 2013-01-28 05:10:51 | [diff] [blame] | 84 | &RemoveOperationDelegate::RemoveNextDirectory, |
[email protected] | 53a9741 | 2013-08-09 03:57:14 | [diff] [blame^] | 85 | weak_factory_.GetWeakPtr())); |
[email protected] | 92b80880 | 2013-01-28 05:10:51 | [diff] [blame] | 86 | } |
| 87 | |
[email protected] | 92b80880 | 2013-01-28 05:10:51 | [diff] [blame] | 88 | } // namespace fileapi |