Don't preload rarely seen large images
[chromium-blink-merge.git] / storage / browser / fileapi / file_system_operation_impl.h
blob6f4b662e5b6faa0e9e086aa13a74c81af9f16681
1 // Copyright 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_FILE_SYSTEM_OPERATION_IMPL_H_
6 #define STORAGE_BROWSER_FILEAPI_FILE_SYSTEM_OPERATION_IMPL_H_
8 #include <vector>
10 #include "base/memory/ref_counted.h"
11 #include "base/memory/scoped_ptr.h"
12 #include "base/memory/weak_ptr.h"
13 #include "storage/browser/blob/scoped_file.h"
14 #include "storage/browser/fileapi/file_system_operation.h"
15 #include "storage/browser/fileapi/file_system_operation_context.h"
16 #include "storage/browser/fileapi/file_system_url.h"
17 #include "storage/browser/fileapi/file_writer_delegate.h"
18 #include "storage/browser/storage_browser_export.h"
19 #include "storage/common/quota/quota_types.h"
21 namespace storage {
23 class AsyncFileUtil;
24 class FileSystemContext;
25 class RecursiveOperationDelegate;
27 // The default implementation of FileSystemOperation for file systems.
28 class STORAGE_EXPORT FileSystemOperationImpl
29 : public NON_EXPORTED_BASE(FileSystemOperation) {
30 public:
31 ~FileSystemOperationImpl() override;
33 // FileSystemOperation overrides.
34 void CreateFile(const FileSystemURL& url,
35 bool exclusive,
36 const StatusCallback& callback) override;
37 void CreateDirectory(const FileSystemURL& url,
38 bool exclusive,
39 bool recursive,
40 const StatusCallback& callback) override;
41 void Copy(const FileSystemURL& src_url,
42 const FileSystemURL& dest_url,
43 CopyOrMoveOption option,
44 ErrorBehavior error_behavior,
45 const CopyProgressCallback& progress_callback,
46 const StatusCallback& callback) override;
47 void Move(const FileSystemURL& src_url,
48 const FileSystemURL& dest_url,
49 CopyOrMoveOption option,
50 const StatusCallback& callback) override;
51 void DirectoryExists(const FileSystemURL& url,
52 const StatusCallback& callback) override;
53 void FileExists(const FileSystemURL& url,
54 const StatusCallback& callback) override;
55 void GetMetadata(const FileSystemURL& url,
56 const GetMetadataCallback& callback) override;
57 void ReadDirectory(const FileSystemURL& url,
58 const ReadDirectoryCallback& callback) override;
59 void Remove(const FileSystemURL& url,
60 bool recursive,
61 const StatusCallback& callback) override;
62 void Write(const FileSystemURL& url,
63 scoped_ptr<FileWriterDelegate> writer_delegate,
64 scoped_ptr<net::URLRequest> blob_request,
65 const WriteCallback& callback) override;
66 void Truncate(const FileSystemURL& url,
67 int64 length,
68 const StatusCallback& callback) override;
69 void TouchFile(const FileSystemURL& url,
70 const base::Time& last_access_time,
71 const base::Time& last_modified_time,
72 const StatusCallback& callback) override;
73 void OpenFile(const FileSystemURL& url,
74 int file_flags,
75 const OpenFileCallback& callback) override;
76 void Cancel(const StatusCallback& cancel_callback) override;
77 void CreateSnapshotFile(const FileSystemURL& path,
78 const SnapshotFileCallback& callback) override;
79 void CopyInForeignFile(const base::FilePath& src_local_disk_path,
80 const FileSystemURL& dest_url,
81 const StatusCallback& callback) override;
82 void RemoveFile(const FileSystemURL& url,
83 const StatusCallback& callback) override;
84 void RemoveDirectory(const FileSystemURL& url,
85 const StatusCallback& callback) override;
86 void CopyFileLocal(const FileSystemURL& src_url,
87 const FileSystemURL& dest_url,
88 CopyOrMoveOption option,
89 const CopyFileProgressCallback& progress_callback,
90 const StatusCallback& callback) override;
91 void MoveFileLocal(const FileSystemURL& src_url,
92 const FileSystemURL& dest_url,
93 CopyOrMoveOption option,
94 const StatusCallback& callback) override;
95 base::File::Error SyncGetPlatformPath(const FileSystemURL& url,
96 base::FilePath* platform_path) override;
98 FileSystemContext* file_system_context() const {
99 return file_system_context_.get();
102 private:
103 friend class FileSystemOperation;
105 FileSystemOperationImpl(
106 const FileSystemURL& url,
107 FileSystemContext* file_system_context,
108 scoped_ptr<FileSystemOperationContext> operation_context);
110 // Queries the quota and usage and then runs the given |task|.
111 // If an error occurs during the quota query it runs |error_callback| instead.
112 void GetUsageAndQuotaThenRunTask(
113 const FileSystemURL& url,
114 const base::Closure& task,
115 const base::Closure& error_callback);
117 // Called after the quota info is obtained from the quota manager
118 // (which is triggered by GetUsageAndQuotaThenRunTask).
119 // Sets the quota info in the operation_context_ and then runs the given
120 // |task| if the returned quota status is successful, otherwise runs
121 // |error_callback|.
122 void DidGetUsageAndQuotaAndRunTask(const base::Closure& task,
123 const base::Closure& error_callback,
124 storage::QuotaStatusCode status,
125 int64 usage,
126 int64 quota);
128 // The 'body' methods that perform the actual work (i.e. posting the
129 // file task on proxy_) after the quota check.
130 void DoCreateFile(const FileSystemURL& url,
131 const StatusCallback& callback, bool exclusive);
132 void DoCreateDirectory(const FileSystemURL& url,
133 const StatusCallback& callback,
134 bool exclusive,
135 bool recursive);
136 void DoCopyFileLocal(const FileSystemURL& src,
137 const FileSystemURL& dest,
138 CopyOrMoveOption option,
139 const CopyFileProgressCallback& progress_callback,
140 const StatusCallback& callback);
141 void DoMoveFileLocal(const FileSystemURL& src,
142 const FileSystemURL& dest,
143 CopyOrMoveOption option,
144 const StatusCallback& callback);
145 void DoCopyInForeignFile(const base::FilePath& src_local_disk_file_path,
146 const FileSystemURL& dest,
147 const StatusCallback& callback);
148 void DoTruncate(const FileSystemURL& url,
149 const StatusCallback& callback, int64 length);
150 void DoOpenFile(const FileSystemURL& url,
151 const OpenFileCallback& callback, int file_flags);
153 // Callback for CreateFile for |exclusive|=true cases.
154 void DidEnsureFileExistsExclusive(const StatusCallback& callback,
155 base::File::Error rv,
156 bool created);
158 // Callback for CreateFile for |exclusive|=false cases.
159 void DidEnsureFileExistsNonExclusive(const StatusCallback& callback,
160 base::File::Error rv,
161 bool created);
163 void DidFinishOperation(const StatusCallback& callback,
164 base::File::Error rv);
165 void DidDirectoryExists(const StatusCallback& callback,
166 base::File::Error rv,
167 const base::File::Info& file_info);
168 void DidFileExists(const StatusCallback& callback,
169 base::File::Error rv,
170 const base::File::Info& file_info);
171 void DidDeleteRecursively(const FileSystemURL& url,
172 const StatusCallback& callback,
173 base::File::Error rv);
174 void DidWrite(const FileSystemURL& url,
175 const WriteCallback& callback,
176 base::File::Error rv,
177 int64 bytes,
178 FileWriterDelegate::WriteProgressStatus write_status);
180 // Used only for internal assertions.
181 // Returns false if there's another inflight pending operation.
182 bool SetPendingOperationType(OperationType type);
184 scoped_refptr<FileSystemContext> file_system_context_;
186 scoped_ptr<FileSystemOperationContext> operation_context_;
187 AsyncFileUtil* async_file_util_; // Not owned.
189 scoped_ptr<FileWriterDelegate> file_writer_delegate_;
190 scoped_ptr<RecursiveOperationDelegate> recursive_operation_delegate_;
192 StatusCallback cancel_callback_;
194 // A flag to make sure we call operation only once per instance.
195 OperationType pending_operation_;
197 base::WeakPtrFactory<FileSystemOperationImpl> weak_factory_;
199 DISALLOW_COPY_AND_ASSIGN(FileSystemOperationImpl);
202 } // namespace storage
204 #endif // STORAGE_BROWSER_FILEAPI_FILE_SYSTEM_OPERATION_IMPL_H_