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