OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "content/child/service_worker/service_worker_provider_context.h" | 5 #include "content/child/service_worker/service_worker_provider_context.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/message_loop/message_loop_proxy.h" | 8 #include "base/message_loop/message_loop_proxy.h" |
9 #include "base/stl_util.h" | 9 #include "base/stl_util.h" |
10 #include "content/child/child_thread.h" | 10 #include "content/child/child_thread.h" |
11 #include "content/child/service_worker/service_worker_dispatcher.h" | 11 #include "content/child/service_worker/service_worker_dispatcher.h" |
12 #include "content/child/service_worker/service_worker_handle_reference.h" | 12 #include "content/child/service_worker/service_worker_handle_reference.h" |
| 13 #include "content/child/service_worker/service_worker_registration_handle_refere
nce.h" |
13 #include "content/child/thread_safe_sender.h" | 14 #include "content/child/thread_safe_sender.h" |
14 #include "content/child/worker_task_runner.h" | 15 #include "content/child/worker_task_runner.h" |
15 #include "content/common/service_worker/service_worker_messages.h" | 16 #include "content/common/service_worker/service_worker_messages.h" |
16 | 17 |
17 namespace content { | 18 namespace content { |
18 | 19 |
19 ServiceWorkerProviderContext::ServiceWorkerProviderContext(int provider_id) | 20 ServiceWorkerProviderContext::ServiceWorkerProviderContext(int provider_id) |
20 : provider_id_(provider_id), | 21 : provider_id_(provider_id), |
21 main_thread_loop_proxy_(base::MessageLoopProxy::current()) { | 22 main_thread_loop_proxy_(base::MessageLoopProxy::current()) { |
22 if (!ChildThread::current()) | 23 if (!ChildThread::current()) |
(...skipping 26 matching lines...) Expand all Loading... |
49 ServiceWorkerHandleReference* ServiceWorkerProviderContext::active() { | 50 ServiceWorkerHandleReference* ServiceWorkerProviderContext::active() { |
50 DCHECK(main_thread_loop_proxy_->RunsTasksOnCurrentThread()); | 51 DCHECK(main_thread_loop_proxy_->RunsTasksOnCurrentThread()); |
51 return active_.get(); | 52 return active_.get(); |
52 } | 53 } |
53 | 54 |
54 ServiceWorkerHandleReference* ServiceWorkerProviderContext::controller() { | 55 ServiceWorkerHandleReference* ServiceWorkerProviderContext::controller() { |
55 DCHECK(main_thread_loop_proxy_->RunsTasksOnCurrentThread()); | 56 DCHECK(main_thread_loop_proxy_->RunsTasksOnCurrentThread()); |
56 return controller_.get(); | 57 return controller_.get(); |
57 } | 58 } |
58 | 59 |
| 60 ServiceWorkerRegistrationHandleReference* |
| 61 ServiceWorkerProviderContext::registration() { |
| 62 DCHECK(main_thread_loop_proxy_->RunsTasksOnCurrentThread()); |
| 63 return registration_.get(); |
| 64 } |
| 65 |
| 66 ServiceWorkerVersionAttributes |
| 67 ServiceWorkerProviderContext::GetVersionAttributes() { |
| 68 ServiceWorkerVersionAttributes attrs; |
| 69 if (installing()) |
| 70 attrs.installing = installing()->info(); |
| 71 if (waiting()) |
| 72 attrs.waiting = waiting()->info(); |
| 73 if (active()) |
| 74 attrs.active = active()->info(); |
| 75 return attrs; |
| 76 } |
| 77 |
| 78 void ServiceWorkerProviderContext::OnAssociateRegistration( |
| 79 const ServiceWorkerRegistrationObjectInfo& info, |
| 80 const ServiceWorkerVersionAttributes& attrs) { |
| 81 DCHECK(!registration_); |
| 82 DCHECK_NE(kInvalidServiceWorkerRegistrationHandleId, info.handle_id); |
| 83 registration_ = ServiceWorkerRegistrationHandleReference::Adopt( |
| 84 info, thread_safe_sender_.get()); |
| 85 installing_ = ServiceWorkerHandleReference::Adopt( |
| 86 attrs.installing, thread_safe_sender_.get()); |
| 87 waiting_ = ServiceWorkerHandleReference::Adopt( |
| 88 attrs.waiting, thread_safe_sender_.get()); |
| 89 active_ = ServiceWorkerHandleReference::Adopt( |
| 90 attrs.active, thread_safe_sender_.get()); |
| 91 } |
| 92 |
| 93 void ServiceWorkerProviderContext::OnDisassociateRegistration() { |
| 94 controller_.reset(); |
| 95 active_.reset(); |
| 96 waiting_.reset(); |
| 97 installing_.reset(); |
| 98 registration_.reset(); |
| 99 } |
| 100 |
59 void ServiceWorkerProviderContext::OnServiceWorkerStateChanged( | 101 void ServiceWorkerProviderContext::OnServiceWorkerStateChanged( |
60 int handle_id, | 102 int handle_id, |
61 blink::WebServiceWorkerState state) { | 103 blink::WebServiceWorkerState state) { |
62 ServiceWorkerHandleReference* which = NULL; | 104 ServiceWorkerHandleReference* which = NULL; |
63 if (handle_id == controller_handle_id()) | 105 if (handle_id == controller_handle_id()) |
64 which = controller_.get(); | 106 which = controller_.get(); |
65 else if (handle_id == active_handle_id()) | 107 else if (handle_id == active_handle_id()) |
66 which = active_.get(); | 108 which = active_.get(); |
67 else if (handle_id == waiting_handle_id()) | 109 else if (handle_id == waiting_handle_id()) |
68 which = waiting_.get(); | 110 which = waiting_.get(); |
69 else if (handle_id == installing_handle_id()) | 111 else if (handle_id == installing_handle_id()) |
70 which = installing_.get(); | 112 which = installing_.get(); |
71 | 113 |
72 // We should only get messages for ServiceWorkers associated with | 114 // We should only get messages for ServiceWorkers associated with |
73 // this provider. | 115 // this provider. |
74 DCHECK(which); | 116 DCHECK(which); |
75 | 117 |
76 which->set_state(state); | 118 which->set_state(state); |
77 | 119 |
78 // TODO(kinuko): We can forward the message to other threads here | 120 // TODO(kinuko): We can forward the message to other threads here |
79 // when we support navigator.serviceWorker in dedicated workers. | 121 // when we support navigator.serviceWorker in dedicated workers. |
80 } | 122 } |
81 | 123 |
82 void ServiceWorkerProviderContext::OnSetInstallingServiceWorker( | 124 void ServiceWorkerProviderContext::OnSetInstallingServiceWorker( |
83 int provider_id, | 125 int registration_handle_id, |
84 const ServiceWorkerObjectInfo& info) { | 126 const ServiceWorkerObjectInfo& info) { |
85 DCHECK_EQ(provider_id_, provider_id); | 127 DCHECK(IsAssociatedWithRegistration(registration_handle_id)); |
86 installing_ = | 128 installing_ = |
87 ServiceWorkerHandleReference::Adopt(info, thread_safe_sender_.get()); | 129 ServiceWorkerHandleReference::Adopt(info, thread_safe_sender_.get()); |
88 } | 130 } |
89 | 131 |
90 void ServiceWorkerProviderContext::OnSetWaitingServiceWorker( | 132 void ServiceWorkerProviderContext::OnSetWaitingServiceWorker( |
91 int provider_id, | 133 int registration_handle_id, |
92 const ServiceWorkerObjectInfo& info) { | 134 const ServiceWorkerObjectInfo& info) { |
93 DCHECK_EQ(provider_id_, provider_id); | 135 DCHECK(IsAssociatedWithRegistration(registration_handle_id)); |
94 waiting_ = | 136 waiting_ = |
95 ServiceWorkerHandleReference::Adopt(info, thread_safe_sender_.get()); | 137 ServiceWorkerHandleReference::Adopt(info, thread_safe_sender_.get()); |
96 } | 138 } |
97 | 139 |
98 void ServiceWorkerProviderContext::OnSetActiveServiceWorker( | 140 void ServiceWorkerProviderContext::OnSetActiveServiceWorker( |
99 int provider_id, | 141 int registration_handle_id, |
100 const ServiceWorkerObjectInfo& info) { | 142 const ServiceWorkerObjectInfo& info) { |
101 DCHECK_EQ(provider_id_, provider_id); | 143 DCHECK(IsAssociatedWithRegistration(registration_handle_id)); |
102 active_ = | 144 active_ = |
103 ServiceWorkerHandleReference::Adopt(info, thread_safe_sender_.get()); | 145 ServiceWorkerHandleReference::Adopt(info, thread_safe_sender_.get()); |
104 } | 146 } |
105 | 147 |
106 void ServiceWorkerProviderContext::OnSetControllerServiceWorker( | 148 void ServiceWorkerProviderContext::OnSetControllerServiceWorker( |
107 int provider_id, | 149 int registration_handle_id, |
108 const ServiceWorkerObjectInfo& info) { | 150 const ServiceWorkerObjectInfo& info) { |
109 DCHECK_EQ(provider_id_, provider_id); | 151 DCHECK(IsAssociatedWithRegistration(registration_handle_id)); |
110 | 152 |
111 // This context is is the primary owner of this handle, keeps the | 153 // This context is is the primary owner of this handle, keeps the |
112 // initial reference until it goes away. | 154 // initial reference until it goes away. |
113 controller_ = | 155 controller_ = |
114 ServiceWorkerHandleReference::Adopt(info, thread_safe_sender_.get()); | 156 ServiceWorkerHandleReference::Adopt(info, thread_safe_sender_.get()); |
115 | 157 |
116 // TODO(kinuko): We can forward the message to other threads here | 158 // TODO(kinuko): We can forward the message to other threads here |
117 // when we support navigator.serviceWorker in dedicated workers. | 159 // when we support navigator.serviceWorker in dedicated workers. |
118 } | 160 } |
119 | 161 |
(...skipping 14 matching lines...) Expand all Loading... |
134 return active_ ? active_->info().handle_id | 176 return active_ ? active_->info().handle_id |
135 : kInvalidServiceWorkerHandleId; | 177 : kInvalidServiceWorkerHandleId; |
136 } | 178 } |
137 | 179 |
138 int ServiceWorkerProviderContext::controller_handle_id() const { | 180 int ServiceWorkerProviderContext::controller_handle_id() const { |
139 DCHECK(main_thread_loop_proxy_->RunsTasksOnCurrentThread()); | 181 DCHECK(main_thread_loop_proxy_->RunsTasksOnCurrentThread()); |
140 return controller_ ? controller_->info().handle_id | 182 return controller_ ? controller_->info().handle_id |
141 : kInvalidServiceWorkerHandleId; | 183 : kInvalidServiceWorkerHandleId; |
142 } | 184 } |
143 | 185 |
| 186 int ServiceWorkerProviderContext::registration_handle_id() const { |
| 187 DCHECK(main_thread_loop_proxy_->RunsTasksOnCurrentThread()); |
| 188 return registration_ ? registration_->info().handle_id |
| 189 : kInvalidServiceWorkerRegistrationHandleId; |
| 190 } |
| 191 |
| 192 bool ServiceWorkerProviderContext::IsAssociatedWithRegistration( |
| 193 int registration_handle_id) const { |
| 194 if (!registration_) |
| 195 return false; |
| 196 if (registration_handle_id == kInvalidServiceWorkerRegistrationHandleId) |
| 197 return false; |
| 198 return registration_->info().handle_id == registration_handle_id; |
| 199 } |
| 200 |
144 } // namespace content | 201 } // namespace content |
OLD | NEW |