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_
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"
26 class SequencedTaskRunner;
30 class QuotaManagerProxy;
31 class SpecialStoragePolicy;
34 namespace webkit_blob {
35 class FileStreamReader;
41 class FileStreamWriter;
42 class FileSystemFileUtil;
43 class FileSystemOperationContext;
45 class FileSystemUsageCache;
46 class ObfuscatedFileUtil;
47 class SandboxFileSystemBackend;
48 class SandboxFileSystemTestHelper;
49 class SandboxQuotaObserver;
51 // Delegate implementation of the some methods in Sandbox/SyncFileSystemBackend.
52 // An instance of this class is created and owned by FileSystemContext.
53 class WEBKIT_STORAGE_BROWSER_EXPORT SandboxFileSystemBackendDelegate
54 : public FileSystemQuotaUtil {
56 typedef FileSystemBackend::OpenFileSystemCallback OpenFileSystemCallback;
58 // The FileSystem directory name.
59 static const base::FilePath::CharType kFileSystemDirectory[];
61 // Origin enumerator interface.
62 // An instance of this interface is assumed to be called on the file thread.
63 class OriginEnumerator {
65 virtual ~OriginEnumerator() {}
67 // Returns the next origin. Returns empty if there are no more origins.
68 virtual GURL Next() = 0;
70 // Returns the current origin's information.
71 virtual bool HasFileSystemType(FileSystemType type) const = 0;
74 // Returns the type directory name in sandbox directory for given |type|.
75 static std::string GetTypeString(FileSystemType type);
77 SandboxFileSystemBackendDelegate(
78 quota::QuotaManagerProxy* quota_manager_proxy,
79 base::SequencedTaskRunner* file_task_runner,
80 const base::FilePath& profile_path,
81 quota::SpecialStoragePolicy* special_storage_policy,
82 const FileSystemOptions& file_system_options);
84 virtual ~SandboxFileSystemBackendDelegate();
86 // Returns an origin enumerator of sandbox filesystem.
87 // This method can only be called on the file thread.
88 OriginEnumerator* CreateOriginEnumerator();
90 // Gets a base directory path of the sandboxed filesystem that is
91 // specified by |origin_url| and |type|.
92 // (The path is similar to the origin's root path but doesn't contain
93 // the 'unique' part.)
94 // Returns an empty path if the given type is invalid.
95 // This method can only be called on the file thread.
96 base::FilePath GetBaseDirectoryForOriginAndType(
97 const GURL& origin_url,
101 // FileSystemBackend helpers.
103 const GURL& origin_url,
105 OpenFileSystemMode mode,
106 const OpenFileSystemCallback& callback,
107 const GURL& root_url);
108 scoped_ptr<FileSystemOperationContext> CreateFileSystemOperationContext(
109 const FileSystemURL& url,
110 FileSystemContext* context,
111 base::PlatformFileError* error_code) const;
112 scoped_ptr<webkit_blob::FileStreamReader> CreateFileStreamReader(
113 const FileSystemURL& url,
115 const base::Time& expected_modification_time,
116 FileSystemContext* context) const;
117 scoped_ptr<FileStreamWriter> CreateFileStreamWriter(
118 const FileSystemURL& url,
120 FileSystemContext* context,
121 FileSystemType type) const;
123 // FileSystemQuotaUtil overrides.
124 virtual base::PlatformFileError DeleteOriginDataOnFileThread(
125 FileSystemContext* context,
126 quota::QuotaManagerProxy* proxy,
127 const GURL& origin_url,
128 FileSystemType type) OVERRIDE;
129 virtual void GetOriginsForTypeOnFileThread(
131 std::set<GURL>* origins) OVERRIDE;
132 virtual void GetOriginsForHostOnFileThread(
134 const std::string& host,
135 std::set<GURL>* origins) OVERRIDE;
136 virtual int64 GetOriginUsageOnFileThread(
137 FileSystemContext* context,
138 const GURL& origin_url,
139 FileSystemType type) OVERRIDE;
140 virtual void AddFileUpdateObserver(
142 FileUpdateObserver* observer,
143 base::SequencedTaskRunner* task_runner) OVERRIDE;
144 virtual void AddFileChangeObserver(
146 FileChangeObserver* observer,
147 base::SequencedTaskRunner* task_runner) OVERRIDE;
148 virtual void AddFileAccessObserver(
150 FileAccessObserver* observer,
151 base::SequencedTaskRunner* task_runner) OVERRIDE;
152 virtual const UpdateObserverList* GetUpdateObservers(
153 FileSystemType type) const OVERRIDE;
154 virtual const ChangeObserverList* GetChangeObservers(
155 FileSystemType type) const OVERRIDE;
156 virtual const AccessObserverList* GetAccessObservers(
157 FileSystemType type) const OVERRIDE;
159 // Registers quota observer for file updates on filesystem of |type|.
160 void RegisterQuotaUpdateObserver(FileSystemType type);
162 void InvalidateUsageCache(const GURL& origin_url,
163 FileSystemType type);
164 void StickyInvalidateUsageCache(const GURL& origin_url,
165 FileSystemType type);
167 void CollectOpenFileSystemMetrics(base::PlatformFileError error_code);
169 base::SequencedTaskRunner* file_task_runner() {
170 return file_task_runner_.get();
173 AsyncFileUtil* file_util() { return sandbox_file_util_.get(); }
174 FileSystemUsageCache* usage_cache() { return file_system_usage_cache_.get(); }
175 SandboxQuotaObserver* quota_observer() { return quota_observer_.get(); }
177 quota::SpecialStoragePolicy* special_storage_policy() {
178 return special_storage_policy_.get();
181 const FileSystemOptions& file_system_options() const {
182 return file_system_options_;
185 FileSystemFileUtil* sync_file_util();
188 friend class SandboxQuotaObserver;
189 friend class SandboxFileSystemTestHelper;
190 FRIEND_TEST_ALL_PREFIXES(SandboxFileSystemBackendDelegateTest, IsAccessValid);
192 // Performs API-specific validity checks on the given path |url|.
193 // Returns true if access to |url| is valid in this filesystem.
194 bool IsAccessValid(const FileSystemURL& url) const;
196 // Returns true if the given |url|'s scheme is allowed to access
198 bool IsAllowedScheme(const GURL& url) const;
200 // Returns a path to the usage cache file.
201 base::FilePath GetUsageCachePathForOriginAndType(
202 const GURL& origin_url,
203 FileSystemType type);
205 // Returns a path to the usage cache file (static version).
206 static base::FilePath GetUsageCachePathForOriginAndType(
207 ObfuscatedFileUtil* sandbox_file_util,
208 const GURL& origin_url,
210 base::PlatformFileError* error_out);
212 int64 RecalculateUsage(FileSystemContext* context,
214 FileSystemType type);
216 ObfuscatedFileUtil* obfuscated_file_util();
218 scoped_refptr<base::SequencedTaskRunner> file_task_runner_;
220 scoped_ptr<AsyncFileUtil> sandbox_file_util_;
221 scoped_ptr<FileSystemUsageCache> file_system_usage_cache_;
222 scoped_ptr<SandboxQuotaObserver> quota_observer_;
224 scoped_refptr<quota::SpecialStoragePolicy> special_storage_policy_;
226 FileSystemOptions file_system_options_;
228 bool is_filesystem_opened_;
229 base::ThreadChecker io_thread_checker_;
231 // Accessed only on the file thread.
232 std::set<GURL> visited_origins_;
234 std::set<std::pair<GURL, FileSystemType> > sticky_dirty_origins_;
236 std::map<FileSystemType, UpdateObserverList> update_observers_;
237 std::map<FileSystemType, ChangeObserverList> change_observers_;
238 std::map<FileSystemType, AccessObserverList> access_observers_;
240 base::Time next_release_time_for_open_filesystem_stat_;
242 base::WeakPtrFactory<SandboxFileSystemBackendDelegate> weak_factory_;
244 DISALLOW_COPY_AND_ASSIGN(SandboxFileSystemBackendDelegate);
247 } // namespace fileapi
249 #endif // WEBKIT_BROWSER_FILEAPI_SANDBOX_FILE_SYSTEM_BACKEND_DELEGATE_H_