| 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" |
| 13 #include "webkit/browser/blob/file_stream_reader.h" | 16 #include "webkit/browser/blob/file_stream_reader.h" |
| 14 #include "webkit/browser/fileapi/copy_or_move_file_validator.h" | 17 #include "webkit/browser/fileapi/copy_or_move_file_validator.h" |
| 15 #include "webkit/browser/fileapi/file_observers.h" | 18 #include "webkit/browser/fileapi/file_observers.h" |
| 16 #include "webkit/browser/fileapi/file_system_operation.h" | 19 #include "webkit/browser/fileapi/file_system_operation.h" |
| 17 #include "webkit/browser/fileapi/file_system_operation_context.h" | 20 #include "webkit/browser/fileapi/file_system_operation_context.h" |
| 18 #include "webkit/browser/fileapi/file_system_quota_util.h" | 21 #include "webkit/browser/fileapi/file_system_quota_util.h" |
| 19 #include "webkit/browser/fileapi/local_file_util.h" | 22 #include "webkit/browser/fileapi/local_file_util.h" |
| 20 #include "webkit/browser/fileapi/native_file_util.h" | 23 #include "webkit/browser/fileapi/native_file_util.h" |
| 21 #include "webkit/browser/fileapi/quota/quota_reservation.h" | 24 #include "webkit/browser/fileapi/quota/quota_reservation.h" |
| 22 #include "webkit/browser/fileapi/sandbox_file_stream_writer.h" | 25 #include "webkit/browser/fileapi/sandbox_file_stream_writer.h" |
| 26 #include "webkit/browser/fileapi/watcher_manager.h" |
| 23 #include "webkit/browser/quota/quota_manager.h" | 27 #include "webkit/browser/quota/quota_manager.h" |
| 24 #include "webkit/common/fileapi/file_system_util.h" | 28 #include "webkit/common/fileapi/file_system_util.h" |
| 25 | 29 |
| 26 using fileapi::FileSystemContext; | 30 using fileapi::FileSystemContext; |
| 27 using fileapi::FileSystemOperation; | 31 using fileapi::FileSystemOperation; |
| 28 using fileapi::FileSystemOperationContext; | 32 using fileapi::FileSystemOperationContext; |
| 29 using fileapi::FileSystemURL; | 33 using fileapi::FileSystemURL; |
| 30 | 34 |
| 31 namespace content { | 35 namespace content { |
| 32 | 36 |
| 33 namespace { | 37 namespace { |
| 34 | 38 |
| 39 // Stub implementation of fileapi::LocalFileUtil. |
| 35 class TestFileUtil : public fileapi::LocalFileUtil { | 40 class TestFileUtil : public fileapi::LocalFileUtil { |
| 36 public: | 41 public: |
| 37 explicit TestFileUtil(const base::FilePath& base_path) | 42 explicit TestFileUtil(const base::FilePath& base_path) |
| 38 : base_path_(base_path) {} | 43 : base_path_(base_path) {} |
| 39 virtual ~TestFileUtil() {} | 44 virtual ~TestFileUtil() {} |
| 40 | 45 |
| 41 // LocalFileUtil overrides. | 46 // LocalFileUtil overrides. |
| 42 virtual base::File::Error GetLocalFilePath( | 47 virtual base::File::Error GetLocalFilePath( |
| 43 FileSystemOperationContext* context, | 48 FileSystemOperationContext* context, |
| 44 const FileSystemURL& file_system_url, | 49 const FileSystemURL& file_system_url, |
| 45 base::FilePath* local_file_path) OVERRIDE { | 50 base::FilePath* local_file_path) OVERRIDE { |
| 46 *local_file_path = base_path_.Append(file_system_url.path()); | 51 *local_file_path = base_path_.Append(file_system_url.path()); |
| 47 return base::File::FILE_OK; | 52 return base::File::FILE_OK; |
| 48 } | 53 } |
| 49 | 54 |
| 50 private: | 55 private: |
| 51 base::FilePath base_path_; | 56 base::FilePath base_path_; |
| 52 }; | 57 }; |
| 53 | 58 |
| 59 // Stub implementation of fileapi::WatcherManager. Emits a fake notification |
| 60 // after a directory watcher is set successfully. |
| 61 class TestWatcherManager : public fileapi::WatcherManager { |
| 62 public: |
| 63 TestWatcherManager() : weak_ptr_factory_(this) {} |
| 64 virtual ~TestWatcherManager() {} |
| 65 |
| 66 // fileapi::WatcherManager overrides. |
| 67 virtual void AddObserver(Observer* observer) OVERRIDE { |
| 68 observers_.AddObserver(observer); |
| 69 } |
| 70 |
| 71 virtual void RemoveObserver(Observer* observer) OVERRIDE { |
| 72 observers_.RemoveObserver(observer); |
| 73 } |
| 74 |
| 75 virtual bool HasObserver(Observer* observer) const OVERRIDE { |
| 76 return observers_.HasObserver(observer); |
| 77 } |
| 78 |
| 79 virtual void WatchDirectory(const fileapi::FileSystemURL& url, |
| 80 bool recursive, |
| 81 const StatusCallback& callback) OVERRIDE { |
| 82 if (recursive) { |
| 83 base::MessageLoopProxy::current()->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::MessageLoopProxy::current()->PostTask( |
| 92 FROM_HERE, base::Bind(callback, base::File::FILE_ERROR_EXISTS)); |
| 93 return; |
| 94 } |
| 95 |
| 96 watched_urls_.insert(gurl); |
| 97 base::MessageLoopProxy::current()->PostTask( |
| 98 FROM_HERE, base::Bind(callback, base::File::FILE_OK)); |
| 99 |
| 100 // Send a fake changed notification. |
| 101 base::MessageLoopProxy::current()->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::MessageLoopProxy::current()->PostTask( |
| 109 FROM_HERE, |
| 110 base::Bind(&TestWatcherManager::SendFakeRemoveNotification, |
| 111 weak_ptr_factory_.GetWeakPtr(), |
| 112 url)); |
| 113 } |
| 114 |
| 115 virtual void UnwatchEntry(const fileapi::FileSystemURL& url, |
| 116 const StatusCallback& callback) OVERRIDE { |
| 117 const GURL gurl = url.ToGURL(); |
| 118 if (watched_urls_.find(gurl) == watched_urls_.end()) { |
| 119 base::MessageLoopProxy::current()->PostTask( |
| 120 FROM_HERE, base::Bind(callback, base::File::FILE_ERROR_NOT_FOUND)); |
| 121 return; |
| 122 } |
| 123 |
| 124 watched_urls_.erase(gurl); |
| 125 base::MessageLoopProxy::current()->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 fileapi::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 fileapi::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 |
| 54 } // namespace | 154 } // namespace |
| 55 | 155 |
| 56 // This only supports single origin. | 156 // This only supports single origin. |
| 57 class TestFileSystemBackend::QuotaUtil | 157 class TestFileSystemBackend::QuotaUtil |
| 58 : public fileapi::FileSystemQuotaUtil, | 158 : public fileapi::FileSystemQuotaUtil, |
| 59 public fileapi::FileUpdateObserver { | 159 public fileapi::FileUpdateObserver { |
| 60 public: | 160 public: |
| 61 explicit QuotaUtil(base::SequencedTaskRunner* task_runner) | 161 explicit QuotaUtil(base::SequencedTaskRunner* task_runner) |
| 62 : usage_(0), | 162 : usage_(0), |
| 63 task_runner_(task_runner) { | 163 task_runner_(task_runner) { |
| (...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 156 fileapi::UpdateObserverList update_observers_; | 256 fileapi::UpdateObserverList update_observers_; |
| 157 fileapi::ChangeObserverList change_observers_; | 257 fileapi::ChangeObserverList change_observers_; |
| 158 }; | 258 }; |
| 159 | 259 |
| 160 TestFileSystemBackend::TestFileSystemBackend( | 260 TestFileSystemBackend::TestFileSystemBackend( |
| 161 base::SequencedTaskRunner* task_runner, | 261 base::SequencedTaskRunner* task_runner, |
| 162 const base::FilePath& base_path) | 262 const base::FilePath& base_path) |
| 163 : base_path_(base_path), | 263 : base_path_(base_path), |
| 164 file_util_( | 264 file_util_( |
| 165 new fileapi::AsyncFileUtilAdapter(new TestFileUtil(base_path))), | 265 new fileapi::AsyncFileUtilAdapter(new TestFileUtil(base_path))), |
| 266 watcher_manager_(new TestWatcherManager()), |
| 166 quota_util_(new QuotaUtil(task_runner)), | 267 quota_util_(new QuotaUtil(task_runner)), |
| 167 require_copy_or_move_validator_(false) { | 268 require_copy_or_move_validator_(false) { |
| 168 } | 269 } |
| 169 | 270 |
| 170 TestFileSystemBackend::~TestFileSystemBackend() { | 271 TestFileSystemBackend::~TestFileSystemBackend() { |
| 171 } | 272 } |
| 172 | 273 |
| 173 bool TestFileSystemBackend::CanHandleType(fileapi::FileSystemType type) const { | 274 bool TestFileSystemBackend::CanHandleType(fileapi::FileSystemType type) const { |
| 174 return (type == fileapi::kFileSystemTypeTest); | 275 return (type == fileapi::kFileSystemTypeTest); |
| 175 } | 276 } |
| 176 | 277 |
| 177 void TestFileSystemBackend::Initialize(FileSystemContext* context) { | 278 void TestFileSystemBackend::Initialize(FileSystemContext* context) { |
| 178 } | 279 } |
| 179 | 280 |
| 180 void TestFileSystemBackend::ResolveURL(const FileSystemURL& url, | 281 void TestFileSystemBackend::ResolveURL(const FileSystemURL& url, |
| 181 fileapi::OpenFileSystemMode mode, | 282 fileapi::OpenFileSystemMode mode, |
| 182 const OpenFileSystemCallback& callback) { | 283 const OpenFileSystemCallback& callback) { |
| 183 callback.Run(GetFileSystemRootURI(url.origin(), url.type()), | 284 callback.Run(GetFileSystemRootURI(url.origin(), url.type()), |
| 184 GetFileSystemName(url.origin(), url.type()), | 285 GetFileSystemName(url.origin(), url.type()), |
| 185 base::File::FILE_OK); | 286 base::File::FILE_OK); |
| 186 } | 287 } |
| 187 | 288 |
| 188 fileapi::AsyncFileUtil* TestFileSystemBackend::GetAsyncFileUtil( | 289 fileapi::AsyncFileUtil* TestFileSystemBackend::GetAsyncFileUtil( |
| 189 fileapi::FileSystemType type) { | 290 fileapi::FileSystemType type) { |
| 190 return file_util_.get(); | 291 return file_util_.get(); |
| 191 } | 292 } |
| 192 | 293 |
| 294 fileapi::WatcherManager* TestFileSystemBackend::GetWatcherManager( |
| 295 fileapi::FileSystemType type) { |
| 296 return watcher_manager_.get(); |
| 297 } |
| 298 |
| 193 fileapi::CopyOrMoveFileValidatorFactory* | 299 fileapi::CopyOrMoveFileValidatorFactory* |
| 194 TestFileSystemBackend::GetCopyOrMoveFileValidatorFactory( | 300 TestFileSystemBackend::GetCopyOrMoveFileValidatorFactory( |
| 195 fileapi::FileSystemType type, | 301 fileapi::FileSystemType type, |
| 196 base::File::Error* error_code) { | 302 base::File::Error* error_code) { |
| 197 DCHECK(error_code); | 303 DCHECK(error_code); |
| 198 *error_code = base::File::FILE_OK; | 304 *error_code = base::File::FILE_OK; |
| 199 if (require_copy_or_move_validator_) { | 305 if (require_copy_or_move_validator_) { |
| 200 if (!copy_or_move_file_validator_factory_) | 306 if (!copy_or_move_file_validator_factory_) |
| 201 *error_code = base::File::FILE_ERROR_SECURITY; | 307 *error_code = base::File::FILE_ERROR_SECURITY; |
| 202 return copy_or_move_file_validator_factory_.get(); | 308 return copy_or_move_file_validator_factory_.get(); |
| (...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 257 return quota_util_->GetUpdateObservers(type); | 363 return quota_util_->GetUpdateObservers(type); |
| 258 } | 364 } |
| 259 | 365 |
| 260 void TestFileSystemBackend::AddFileChangeObserver( | 366 void TestFileSystemBackend::AddFileChangeObserver( |
| 261 fileapi::FileChangeObserver* observer) { | 367 fileapi::FileChangeObserver* observer) { |
| 262 quota_util_->AddFileChangeObserver( | 368 quota_util_->AddFileChangeObserver( |
| 263 fileapi::kFileSystemTypeTest, observer, quota_util_->task_runner()); | 369 fileapi::kFileSystemTypeTest, observer, quota_util_->task_runner()); |
| 264 } | 370 } |
| 265 | 371 |
| 266 } // namespace content | 372 } // namespace content |
| OLD | NEW |