Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "content/public/test/test_fileapi_operation_waiter.h" | |
| 6 | |
| 7 #include "base/bind_helpers.h" | |
| 8 #include "base/lazy_instance.h" | |
| 9 #include "base/observer_list.h" | |
| 10 #include "content/public/browser/browser_thread.h" | |
| 11 #include "storage/browser/fileapi/file_system_context.h" | |
| 12 #include "storage/browser/fileapi/sandbox_file_system_backend_delegate.h" | |
| 13 | |
| 14 namespace content { | |
| 15 | |
| 16 using storage::FileSystemContext; | |
| 17 using storage::FileSystemURL; | |
| 18 using storage::FileUpdateObserver; | |
| 19 | |
| 20 namespace { | |
| 21 | |
| 22 // Because of how fileapi internally creates copies of its observer lists, | |
| 23 // removing an observer is not a supported operation. So to support temporary, | |
|
alexmos
2016/12/02 00:01:56
That does sound like an awkward observer API. So
ncarter (slow)
2016/12/02 19:17:45
From what I can tell, the other FileUpdateObserver
| |
| 24 // test-style observers, we create one long-lived global observer instance that | |
| 25 // dispatches to a list of short-lived observers. | |
| 26 // | |
| 27 // This object operates on the UI thread, though it registers itself as an | |
| 28 // observer on the IO thread. | |
| 29 class FileUpdateObserverMultiplexer : public FileUpdateObserver { | |
| 30 public: | |
| 31 FileUpdateObserverMultiplexer() {} | |
| 32 | |
| 33 void AddObserver(FileSystemContext* context, FileUpdateObserver* observer) { | |
| 34 DCHECK_CURRENTLY_ON(BrowserThread::UI); | |
| 35 | |
| 36 // On first initialization, install ourself as an observer. We never | |
| 37 // uninstall, because we expect to leak. | |
| 38 if (!context_) { | |
| 39 // Currently we only listen to kFileSystemTypeTemporary; it should be fine | |
| 40 // to add other filesystem types as needed. | |
| 41 context_ = context; | |
| 42 base::Closure task = base::Bind( | |
| 43 &storage::SandboxFileSystemBackendDelegate::AddFileUpdateObserver, | |
| 44 base::Unretained(context_->sandbox_delegate()), | |
| 45 storage::kFileSystemTypeTemporary, base::Unretained(this), | |
| 46 base::RetainedRef( | |
| 47 BrowserThread::GetTaskRunnerForThread(BrowserThread::UI))); | |
| 48 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, task); | |
| 49 } | |
| 50 | |
| 51 CHECK_EQ(context, context_) << "Multiprofile is not implemented"; | |
| 52 | |
| 53 observers_.AddObserver(observer); | |
| 54 } | |
| 55 | |
| 56 void RemoveObserver(FileUpdateObserver* observer) { | |
| 57 DCHECK_CURRENTLY_ON(BrowserThread::UI); | |
| 58 observers_.RemoveObserver(observer); | |
| 59 } | |
| 60 | |
| 61 // FileUpdateObserver overrides: | |
| 62 void OnStartUpdate(const FileSystemURL& url) override { | |
| 63 DCHECK_CURRENTLY_ON(BrowserThread::UI); | |
| 64 for (auto& observer : observers_) | |
| 65 observer.OnStartUpdate(url); | |
| 66 } | |
| 67 void OnUpdate(const FileSystemURL& url, int64_t delta) override { | |
| 68 DCHECK_CURRENTLY_ON(BrowserThread::UI); | |
| 69 for (auto& observer : observers_) | |
| 70 observer.OnUpdate(url, delta); | |
| 71 } | |
| 72 void OnEndUpdate(const FileSystemURL& url) override { | |
| 73 DCHECK_CURRENTLY_ON(BrowserThread::UI); | |
| 74 | |
|
alexmos
2016/12/02 00:01:56
nit: no blank line (for consistency with the other
| |
| 75 for (auto& observer : observers_) | |
| 76 observer.OnEndUpdate(url); | |
| 77 } | |
| 78 | |
| 79 private: | |
| 80 FileSystemContext* context_ = nullptr; | |
| 81 base::ObserverList<FileUpdateObserver> observers_; | |
| 82 DISALLOW_COPY_AND_ASSIGN(FileUpdateObserverMultiplexer); | |
| 83 }; | |
| 84 | |
| 85 static base::LazyInstance<FileUpdateObserverMultiplexer>::Leaky g_multiplexer = | |
| 86 LAZY_INSTANCE_INITIALIZER; | |
| 87 | |
| 88 } // namespace | |
| 89 | |
| 90 TestFileapiOperationWaiter::TestFileapiOperationWaiter( | |
| 91 FileSystemContext* context) { | |
| 92 g_multiplexer.Get().AddObserver(context, this); | |
| 93 } | |
| 94 | |
| 95 TestFileapiOperationWaiter::~TestFileapiOperationWaiter() { | |
| 96 g_multiplexer.Get().RemoveObserver(this); | |
| 97 } | |
| 98 | |
| 99 void TestFileapiOperationWaiter::WaitForEndUpdate() { | |
| 100 run_loop_.Run(); | |
| 101 } | |
| 102 | |
| 103 void TestFileapiOperationWaiter::OnStartUpdate(const FileSystemURL& url) { | |
| 104 did_start_update_ = true; | |
| 105 } | |
| 106 | |
| 107 void TestFileapiOperationWaiter::OnUpdate(const FileSystemURL& url, | |
| 108 int64_t delta) {} | |
| 109 | |
| 110 void TestFileapiOperationWaiter::OnEndUpdate(const FileSystemURL& url) { | |
| 111 run_loop_.Quit(); | |
| 112 } | |
| 113 | |
| 114 } // namespace content | |
| OLD | NEW |