Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / storage / browser / fileapi / copy_or_move_operation_delegate.h
blobe86c0ab9c0abcb9aba844be62cb5a468cf914dc2
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_COPY_OR_MOVE_OPERATION_DELEGATE_H_
6 #define STORAGE_BROWSER_FILEAPI_COPY_OR_MOVE_OPERATION_DELEGATE_H_
8 #include <set>
9 #include <stack>
11 #include "base/memory/ref_counted.h"
12 #include "base/memory/scoped_ptr.h"
13 #include "base/time/time.h"
14 #include "storage/browser/fileapi/recursive_operation_delegate.h"
16 namespace net {
17 class DrainableIOBuffer;
18 class IOBufferWithSize;
21 namespace storage {
22 class FileStreamReader;
23 class ShareableFileReference;
24 enum class FlushPolicy;
27 namespace storage {
29 class CopyOrMoveFileValidator;
30 class FileStreamWriter;
32 // A delegate class for recursive copy or move operations.
33 class CopyOrMoveOperationDelegate
34 : public RecursiveOperationDelegate {
35 public:
36 class CopyOrMoveImpl;
37 typedef FileSystemOperation::CopyProgressCallback CopyProgressCallback;
38 typedef FileSystemOperation::CopyOrMoveOption CopyOrMoveOption;
39 typedef FileSystemOperation::ErrorBehavior ErrorBehavior;
41 enum OperationType {
42 OPERATION_COPY,
43 OPERATION_MOVE
46 // Helper to copy a file by reader and writer streams.
47 // Export for testing.
48 class STORAGE_EXPORT StreamCopyHelper {
49 public:
50 StreamCopyHelper(
51 scoped_ptr<storage::FileStreamReader> reader,
52 scoped_ptr<FileStreamWriter> writer,
53 FlushPolicy flush_policy,
54 int buffer_size,
55 const FileSystemOperation::CopyFileProgressCallback&
56 file_progress_callback,
57 const base::TimeDelta& min_progress_callback_invocation_span);
58 ~StreamCopyHelper();
60 void Run(const StatusCallback& callback);
62 // Requests cancelling. After the cancelling is done, |callback| passed to
63 // Run will be called.
64 void Cancel();
66 private:
67 // Reads the content from the |reader_|.
68 void Read(const StatusCallback& callback);
69 void DidRead(const StatusCallback& callback, int result);
71 // Writes the content in |buffer| to |writer_|.
72 void Write(const StatusCallback& callback,
73 scoped_refptr<net::DrainableIOBuffer> buffer);
74 void DidWrite(const StatusCallback& callback,
75 scoped_refptr<net::DrainableIOBuffer> buffer, int result);
77 // Flushes the written content in |writer_|.
78 void Flush(const StatusCallback& callback, bool is_eof);
79 void DidFlush(const StatusCallback& callback, bool is_eof, int result);
81 scoped_ptr<storage::FileStreamReader> reader_;
82 scoped_ptr<FileStreamWriter> writer_;
83 const FlushPolicy flush_policy_;
84 FileSystemOperation::CopyFileProgressCallback file_progress_callback_;
85 scoped_refptr<net::IOBufferWithSize> io_buffer_;
86 int64 num_copied_bytes_;
87 int64 previous_flush_offset_;
88 base::Time last_progress_callback_invocation_time_;
89 base::TimeDelta min_progress_callback_invocation_span_;
90 bool cancel_requested_;
91 base::WeakPtrFactory<StreamCopyHelper> weak_factory_;
92 DISALLOW_COPY_AND_ASSIGN(StreamCopyHelper);
95 CopyOrMoveOperationDelegate(FileSystemContext* file_system_context,
96 const FileSystemURL& src_root,
97 const FileSystemURL& dest_root,
98 OperationType operation_type,
99 CopyOrMoveOption option,
100 ErrorBehavior error_behavior,
101 const CopyProgressCallback& progress_callback,
102 const StatusCallback& callback);
103 ~CopyOrMoveOperationDelegate() override;
105 // RecursiveOperationDelegate overrides:
106 void Run() override;
107 void RunRecursively() override;
108 void ProcessFile(const FileSystemURL& url,
109 const StatusCallback& callback) override;
110 void ProcessDirectory(const FileSystemURL& url,
111 const StatusCallback& callback) override;
112 void PostProcessDirectory(const FileSystemURL& url,
113 const StatusCallback& callback) override;
115 protected:
116 void OnCancel() override;
118 private:
119 void DidCopyOrMoveFile(const FileSystemURL& src_url,
120 const FileSystemURL& dest_url,
121 const StatusCallback& callback,
122 CopyOrMoveImpl* impl,
123 base::File::Error error);
124 void DidTryRemoveDestRoot(const StatusCallback& callback,
125 base::File::Error error);
126 void ProcessDirectoryInternal(const FileSystemURL& src_url,
127 const FileSystemURL& dest_url,
128 const StatusCallback& callback);
129 void DidCreateDirectory(const FileSystemURL& src_url,
130 const FileSystemURL& dest_url,
131 const StatusCallback& callback,
132 base::File::Error error);
133 void PostProcessDirectoryAfterGetMetadata(
134 const FileSystemURL& src_url,
135 const StatusCallback& callback,
136 base::File::Error error,
137 const base::File::Info& file_info);
138 void PostProcessDirectoryAfterTouchFile(const FileSystemURL& src_url,
139 const StatusCallback& callback,
140 base::File::Error error);
141 void DidRemoveSourceForMove(const StatusCallback& callback,
142 base::File::Error error);
144 void OnCopyFileProgress(const FileSystemURL& src_url, int64 size);
145 FileSystemURL CreateDestURL(const FileSystemURL& src_url) const;
147 FileSystemURL src_root_;
148 FileSystemURL dest_root_;
149 bool same_file_system_;
150 OperationType operation_type_;
151 CopyOrMoveOption option_;
152 ErrorBehavior error_behavior_;
153 CopyProgressCallback progress_callback_;
154 StatusCallback callback_;
156 std::set<CopyOrMoveImpl*> running_copy_set_;
157 base::WeakPtrFactory<CopyOrMoveOperationDelegate> weak_factory_;
159 DISALLOW_COPY_AND_ASSIGN(CopyOrMoveOperationDelegate);
162 } // namespace storage
164 #endif // STORAGE_BROWSER_FILEAPI_COPY_OR_MOVE_OPERATION_DELEGATE_H_