blob: a6b0276686866e5b45ddd81cd5ae135f88960daa [file] [log] [blame]
[email protected]d77ecbf62013-10-02 06:43:081// Copyright 2013 The Chromium Authors. All rights reserved.
[email protected]6faad822012-05-11 12:58:292// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
[email protected]d77ecbf62013-10-02 06:43:085#include "content/public/test/test_file_system_backend.h"
[email protected]6faad822012-05-11 12:58:296
7#include <set>
8#include <string>
9#include <vector>
10
11#include "base/file_util.h"
[email protected]fb441962013-05-08 05:35:2412#include "base/sequenced_task_runner.h"
[email protected]b8f2f422013-09-12 17:38:3913#include "webkit/browser/blob/file_stream_reader.h"
[email protected]20c2df62013-05-21 08:26:3614#include "webkit/browser/fileapi/copy_or_move_file_validator.h"
[email protected]f25e1132013-05-24 13:58:0415#include "webkit/browser/fileapi/file_observers.h"
[email protected]bd69f572013-09-09 06:15:2316#include "webkit/browser/fileapi/file_system_operation.h"
[email protected]c6f9203a2013-05-28 02:08:0717#include "webkit/browser/fileapi/file_system_operation_context.h"
[email protected]20c2df62013-05-21 08:26:3618#include "webkit/browser/fileapi/file_system_quota_util.h"
[email protected]5a20d042013-05-22 12:54:1819#include "webkit/browser/fileapi/local_file_util.h"
20#include "webkit/browser/fileapi/native_file_util.h"
[email protected]6372a702013-11-12 18:09:0721#include "webkit/browser/fileapi/quota/quota_reservation.h"
[email protected]28f051c32013-05-21 05:15:2622#include "webkit/browser/fileapi/sandbox_file_stream_writer.h"
[email protected]7660ec92013-05-30 05:12:3923#include "webkit/browser/quota/quota_manager.h"
[email protected]61d271f32013-05-28 04:59:2324#include "webkit/common/fileapi/file_system_util.h"
[email protected]6faad822012-05-11 12:58:2925
[email protected]7ade66ee2014-01-16 17:35:5326using fileapi::FileSystemContext;
27using fileapi::FileSystemOperation;
28using fileapi::FileSystemOperationContext;
29using fileapi::FileSystemType;
30using fileapi::FileSystemURL;
31
32namespace content {
[email protected]6faad822012-05-11 12:58:2933
[email protected]cb1e98a2013-09-12 11:09:0634namespace {
35
[email protected]7ade66ee2014-01-16 17:35:5336class TestFileUtil : public fileapi::LocalFileUtil {
[email protected]cb1e98a2013-09-12 11:09:0637 public:
38 explicit TestFileUtil(const base::FilePath& base_path)
39 : base_path_(base_path) {}
40 virtual ~TestFileUtil() {}
41
42 // LocalFileUtil overrides.
[email protected]141bcc52014-01-27 21:36:0043 virtual base::File::Error GetLocalFilePath(
[email protected]cb1e98a2013-09-12 11:09:0644 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());
[email protected]141bcc52014-01-27 21:36:0048 return base::File::FILE_OK;
[email protected]cb1e98a2013-09-12 11:09:0649 }
50
51 private:
52 base::FilePath base_path_;
53};
54
55} // namespace
56
[email protected]6faad822012-05-11 12:58:2957// This only supports single origin.
[email protected]f19bbf62013-07-09 01:22:3258class TestFileSystemBackend::QuotaUtil
[email protected]7ade66ee2014-01-16 17:35:5359 : public fileapi::FileSystemQuotaUtil,
60 public fileapi::FileUpdateObserver {
[email protected]6faad822012-05-11 12:58:2961 public:
[email protected]6372a702013-11-12 18:09:0762 explicit QuotaUtil(base::SequencedTaskRunner* task_runner)
[email protected]3c2f54642013-07-12 05:14:0963 : usage_(0),
64 task_runner_(task_runner) {
[email protected]072865b2013-07-12 07:55:0865 update_observers_ = update_observers_.AddObserver(this, task_runner_.get());
[email protected]3c2f54642013-07-12 05:14:0966 }
[email protected]caf66702012-09-07 07:02:2067 virtual ~QuotaUtil() {}
[email protected]6faad822012-05-11 12:58:2968
[email protected]caf66702012-09-07 07:02:2069 // FileSystemQuotaUtil overrides.
[email protected]141bcc52014-01-27 21:36:0070 virtual base::File::Error DeleteOriginDataOnFileTaskRunner(
[email protected]2a030ee2013-06-18 15:16:4871 FileSystemContext* context,
72 quota::QuotaManagerProxy* proxy,
73 const GURL& origin_url,
74 FileSystemType type) OVERRIDE {
75 NOTREACHED();
[email protected]141bcc52014-01-27 21:36:0076 return base::File::FILE_OK;
[email protected]2a030ee2013-06-18 15:16:4877 }
[email protected]3c2f54642013-07-12 05:14:0978
[email protected]7ade66ee2014-01-16 17:35:5379 virtual scoped_refptr<fileapi::QuotaReservation>
[email protected]6372a702013-11-12 18:09:0780 CreateQuotaReservationOnFileTaskRunner(
81 const GURL& origin_url,
82 FileSystemType type) OVERRIDE {
83 NOTREACHED();
[email protected]7ade66ee2014-01-16 17:35:5384 return scoped_refptr<fileapi::QuotaReservation>();
[email protected]6372a702013-11-12 18:09:0785 }
86
[email protected]c4ba7982013-12-20 12:27:3887 virtual void GetOriginsForTypeOnFileTaskRunner(
[email protected]6faad822012-05-11 12:58:2988 FileSystemType type,
89 std::set<GURL>* origins) OVERRIDE {
90 NOTREACHED();
91 }
[email protected]3c2f54642013-07-12 05:14:0992
[email protected]c4ba7982013-12-20 12:27:3893 virtual void GetOriginsForHostOnFileTaskRunner(
[email protected]6faad822012-05-11 12:58:2994 FileSystemType type,
95 const std::string& host,
96 std::set<GURL>* origins) OVERRIDE {
97 NOTREACHED();
98 }
[email protected]3c2f54642013-07-12 05:14:0999
[email protected]c4ba7982013-12-20 12:27:38100 virtual int64 GetOriginUsageOnFileTaskRunner(
[email protected]22894522012-05-23 07:14:58101 FileSystemContext* context,
[email protected]6faad822012-05-11 12:58:29102 const GURL& origin_url,
103 FileSystemType type) OVERRIDE {
104 return usage_;
105 }
[email protected]3c2f54642013-07-12 05:14:09106
[email protected]3c2f54642013-07-12 05:14:09107 virtual void AddFileUpdateObserver(
108 FileSystemType type,
109 FileUpdateObserver* observer,
110 base::SequencedTaskRunner* task_runner) OVERRIDE {
111 NOTIMPLEMENTED();
112 }
113
114 virtual void AddFileChangeObserver(
115 FileSystemType type,
[email protected]7ade66ee2014-01-16 17:35:53116 fileapi::FileChangeObserver* observer,
[email protected]3c2f54642013-07-12 05:14:09117 base::SequencedTaskRunner* task_runner) OVERRIDE {
[email protected]072865b2013-07-12 07:55:08118 change_observers_ = change_observers_.AddObserver(observer, task_runner);
[email protected]3c2f54642013-07-12 05:14:09119 }
120
121 virtual void AddFileAccessObserver(
122 FileSystemType type,
[email protected]7ade66ee2014-01-16 17:35:53123 fileapi::FileAccessObserver* observer,
[email protected]3c2f54642013-07-12 05:14:09124 base::SequencedTaskRunner* task_runner) OVERRIDE {
125 NOTIMPLEMENTED();
126 }
127
[email protected]7ade66ee2014-01-16 17:35:53128 virtual const fileapi::UpdateObserverList* GetUpdateObservers(
[email protected]3c2f54642013-07-12 05:14:09129 FileSystemType type) const OVERRIDE {
130 return &update_observers_;
131 }
132
[email protected]7ade66ee2014-01-16 17:35:53133 virtual const fileapi::ChangeObserverList* GetChangeObservers(
[email protected]3c2f54642013-07-12 05:14:09134 FileSystemType type) const OVERRIDE {
135 return &change_observers_;
136 }
137
[email protected]7ade66ee2014-01-16 17:35:53138 virtual const fileapi::AccessObserverList* GetAccessObservers(
[email protected]3c2f54642013-07-12 05:14:09139 FileSystemType type) const OVERRIDE {
140 NOTIMPLEMENTED();
141 return NULL;
142 }
143
[email protected]caf66702012-09-07 07:02:20144 // FileUpdateObserver overrides.
145 virtual void OnStartUpdate(const FileSystemURL& url) OVERRIDE {}
146 virtual void OnUpdate(const FileSystemURL& url, int64 delta) OVERRIDE {
147 usage_ += delta;
148 }
149 virtual void OnEndUpdate(const FileSystemURL& url) OVERRIDE {}
150
[email protected]3c2f54642013-07-12 05:14:09151 base::SequencedTaskRunner* task_runner() { return task_runner_.get(); }
152
[email protected]6faad822012-05-11 12:58:29153 private:
154 int64 usage_;
[email protected]3c2f54642013-07-12 05:14:09155
156 scoped_refptr<base::SequencedTaskRunner> task_runner_;
157
[email protected]7ade66ee2014-01-16 17:35:53158 fileapi::UpdateObserverList update_observers_;
159 fileapi::ChangeObserverList change_observers_;
[email protected]6faad822012-05-11 12:58:29160};
161
[email protected]f19bbf62013-07-09 01:22:32162TestFileSystemBackend::TestFileSystemBackend(
[email protected]6faad822012-05-11 12:58:29163 base::SequencedTaskRunner* task_runner,
[email protected]a3ef4832013-02-02 05:12:33164 const base::FilePath& base_path)
[email protected]6faad822012-05-11 12:58:29165 : base_path_(base_path),
[email protected]7ade66ee2014-01-16 17:35:53166 file_util_(
167 new fileapi::AsyncFileUtilAdapter(new TestFileUtil(base_path))),
[email protected]3c2f54642013-07-12 05:14:09168 quota_util_(new QuotaUtil(task_runner)),
[email protected]c3d638b32013-04-22 02:18:49169 require_copy_or_move_validator_(false) {
[email protected]6faad822012-05-11 12:58:29170}
171
[email protected]f19bbf62013-07-09 01:22:32172TestFileSystemBackend::~TestFileSystemBackend() {
[email protected]6faad822012-05-11 12:58:29173}
174
[email protected]f19bbf62013-07-09 01:22:32175bool TestFileSystemBackend::CanHandleType(FileSystemType type) const {
[email protected]7ade66ee2014-01-16 17:35:53176 return (type == fileapi::kFileSystemTypeTest);
[email protected]420fb562013-04-18 01:46:34177}
178
[email protected]3fc17aed2013-07-24 10:01:50179void TestFileSystemBackend::Initialize(FileSystemContext* context) {
180}
181
182void TestFileSystemBackend::OpenFileSystem(
[email protected]6faad822012-05-11 12:58:29183 const GURL& origin_url,
184 FileSystemType type,
[email protected]7ade66ee2014-01-16 17:35:53185 fileapi::OpenFileSystemMode mode,
[email protected]3fc17aed2013-07-24 10:01:50186 const OpenFileSystemCallback& callback) {
[email protected]a836883d2013-07-12 03:50:02187 callback.Run(GetFileSystemRootURI(origin_url, type),
188 GetFileSystemName(origin_url, type),
[email protected]141bcc52014-01-27 21:36:00189 base::File::FILE_OK);
[email protected]6faad822012-05-11 12:58:29190}
191
[email protected]7ade66ee2014-01-16 17:35:53192fileapi::AsyncFileUtil* TestFileSystemBackend::GetAsyncFileUtil(
193 FileSystemType type) {
[email protected]cb1e98a2013-09-12 11:09:06194 return file_util_.get();
[email protected]6faad822012-05-11 12:58:29195}
196
[email protected]7ade66ee2014-01-16 17:35:53197fileapi::CopyOrMoveFileValidatorFactory*
[email protected]f19bbf62013-07-09 01:22:32198TestFileSystemBackend::GetCopyOrMoveFileValidatorFactory(
[email protected]141bcc52014-01-27 21:36:00199 FileSystemType type, base::File::Error* error_code) {
[email protected]98407ee2013-04-04 08:52:17200 DCHECK(error_code);
[email protected]141bcc52014-01-27 21:36:00201 *error_code = base::File::FILE_OK;
[email protected]c3d638b32013-04-22 02:18:49202 if (require_copy_or_move_validator_) {
203 if (!copy_or_move_file_validator_factory_)
[email protected]141bcc52014-01-27 21:36:00204 *error_code = base::File::FILE_ERROR_SECURITY;
[email protected]c3d638b32013-04-22 02:18:49205 return copy_or_move_file_validator_factory_.get();
206 }
[email protected]98407ee2013-04-04 08:52:17207 return NULL;
208}
209
[email protected]f19bbf62013-07-09 01:22:32210void TestFileSystemBackend::InitializeCopyOrMoveFileValidatorFactory(
[email protected]7ade66ee2014-01-16 17:35:53211 scoped_ptr<fileapi::CopyOrMoveFileValidatorFactory> factory) {
[email protected]c3d638b32013-04-22 02:18:49212 if (!copy_or_move_file_validator_factory_)
213 copy_or_move_file_validator_factory_ = factory.Pass();
[email protected]98407ee2013-04-04 08:52:17214}
215
[email protected]f19bbf62013-07-09 01:22:32216FileSystemOperation* TestFileSystemBackend::CreateFileSystemOperation(
[email protected]949f25a2012-06-27 01:53:09217 const FileSystemURL& url,
[email protected]d23a00cc2012-09-11 17:38:13218 FileSystemContext* context,
[email protected]141bcc52014-01-27 21:36:00219 base::File::Error* error_code) const {
[email protected]75af1a42012-07-26 04:06:20220 scoped_ptr<FileSystemOperationContext> operation_context(
221 new FileSystemOperationContext(context));
[email protected]3c2f54642013-07-12 05:14:09222 operation_context->set_update_observers(*GetUpdateObservers(url.type()));
223 operation_context->set_change_observers(
224 *quota_util_->GetChangeObservers(url.type()));
[email protected]bd69f572013-09-09 06:15:23225 return FileSystemOperation::Create(url, context, operation_context.Pass());
[email protected]6faad822012-05-11 12:58:29226}
227
[email protected]3ee9eb02013-04-10 09:17:05228scoped_ptr<webkit_blob::FileStreamReader>
[email protected]f19bbf62013-07-09 01:22:32229TestFileSystemBackend::CreateFileStreamReader(
[email protected]949f25a2012-06-27 01:53:09230 const FileSystemURL& url,
[email protected]6faad822012-05-11 12:58:29231 int64 offset,
[email protected]a1057832012-10-15 13:28:06232 const base::Time& expected_modification_time,
[email protected]6faad822012-05-11 12:58:29233 FileSystemContext* context) const {
[email protected]3ee9eb02013-04-10 09:17:05234 return scoped_ptr<webkit_blob::FileStreamReader>(
[email protected]b8f2f422013-09-12 17:38:39235 webkit_blob::FileStreamReader::CreateForFileSystemFile(
[email protected]3ee9eb02013-04-10 09:17:05236 context, url, offset, expected_modification_time));
[email protected]6faad822012-05-11 12:58:29237}
238
[email protected]3ee9eb02013-04-10 09:17:05239scoped_ptr<fileapi::FileStreamWriter>
[email protected]f19bbf62013-07-09 01:22:32240TestFileSystemBackend::CreateFileStreamWriter(
[email protected]949f25a2012-06-27 01:53:09241 const FileSystemURL& url,
[email protected]7e84b912012-05-16 04:42:01242 int64 offset,
243 FileSystemContext* context) const {
[email protected]3ee9eb02013-04-10 09:17:05244 return scoped_ptr<fileapi::FileStreamWriter>(
[email protected]7ade66ee2014-01-16 17:35:53245 new fileapi::SandboxFileStreamWriter(context, url, offset,
246 *GetUpdateObservers(url.type())));
[email protected]7e84b912012-05-16 04:42:01247}
248
[email protected]7ade66ee2014-01-16 17:35:53249fileapi::FileSystemQuotaUtil* TestFileSystemBackend::GetQuotaUtil() {
[email protected]6faad822012-05-11 12:58:29250 return quota_util_.get();
251}
252
[email protected]7ade66ee2014-01-16 17:35:53253const fileapi::UpdateObserverList* TestFileSystemBackend::GetUpdateObservers(
[email protected]caf66702012-09-07 07:02:20254 FileSystemType type) const {
[email protected]3c2f54642013-07-12 05:14:09255 return quota_util_->GetUpdateObservers(type);
[email protected]3a7bf222013-06-09 14:14:12256}
257
[email protected]f19bbf62013-07-09 01:22:32258void TestFileSystemBackend::AddFileChangeObserver(
[email protected]7ade66ee2014-01-16 17:35:53259 fileapi::FileChangeObserver* observer) {
[email protected]3c2f54642013-07-12 05:14:09260 quota_util_->AddFileChangeObserver(
[email protected]7ade66ee2014-01-16 17:35:53261 fileapi::kFileSystemTypeTest, observer, quota_util_->task_runner());
[email protected]caf66702012-09-07 07:02:20262}
263
[email protected]7ade66ee2014-01-16 17:35:53264} // namespace content