summaryrefslogtreecommitdiff
path: root/chromium/webkit/browser/fileapi/recursive_operation_delegate.h
blob: 551d875d125965123e853f49fdfc07c6deba07d3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
// Copyright (c) 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef WEBKIT_BROWSER_FILEAPI_RECURSIVE_OPERATION_DELEGATE_H_
#define WEBKIT_BROWSER_FILEAPI_RECURSIVE_OPERATION_DELEGATE_H_

#include <queue>

#include "base/basictypes.h"
#include "base/callback.h"
#include "base/memory/weak_ptr.h"
#include "webkit/browser/fileapi/file_system_operation.h"
#include "webkit/browser/fileapi/file_system_url.h"

namespace fileapi {

class FileSystemContext;
class FileSystemOperationRunner;

// A base class for recursive operation delegates.
// This also provides some convenient default implementations for subclasses
// like StartRecursiveOperation() and  NewNestedOperation().
//
// In short, each subclass should override ProcessFile and ProcessDirectory
// to process a directory or a file. To start the recursive operation it
// should also call StartRecursiveOperation.
//
// Each subclass can call NewNestedOperation to create a new file system
// operation to perform a sub-operations, e.g. can call RemoveFile for
// recursive Remove.
class RecursiveOperationDelegate
    : public base::SupportsWeakPtr<RecursiveOperationDelegate> {
 public:
  typedef FileSystemOperation::StatusCallback StatusCallback;
  typedef FileSystemOperation::FileEntryList FileEntryList;

  RecursiveOperationDelegate(FileSystemContext* file_system_context);
  virtual ~RecursiveOperationDelegate();

  // This is called when the consumer of this instance starts a non-recursive
  // operation.
  virtual void Run() = 0;

  // This is called when the consumer of this instance starts a recursive
  // operation.
  virtual void RunRecursively() = 0;

  // This is called each time a file is found while recursively
  // performing an operation.
  virtual void ProcessFile(const FileSystemURL& url,
                           const StatusCallback& callback) = 0;

  // This is called each time a directory is found while recursively
  // performing an operation.
  virtual void ProcessDirectory(const FileSystemURL& url,
                                const StatusCallback& callback) = 0;

 protected:
  // Starts to process files/directories recursively from the given |root|.
  // This will call ProcessFile and ProcessDirectory on each directory or file.
  // If the given |root| is a file this simply calls ProcessFile and exits.
  //
  // |callback| is fired with base::PLATFORM_FILE_OK when every file/directory
  // under |root| is processed, or fired earlier when any suboperation fails.
  void StartRecursiveOperation(const FileSystemURL& root,
                               const StatusCallback& callback);

  FileSystemContext* file_system_context() { return file_system_context_; }
  const FileSystemContext* file_system_context() const {
    return file_system_context_;
  }

  FileSystemOperationRunner* operation_runner();

 private:
  void ProcessNextDirectory();
  void ProcessPendingFiles();
  void DidProcessFile(base::PlatformFileError error);
  void DidProcessDirectory(const FileSystemURL& url,
                           base::PlatformFileError error);
  void DidReadDirectory(
      const FileSystemURL& parent,
      base::PlatformFileError error,
      const FileEntryList& entries,
      bool has_more);
  void DidTryProcessFile(base::PlatformFileError previous_error,
                         base::PlatformFileError error);

  FileSystemContext* file_system_context_;
  StatusCallback callback_;
  std::queue<FileSystemURL> pending_directories_;
  std::queue<FileSystemURL> pending_files_;
  int inflight_operations_;

  DISALLOW_COPY_AND_ASSIGN(RecursiveOperationDelegate);
};

}  // namespace fileapi

#endif  // WEBKIT_BROWSER_FILEAPI_RECURSIVE_OPERATION_DELEGATE_H_