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

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: address comments 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 adopted and owned by the provider context.
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 OnAssociateRegistration(kDocumentMainThreadId, kProviderId, info, attrs);
205 ASSERT_EQ(4UL, ipc_sink()->message_count());
206 EXPECT_EQ(ServiceWorkerHostMsg_DecrementServiceWorkerRefCount::ID,
207 ipc_sink()->GetMessageAt(0)->type());
208 EXPECT_EQ(ServiceWorkerHostMsg_DecrementServiceWorkerRefCount::ID,
209 ipc_sink()->GetMessageAt(1)->type());
210 EXPECT_EQ(ServiceWorkerHostMsg_DecrementServiceWorkerRefCount::ID,
211 ipc_sink()->GetMessageAt(2)->type());
212 EXPECT_EQ(ServiceWorkerHostMsg_DecrementRegistrationRefCount::ID,
213 ipc_sink()->GetMessageAt(3)->type());
214 ipc_sink()->ClearMessages();
215
216 // Set up ServiceWorkerProviderContext for a document context.
217 scoped_refptr<ServiceWorkerProviderContext> provider_context(
218 new ServiceWorkerProviderContext(kProviderId,
219 SERVICE_WORKER_PROVIDER_FOR_WINDOW,
220 thread_safe_sender()));
221
222 // The passed references should be adopted and only the registration reference
223 // should be owned by the provider context.
224 OnAssociateRegistration(kDocumentMainThreadId, kProviderId, info, attrs);
225 ASSERT_EQ(3UL, ipc_sink()->message_count());
226 EXPECT_EQ(ServiceWorkerHostMsg_DecrementServiceWorkerRefCount::ID,
227 ipc_sink()->GetMessageAt(0)->type());
228 EXPECT_EQ(ServiceWorkerHostMsg_DecrementServiceWorkerRefCount::ID,
229 ipc_sink()->GetMessageAt(1)->type());
230 EXPECT_EQ(ServiceWorkerHostMsg_DecrementServiceWorkerRefCount::ID,
231 ipc_sink()->GetMessageAt(2)->type());
232 ipc_sink()->ClearMessages();
233
234 // Disassociating the provider context from the registration should release
235 // the reference.
236 OnDisassociateRegistration(kDocumentMainThreadId, kProviderId);
237 ASSERT_EQ(1UL, ipc_sink()->message_count());
238 EXPECT_EQ(ServiceWorkerHostMsg_DecrementRegistrationRefCount::ID,
239 ipc_sink()->GetMessageAt(0)->type());
240 }
130 241
131 TEST_F(ServiceWorkerDispatcherTest, OnSetControllerServiceWorker) { 242 TEST_F(ServiceWorkerDispatcherTest, OnSetControllerServiceWorker) {
132 const int kProviderId = 10; 243 const int kProviderId = 10;
133 bool should_notify_controllerchange = true; 244 bool should_notify_controllerchange = true;
134 245
135 // Assume that these objects are passed from the browser process and own 246 // Assume that these objects are passed from the browser process and own
136 // references to browser-side registration/worker representations. 247 // references to browser-side registration/worker representations.
137 ServiceWorkerRegistrationObjectInfo info; 248 ServiceWorkerRegistrationObjectInfo info;
138 ServiceWorkerVersionAttributes attrs; 249 ServiceWorkerVersionAttributes attrs;
139 CreateObjectInfoAndVersionAttributes(&info, &attrs); 250 CreateObjectInfoAndVersionAttributes(&info, &attrs);
(...skipping 211 matching lines...) Expand 10 before | Expand all | Expand 10 after
351 ipc_sink()->GetMessageAt(0)->type()); 462 ipc_sink()->GetMessageAt(0)->type());
352 EXPECT_EQ(ServiceWorkerHostMsg_DecrementServiceWorkerRefCount::ID, 463 EXPECT_EQ(ServiceWorkerHostMsg_DecrementServiceWorkerRefCount::ID,
353 ipc_sink()->GetMessageAt(1)->type()); 464 ipc_sink()->GetMessageAt(1)->type());
354 EXPECT_EQ(ServiceWorkerHostMsg_DecrementServiceWorkerRefCount::ID, 465 EXPECT_EQ(ServiceWorkerHostMsg_DecrementServiceWorkerRefCount::ID,
355 ipc_sink()->GetMessageAt(2)->type()); 466 ipc_sink()->GetMessageAt(2)->type());
356 EXPECT_EQ(ServiceWorkerHostMsg_DecrementRegistrationRefCount::ID, 467 EXPECT_EQ(ServiceWorkerHostMsg_DecrementRegistrationRefCount::ID,
357 ipc_sink()->GetMessageAt(3)->type()); 468 ipc_sink()->GetMessageAt(3)->type());
358 } 469 }
359 470
360 } // namespace content 471 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698