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 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 // Adds a ref to StoppedObserver to keep |this| around | |
26 // until the worker is stopped. | |
27 base::BindOnce(&StoppedObserver::OnStopped, this)), | |
28 completion_callback_ui_(std::move(completion_callback_ui)) {} | |
29 | |
30 private: | |
31 friend class base::RefCountedThreadSafe<StoppedObserver>; | |
32 ~StoppedObserver() {} | |
33 class Observer : public ServiceWorkerContextCoreObserver { | |
34 public: | |
35 Observer(ServiceWorkerContextWrapper* context, | |
36 int64_t service_worker_version_id, | |
37 base::OnceClosure stopped_callback) | |
38 : context_(context), | |
39 version_id_(service_worker_version_id), | |
40 stopped_callback_(std::move(stopped_callback)) { | |
41 context_->AddObserver(this); | |
42 } | |
43 | |
44 // ServiceWorkerContextCoreObserver: | |
45 void OnRunningStateChanged(int64_t version_id, | |
46 EmbeddedWorkerStatus status) override { | |
47 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | |
48 if (version_id != version_id_ || status != EmbeddedWorkerStatus::STOPPED) | |
49 return; | |
50 std::move(stopped_callback_).Run(); | |
51 } | |
52 ~Observer() override { context_->RemoveObserver(this); } | |
53 | |
54 private: | |
55 ServiceWorkerContextWrapper* const context_; | |
56 int64_t version_id_; | |
57 base::OnceClosure stopped_callback_; | |
58 }; | |
59 | |
60 void OnStopped() { | |
61 if (!BrowserThread::CurrentlyOn(BrowserThread::UI)) { | |
62 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, | |
63 base::Bind(&StoppedObserver::OnStopped, this)); | |
64 return; | |
65 } | |
66 std::move(completion_callback_ui_).Run(); | |
67 } | |
68 | |
69 Observer inner_observer_; | |
70 base::OnceClosure completion_callback_ui_; | |
71 | |
72 DISALLOW_COPY_AND_ASSIGN(StoppedObserver); | |
73 }; | |
74 | |
75 void FoundReadyRegistration( | |
76 ServiceWorkerContextWrapper* context_wrapper, | |
77 base::OnceClosure completion_callback, | |
78 ServiceWorkerStatusCode service_worker_status, | |
79 scoped_refptr<ServiceWorkerRegistration> service_worker_registration) { | |
80 DCHECK_EQ(SERVICE_WORKER_OK, service_worker_status); | |
81 int64_t version_id = | |
82 service_worker_registration->active_version()->version_id(); | |
83 scoped_refptr<StoppedObserver> observer(new StoppedObserver( | |
84 context_wrapper, version_id, std::move(completion_callback))); | |
85 service_worker_registration->active_version()->embedded_worker()->Stop(); | |
86 } | |
87 | |
88 } // namespace | |
89 | |
90 void StopServiceWorkerAtOrigin(ServiceWorkerContext* context, | |
91 const GURL& origin, | |
92 base::OnceClosure completion_callback_ui) { | |
93 if (!BrowserThread::CurrentlyOn(BrowserThread::IO)) { | |
94 BrowserThread::PostTask( | |
95 BrowserThread::IO, FROM_HERE, | |
96 base::Bind(&StopServiceWorkerAtOrigin, context, origin, | |
97 base::Passed(&completion_callback_ui))); | |
98 return; | |
99 } | |
100 auto* context_wrapper = static_cast<ServiceWorkerContextWrapper*>(context); | |
101 context_wrapper->FindReadyRegistrationForPattern( | |
102 origin, base::Bind(&FoundReadyRegistration, context_wrapper, | |
falken
2017/06/22 03:35:57
Similarly here, since this is using FindReadyRegis
lazyboy
2017/06/23 02:18:30
Done.
| |
103 base::Passed(&completion_callback_ui))); | |
104 } | |
105 | |
106 } // namespace content | |
OLD | NEW |