net cleanup: Remove unnecessary namespace prefixes.
[chromium-blink-merge.git] / storage / browser / fileapi / copy_or_move_operation_delegate.h
blobe6785e91bcccc156a6902fd6d2d7015d221ba2db
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;
40 enum OperationType {
41 OPERATION_COPY,
42 OPERATION_MOVE
45 // Helper to copy a file by reader and writer streams.
46 // Export for testing.
47 class STORAGE_EXPORT StreamCopyHelper {
48 public:
49 StreamCopyHelper(
50 scoped_ptr<storage::FileStreamReader> reader,
51 scoped_ptr<FileStreamWriter> writer,
52 FlushPolicy flush_policy,
53 int buffer_size,
54 const FileSystemOperation::CopyFileProgressCallback&
55 file_progress_callback,
56 const base::TimeDelta& min_progress_callback_invocation_span);
57 ~StreamCopyHelper();
59 void Run(const StatusCallback& callback);
61 // Requests cancelling. After the cancelling is done, |callback| passed to
62 // Run will be called.
63 void Cancel();
65 private:
66 // Reads the content from the |reader_|.
67 void Read(const StatusCallback& callback);
68 void DidRead(const StatusCallback& callback, int result);
70 // Writes the content in |buffer| to |writer_|.
71 void Write(const StatusCallback& callback,
72 scoped_refptr<net::DrainableIOBuffer> buffer);
73 void DidWrite(const StatusCallback& callback,
74 scoped_refptr<net::DrainableIOBuffer> buffer, int result);
76 // Flushes the written content in |writer_|.
77 void Flush(const StatusCallback& callback, bool is_eof);
78 void DidFlush(const StatusCallback& callback, bool is_eof, int result);
80 scoped_ptr<storage::FileStreamReader> reader_;
81 scoped_ptr<FileStreamWriter> writer_;
82 const FlushPolicy flush_policy_;
83 FileSystemOperation::CopyFileProgressCallback file_progress_callback_;
84 scoped_refptr<net::IOBufferWithSize> io_buffer_;
85 int64 num_copied_bytes_;
86 int64 previous_flush_offset_;
87 base::Time last_progress_callback_invocation_time_;
88 base::TimeDelta min_progress_callback_invocation_span_;
89 bool cancel_requested_;
90 base::WeakPtrFactory<StreamCopyHelper> weak_factory_;
91 DISALLOW_COPY_AND_ASSIGN(StreamCopyHelper);
94 CopyOrMoveOperationDelegate(
95 FileSystemContext* file_system_context,
96 const FileSystemURL& src_root,
97 const FileSystemURL& dest_root,
98 OperationType operation_type,
99 CopyOrMoveOption option,
100 const CopyProgressCallback& progress_callback,
101 const StatusCallback& callback);
102 ~CopyOrMoveOperationDelegate() override;
104 // RecursiveOperationDelegate overrides:
105 void Run() override;
106 void RunRecursively() override;
107 void ProcessFile(const FileSystemURL& url,
108 const StatusCallback& callback) override;
109 void ProcessDirectory(const FileSystemURL& url,
110 const StatusCallback& callback) override;
111 void PostProcessDirectory(const FileSystemURL& url,
112 const StatusCallback& callback) override;
114 protected:
115 void OnCancel() override;
117 private:
118 void DidCopyOrMoveFile(const FileSystemURL& src_url,
119 const FileSystemURL& dest_url,
120 const StatusCallback& callback,
121 CopyOrMoveImpl* impl,
122 base::File::Error error);
123 void DidTryRemoveDestRoot(const StatusCallback& callback,
124 base::File::Error error);
125 void ProcessDirectoryInternal(const FileSystemURL& src_url,
126 const FileSystemURL& dest_url,
127 const StatusCallback& callback);
128 void DidCreateDirectory(const FileSystemURL& src_url,
129 const FileSystemURL& dest_url,
130 const StatusCallback& callback,
131 base::File::Error error);
132 void PostProcessDirectoryAfterGetMetadata(
133 const FileSystemURL& src_url,
134 const StatusCallback& callback,
135 base::File::Error error,
136 const base::File::Info& file_info);
137 void PostProcessDirectoryAfterTouchFile(const FileSystemURL& src_url,
138 const StatusCallback& callback,
139 base::File::Error error);
140 void DidRemoveSourceForMove(const StatusCallback& callback,
141 base::File::Error error);
143 void OnCopyFileProgress(const FileSystemURL& src_url, int64 size);
144 FileSystemURL CreateDestURL(const FileSystemURL& src_url) const;
146 FileSystemURL src_root_;
147 FileSystemURL dest_root_;
148 bool same_file_system_;
149 OperationType operation_type_;
150 CopyOrMoveOption option_;
151 CopyProgressCallback progress_callback_;
152 StatusCallback callback_;
154 std::set<CopyOrMoveImpl*> running_copy_set_;
155 base::WeakPtrFactory<CopyOrMoveOperationDelegate> weak_factory_;
157 DISALLOW_COPY_AND_ASSIGN(CopyOrMoveOperationDelegate);
160 } // namespace storage
162 #endif // STORAGE_BROWSER_FILEAPI_COPY_OR_MOVE_OPERATION_DELEGATE_H_