OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2017 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/service_worker_test_helpers.h" | |
6 | |
7 #include "base/bind.h" | |
8 #include "base/run_loop.h" | |
9 #include "content/browser/service_worker/service_worker_context_core_observer.h" | |
10 #include "content/browser/service_worker/service_worker_context_wrapper.h" | |
11 #include "content/public/browser/browser_thread.h" | |
12 #include "url/gurl.h" | |
13 | |
14 namespace content { | |
15 | |
16 namespace { | |
17 | |
18 class StoppedObserver : public base::RefCountedThreadSafe<StoppedObserver> { | |
19 public: | |
20 explicit StoppedObserver(ServiceWorkerContextWrapper* context, | |
21 int64_t service_worker_version_id, | |
22 base::OnceClosure completion_callback_ui) | |
23 : inner_observer_(context, | |
24 service_worker_version_id, | |
25 base::BindOnce(&StoppedObserver::OnStopped, this)), | |
26 completion_callback_ui_(std::move(completion_callback_ui)) {} | |
27 | |
28 private: | |
29 friend class base::RefCountedThreadSafe<StoppedObserver>; | |
30 ~StoppedObserver() {} | |
31 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
| |
32 public: | |
33 Observer(ServiceWorkerContextWrapper* context, | |
34 int64_t service_worker_version_id, | |
35 base::OnceClosure stopped_callback) | |
36 : context_(context), | |
37 version_id_(service_worker_version_id), | |
38 stopped_callback_(std::move(stopped_callback)) { | |
39 context_->AddObserver(this); | |
40 } | |
41 | |
42 // ServiceWorkerContextCoreObserver: | |
43 void OnRunningStateChanged(int64_t version_id, | |
44 EmbeddedWorkerStatus status) override { | |
45 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | |
46 if (version_id != version_id_ || status != EmbeddedWorkerStatus::STOPPED) | |
47 return; | |
48 std::move(stopped_callback_).Run(); | |
49 } | |
50 ~Observer() override { context_->RemoveObserver(this); } | |
51 | |
52 private: | |
53 ServiceWorkerContextWrapper* const context_; | |
54 int64_t version_id_; | |
55 base::OnceClosure stopped_callback_; | |
56 }; | |
57 | |
58 void OnStopped() { | |
59 if (!BrowserThread::CurrentlyOn(BrowserThread::UI)) { | |
60 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, | |
61 base::Bind(&StoppedObserver::OnStopped, this)); | |
62 return; | |
63 } | |
64 std::move(completion_callback_ui_).Run(); | |
65 } | |
66 | |
67 Observer inner_observer_; | |
68 base::OnceClosure completion_callback_ui_; | |
69 | |
70 DISALLOW_COPY_AND_ASSIGN(StoppedObserver); | |
71 }; | |
72 | |
73 void FoundReadyRegistration( | |
74 ServiceWorkerContextWrapper* context_wrapper, | |
75 base::OnceClosure completion_callback, | |
76 ServiceWorkerStatusCode service_worker_status, | |
77 scoped_refptr<ServiceWorkerRegistration> service_worker_registration) { | |
78 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.
| |
79 int64_t version_id = | |
80 service_worker_registration->active_version()->version_id(); | |
81 scoped_refptr<StoppedObserver> observer(new StoppedObserver( | |
82 context_wrapper, version_id, std::move(completion_callback))); | |
83 service_worker_registration->active_version()->embedded_worker()->Stop(); | |
84 } | |
85 | |
86 } // namespace | |
87 | |
88 void StopServiceWorkerAtOrigin(ServiceWorkerContext* context, | |
89 const GURL& origin, | |
90 base::OnceClosure completion_callback_ui) { | |
91 if (!BrowserThread::CurrentlyOn(BrowserThread::IO)) { | |
92 BrowserThread::PostTask( | |
93 BrowserThread::IO, FROM_HERE, | |
94 base::Bind(&StopServiceWorkerAtOrigin, context, origin, | |
95 base::Passed(&completion_callback_ui))); | |
96 return; | |
97 } | |
98 auto* context_wrapper = static_cast<ServiceWorkerContextWrapper*>(context); | |
99 context_wrapper->FindReadyRegistrationForPattern( | |
100 origin, base::Bind(&FoundReadyRegistration, context_wrapper, | |
101 base::Passed(&completion_callback_ui))); | |
102 } | |
103 | |
104 } // namespace content | |
OLD | NEW |