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 ~SandboxFileSystemBackendDelegate() override
;
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 base::File::Error
DeleteOriginDataOnFileTaskRunner(
131 FileSystemContext
* context
,
132 storage::QuotaManagerProxy
* proxy
,
133 const GURL
& origin_url
,
134 FileSystemType type
) override
;
135 void GetOriginsForTypeOnFileTaskRunner(FileSystemType type
,
136 std::set
<GURL
>* origins
) override
;
137 void GetOriginsForHostOnFileTaskRunner(FileSystemType type
,
138 const std::string
& host
,
139 std::set
<GURL
>* origins
) override
;
140 int64
GetOriginUsageOnFileTaskRunner(FileSystemContext
* context
,
141 const GURL
& origin_url
,
142 FileSystemType type
) override
;
143 scoped_refptr
<QuotaReservation
> CreateQuotaReservationOnFileTaskRunner(
144 const GURL
& origin_url
,
145 FileSystemType type
) override
;
147 // Adds an observer for the secified |type| of a file system, bound to
149 virtual void AddFileUpdateObserver(FileSystemType type
,
150 FileUpdateObserver
* observer
,
151 base::SequencedTaskRunner
* task_runner
);
152 virtual void AddFileChangeObserver(FileSystemType type
,
153 FileChangeObserver
* observer
,
154 base::SequencedTaskRunner
* task_runner
);
155 virtual void AddFileAccessObserver(FileSystemType type
,
156 FileAccessObserver
* observer
,
157 base::SequencedTaskRunner
* task_runner
);
159 // Returns observer lists for the specified |type| of a file system.
160 virtual const UpdateObserverList
* GetUpdateObservers(
161 FileSystemType type
) const;
162 virtual const ChangeObserverList
* GetChangeObservers(
163 FileSystemType type
) const;
164 virtual const AccessObserverList
* GetAccessObservers(
165 FileSystemType type
) const;
167 // Registers quota observer for file updates on filesystem of |type|.
168 void RegisterQuotaUpdateObserver(FileSystemType type
);
170 void InvalidateUsageCache(const GURL
& origin_url
,
171 FileSystemType type
);
172 void StickyInvalidateUsageCache(const GURL
& origin_url
,
173 FileSystemType type
);
175 void CollectOpenFileSystemMetrics(base::File::Error error_code
);
177 // Used for migrating from the general storage partition to an isolated
179 void CopyFileSystem(const GURL
& origin_url
,
181 SandboxFileSystemBackendDelegate
* destination
);
183 base::SequencedTaskRunner
* file_task_runner() {
184 return file_task_runner_
.get();
187 AsyncFileUtil
* file_util() { return sandbox_file_util_
.get(); }
188 FileSystemUsageCache
* usage_cache() { return file_system_usage_cache_
.get(); }
189 SandboxQuotaObserver
* quota_observer() { return quota_observer_
.get(); }
191 storage::SpecialStoragePolicy
* special_storage_policy() {
192 return special_storage_policy_
.get();
195 const FileSystemOptions
& file_system_options() const {
196 return file_system_options_
;
199 FileSystemFileUtil
* sync_file_util();
202 friend class QuotaBackendImpl
;
203 friend class SandboxQuotaObserver
;
204 friend class content::SandboxFileSystemBackendDelegateTest
;
205 friend class content::SandboxFileSystemTestHelper
;
207 // Performs API-specific validity checks on the given path |url|.
208 // Returns true if access to |url| is valid in this filesystem.
209 bool IsAccessValid(const FileSystemURL
& url
) const;
211 // Returns true if the given |url|'s scheme is allowed to access
213 bool IsAllowedScheme(const GURL
& url
) const;
215 // Returns a path to the usage cache file.
216 base::FilePath
GetUsageCachePathForOriginAndType(
217 const GURL
& origin_url
,
218 FileSystemType type
);
220 // Returns a path to the usage cache file (static version).
221 static base::FilePath
GetUsageCachePathForOriginAndType(
222 ObfuscatedFileUtil
* sandbox_file_util
,
223 const GURL
& origin_url
,
225 base::File::Error
* error_out
);
227 int64
RecalculateUsage(FileSystemContext
* context
,
229 FileSystemType type
);
231 ObfuscatedFileUtil
* obfuscated_file_util();
233 scoped_refptr
<base::SequencedTaskRunner
> file_task_runner_
;
235 scoped_ptr
<AsyncFileUtil
> sandbox_file_util_
;
236 scoped_ptr
<FileSystemUsageCache
> file_system_usage_cache_
;
237 scoped_ptr
<SandboxQuotaObserver
> quota_observer_
;
238 scoped_ptr
<QuotaReservationManager
> quota_reservation_manager_
;
240 scoped_refptr
<storage::SpecialStoragePolicy
> special_storage_policy_
;
242 FileSystemOptions file_system_options_
;
244 bool is_filesystem_opened_
;
245 base::ThreadChecker io_thread_checker_
;
247 // Accessed only on the file thread.
248 std::set
<GURL
> visited_origins_
;
250 std::set
<std::pair
<GURL
, FileSystemType
> > sticky_dirty_origins_
;
252 std::map
<FileSystemType
, UpdateObserverList
> update_observers_
;
253 std::map
<FileSystemType
, ChangeObserverList
> change_observers_
;
254 std::map
<FileSystemType
, AccessObserverList
> access_observers_
;
256 base::Time next_release_time_for_open_filesystem_stat_
;
258 base::WeakPtrFactory
<SandboxFileSystemBackendDelegate
> weak_factory_
;
260 DISALLOW_COPY_AND_ASSIGN(SandboxFileSystemBackendDelegate
);
263 } // namespace storage
265 #endif // STORAGE_BROWSER_FILEAPI_SANDBOX_FILE_SYSTEM_BACKEND_DELEGATE_H_