[email protected] | 6faad82 | 2012-05-11 12:58:29 | [diff] [blame] | 1 | // 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 | |
| 5 | #include "webkit/fileapi/test_mount_point_provider.h" |
| 6 | |
| 7 | #include <set> |
| 8 | #include <string> |
| 9 | #include <vector> |
| 10 | |
| 11 | #include "base/file_util.h" |
| 12 | #include "base/sequenced_task_runner.h" |
[email protected] | 98407ee | 2013-04-04 08:52:17 | [diff] [blame] | 13 | #include "webkit/fileapi/copy_or_move_file_validator.h" |
[email protected] | caf6670 | 2012-09-07 07:02:20 | [diff] [blame] | 14 | #include "webkit/fileapi/file_observers.h" |
[email protected] | c4ca3b45 | 2012-05-31 03:15:46 | [diff] [blame] | 15 | #include "webkit/fileapi/file_system_file_stream_reader.h" |
[email protected] | 5f778268 | 2013-03-04 02:43:35 | [diff] [blame] | 16 | #include "webkit/fileapi/file_system_operation_context.h" |
[email protected] | 6faad82 | 2012-05-11 12:58:29 | [diff] [blame] | 17 | #include "webkit/fileapi/file_system_quota_util.h" |
[email protected] | 7e836a3d | 2012-05-31 05:14:59 | [diff] [blame] | 18 | #include "webkit/fileapi/file_system_util.h" |
[email protected] | 02a6054 | 2012-07-24 20:05:33 | [diff] [blame] | 19 | #include "webkit/fileapi/local_file_system_operation.h" |
[email protected] | 6faad82 | 2012-05-11 12:58:29 | [diff] [blame] | 20 | #include "webkit/fileapi/local_file_util.h" |
| 21 | #include "webkit/fileapi/native_file_util.h" |
[email protected] | 7469890 | 2012-06-12 12:16:38 | [diff] [blame] | 22 | #include "webkit/fileapi/sandbox_file_stream_writer.h" |
[email protected] | 6faad82 | 2012-05-11 12:58:29 | [diff] [blame] | 23 | #include "webkit/quota/quota_manager.h" |
| 24 | |
| 25 | namespace fileapi { |
| 26 | |
[email protected] | 6faad82 | 2012-05-11 12:58:29 | [diff] [blame] | 27 | // This only supports single origin. |
[email protected] | caf6670 | 2012-09-07 07:02:20 | [diff] [blame] | 28 | class TestMountPointProvider::QuotaUtil |
| 29 | : public FileSystemQuotaUtil, |
| 30 | public FileUpdateObserver { |
[email protected] | 6faad82 | 2012-05-11 12:58:29 | [diff] [blame] | 31 | public: |
[email protected] | caf6670 | 2012-09-07 07:02:20 | [diff] [blame] | 32 | QuotaUtil() : usage_(0) {} |
| 33 | virtual ~QuotaUtil() {} |
[email protected] | 6faad82 | 2012-05-11 12:58:29 | [diff] [blame] | 34 | |
[email protected] | caf6670 | 2012-09-07 07:02:20 | [diff] [blame] | 35 | // FileSystemQuotaUtil overrides. |
[email protected] | 6faad82 | 2012-05-11 12:58:29 | [diff] [blame] | 36 | 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] | 2289452 | 2012-05-23 07:14:58 | [diff] [blame] | 48 | FileSystemContext* context, |
[email protected] | 6faad82 | 2012-05-11 12:58:29 | [diff] [blame] | 49 | const GURL& origin_url, |
| 50 | FileSystemType type) OVERRIDE { |
| 51 | return usage_; |
| 52 | } |
[email protected] | 6faad82 | 2012-05-11 12:58:29 | [diff] [blame] | 53 | virtual void InvalidateUsageCache(const GURL& origin_url, |
| 54 | FileSystemType type) OVERRIDE { |
| 55 | // Do nothing. |
| 56 | } |
| 57 | |
[email protected] | caf6670 | 2012-09-07 07:02:20 | [diff] [blame] | 58 | // FileUpdateObserver overrides. |
| 59 | virtual void OnStartUpdate(const FileSystemURL& url) OVERRIDE {} |
| 60 | virtual void OnUpdate(const FileSystemURL& url, int64 delta) OVERRIDE { |
| 61 | usage_ += delta; |
| 62 | } |
| 63 | virtual void OnEndUpdate(const FileSystemURL& url) OVERRIDE {} |
| 64 | |
[email protected] | 6faad82 | 2012-05-11 12:58:29 | [diff] [blame] | 65 | private: |
| 66 | int64 usage_; |
| 67 | }; |
| 68 | |
[email protected] | 6faad82 | 2012-05-11 12:58:29 | [diff] [blame] | 69 | TestMountPointProvider::TestMountPointProvider( |
| 70 | base::SequencedTaskRunner* task_runner, |
[email protected] | a3ef483 | 2013-02-02 05:12:33 | [diff] [blame] | 71 | const base::FilePath& base_path) |
[email protected] | 6faad82 | 2012-05-11 12:58:29 | [diff] [blame] | 72 | : base_path_(base_path), |
[email protected] | caf6670 | 2012-09-07 07:02:20 | [diff] [blame] | 73 | task_runner_(task_runner), |
[email protected] | 25b69799 | 2013-01-29 07:10:05 | [diff] [blame] | 74 | local_file_util_(new AsyncFileUtilAdapter(new LocalFileUtil())), |
[email protected] | c3d638b3 | 2013-04-22 02:18:49 | [diff] [blame^] | 75 | quota_util_(new QuotaUtil), |
| 76 | require_copy_or_move_validator_(false) { |
[email protected] | caf6670 | 2012-09-07 07:02:20 | [diff] [blame] | 77 | UpdateObserverList::Source source; |
| 78 | source.AddObserver(quota_util_.get(), task_runner_); |
| 79 | observers_ = UpdateObserverList(source); |
[email protected] | 6faad82 | 2012-05-11 12:58:29 | [diff] [blame] | 80 | } |
| 81 | |
| 82 | TestMountPointProvider::~TestMountPointProvider() { |
| 83 | } |
| 84 | |
[email protected] | 420fb56 | 2013-04-18 01:46:34 | [diff] [blame] | 85 | bool TestMountPointProvider::CanHandleType(FileSystemType type) const { |
| 86 | return (type == kFileSystemTypeTest); |
| 87 | } |
| 88 | |
[email protected] | 6faad82 | 2012-05-11 12:58:29 | [diff] [blame] | 89 | void TestMountPointProvider::ValidateFileSystemRoot( |
| 90 | const GURL& origin_url, |
| 91 | FileSystemType type, |
| 92 | bool create, |
| 93 | const ValidateFileSystemCallback& callback) { |
| 94 | // This won't be called unless we add test code that opens a test |
| 95 | // filesystem by OpenFileSystem. |
| 96 | NOTREACHED(); |
| 97 | } |
| 98 | |
[email protected] | a3ef483 | 2013-02-02 05:12:33 | [diff] [blame] | 99 | base::FilePath TestMountPointProvider::GetFileSystemRootPathOnFileThread( |
[email protected] | 199263e | 2012-12-14 07:14:20 | [diff] [blame] | 100 | const FileSystemURL& url, |
[email protected] | 6faad82 | 2012-05-11 12:58:29 | [diff] [blame] | 101 | bool create) { |
[email protected] | 199263e | 2012-12-14 07:14:20 | [diff] [blame] | 102 | DCHECK_EQ(kFileSystemTypeTest, url.type()); |
[email protected] | 6faad82 | 2012-05-11 12:58:29 | [diff] [blame] | 103 | bool success = true; |
| 104 | if (create) |
| 105 | success = file_util::CreateDirectory(base_path_); |
| 106 | else |
| 107 | success = file_util::DirectoryExists(base_path_); |
[email protected] | a3ef483 | 2013-02-02 05:12:33 | [diff] [blame] | 108 | return success ? base_path_ : base::FilePath(); |
[email protected] | 6faad82 | 2012-05-11 12:58:29 | [diff] [blame] | 109 | } |
| 110 | |
[email protected] | d6afd11 | 2012-07-25 22:55:04 | [diff] [blame] | 111 | FileSystemFileUtil* TestMountPointProvider::GetFileUtil(FileSystemType type) { |
[email protected] | 25b69799 | 2013-01-29 07:10:05 | [diff] [blame] | 112 | DCHECK(local_file_util_.get()); |
| 113 | return local_file_util_->sync_file_util(); |
| 114 | } |
| 115 | |
| 116 | AsyncFileUtil* TestMountPointProvider::GetAsyncFileUtil(FileSystemType type) { |
[email protected] | 6faad82 | 2012-05-11 12:58:29 | [diff] [blame] | 117 | return local_file_util_.get(); |
| 118 | } |
| 119 | |
[email protected] | 98407ee | 2013-04-04 08:52:17 | [diff] [blame] | 120 | CopyOrMoveFileValidatorFactory* |
| 121 | TestMountPointProvider::GetCopyOrMoveFileValidatorFactory( |
| 122 | FileSystemType type, base::PlatformFileError* error_code) { |
| 123 | DCHECK(error_code); |
| 124 | *error_code = base::PLATFORM_FILE_OK; |
[email protected] | c3d638b3 | 2013-04-22 02:18:49 | [diff] [blame^] | 125 | if (require_copy_or_move_validator_) { |
| 126 | if (!copy_or_move_file_validator_factory_) |
| 127 | *error_code = base::PLATFORM_FILE_ERROR_SECURITY; |
| 128 | return copy_or_move_file_validator_factory_.get(); |
| 129 | } |
[email protected] | 98407ee | 2013-04-04 08:52:17 | [diff] [blame] | 130 | return NULL; |
| 131 | } |
| 132 | |
| 133 | void TestMountPointProvider::InitializeCopyOrMoveFileValidatorFactory( |
| 134 | FileSystemType type, scoped_ptr<CopyOrMoveFileValidatorFactory> factory) { |
[email protected] | c3d638b3 | 2013-04-22 02:18:49 | [diff] [blame^] | 135 | if (!require_copy_or_move_validator_) { |
| 136 | DCHECK(!factory); |
| 137 | return; |
| 138 | } |
| 139 | if (!copy_or_move_file_validator_factory_) |
| 140 | copy_or_move_file_validator_factory_ = factory.Pass(); |
[email protected] | 98407ee | 2013-04-04 08:52:17 | [diff] [blame] | 141 | } |
| 142 | |
[email protected] | b40ffe7 | 2013-01-10 04:05:31 | [diff] [blame] | 143 | FilePermissionPolicy TestMountPointProvider::GetPermissionPolicy( |
| 144 | const FileSystemURL& url, int permissions) const { |
| 145 | return FILE_PERMISSION_ALWAYS_DENY; |
[email protected] | 6faad82 | 2012-05-11 12:58:29 | [diff] [blame] | 146 | } |
| 147 | |
[email protected] | 8e3bc3e | 2012-08-24 13:12:53 | [diff] [blame] | 148 | FileSystemOperation* TestMountPointProvider::CreateFileSystemOperation( |
[email protected] | 949f25a | 2012-06-27 01:53:09 | [diff] [blame] | 149 | const FileSystemURL& url, |
[email protected] | d23a00cc | 2012-09-11 17:38:13 | [diff] [blame] | 150 | FileSystemContext* context, |
| 151 | base::PlatformFileError* error_code) const { |
[email protected] | 75af1a4 | 2012-07-26 04:06:20 | [diff] [blame] | 152 | scoped_ptr<FileSystemOperationContext> operation_context( |
| 153 | new FileSystemOperationContext(context)); |
[email protected] | caf6670 | 2012-09-07 07:02:20 | [diff] [blame] | 154 | operation_context->set_update_observers(observers_); |
[email protected] | 75af1a4 | 2012-07-26 04:06:20 | [diff] [blame] | 155 | return new LocalFileSystemOperation(context, operation_context.Pass()); |
[email protected] | 6faad82 | 2012-05-11 12:58:29 | [diff] [blame] | 156 | } |
| 157 | |
[email protected] | 3ee9eb0 | 2013-04-10 09:17:05 | [diff] [blame] | 158 | scoped_ptr<webkit_blob::FileStreamReader> |
| 159 | TestMountPointProvider::CreateFileStreamReader( |
[email protected] | 949f25a | 2012-06-27 01:53:09 | [diff] [blame] | 160 | const FileSystemURL& url, |
[email protected] | 6faad82 | 2012-05-11 12:58:29 | [diff] [blame] | 161 | int64 offset, |
[email protected] | a105783 | 2012-10-15 13:28:06 | [diff] [blame] | 162 | const base::Time& expected_modification_time, |
[email protected] | 6faad82 | 2012-05-11 12:58:29 | [diff] [blame] | 163 | FileSystemContext* context) const { |
[email protected] | 3ee9eb0 | 2013-04-10 09:17:05 | [diff] [blame] | 164 | return scoped_ptr<webkit_blob::FileStreamReader>( |
| 165 | new FileSystemFileStreamReader( |
| 166 | context, url, offset, expected_modification_time)); |
[email protected] | 6faad82 | 2012-05-11 12:58:29 | [diff] [blame] | 167 | } |
| 168 | |
[email protected] | 3ee9eb0 | 2013-04-10 09:17:05 | [diff] [blame] | 169 | scoped_ptr<fileapi::FileStreamWriter> |
| 170 | TestMountPointProvider::CreateFileStreamWriter( |
[email protected] | 949f25a | 2012-06-27 01:53:09 | [diff] [blame] | 171 | const FileSystemURL& url, |
[email protected] | 7e84b91 | 2012-05-16 04:42:01 | [diff] [blame] | 172 | int64 offset, |
| 173 | FileSystemContext* context) const { |
[email protected] | 3ee9eb0 | 2013-04-10 09:17:05 | [diff] [blame] | 174 | return scoped_ptr<fileapi::FileStreamWriter>( |
| 175 | new SandboxFileStreamWriter(context, url, offset, observers_)); |
[email protected] | 7e84b91 | 2012-05-16 04:42:01 | [diff] [blame] | 176 | } |
| 177 | |
[email protected] | 6faad82 | 2012-05-11 12:58:29 | [diff] [blame] | 178 | FileSystemQuotaUtil* TestMountPointProvider::GetQuotaUtil() { |
| 179 | return quota_util_.get(); |
| 180 | } |
| 181 | |
[email protected] | d5e0855 | 2012-08-02 21:43:40 | [diff] [blame] | 182 | void TestMountPointProvider::DeleteFileSystem( |
| 183 | const GURL& origin_url, |
| 184 | FileSystemType type, |
| 185 | FileSystemContext* context, |
| 186 | const DeleteFileSystemCallback& callback) { |
| 187 | // This won't be called unless we add test code that opens a test |
| 188 | // filesystem by OpenFileSystem. |
| 189 | NOTREACHED(); |
| 190 | callback.Run(base::PLATFORM_FILE_ERROR_INVALID_OPERATION); |
| 191 | } |
| 192 | |
[email protected] | caf6670 | 2012-09-07 07:02:20 | [diff] [blame] | 193 | const UpdateObserverList* TestMountPointProvider::GetUpdateObservers( |
| 194 | FileSystemType type) const { |
| 195 | return &observers_; |
| 196 | } |
| 197 | |
[email protected] | 6faad82 | 2012-05-11 12:58:29 | [diff] [blame] | 198 | } // namespace fileapi |