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
5#include "webkit/browser/fileapi/remove_operation_delegate.h"
6
7#include "base/bind.h"
8#include "webkit/browser/fileapi/file_system_context.h"
9#include "webkit/browser/fileapi/file_system_operation_runner.h"
10
11namespace fileapi {
12
13RemoveOperationDelegate::RemoveOperationDelegate(
14    FileSystemContext* file_system_context,
15    const FileSystemURL& url,
16    const StatusCallback& callback)
17    : RecursiveOperationDelegate(file_system_context),
18      url_(url),
19      callback_(callback),
20      weak_factory_(this) {
21}
22
23RemoveOperationDelegate::~RemoveOperationDelegate() {}
24
25void RemoveOperationDelegate::Run() {
26  operation_runner()->RemoveFile(url_, base::Bind(
27      &RemoveOperationDelegate::DidTryRemoveFile, weak_factory_.GetWeakPtr()));
28}
29
30void RemoveOperationDelegate::RunRecursively() {
31  StartRecursiveOperation(
32      url_,
33      base::Bind(&RemoveOperationDelegate::RemoveNextDirectory,
34                 weak_factory_.GetWeakPtr()));
35}
36
37void RemoveOperationDelegate::ProcessFile(const FileSystemURL& url,
38                                          const StatusCallback& callback) {
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  }
44  operation_runner()->RemoveFile(url, base::Bind(
45      &RemoveOperationDelegate::DidRemoveFile,
46      weak_factory_.GetWeakPtr(), callback));
47}
48
49void RemoveOperationDelegate::ProcessDirectory(const FileSystemURL& url,
50                                               const StatusCallback& callback) {
51  to_remove_directories_.push(url);
52  callback.Run(base::PLATFORM_FILE_OK);
53}
54
55void RemoveOperationDelegate::DidTryRemoveFile(
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  }
62  operation_runner()->RemoveDirectory(url_, callback_);
63}
64
65void 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);
69    return;
70  }
71  callback.Run(error);
72}
73
74void RemoveOperationDelegate::RemoveNextDirectory(
75    base::PlatformFileError error) {
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();
83  operation_runner()->RemoveDirectory(url, base::Bind(
84      &RemoveOperationDelegate::RemoveNextDirectory,
85      weak_factory_.GetWeakPtr()));
86}
87
88}  // namespace fileapi
89