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, |
| 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 for (auto& observer : observers_) |
| 75 observer.OnEndUpdate(url); |
| 76 } |
| 77 |
| 78 private: |
| 79 FileSystemContext* context_ = nullptr; |
| 80 base::ObserverList<FileUpdateObserver> observers_; |
| 81 DISALLOW_COPY_AND_ASSIGN(FileUpdateObserverMultiplexer); |
| 82 }; |
| 83 |
| 84 static base::LazyInstance<FileUpdateObserverMultiplexer>::Leaky g_multiplexer = |
| 85 LAZY_INSTANCE_INITIALIZER; |
| 86 |
| 87 } // namespace |
| 88 |
| 89 TestFileapiOperationWaiter::TestFileapiOperationWaiter( |
| 90 FileSystemContext* context) { |
| 91 g_multiplexer.Get().AddObserver(context, this); |
| 92 } |
| 93 |
| 94 TestFileapiOperationWaiter::~TestFileapiOperationWaiter() { |
| 95 g_multiplexer.Get().RemoveObserver(this); |
| 96 } |
| 97 |
| 98 void TestFileapiOperationWaiter::WaitForEndUpdate() { |
| 99 run_loop_.Run(); |
| 100 } |
| 101 |
| 102 void TestFileapiOperationWaiter::OnStartUpdate(const FileSystemURL& url) { |
| 103 did_start_update_ = true; |
| 104 } |
| 105 |
| 106 void TestFileapiOperationWaiter::OnUpdate(const FileSystemURL& url, |
| 107 int64_t delta) {} |
| 108 |
| 109 void TestFileapiOperationWaiter::OnEndUpdate(const FileSystemURL& url) { |
| 110 run_loop_.Quit(); |
| 111 } |
| 112 |
| 113 } // namespace content |
OLD | NEW |