| OLD | NEW |
| (Empty) |
| 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/browser/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" | |
| 13 #include "webkit/browser/fileapi/copy_or_move_file_validator.h" | |
| 14 #include "webkit/browser/fileapi/file_observers.h" | |
| 15 #include "webkit/browser/fileapi/file_system_file_stream_reader.h" | |
| 16 #include "webkit/browser/fileapi/file_system_operation_context.h" | |
| 17 #include "webkit/browser/fileapi/file_system_quota_util.h" | |
| 18 #include "webkit/browser/fileapi/local_file_system_operation.h" | |
| 19 #include "webkit/browser/fileapi/local_file_util.h" | |
| 20 #include "webkit/browser/fileapi/native_file_util.h" | |
| 21 #include "webkit/browser/fileapi/sandbox_file_stream_writer.h" | |
| 22 #include "webkit/browser/quota/quota_manager.h" | |
| 23 #include "webkit/common/fileapi/file_system_util.h" | |
| 24 | |
| 25 namespace fileapi { | |
| 26 | |
| 27 // This only supports single origin. | |
| 28 class TestMountPointProvider::QuotaUtil | |
| 29 : public FileSystemQuotaUtil, | |
| 30 public FileUpdateObserver { | |
| 31 public: | |
| 32 QuotaUtil() : usage_(0) {} | |
| 33 virtual ~QuotaUtil() {} | |
| 34 | |
| 35 // FileSystemQuotaUtil overrides. | |
| 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 } | |
| 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( | |
| 56 FileSystemContext* context, | |
| 57 const GURL& origin_url, | |
| 58 FileSystemType type) OVERRIDE { | |
| 59 return usage_; | |
| 60 } | |
| 61 virtual void InvalidateUsageCache(const GURL& origin_url, | |
| 62 FileSystemType type) OVERRIDE { | |
| 63 // Do nothing. | |
| 64 } | |
| 65 virtual void StickyInvalidateUsageCache( | |
| 66 const GURL& origin, | |
| 67 FileSystemType type) OVERRIDE { | |
| 68 // Do nothing. | |
| 69 } | |
| 70 | |
| 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 | |
| 78 private: | |
| 79 int64 usage_; | |
| 80 }; | |
| 81 | |
| 82 TestMountPointProvider::TestMountPointProvider( | |
| 83 base::SequencedTaskRunner* task_runner, | |
| 84 const base::FilePath& base_path) | |
| 85 : base_path_(base_path), | |
| 86 task_runner_(task_runner), | |
| 87 local_file_util_(new AsyncFileUtilAdapter(new LocalFileUtil())), | |
| 88 quota_util_(new QuotaUtil), | |
| 89 require_copy_or_move_validator_(false) { | |
| 90 UpdateObserverList::Source source; | |
| 91 source.AddObserver(quota_util_.get(), task_runner_.get()); | |
| 92 update_observers_ = UpdateObserverList(source); | |
| 93 } | |
| 94 | |
| 95 TestMountPointProvider::~TestMountPointProvider() { | |
| 96 } | |
| 97 | |
| 98 bool TestMountPointProvider::CanHandleType(FileSystemType type) const { | |
| 99 return (type == kFileSystemTypeTest); | |
| 100 } | |
| 101 | |
| 102 void TestMountPointProvider::OpenFileSystem( | |
| 103 const GURL& origin_url, | |
| 104 FileSystemType type, | |
| 105 OpenFileSystemMode mode, | |
| 106 const OpenFileSystemCallback& callback) { | |
| 107 callback.Run(base::PLATFORM_FILE_OK); | |
| 108 } | |
| 109 | |
| 110 FileSystemFileUtil* TestMountPointProvider::GetFileUtil(FileSystemType type) { | |
| 111 DCHECK(local_file_util_.get()); | |
| 112 return local_file_util_->sync_file_util(); | |
| 113 } | |
| 114 | |
| 115 AsyncFileUtil* TestMountPointProvider::GetAsyncFileUtil(FileSystemType type) { | |
| 116 return local_file_util_.get(); | |
| 117 } | |
| 118 | |
| 119 CopyOrMoveFileValidatorFactory* | |
| 120 TestMountPointProvider::GetCopyOrMoveFileValidatorFactory( | |
| 121 FileSystemType type, base::PlatformFileError* error_code) { | |
| 122 DCHECK(error_code); | |
| 123 *error_code = base::PLATFORM_FILE_OK; | |
| 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 } | |
| 129 return NULL; | |
| 130 } | |
| 131 | |
| 132 void TestMountPointProvider::InitializeCopyOrMoveFileValidatorFactory( | |
| 133 scoped_ptr<CopyOrMoveFileValidatorFactory> factory) { | |
| 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(); | |
| 140 } | |
| 141 | |
| 142 FileSystemOperation* TestMountPointProvider::CreateFileSystemOperation( | |
| 143 const FileSystemURL& url, | |
| 144 FileSystemContext* context, | |
| 145 base::PlatformFileError* error_code) const { | |
| 146 scoped_ptr<FileSystemOperationContext> operation_context( | |
| 147 new FileSystemOperationContext(context)); | |
| 148 operation_context->set_update_observers(update_observers_); | |
| 149 operation_context->set_change_observers(change_observers_); | |
| 150 operation_context->set_root_path(base_path_); | |
| 151 return new LocalFileSystemOperation(url, context, operation_context.Pass()); | |
| 152 } | |
| 153 | |
| 154 scoped_ptr<webkit_blob::FileStreamReader> | |
| 155 TestMountPointProvider::CreateFileStreamReader( | |
| 156 const FileSystemURL& url, | |
| 157 int64 offset, | |
| 158 const base::Time& expected_modification_time, | |
| 159 FileSystemContext* context) const { | |
| 160 return scoped_ptr<webkit_blob::FileStreamReader>( | |
| 161 new FileSystemFileStreamReader( | |
| 162 context, url, offset, expected_modification_time)); | |
| 163 } | |
| 164 | |
| 165 scoped_ptr<fileapi::FileStreamWriter> | |
| 166 TestMountPointProvider::CreateFileStreamWriter( | |
| 167 const FileSystemURL& url, | |
| 168 int64 offset, | |
| 169 FileSystemContext* context) const { | |
| 170 return scoped_ptr<fileapi::FileStreamWriter>( | |
| 171 new SandboxFileStreamWriter(context, url, offset, update_observers_)); | |
| 172 } | |
| 173 | |
| 174 FileSystemQuotaUtil* TestMountPointProvider::GetQuotaUtil() { | |
| 175 return quota_util_.get(); | |
| 176 } | |
| 177 | |
| 178 const UpdateObserverList* TestMountPointProvider::GetUpdateObservers( | |
| 179 FileSystemType type) const { | |
| 180 return &update_observers_; | |
| 181 } | |
| 182 | |
| 183 void TestMountPointProvider::AddFileChangeObserver( | |
| 184 FileChangeObserver* observer) { | |
| 185 ChangeObserverList::Source source = change_observers_.source(); | |
| 186 source.AddObserver(observer, task_runner_.get()); | |
| 187 change_observers_ = ChangeObserverList(source); | |
| 188 } | |
| 189 | |
| 190 } // namespace fileapi | |
| OLD | NEW |