| OLD | NEW |
| (Empty) |
| 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 #ifndef WEBKIT_FILEAPI_RECURSIVE_OPERATION_DELEGATE_H_ | |
| 6 #define WEBKIT_FILEAPI_RECURSIVE_OPERATION_DELEGATE_H_ | |
| 7 | |
| 8 #include <queue> | |
| 9 | |
| 10 #include "base/basictypes.h" | |
| 11 #include "base/callback.h" | |
| 12 #include "base/memory/weak_ptr.h" | |
| 13 #include "webkit/fileapi/file_system_operation.h" | |
| 14 #include "webkit/fileapi/file_system_url.h" | |
| 15 | |
| 16 namespace fileapi { | |
| 17 | |
| 18 class FileSystemContext; | |
| 19 | |
| 20 // A base class for recursive operation delegates. | |
| 21 // This also provides some convenient default implementations for subclasses | |
| 22 // like StartRecursiveOperation() and NewNestedOperation(). | |
| 23 // | |
| 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 // | |
| 28 // Each subclass can call NewNestedOperation to create a new file system | |
| 29 // operation to perform a sub-operations, e.g. can call RemoveFile for | |
| 30 // recursive Remove. | |
| 31 class RecursiveOperationDelegate | |
| 32 : public base::SupportsWeakPtr<RecursiveOperationDelegate> { | |
| 33 public: | |
| 34 typedef FileSystemOperation::StatusCallback StatusCallback; | |
| 35 typedef FileSystemOperation::FileEntryList FileEntryList; | |
| 36 | |
| 37 RecursiveOperationDelegate(FileSystemContext* file_system_context, | |
| 38 LocalFileSystemOperation* operation); | |
| 39 virtual ~RecursiveOperationDelegate(); | |
| 40 | |
| 41 // This is called when the consumer of this instance starts a non-recursive | |
| 42 // operation. | |
| 43 virtual void Run() = 0; | |
| 44 | |
| 45 // This is called when the consumer of this instance starts a recursive | |
| 46 // operation. | |
| 47 virtual void RunRecursively() = 0; | |
| 48 | |
| 49 // This is called each time a file is found while recursively | |
| 50 // performing an operation. | |
| 51 virtual void ProcessFile(const FileSystemURL& url, | |
| 52 const StatusCallback& callback) = 0; | |
| 53 | |
| 54 // This is called each time a directory is found while recursively | |
| 55 // performing an operation. | |
| 56 virtual void ProcessDirectory(const FileSystemURL& url, | |
| 57 const StatusCallback& callback) = 0; | |
| 58 | |
| 59 protected: | |
| 60 // Starts to process files/directories recursively from the given |root|. | |
| 61 // This will call ProcessFile and ProcessDirectory on each directory or file. | |
| 62 // If the given |root| is a file this simply calls ProcessFile and exits. | |
| 63 // | |
| 64 // |callback| is fired with base::PLATFORM_FILE_OK when every file/directory | |
| 65 // under |root| is processed, or fired earlier when any suboperation fails. | |
| 66 void StartRecursiveOperation(const FileSystemURL& root, | |
| 67 const StatusCallback& callback); | |
| 68 | |
| 69 // Returns new nested sub-operation. | |
| 70 LocalFileSystemOperation* NewNestedOperation(); | |
| 71 | |
| 72 FileSystemContext* file_system_context() { return file_system_context_; } | |
| 73 const FileSystemContext* file_system_context() const { | |
| 74 return file_system_context_; | |
| 75 } | |
| 76 | |
| 77 private: | |
| 78 void ProcessNextDirectory(); | |
| 79 void ProcessPendingFiles(); | |
| 80 void DidProcessFile(base::PlatformFileError error); | |
| 81 void DidProcessDirectory(const FileSystemURL& url, | |
| 82 base::PlatformFileError error); | |
| 83 void DidReadDirectory( | |
| 84 const FileSystemURL& parent, | |
| 85 base::PlatformFileError error, | |
| 86 const FileEntryList& entries, | |
| 87 bool has_more); | |
| 88 void DidTryProcessFile(base::PlatformFileError previous_error, | |
| 89 base::PlatformFileError error); | |
| 90 | |
| 91 FileSystemContext* file_system_context_; | |
| 92 LocalFileSystemOperation* operation_; | |
| 93 StatusCallback callback_; | |
| 94 std::queue<FileSystemURL> pending_directories_; | |
| 95 std::queue<FileSystemURL> pending_files_; | |
| 96 int inflight_operations_; | |
| 97 | |
| 98 DISALLOW_COPY_AND_ASSIGN(RecursiveOperationDelegate); | |
| 99 }; | |
| 100 | |
| 101 } // namespace fileapi | |
| 102 | |
| 103 #endif // WEBKIT_FILEAPI_RECURSIVE_OPERATION_DELEGATE_H_ | |
| OLD | NEW |