Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(94)

Side by Side Diff: content/public/test/test_file_system_backend.cc

Issue 642343004: [ew] Simplify the entry watcher logic. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 6 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 12 matching lines...) Expand all
50 const FileSystemURL& file_system_url, 49 const FileSystemURL& file_system_url,
51 base::FilePath* local_file_path) override { 50 base::FilePath* local_file_path) override {
52 *local_file_path = base_path_.Append(file_system_url.path()); 51 *local_file_path = base_path_.Append(file_system_url.path());
53 return base::File::FILE_OK; 52 return base::File::FILE_OK;
54 } 53 }
55 54
56 private: 55 private:
57 base::FilePath base_path_; 56 base::FilePath base_path_;
58 }; 57 };
59 58
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(
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
155 } // namespace 59 } // namespace
156 60
157 // This only supports single origin. 61 // This only supports single origin.
158 class TestFileSystemBackend::QuotaUtil : public storage::FileSystemQuotaUtil, 62 class TestFileSystemBackend::QuotaUtil : public storage::FileSystemQuotaUtil,
159 public storage::FileUpdateObserver { 63 public storage::FileUpdateObserver {
160 public: 64 public:
161 QuotaUtil() : usage_(0) {} 65 QuotaUtil() : usage_(0) {}
162 virtual ~QuotaUtil() {} 66 virtual ~QuotaUtil() {}
163 67
164 // FileSystemQuotaUtil overrides. 68 // FileSystemQuotaUtil overrides.
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after
211 DISALLOW_COPY_AND_ASSIGN(QuotaUtil); 115 DISALLOW_COPY_AND_ASSIGN(QuotaUtil);
212 }; 116 };
213 117
214 TestFileSystemBackend::TestFileSystemBackend( 118 TestFileSystemBackend::TestFileSystemBackend(
215 base::SequencedTaskRunner* task_runner, 119 base::SequencedTaskRunner* task_runner,
216 const base::FilePath& base_path) 120 const base::FilePath& base_path)
217 : base_path_(base_path), 121 : base_path_(base_path),
218 task_runner_(task_runner), 122 task_runner_(task_runner),
219 file_util_( 123 file_util_(
220 new storage::AsyncFileUtilAdapter(new TestFileUtil(base_path))), 124 new storage::AsyncFileUtilAdapter(new TestFileUtil(base_path))),
221 watcher_manager_(new TestWatcherManager()),
222 quota_util_(new QuotaUtil), 125 quota_util_(new QuotaUtil),
223 require_copy_or_move_validator_(false) { 126 require_copy_or_move_validator_(false) {
224 update_observers_ = 127 update_observers_ =
225 update_observers_.AddObserver(quota_util_.get(), task_runner_.get()); 128 update_observers_.AddObserver(quota_util_.get(), task_runner_.get());
226 } 129 }
227 130
228 TestFileSystemBackend::~TestFileSystemBackend() { 131 TestFileSystemBackend::~TestFileSystemBackend() {
229 } 132 }
230 133
231 bool TestFileSystemBackend::CanHandleType(storage::FileSystemType type) const { 134 bool TestFileSystemBackend::CanHandleType(storage::FileSystemType type) const {
(...skipping 11 matching lines...) Expand all
243 base::File::FILE_OK); 146 base::File::FILE_OK);
244 } 147 }
245 148
246 storage::AsyncFileUtil* TestFileSystemBackend::GetAsyncFileUtil( 149 storage::AsyncFileUtil* TestFileSystemBackend::GetAsyncFileUtil(
247 storage::FileSystemType type) { 150 storage::FileSystemType type) {
248 return file_util_.get(); 151 return file_util_.get();
249 } 152 }
250 153
251 storage::WatcherManager* TestFileSystemBackend::GetWatcherManager( 154 storage::WatcherManager* TestFileSystemBackend::GetWatcherManager(
252 storage::FileSystemType type) { 155 storage::FileSystemType type) {
253 return watcher_manager_.get(); 156 return NULL;
254 } 157 }
255 158
256 storage::CopyOrMoveFileValidatorFactory* 159 storage::CopyOrMoveFileValidatorFactory*
257 TestFileSystemBackend::GetCopyOrMoveFileValidatorFactory( 160 TestFileSystemBackend::GetCopyOrMoveFileValidatorFactory(
258 storage::FileSystemType type, 161 storage::FileSystemType type,
259 base::File::Error* error_code) { 162 base::File::Error* error_code) {
260 DCHECK(error_code); 163 DCHECK(error_code);
261 *error_code = base::File::FILE_OK; 164 *error_code = base::File::FILE_OK;
262 if (require_copy_or_move_validator_) { 165 if (require_copy_or_move_validator_) {
263 if (!copy_or_move_file_validator_factory_) 166 if (!copy_or_move_file_validator_factory_)
(...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after
335 return NULL; 238 return NULL;
336 } 239 }
337 240
338 void TestFileSystemBackend::AddFileChangeObserver( 241 void TestFileSystemBackend::AddFileChangeObserver(
339 storage::FileChangeObserver* observer) { 242 storage::FileChangeObserver* observer) {
340 change_observers_ = 243 change_observers_ =
341 change_observers_.AddObserver(observer, task_runner_.get()); 244 change_observers_.AddObserver(observer, task_runner_.get());
342 } 245 }
343 246
344 } // namespace content 247 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698