| OLD | NEW |
| 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef WEBKIT_BROWSER_FILEAPI_COPY_OR_MOVE_OPERATION_DELEGATE_H_ | 5 #ifndef WEBKIT_BROWSER_FILEAPI_COPY_OR_MOVE_OPERATION_DELEGATE_H_ |
| 6 #define WEBKIT_BROWSER_FILEAPI_COPY_OR_MOVE_OPERATION_DELEGATE_H_ | 6 #define WEBKIT_BROWSER_FILEAPI_COPY_OR_MOVE_OPERATION_DELEGATE_H_ |
| 7 | 7 |
| 8 #include <set> | 8 #include <set> |
| 9 #include <stack> | 9 #include <stack> |
| 10 | 10 |
| 11 #include "base/memory/ref_counted.h" | 11 #include "base/memory/ref_counted.h" |
| 12 #include "base/memory/scoped_ptr.h" | 12 #include "base/memory/scoped_ptr.h" |
| 13 #include "base/time/time.h" | 13 #include "base/time/time.h" |
| 14 #include "webkit/browser/fileapi/recursive_operation_delegate.h" | 14 #include "storage/browser/fileapi/recursive_operation_delegate.h" |
| 15 | 15 |
| 16 namespace net { | 16 namespace net { |
| 17 class DrainableIOBuffer; | 17 class DrainableIOBuffer; |
| 18 class IOBufferWithSize; | 18 class IOBufferWithSize; |
| 19 } | 19 } |
| 20 | 20 |
| 21 namespace webkit_blob { | 21 namespace storage { |
| 22 class FileStreamReader; | 22 class FileStreamReader; |
| 23 class ShareableFileReference; | 23 class ShareableFileReference; |
| 24 } | 24 } |
| 25 | 25 |
| 26 namespace fileapi { | 26 namespace storage { |
| 27 | 27 |
| 28 class CopyOrMoveFileValidator; | 28 class CopyOrMoveFileValidator; |
| 29 class FileStreamWriter; | 29 class FileStreamWriter; |
| 30 | 30 |
| 31 // A delegate class for recursive copy or move operations. | 31 // A delegate class for recursive copy or move operations. |
| 32 class CopyOrMoveOperationDelegate | 32 class CopyOrMoveOperationDelegate : public RecursiveOperationDelegate { |
| 33 : public RecursiveOperationDelegate { | |
| 34 public: | 33 public: |
| 35 class CopyOrMoveImpl; | 34 class CopyOrMoveImpl; |
| 36 typedef FileSystemOperation::CopyProgressCallback CopyProgressCallback; | 35 typedef FileSystemOperation::CopyProgressCallback CopyProgressCallback; |
| 37 typedef FileSystemOperation::CopyOrMoveOption CopyOrMoveOption; | 36 typedef FileSystemOperation::CopyOrMoveOption CopyOrMoveOption; |
| 38 | 37 |
| 39 enum OperationType { | 38 enum OperationType { OPERATION_COPY, OPERATION_MOVE }; |
| 40 OPERATION_COPY, | |
| 41 OPERATION_MOVE | |
| 42 }; | |
| 43 | 39 |
| 44 // Helper to copy a file by reader and writer streams. | 40 // Helper to copy a file by reader and writer streams. |
| 45 // Export for testing. | 41 // Export for testing. |
| 46 class WEBKIT_STORAGE_BROWSER_EXPORT StreamCopyHelper { | 42 class STORAGE_EXPORT StreamCopyHelper { |
| 47 public: | 43 public: |
| 48 StreamCopyHelper( | 44 StreamCopyHelper( |
| 49 scoped_ptr<webkit_blob::FileStreamReader> reader, | 45 scoped_ptr<storage::FileStreamReader> reader, |
| 50 scoped_ptr<FileStreamWriter> writer, | 46 scoped_ptr<FileStreamWriter> writer, |
| 51 bool need_flush, | 47 bool need_flush, |
| 52 int buffer_size, | 48 int buffer_size, |
| 53 const FileSystemOperation::CopyFileProgressCallback& | 49 const FileSystemOperation::CopyFileProgressCallback& |
| 54 file_progress_callback, | 50 file_progress_callback, |
| 55 const base::TimeDelta& min_progress_callback_invocation_span); | 51 const base::TimeDelta& min_progress_callback_invocation_span); |
| 56 ~StreamCopyHelper(); | 52 ~StreamCopyHelper(); |
| 57 | 53 |
| 58 void Run(const StatusCallback& callback); | 54 void Run(const StatusCallback& callback); |
| 59 | 55 |
| 60 // Requests cancelling. After the cancelling is done, |callback| passed to | 56 // Requests cancelling. After the cancelling is done, |callback| passed to |
| 61 // Run will be called. | 57 // Run will be called. |
| 62 void Cancel(); | 58 void Cancel(); |
| 63 | 59 |
| 64 private: | 60 private: |
| 65 // Reads the content from the |reader_|. | 61 // Reads the content from the |reader_|. |
| 66 void Read(const StatusCallback& callback); | 62 void Read(const StatusCallback& callback); |
| 67 void DidRead(const StatusCallback& callback, int result); | 63 void DidRead(const StatusCallback& callback, int result); |
| 68 | 64 |
| 69 // Writes the content in |buffer| to |writer_|. | 65 // Writes the content in |buffer| to |writer_|. |
| 70 void Write(const StatusCallback& callback, | 66 void Write(const StatusCallback& callback, |
| 71 scoped_refptr<net::DrainableIOBuffer> buffer); | 67 scoped_refptr<net::DrainableIOBuffer> buffer); |
| 72 void DidWrite(const StatusCallback& callback, | 68 void DidWrite(const StatusCallback& callback, |
| 73 scoped_refptr<net::DrainableIOBuffer> buffer, int result); | 69 scoped_refptr<net::DrainableIOBuffer> buffer, |
| 70 int result); |
| 74 | 71 |
| 75 // Flushes the written content in |writer_|. | 72 // Flushes the written content in |writer_|. |
| 76 void Flush(const StatusCallback& callback, bool is_eof); | 73 void Flush(const StatusCallback& callback, bool is_eof); |
| 77 void DidFlush(const StatusCallback& callback, bool is_eof, int result); | 74 void DidFlush(const StatusCallback& callback, bool is_eof, int result); |
| 78 | 75 |
| 79 scoped_ptr<webkit_blob::FileStreamReader> reader_; | 76 scoped_ptr<storage::FileStreamReader> reader_; |
| 80 scoped_ptr<FileStreamWriter> writer_; | 77 scoped_ptr<FileStreamWriter> writer_; |
| 81 const bool need_flush_; | 78 const bool need_flush_; |
| 82 FileSystemOperation::CopyFileProgressCallback file_progress_callback_; | 79 FileSystemOperation::CopyFileProgressCallback file_progress_callback_; |
| 83 scoped_refptr<net::IOBufferWithSize> io_buffer_; | 80 scoped_refptr<net::IOBufferWithSize> io_buffer_; |
| 84 int64 num_copied_bytes_; | 81 int64 num_copied_bytes_; |
| 85 int64 previous_flush_offset_; | 82 int64 previous_flush_offset_; |
| 86 base::Time last_progress_callback_invocation_time_; | 83 base::Time last_progress_callback_invocation_time_; |
| 87 base::TimeDelta min_progress_callback_invocation_span_; | 84 base::TimeDelta min_progress_callback_invocation_span_; |
| 88 bool cancel_requested_; | 85 bool cancel_requested_; |
| 89 base::WeakPtrFactory<StreamCopyHelper> weak_factory_; | 86 base::WeakPtrFactory<StreamCopyHelper> weak_factory_; |
| 90 DISALLOW_COPY_AND_ASSIGN(StreamCopyHelper); | 87 DISALLOW_COPY_AND_ASSIGN(StreamCopyHelper); |
| 91 }; | 88 }; |
| 92 | 89 |
| 93 CopyOrMoveOperationDelegate( | 90 CopyOrMoveOperationDelegate(FileSystemContext* file_system_context, |
| 94 FileSystemContext* file_system_context, | 91 const FileSystemURL& src_root, |
| 95 const FileSystemURL& src_root, | 92 const FileSystemURL& dest_root, |
| 96 const FileSystemURL& dest_root, | 93 OperationType operation_type, |
| 97 OperationType operation_type, | 94 CopyOrMoveOption option, |
| 98 CopyOrMoveOption option, | 95 const CopyProgressCallback& progress_callback, |
| 99 const CopyProgressCallback& progress_callback, | 96 const StatusCallback& callback); |
| 100 const StatusCallback& callback); | |
| 101 virtual ~CopyOrMoveOperationDelegate(); | 97 virtual ~CopyOrMoveOperationDelegate(); |
| 102 | 98 |
| 103 // RecursiveOperationDelegate overrides: | 99 // RecursiveOperationDelegate overrides: |
| 104 virtual void Run() OVERRIDE; | 100 virtual void Run() OVERRIDE; |
| 105 virtual void RunRecursively() OVERRIDE; | 101 virtual void RunRecursively() OVERRIDE; |
| 106 virtual void ProcessFile(const FileSystemURL& url, | 102 virtual void ProcessFile(const FileSystemURL& url, |
| 107 const StatusCallback& callback) OVERRIDE; | 103 const StatusCallback& callback) OVERRIDE; |
| 108 virtual void ProcessDirectory(const FileSystemURL& url, | 104 virtual void ProcessDirectory(const FileSystemURL& url, |
| 109 const StatusCallback& callback) OVERRIDE; | 105 const StatusCallback& callback) OVERRIDE; |
| 110 virtual void PostProcessDirectory(const FileSystemURL& url, | 106 virtual void PostProcessDirectory(const FileSystemURL& url, |
| 111 const StatusCallback& callback) OVERRIDE; | 107 const StatusCallback& callback) OVERRIDE; |
| 112 | 108 |
| 113 | |
| 114 protected: | 109 protected: |
| 115 virtual void OnCancel() OVERRIDE; | 110 virtual void OnCancel() OVERRIDE; |
| 116 | 111 |
| 117 private: | 112 private: |
| 118 void DidCopyOrMoveFile(const FileSystemURL& src_url, | 113 void DidCopyOrMoveFile(const FileSystemURL& src_url, |
| 119 const FileSystemURL& dest_url, | 114 const FileSystemURL& dest_url, |
| 120 const StatusCallback& callback, | 115 const StatusCallback& callback, |
| 121 CopyOrMoveImpl* impl, | 116 CopyOrMoveImpl* impl, |
| 122 base::File::Error error); | 117 base::File::Error error); |
| 123 void DidTryRemoveDestRoot(const StatusCallback& callback, | 118 void DidTryRemoveDestRoot(const StatusCallback& callback, |
| 124 base::File::Error error); | 119 base::File::Error error); |
| 125 void ProcessDirectoryInternal(const FileSystemURL& src_url, | 120 void ProcessDirectoryInternal(const FileSystemURL& src_url, |
| 126 const FileSystemURL& dest_url, | 121 const FileSystemURL& dest_url, |
| 127 const StatusCallback& callback); | 122 const StatusCallback& callback); |
| 128 void DidCreateDirectory(const FileSystemURL& src_url, | 123 void DidCreateDirectory(const FileSystemURL& src_url, |
| 129 const FileSystemURL& dest_url, | 124 const FileSystemURL& dest_url, |
| 130 const StatusCallback& callback, | 125 const StatusCallback& callback, |
| 131 base::File::Error error); | 126 base::File::Error error); |
| 132 void PostProcessDirectoryAfterGetMetadata( | 127 void PostProcessDirectoryAfterGetMetadata(const FileSystemURL& src_url, |
| 133 const FileSystemURL& src_url, | 128 const StatusCallback& callback, |
| 134 const StatusCallback& callback, | 129 base::File::Error error, |
| 135 base::File::Error error, | 130 const base::File::Info& file_info); |
| 136 const base::File::Info& file_info); | |
| 137 void PostProcessDirectoryAfterTouchFile(const FileSystemURL& src_url, | 131 void PostProcessDirectoryAfterTouchFile(const FileSystemURL& src_url, |
| 138 const StatusCallback& callback, | 132 const StatusCallback& callback, |
| 139 base::File::Error error); | 133 base::File::Error error); |
| 140 void DidRemoveSourceForMove(const StatusCallback& callback, | 134 void DidRemoveSourceForMove(const StatusCallback& callback, |
| 141 base::File::Error error); | 135 base::File::Error error); |
| 142 | 136 |
| 143 void OnCopyFileProgress(const FileSystemURL& src_url, int64 size); | 137 void OnCopyFileProgress(const FileSystemURL& src_url, int64 size); |
| 144 FileSystemURL CreateDestURL(const FileSystemURL& src_url) const; | 138 FileSystemURL CreateDestURL(const FileSystemURL& src_url) const; |
| 145 | 139 |
| 146 FileSystemURL src_root_; | 140 FileSystemURL src_root_; |
| 147 FileSystemURL dest_root_; | 141 FileSystemURL dest_root_; |
| 148 bool same_file_system_; | 142 bool same_file_system_; |
| 149 OperationType operation_type_; | 143 OperationType operation_type_; |
| 150 CopyOrMoveOption option_; | 144 CopyOrMoveOption option_; |
| 151 CopyProgressCallback progress_callback_; | 145 CopyProgressCallback progress_callback_; |
| 152 StatusCallback callback_; | 146 StatusCallback callback_; |
| 153 | 147 |
| 154 std::set<CopyOrMoveImpl*> running_copy_set_; | 148 std::set<CopyOrMoveImpl*> running_copy_set_; |
| 155 base::WeakPtrFactory<CopyOrMoveOperationDelegate> weak_factory_; | 149 base::WeakPtrFactory<CopyOrMoveOperationDelegate> weak_factory_; |
| 156 | 150 |
| 157 DISALLOW_COPY_AND_ASSIGN(CopyOrMoveOperationDelegate); | 151 DISALLOW_COPY_AND_ASSIGN(CopyOrMoveOperationDelegate); |
| 158 }; | 152 }; |
| 159 | 153 |
| 160 } // namespace fileapi | 154 } // namespace storage |
| 161 | 155 |
| 162 #endif // WEBKIT_BROWSER_FILEAPI_COPY_OR_MOVE_OPERATION_DELEGATE_H_ | 156 #endif // WEBKIT_BROWSER_FILEAPI_COPY_OR_MOVE_OPERATION_DELEGATE_H_ |
| OLD | NEW |