Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "content/public/test/test_file_system_backend.h" | 5 #include "content/public/test/test_file_system_backend.h" |
| 6 | 6 |
| 7 #include <set> | 7 #include <set> |
| 8 #include <string> | 8 #include <string> |
| 9 #include <vector> | 9 #include <vector> |
| 10 | 10 |
| 11 #include "base/file_util.h" | 11 #include "base/file_util.h" |
| 12 #include "base/files/file.h" | |
| 13 #include "base/memory/weak_ptr.h" | |
| 14 #include "base/observer_list.h" | |
| 12 #include "base/sequenced_task_runner.h" | 15 #include "base/sequenced_task_runner.h" |
| 16 #include "base/thread_task_runner_handle.h" | |
| 13 #include "webkit/browser/blob/file_stream_reader.h" | 17 #include "webkit/browser/blob/file_stream_reader.h" |
| 14 #include "webkit/browser/fileapi/copy_or_move_file_validator.h" | 18 #include "webkit/browser/fileapi/copy_or_move_file_validator.h" |
| 15 #include "webkit/browser/fileapi/file_observers.h" | 19 #include "webkit/browser/fileapi/file_observers.h" |
| 16 #include "webkit/browser/fileapi/file_system_operation.h" | 20 #include "webkit/browser/fileapi/file_system_operation.h" |
| 17 #include "webkit/browser/fileapi/file_system_operation_context.h" | 21 #include "webkit/browser/fileapi/file_system_operation_context.h" |
| 18 #include "webkit/browser/fileapi/file_system_quota_util.h" | 22 #include "webkit/browser/fileapi/file_system_quota_util.h" |
| 19 #include "webkit/browser/fileapi/local_file_util.h" | 23 #include "webkit/browser/fileapi/local_file_util.h" |
| 20 #include "webkit/browser/fileapi/native_file_util.h" | 24 #include "webkit/browser/fileapi/native_file_util.h" |
| 21 #include "webkit/browser/fileapi/quota/quota_reservation.h" | 25 #include "webkit/browser/fileapi/quota/quota_reservation.h" |
| 22 #include "webkit/browser/fileapi/sandbox_file_stream_writer.h" | 26 #include "webkit/browser/fileapi/sandbox_file_stream_writer.h" |
| 27 #include "webkit/browser/fileapi/watcher_manager.h" | |
| 23 #include "webkit/browser/quota/quota_manager.h" | 28 #include "webkit/browser/quota/quota_manager.h" |
| 24 #include "webkit/common/fileapi/file_system_util.h" | 29 #include "webkit/common/fileapi/file_system_util.h" |
| 25 | 30 |
| 26 using storage::FileSystemContext; | 31 using storage::FileSystemContext; |
| 27 using storage::FileSystemOperation; | 32 using storage::FileSystemOperation; |
| 28 using storage::FileSystemOperationContext; | 33 using storage::FileSystemOperationContext; |
| 29 using storage::FileSystemURL; | 34 using storage::FileSystemURL; |
| 30 | 35 |
| 31 namespace content { | 36 namespace content { |
| 32 | 37 |
| 33 namespace { | 38 namespace { |
| 34 | 39 |
| 40 // Stub implementation of storage::LocalFileUtil. | |
| 35 class TestFileUtil : public storage::LocalFileUtil { | 41 class TestFileUtil : public storage::LocalFileUtil { |
| 36 public: | 42 public: |
| 37 explicit TestFileUtil(const base::FilePath& base_path) | 43 explicit TestFileUtil(const base::FilePath& base_path) |
| 38 : base_path_(base_path) {} | 44 : base_path_(base_path) {} |
| 39 virtual ~TestFileUtil() {} | 45 virtual ~TestFileUtil() {} |
| 40 | 46 |
| 41 // LocalFileUtil overrides. | 47 // LocalFileUtil overrides. |
| 42 virtual base::File::Error GetLocalFilePath( | 48 virtual base::File::Error GetLocalFilePath( |
| 43 FileSystemOperationContext* context, | 49 FileSystemOperationContext* context, |
| 44 const FileSystemURL& file_system_url, | 50 const FileSystemURL& file_system_url, |
| 45 base::FilePath* local_file_path) OVERRIDE { | 51 base::FilePath* local_file_path) OVERRIDE { |
| 46 *local_file_path = base_path_.Append(file_system_url.path()); | 52 *local_file_path = base_path_.Append(file_system_url.path()); |
| 47 return base::File::FILE_OK; | 53 return base::File::FILE_OK; |
| 48 } | 54 } |
| 49 | 55 |
| 50 private: | 56 private: |
| 51 base::FilePath base_path_; | 57 base::FilePath base_path_; |
| 52 }; | 58 }; |
| 53 | 59 |
| 60 // Stub implementation of storage::WatcherManager. Emits a fake notification | |
| 61 // after a directory watcher is set successfully. | |
| 62 class TestWatcherManager : public storage::WatcherManager { | |
| 63 public: | |
| 64 TestWatcherManager() : weak_ptr_factory_(this) {} | |
| 65 virtual ~TestWatcherManager() {} | |
| 66 | |
| 67 // storage::WatcherManager overrides. | |
| 68 virtual void AddObserver(Observer* observer) OVERRIDE { | |
| 69 observers_.AddObserver(observer); | |
| 70 } | |
| 71 | |
| 72 virtual void RemoveObserver(Observer* observer) OVERRIDE { | |
| 73 observers_.RemoveObserver(observer); | |
| 74 } | |
| 75 | |
| 76 virtual bool HasObserver(Observer* observer) const OVERRIDE { | |
| 77 return observers_.HasObserver(observer); | |
| 78 } | |
| 79 | |
| 80 virtual void WatchDirectory(const storage::FileSystemURL& url, | |
| 81 bool recursive, | |
| 82 const StatusCallback& callback) OVERRIDE { | |
| 83 if (recursive) { | |
| 84 base::ThreadTaskRunnerHandle::Get()->PostTask( | |
| 85 FROM_HERE, | |
| 86 base::Bind(callback, base::File::FILE_ERROR_INVALID_OPERATION)); | |
| 87 return; | |
| 88 } | |
| 89 | |
| 90 const GURL gurl = url.ToGURL(); | |
| 91 if (watched_urls_.find(gurl) != watched_urls_.end()) { | |
| 92 base::ThreadTaskRunnerHandle::Get()->PostTask( | |
| 93 FROM_HERE, base::Bind(callback, base::File::FILE_ERROR_EXISTS)); | |
| 94 return; | |
| 95 } | |
| 96 | |
| 97 watched_urls_.insert(gurl); | |
| 98 base::ThreadTaskRunnerHandle::Get()->PostTask( | |
| 99 FROM_HERE, base::Bind(callback, base::File::FILE_OK)); | |
| 100 | |
| 101 // Send a fake changed notification. | |
| 102 base::ThreadTaskRunnerHandle::Get()->PostTask( | |
|
sky
2014/08/26 16:45:27
Is base::ThreadTaskRunnerHandle::Get() a separate
mtomasz
2014/08/26 23:04:53
IIUC ThreadTaskRunnerHandle::Get basically returns
sky
2014/08/26 23:51:09
Is there a reason you don't do MessageLoop::curren
| |
| 103 FROM_HERE, | |
| 104 base::Bind(&TestWatcherManager::SendFakeChangeNotification, | |
| 105 weak_ptr_factory_.GetWeakPtr(), | |
| 106 url)); | |
| 107 | |
| 108 // Send a fake removed notification. | |
| 109 base::ThreadTaskRunnerHandle::Get()->PostTask( | |
| 110 FROM_HERE, | |
| 111 base::Bind(&TestWatcherManager::SendFakeRemoveNotification, | |
| 112 weak_ptr_factory_.GetWeakPtr(), | |
| 113 url)); | |
| 114 } | |
| 115 | |
| 116 virtual void UnwatchEntry(const storage::FileSystemURL& url, | |
| 117 const StatusCallback& callback) OVERRIDE { | |
| 118 const GURL gurl = url.ToGURL(); | |
| 119 if (watched_urls_.find(gurl) == watched_urls_.end()) { | |
| 120 base::ThreadTaskRunnerHandle::Get()->PostTask( | |
| 121 FROM_HERE, base::Bind(callback, base::File::FILE_ERROR_NOT_FOUND)); | |
| 122 return; | |
| 123 } | |
| 124 | |
| 125 watched_urls_.erase(gurl); | |
| 126 base::ThreadTaskRunnerHandle::Get()->PostTask( | |
| 127 FROM_HERE, base::Bind(callback, base::File::FILE_OK)); | |
| 128 } | |
| 129 | |
| 130 private: | |
| 131 // Sends a fake notification to each observer about a changed entry | |
| 132 // represented by |url|, as long as it is still being watched. | |
| 133 void SendFakeChangeNotification(const storage::FileSystemURL& url) { | |
| 134 if (watched_urls_.find(url.ToGURL()) == watched_urls_.end()) | |
| 135 return; | |
| 136 | |
| 137 FOR_EACH_OBSERVER(Observer, observers_, OnEntryChanged(url)); | |
| 138 } | |
| 139 | |
| 140 // Sends a fake notification to each observer about a removed entry | |
| 141 // represented by |url|, as long as it is still being watched. | |
| 142 void SendFakeRemoveNotification(const storage::FileSystemURL& url) { | |
| 143 if (watched_urls_.find(url.ToGURL()) == watched_urls_.end()) | |
| 144 return; | |
| 145 | |
| 146 FOR_EACH_OBSERVER(Observer, observers_, OnEntryRemoved(url)); | |
| 147 } | |
| 148 | |
| 149 ObserverList<Observer> observers_; | |
| 150 std::set<GURL> watched_urls_; | |
| 151 | |
| 152 base::WeakPtrFactory<TestWatcherManager> weak_ptr_factory_; | |
| 153 }; | |
| 154 | |
| 54 } // namespace | 155 } // namespace |
| 55 | 156 |
| 56 // This only supports single origin. | 157 // This only supports single origin. |
| 57 class TestFileSystemBackend::QuotaUtil : public storage::FileSystemQuotaUtil, | 158 class TestFileSystemBackend::QuotaUtil : public storage::FileSystemQuotaUtil, |
| 58 public storage::FileUpdateObserver { | 159 public storage::FileUpdateObserver { |
| 59 public: | 160 public: |
| 60 explicit QuotaUtil(base::SequencedTaskRunner* task_runner) | 161 explicit QuotaUtil(base::SequencedTaskRunner* task_runner) |
| 61 : usage_(0), | 162 : usage_(0), |
| 62 task_runner_(task_runner) { | 163 task_runner_(task_runner) { |
| 63 update_observers_ = update_observers_.AddObserver(this, task_runner_.get()); | 164 update_observers_ = update_observers_.AddObserver(this, task_runner_.get()); |
| (...skipping 91 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 155 storage::UpdateObserverList update_observers_; | 256 storage::UpdateObserverList update_observers_; |
| 156 storage::ChangeObserverList change_observers_; | 257 storage::ChangeObserverList change_observers_; |
| 157 }; | 258 }; |
| 158 | 259 |
| 159 TestFileSystemBackend::TestFileSystemBackend( | 260 TestFileSystemBackend::TestFileSystemBackend( |
| 160 base::SequencedTaskRunner* task_runner, | 261 base::SequencedTaskRunner* task_runner, |
| 161 const base::FilePath& base_path) | 262 const base::FilePath& base_path) |
| 162 : base_path_(base_path), | 263 : base_path_(base_path), |
| 163 file_util_( | 264 file_util_( |
| 164 new storage::AsyncFileUtilAdapter(new TestFileUtil(base_path))), | 265 new storage::AsyncFileUtilAdapter(new TestFileUtil(base_path))), |
| 266 watcher_manager_(new TestWatcherManager()), | |
| 165 quota_util_(new QuotaUtil(task_runner)), | 267 quota_util_(new QuotaUtil(task_runner)), |
| 166 require_copy_or_move_validator_(false) { | 268 require_copy_or_move_validator_(false) { |
| 167 } | 269 } |
| 168 | 270 |
| 169 TestFileSystemBackend::~TestFileSystemBackend() { | 271 TestFileSystemBackend::~TestFileSystemBackend() { |
| 170 } | 272 } |
| 171 | 273 |
| 172 bool TestFileSystemBackend::CanHandleType(storage::FileSystemType type) const { | 274 bool TestFileSystemBackend::CanHandleType(storage::FileSystemType type) const { |
| 173 return (type == storage::kFileSystemTypeTest); | 275 return (type == storage::kFileSystemTypeTest); |
| 174 } | 276 } |
| 175 | 277 |
| 176 void TestFileSystemBackend::Initialize(FileSystemContext* context) { | 278 void TestFileSystemBackend::Initialize(FileSystemContext* context) { |
| 177 } | 279 } |
| 178 | 280 |
| 179 void TestFileSystemBackend::ResolveURL(const FileSystemURL& url, | 281 void TestFileSystemBackend::ResolveURL(const FileSystemURL& url, |
| 180 storage::OpenFileSystemMode mode, | 282 storage::OpenFileSystemMode mode, |
| 181 const OpenFileSystemCallback& callback) { | 283 const OpenFileSystemCallback& callback) { |
| 182 callback.Run(GetFileSystemRootURI(url.origin(), url.type()), | 284 callback.Run(GetFileSystemRootURI(url.origin(), url.type()), |
| 183 GetFileSystemName(url.origin(), url.type()), | 285 GetFileSystemName(url.origin(), url.type()), |
| 184 base::File::FILE_OK); | 286 base::File::FILE_OK); |
| 185 } | 287 } |
| 186 | 288 |
| 187 storage::AsyncFileUtil* TestFileSystemBackend::GetAsyncFileUtil( | 289 storage::AsyncFileUtil* TestFileSystemBackend::GetAsyncFileUtil( |
| 188 storage::FileSystemType type) { | 290 storage::FileSystemType type) { |
| 189 return file_util_.get(); | 291 return file_util_.get(); |
| 190 } | 292 } |
| 191 | 293 |
| 294 storage::WatcherManager* TestFileSystemBackend::GetWatcherManager( | |
| 295 storage::FileSystemType type) { | |
| 296 return watcher_manager_.get(); | |
| 297 } | |
| 298 | |
| 192 storage::CopyOrMoveFileValidatorFactory* | 299 storage::CopyOrMoveFileValidatorFactory* |
| 193 TestFileSystemBackend::GetCopyOrMoveFileValidatorFactory( | 300 TestFileSystemBackend::GetCopyOrMoveFileValidatorFactory( |
| 194 storage::FileSystemType type, | 301 storage::FileSystemType type, |
| 195 base::File::Error* error_code) { | 302 base::File::Error* error_code) { |
| 196 DCHECK(error_code); | 303 DCHECK(error_code); |
| 197 *error_code = base::File::FILE_OK; | 304 *error_code = base::File::FILE_OK; |
| 198 if (require_copy_or_move_validator_) { | 305 if (require_copy_or_move_validator_) { |
| 199 if (!copy_or_move_file_validator_factory_) | 306 if (!copy_or_move_file_validator_factory_) |
| 200 *error_code = base::File::FILE_ERROR_SECURITY; | 307 *error_code = base::File::FILE_ERROR_SECURITY; |
| 201 return copy_or_move_file_validator_factory_.get(); | 308 return copy_or_move_file_validator_factory_.get(); |
| (...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 261 return quota_util_->GetUpdateObservers(type); | 368 return quota_util_->GetUpdateObservers(type); |
| 262 } | 369 } |
| 263 | 370 |
| 264 void TestFileSystemBackend::AddFileChangeObserver( | 371 void TestFileSystemBackend::AddFileChangeObserver( |
| 265 storage::FileChangeObserver* observer) { | 372 storage::FileChangeObserver* observer) { |
| 266 quota_util_->AddFileChangeObserver( | 373 quota_util_->AddFileChangeObserver( |
| 267 storage::kFileSystemTypeTest, observer, quota_util_->task_runner()); | 374 storage::kFileSystemTypeTest, observer, quota_util_->task_runner()); |
| 268 } | 375 } |
| 269 | 376 |
| 270 } // namespace content | 377 } // namespace content |
| OLD | NEW |