FileAPI: Rename *OnFileThread to *OnFileTaskRunner
[chromium-blink-merge.git] / webkit / browser / fileapi / sandbox_file_system_backend_delegate.h
blob7e326b2dd51a47fc214f572dd17bfc23262856e1
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 WEBKIT_BROWSER_FILEAPI_SANDBOX_FILE_SYSTEM_BACKEND_DELEGATE_H_
6 #define WEBKIT_BROWSER_FILEAPI_SANDBOX_FILE_SYSTEM_BACKEND_DELEGATE_H_
8 #include <map>
9 #include <set>
10 #include <string>
11 #include <utility>
13 #include "base/files/file_path.h"
14 #include "base/gtest_prod_util.h"
15 #include "base/memory/ref_counted.h"
16 #include "base/memory/scoped_ptr.h"
17 #include "base/memory/weak_ptr.h"
18 #include "base/threading/thread_checker.h"
19 #include "base/time/time.h"
20 #include "webkit/browser/fileapi/file_system_backend.h"
21 #include "webkit/browser/fileapi/file_system_options.h"
22 #include "webkit/browser/fileapi/file_system_quota_util.h"
23 #include "webkit/browser/webkit_storage_browser_export.h"
25 namespace base {
26 class SequencedTaskRunner;
29 namespace quota {
30 class QuotaManagerProxy;
31 class SpecialStoragePolicy;
34 namespace webkit_blob {
35 class FileStreamReader;
38 namespace fileapi {
40 class AsyncFileUtil;
41 class FileStreamWriter;
42 class FileSystemFileUtil;
43 class FileSystemOperationContext;
44 class FileSystemURL;
45 class FileSystemUsageCache;
46 class ObfuscatedFileUtil;
47 class QuotaReservationManager;
48 class SandboxFileSystemBackend;
49 class SandboxFileSystemTestHelper;
50 class SandboxQuotaObserver;
52 // Delegate implementation of the some methods in Sandbox/SyncFileSystemBackend.
53 // An instance of this class is created and owned by FileSystemContext.
54 class WEBKIT_STORAGE_BROWSER_EXPORT SandboxFileSystemBackendDelegate
55 : public FileSystemQuotaUtil {
56 public:
57 typedef FileSystemBackend::OpenFileSystemCallback OpenFileSystemCallback;
59 // The FileSystem directory name.
60 static const base::FilePath::CharType kFileSystemDirectory[];
62 // Origin enumerator interface.
63 // An instance of this interface is assumed to be called on the file thread.
64 class OriginEnumerator {
65 public:
66 virtual ~OriginEnumerator() {}
68 // Returns the next origin. Returns empty if there are no more origins.
69 virtual GURL Next() = 0;
71 // Returns the current origin's information.
72 virtual bool HasFileSystemType(FileSystemType type) const = 0;
75 // Returns the type directory name in sandbox directory for given |type|.
76 static std::string GetTypeString(FileSystemType type);
78 SandboxFileSystemBackendDelegate(
79 quota::QuotaManagerProxy* quota_manager_proxy,
80 base::SequencedTaskRunner* file_task_runner,
81 const base::FilePath& profile_path,
82 quota::SpecialStoragePolicy* special_storage_policy,
83 const FileSystemOptions& file_system_options);
85 virtual ~SandboxFileSystemBackendDelegate();
87 // Returns an origin enumerator of sandbox filesystem.
88 // This method can only be called on the file thread.
89 OriginEnumerator* CreateOriginEnumerator();
91 // Gets a base directory path of the sandboxed filesystem that is
92 // specified by |origin_url| and |type|.
93 // (The path is similar to the origin's root path but doesn't contain
94 // the 'unique' part.)
95 // Returns an empty path if the given type is invalid.
96 // This method can only be called on the file thread.
97 base::FilePath GetBaseDirectoryForOriginAndType(
98 const GURL& origin_url,
99 FileSystemType type,
100 bool create);
102 // FileSystemBackend helpers.
103 void OpenFileSystem(
104 const GURL& origin_url,
105 FileSystemType type,
106 OpenFileSystemMode mode,
107 const OpenFileSystemCallback& callback,
108 const GURL& root_url);
109 scoped_ptr<FileSystemOperationContext> CreateFileSystemOperationContext(
110 const FileSystemURL& url,
111 FileSystemContext* context,
112 base::PlatformFileError* error_code) const;
113 scoped_ptr<webkit_blob::FileStreamReader> CreateFileStreamReader(
114 const FileSystemURL& url,
115 int64 offset,
116 const base::Time& expected_modification_time,
117 FileSystemContext* context) const;
118 scoped_ptr<FileStreamWriter> CreateFileStreamWriter(
119 const FileSystemURL& url,
120 int64 offset,
121 FileSystemContext* context,
122 FileSystemType type) const;
124 // FileSystemQuotaUtil overrides.
125 virtual base::PlatformFileError DeleteOriginDataOnFileTaskRunner(
126 FileSystemContext* context,
127 quota::QuotaManagerProxy* proxy,
128 const GURL& origin_url,
129 FileSystemType type) OVERRIDE;
130 virtual void GetOriginsForTypeOnFileTaskRunner(
131 FileSystemType type,
132 std::set<GURL>* origins) OVERRIDE;
133 virtual void GetOriginsForHostOnFileTaskRunner(
134 FileSystemType type,
135 const std::string& host,
136 std::set<GURL>* origins) OVERRIDE;
137 virtual int64 GetOriginUsageOnFileTaskRunner(
138 FileSystemContext* context,
139 const GURL& origin_url,
140 FileSystemType type) OVERRIDE;
141 virtual scoped_refptr<QuotaReservation>
142 CreateQuotaReservationOnFileTaskRunner(
143 const GURL& origin_url,
144 FileSystemType type) OVERRIDE;
145 virtual void AddFileUpdateObserver(
146 FileSystemType type,
147 FileUpdateObserver* observer,
148 base::SequencedTaskRunner* task_runner) OVERRIDE;
149 virtual void AddFileChangeObserver(
150 FileSystemType type,
151 FileChangeObserver* observer,
152 base::SequencedTaskRunner* task_runner) OVERRIDE;
153 virtual void AddFileAccessObserver(
154 FileSystemType type,
155 FileAccessObserver* observer,
156 base::SequencedTaskRunner* task_runner) OVERRIDE;
157 virtual const UpdateObserverList* GetUpdateObservers(
158 FileSystemType type) const OVERRIDE;
159 virtual const ChangeObserverList* GetChangeObservers(
160 FileSystemType type) const OVERRIDE;
161 virtual const AccessObserverList* GetAccessObservers(
162 FileSystemType type) const OVERRIDE;
164 // Registers quota observer for file updates on filesystem of |type|.
165 void RegisterQuotaUpdateObserver(FileSystemType type);
167 void InvalidateUsageCache(const GURL& origin_url,
168 FileSystemType type);
169 void StickyInvalidateUsageCache(const GURL& origin_url,
170 FileSystemType type);
172 void CollectOpenFileSystemMetrics(base::PlatformFileError error_code);
174 base::SequencedTaskRunner* file_task_runner() {
175 return file_task_runner_.get();
178 AsyncFileUtil* file_util() { return sandbox_file_util_.get(); }
179 FileSystemUsageCache* usage_cache() { return file_system_usage_cache_.get(); }
180 SandboxQuotaObserver* quota_observer() { return quota_observer_.get(); }
182 quota::SpecialStoragePolicy* special_storage_policy() {
183 return special_storage_policy_.get();
186 const FileSystemOptions& file_system_options() const {
187 return file_system_options_;
190 FileSystemFileUtil* sync_file_util();
192 private:
193 friend class SandboxQuotaObserver;
194 friend class SandboxFileSystemTestHelper;
195 friend class QuotaBackendImpl;
196 FRIEND_TEST_ALL_PREFIXES(SandboxFileSystemBackendDelegateTest, IsAccessValid);
198 // Performs API-specific validity checks on the given path |url|.
199 // Returns true if access to |url| is valid in this filesystem.
200 bool IsAccessValid(const FileSystemURL& url) const;
202 // Returns true if the given |url|'s scheme is allowed to access
203 // filesystem.
204 bool IsAllowedScheme(const GURL& url) const;
206 // Returns a path to the usage cache file.
207 base::FilePath GetUsageCachePathForOriginAndType(
208 const GURL& origin_url,
209 FileSystemType type);
211 // Returns a path to the usage cache file (static version).
212 static base::FilePath GetUsageCachePathForOriginAndType(
213 ObfuscatedFileUtil* sandbox_file_util,
214 const GURL& origin_url,
215 FileSystemType type,
216 base::PlatformFileError* error_out);
218 int64 RecalculateUsage(FileSystemContext* context,
219 const GURL& origin,
220 FileSystemType type);
222 ObfuscatedFileUtil* obfuscated_file_util();
224 scoped_refptr<base::SequencedTaskRunner> file_task_runner_;
226 scoped_ptr<AsyncFileUtil> sandbox_file_util_;
227 scoped_ptr<FileSystemUsageCache> file_system_usage_cache_;
228 scoped_ptr<SandboxQuotaObserver> quota_observer_;
229 scoped_ptr<QuotaReservationManager> quota_reservation_manager_;
231 scoped_refptr<quota::SpecialStoragePolicy> special_storage_policy_;
233 FileSystemOptions file_system_options_;
235 bool is_filesystem_opened_;
236 base::ThreadChecker io_thread_checker_;
238 // Accessed only on the file thread.
239 std::set<GURL> visited_origins_;
241 std::set<std::pair<GURL, FileSystemType> > sticky_dirty_origins_;
243 std::map<FileSystemType, UpdateObserverList> update_observers_;
244 std::map<FileSystemType, ChangeObserverList> change_observers_;
245 std::map<FileSystemType, AccessObserverList> access_observers_;
247 base::Time next_release_time_for_open_filesystem_stat_;
249 base::WeakPtrFactory<SandboxFileSystemBackendDelegate> weak_factory_;
251 DISALLOW_COPY_AND_ASSIGN(SandboxFileSystemBackendDelegate);
254 } // namespace fileapi
256 #endif // WEBKIT_BROWSER_FILEAPI_SANDBOX_FILE_SYSTEM_BACKEND_DELEGATE_H_