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_SANDBOX_FILE_SYSTEM_BACKEND_DELEGATE_H_
6 #define STORAGE_BROWSER_FILEAPI_SANDBOX_FILE_SYSTEM_BACKEND_DELEGATE_H_
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 "storage/browser/fileapi/file_system_backend.h"
21 #include "storage/browser/fileapi/file_system_options.h"
22 #include "storage/browser/fileapi/file_system_quota_util.h"
23 #include "storage/browser/fileapi/task_runner_bound_observer_list.h"
24 #include "storage/browser/storage_browser_export.h"
27 class SequencedTaskRunner;
31 class SandboxFileSystemBackendDelegateTest;
32 class SandboxFileSystemTestHelper;
36 class QuotaManagerProxy;
37 class SpecialStoragePolicy;
41 class FileStreamReader;
47 class FileStreamWriter;
48 class FileSystemFileUtil;
49 class FileSystemOperationContext;
51 class FileSystemUsageCache;
52 class ObfuscatedFileUtil;
53 class QuotaReservationManager;
54 class SandboxFileSystemBackend;
55 class SandboxQuotaObserver;
57 // Delegate implementation of the some methods in Sandbox/SyncFileSystemBackend.
58 // An instance of this class is created and owned by FileSystemContext.
59 class STORAGE_EXPORT SandboxFileSystemBackendDelegate
60 : public FileSystemQuotaUtil {
62 typedef FileSystemBackend::OpenFileSystemCallback OpenFileSystemCallback;
64 // The FileSystem directory name.
65 static const base::FilePath::CharType kFileSystemDirectory[];
67 // Origin enumerator interface.
68 // An instance of this interface is assumed to be called on the file thread.
69 class OriginEnumerator {
71 virtual ~OriginEnumerator() {}
73 // Returns the next origin. Returns empty if there are no more origins.
74 virtual GURL Next() = 0;
76 // Returns the current origin's information.
77 virtual bool HasFileSystemType(FileSystemType type) const = 0;
80 // Returns the type directory name in sandbox directory for given |type|.
81 static std::string GetTypeString(FileSystemType type);
83 SandboxFileSystemBackendDelegate(
84 storage::QuotaManagerProxy* quota_manager_proxy,
85 base::SequencedTaskRunner* file_task_runner,
86 const base::FilePath& profile_path,
87 storage::SpecialStoragePolicy* special_storage_policy,
88 const FileSystemOptions& file_system_options);
90 virtual ~SandboxFileSystemBackendDelegate();
92 // Returns an origin enumerator of sandbox filesystem.
93 // This method can only be called on the file thread.
94 OriginEnumerator* CreateOriginEnumerator();
96 // Gets a base directory path of the sandboxed filesystem that is
97 // specified by |origin_url| and |type|.
98 // (The path is similar to the origin's root path but doesn't contain
99 // the 'unique' part.)
100 // Returns an empty path if the given type is invalid.
101 // This method can only be called on the file thread.
102 base::FilePath GetBaseDirectoryForOriginAndType(
103 const GURL& origin_url,
107 // FileSystemBackend helpers.
109 const GURL& origin_url,
111 OpenFileSystemMode mode,
112 const OpenFileSystemCallback& callback,
113 const GURL& root_url);
114 scoped_ptr<FileSystemOperationContext> CreateFileSystemOperationContext(
115 const FileSystemURL& url,
116 FileSystemContext* context,
117 base::File::Error* error_code) const;
118 scoped_ptr<storage::FileStreamReader> CreateFileStreamReader(
119 const FileSystemURL& url,
121 const base::Time& expected_modification_time,
122 FileSystemContext* context) const;
123 scoped_ptr<FileStreamWriter> CreateFileStreamWriter(
124 const FileSystemURL& url,
126 FileSystemContext* context,
127 FileSystemType type) const;
129 // FileSystemQuotaUtil overrides.
130 virtual base::File::Error DeleteOriginDataOnFileTaskRunner(
131 FileSystemContext* context,
132 storage::QuotaManagerProxy* proxy,
133 const GURL& origin_url,
134 FileSystemType type) OVERRIDE;
135 virtual void GetOriginsForTypeOnFileTaskRunner(
137 std::set<GURL>* origins) OVERRIDE;
138 virtual void GetOriginsForHostOnFileTaskRunner(
140 const std::string& host,
141 std::set<GURL>* origins) OVERRIDE;
142 virtual int64 GetOriginUsageOnFileTaskRunner(
143 FileSystemContext* context,
144 const GURL& origin_url,
145 FileSystemType type) OVERRIDE;
146 virtual scoped_refptr<QuotaReservation>
147 CreateQuotaReservationOnFileTaskRunner(
148 const GURL& origin_url,
149 FileSystemType type) OVERRIDE;
151 // Adds an observer for the secified |type| of a file system, bound to
153 virtual void AddFileUpdateObserver(FileSystemType type,
154 FileUpdateObserver* observer,
155 base::SequencedTaskRunner* task_runner);
156 virtual void AddFileChangeObserver(FileSystemType type,
157 FileChangeObserver* observer,
158 base::SequencedTaskRunner* task_runner);
159 virtual void AddFileAccessObserver(FileSystemType type,
160 FileAccessObserver* observer,
161 base::SequencedTaskRunner* task_runner);
163 // Returns observer lists for the specified |type| of a file system.
164 virtual const UpdateObserverList* GetUpdateObservers(
165 FileSystemType type) const;
166 virtual const ChangeObserverList* GetChangeObservers(
167 FileSystemType type) const;
168 virtual const AccessObserverList* GetAccessObservers(
169 FileSystemType type) const;
171 // Registers quota observer for file updates on filesystem of |type|.
172 void RegisterQuotaUpdateObserver(FileSystemType type);
174 void InvalidateUsageCache(const GURL& origin_url,
175 FileSystemType type);
176 void StickyInvalidateUsageCache(const GURL& origin_url,
177 FileSystemType type);
179 void CollectOpenFileSystemMetrics(base::File::Error error_code);
181 base::SequencedTaskRunner* file_task_runner() {
182 return file_task_runner_.get();
185 AsyncFileUtil* file_util() { return sandbox_file_util_.get(); }
186 FileSystemUsageCache* usage_cache() { return file_system_usage_cache_.get(); }
187 SandboxQuotaObserver* quota_observer() { return quota_observer_.get(); }
189 storage::SpecialStoragePolicy* special_storage_policy() {
190 return special_storage_policy_.get();
193 const FileSystemOptions& file_system_options() const {
194 return file_system_options_;
197 FileSystemFileUtil* sync_file_util();
200 friend class QuotaBackendImpl;
201 friend class SandboxQuotaObserver;
202 friend class content::SandboxFileSystemBackendDelegateTest;
203 friend class content::SandboxFileSystemTestHelper;
205 // Performs API-specific validity checks on the given path |url|.
206 // Returns true if access to |url| is valid in this filesystem.
207 bool IsAccessValid(const FileSystemURL& url) const;
209 // Returns true if the given |url|'s scheme is allowed to access
211 bool IsAllowedScheme(const GURL& url) const;
213 // Returns a path to the usage cache file.
214 base::FilePath GetUsageCachePathForOriginAndType(
215 const GURL& origin_url,
216 FileSystemType type);
218 // Returns a path to the usage cache file (static version).
219 static base::FilePath GetUsageCachePathForOriginAndType(
220 ObfuscatedFileUtil* sandbox_file_util,
221 const GURL& origin_url,
223 base::File::Error* error_out);
225 int64 RecalculateUsage(FileSystemContext* context,
227 FileSystemType type);
229 ObfuscatedFileUtil* obfuscated_file_util();
231 scoped_refptr<base::SequencedTaskRunner> file_task_runner_;
233 scoped_ptr<AsyncFileUtil> sandbox_file_util_;
234 scoped_ptr<FileSystemUsageCache> file_system_usage_cache_;
235 scoped_ptr<SandboxQuotaObserver> quota_observer_;
236 scoped_ptr<QuotaReservationManager> quota_reservation_manager_;
238 scoped_refptr<storage::SpecialStoragePolicy> special_storage_policy_;
240 FileSystemOptions file_system_options_;
242 bool is_filesystem_opened_;
243 base::ThreadChecker io_thread_checker_;
245 // Accessed only on the file thread.
246 std::set<GURL> visited_origins_;
248 std::set<std::pair<GURL, FileSystemType> > sticky_dirty_origins_;
250 std::map<FileSystemType, UpdateObserverList> update_observers_;
251 std::map<FileSystemType, ChangeObserverList> change_observers_;
252 std::map<FileSystemType, AccessObserverList> access_observers_;
254 base::Time next_release_time_for_open_filesystem_stat_;
256 base::WeakPtrFactory<SandboxFileSystemBackendDelegate> weak_factory_;
258 DISALLOW_COPY_AND_ASSIGN(SandboxFileSystemBackendDelegate);
261 } // namespace storage
263 #endif // STORAGE_BROWSER_FILEAPI_SANDBOX_FILE_SYSTEM_BACKEND_DELEGATE_H_