blob: fcd9ba9cee2cc72715add74b17c9e9379ab32093 [file] [log] [blame]
[email protected]92b808802013-01-28 05:10:511// 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]c6f9203a2013-05-28 02:08:075#include "webkit/browser/fileapi/remove_operation_delegate.h"
[email protected]92b808802013-01-28 05:10:516
7#include "base/bind.h"
[email protected]c6f9203a2013-05-28 02:08:078#include "webkit/browser/fileapi/file_system_context.h"
[email protected]3c631b22013-06-05 16:13:349#include "webkit/browser/fileapi/file_system_operation_runner.h"
[email protected]92b808802013-01-28 05:10:5110
11namespace fileapi {
12
13RemoveOperationDelegate::RemoveOperationDelegate(
[email protected]a85efc32013-03-04 10:45:5114 FileSystemContext* file_system_context,
[email protected]aeba9c12013-01-28 10:44:4215 const FileSystemURL& url,
[email protected]92b808802013-01-28 05:10:5116 const StatusCallback& callback)
[email protected]3c631b22013-06-05 16:13:3417 : RecursiveOperationDelegate(file_system_context),
[email protected]aeba9c12013-01-28 10:44:4218 url_(url),
[email protected]53a97412013-08-09 03:57:1419 callback_(callback),
20 weak_factory_(this) {
[email protected]92b808802013-01-28 05:10:5121}
22
23RemoveOperationDelegate::~RemoveOperationDelegate() {}
24
[email protected]aeba9c12013-01-28 10:44:4225void RemoveOperationDelegate::Run() {
[email protected]3c631b22013-06-05 16:13:3426 operation_runner()->RemoveFile(url_, base::Bind(
[email protected]53a97412013-08-09 03:57:1427 &RemoveOperationDelegate::DidTryRemoveFile, weak_factory_.GetWeakPtr()));
[email protected]92b808802013-01-28 05:10:5128}
29
[email protected]aeba9c12013-01-28 10:44:4230void RemoveOperationDelegate::RunRecursively() {
31 StartRecursiveOperation(
32 url_,
[email protected]53a97412013-08-09 03:57:1433 base::Bind(&RemoveOperationDelegate::RemoveNextDirectory,
34 weak_factory_.GetWeakPtr()));
[email protected]aeba9c12013-01-28 10:44:4235}
36
37void RemoveOperationDelegate::ProcessFile(const FileSystemURL& url,
38 const StatusCallback& callback) {
[email protected]aeba9c12013-01-28 10:44:4239 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]3c631b22013-06-05 16:13:3444 operation_runner()->RemoveFile(url, base::Bind(
[email protected]53a97412013-08-09 03:57:1445 &RemoveOperationDelegate::DidRemoveFile,
46 weak_factory_.GetWeakPtr(), callback));
[email protected]aeba9c12013-01-28 10:44:4247}
48
49void RemoveOperationDelegate::ProcessDirectory(const FileSystemURL& url,
50 const StatusCallback& callback) {
51 to_remove_directories_.push(url);
52 callback.Run(base::PLATFORM_FILE_OK);
[email protected]92b808802013-01-28 05:10:5153}
54
55void RemoveOperationDelegate::DidTryRemoveFile(
[email protected]92b808802013-01-28 05:10:5156 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]3c631b22013-06-05 16:13:3462 operation_runner()->RemoveDirectory(url_, callback_);
[email protected]92b808802013-01-28 05:10:5163}
64
[email protected]aeba9c12013-01-28 10:44:4265void 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]92b808802013-01-28 05:10:5169 return;
70 }
[email protected]aeba9c12013-01-28 10:44:4271 callback.Run(error);
[email protected]92b808802013-01-28 05:10:5172}
73
74void RemoveOperationDelegate::RemoveNextDirectory(
75 base::PlatformFileError error) {
[email protected]92b808802013-01-28 05:10:5176 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]3c631b22013-06-05 16:13:3483 operation_runner()->RemoveDirectory(url, base::Bind(
[email protected]92b808802013-01-28 05:10:5184 &RemoveOperationDelegate::RemoveNextDirectory,
[email protected]53a97412013-08-09 03:57:1485 weak_factory_.GetWeakPtr()));
[email protected]92b808802013-01-28 05:10:5186}
87
[email protected]92b808802013-01-28 05:10:5188} // namespace fileapi