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/storage_export.h"
26 class SequencedTaskRunner
;
30 class SandboxFileSystemBackendDelegateTest
;
31 class SandboxFileSystemTestHelper
;
35 class QuotaManagerProxy
;
36 class SpecialStoragePolicy
;
40 class FileStreamReader
;
46 class FileStreamWriter
;
47 class FileSystemFileUtil
;
48 class FileSystemOperationContext
;
50 class FileSystemUsageCache
;
51 class ObfuscatedFileUtil
;
52 class QuotaReservationManager
;
53 class SandboxFileSystemBackend
;
54 class SandboxQuotaObserver
;
56 // Delegate implementation of the some methods in Sandbox/SyncFileSystemBackend.
57 // An instance of this class is created and owned by FileSystemContext.
58 class STORAGE_EXPORT SandboxFileSystemBackendDelegate
59 : public FileSystemQuotaUtil
{
61 typedef FileSystemBackend::OpenFileSystemCallback OpenFileSystemCallback
;
63 // The FileSystem directory name.
64 static const base::FilePath::CharType kFileSystemDirectory
[];
66 // Origin enumerator interface.
67 // An instance of this interface is assumed to be called on the file thread.
68 class OriginEnumerator
{
70 virtual ~OriginEnumerator() {}
72 // Returns the next origin. Returns empty if there are no more origins.
73 virtual GURL
Next() = 0;
75 // Returns the current origin's information.
76 virtual bool HasFileSystemType(FileSystemType type
) const = 0;
79 // Returns the type directory name in sandbox directory for given |type|.
80 static std::string
GetTypeString(FileSystemType type
);
82 SandboxFileSystemBackendDelegate(
83 storage::QuotaManagerProxy
* quota_manager_proxy
,
84 base::SequencedTaskRunner
* file_task_runner
,
85 const base::FilePath
& profile_path
,
86 storage::SpecialStoragePolicy
* special_storage_policy
,
87 const FileSystemOptions
& file_system_options
);
89 virtual ~SandboxFileSystemBackendDelegate();
91 // Returns an origin enumerator of sandbox filesystem.
92 // This method can only be called on the file thread.
93 OriginEnumerator
* CreateOriginEnumerator();
95 // Gets a base directory path of the sandboxed filesystem that is
96 // specified by |origin_url| and |type|.
97 // (The path is similar to the origin's root path but doesn't contain
98 // the 'unique' part.)
99 // Returns an empty path if the given type is invalid.
100 // This method can only be called on the file thread.
101 base::FilePath
GetBaseDirectoryForOriginAndType(
102 const GURL
& origin_url
,
106 // FileSystemBackend helpers.
108 const GURL
& origin_url
,
110 OpenFileSystemMode mode
,
111 const OpenFileSystemCallback
& callback
,
112 const GURL
& root_url
);
113 scoped_ptr
<FileSystemOperationContext
> CreateFileSystemOperationContext(
114 const FileSystemURL
& url
,
115 FileSystemContext
* context
,
116 base::File::Error
* error_code
) const;
117 scoped_ptr
<storage::FileStreamReader
> CreateFileStreamReader(
118 const FileSystemURL
& url
,
120 const base::Time
& expected_modification_time
,
121 FileSystemContext
* context
) const;
122 scoped_ptr
<FileStreamWriter
> CreateFileStreamWriter(
123 const FileSystemURL
& url
,
125 FileSystemContext
* context
,
126 FileSystemType type
) const;
128 // FileSystemQuotaUtil overrides.
129 virtual base::File::Error
DeleteOriginDataOnFileTaskRunner(
130 FileSystemContext
* context
,
131 storage::QuotaManagerProxy
* proxy
,
132 const GURL
& origin_url
,
133 FileSystemType type
) OVERRIDE
;
134 virtual void GetOriginsForTypeOnFileTaskRunner(
136 std::set
<GURL
>* origins
) OVERRIDE
;
137 virtual void GetOriginsForHostOnFileTaskRunner(
139 const std::string
& host
,
140 std::set
<GURL
>* origins
) OVERRIDE
;
141 virtual int64
GetOriginUsageOnFileTaskRunner(
142 FileSystemContext
* context
,
143 const GURL
& origin_url
,
144 FileSystemType type
) OVERRIDE
;
145 virtual scoped_refptr
<QuotaReservation
>
146 CreateQuotaReservationOnFileTaskRunner(
147 const GURL
& origin_url
,
148 FileSystemType type
) OVERRIDE
;
149 virtual void AddFileUpdateObserver(
151 FileUpdateObserver
* observer
,
152 base::SequencedTaskRunner
* task_runner
) OVERRIDE
;
153 virtual void AddFileChangeObserver(
155 FileChangeObserver
* observer
,
156 base::SequencedTaskRunner
* task_runner
) OVERRIDE
;
157 virtual void AddFileAccessObserver(
159 FileAccessObserver
* observer
,
160 base::SequencedTaskRunner
* task_runner
) OVERRIDE
;
161 virtual const UpdateObserverList
* GetUpdateObservers(
162 FileSystemType type
) const OVERRIDE
;
163 virtual const ChangeObserverList
* GetChangeObservers(
164 FileSystemType type
) const OVERRIDE
;
165 virtual const AccessObserverList
* GetAccessObservers(
166 FileSystemType type
) const OVERRIDE
;
168 // Registers quota observer for file updates on filesystem of |type|.
169 void RegisterQuotaUpdateObserver(FileSystemType type
);
171 void InvalidateUsageCache(const GURL
& origin_url
,
172 FileSystemType type
);
173 void StickyInvalidateUsageCache(const GURL
& origin_url
,
174 FileSystemType type
);
176 void CollectOpenFileSystemMetrics(base::File::Error error_code
);
178 base::SequencedTaskRunner
* file_task_runner() {
179 return file_task_runner_
.get();
182 AsyncFileUtil
* file_util() { return sandbox_file_util_
.get(); }
183 FileSystemUsageCache
* usage_cache() { return file_system_usage_cache_
.get(); }
184 SandboxQuotaObserver
* quota_observer() { return quota_observer_
.get(); }
186 storage::SpecialStoragePolicy
* special_storage_policy() {
187 return special_storage_policy_
.get();
190 const FileSystemOptions
& file_system_options() const {
191 return file_system_options_
;
194 FileSystemFileUtil
* sync_file_util();
197 friend class QuotaBackendImpl
;
198 friend class SandboxQuotaObserver
;
199 friend class content::SandboxFileSystemBackendDelegateTest
;
200 friend class content::SandboxFileSystemTestHelper
;
202 // Performs API-specific validity checks on the given path |url|.
203 // Returns true if access to |url| is valid in this filesystem.
204 bool IsAccessValid(const FileSystemURL
& url
) const;
206 // Returns true if the given |url|'s scheme is allowed to access
208 bool IsAllowedScheme(const GURL
& url
) const;
210 // Returns a path to the usage cache file.
211 base::FilePath
GetUsageCachePathForOriginAndType(
212 const GURL
& origin_url
,
213 FileSystemType type
);
215 // Returns a path to the usage cache file (static version).
216 static base::FilePath
GetUsageCachePathForOriginAndType(
217 ObfuscatedFileUtil
* sandbox_file_util
,
218 const GURL
& origin_url
,
220 base::File::Error
* error_out
);
222 int64
RecalculateUsage(FileSystemContext
* context
,
224 FileSystemType type
);
226 ObfuscatedFileUtil
* obfuscated_file_util();
228 scoped_refptr
<base::SequencedTaskRunner
> file_task_runner_
;
230 scoped_ptr
<AsyncFileUtil
> sandbox_file_util_
;
231 scoped_ptr
<FileSystemUsageCache
> file_system_usage_cache_
;
232 scoped_ptr
<SandboxQuotaObserver
> quota_observer_
;
233 scoped_ptr
<QuotaReservationManager
> quota_reservation_manager_
;
235 scoped_refptr
<storage::SpecialStoragePolicy
> special_storage_policy_
;
237 FileSystemOptions file_system_options_
;
239 bool is_filesystem_opened_
;
240 base::ThreadChecker io_thread_checker_
;
242 // Accessed only on the file thread.
243 std::set
<GURL
> visited_origins_
;
245 std::set
<std::pair
<GURL
, FileSystemType
> > sticky_dirty_origins_
;
247 std::map
<FileSystemType
, UpdateObserverList
> update_observers_
;
248 std::map
<FileSystemType
, ChangeObserverList
> change_observers_
;
249 std::map
<FileSystemType
, AccessObserverList
> access_observers_
;
251 base::Time next_release_time_for_open_filesystem_stat_
;
253 base::WeakPtrFactory
<SandboxFileSystemBackendDelegate
> weak_factory_
;
255 DISALLOW_COPY_AND_ASSIGN(SandboxFileSystemBackendDelegate
);
258 } // namespace storage
260 #endif // WEBKIT_BROWSER_FILEAPI_SANDBOX_FILE_SYSTEM_BACKEND_DELEGATE_H_