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 #include "content/public/test/test_file_system_backend.h"
11 #include "base/file_util.h"
12 #include "base/sequenced_task_runner.h"
13 #include "webkit/browser/blob/file_stream_reader.h"
14 #include "webkit/browser/fileapi/copy_or_move_file_validator.h"
15 #include "webkit/browser/fileapi/file_observers.h"
16 #include "webkit/browser/fileapi/file_system_operation.h"
17 #include "webkit/browser/fileapi/file_system_operation_context.h"
18 #include "webkit/browser/fileapi/file_system_quota_util.h"
19 #include "webkit/browser/fileapi/local_file_util.h"
20 #include "webkit/browser/fileapi/native_file_util.h"
21 #include "webkit/browser/fileapi/quota/quota_reservation.h"
22 #include "webkit/browser/fileapi/sandbox_file_stream_writer.h"
23 #include "webkit/browser/quota/quota_manager.h"
24 #include "webkit/common/fileapi/file_system_util.h"
26 using fileapi::FileSystemContext
;
27 using fileapi::FileSystemOperation
;
28 using fileapi::FileSystemOperationContext
;
29 using fileapi::FileSystemType
;
30 using fileapi::FileSystemURL
;
36 class TestFileUtil
: public fileapi::LocalFileUtil
{
38 explicit TestFileUtil(const base::FilePath
& base_path
)
39 : base_path_(base_path
) {}
40 virtual ~TestFileUtil() {}
42 // LocalFileUtil overrides.
43 virtual base::File::Error
GetLocalFilePath(
44 FileSystemOperationContext
* context
,
45 const FileSystemURL
& file_system_url
,
46 base::FilePath
* local_file_path
) OVERRIDE
{
47 *local_file_path
= base_path_
.Append(file_system_url
.path());
48 return base::File::FILE_OK
;
52 base::FilePath base_path_
;
57 // This only supports single origin.
58 class TestFileSystemBackend::QuotaUtil
59 : public fileapi::FileSystemQuotaUtil
,
60 public fileapi::FileUpdateObserver
{
62 explicit QuotaUtil(base::SequencedTaskRunner
* task_runner
)
64 task_runner_(task_runner
) {
65 update_observers_
= update_observers_
.AddObserver(this, task_runner_
.get());
67 virtual ~QuotaUtil() {}
69 // FileSystemQuotaUtil overrides.
70 virtual base::File::Error
DeleteOriginDataOnFileTaskRunner(
71 FileSystemContext
* context
,
72 quota::QuotaManagerProxy
* proxy
,
73 const GURL
& origin_url
,
74 FileSystemType type
) OVERRIDE
{
76 return base::File::FILE_OK
;
79 virtual scoped_refptr
<fileapi::QuotaReservation
>
80 CreateQuotaReservationOnFileTaskRunner(
81 const GURL
& origin_url
,
82 FileSystemType type
) OVERRIDE
{
84 return scoped_refptr
<fileapi::QuotaReservation
>();
87 virtual void GetOriginsForTypeOnFileTaskRunner(
89 std::set
<GURL
>* origins
) OVERRIDE
{
93 virtual void GetOriginsForHostOnFileTaskRunner(
95 const std::string
& host
,
96 std::set
<GURL
>* origins
) OVERRIDE
{
100 virtual int64
GetOriginUsageOnFileTaskRunner(
101 FileSystemContext
* context
,
102 const GURL
& origin_url
,
103 FileSystemType type
) OVERRIDE
{
107 virtual void AddFileUpdateObserver(
109 FileUpdateObserver
* observer
,
110 base::SequencedTaskRunner
* task_runner
) OVERRIDE
{
114 virtual void AddFileChangeObserver(
116 fileapi::FileChangeObserver
* observer
,
117 base::SequencedTaskRunner
* task_runner
) OVERRIDE
{
118 change_observers_
= change_observers_
.AddObserver(observer
, task_runner
);
121 virtual void AddFileAccessObserver(
123 fileapi::FileAccessObserver
* observer
,
124 base::SequencedTaskRunner
* task_runner
) OVERRIDE
{
128 virtual const fileapi::UpdateObserverList
* GetUpdateObservers(
129 FileSystemType type
) const OVERRIDE
{
130 return &update_observers_
;
133 virtual const fileapi::ChangeObserverList
* GetChangeObservers(
134 FileSystemType type
) const OVERRIDE
{
135 return &change_observers_
;
138 virtual const fileapi::AccessObserverList
* GetAccessObservers(
139 FileSystemType type
) const OVERRIDE
{
143 // FileUpdateObserver overrides.
144 virtual void OnStartUpdate(const FileSystemURL
& url
) OVERRIDE
{}
145 virtual void OnUpdate(const FileSystemURL
& url
, int64 delta
) OVERRIDE
{
148 virtual void OnEndUpdate(const FileSystemURL
& url
) OVERRIDE
{}
150 base::SequencedTaskRunner
* task_runner() { return task_runner_
.get(); }
155 scoped_refptr
<base::SequencedTaskRunner
> task_runner_
;
157 fileapi::UpdateObserverList update_observers_
;
158 fileapi::ChangeObserverList change_observers_
;
161 TestFileSystemBackend::TestFileSystemBackend(
162 base::SequencedTaskRunner
* task_runner
,
163 const base::FilePath
& base_path
)
164 : base_path_(base_path
),
166 new fileapi::AsyncFileUtilAdapter(new TestFileUtil(base_path
))),
167 quota_util_(new QuotaUtil(task_runner
)),
168 require_copy_or_move_validator_(false) {
171 TestFileSystemBackend::~TestFileSystemBackend() {
174 bool TestFileSystemBackend::CanHandleType(FileSystemType type
) const {
175 return (type
== fileapi::kFileSystemTypeTest
);
178 void TestFileSystemBackend::Initialize(FileSystemContext
* context
) {
181 void TestFileSystemBackend::ResolveURL(const FileSystemURL
& url
,
182 fileapi::OpenFileSystemMode mode
,
183 const OpenFileSystemCallback
& callback
) {
184 callback
.Run(GetFileSystemRootURI(url
.origin(), url
.type()),
185 GetFileSystemName(url
.origin(), url
.type()),
186 base::File::FILE_OK
);
189 fileapi::AsyncFileUtil
* TestFileSystemBackend::GetAsyncFileUtil(
190 FileSystemType type
) {
191 return file_util_
.get();
194 fileapi::CopyOrMoveFileValidatorFactory
*
195 TestFileSystemBackend::GetCopyOrMoveFileValidatorFactory(
196 FileSystemType type
, base::File::Error
* error_code
) {
198 *error_code
= base::File::FILE_OK
;
199 if (require_copy_or_move_validator_
) {
200 if (!copy_or_move_file_validator_factory_
)
201 *error_code
= base::File::FILE_ERROR_SECURITY
;
202 return copy_or_move_file_validator_factory_
.get();
207 void TestFileSystemBackend::InitializeCopyOrMoveFileValidatorFactory(
208 scoped_ptr
<fileapi::CopyOrMoveFileValidatorFactory
> factory
) {
209 if (!copy_or_move_file_validator_factory_
)
210 copy_or_move_file_validator_factory_
= factory
.Pass();
213 FileSystemOperation
* TestFileSystemBackend::CreateFileSystemOperation(
214 const FileSystemURL
& url
,
215 FileSystemContext
* context
,
216 base::File::Error
* error_code
) const {
217 scoped_ptr
<FileSystemOperationContext
> operation_context(
218 new FileSystemOperationContext(context
));
219 operation_context
->set_update_observers(*GetUpdateObservers(url
.type()));
220 operation_context
->set_change_observers(
221 *quota_util_
->GetChangeObservers(url
.type()));
222 return FileSystemOperation::Create(url
, context
, operation_context
.Pass());
225 bool TestFileSystemBackend::SupportsStreaming(
226 const fileapi::FileSystemURL
& url
) const {
230 scoped_ptr
<webkit_blob::FileStreamReader
>
231 TestFileSystemBackend::CreateFileStreamReader(
232 const FileSystemURL
& url
,
234 const base::Time
& expected_modification_time
,
235 FileSystemContext
* context
) const {
236 return scoped_ptr
<webkit_blob::FileStreamReader
>(
237 webkit_blob::FileStreamReader::CreateForFileSystemFile(
238 context
, url
, offset
, expected_modification_time
));
241 scoped_ptr
<fileapi::FileStreamWriter
>
242 TestFileSystemBackend::CreateFileStreamWriter(
243 const FileSystemURL
& url
,
245 FileSystemContext
* context
) const {
246 return scoped_ptr
<fileapi::FileStreamWriter
>(
247 new fileapi::SandboxFileStreamWriter(context
, url
, offset
,
248 *GetUpdateObservers(url
.type())));
251 fileapi::FileSystemQuotaUtil
* TestFileSystemBackend::GetQuotaUtil() {
252 return quota_util_
.get();
255 const fileapi::UpdateObserverList
* TestFileSystemBackend::GetUpdateObservers(
256 FileSystemType type
) const {
257 return quota_util_
->GetUpdateObservers(type
);
260 void TestFileSystemBackend::AddFileChangeObserver(
261 fileapi::FileChangeObserver
* observer
) {
262 quota_util_
->AddFileChangeObserver(
263 fileapi::kFileSystemTypeTest
, observer
, quota_util_
->task_runner());
266 } // namespace content