Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(170)

Side by Side Diff: content/child/service_worker/service_worker_dispatcher_unittest.cc

Issue 1454963003: ServiceWorker: Ensure that ServiceWorkerDispatcher always adopts passed handle references (1) (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 5 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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/service_worker_provider_context.h" 6 #include "content/child/service_worker/service_worker_provider_context.h"
7 #include "content/child/service_worker/web_service_worker_impl.h" 7 #include "content/child/service_worker/web_service_worker_impl.h"
8 #include "content/child/service_worker/web_service_worker_registration_impl.h" 8 #include "content/child/service_worker/web_service_worker_registration_impl.h"
9 #include "content/child/thread_safe_sender.h" 9 #include "content/child/thread_safe_sender.h"
10 #include "content/common/service_worker/service_worker_messages.h" 10 #include "content/common/service_worker/service_worker_messages.h"
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after
58 } 58 }
59 59
60 bool ContainsServiceWorker(int handle_id) { 60 bool ContainsServiceWorker(int handle_id) {
61 return ContainsKey(dispatcher_->service_workers_, handle_id); 61 return ContainsKey(dispatcher_->service_workers_, handle_id);
62 } 62 }
63 63
64 bool ContainsRegistration(int registration_handle_id) { 64 bool ContainsRegistration(int registration_handle_id) {
65 return ContainsKey(dispatcher_->registrations_, registration_handle_id); 65 return ContainsKey(dispatcher_->registrations_, registration_handle_id);
66 } 66 }
67 67
68 void OnAssociateRegistrationWithServiceWorker(
69 int thread_id,
70 int provider_id,
71 const ServiceWorkerRegistrationObjectInfo& info,
72 const ServiceWorkerVersionAttributes& attrs) {
73 dispatcher_->OnAssociateRegistrationWithServiceWorker(
74 thread_id, provider_id, info, attrs);
75 }
76
68 void OnAssociateRegistration(int thread_id, 77 void OnAssociateRegistration(int thread_id,
69 int provider_id, 78 int provider_id,
70 const ServiceWorkerRegistrationObjectInfo& info, 79 const ServiceWorkerRegistrationObjectInfo& info,
71 const ServiceWorkerVersionAttributes& attrs) { 80 const ServiceWorkerVersionAttributes& attrs) {
72 dispatcher_->OnAssociateRegistration(thread_id, provider_id, info, attrs); 81 dispatcher_->OnAssociateRegistration(thread_id, provider_id, info, attrs);
73 } 82 }
74 83
84 void OnDisassociateRegistration(int thread_id, int provider_id) {
85 dispatcher_->OnDisassociateRegistration(thread_id, provider_id);
86 }
87
75 void OnSetControllerServiceWorker(int thread_id, 88 void OnSetControllerServiceWorker(int thread_id,
76 int provider_id, 89 int provider_id,
77 const ServiceWorkerObjectInfo& info, 90 const ServiceWorkerObjectInfo& info,
78 bool should_notify_controllerchange) { 91 bool should_notify_controllerchange) {
79 dispatcher_->OnSetControllerServiceWorker(thread_id, provider_id, info, 92 dispatcher_->OnSetControllerServiceWorker(thread_id, provider_id, info,
80 should_notify_controllerchange); 93 should_notify_controllerchange);
81 } 94 }
82 95
83 ServiceWorkerDispatcher* dispatcher() { return dispatcher_.get(); } 96 ServiceWorkerDispatcher* dispatcher() { return dispatcher_.get(); }
84 ThreadSafeSender* thread_safe_sender() { return sender_.get(); } 97 ThreadSafeSender* thread_safe_sender() { return sender_.get(); }
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after
119 const blink::WebMessagePortChannelArray& channels) override { 132 const blink::WebMessagePortChannelArray& channels) override {
120 NOTREACHED(); 133 NOTREACHED();
121 } 134 }
122 135
123 private: 136 private:
124 const int provider_id_; 137 const int provider_id_;
125 ServiceWorkerDispatcher* dispatcher_; 138 ServiceWorkerDispatcher* dispatcher_;
126 }; 139 };
127 140
128 // TODO(nhiroki): Add tests for message handlers especially to receive reference 141 // TODO(nhiroki): Add tests for message handlers especially to receive reference
129 // counts like OnAssociateRegistration(). 142 // counts.
143
144 TEST_F(ServiceWorkerDispatcherTest, OnAssociateRegistrationWithServiceWorker) {
145 const int kProviderId = 10;
146
147 // Assume that these objects are passed from the browser process and own
148 // references to browser-side registration/worker representations.
149 ServiceWorkerRegistrationObjectInfo info;
150 ServiceWorkerVersionAttributes attrs;
151 CreateObjectInfoAndVersionAttributes(&info, &attrs);
152
153 // The passed references should be adopted but immediately destroyed because
154 // there is no provider context to own the references.
155 OnAssociateRegistrationWithServiceWorker(kDocumentMainThreadId, kProviderId,
156 info, attrs);
157 ASSERT_EQ(4UL, ipc_sink()->message_count());
158 EXPECT_EQ(ServiceWorkerHostMsg_DecrementServiceWorkerRefCount::ID,
159 ipc_sink()->GetMessageAt(0)->type());
160 EXPECT_EQ(ServiceWorkerHostMsg_DecrementServiceWorkerRefCount::ID,
161 ipc_sink()->GetMessageAt(1)->type());
162 EXPECT_EQ(ServiceWorkerHostMsg_DecrementServiceWorkerRefCount::ID,
163 ipc_sink()->GetMessageAt(2)->type());
164 EXPECT_EQ(ServiceWorkerHostMsg_DecrementRegistrationRefCount::ID,
165 ipc_sink()->GetMessageAt(3)->type());
166 ipc_sink()->ClearMessages();
167
168 // Set up ServiceWorkerProviderContext for ServiceWorkerGlobalScope.
169 scoped_refptr<ServiceWorkerProviderContext> provider_context(
170 new ServiceWorkerProviderContext(kProviderId,
171 SERVICE_WORKER_PROVIDER_FOR_CONTROLLER,
172 thread_safe_sender()));
173
174 // The passed references should be adoped and owned by the provider context.
falken 2015/11/20 03:35:54 nit: adopted (two places)
nhiroki 2015/11/20 05:14:59 Done.
175 OnAssociateRegistrationWithServiceWorker(kDocumentMainThreadId, kProviderId,
176 info, attrs);
177 EXPECT_EQ(0UL, ipc_sink()->message_count());
178
179 // Destruction of the provider context should release references to the
180 // associated registration and its versions.
181 provider_context = nullptr;
182 ASSERT_EQ(4UL, ipc_sink()->message_count());
183 EXPECT_EQ(ServiceWorkerHostMsg_DecrementServiceWorkerRefCount::ID,
184 ipc_sink()->GetMessageAt(0)->type());
185 EXPECT_EQ(ServiceWorkerHostMsg_DecrementServiceWorkerRefCount::ID,
186 ipc_sink()->GetMessageAt(1)->type());
187 EXPECT_EQ(ServiceWorkerHostMsg_DecrementServiceWorkerRefCount::ID,
188 ipc_sink()->GetMessageAt(2)->type());
189 EXPECT_EQ(ServiceWorkerHostMsg_DecrementRegistrationRefCount::ID,
190 ipc_sink()->GetMessageAt(3)->type());
191 }
192
193 TEST_F(ServiceWorkerDispatcherTest, OnAssociateRegistration) {
194 const int kProviderId = 10;
195
196 // Assume that these objects are passed from the browser process and own
197 // references to browser-side registration/worker representations.
198 ServiceWorkerRegistrationObjectInfo info;
199 ServiceWorkerVersionAttributes attrs;
200 CreateObjectInfoAndVersionAttributes(&info, &attrs);
201
202 // The passed references should be adopted but immediately destroyed because
203 // there is no provider context to own the references.
204 OnAssociateRegistrationWithServiceWorker(kDocumentMainThreadId, kProviderId,
205 info, attrs);
falken 2015/11/20 03:35:54 How does this test differ from the one above it? T
nhiroki 2015/11/20 05:14:59 Good catch! That should be OnAssociateRegistration
falken 2015/11/20 05:26:25 Agree with merging them.
206 ASSERT_EQ(4UL, ipc_sink()->message_count());
207 EXPECT_EQ(ServiceWorkerHostMsg_DecrementServiceWorkerRefCount::ID,
208 ipc_sink()->GetMessageAt(0)->type());
209 EXPECT_EQ(ServiceWorkerHostMsg_DecrementServiceWorkerRefCount::ID,
210 ipc_sink()->GetMessageAt(1)->type());
211 EXPECT_EQ(ServiceWorkerHostMsg_DecrementServiceWorkerRefCount::ID,
212 ipc_sink()->GetMessageAt(2)->type());
213 EXPECT_EQ(ServiceWorkerHostMsg_DecrementRegistrationRefCount::ID,
214 ipc_sink()->GetMessageAt(3)->type());
215 ipc_sink()->ClearMessages();
216
217 // Set up ServiceWorkerProviderContext for a document context.
218 scoped_refptr<ServiceWorkerProviderContext> provider_context(
219 new ServiceWorkerProviderContext(kProviderId,
220 SERVICE_WORKER_PROVIDER_FOR_WINDOW,
nhiroki 2015/11/20 05:14:59 fyi: This provider type is different from one in
221 thread_safe_sender()));
222
223 // The passed references should be adoped and only the registration reference
224 // should be owned by the provider context.
225 OnAssociateRegistrationWithServiceWorker(kDocumentMainThreadId, kProviderId,
226 info, attrs);
227 ASSERT_EQ(3UL, ipc_sink()->message_count());
228 EXPECT_EQ(ServiceWorkerHostMsg_DecrementServiceWorkerRefCount::ID,
229 ipc_sink()->GetMessageAt(0)->type());
230 EXPECT_EQ(ServiceWorkerHostMsg_DecrementServiceWorkerRefCount::ID,
231 ipc_sink()->GetMessageAt(1)->type());
232 EXPECT_EQ(ServiceWorkerHostMsg_DecrementServiceWorkerRefCount::ID,
233 ipc_sink()->GetMessageAt(2)->type());
234 ipc_sink()->ClearMessages();
235
236 // Disassociating the provider context from the registration should release
237 // the reference.
238 OnDisassociateRegistration(kDocumentMainThreadId, kProviderId);
239 ASSERT_EQ(1UL, ipc_sink()->message_count());
240 EXPECT_EQ(ServiceWorkerHostMsg_DecrementRegistrationRefCount::ID,
241 ipc_sink()->GetMessageAt(0)->type());
242 }
130 243
131 TEST_F(ServiceWorkerDispatcherTest, OnSetControllerServiceWorker) { 244 TEST_F(ServiceWorkerDispatcherTest, OnSetControllerServiceWorker) {
132 const int kProviderId = 10; 245 const int kProviderId = 10;
133 bool should_notify_controllerchange = true; 246 bool should_notify_controllerchange = true;
134 247
135 // Assume that these objects are passed from the browser process and own 248 // Assume that these objects are passed from the browser process and own
136 // references to browser-side registration/worker representations. 249 // references to browser-side registration/worker representations.
137 ServiceWorkerRegistrationObjectInfo info; 250 ServiceWorkerRegistrationObjectInfo info;
138 ServiceWorkerVersionAttributes attrs; 251 ServiceWorkerVersionAttributes attrs;
139 CreateObjectInfoAndVersionAttributes(&info, &attrs); 252 CreateObjectInfoAndVersionAttributes(&info, &attrs);
(...skipping 211 matching lines...) Expand 10 before | Expand all | Expand 10 after
351 ipc_sink()->GetMessageAt(0)->type()); 464 ipc_sink()->GetMessageAt(0)->type());
352 EXPECT_EQ(ServiceWorkerHostMsg_DecrementServiceWorkerRefCount::ID, 465 EXPECT_EQ(ServiceWorkerHostMsg_DecrementServiceWorkerRefCount::ID,
353 ipc_sink()->GetMessageAt(1)->type()); 466 ipc_sink()->GetMessageAt(1)->type());
354 EXPECT_EQ(ServiceWorkerHostMsg_DecrementServiceWorkerRefCount::ID, 467 EXPECT_EQ(ServiceWorkerHostMsg_DecrementServiceWorkerRefCount::ID,
355 ipc_sink()->GetMessageAt(2)->type()); 468 ipc_sink()->GetMessageAt(2)->type());
356 EXPECT_EQ(ServiceWorkerHostMsg_DecrementRegistrationRefCount::ID, 469 EXPECT_EQ(ServiceWorkerHostMsg_DecrementRegistrationRefCount::ID,
357 ipc_sink()->GetMessageAt(3)->type()); 470 ipc_sink()->GetMessageAt(3)->type());
358 } 471 }
359 472
360 } // namespace content 473 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698