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/files/file.h"
12 #include "base/files/file_util.h"
13 #include "base/memory/weak_ptr.h"
14 #include "base/observer_list.h"
15 #include "base/sequenced_task_runner.h"
16 #include "base/thread_task_runner_handle.h"
17 #include "storage/browser/fileapi/copy_or_move_file_validator.h"
18 #include "storage/browser/fileapi/file_observers.h"
19 #include "storage/browser/fileapi/file_stream_reader.h"
20 #include "storage/browser/fileapi/file_system_operation.h"
21 #include "storage/browser/fileapi/file_system_operation_context.h"
22 #include "storage/browser/fileapi/file_system_quota_util.h"
23 #include "storage/browser/fileapi/local_file_util.h"
24 #include "storage/browser/fileapi/native_file_util.h"
25 #include "storage/browser/fileapi/quota/quota_reservation.h"
26 #include "storage/browser/fileapi/sandbox_file_stream_writer.h"
27 #include "storage/browser/quota/quota_manager.h"
28 #include "storage/common/fileapi/file_system_util.h"
30 using storage::FileSystemContext
;
31 using storage::FileSystemOperation
;
32 using storage::FileSystemOperationContext
;
33 using storage::FileSystemURL
;
39 // Stub implementation of storage::LocalFileUtil.
40 class TestFileUtil
: public storage::LocalFileUtil
{
42 explicit TestFileUtil(const base::FilePath
& base_path
)
43 : base_path_(base_path
) {}
44 ~TestFileUtil() override
{}
46 // LocalFileUtil overrides.
47 base::File::Error
GetLocalFilePath(FileSystemOperationContext
* context
,
48 const FileSystemURL
& file_system_url
,
49 base::FilePath
* local_file_path
) override
{
50 *local_file_path
= base_path_
.Append(file_system_url
.path());
51 return base::File::FILE_OK
;
55 base::FilePath base_path_
;
60 // This only supports single origin.
61 class TestFileSystemBackend::QuotaUtil
: public storage::FileSystemQuotaUtil
,
62 public storage::FileUpdateObserver
{
64 QuotaUtil() : usage_(0) {}
65 ~QuotaUtil() override
{}
67 // FileSystemQuotaUtil overrides.
68 base::File::Error
DeleteOriginDataOnFileTaskRunner(
69 FileSystemContext
* context
,
70 storage::QuotaManagerProxy
* proxy
,
71 const GURL
& origin_url
,
72 storage::FileSystemType type
) override
{
74 return base::File::FILE_OK
;
77 scoped_refptr
<storage::QuotaReservation
>
78 CreateQuotaReservationOnFileTaskRunner(
79 const GURL
& origin_url
,
80 storage::FileSystemType type
) override
{
82 return scoped_refptr
<storage::QuotaReservation
>();
85 void GetOriginsForTypeOnFileTaskRunner(storage::FileSystemType type
,
86 std::set
<GURL
>* origins
) override
{
90 void GetOriginsForHostOnFileTaskRunner(storage::FileSystemType type
,
91 const std::string
& host
,
92 std::set
<GURL
>* origins
) override
{
96 int64
GetOriginUsageOnFileTaskRunner(FileSystemContext
* context
,
97 const GURL
& origin_url
,
98 storage::FileSystemType type
) override
{
102 // FileUpdateObserver overrides.
103 void OnStartUpdate(const FileSystemURL
& url
) override
{}
104 void OnUpdate(const FileSystemURL
& url
, int64 delta
) override
{
107 void OnEndUpdate(const FileSystemURL
& url
) override
{}
111 DISALLOW_COPY_AND_ASSIGN(QuotaUtil
);
114 TestFileSystemBackend::TestFileSystemBackend(
115 base::SequencedTaskRunner
* task_runner
,
116 const base::FilePath
& base_path
)
117 : base_path_(base_path
),
118 task_runner_(task_runner
),
120 new storage::AsyncFileUtilAdapter(new TestFileUtil(base_path
))),
121 quota_util_(new QuotaUtil
),
122 require_copy_or_move_validator_(false) {
124 update_observers_
.AddObserver(quota_util_
.get(), task_runner_
.get());
127 TestFileSystemBackend::~TestFileSystemBackend() {
130 bool TestFileSystemBackend::CanHandleType(storage::FileSystemType type
) const {
131 return (type
== storage::kFileSystemTypeTest
);
134 void TestFileSystemBackend::Initialize(FileSystemContext
* context
) {
137 void TestFileSystemBackend::ResolveURL(const FileSystemURL
& url
,
138 storage::OpenFileSystemMode mode
,
139 const OpenFileSystemCallback
& callback
) {
140 callback
.Run(GetFileSystemRootURI(url
.origin(), url
.type()),
141 GetFileSystemName(url
.origin(), url
.type()),
142 base::File::FILE_OK
);
145 storage::AsyncFileUtil
* TestFileSystemBackend::GetAsyncFileUtil(
146 storage::FileSystemType type
) {
147 return file_util_
.get();
150 storage::WatcherManager
* TestFileSystemBackend::GetWatcherManager(
151 storage::FileSystemType type
) {
155 storage::CopyOrMoveFileValidatorFactory
*
156 TestFileSystemBackend::GetCopyOrMoveFileValidatorFactory(
157 storage::FileSystemType type
,
158 base::File::Error
* error_code
) {
160 *error_code
= base::File::FILE_OK
;
161 if (require_copy_or_move_validator_
) {
162 if (!copy_or_move_file_validator_factory_
)
163 *error_code
= base::File::FILE_ERROR_SECURITY
;
164 return copy_or_move_file_validator_factory_
.get();
169 void TestFileSystemBackend::InitializeCopyOrMoveFileValidatorFactory(
170 scoped_ptr
<storage::CopyOrMoveFileValidatorFactory
> factory
) {
171 if (!copy_or_move_file_validator_factory_
)
172 copy_or_move_file_validator_factory_
= factory
.Pass();
175 FileSystemOperation
* TestFileSystemBackend::CreateFileSystemOperation(
176 const FileSystemURL
& url
,
177 FileSystemContext
* context
,
178 base::File::Error
* error_code
) const {
179 scoped_ptr
<FileSystemOperationContext
> operation_context(
180 new FileSystemOperationContext(context
));
181 operation_context
->set_update_observers(*GetUpdateObservers(url
.type()));
182 operation_context
->set_change_observers(*GetChangeObservers(url
.type()));
183 return FileSystemOperation::Create(url
, context
, operation_context
.Pass());
186 bool TestFileSystemBackend::SupportsStreaming(
187 const storage::FileSystemURL
& url
) const {
191 bool TestFileSystemBackend::HasInplaceCopyImplementation(
192 storage::FileSystemType type
) const {
196 scoped_ptr
<storage::FileStreamReader
>
197 TestFileSystemBackend::CreateFileStreamReader(
198 const FileSystemURL
& url
,
200 int64 max_bytes_to_read
,
201 const base::Time
& expected_modification_time
,
202 FileSystemContext
* context
) const {
203 return scoped_ptr
<storage::FileStreamReader
>(
204 storage::FileStreamReader::CreateForFileSystemFile(
205 context
, url
, offset
, expected_modification_time
));
208 scoped_ptr
<storage::FileStreamWriter
>
209 TestFileSystemBackend::CreateFileStreamWriter(
210 const FileSystemURL
& url
,
212 FileSystemContext
* context
) const {
213 return scoped_ptr
<storage::FileStreamWriter
>(
214 new storage::SandboxFileStreamWriter(
215 context
, url
, offset
, *GetUpdateObservers(url
.type())));
218 storage::FileSystemQuotaUtil
* TestFileSystemBackend::GetQuotaUtil() {
219 return quota_util_
.get();
222 const storage::UpdateObserverList
* TestFileSystemBackend::GetUpdateObservers(
223 storage::FileSystemType type
) const {
224 return &update_observers_
;
227 const storage::ChangeObserverList
* TestFileSystemBackend::GetChangeObservers(
228 storage::FileSystemType type
) const {
229 return &change_observers_
;
232 const storage::AccessObserverList
* TestFileSystemBackend::GetAccessObservers(
233 storage::FileSystemType type
) const {
237 void TestFileSystemBackend::AddFileChangeObserver(
238 storage::FileChangeObserver
* observer
) {
240 change_observers_
.AddObserver(observer
, task_runner_
.get());
243 } // namespace content