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

Side by Side Diff: content/browser/service_worker/embedded_worker_test_helper.cc

Issue 2779763004: Create ServiceWorkerProviderHost before starting worker (Closed)
Patch Set: Pass the param of BindWithProviderInfo by value instead of pointer Created 3 years, 6 months 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 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/browser/service_worker/embedded_worker_test_helper.h" 5 #include "content/browser/service_worker/embedded_worker_test_helper.h"
6 6
7 #include <map> 7 #include <map>
8 #include <memory> 8 #include <memory>
9 #include <string> 9 #include <string>
10 #include <utility> 10 #include <utility>
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after
66 MockEmbeddedWorkerInstanceClient( 66 MockEmbeddedWorkerInstanceClient(
67 base::WeakPtr<EmbeddedWorkerTestHelper> helper) 67 base::WeakPtr<EmbeddedWorkerTestHelper> helper)
68 : helper_(helper), binding_(this) {} 68 : helper_(helper), binding_(this) {}
69 69
70 EmbeddedWorkerTestHelper::MockEmbeddedWorkerInstanceClient:: 70 EmbeddedWorkerTestHelper::MockEmbeddedWorkerInstanceClient::
71 ~MockEmbeddedWorkerInstanceClient() {} 71 ~MockEmbeddedWorkerInstanceClient() {}
72 72
73 void EmbeddedWorkerTestHelper::MockEmbeddedWorkerInstanceClient::StartWorker( 73 void EmbeddedWorkerTestHelper::MockEmbeddedWorkerInstanceClient::StartWorker(
74 const EmbeddedWorkerStartParams& params, 74 const EmbeddedWorkerStartParams& params,
75 mojom::ServiceWorkerEventDispatcherRequest dispatcher_request, 75 mojom::ServiceWorkerEventDispatcherRequest dispatcher_request,
76 mojom::EmbeddedWorkerInstanceHostAssociatedPtrInfo instance_host) { 76 mojom::EmbeddedWorkerInstanceHostAssociatedPtrInfo instance_host,
77 mojom::ServiceWorkerProviderInfoForStartWorkerPtr provider_info) {
77 if (!helper_) 78 if (!helper_)
78 return; 79 return;
79 80
80 embedded_worker_id_ = params.embedded_worker_id; 81 embedded_worker_id_ = params.embedded_worker_id;
81 82
82 EmbeddedWorkerInstance* worker = 83 EmbeddedWorkerInstance* worker =
83 helper_->registry()->GetWorker(params.embedded_worker_id); 84 helper_->registry()->GetWorker(params.embedded_worker_id);
84 ASSERT_TRUE(worker); 85 ASSERT_TRUE(worker);
85 EXPECT_EQ(EmbeddedWorkerStatus::STARTING, worker->status()); 86 EXPECT_EQ(EmbeddedWorkerStatus::STARTING, worker->status());
86 87
87 helper_->OnStartWorkerStub(params, std::move(dispatcher_request), 88 helper_->OnStartWorkerStub(params, std::move(dispatcher_request),
88 std::move(instance_host)); 89 std::move(instance_host),
90 std::move(provider_info));
89 } 91 }
90 92
91 void EmbeddedWorkerTestHelper::MockEmbeddedWorkerInstanceClient::StopWorker() { 93 void EmbeddedWorkerTestHelper::MockEmbeddedWorkerInstanceClient::StopWorker() {
92 if (!helper_) 94 if (!helper_)
93 return; 95 return;
94 96
95 ASSERT_TRUE(embedded_worker_id_); 97 ASSERT_TRUE(embedded_worker_id_);
96 EmbeddedWorkerInstance* worker = 98 EmbeddedWorkerInstance* worker =
97 helper_->registry()->GetWorker(embedded_worker_id_.value()); 99 helper_->registry()->GetWorker(embedded_worker_id_.value());
98 // |worker| is possible to be null when corresponding EmbeddedWorkerInstance 100 // |worker| is possible to be null when corresponding EmbeddedWorkerInstance
(...skipping 184 matching lines...) Expand 10 before | Expand all | Expand 10 after
283 285
284 EmbeddedWorkerTestHelper::EmbeddedWorkerTestHelper( 286 EmbeddedWorkerTestHelper::EmbeddedWorkerTestHelper(
285 const base::FilePath& user_data_directory) 287 const base::FilePath& user_data_directory)
286 : browser_context_(new TestBrowserContext), 288 : browser_context_(new TestBrowserContext),
287 render_process_host_(new MockRenderProcessHost(browser_context_.get())), 289 render_process_host_(new MockRenderProcessHost(browser_context_.get())),
288 new_render_process_host_( 290 new_render_process_host_(
289 new MockRenderProcessHost(browser_context_.get())), 291 new MockRenderProcessHost(browser_context_.get())),
290 wrapper_(new ServiceWorkerContextWrapper(browser_context_.get())), 292 wrapper_(new ServiceWorkerContextWrapper(browser_context_.get())),
291 mock_instance_clients_next_index_(0), 293 mock_instance_clients_next_index_(0),
292 next_thread_id_(0), 294 next_thread_id_(0),
293 next_provider_id_(1000),
294 mock_render_process_id_(render_process_host_->GetID()), 295 mock_render_process_id_(render_process_host_->GetID()),
295 new_mock_render_process_id_(new_render_process_host_->GetID()), 296 new_mock_render_process_id_(new_render_process_host_->GetID()),
296 weak_factory_(this) { 297 weak_factory_(this) {
297 std::unique_ptr<MockServiceWorkerDatabaseTaskManager> database_task_manager( 298 std::unique_ptr<MockServiceWorkerDatabaseTaskManager> database_task_manager(
298 new MockServiceWorkerDatabaseTaskManager( 299 new MockServiceWorkerDatabaseTaskManager(
299 base::ThreadTaskRunnerHandle::Get())); 300 base::ThreadTaskRunnerHandle::Get()));
300 wrapper_->InitInternal(user_data_directory, std::move(database_task_manager), 301 wrapper_->InitInternal(user_data_directory, std::move(database_task_manager),
301 base::ThreadTaskRunnerHandle::Get(), nullptr, nullptr); 302 base::ThreadTaskRunnerHandle::Get(), nullptr, nullptr);
302 wrapper_->process_manager()->SetProcessIdForTest(mock_render_process_id()); 303 wrapper_->process_manager()->SetProcessIdForTest(mock_render_process_id());
303 wrapper_->process_manager()->SetNewProcessIdForTest(new_render_process_id()); 304 wrapper_->process_manager()->SetNewProcessIdForTest(new_render_process_id());
(...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after
388 return info; 389 return info;
389 } 390 }
390 391
391 void EmbeddedWorkerTestHelper::OnStartWorker( 392 void EmbeddedWorkerTestHelper::OnStartWorker(
392 int embedded_worker_id, 393 int embedded_worker_id,
393 int64_t service_worker_version_id, 394 int64_t service_worker_version_id,
394 const GURL& scope, 395 const GURL& scope,
395 const GURL& script_url, 396 const GURL& script_url,
396 bool pause_after_download, 397 bool pause_after_download,
397 mojom::ServiceWorkerEventDispatcherRequest request, 398 mojom::ServiceWorkerEventDispatcherRequest request,
398 mojom::EmbeddedWorkerInstanceHostAssociatedPtrInfo instance_host) { 399 mojom::EmbeddedWorkerInstanceHostAssociatedPtrInfo instance_host,
400 mojom::ServiceWorkerProviderInfoForStartWorkerPtr provider_info) {
399 EmbeddedWorkerInstance* worker = registry()->GetWorker(embedded_worker_id); 401 EmbeddedWorkerInstance* worker = registry()->GetWorker(embedded_worker_id);
400 ASSERT_TRUE(worker); 402 ASSERT_TRUE(worker);
401 MockServiceWorkerEventDispatcher::Create(AsWeakPtr(), worker->thread_id(), 403 MockServiceWorkerEventDispatcher::Create(AsWeakPtr(), worker->thread_id(),
402 std::move(request)); 404 std::move(request));
403 405
404 embedded_worker_id_service_worker_version_id_map_[embedded_worker_id] = 406 embedded_worker_id_service_worker_version_id_map_[embedded_worker_id] =
405 service_worker_version_id; 407 service_worker_version_id;
406 embedded_worker_id_instance_host_ptr_map_[embedded_worker_id].Bind( 408 embedded_worker_id_instance_host_ptr_map_[embedded_worker_id].Bind(
407 std::move(instance_host)); 409 std::move(instance_host));
410 ServiceWorkerRemoteProviderEndpoint* provider_endpoint =
411 &embedded_worker_id_remote_provider_map_[embedded_worker_id];
412 provider_endpoint->BindWithProviderInfo(std::move(provider_info));
408 413
409 SimulateWorkerReadyForInspection(embedded_worker_id); 414 SimulateWorkerReadyForInspection(embedded_worker_id);
410 SimulateWorkerScriptCached(embedded_worker_id); 415 SimulateWorkerScriptCached(embedded_worker_id);
411 SimulateWorkerScriptLoaded(embedded_worker_id); 416 SimulateWorkerScriptLoaded(embedded_worker_id);
412 if (!pause_after_download) 417 if (!pause_after_download)
413 OnResumeAfterDownload(embedded_worker_id); 418 OnResumeAfterDownload(embedded_worker_id);
414 } 419 }
415 420
416 void EmbeddedWorkerTestHelper::OnResumeAfterDownload(int embedded_worker_id) { 421 void EmbeddedWorkerTestHelper::OnResumeAfterDownload(int embedded_worker_id) {
417 SimulateWorkerThreadStarted(GetNextThreadId(), embedded_worker_id, 422 SimulateWorkerThreadStarted(GetNextThreadId(), embedded_worker_id);
418 GetNextProviderId());
419 SimulateWorkerScriptEvaluated(embedded_worker_id, true /* success */); 423 SimulateWorkerScriptEvaluated(embedded_worker_id, true /* success */);
420 SimulateWorkerStarted(embedded_worker_id); 424 SimulateWorkerStarted(embedded_worker_id);
421 } 425 }
422 426
423 void EmbeddedWorkerTestHelper::OnStopWorker(int embedded_worker_id) { 427 void EmbeddedWorkerTestHelper::OnStopWorker(int embedded_worker_id) {
424 // By default just notify the sender that the worker is stopped. 428 // By default just notify the sender that the worker is stopped.
425 SimulateWorkerStopped(embedded_worker_id); 429 SimulateWorkerStopped(embedded_worker_id);
426 } 430 }
427 431
428 void EmbeddedWorkerTestHelper::OnActivateEvent( 432 void EmbeddedWorkerTestHelper::OnActivateEvent(
(...skipping 140 matching lines...) Expand 10 before | Expand all | Expand 10 after
569 EmbeddedWorkerInstance* worker = registry()->GetWorker(embedded_worker_id); 573 EmbeddedWorkerInstance* worker = registry()->GetWorker(embedded_worker_id);
570 ASSERT_TRUE(worker); 574 ASSERT_TRUE(worker);
571 ASSERT_TRUE(embedded_worker_id_instance_host_ptr_map_[embedded_worker_id]); 575 ASSERT_TRUE(embedded_worker_id_instance_host_ptr_map_[embedded_worker_id]);
572 embedded_worker_id_instance_host_ptr_map_[embedded_worker_id] 576 embedded_worker_id_instance_host_ptr_map_[embedded_worker_id]
573 ->OnScriptLoaded(); 577 ->OnScriptLoaded();
574 base::RunLoop().RunUntilIdle(); 578 base::RunLoop().RunUntilIdle();
575 } 579 }
576 580
577 void EmbeddedWorkerTestHelper::SimulateWorkerThreadStarted( 581 void EmbeddedWorkerTestHelper::SimulateWorkerThreadStarted(
578 int thread_id, 582 int thread_id,
579 int embedded_worker_id, 583 int embedded_worker_id) {
580 int provider_id) {
581 EmbeddedWorkerInstance* worker = registry()->GetWorker(embedded_worker_id); 584 EmbeddedWorkerInstance* worker = registry()->GetWorker(embedded_worker_id);
582 ASSERT_TRUE(worker); 585 ASSERT_TRUE(worker);
583 ASSERT_TRUE(embedded_worker_id_instance_host_ptr_map_[embedded_worker_id]); 586 ASSERT_TRUE(embedded_worker_id_instance_host_ptr_map_[embedded_worker_id]);
584
585 // Prepare a provider host to be used by following OnThreadStarted().
586 std::unique_ptr<ServiceWorkerProviderHost> host =
587 CreateProviderHostForServiceWorkerContext(
588 worker->process_id(), provider_id, true /* is_parent_frame_secure */,
589 context()->AsWeakPtr(),
590 &embedded_worker_id_remote_provider_map_[embedded_worker_id]);
591 context()->AddProviderHost(std::move(host));
592
593 embedded_worker_id_instance_host_ptr_map_[embedded_worker_id] 587 embedded_worker_id_instance_host_ptr_map_[embedded_worker_id]
594 ->OnThreadStarted(thread_id, provider_id); 588 ->OnThreadStarted(thread_id);
595 base::RunLoop().RunUntilIdle(); 589 base::RunLoop().RunUntilIdle();
596 } 590 }
597 591
598 void EmbeddedWorkerTestHelper::SimulateWorkerScriptEvaluated( 592 void EmbeddedWorkerTestHelper::SimulateWorkerScriptEvaluated(
599 int embedded_worker_id, 593 int embedded_worker_id,
600 bool success) { 594 bool success) {
601 EmbeddedWorkerInstance* worker = registry()->GetWorker(embedded_worker_id); 595 EmbeddedWorkerInstance* worker = registry()->GetWorker(embedded_worker_id);
602 ASSERT_TRUE(worker); 596 ASSERT_TRUE(worker);
603 ASSERT_TRUE(embedded_worker_id_instance_host_ptr_map_[embedded_worker_id]); 597 ASSERT_TRUE(embedded_worker_id_instance_host_ptr_map_[embedded_worker_id]);
604 embedded_worker_id_instance_host_ptr_map_[embedded_worker_id] 598 embedded_worker_id_instance_host_ptr_map_[embedded_worker_id]
(...skipping 19 matching lines...) Expand all
624 } 618 }
625 619
626 void EmbeddedWorkerTestHelper::SimulateSend(IPC::Message* message) { 620 void EmbeddedWorkerTestHelper::SimulateSend(IPC::Message* message) {
627 registry()->OnMessageReceived(*message, mock_render_process_id_); 621 registry()->OnMessageReceived(*message, mock_render_process_id_);
628 delete message; 622 delete message;
629 } 623 }
630 624
631 void EmbeddedWorkerTestHelper::OnStartWorkerStub( 625 void EmbeddedWorkerTestHelper::OnStartWorkerStub(
632 const EmbeddedWorkerStartParams& params, 626 const EmbeddedWorkerStartParams& params,
633 mojom::ServiceWorkerEventDispatcherRequest request, 627 mojom::ServiceWorkerEventDispatcherRequest request,
634 mojom::EmbeddedWorkerInstanceHostAssociatedPtrInfo instance_host) { 628 mojom::EmbeddedWorkerInstanceHostAssociatedPtrInfo instance_host,
629 mojom::ServiceWorkerProviderInfoForStartWorkerPtr provider_info) {
635 EmbeddedWorkerInstance* worker = 630 EmbeddedWorkerInstance* worker =
636 registry()->GetWorker(params.embedded_worker_id); 631 registry()->GetWorker(params.embedded_worker_id);
637 ASSERT_TRUE(worker); 632 ASSERT_TRUE(worker);
638 EXPECT_EQ(EmbeddedWorkerStatus::STARTING, worker->status()); 633 EXPECT_EQ(EmbeddedWorkerStatus::STARTING, worker->status());
639 base::ThreadTaskRunnerHandle::Get()->PostTask( 634 base::ThreadTaskRunnerHandle::Get()->PostTask(
640 FROM_HERE, 635 FROM_HERE,
641 base::Bind(&EmbeddedWorkerTestHelper::OnStartWorker, AsWeakPtr(), 636 base::Bind(&EmbeddedWorkerTestHelper::OnStartWorker, AsWeakPtr(),
642 params.embedded_worker_id, params.service_worker_version_id, 637 params.embedded_worker_id, params.service_worker_version_id,
643 params.scope, params.script_url, params.pause_after_download, 638 params.scope, params.script_url, params.pause_after_download,
644 base::Passed(&request), base::Passed(&instance_host))); 639 base::Passed(&request), base::Passed(&instance_host),
640 base::Passed(&provider_info)));
645 } 641 }
646 642
647 void EmbeddedWorkerTestHelper::OnResumeAfterDownloadStub( 643 void EmbeddedWorkerTestHelper::OnResumeAfterDownloadStub(
648 int embedded_worker_id) { 644 int embedded_worker_id) {
649 EmbeddedWorkerInstance* worker = registry()->GetWorker(embedded_worker_id); 645 EmbeddedWorkerInstance* worker = registry()->GetWorker(embedded_worker_id);
650 ASSERT_TRUE(worker); 646 ASSERT_TRUE(worker);
651 base::ThreadTaskRunnerHandle::Get()->PostTask( 647 base::ThreadTaskRunnerHandle::Get()->PostTask(
652 FROM_HERE, base::Bind(&EmbeddedWorkerTestHelper::OnResumeAfterDownload, 648 FROM_HERE, base::Bind(&EmbeddedWorkerTestHelper::OnResumeAfterDownload,
653 AsWeakPtr(), embedded_worker_id)); 649 AsWeakPtr(), embedded_worker_id));
654 } 650 }
(...skipping 143 matching lines...) Expand 10 before | Expand all | Expand 10 after
798 base::Passed(&event_data), base::Passed(&response_callback), 794 base::Passed(&event_data), base::Passed(&response_callback),
799 base::Passed(&callback))); 795 base::Passed(&callback)));
800 } 796 }
801 797
802 EmbeddedWorkerRegistry* EmbeddedWorkerTestHelper::registry() { 798 EmbeddedWorkerRegistry* EmbeddedWorkerTestHelper::registry() {
803 DCHECK(context()); 799 DCHECK(context());
804 return context()->embedded_worker_registry(); 800 return context()->embedded_worker_registry();
805 } 801 }
806 802
807 } // namespace content 803 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698