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/files/file.h" | 11 #include "base/files/file.h" |
| 12 #include "base/files/file_util.h" | 12 #include "base/files/file_util.h" |
| 13 #include "base/memory/weak_ptr.h" | 13 #include "base/memory/weak_ptr.h" |
| 14 #include "base/observer_list.h" | 14 #include "base/observer_list.h" |
| 15 #include "base/sequenced_task_runner.h" | 15 #include "base/sequenced_task_runner.h" |
| 16 #include "base/thread_task_runner_handle.h" | 16 #include "base/thread_task_runner_handle.h" |
| 17 #include "storage/browser/blob/file_stream_reader.h" | 17 #include "storage/browser/blob/file_stream_reader.h" |
| 18 #include "storage/browser/fileapi/copy_or_move_file_validator.h" | 18 #include "storage/browser/fileapi/copy_or_move_file_validator.h" |
| 19 #include "storage/browser/fileapi/file_observers.h" | 19 #include "storage/browser/fileapi/file_observers.h" |
| 20 #include "storage/browser/fileapi/file_system_operation.h" | 20 #include "storage/browser/fileapi/file_system_operation.h" |
| 21 #include "storage/browser/fileapi/file_system_operation_context.h" | 21 #include "storage/browser/fileapi/file_system_operation_context.h" |
| 22 #include "storage/browser/fileapi/file_system_quota_util.h" | 22 #include "storage/browser/fileapi/file_system_quota_util.h" |
| 23 #include "storage/browser/fileapi/local_file_util.h" | 23 #include "storage/browser/fileapi/local_file_util.h" |
| 24 #include "storage/browser/fileapi/native_file_util.h" | 24 #include "storage/browser/fileapi/native_file_util.h" |
| 25 #include "storage/browser/fileapi/quota/quota_reservation.h" | 25 #include "storage/browser/fileapi/quota/quota_reservation.h" |
| 26 #include "storage/browser/fileapi/sandbox_file_stream_writer.h" | 26 #include "storage/browser/fileapi/sandbox_file_stream_writer.h" |
| 27 #include "storage/browser/fileapi/watcher_manager.h" | |
| 28 #include "storage/browser/quota/quota_manager.h" | 27 #include "storage/browser/quota/quota_manager.h" |
| 29 #include "storage/common/fileapi/file_system_util.h" | 28 #include "storage/common/fileapi/file_system_util.h" |
| 30 | 29 |
| 31 using storage::FileSystemContext; | 30 using storage::FileSystemContext; |
| 32 using storage::FileSystemOperation; | 31 using storage::FileSystemOperation; |
| 33 using storage::FileSystemOperationContext; | 32 using storage::FileSystemOperationContext; |
| 34 using storage::FileSystemURL; | 33 using storage::FileSystemURL; |
| 35 | 34 |
| 36 namespace content { | 35 namespace content { |
| 37 | 36 |
| (...skipping 11 matching lines...) Expand all Loading... | |
| 49 const FileSystemURL& file_system_url, | 48 const FileSystemURL& file_system_url, |
| 50 base::FilePath* local_file_path) override { | 49 base::FilePath* local_file_path) override { |
| 51 *local_file_path = base_path_.Append(file_system_url.path()); | 50 *local_file_path = base_path_.Append(file_system_url.path()); |
| 52 return base::File::FILE_OK; | 51 return base::File::FILE_OK; |
| 53 } | 52 } |
| 54 | 53 |
| 55 private: | 54 private: |
| 56 base::FilePath base_path_; | 55 base::FilePath base_path_; |
| 57 }; | 56 }; |
| 58 | 57 |
| 59 // Stub implementation of storage::WatcherManager. Emits a fake notification | |
| 60 // after a directory watcher is set successfully. | |
| 61 class TestWatcherManager : public storage::WatcherManager { | |
| 62 public: | |
| 63 TestWatcherManager() : weak_ptr_factory_(this) {} | |
| 64 ~TestWatcherManager() override {} | |
| 65 | |
| 66 // storage::WatcherManager overrides. | |
| 67 void AddObserver(Observer* observer) override { | |
| 68 observers_.AddObserver(observer); | |
| 69 } | |
| 70 | |
| 71 void RemoveObserver(Observer* observer) override { | |
| 72 observers_.RemoveObserver(observer); | |
| 73 } | |
| 74 | |
| 75 bool HasObserver(Observer* observer) const override { | |
| 76 return observers_.HasObserver(observer); | |
| 77 } | |
| 78 | |
| 79 void WatchDirectory(const storage::FileSystemURL& url, | |
| 80 bool recursive, | |
| 81 const StatusCallback& callback) override { | |
| 82 if (recursive) { | |
| 83 base::ThreadTaskRunnerHandle::Get()->PostTask( | |
| 84 FROM_HERE, | |
| 85 base::Bind(callback, base::File::FILE_ERROR_INVALID_OPERATION)); | |
| 86 return; | |
| 87 } | |
| 88 | |
| 89 const GURL gurl = url.ToGURL(); | |
| 90 if (watched_urls_.find(gurl) != watched_urls_.end()) { | |
| 91 base::ThreadTaskRunnerHandle::Get()->PostTask( | |
| 92 FROM_HERE, base::Bind(callback, base::File::FILE_ERROR_EXISTS)); | |
| 93 return; | |
| 94 } | |
| 95 | |
| 96 watched_urls_.insert(gurl); | |
| 97 base::ThreadTaskRunnerHandle::Get()->PostTask( | |
| 98 FROM_HERE, base::Bind(callback, base::File::FILE_OK)); | |
| 99 | |
| 100 // Send a fake changed notification. | |
| 101 base::ThreadTaskRunnerHandle::Get()->PostTask( | |
| 102 FROM_HERE, | |
| 103 base::Bind(&TestWatcherManager::SendFakeChangeNotification, | |
| 104 weak_ptr_factory_.GetWeakPtr(), | |
| 105 url)); | |
| 106 | |
| 107 // Send a fake removed notification. | |
| 108 base::ThreadTaskRunnerHandle::Get()->PostTask( | |
| 109 FROM_HERE, | |
| 110 base::Bind(&TestWatcherManager::SendFakeRemoveNotification, | |
| 111 weak_ptr_factory_.GetWeakPtr(), | |
| 112 url)); | |
| 113 } | |
| 114 | |
| 115 void UnwatchEntry(const storage::FileSystemURL& url, | |
| 116 const StatusCallback& callback) override { | |
| 117 const GURL gurl = url.ToGURL(); | |
| 118 if (watched_urls_.find(gurl) == watched_urls_.end()) { | |
| 119 base::ThreadTaskRunnerHandle::Get()->PostTask( | |
| 120 FROM_HERE, base::Bind(callback, base::File::FILE_ERROR_NOT_FOUND)); | |
| 121 return; | |
| 122 } | |
| 123 | |
| 124 watched_urls_.erase(gurl); | |
| 125 base::ThreadTaskRunnerHandle::Get()->PostTask( | |
| 126 FROM_HERE, base::Bind(callback, base::File::FILE_OK)); | |
| 127 } | |
| 128 | |
| 129 private: | |
| 130 // Sends a fake notification to each observer about a changed entry | |
| 131 // represented by |url|, as long as it is still being watched. | |
| 132 void SendFakeChangeNotification(const storage::FileSystemURL& url) { | |
| 133 if (watched_urls_.find(url.ToGURL()) == watched_urls_.end()) | |
| 134 return; | |
| 135 | |
| 136 FOR_EACH_OBSERVER(Observer, observers_, OnEntryChanged(url)); | |
| 137 } | |
| 138 | |
| 139 // Sends a fake notification to each observer about a removed entry | |
| 140 // represented by |url|, as long as it is still being watched. | |
| 141 void SendFakeRemoveNotification(const storage::FileSystemURL& url) { | |
| 142 if (watched_urls_.find(url.ToGURL()) == watched_urls_.end()) | |
| 143 return; | |
| 144 | |
| 145 FOR_EACH_OBSERVER(Observer, observers_, OnEntryRemoved(url)); | |
| 146 } | |
| 147 | |
| 148 ObserverList<Observer> observers_; | |
| 149 std::set<GURL> watched_urls_; | |
| 150 | |
| 151 base::WeakPtrFactory<TestWatcherManager> weak_ptr_factory_; | |
| 152 }; | |
| 153 | |
| 154 } // namespace | 58 } // namespace |
| 155 | 59 |
| 156 // This only supports single origin. | 60 // This only supports single origin. |
| 157 class TestFileSystemBackend::QuotaUtil : public storage::FileSystemQuotaUtil, | 61 class TestFileSystemBackend::QuotaUtil : public storage::FileSystemQuotaUtil, |
| 158 public storage::FileUpdateObserver { | 62 public storage::FileUpdateObserver { |
| 159 public: | 63 public: |
| 160 QuotaUtil() : usage_(0) {} | 64 QuotaUtil() : usage_(0) {} |
| 161 ~QuotaUtil() override {} | 65 ~QuotaUtil() override {} |
| 162 | 66 |
| 163 // FileSystemQuotaUtil overrides. | 67 // FileSystemQuotaUtil overrides. |
| (...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 207 DISALLOW_COPY_AND_ASSIGN(QuotaUtil); | 111 DISALLOW_COPY_AND_ASSIGN(QuotaUtil); |
| 208 }; | 112 }; |
| 209 | 113 |
| 210 TestFileSystemBackend::TestFileSystemBackend( | 114 TestFileSystemBackend::TestFileSystemBackend( |
| 211 base::SequencedTaskRunner* task_runner, | 115 base::SequencedTaskRunner* task_runner, |
| 212 const base::FilePath& base_path) | 116 const base::FilePath& base_path) |
| 213 : base_path_(base_path), | 117 : base_path_(base_path), |
| 214 task_runner_(task_runner), | 118 task_runner_(task_runner), |
| 215 file_util_( | 119 file_util_( |
| 216 new storage::AsyncFileUtilAdapter(new TestFileUtil(base_path))), | 120 new storage::AsyncFileUtilAdapter(new TestFileUtil(base_path))), |
| 217 watcher_manager_(new TestWatcherManager()), | |
| 218 quota_util_(new QuotaUtil), | 121 quota_util_(new QuotaUtil), |
| 219 require_copy_or_move_validator_(false) { | 122 require_copy_or_move_validator_(false) { |
| 220 update_observers_ = | 123 update_observers_ = |
| 221 update_observers_.AddObserver(quota_util_.get(), task_runner_.get()); | 124 update_observers_.AddObserver(quota_util_.get(), task_runner_.get()); |
| 222 } | 125 } |
| 223 | 126 |
| 224 TestFileSystemBackend::~TestFileSystemBackend() { | 127 TestFileSystemBackend::~TestFileSystemBackend() { |
| 225 } | 128 } |
| 226 | 129 |
| 227 bool TestFileSystemBackend::CanHandleType(storage::FileSystemType type) const { | 130 bool TestFileSystemBackend::CanHandleType(storage::FileSystemType type) const { |
| (...skipping 11 matching lines...) Expand all Loading... | |
| 239 base::File::FILE_OK); | 142 base::File::FILE_OK); |
| 240 } | 143 } |
| 241 | 144 |
| 242 storage::AsyncFileUtil* TestFileSystemBackend::GetAsyncFileUtil( | 145 storage::AsyncFileUtil* TestFileSystemBackend::GetAsyncFileUtil( |
| 243 storage::FileSystemType type) { | 146 storage::FileSystemType type) { |
| 244 return file_util_.get(); | 147 return file_util_.get(); |
| 245 } | 148 } |
| 246 | 149 |
| 247 storage::WatcherManager* TestFileSystemBackend::GetWatcherManager( | 150 storage::WatcherManager* TestFileSystemBackend::GetWatcherManager( |
| 248 storage::FileSystemType type) { | 151 storage::FileSystemType type) { |
| 249 return watcher_manager_.get(); | 152 return NULL; |
|
sky
2014/10/23 17:03:20
nullptr
| |
| 250 } | 153 } |
| 251 | 154 |
| 252 storage::CopyOrMoveFileValidatorFactory* | 155 storage::CopyOrMoveFileValidatorFactory* |
| 253 TestFileSystemBackend::GetCopyOrMoveFileValidatorFactory( | 156 TestFileSystemBackend::GetCopyOrMoveFileValidatorFactory( |
| 254 storage::FileSystemType type, | 157 storage::FileSystemType type, |
| 255 base::File::Error* error_code) { | 158 base::File::Error* error_code) { |
| 256 DCHECK(error_code); | 159 DCHECK(error_code); |
| 257 *error_code = base::File::FILE_OK; | 160 *error_code = base::File::FILE_OK; |
| 258 if (require_copy_or_move_validator_) { | 161 if (require_copy_or_move_validator_) { |
| 259 if (!copy_or_move_file_validator_factory_) | 162 if (!copy_or_move_file_validator_factory_) |
| (...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 331 return NULL; | 234 return NULL; |
| 332 } | 235 } |
| 333 | 236 |
| 334 void TestFileSystemBackend::AddFileChangeObserver( | 237 void TestFileSystemBackend::AddFileChangeObserver( |
| 335 storage::FileChangeObserver* observer) { | 238 storage::FileChangeObserver* observer) { |
| 336 change_observers_ = | 239 change_observers_ = |
| 337 change_observers_.AddObserver(observer, task_runner_.get()); | 240 change_observers_.AddObserver(observer, task_runner_.get()); |
| 338 } | 241 } |
| 339 | 242 |
| 340 } // namespace content | 243 } // namespace content |
| OLD | NEW |