Chromium Code Reviews| Index: content/public/test/service_worker_test_helpers.cc |
| diff --git a/content/public/test/service_worker_test_helpers.cc b/content/public/test/service_worker_test_helpers.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..ab08c59616f6286f88de3af1b29d0a40f1917335 |
| --- /dev/null |
| +++ b/content/public/test/service_worker_test_helpers.cc |
| @@ -0,0 +1,104 @@ |
| +// Copyright 2017 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "content/public/test/service_worker_test_helpers.h" |
| + |
| +#include "base/bind.h" |
| +#include "base/run_loop.h" |
| +#include "content/browser/service_worker/service_worker_context_core_observer.h" |
| +#include "content/browser/service_worker/service_worker_context_wrapper.h" |
| +#include "content/public/browser/browser_thread.h" |
| +#include "url/gurl.h" |
| + |
| +namespace content { |
| + |
| +namespace { |
| + |
| +class StoppedObserver : public base::RefCountedThreadSafe<StoppedObserver> { |
| + public: |
| + explicit StoppedObserver(ServiceWorkerContextWrapper* context, |
| + int64_t service_worker_version_id, |
| + base::OnceClosure completion_callback_ui) |
| + : inner_observer_(context, |
| + service_worker_version_id, |
| + base::BindOnce(&StoppedObserver::OnStopped, this)), |
| + completion_callback_ui_(std::move(completion_callback_ui)) {} |
| + |
| + private: |
| + friend class base::RefCountedThreadSafe<StoppedObserver>; |
| + ~StoppedObserver() {} |
| + class Observer : public ServiceWorkerContextCoreObserver { |
|
Devlin
2017/06/16 02:34:42
Why have the inner observer rather than just have
lazyboy
2017/06/16 18:48:04
Only reason was to hide OnRunningStateChanged, but
|
| + public: |
| + Observer(ServiceWorkerContextWrapper* context, |
| + int64_t service_worker_version_id, |
| + base::OnceClosure stopped_callback) |
| + : context_(context), |
| + version_id_(service_worker_version_id), |
| + stopped_callback_(std::move(stopped_callback)) { |
| + context_->AddObserver(this); |
| + } |
| + |
| + // ServiceWorkerContextCoreObserver: |
| + void OnRunningStateChanged(int64_t version_id, |
| + EmbeddedWorkerStatus status) override { |
| + DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| + if (version_id != version_id_ || status != EmbeddedWorkerStatus::STOPPED) |
| + return; |
| + std::move(stopped_callback_).Run(); |
| + } |
| + ~Observer() override { context_->RemoveObserver(this); } |
| + |
| + private: |
| + ServiceWorkerContextWrapper* const context_; |
| + int64_t version_id_; |
| + base::OnceClosure stopped_callback_; |
| + }; |
| + |
| + void OnStopped() { |
| + if (!BrowserThread::CurrentlyOn(BrowserThread::UI)) { |
| + BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, |
| + base::Bind(&StoppedObserver::OnStopped, this)); |
| + return; |
| + } |
| + std::move(completion_callback_ui_).Run(); |
| + } |
| + |
| + Observer inner_observer_; |
| + base::OnceClosure completion_callback_ui_; |
| + |
| + DISALLOW_COPY_AND_ASSIGN(StoppedObserver); |
| +}; |
| + |
| +void FoundReadyRegistration( |
| + ServiceWorkerContextWrapper* context_wrapper, |
| + base::OnceClosure completion_callback, |
| + ServiceWorkerStatusCode service_worker_status, |
| + scoped_refptr<ServiceWorkerRegistration> service_worker_registration) { |
| + DCHECK(service_worker_status == SERVICE_WORKER_OK); |
|
Devlin
2017/06/16 02:34:42
nit DCHECK_EQ?
lazyboy
2017/06/16 18:48:05
Done.
|
| + int64_t version_id = |
| + service_worker_registration->active_version()->version_id(); |
| + scoped_refptr<StoppedObserver> observer(new StoppedObserver( |
| + context_wrapper, version_id, std::move(completion_callback))); |
| + service_worker_registration->active_version()->embedded_worker()->Stop(); |
| +} |
| + |
| +} // namespace |
| + |
| +void StopServiceWorkerAtOrigin(ServiceWorkerContext* context, |
| + const GURL& origin, |
| + base::OnceClosure completion_callback_ui) { |
| + if (!BrowserThread::CurrentlyOn(BrowserThread::IO)) { |
| + BrowserThread::PostTask( |
| + BrowserThread::IO, FROM_HERE, |
| + base::Bind(&StopServiceWorkerAtOrigin, context, origin, |
| + base::Passed(&completion_callback_ui))); |
| + return; |
| + } |
| + auto* context_wrapper = static_cast<ServiceWorkerContextWrapper*>(context); |
| + context_wrapper->FindReadyRegistrationForPattern( |
| + origin, base::Bind(&FoundReadyRegistration, context_wrapper, |
| + base::Passed(&completion_callback_ui))); |
| +} |
| + |
| +} // namespace content |