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" |
(...skipping 13 matching lines...) Expand all Loading... |
24 return; // May be null in some tests. | 24 return; // May be null in some tests. |
25 thread_safe_sender_ = ChildThread::current()->thread_safe_sender(); | 25 thread_safe_sender_ = ChildThread::current()->thread_safe_sender(); |
26 ServiceWorkerDispatcher* dispatcher = | 26 ServiceWorkerDispatcher* dispatcher = |
27 ServiceWorkerDispatcher::GetOrCreateThreadSpecificInstance( | 27 ServiceWorkerDispatcher::GetOrCreateThreadSpecificInstance( |
28 thread_safe_sender_.get()); | 28 thread_safe_sender_.get()); |
29 DCHECK(dispatcher); | 29 DCHECK(dispatcher); |
30 dispatcher->AddProviderContext(this); | 30 dispatcher->AddProviderContext(this); |
31 } | 31 } |
32 | 32 |
33 ServiceWorkerProviderContext::~ServiceWorkerProviderContext() { | 33 ServiceWorkerProviderContext::~ServiceWorkerProviderContext() { |
| 34 DCHECK(main_thread_loop_proxy_->RunsTasksOnCurrentThread()); |
34 if (ServiceWorkerDispatcher* dispatcher = | 35 if (ServiceWorkerDispatcher* dispatcher = |
35 ServiceWorkerDispatcher::GetThreadSpecificInstance()) { | 36 ServiceWorkerDispatcher::GetThreadSpecificInstance()) { |
| 37 // Remove this context from the dispatcher living on the main thread. |
36 dispatcher->RemoveProviderContext(this); | 38 dispatcher->RemoveProviderContext(this); |
37 } | 39 } |
38 } | 40 } |
39 | 41 |
40 ServiceWorkerHandleReference* ServiceWorkerProviderContext::controller() { | 42 ServiceWorkerHandleReference* ServiceWorkerProviderContext::controller() { |
41 DCHECK(main_thread_loop_proxy_->RunsTasksOnCurrentThread()); | 43 DCHECK(main_thread_loop_proxy_->RunsTasksOnCurrentThread()); |
42 return controller_.get(); | 44 return controller_.get(); |
43 } | 45 } |
44 | 46 |
45 ServiceWorkerRegistrationHandleReference* | 47 ServiceWorkerRegistrationHandleReference* |
46 ServiceWorkerProviderContext::registration() { | 48 ServiceWorkerProviderContext::registration() { |
47 DCHECK(main_thread_loop_proxy_->RunsTasksOnCurrentThread()); | 49 base::AutoLock lock(lock_); |
48 return registration_.get(); | 50 return registration_.get(); |
49 } | 51 } |
50 | 52 |
51 ServiceWorkerVersionAttributes | 53 ServiceWorkerVersionAttributes |
52 ServiceWorkerProviderContext::GetVersionAttributes() { | 54 ServiceWorkerProviderContext::GetVersionAttributes() { |
| 55 base::AutoLock lock(lock_); |
| 56 DCHECK(registration_); |
| 57 |
53 ServiceWorkerVersionAttributes attrs; | 58 ServiceWorkerVersionAttributes attrs; |
54 if (installing_) | 59 if (installing_) |
55 attrs.installing = installing_->info(); | 60 attrs.installing = installing_->info(); |
56 if (waiting_) | 61 if (waiting_) |
57 attrs.waiting = waiting_->info(); | 62 attrs.waiting = waiting_->info(); |
58 if (active_) | 63 if (active_) |
59 attrs.active = active_->info(); | 64 attrs.active = active_->info(); |
60 return attrs; | 65 return attrs; |
61 } | 66 } |
62 | 67 |
63 void ServiceWorkerProviderContext::SetVersionAttributes( | 68 void ServiceWorkerProviderContext::SetVersionAttributes( |
64 ChangedVersionAttributesMask mask, | 69 ChangedVersionAttributesMask mask, |
65 const ServiceWorkerVersionAttributes& attrs) { | 70 const ServiceWorkerVersionAttributes& attrs) { |
| 71 base::AutoLock lock(lock_); |
66 DCHECK(main_thread_loop_proxy_->RunsTasksOnCurrentThread()); | 72 DCHECK(main_thread_loop_proxy_->RunsTasksOnCurrentThread()); |
67 DCHECK(registration_); | 73 DCHECK(registration_); |
68 | 74 |
69 if (mask.installing_changed()) { | 75 if (mask.installing_changed()) { |
70 installing_ = ServiceWorkerHandleReference::Adopt( | 76 installing_ = ServiceWorkerHandleReference::Adopt( |
71 attrs.installing, thread_safe_sender_.get()); | 77 attrs.installing, thread_safe_sender_.get()); |
72 } | 78 } |
73 if (mask.waiting_changed()) { | 79 if (mask.waiting_changed()) { |
74 waiting_ = ServiceWorkerHandleReference::Adopt( | 80 waiting_ = ServiceWorkerHandleReference::Adopt( |
75 attrs.waiting, thread_safe_sender_.get()); | 81 attrs.waiting, thread_safe_sender_.get()); |
76 } | 82 } |
77 if (mask.active_changed()) { | 83 if (mask.active_changed()) { |
78 active_ = ServiceWorkerHandleReference::Adopt( | 84 active_ = ServiceWorkerHandleReference::Adopt( |
79 attrs.active, thread_safe_sender_.get()); | 85 attrs.active, thread_safe_sender_.get()); |
80 } | 86 } |
81 } | 87 } |
82 | 88 |
83 void ServiceWorkerProviderContext::OnAssociateRegistration( | 89 void ServiceWorkerProviderContext::OnAssociateRegistration( |
84 const ServiceWorkerRegistrationObjectInfo& info, | 90 const ServiceWorkerRegistrationObjectInfo& info, |
85 const ServiceWorkerVersionAttributes& attrs) { | 91 const ServiceWorkerVersionAttributes& attrs) { |
| 92 base::AutoLock lock(lock_); |
86 DCHECK(main_thread_loop_proxy_->RunsTasksOnCurrentThread()); | 93 DCHECK(main_thread_loop_proxy_->RunsTasksOnCurrentThread()); |
87 DCHECK(!registration_); | 94 DCHECK(!registration_); |
88 DCHECK_NE(kInvalidServiceWorkerRegistrationId, info.registration_id); | 95 DCHECK_NE(kInvalidServiceWorkerRegistrationId, info.registration_id); |
89 DCHECK_NE(kInvalidServiceWorkerRegistrationHandleId, info.handle_id); | 96 DCHECK_NE(kInvalidServiceWorkerRegistrationHandleId, info.handle_id); |
90 | 97 |
91 registration_ = ServiceWorkerRegistrationHandleReference::Adopt( | 98 registration_ = ServiceWorkerRegistrationHandleReference::Adopt( |
92 info, thread_safe_sender_.get()); | 99 info, thread_safe_sender_.get()); |
93 installing_ = ServiceWorkerHandleReference::Adopt( | 100 installing_ = ServiceWorkerHandleReference::Adopt( |
94 attrs.installing, thread_safe_sender_.get()); | 101 attrs.installing, thread_safe_sender_.get()); |
95 waiting_ = ServiceWorkerHandleReference::Adopt( | 102 waiting_ = ServiceWorkerHandleReference::Adopt( |
96 attrs.waiting, thread_safe_sender_.get()); | 103 attrs.waiting, thread_safe_sender_.get()); |
97 active_ = ServiceWorkerHandleReference::Adopt( | 104 active_ = ServiceWorkerHandleReference::Adopt( |
98 attrs.active, thread_safe_sender_.get()); | 105 attrs.active, thread_safe_sender_.get()); |
99 } | 106 } |
100 | 107 |
101 void ServiceWorkerProviderContext::OnDisassociateRegistration() { | 108 void ServiceWorkerProviderContext::OnDisassociateRegistration() { |
| 109 base::AutoLock lock(lock_); |
102 DCHECK(main_thread_loop_proxy_->RunsTasksOnCurrentThread()); | 110 DCHECK(main_thread_loop_proxy_->RunsTasksOnCurrentThread()); |
| 111 |
103 controller_.reset(); | 112 controller_.reset(); |
104 active_.reset(); | 113 active_.reset(); |
105 waiting_.reset(); | 114 waiting_.reset(); |
106 installing_.reset(); | 115 installing_.reset(); |
107 registration_.reset(); | 116 registration_.reset(); |
108 } | 117 } |
109 | 118 |
110 void ServiceWorkerProviderContext::OnServiceWorkerStateChanged( | 119 void ServiceWorkerProviderContext::OnServiceWorkerStateChanged( |
111 int handle_id, | 120 int handle_id, |
112 blink::WebServiceWorkerState state) { | 121 blink::WebServiceWorkerState state) { |
| 122 base::AutoLock lock(lock_); |
113 DCHECK(main_thread_loop_proxy_->RunsTasksOnCurrentThread()); | 123 DCHECK(main_thread_loop_proxy_->RunsTasksOnCurrentThread()); |
114 | 124 |
115 ServiceWorkerHandleReference* which = NULL; | 125 ServiceWorkerHandleReference* which = NULL; |
116 if (handle_id == controller_handle_id()) | 126 if (handle_id == controller_handle_id()) |
117 which = controller_.get(); | 127 which = controller_.get(); |
118 else if (handle_id == active_handle_id()) | 128 else if (handle_id == active_handle_id()) |
119 which = active_.get(); | 129 which = active_.get(); |
120 else if (handle_id == waiting_handle_id()) | 130 else if (handle_id == waiting_handle_id()) |
121 which = waiting_.get(); | 131 which = waiting_.get(); |
122 else if (handle_id == installing_handle_id()) | 132 else if (handle_id == installing_handle_id()) |
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
169 return controller_ ? controller_->info().handle_id | 179 return controller_ ? controller_->info().handle_id |
170 : kInvalidServiceWorkerHandleId; | 180 : kInvalidServiceWorkerHandleId; |
171 } | 181 } |
172 | 182 |
173 int ServiceWorkerProviderContext::registration_handle_id() const { | 183 int ServiceWorkerProviderContext::registration_handle_id() const { |
174 DCHECK(main_thread_loop_proxy_->RunsTasksOnCurrentThread()); | 184 DCHECK(main_thread_loop_proxy_->RunsTasksOnCurrentThread()); |
175 return registration_ ? registration_->info().handle_id | 185 return registration_ ? registration_->info().handle_id |
176 : kInvalidServiceWorkerRegistrationHandleId; | 186 : kInvalidServiceWorkerRegistrationHandleId; |
177 } | 187 } |
178 | 188 |
| 189 void ServiceWorkerProviderContext::DestructOnMainThread() const { |
| 190 if (!main_thread_loop_proxy_->RunsTasksOnCurrentThread()) |
| 191 main_thread_loop_proxy_->DeleteSoon(FROM_HERE, this); |
| 192 } |
| 193 |
179 } // namespace content | 194 } // namespace content |
OLD | NEW |