| 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 "base/macros.h" | 5 #include "base/macros.h" |
| 6 #include "base/message_loop/message_loop.h" | 6 #include "base/message_loop/message_loop.h" |
| 7 #include "content/child/service_worker/service_worker_dispatcher.h" | 7 #include "content/child/service_worker/service_worker_dispatcher.h" |
| 8 #include "content/child/service_worker/service_worker_handle_reference.h" | 8 #include "content/child/service_worker/service_worker_handle_reference.h" |
| 9 #include "content/child/service_worker/service_worker_provider_context.h" | 9 #include "content/child/service_worker/service_worker_provider_context.h" |
| 10 #include "content/child/service_worker/web_service_worker_impl.h" | 10 #include "content/child/service_worker/web_service_worker_impl.h" |
| (...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 79 dispatcher_->OnAssociateRegistration(thread_id, provider_id, info, attrs); | 79 dispatcher_->OnAssociateRegistration(thread_id, provider_id, info, attrs); |
| 80 } | 80 } |
| 81 | 81 |
| 82 void OnDisassociateRegistration(int thread_id, int provider_id) { | 82 void OnDisassociateRegistration(int thread_id, int provider_id) { |
| 83 dispatcher_->OnDisassociateRegistration(thread_id, provider_id); | 83 dispatcher_->OnDisassociateRegistration(thread_id, provider_id); |
| 84 } | 84 } |
| 85 | 85 |
| 86 void OnSetControllerServiceWorker(int thread_id, | 86 void OnSetControllerServiceWorker(int thread_id, |
| 87 int provider_id, | 87 int provider_id, |
| 88 const ServiceWorkerObjectInfo& info, | 88 const ServiceWorkerObjectInfo& info, |
| 89 bool should_notify_controllerchange) { | 89 bool should_notify_controllerchange, |
| 90 const std::set<uint32_t>& used_features) { |
| 90 dispatcher_->OnSetControllerServiceWorker(thread_id, provider_id, info, | 91 dispatcher_->OnSetControllerServiceWorker(thread_id, provider_id, info, |
| 91 should_notify_controllerchange); | 92 should_notify_controllerchange, |
| 93 used_features); |
| 92 } | 94 } |
| 93 | 95 |
| 94 void OnPostMessage(const ServiceWorkerMsg_MessageToDocument_Params& params) { | 96 void OnPostMessage(const ServiceWorkerMsg_MessageToDocument_Params& params) { |
| 95 dispatcher_->OnPostMessage(params); | 97 dispatcher_->OnPostMessage(params); |
| 96 } | 98 } |
| 97 | 99 |
| 98 std::unique_ptr<ServiceWorkerHandleReference> Adopt( | 100 std::unique_ptr<ServiceWorkerHandleReference> Adopt( |
| 99 const ServiceWorkerObjectInfo& info) { | 101 const ServiceWorkerObjectInfo& info) { |
| 100 return dispatcher_->Adopt(info); | 102 return dispatcher_->Adopt(info); |
| 101 } | 103 } |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 135 | 137 |
| 136 void dispatchMessageEvent( | 138 void dispatchMessageEvent( |
| 137 std::unique_ptr<blink::WebServiceWorker::Handle> handle, | 139 std::unique_ptr<blink::WebServiceWorker::Handle> handle, |
| 138 const blink::WebString& message, | 140 const blink::WebString& message, |
| 139 const blink::WebMessagePortChannelArray& channels) override { | 141 const blink::WebMessagePortChannelArray& channels) override { |
| 140 // WebPassOwnPtr cannot be owned in Chromium, so drop the handle here. | 142 // WebPassOwnPtr cannot be owned in Chromium, so drop the handle here. |
| 141 // The destruction releases ServiceWorkerHandleReference. | 143 // The destruction releases ServiceWorkerHandleReference. |
| 142 is_dispatch_message_event_called_ = true; | 144 is_dispatch_message_event_called_ = true; |
| 143 } | 145 } |
| 144 | 146 |
| 147 void countFeature(uint32_t feature) override { |
| 148 used_features_.insert(feature); |
| 149 } |
| 150 |
| 145 bool is_set_controlled_called() const { return is_set_controlled_called_; } | 151 bool is_set_controlled_called() const { return is_set_controlled_called_; } |
| 146 | 152 |
| 147 bool is_dispatch_message_event_called() const { | 153 bool is_dispatch_message_event_called() const { |
| 148 return is_dispatch_message_event_called_; | 154 return is_dispatch_message_event_called_; |
| 149 } | 155 } |
| 150 | 156 |
| 151 private: | 157 private: |
| 152 const int provider_id_; | 158 const int provider_id_; |
| 153 bool is_set_controlled_called_ = false; | 159 bool is_set_controlled_called_ = false; |
| 154 bool is_dispatch_message_event_called_ = false; | 160 bool is_dispatch_message_event_called_ = false; |
| 155 ServiceWorkerDispatcher* dispatcher_; | 161 ServiceWorkerDispatcher* dispatcher_; |
| 162 std::set<uint32_t> used_features_; |
| 156 }; | 163 }; |
| 157 | 164 |
| 158 TEST_F(ServiceWorkerDispatcherTest, OnAssociateRegistration_NoProviderContext) { | 165 TEST_F(ServiceWorkerDispatcherTest, OnAssociateRegistration_NoProviderContext) { |
| 159 // Assume that these objects are passed from the browser process and own | 166 // Assume that these objects are passed from the browser process and own |
| 160 // references to browser-side registration/worker representations. | 167 // references to browser-side registration/worker representations. |
| 161 ServiceWorkerRegistrationObjectInfo info; | 168 ServiceWorkerRegistrationObjectInfo info; |
| 162 ServiceWorkerVersionAttributes attrs; | 169 ServiceWorkerVersionAttributes attrs; |
| 163 CreateObjectInfoAndVersionAttributes(&info, &attrs); | 170 CreateObjectInfoAndVersionAttributes(&info, &attrs); |
| 164 | 171 |
| 165 // The passed references should be adopted but immediately released because | 172 // The passed references should be adopted but immediately released because |
| (...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 252 // Assume that these objects are passed from the browser process and own | 259 // Assume that these objects are passed from the browser process and own |
| 253 // references to browser-side registration/worker representations. | 260 // references to browser-side registration/worker representations. |
| 254 ServiceWorkerRegistrationObjectInfo info; | 261 ServiceWorkerRegistrationObjectInfo info; |
| 255 ServiceWorkerVersionAttributes attrs; | 262 ServiceWorkerVersionAttributes attrs; |
| 256 CreateObjectInfoAndVersionAttributes(&info, &attrs); | 263 CreateObjectInfoAndVersionAttributes(&info, &attrs); |
| 257 | 264 |
| 258 // (1) In the case there are no SWProviderContext and WebSWProviderClient for | 265 // (1) In the case there are no SWProviderContext and WebSWProviderClient for |
| 259 // the provider, the passed reference to the active worker should be adopted | 266 // the provider, the passed reference to the active worker should be adopted |
| 260 // but immediately released because there is no provider context to own it. | 267 // but immediately released because there is no provider context to own it. |
| 261 OnSetControllerServiceWorker(kDocumentMainThreadId, kProviderId, attrs.active, | 268 OnSetControllerServiceWorker(kDocumentMainThreadId, kProviderId, attrs.active, |
| 262 should_notify_controllerchange); | 269 should_notify_controllerchange, |
| 270 std::set<uint32_t>()); |
| 263 ASSERT_EQ(1UL, ipc_sink()->message_count()); | 271 ASSERT_EQ(1UL, ipc_sink()->message_count()); |
| 264 EXPECT_EQ(ServiceWorkerHostMsg_DecrementServiceWorkerRefCount::ID, | 272 EXPECT_EQ(ServiceWorkerHostMsg_DecrementServiceWorkerRefCount::ID, |
| 265 ipc_sink()->GetMessageAt(0)->type()); | 273 ipc_sink()->GetMessageAt(0)->type()); |
| 266 ipc_sink()->ClearMessages(); | 274 ipc_sink()->ClearMessages(); |
| 267 | 275 |
| 268 // (2) In the case there is no WebSWProviderClient but SWProviderContext for | 276 // (2) In the case there is no WebSWProviderClient but SWProviderContext for |
| 269 // the provider, the passed referecence should be adopted and owned by the | 277 // the provider, the passed referecence should be adopted and owned by the |
| 270 // provider context. | 278 // provider context. |
| 271 scoped_refptr<ServiceWorkerProviderContext> provider_context( | 279 scoped_refptr<ServiceWorkerProviderContext> provider_context( |
| 272 new ServiceWorkerProviderContext(kProviderId, | 280 new ServiceWorkerProviderContext(kProviderId, |
| 273 SERVICE_WORKER_PROVIDER_FOR_WINDOW, | 281 SERVICE_WORKER_PROVIDER_FOR_WINDOW, |
| 274 thread_safe_sender())); | 282 thread_safe_sender())); |
| 275 OnAssociateRegistration(kDocumentMainThreadId, kProviderId, info, attrs); | 283 OnAssociateRegistration(kDocumentMainThreadId, kProviderId, info, attrs); |
| 276 ipc_sink()->ClearMessages(); | 284 ipc_sink()->ClearMessages(); |
| 277 OnSetControllerServiceWorker(kDocumentMainThreadId, kProviderId, attrs.active, | 285 OnSetControllerServiceWorker(kDocumentMainThreadId, kProviderId, attrs.active, |
| 278 should_notify_controllerchange); | 286 should_notify_controllerchange, |
| 287 std::set<uint32_t>()); |
| 279 EXPECT_EQ(0UL, ipc_sink()->message_count()); | 288 EXPECT_EQ(0UL, ipc_sink()->message_count()); |
| 280 | 289 |
| 281 // Destruction of the provider context should release references to the | 290 // Destruction of the provider context should release references to the |
| 282 // associated registration and the controller. | 291 // associated registration and the controller. |
| 283 provider_context = nullptr; | 292 provider_context = nullptr; |
| 284 ASSERT_EQ(2UL, ipc_sink()->message_count()); | 293 ASSERT_EQ(2UL, ipc_sink()->message_count()); |
| 285 EXPECT_EQ(ServiceWorkerHostMsg_DecrementServiceWorkerRefCount::ID, | 294 EXPECT_EQ(ServiceWorkerHostMsg_DecrementServiceWorkerRefCount::ID, |
| 286 ipc_sink()->GetMessageAt(0)->type()); | 295 ipc_sink()->GetMessageAt(0)->type()); |
| 287 EXPECT_EQ(ServiceWorkerHostMsg_DecrementRegistrationRefCount::ID, | 296 EXPECT_EQ(ServiceWorkerHostMsg_DecrementRegistrationRefCount::ID, |
| 288 ipc_sink()->GetMessageAt(1)->type()); | 297 ipc_sink()->GetMessageAt(1)->type()); |
| 289 ipc_sink()->ClearMessages(); | 298 ipc_sink()->ClearMessages(); |
| 290 | 299 |
| 291 // (3) In the case there is no SWProviderContext but WebSWProviderClient for | 300 // (3) In the case there is no SWProviderContext but WebSWProviderClient for |
| 292 // the provider, the new reference should be created and owned by the provider | 301 // the provider, the new reference should be created and owned by the provider |
| 293 // client (but the reference is immediately released due to limitation of the | 302 // client (but the reference is immediately released due to limitation of the |
| 294 // mock provider client. See the comment on setController() of the mock). | 303 // mock provider client. See the comment on setController() of the mock). |
| 295 // In addition, the passed reference should be adopted but immediately | 304 // In addition, the passed reference should be adopted but immediately |
| 296 // released because there is no provider context to own it. | 305 // released because there is no provider context to own it. |
| 297 std::unique_ptr<MockWebServiceWorkerProviderClientImpl> provider_client( | 306 std::unique_ptr<MockWebServiceWorkerProviderClientImpl> provider_client( |
| 298 new MockWebServiceWorkerProviderClientImpl(kProviderId, dispatcher())); | 307 new MockWebServiceWorkerProviderClientImpl(kProviderId, dispatcher())); |
| 299 ASSERT_FALSE(provider_client->is_set_controlled_called()); | 308 ASSERT_FALSE(provider_client->is_set_controlled_called()); |
| 300 OnSetControllerServiceWorker(kDocumentMainThreadId, kProviderId, attrs.active, | 309 OnSetControllerServiceWorker(kDocumentMainThreadId, kProviderId, attrs.active, |
| 301 should_notify_controllerchange); | 310 should_notify_controllerchange, |
| 311 std::set<uint32_t>()); |
| 302 EXPECT_TRUE(provider_client->is_set_controlled_called()); | 312 EXPECT_TRUE(provider_client->is_set_controlled_called()); |
| 303 ASSERT_EQ(3UL, ipc_sink()->message_count()); | 313 ASSERT_EQ(3UL, ipc_sink()->message_count()); |
| 304 EXPECT_EQ(ServiceWorkerHostMsg_IncrementServiceWorkerRefCount::ID, | 314 EXPECT_EQ(ServiceWorkerHostMsg_IncrementServiceWorkerRefCount::ID, |
| 305 ipc_sink()->GetMessageAt(0)->type()); | 315 ipc_sink()->GetMessageAt(0)->type()); |
| 306 EXPECT_EQ(ServiceWorkerHostMsg_DecrementServiceWorkerRefCount::ID, | 316 EXPECT_EQ(ServiceWorkerHostMsg_DecrementServiceWorkerRefCount::ID, |
| 307 ipc_sink()->GetMessageAt(1)->type()); | 317 ipc_sink()->GetMessageAt(1)->type()); |
| 308 EXPECT_EQ(ServiceWorkerHostMsg_DecrementServiceWorkerRefCount::ID, | 318 EXPECT_EQ(ServiceWorkerHostMsg_DecrementServiceWorkerRefCount::ID, |
| 309 ipc_sink()->GetMessageAt(2)->type()); | 319 ipc_sink()->GetMessageAt(2)->type()); |
| 310 provider_client.reset(); | 320 provider_client.reset(); |
| 311 ipc_sink()->ClearMessages(); | 321 ipc_sink()->ClearMessages(); |
| 312 | 322 |
| 313 // (4) In the case there are both SWProviderContext and SWProviderClient for | 323 // (4) In the case there are both SWProviderContext and SWProviderClient for |
| 314 // the provider, the passed referecence should be adopted and owned by the | 324 // the provider, the passed referecence should be adopted and owned by the |
| 315 // provider context. In addition, the new reference should be created for the | 325 // provider context. In addition, the new reference should be created for the |
| 316 // provider client and immediately released due to limitation of the mock | 326 // provider client and immediately released due to limitation of the mock |
| 317 // implementation. | 327 // implementation. |
| 318 provider_context = new ServiceWorkerProviderContext( | 328 provider_context = new ServiceWorkerProviderContext( |
| 319 kProviderId, SERVICE_WORKER_PROVIDER_FOR_WINDOW, thread_safe_sender()); | 329 kProviderId, SERVICE_WORKER_PROVIDER_FOR_WINDOW, thread_safe_sender()); |
| 320 OnAssociateRegistration(kDocumentMainThreadId, kProviderId, info, attrs); | 330 OnAssociateRegistration(kDocumentMainThreadId, kProviderId, info, attrs); |
| 321 provider_client.reset( | 331 provider_client.reset( |
| 322 new MockWebServiceWorkerProviderClientImpl(kProviderId, dispatcher())); | 332 new MockWebServiceWorkerProviderClientImpl(kProviderId, dispatcher())); |
| 323 ASSERT_FALSE(provider_client->is_set_controlled_called()); | 333 ASSERT_FALSE(provider_client->is_set_controlled_called()); |
| 324 ipc_sink()->ClearMessages(); | 334 ipc_sink()->ClearMessages(); |
| 325 OnSetControllerServiceWorker(kDocumentMainThreadId, kProviderId, attrs.active, | 335 OnSetControllerServiceWorker(kDocumentMainThreadId, kProviderId, attrs.active, |
| 326 should_notify_controllerchange); | 336 should_notify_controllerchange, |
| 337 std::set<uint32_t>()); |
| 327 EXPECT_TRUE(provider_client->is_set_controlled_called()); | 338 EXPECT_TRUE(provider_client->is_set_controlled_called()); |
| 328 ASSERT_EQ(2UL, ipc_sink()->message_count()); | 339 ASSERT_EQ(2UL, ipc_sink()->message_count()); |
| 329 EXPECT_EQ(ServiceWorkerHostMsg_IncrementServiceWorkerRefCount::ID, | 340 EXPECT_EQ(ServiceWorkerHostMsg_IncrementServiceWorkerRefCount::ID, |
| 330 ipc_sink()->GetMessageAt(0)->type()); | 341 ipc_sink()->GetMessageAt(0)->type()); |
| 331 EXPECT_EQ(ServiceWorkerHostMsg_DecrementServiceWorkerRefCount::ID, | 342 EXPECT_EQ(ServiceWorkerHostMsg_DecrementServiceWorkerRefCount::ID, |
| 332 ipc_sink()->GetMessageAt(1)->type()); | 343 ipc_sink()->GetMessageAt(1)->type()); |
| 333 } | 344 } |
| 334 | 345 |
| 335 // Test that clearing the controller by sending a kInvalidServiceWorkerHandle | 346 // Test that clearing the controller by sending a kInvalidServiceWorkerHandle |
| 336 // results in the provider context having a null controller. | 347 // results in the provider context having a null controller. |
| 337 TEST_F(ServiceWorkerDispatcherTest, OnSetControllerServiceWorker_Null) { | 348 TEST_F(ServiceWorkerDispatcherTest, OnSetControllerServiceWorker_Null) { |
| 338 const int kProviderId = 10; | 349 const int kProviderId = 10; |
| 339 bool should_notify_controllerchange = true; | 350 bool should_notify_controllerchange = true; |
| 340 | 351 |
| 341 ServiceWorkerRegistrationObjectInfo info; | 352 ServiceWorkerRegistrationObjectInfo info; |
| 342 ServiceWorkerVersionAttributes attrs; | 353 ServiceWorkerVersionAttributes attrs; |
| 343 CreateObjectInfoAndVersionAttributes(&info, &attrs); | 354 CreateObjectInfoAndVersionAttributes(&info, &attrs); |
| 344 | 355 |
| 345 std::unique_ptr<MockWebServiceWorkerProviderClientImpl> provider_client( | 356 std::unique_ptr<MockWebServiceWorkerProviderClientImpl> provider_client( |
| 346 new MockWebServiceWorkerProviderClientImpl(kProviderId, dispatcher())); | 357 new MockWebServiceWorkerProviderClientImpl(kProviderId, dispatcher())); |
| 347 scoped_refptr<ServiceWorkerProviderContext> provider_context( | 358 scoped_refptr<ServiceWorkerProviderContext> provider_context( |
| 348 new ServiceWorkerProviderContext(kProviderId, | 359 new ServiceWorkerProviderContext(kProviderId, |
| 349 SERVICE_WORKER_PROVIDER_FOR_WINDOW, | 360 SERVICE_WORKER_PROVIDER_FOR_WINDOW, |
| 350 thread_safe_sender())); | 361 thread_safe_sender())); |
| 351 | 362 |
| 352 OnAssociateRegistration(kDocumentMainThreadId, kProviderId, info, attrs); | 363 OnAssociateRegistration(kDocumentMainThreadId, kProviderId, info, attrs); |
| 353 | 364 |
| 354 // Set the controller to kInvalidServiceWorkerHandle. | 365 // Set the controller to kInvalidServiceWorkerHandle. |
| 355 OnSetControllerServiceWorker(kDocumentMainThreadId, kProviderId, | 366 OnSetControllerServiceWorker( |
| 356 ServiceWorkerObjectInfo(), | 367 kDocumentMainThreadId, kProviderId, ServiceWorkerObjectInfo(), |
| 357 should_notify_controllerchange); | 368 should_notify_controllerchange, std::set<uint32_t>()); |
| 358 | 369 |
| 359 // Check that it became null. | 370 // Check that it became null. |
| 360 EXPECT_EQ(nullptr, provider_context->controller()); | 371 EXPECT_EQ(nullptr, provider_context->controller()); |
| 361 EXPECT_TRUE(provider_client->is_set_controlled_called()); | 372 EXPECT_TRUE(provider_client->is_set_controlled_called()); |
| 362 } | 373 } |
| 363 | 374 |
| 364 TEST_F(ServiceWorkerDispatcherTest, OnPostMessage) { | 375 TEST_F(ServiceWorkerDispatcherTest, OnPostMessage) { |
| 365 const int kProviderId = 10; | 376 const int kProviderId = 10; |
| 366 | 377 |
| 367 // Assume that these objects are passed from the browser process and own | 378 // Assume that these objects are passed from the browser process and own |
| (...skipping 147 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 515 ipc_sink()->GetMessageAt(0)->type()); | 526 ipc_sink()->GetMessageAt(0)->type()); |
| 516 EXPECT_EQ(ServiceWorkerHostMsg_DecrementServiceWorkerRefCount::ID, | 527 EXPECT_EQ(ServiceWorkerHostMsg_DecrementServiceWorkerRefCount::ID, |
| 517 ipc_sink()->GetMessageAt(1)->type()); | 528 ipc_sink()->GetMessageAt(1)->type()); |
| 518 EXPECT_EQ(ServiceWorkerHostMsg_DecrementServiceWorkerRefCount::ID, | 529 EXPECT_EQ(ServiceWorkerHostMsg_DecrementServiceWorkerRefCount::ID, |
| 519 ipc_sink()->GetMessageAt(2)->type()); | 530 ipc_sink()->GetMessageAt(2)->type()); |
| 520 EXPECT_EQ(ServiceWorkerHostMsg_DecrementRegistrationRefCount::ID, | 531 EXPECT_EQ(ServiceWorkerHostMsg_DecrementRegistrationRefCount::ID, |
| 521 ipc_sink()->GetMessageAt(3)->type()); | 532 ipc_sink()->GetMessageAt(3)->type()); |
| 522 } | 533 } |
| 523 | 534 |
| 524 } // namespace content | 535 } // namespace content |
| OLD | NEW |