| OLD | NEW |
| (Empty) |
| 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. | |
| 4 | |
| 5 #include "content/public/test/test_file_system_backend.h" | |
| 6 | |
| 7 #include <set> | |
| 8 #include <string> | |
| 9 #include <utility> | |
| 10 #include <vector> | |
| 11 | |
| 12 #include "base/files/file.h" | |
| 13 #include "base/files/file_util.h" | |
| 14 #include "base/macros.h" | |
| 15 #include "base/memory/weak_ptr.h" | |
| 16 #include "base/observer_list.h" | |
| 17 #include "base/sequenced_task_runner.h" | |
| 18 #include "base/threading/thread_task_runner_handle.h" | |
| 19 #include "storage/browser/fileapi/copy_or_move_file_validator.h" | |
| 20 #include "storage/browser/fileapi/file_observers.h" | |
| 21 #include "storage/browser/fileapi/file_stream_reader.h" | |
| 22 #include "storage/browser/fileapi/file_system_operation.h" | |
| 23 #include "storage/browser/fileapi/file_system_operation_context.h" | |
| 24 #include "storage/browser/fileapi/file_system_quota_util.h" | |
| 25 #include "storage/browser/fileapi/local_file_util.h" | |
| 26 #include "storage/browser/fileapi/native_file_util.h" | |
| 27 #include "storage/browser/fileapi/quota/quota_reservation.h" | |
| 28 #include "storage/browser/fileapi/sandbox_file_stream_writer.h" | |
| 29 #include "storage/browser/quota/quota_manager.h" | |
| 30 #include "storage/common/fileapi/file_system_util.h" | |
| 31 | |
| 32 using storage::FileSystemContext; | |
| 33 using storage::FileSystemOperation; | |
| 34 using storage::FileSystemOperationContext; | |
| 35 using storage::FileSystemURL; | |
| 36 | |
| 37 namespace content { | |
| 38 | |
| 39 namespace { | |
| 40 | |
| 41 // Stub implementation of storage::LocalFileUtil. | |
| 42 class TestFileUtil : public storage::LocalFileUtil { | |
| 43 public: | |
| 44 explicit TestFileUtil(const base::FilePath& base_path) | |
| 45 : base_path_(base_path) {} | |
| 46 ~TestFileUtil() override {} | |
| 47 | |
| 48 // LocalFileUtil overrides. | |
| 49 base::File::Error GetLocalFilePath(FileSystemOperationContext* context, | |
| 50 const FileSystemURL& file_system_url, | |
| 51 base::FilePath* local_file_path) override { | |
| 52 *local_file_path = base_path_.Append(file_system_url.path()); | |
| 53 return base::File::FILE_OK; | |
| 54 } | |
| 55 | |
| 56 private: | |
| 57 base::FilePath base_path_; | |
| 58 }; | |
| 59 | |
| 60 } // namespace | |
| 61 | |
| 62 // This only supports single origin. | |
| 63 class TestFileSystemBackend::QuotaUtil : public storage::FileSystemQuotaUtil, | |
| 64 public storage::FileUpdateObserver { | |
| 65 public: | |
| 66 QuotaUtil() : usage_(0) {} | |
| 67 ~QuotaUtil() override {} | |
| 68 | |
| 69 // FileSystemQuotaUtil overrides. | |
| 70 base::File::Error DeleteOriginDataOnFileTaskRunner( | |
| 71 FileSystemContext* context, | |
| 72 storage::QuotaManagerProxy* proxy, | |
| 73 const GURL& origin_url, | |
| 74 storage::FileSystemType type) override { | |
| 75 NOTREACHED(); | |
| 76 return base::File::FILE_OK; | |
| 77 } | |
| 78 | |
| 79 scoped_refptr<storage::QuotaReservation> | |
| 80 CreateQuotaReservationOnFileTaskRunner( | |
| 81 const GURL& origin_url, | |
| 82 storage::FileSystemType type) override { | |
| 83 NOTREACHED(); | |
| 84 return scoped_refptr<storage::QuotaReservation>(); | |
| 85 } | |
| 86 | |
| 87 void GetOriginsForTypeOnFileTaskRunner(storage::FileSystemType type, | |
| 88 std::set<GURL>* origins) override { | |
| 89 NOTREACHED(); | |
| 90 } | |
| 91 | |
| 92 void GetOriginsForHostOnFileTaskRunner(storage::FileSystemType type, | |
| 93 const std::string& host, | |
| 94 std::set<GURL>* origins) override { | |
| 95 NOTREACHED(); | |
| 96 } | |
| 97 | |
| 98 int64_t GetOriginUsageOnFileTaskRunner( | |
| 99 FileSystemContext* context, | |
| 100 const GURL& origin_url, | |
| 101 storage::FileSystemType type) override { | |
| 102 return usage_; | |
| 103 } | |
| 104 | |
| 105 // FileUpdateObserver overrides. | |
| 106 void OnStartUpdate(const FileSystemURL& url) override {} | |
| 107 void OnUpdate(const FileSystemURL& url, int64_t delta) override { | |
| 108 usage_ += delta; | |
| 109 } | |
| 110 void OnEndUpdate(const FileSystemURL& url) override {} | |
| 111 | |
| 112 private: | |
| 113 int64_t usage_; | |
| 114 DISALLOW_COPY_AND_ASSIGN(QuotaUtil); | |
| 115 }; | |
| 116 | |
| 117 TestFileSystemBackend::TestFileSystemBackend( | |
| 118 base::SequencedTaskRunner* task_runner, | |
| 119 const base::FilePath& base_path) | |
| 120 : base_path_(base_path), | |
| 121 task_runner_(task_runner), | |
| 122 file_util_( | |
| 123 new storage::AsyncFileUtilAdapter(new TestFileUtil(base_path))), | |
| 124 quota_util_(new QuotaUtil), | |
| 125 require_copy_or_move_validator_(false) { | |
| 126 update_observers_ = | |
| 127 update_observers_.AddObserver(quota_util_.get(), task_runner_.get()); | |
| 128 } | |
| 129 | |
| 130 TestFileSystemBackend::~TestFileSystemBackend() { | |
| 131 } | |
| 132 | |
| 133 bool TestFileSystemBackend::CanHandleType(storage::FileSystemType type) const { | |
| 134 return (type == storage::kFileSystemTypeTest); | |
| 135 } | |
| 136 | |
| 137 void TestFileSystemBackend::Initialize(FileSystemContext* context) { | |
| 138 } | |
| 139 | |
| 140 void TestFileSystemBackend::ResolveURL(const FileSystemURL& url, | |
| 141 storage::OpenFileSystemMode mode, | |
| 142 const OpenFileSystemCallback& callback) { | |
| 143 callback.Run(GetFileSystemRootURI(url.origin(), url.type()), | |
| 144 GetFileSystemName(url.origin(), url.type()), | |
| 145 base::File::FILE_OK); | |
| 146 } | |
| 147 | |
| 148 storage::AsyncFileUtil* TestFileSystemBackend::GetAsyncFileUtil( | |
| 149 storage::FileSystemType type) { | |
| 150 return file_util_.get(); | |
| 151 } | |
| 152 | |
| 153 storage::WatcherManager* TestFileSystemBackend::GetWatcherManager( | |
| 154 storage::FileSystemType type) { | |
| 155 return nullptr; | |
| 156 } | |
| 157 | |
| 158 storage::CopyOrMoveFileValidatorFactory* | |
| 159 TestFileSystemBackend::GetCopyOrMoveFileValidatorFactory( | |
| 160 storage::FileSystemType type, | |
| 161 base::File::Error* error_code) { | |
| 162 DCHECK(error_code); | |
| 163 *error_code = base::File::FILE_OK; | |
| 164 if (require_copy_or_move_validator_) { | |
| 165 if (!copy_or_move_file_validator_factory_) | |
| 166 *error_code = base::File::FILE_ERROR_SECURITY; | |
| 167 return copy_or_move_file_validator_factory_.get(); | |
| 168 } | |
| 169 return nullptr; | |
| 170 } | |
| 171 | |
| 172 void TestFileSystemBackend::InitializeCopyOrMoveFileValidatorFactory( | |
| 173 std::unique_ptr<storage::CopyOrMoveFileValidatorFactory> factory) { | |
| 174 if (!copy_or_move_file_validator_factory_) | |
| 175 copy_or_move_file_validator_factory_ = std::move(factory); | |
| 176 } | |
| 177 | |
| 178 FileSystemOperation* TestFileSystemBackend::CreateFileSystemOperation( | |
| 179 const FileSystemURL& url, | |
| 180 FileSystemContext* context, | |
| 181 base::File::Error* error_code) const { | |
| 182 std::unique_ptr<FileSystemOperationContext> operation_context( | |
| 183 new FileSystemOperationContext(context)); | |
| 184 operation_context->set_update_observers(*GetUpdateObservers(url.type())); | |
| 185 operation_context->set_change_observers(*GetChangeObservers(url.type())); | |
| 186 return FileSystemOperation::Create(url, context, | |
| 187 std::move(operation_context)); | |
| 188 } | |
| 189 | |
| 190 bool TestFileSystemBackend::SupportsStreaming( | |
| 191 const storage::FileSystemURL& url) const { | |
| 192 return false; | |
| 193 } | |
| 194 | |
| 195 bool TestFileSystemBackend::HasInplaceCopyImplementation( | |
| 196 storage::FileSystemType type) const { | |
| 197 return true; | |
| 198 } | |
| 199 | |
| 200 std::unique_ptr<storage::FileStreamReader> | |
| 201 TestFileSystemBackend::CreateFileStreamReader( | |
| 202 const FileSystemURL& url, | |
| 203 int64_t offset, | |
| 204 int64_t max_bytes_to_read, | |
| 205 const base::Time& expected_modification_time, | |
| 206 FileSystemContext* context) const { | |
| 207 return std::unique_ptr<storage::FileStreamReader>( | |
| 208 storage::FileStreamReader::CreateForFileSystemFile( | |
| 209 context, url, offset, expected_modification_time)); | |
| 210 } | |
| 211 | |
| 212 std::unique_ptr<storage::FileStreamWriter> | |
| 213 TestFileSystemBackend::CreateFileStreamWriter( | |
| 214 const FileSystemURL& url, | |
| 215 int64_t offset, | |
| 216 FileSystemContext* context) const { | |
| 217 return std::unique_ptr<storage::FileStreamWriter>( | |
| 218 new storage::SandboxFileStreamWriter(context, url, offset, | |
| 219 *GetUpdateObservers(url.type()))); | |
| 220 } | |
| 221 | |
| 222 storage::FileSystemQuotaUtil* TestFileSystemBackend::GetQuotaUtil() { | |
| 223 return quota_util_.get(); | |
| 224 } | |
| 225 | |
| 226 const storage::UpdateObserverList* TestFileSystemBackend::GetUpdateObservers( | |
| 227 storage::FileSystemType type) const { | |
| 228 return &update_observers_; | |
| 229 } | |
| 230 | |
| 231 const storage::ChangeObserverList* TestFileSystemBackend::GetChangeObservers( | |
| 232 storage::FileSystemType type) const { | |
| 233 return &change_observers_; | |
| 234 } | |
| 235 | |
| 236 const storage::AccessObserverList* TestFileSystemBackend::GetAccessObservers( | |
| 237 storage::FileSystemType type) const { | |
| 238 return nullptr; | |
| 239 } | |
| 240 | |
| 241 void TestFileSystemBackend::AddFileChangeObserver( | |
| 242 storage::FileChangeObserver* observer) { | |
| 243 change_observers_ = | |
| 244 change_observers_.AddObserver(observer, task_runner_.get()); | |
| 245 } | |
| 246 | |
| 247 } // namespace content | |
| OLD | NEW |