OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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_dispatcher.h" | 5 #include "content/child/service_worker/service_worker_dispatcher.h" |
6 #include "content/child/service_worker/web_service_worker_impl.h" | 6 #include "content/child/service_worker/web_service_worker_impl.h" |
7 #include "content/child/service_worker/web_service_worker_registration_impl.h" | 7 #include "content/child/service_worker/web_service_worker_registration_impl.h" |
8 #include "content/child/thread_safe_sender.h" | 8 #include "content/child/thread_safe_sender.h" |
9 #include "content/common/service_worker/service_worker_messages.h" | 9 #include "content/common/service_worker/service_worker_messages.h" |
10 #include "content/common/service_worker/service_worker_types.h" | 10 #include "content/common/service_worker/service_worker_types.h" |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
48 info->registration_id = 20; | 48 info->registration_id = 20; |
49 | 49 |
50 attrs->active.handle_id = 100; | 50 attrs->active.handle_id = 100; |
51 attrs->active.version_id = 200; | 51 attrs->active.version_id = 200; |
52 attrs->waiting.handle_id = 101; | 52 attrs->waiting.handle_id = 101; |
53 attrs->waiting.version_id = 201; | 53 attrs->waiting.version_id = 201; |
54 attrs->installing.handle_id = 102; | 54 attrs->installing.handle_id = 102; |
55 attrs->installing.version_id = 202; | 55 attrs->installing.version_id = 202; |
56 } | 56 } |
57 | 57 |
58 WebServiceWorkerRegistrationImpl* FindOrCreateRegistration( | |
59 const ServiceWorkerRegistrationObjectInfo& info, | |
60 const ServiceWorkerVersionAttributes& attrs) { | |
61 return dispatcher_->FindOrCreateRegistration(info, attrs); | |
62 } | |
63 | |
64 bool ContainsServiceWorker(int handle_id) { | 58 bool ContainsServiceWorker(int handle_id) { |
65 return ContainsKey(dispatcher_->service_workers_, handle_id); | 59 return ContainsKey(dispatcher_->service_workers_, handle_id); |
66 } | 60 } |
67 | 61 |
68 bool ContainsRegistration(int registration_handle_id) { | 62 bool ContainsRegistration(int registration_handle_id) { |
69 return ContainsKey(dispatcher_->registrations_, registration_handle_id); | 63 return ContainsKey(dispatcher_->registrations_, registration_handle_id); |
70 } | 64 } |
71 | 65 |
72 ServiceWorkerDispatcher* dispatcher() { return dispatcher_.get(); } | 66 ServiceWorkerDispatcher* dispatcher() { return dispatcher_.get(); } |
73 IPC::TestSink* ipc_sink() { return &ipc_sink_; } | 67 IPC::TestSink* ipc_sink() { return &ipc_sink_; } |
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
132 EXPECT_FALSE(invalid_worker); | 126 EXPECT_FALSE(invalid_worker); |
133 EXPECT_EQ(0UL, ipc_sink()->message_count()); | 127 EXPECT_EQ(0UL, ipc_sink()->message_count()); |
134 | 128 |
135 adopt_handle = true; | 129 adopt_handle = true; |
136 invalid_worker = | 130 invalid_worker = |
137 dispatcher()->GetServiceWorker(ServiceWorkerObjectInfo(), adopt_handle); | 131 dispatcher()->GetServiceWorker(ServiceWorkerObjectInfo(), adopt_handle); |
138 EXPECT_FALSE(invalid_worker); | 132 EXPECT_FALSE(invalid_worker); |
139 EXPECT_EQ(0UL, ipc_sink()->message_count()); | 133 EXPECT_EQ(0UL, ipc_sink()->message_count()); |
140 } | 134 } |
141 | 135 |
142 TEST_F(ServiceWorkerDispatcherTest, CreateServiceWorkerRegistration) { | 136 TEST_F(ServiceWorkerDispatcherTest, CreateRegistration) { |
143 ServiceWorkerRegistrationObjectInfo info; | 137 ServiceWorkerRegistrationObjectInfo info; |
144 ServiceWorkerVersionAttributes attrs; | 138 ServiceWorkerVersionAttributes attrs; |
145 CreateObjectInfoAndVersionAttributes(&info, &attrs); | 139 CreateObjectInfoAndVersionAttributes(&info, &attrs); |
146 | 140 |
147 // Should return a registration object newly created with incrementing | 141 // Should return a registration object newly created with adopting refcount. |
148 // refcount. | |
149 bool adopt_handle = false; | |
150 scoped_ptr<WebServiceWorkerRegistrationImpl> registration( | 142 scoped_ptr<WebServiceWorkerRegistrationImpl> registration( |
151 dispatcher()->CreateServiceWorkerRegistration(info, adopt_handle)); | 143 dispatcher()->AdoptRegistration(info, attrs)); |
152 EXPECT_TRUE(registration); | |
153 EXPECT_TRUE(ContainsRegistration(info.handle_id)); | |
154 ASSERT_EQ(1UL, ipc_sink()->message_count()); | |
155 EXPECT_EQ(ServiceWorkerHostMsg_IncrementRegistrationRefCount::ID, | |
156 ipc_sink()->GetMessageAt(0)->type()); | |
157 | |
158 registration.reset(); | |
159 EXPECT_FALSE(ContainsRegistration(info.handle_id)); | |
160 ipc_sink()->ClearMessages(); | |
161 | |
162 // Should return another registration object newly created with adopting | |
163 // refcount. | |
164 adopt_handle = true; | |
165 scoped_ptr<WebServiceWorkerRegistrationImpl> another_registration( | |
166 dispatcher()->CreateServiceWorkerRegistration(info, adopt_handle)); | |
167 EXPECT_TRUE(another_registration); | |
168 EXPECT_TRUE(ContainsRegistration(info.handle_id)); | |
169 EXPECT_EQ(0UL, ipc_sink()->message_count()); | |
170 | |
171 another_registration.reset(); | |
172 ipc_sink()->ClearMessages(); | |
173 | |
174 // should return nullptr when a given object info is invalid. | |
175 adopt_handle = false; | |
176 scoped_ptr<WebServiceWorkerRegistrationImpl> invalid_registration( | |
177 dispatcher()->CreateServiceWorkerRegistration( | |
178 ServiceWorkerRegistrationObjectInfo(), adopt_handle)); | |
179 EXPECT_FALSE(invalid_registration); | |
180 EXPECT_FALSE(ContainsRegistration(info.handle_id)); | |
181 EXPECT_EQ(0UL, ipc_sink()->message_count()); | |
182 | |
183 adopt_handle = true; | |
184 invalid_registration.reset(dispatcher()->CreateServiceWorkerRegistration( | |
185 ServiceWorkerRegistrationObjectInfo(), adopt_handle)); | |
186 EXPECT_FALSE(invalid_registration); | |
187 EXPECT_FALSE(ContainsRegistration(info.handle_id)); | |
188 EXPECT_EQ(0UL, ipc_sink()->message_count()); | |
189 } | |
190 | |
191 TEST_F(ServiceWorkerDispatcherTest, FindOrCreateRegistration) { | |
192 ServiceWorkerRegistrationObjectInfo info; | |
193 ServiceWorkerVersionAttributes attrs; | |
194 CreateObjectInfoAndVersionAttributes(&info, &attrs); | |
195 | |
196 // Should return a registration object newly created with adopting refcounts. | |
197 scoped_ptr<WebServiceWorkerRegistrationImpl> registration( | |
198 FindOrCreateRegistration(info, attrs)); | |
199 EXPECT_TRUE(registration); | 144 EXPECT_TRUE(registration); |
200 EXPECT_EQ(info.registration_id, registration->registration_id()); | 145 EXPECT_EQ(info.registration_id, registration->registration_id()); |
201 EXPECT_EQ(0UL, ipc_sink()->message_count()); | 146 EXPECT_EQ(0UL, ipc_sink()->message_count()); |
202 | 147 |
203 // Should return the existing registration object with adopting refcounts. | 148 // The registration dtor decrements the refcount. |
204 WebServiceWorkerRegistrationImpl* existing_registration = | 149 registration.reset(); |
205 FindOrCreateRegistration(info, attrs); | |
206 EXPECT_EQ(registration, existing_registration); | |
207 ASSERT_EQ(4UL, ipc_sink()->message_count()); | 150 ASSERT_EQ(4UL, ipc_sink()->message_count()); |
208 EXPECT_EQ(ServiceWorkerHostMsg_DecrementRegistrationRefCount::ID, | 151 EXPECT_EQ(ServiceWorkerHostMsg_DecrementServiceWorkerRefCount::ID, |
209 ipc_sink()->GetMessageAt(0)->type()); | 152 ipc_sink()->GetMessageAt(0)->type()); |
210 EXPECT_EQ(ServiceWorkerHostMsg_DecrementServiceWorkerRefCount::ID, | 153 EXPECT_EQ(ServiceWorkerHostMsg_DecrementServiceWorkerRefCount::ID, |
211 ipc_sink()->GetMessageAt(1)->type()); | 154 ipc_sink()->GetMessageAt(1)->type()); |
212 EXPECT_EQ(ServiceWorkerHostMsg_DecrementServiceWorkerRefCount::ID, | 155 EXPECT_EQ(ServiceWorkerHostMsg_DecrementServiceWorkerRefCount::ID, |
213 ipc_sink()->GetMessageAt(2)->type()); | 156 ipc_sink()->GetMessageAt(2)->type()); |
| 157 EXPECT_EQ(ServiceWorkerHostMsg_DecrementRegistrationRefCount::ID, |
| 158 ipc_sink()->GetMessageAt(3)->type()); |
| 159 |
| 160 ipc_sink()->ClearMessages(); |
| 161 |
| 162 // Should return a registration object newly created with incrementing |
| 163 // refcount. |
| 164 registration = dispatcher()->CreateRegistration(info, attrs); |
| 165 EXPECT_TRUE(registration); |
| 166 EXPECT_TRUE(ContainsRegistration(info.handle_id)); |
| 167 ASSERT_EQ(4UL, ipc_sink()->message_count()); |
| 168 EXPECT_EQ(ServiceWorkerHostMsg_IncrementRegistrationRefCount::ID, |
| 169 ipc_sink()->GetMessageAt(0)->type()); |
| 170 EXPECT_EQ(ServiceWorkerHostMsg_IncrementServiceWorkerRefCount::ID, |
| 171 ipc_sink()->GetMessageAt(1)->type()); |
| 172 EXPECT_EQ(ServiceWorkerHostMsg_IncrementServiceWorkerRefCount::ID, |
| 173 ipc_sink()->GetMessageAt(2)->type()); |
| 174 EXPECT_EQ(ServiceWorkerHostMsg_IncrementServiceWorkerRefCount::ID, |
| 175 ipc_sink()->GetMessageAt(3)->type()); |
| 176 |
| 177 ipc_sink()->ClearMessages(); |
| 178 |
| 179 // The registration dtor decrements the refcount. |
| 180 registration.reset(); |
| 181 ASSERT_EQ(4UL, ipc_sink()->message_count()); |
214 EXPECT_EQ(ServiceWorkerHostMsg_DecrementServiceWorkerRefCount::ID, | 182 EXPECT_EQ(ServiceWorkerHostMsg_DecrementServiceWorkerRefCount::ID, |
| 183 ipc_sink()->GetMessageAt(0)->type()); |
| 184 EXPECT_EQ(ServiceWorkerHostMsg_DecrementServiceWorkerRefCount::ID, |
| 185 ipc_sink()->GetMessageAt(1)->type()); |
| 186 EXPECT_EQ(ServiceWorkerHostMsg_DecrementServiceWorkerRefCount::ID, |
| 187 ipc_sink()->GetMessageAt(2)->type()); |
| 188 EXPECT_EQ(ServiceWorkerHostMsg_DecrementRegistrationRefCount::ID, |
215 ipc_sink()->GetMessageAt(3)->type()); | 189 ipc_sink()->GetMessageAt(3)->type()); |
216 } | 190 } |
217 | 191 |
218 } // namespace content | 192 } // namespace content |
OLD | NEW |