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