blob: e0caaa4c6623d565ad8ead26d16101392e3dbc7e [file] [log] [blame]
[email protected]6faad822012-05-11 12:58:291// Copyright (c) 2012 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.
4
[email protected]c6f9203a2013-05-28 02:08:075#include "webkit/browser/fileapi/test_mount_point_provider.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]20c2df62013-05-21 08:26:3613#include "webkit/browser/fileapi/copy_or_move_file_validator.h"
[email protected]f25e1132013-05-24 13:58:0414#include "webkit/browser/fileapi/file_observers.h"
[email protected]c6f9203a2013-05-28 02:08:0715#include "webkit/browser/fileapi/file_system_file_stream_reader.h"
16#include "webkit/browser/fileapi/file_system_operation_context.h"
[email protected]20c2df62013-05-21 08:26:3617#include "webkit/browser/fileapi/file_system_quota_util.h"
[email protected]f25e1132013-05-24 13:58:0418#include "webkit/browser/fileapi/local_file_system_operation.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]28f051c32013-05-21 05:15:2621#include "webkit/browser/fileapi/sandbox_file_stream_writer.h"
[email protected]7e836a3d2012-05-31 05:14:5922#include "webkit/fileapi/file_system_util.h"
[email protected]6faad822012-05-11 12:58:2923#include "webkit/quota/quota_manager.h"
24
25namespace fileapi {
26
[email protected]6faad822012-05-11 12:58:2927// This only supports single origin.
[email protected]caf66702012-09-07 07:02:2028class TestMountPointProvider::QuotaUtil
29 : public FileSystemQuotaUtil,
30 public FileUpdateObserver {
[email protected]6faad822012-05-11 12:58:2931 public:
[email protected]caf66702012-09-07 07:02:2032 QuotaUtil() : usage_(0) {}
33 virtual ~QuotaUtil() {}
[email protected]6faad822012-05-11 12:58:2934
[email protected]caf66702012-09-07 07:02:2035 // FileSystemQuotaUtil overrides.
[email protected]6faad822012-05-11 12:58:2936 virtual void GetOriginsForTypeOnFileThread(
37 FileSystemType type,
38 std::set<GURL>* origins) OVERRIDE {
39 NOTREACHED();
40 }
41 virtual void GetOriginsForHostOnFileThread(
42 FileSystemType type,
43 const std::string& host,
44 std::set<GURL>* origins) OVERRIDE {
45 NOTREACHED();
46 }
47 virtual int64 GetOriginUsageOnFileThread(
[email protected]22894522012-05-23 07:14:5848 FileSystemContext* context,
[email protected]6faad822012-05-11 12:58:2949 const GURL& origin_url,
50 FileSystemType type) OVERRIDE {
51 return usage_;
52 }
[email protected]6faad822012-05-11 12:58:2953 virtual void InvalidateUsageCache(const GURL& origin_url,
54 FileSystemType type) OVERRIDE {
55 // Do nothing.
56 }
[email protected]bd4219cf2013-04-26 14:33:4657 virtual void StickyInvalidateUsageCache(
58 const GURL& origin,
59 FileSystemType type) OVERRIDE {
60 // Do nothing.
61 }
[email protected]6faad822012-05-11 12:58:2962
[email protected]caf66702012-09-07 07:02:2063 // FileUpdateObserver overrides.
64 virtual void OnStartUpdate(const FileSystemURL& url) OVERRIDE {}
65 virtual void OnUpdate(const FileSystemURL& url, int64 delta) OVERRIDE {
66 usage_ += delta;
67 }
68 virtual void OnEndUpdate(const FileSystemURL& url) OVERRIDE {}
69
[email protected]6faad822012-05-11 12:58:2970 private:
71 int64 usage_;
72};
73
[email protected]6faad822012-05-11 12:58:2974TestMountPointProvider::TestMountPointProvider(
75 base::SequencedTaskRunner* task_runner,
[email protected]a3ef4832013-02-02 05:12:3376 const base::FilePath& base_path)
[email protected]6faad822012-05-11 12:58:2977 : base_path_(base_path),
[email protected]caf66702012-09-07 07:02:2078 task_runner_(task_runner),
[email protected]25b697992013-01-29 07:10:0579 local_file_util_(new AsyncFileUtilAdapter(new LocalFileUtil())),
[email protected]c3d638b32013-04-22 02:18:4980 quota_util_(new QuotaUtil),
81 require_copy_or_move_validator_(false) {
[email protected]caf66702012-09-07 07:02:2082 UpdateObserverList::Source source;
83 source.AddObserver(quota_util_.get(), task_runner_);
84 observers_ = UpdateObserverList(source);
[email protected]6faad822012-05-11 12:58:2985}
86
87TestMountPointProvider::~TestMountPointProvider() {
88}
89
[email protected]420fb562013-04-18 01:46:3490bool TestMountPointProvider::CanHandleType(FileSystemType type) const {
91 return (type == kFileSystemTypeTest);
92}
93
[email protected]6faad822012-05-11 12:58:2994void TestMountPointProvider::ValidateFileSystemRoot(
95 const GURL& origin_url,
96 FileSystemType type,
97 bool create,
98 const ValidateFileSystemCallback& callback) {
99 // This won't be called unless we add test code that opens a test
100 // filesystem by OpenFileSystem.
101 NOTREACHED();
102}
103
[email protected]a3ef4832013-02-02 05:12:33104base::FilePath TestMountPointProvider::GetFileSystemRootPathOnFileThread(
[email protected]199263e2012-12-14 07:14:20105 const FileSystemURL& url,
[email protected]6faad822012-05-11 12:58:29106 bool create) {
[email protected]199263e2012-12-14 07:14:20107 DCHECK_EQ(kFileSystemTypeTest, url.type());
[email protected]6faad822012-05-11 12:58:29108 bool success = true;
109 if (create)
110 success = file_util::CreateDirectory(base_path_);
111 else
112 success = file_util::DirectoryExists(base_path_);
[email protected]a3ef4832013-02-02 05:12:33113 return success ? base_path_ : base::FilePath();
[email protected]6faad822012-05-11 12:58:29114}
115
[email protected]d6afd112012-07-25 22:55:04116FileSystemFileUtil* TestMountPointProvider::GetFileUtil(FileSystemType type) {
[email protected]25b697992013-01-29 07:10:05117 DCHECK(local_file_util_.get());
118 return local_file_util_->sync_file_util();
119}
120
121AsyncFileUtil* TestMountPointProvider::GetAsyncFileUtil(FileSystemType type) {
[email protected]6faad822012-05-11 12:58:29122 return local_file_util_.get();
123}
124
[email protected]98407ee2013-04-04 08:52:17125CopyOrMoveFileValidatorFactory*
126TestMountPointProvider::GetCopyOrMoveFileValidatorFactory(
127 FileSystemType type, base::PlatformFileError* error_code) {
128 DCHECK(error_code);
129 *error_code = base::PLATFORM_FILE_OK;
[email protected]c3d638b32013-04-22 02:18:49130 if (require_copy_or_move_validator_) {
131 if (!copy_or_move_file_validator_factory_)
132 *error_code = base::PLATFORM_FILE_ERROR_SECURITY;
133 return copy_or_move_file_validator_factory_.get();
134 }
[email protected]98407ee2013-04-04 08:52:17135 return NULL;
136}
137
138void TestMountPointProvider::InitializeCopyOrMoveFileValidatorFactory(
139 FileSystemType type, scoped_ptr<CopyOrMoveFileValidatorFactory> factory) {
[email protected]c3d638b32013-04-22 02:18:49140 if (!require_copy_or_move_validator_) {
141 DCHECK(!factory);
142 return;
143 }
144 if (!copy_or_move_file_validator_factory_)
145 copy_or_move_file_validator_factory_ = factory.Pass();
[email protected]98407ee2013-04-04 08:52:17146}
147
[email protected]b40ffe72013-01-10 04:05:31148FilePermissionPolicy TestMountPointProvider::GetPermissionPolicy(
149 const FileSystemURL& url, int permissions) const {
150 return FILE_PERMISSION_ALWAYS_DENY;
[email protected]6faad822012-05-11 12:58:29151}
152
[email protected]8e3bc3e2012-08-24 13:12:53153FileSystemOperation* TestMountPointProvider::CreateFileSystemOperation(
[email protected]949f25a2012-06-27 01:53:09154 const FileSystemURL& url,
[email protected]d23a00cc2012-09-11 17:38:13155 FileSystemContext* context,
156 base::PlatformFileError* error_code) const {
[email protected]75af1a42012-07-26 04:06:20157 scoped_ptr<FileSystemOperationContext> operation_context(
158 new FileSystemOperationContext(context));
[email protected]caf66702012-09-07 07:02:20159 operation_context->set_update_observers(observers_);
[email protected]75af1a42012-07-26 04:06:20160 return new LocalFileSystemOperation(context, operation_context.Pass());
[email protected]6faad822012-05-11 12:58:29161}
162
[email protected]3ee9eb02013-04-10 09:17:05163scoped_ptr<webkit_blob::FileStreamReader>
164TestMountPointProvider::CreateFileStreamReader(
[email protected]949f25a2012-06-27 01:53:09165 const FileSystemURL& url,
[email protected]6faad822012-05-11 12:58:29166 int64 offset,
[email protected]a1057832012-10-15 13:28:06167 const base::Time& expected_modification_time,
[email protected]6faad822012-05-11 12:58:29168 FileSystemContext* context) const {
[email protected]3ee9eb02013-04-10 09:17:05169 return scoped_ptr<webkit_blob::FileStreamReader>(
170 new FileSystemFileStreamReader(
171 context, url, offset, expected_modification_time));
[email protected]6faad822012-05-11 12:58:29172}
173
[email protected]3ee9eb02013-04-10 09:17:05174scoped_ptr<fileapi::FileStreamWriter>
175TestMountPointProvider::CreateFileStreamWriter(
[email protected]949f25a2012-06-27 01:53:09176 const FileSystemURL& url,
[email protected]7e84b912012-05-16 04:42:01177 int64 offset,
178 FileSystemContext* context) const {
[email protected]3ee9eb02013-04-10 09:17:05179 return scoped_ptr<fileapi::FileStreamWriter>(
180 new SandboxFileStreamWriter(context, url, offset, observers_));
[email protected]7e84b912012-05-16 04:42:01181}
182
[email protected]6faad822012-05-11 12:58:29183FileSystemQuotaUtil* TestMountPointProvider::GetQuotaUtil() {
184 return quota_util_.get();
185}
186
[email protected]d5e08552012-08-02 21:43:40187void TestMountPointProvider::DeleteFileSystem(
188 const GURL& origin_url,
189 FileSystemType type,
190 FileSystemContext* context,
191 const DeleteFileSystemCallback& callback) {
192 // This won't be called unless we add test code that opens a test
193 // filesystem by OpenFileSystem.
194 NOTREACHED();
195 callback.Run(base::PLATFORM_FILE_ERROR_INVALID_OPERATION);
196}
197
[email protected]caf66702012-09-07 07:02:20198const UpdateObserverList* TestMountPointProvider::GetUpdateObservers(
199 FileSystemType type) const {
200 return &observers_;
201}
202
[email protected]6faad822012-05-11 12:58:29203} // namespace fileapi