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 fileapi::FileSystemContext; | 31 using fileapi::FileSystemContext; |
27 using fileapi::FileSystemOperation; | 32 using fileapi::FileSystemOperation; |
28 using fileapi::FileSystemOperationContext; | 33 using fileapi::FileSystemOperationContext; |
29 using fileapi::FileSystemURL; | 34 using fileapi::FileSystemURL; |
30 | 35 |
31 namespace content { | 36 namespace content { |
32 | 37 |
33 namespace { | 38 namespace { |
34 | 39 |
| 40 // Stub implementation of fileapi::LocalFileUtil. |
35 class TestFileUtil : public fileapi::LocalFileUtil { | 41 class TestFileUtil : public fileapi::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 fileapi::WatcherManager. Emits a fake notification |
| 61 // after a directory watcher is set successfully. |
| 62 class TestWatcherManager : public fileapi::WatcherManager { |
| 63 public: |
| 64 TestWatcherManager() : weak_ptr_factory_(this) {} |
| 65 virtual ~TestWatcherManager() {} |
| 66 |
| 67 // fileapi::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 fileapi::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( |
| 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 fileapi::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 fileapi::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 fileapi::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 | 158 class TestFileSystemBackend::QuotaUtil |
58 : public fileapi::FileSystemQuotaUtil, | 159 : public fileapi::FileSystemQuotaUtil, |
59 public fileapi::FileUpdateObserver { | 160 public fileapi::FileUpdateObserver { |
60 public: | 161 public: |
61 explicit QuotaUtil(base::SequencedTaskRunner* task_runner) | 162 explicit QuotaUtil(base::SequencedTaskRunner* task_runner) |
62 : usage_(0), | 163 : usage_(0), |
63 task_runner_(task_runner) { | 164 task_runner_(task_runner) { |
(...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
156 fileapi::UpdateObserverList update_observers_; | 257 fileapi::UpdateObserverList update_observers_; |
157 fileapi::ChangeObserverList change_observers_; | 258 fileapi::ChangeObserverList change_observers_; |
158 }; | 259 }; |
159 | 260 |
160 TestFileSystemBackend::TestFileSystemBackend( | 261 TestFileSystemBackend::TestFileSystemBackend( |
161 base::SequencedTaskRunner* task_runner, | 262 base::SequencedTaskRunner* task_runner, |
162 const base::FilePath& base_path) | 263 const base::FilePath& base_path) |
163 : base_path_(base_path), | 264 : base_path_(base_path), |
164 file_util_( | 265 file_util_( |
165 new fileapi::AsyncFileUtilAdapter(new TestFileUtil(base_path))), | 266 new fileapi::AsyncFileUtilAdapter(new TestFileUtil(base_path))), |
| 267 watcher_manager_(new TestWatcherManager()), |
166 quota_util_(new QuotaUtil(task_runner)), | 268 quota_util_(new QuotaUtil(task_runner)), |
167 require_copy_or_move_validator_(false) { | 269 require_copy_or_move_validator_(false) { |
168 } | 270 } |
169 | 271 |
170 TestFileSystemBackend::~TestFileSystemBackend() { | 272 TestFileSystemBackend::~TestFileSystemBackend() { |
171 } | 273 } |
172 | 274 |
173 bool TestFileSystemBackend::CanHandleType(fileapi::FileSystemType type) const { | 275 bool TestFileSystemBackend::CanHandleType(fileapi::FileSystemType type) const { |
174 return (type == fileapi::kFileSystemTypeTest); | 276 return (type == fileapi::kFileSystemTypeTest); |
175 } | 277 } |
176 | 278 |
177 void TestFileSystemBackend::Initialize(FileSystemContext* context) { | 279 void TestFileSystemBackend::Initialize(FileSystemContext* context) { |
178 } | 280 } |
179 | 281 |
180 void TestFileSystemBackend::ResolveURL(const FileSystemURL& url, | 282 void TestFileSystemBackend::ResolveURL(const FileSystemURL& url, |
181 fileapi::OpenFileSystemMode mode, | 283 fileapi::OpenFileSystemMode mode, |
182 const OpenFileSystemCallback& callback) { | 284 const OpenFileSystemCallback& callback) { |
183 callback.Run(GetFileSystemRootURI(url.origin(), url.type()), | 285 callback.Run(GetFileSystemRootURI(url.origin(), url.type()), |
184 GetFileSystemName(url.origin(), url.type()), | 286 GetFileSystemName(url.origin(), url.type()), |
185 base::File::FILE_OK); | 287 base::File::FILE_OK); |
186 } | 288 } |
187 | 289 |
188 fileapi::AsyncFileUtil* TestFileSystemBackend::GetAsyncFileUtil( | 290 fileapi::AsyncFileUtil* TestFileSystemBackend::GetAsyncFileUtil( |
189 fileapi::FileSystemType type) { | 291 fileapi::FileSystemType type) { |
190 return file_util_.get(); | 292 return file_util_.get(); |
191 } | 293 } |
192 | 294 |
| 295 fileapi::WatcherManager* TestFileSystemBackend::GetWatcherManager( |
| 296 fileapi::FileSystemType type) { |
| 297 return watcher_manager_.get(); |
| 298 } |
| 299 |
193 fileapi::CopyOrMoveFileValidatorFactory* | 300 fileapi::CopyOrMoveFileValidatorFactory* |
194 TestFileSystemBackend::GetCopyOrMoveFileValidatorFactory( | 301 TestFileSystemBackend::GetCopyOrMoveFileValidatorFactory( |
195 fileapi::FileSystemType type, | 302 fileapi::FileSystemType type, |
196 base::File::Error* error_code) { | 303 base::File::Error* error_code) { |
197 DCHECK(error_code); | 304 DCHECK(error_code); |
198 *error_code = base::File::FILE_OK; | 305 *error_code = base::File::FILE_OK; |
199 if (require_copy_or_move_validator_) { | 306 if (require_copy_or_move_validator_) { |
200 if (!copy_or_move_file_validator_factory_) | 307 if (!copy_or_move_file_validator_factory_) |
201 *error_code = base::File::FILE_ERROR_SECURITY; | 308 *error_code = base::File::FILE_ERROR_SECURITY; |
202 return copy_or_move_file_validator_factory_.get(); | 309 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); | 364 return quota_util_->GetUpdateObservers(type); |
258 } | 365 } |
259 | 366 |
260 void TestFileSystemBackend::AddFileChangeObserver( | 367 void TestFileSystemBackend::AddFileChangeObserver( |
261 fileapi::FileChangeObserver* observer) { | 368 fileapi::FileChangeObserver* observer) { |
262 quota_util_->AddFileChangeObserver( | 369 quota_util_->AddFileChangeObserver( |
263 fileapi::kFileSystemTypeTest, observer, quota_util_->task_runner()); | 370 fileapi::kFileSystemTypeTest, observer, quota_util_->task_runner()); |
264 } | 371 } |
265 | 372 |
266 } // namespace content | 373 } // namespace content |
OLD | NEW |