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.
5 #ifndef STORAGE_BROWSER_FILEAPI_RECURSIVE_OPERATION_DELEGATE_H_
6 #define STORAGE_BROWSER_FILEAPI_RECURSIVE_OPERATION_DELEGATE_H_
11 #include "base/basictypes.h"
12 #include "base/callback.h"
13 #include "base/memory/weak_ptr.h"
14 #include "storage/browser/fileapi/file_system_operation.h"
15 #include "storage/browser/fileapi/file_system_url.h"
19 class FileSystemContext
;
20 class FileSystemOperationRunner
;
22 // A base class for recursive operation delegates.
24 // In short, each subclass should override ProcessFile and ProcessDirectory
25 // to process a directory or a file. To start the recursive operation it
26 // should also call StartRecursiveOperation.
27 class STORAGE_EXPORT RecursiveOperationDelegate
28 : public base::SupportsWeakPtr
<RecursiveOperationDelegate
> {
30 typedef FileSystemOperation::StatusCallback StatusCallback
;
31 typedef FileSystemOperation::FileEntryList FileEntryList
;
32 typedef FileSystemOperation::ErrorBehavior ErrorBehavior
;
34 virtual ~RecursiveOperationDelegate();
36 // This is called when the consumer of this instance starts a non-recursive
38 virtual void Run() = 0;
40 // This is called when the consumer of this instance starts a recursive
42 virtual void RunRecursively() = 0;
44 // This is called each time a file is found while recursively
45 // performing an operation.
46 virtual void ProcessFile(const FileSystemURL
& url
,
47 const StatusCallback
& callback
) = 0;
49 // This is called each time a directory is found while recursively
50 // performing an operation.
51 virtual void ProcessDirectory(const FileSystemURL
& url
,
52 const StatusCallback
& callback
) = 0;
55 // This is called each time after files and subdirectories for a
56 // directory is processed while recursively performing an operation.
57 virtual void PostProcessDirectory(const FileSystemURL
& url
,
58 const StatusCallback
& callback
) = 0;
60 // Cancels the currently running operation.
64 explicit RecursiveOperationDelegate(FileSystemContext
* file_system_context
);
66 // Starts to process files/directories recursively from the given |root|.
67 // This will call ProcessFile and ProcessDirectory on each file or directory.
69 // First, this tries to call ProcessFile with |root| regardless whether it is
70 // actually a file or a directory. If it is a directory, ProcessFile should
71 // return File::FILE_NOT_A_FILE.
73 // For each directory, the recursive operation works as follows:
74 // ProcessDirectory is called first for the directory.
75 // Then the directory contents are read (to obtain its sub directories and
77 // ProcessFile is called for found files. This may run in parallel.
78 // The same step is recursively applied to each subdirectory.
79 // After all files and subdirectories in a directory are processed,
80 // PostProcessDirectory is called for the directory.
81 // Here is an example;
82 // a_dir/ -+- b1_dir/ -+- c1_dir/ -+- d1_file
84 // | +- c2_file +- d2_file
86 // +- b2_dir/ --- e_dir/
91 // Then traverse order is:
92 // ProcessFile(a_dir) (This should return File::FILE_NOT_A_FILE).
93 // ProcessDirectory(a_dir).
94 // ProcessFile(b3_file), ProcessFile(b4_file). (in parallel).
95 // ProcessDirectory(b1_dir).
96 // ProcessFile(c2_file)
97 // ProcessDirectory(c1_dir).
98 // ProcessFile(d1_file), ProcessFile(d2_file). (in parallel).
99 // PostProcessDirectory(c1_dir)
100 // PostProcessDirectory(b1_dir).
101 // ProcessDirectory(b2_dir)
102 // ProcessDirectory(e_dir)
103 // PostProcessDirectory(e_dir)
104 // PostProcessDirectory(b2_dir)
105 // PostProcessDirectory(a_dir)
107 // |error_behavior| is to specify how this behaves when an operation have
109 // |callback| is fired with base::File::FILE_OK when every file/directory
110 // under |root| is processed, or fired earlier when any suboperation fails.
111 void StartRecursiveOperation(const FileSystemURL
& root
,
112 ErrorBehavior error_behavior
,
113 const StatusCallback
& callback
);
115 FileSystemContext
* file_system_context() { return file_system_context_
; }
116 const FileSystemContext
* file_system_context() const {
117 return file_system_context_
;
120 FileSystemOperationRunner
* operation_runner();
122 // Called when Cancel() is called. This is a hook to do something more
123 // in a derived class. By default, do nothing.
124 virtual void OnCancel();
127 void TryProcessFile(const FileSystemURL
& root
);
128 void DidTryProcessFile(const FileSystemURL
& root
,
129 base::File::Error error
);
130 void ProcessNextDirectory();
131 void DidProcessDirectory(base::File::Error error
);
132 void DidReadDirectory(const FileSystemURL
& parent
,
133 base::File::Error error
,
134 const FileEntryList
& entries
,
136 void ProcessPendingFiles();
137 void DidProcessFile(const FileSystemURL
& url
, base::File::Error error
);
138 void ProcessSubDirectory();
139 void DidPostProcessDirectory(base::File::Error error
);
141 // Called when all recursive operation is done (or an error occurs).
142 void Done(base::File::Error error
);
144 FileSystemContext
* file_system_context_
;
145 StatusCallback callback_
;
146 std::stack
<FileSystemURL
> pending_directories_
;
147 std::stack
<std::queue
<FileSystemURL
> > pending_directory_stack_
;
148 std::queue
<FileSystemURL
> pending_files_
;
149 int inflight_operations_
;
151 ErrorBehavior error_behavior_
;
152 bool failed_some_operations_
;
154 DISALLOW_COPY_AND_ASSIGN(RecursiveOperationDelegate
);
157 } // namespace storage
159 #endif // STORAGE_BROWSER_FILEAPI_RECURSIVE_OPERATION_DELEGATE_H_