OLD | NEW |
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> |
11 #include <vector> | 11 #include <vector> |
12 | 12 |
13 #include "base/atomic_sequence_num.h" | 13 #include "base/atomic_sequence_num.h" |
14 #include "base/bind.h" | 14 #include "base/bind.h" |
15 #include "base/memory/ptr_util.h" | 15 #include "base/memory/ptr_util.h" |
16 #include "base/threading/thread_task_runner_handle.h" | 16 #include "base/threading/thread_task_runner_handle.h" |
17 #include "base/time/time.h" | 17 #include "base/time/time.h" |
18 #include "content/browser/message_port_message_filter.h" | |
19 #include "content/browser/service_worker/embedded_worker_instance.h" | 18 #include "content/browser/service_worker/embedded_worker_instance.h" |
20 #include "content/browser/service_worker/embedded_worker_registry.h" | 19 #include "content/browser/service_worker/embedded_worker_registry.h" |
21 #include "content/browser/service_worker/embedded_worker_status.h" | 20 #include "content/browser/service_worker/embedded_worker_status.h" |
22 #include "content/browser/service_worker/service_worker_context_core.h" | 21 #include "content/browser/service_worker/service_worker_context_core.h" |
23 #include "content/browser/service_worker/service_worker_context_wrapper.h" | 22 #include "content/browser/service_worker/service_worker_context_wrapper.h" |
24 #include "content/common/service_worker/embedded_worker_messages.h" | 23 #include "content/common/service_worker/embedded_worker_messages.h" |
25 #include "content/common/service_worker/embedded_worker_start_params.h" | 24 #include "content/common/service_worker/embedded_worker_start_params.h" |
26 #include "content/common/service_worker/service_worker_messages.h" | 25 #include "content/common/service_worker/service_worker_messages.h" |
27 #include "content/common/service_worker/service_worker_utils.h" | 26 #include "content/common/service_worker/service_worker_utils.h" |
28 #include "content/public/common/push_event_payload.h" | 27 #include "content/public/common/push_event_payload.h" |
29 #include "content/public/test/mock_render_process_host.h" | 28 #include "content/public/test/mock_render_process_host.h" |
30 #include "content/public/test/test_browser_context.h" | 29 #include "content/public/test/test_browser_context.h" |
31 #include "mojo/public/cpp/bindings/interface_request.h" | 30 #include "mojo/public/cpp/bindings/interface_request.h" |
32 #include "mojo/public/cpp/bindings/strong_binding.h" | 31 #include "mojo/public/cpp/bindings/strong_binding.h" |
33 #include "services/service_manager/public/cpp/interface_provider.h" | 32 #include "services/service_manager/public/cpp/interface_provider.h" |
34 #include "services/service_manager/public/cpp/interface_registry.h" | 33 #include "services/service_manager/public/cpp/interface_registry.h" |
35 #include "services/service_manager/public/interfaces/interface_provider.mojom.h" | 34 #include "services/service_manager/public/interfaces/interface_provider.mojom.h" |
36 #include "testing/gtest/include/gtest/gtest.h" | 35 #include "testing/gtest/include/gtest/gtest.h" |
37 | 36 |
38 namespace content { | 37 namespace content { |
39 | 38 |
40 namespace { | |
41 | |
42 class MockMessagePortMessageFilter : public MessagePortMessageFilter { | |
43 public: | |
44 MockMessagePortMessageFilter() | |
45 : MessagePortMessageFilter( | |
46 base::Bind(&base::AtomicSequenceNumber::GetNext, | |
47 base::Unretained(&next_routing_id_))) {} | |
48 | |
49 bool Send(IPC::Message* message) override { | |
50 message_queue_.push_back(base::WrapUnique(message)); | |
51 return true; | |
52 } | |
53 | |
54 private: | |
55 ~MockMessagePortMessageFilter() override {} | |
56 base::AtomicSequenceNumber next_routing_id_; | |
57 std::vector<std::unique_ptr<IPC::Message>> message_queue_; | |
58 }; | |
59 | |
60 } // namespace | |
61 | |
62 EmbeddedWorkerTestHelper::MockEmbeddedWorkerInstanceClient:: | 39 EmbeddedWorkerTestHelper::MockEmbeddedWorkerInstanceClient:: |
63 MockEmbeddedWorkerInstanceClient( | 40 MockEmbeddedWorkerInstanceClient( |
64 base::WeakPtr<EmbeddedWorkerTestHelper> helper) | 41 base::WeakPtr<EmbeddedWorkerTestHelper> helper) |
65 : helper_(helper), binding_(this) {} | 42 : helper_(helper), binding_(this) {} |
66 | 43 |
67 EmbeddedWorkerTestHelper::MockEmbeddedWorkerInstanceClient:: | 44 EmbeddedWorkerTestHelper::MockEmbeddedWorkerInstanceClient:: |
68 ~MockEmbeddedWorkerInstanceClient() {} | 45 ~MockEmbeddedWorkerInstanceClient() {} |
69 | 46 |
70 void EmbeddedWorkerTestHelper::MockEmbeddedWorkerInstanceClient::StartWorker( | 47 void EmbeddedWorkerTestHelper::MockEmbeddedWorkerInstanceClient::StartWorker( |
71 const EmbeddedWorkerStartParams& params, | 48 const EmbeddedWorkerStartParams& params, |
(...skipping 134 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
206 mock_render_process_id_(render_process_host_->GetID()), | 183 mock_render_process_id_(render_process_host_->GetID()), |
207 new_mock_render_process_id_(new_render_process_host_->GetID()), | 184 new_mock_render_process_id_(new_render_process_host_->GetID()), |
208 weak_factory_(this) { | 185 weak_factory_(this) { |
209 std::unique_ptr<MockServiceWorkerDatabaseTaskManager> database_task_manager( | 186 std::unique_ptr<MockServiceWorkerDatabaseTaskManager> database_task_manager( |
210 new MockServiceWorkerDatabaseTaskManager( | 187 new MockServiceWorkerDatabaseTaskManager( |
211 base::ThreadTaskRunnerHandle::Get())); | 188 base::ThreadTaskRunnerHandle::Get())); |
212 wrapper_->InitInternal(user_data_directory, std::move(database_task_manager), | 189 wrapper_->InitInternal(user_data_directory, std::move(database_task_manager), |
213 base::ThreadTaskRunnerHandle::Get(), nullptr, nullptr); | 190 base::ThreadTaskRunnerHandle::Get(), nullptr, nullptr); |
214 wrapper_->process_manager()->SetProcessIdForTest(mock_render_process_id()); | 191 wrapper_->process_manager()->SetProcessIdForTest(mock_render_process_id()); |
215 wrapper_->process_manager()->SetNewProcessIdForTest(new_render_process_id()); | 192 wrapper_->process_manager()->SetNewProcessIdForTest(new_render_process_id()); |
216 registry()->AddChildProcessSender(mock_render_process_id_, this, | 193 registry()->AddChildProcessSender(mock_render_process_id_, this); |
217 NewMessagePortMessageFilter()); | |
218 | 194 |
219 // Setup process level interface registry. | 195 // Setup process level interface registry. |
220 render_process_interface_registry_ = | 196 render_process_interface_registry_ = |
221 CreateInterfaceRegistry(render_process_host_.get()); | 197 CreateInterfaceRegistry(render_process_host_.get()); |
222 new_render_process_interface_registry_ = | 198 new_render_process_interface_registry_ = |
223 CreateInterfaceRegistry(new_render_process_host_.get()); | 199 CreateInterfaceRegistry(new_render_process_host_.get()); |
224 } | 200 } |
225 | 201 |
226 EmbeddedWorkerTestHelper::~EmbeddedWorkerTestHelper() { | 202 EmbeddedWorkerTestHelper::~EmbeddedWorkerTestHelper() { |
227 if (wrapper_.get()) | 203 if (wrapper_.get()) |
228 wrapper_->Shutdown(); | 204 wrapper_->Shutdown(); |
229 } | 205 } |
230 | 206 |
231 void EmbeddedWorkerTestHelper::SimulateAddProcessToPattern(const GURL& pattern, | 207 void EmbeddedWorkerTestHelper::SimulateAddProcessToPattern(const GURL& pattern, |
232 int process_id) { | 208 int process_id) { |
233 registry()->AddChildProcessSender(process_id, this, | 209 registry()->AddChildProcessSender(process_id, this); |
234 NewMessagePortMessageFilter()); | |
235 wrapper_->process_manager()->AddProcessReferenceToPattern(pattern, | 210 wrapper_->process_manager()->AddProcessReferenceToPattern(pattern, |
236 process_id); | 211 process_id); |
237 } | 212 } |
238 | 213 |
239 bool EmbeddedWorkerTestHelper::Send(IPC::Message* message) { | 214 bool EmbeddedWorkerTestHelper::Send(IPC::Message* message) { |
240 OnMessageReceived(*message); | 215 OnMessageReceived(*message); |
241 delete message; | 216 delete message; |
242 return true; | 217 return true; |
243 } | 218 } |
244 | 219 |
(...skipping 312 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
557 FROM_HERE, | 532 FROM_HERE, |
558 base::Bind(&EmbeddedWorkerTestHelper::OnPaymentRequestEvent, AsWeakPtr(), | 533 base::Bind(&EmbeddedWorkerTestHelper::OnPaymentRequestEvent, AsWeakPtr(), |
559 base::Passed(std::move(app_request)), callback)); | 534 base::Passed(std::move(app_request)), callback)); |
560 } | 535 } |
561 | 536 |
562 EmbeddedWorkerRegistry* EmbeddedWorkerTestHelper::registry() { | 537 EmbeddedWorkerRegistry* EmbeddedWorkerTestHelper::registry() { |
563 DCHECK(context()); | 538 DCHECK(context()); |
564 return context()->embedded_worker_registry(); | 539 return context()->embedded_worker_registry(); |
565 } | 540 } |
566 | 541 |
567 MessagePortMessageFilter* | |
568 EmbeddedWorkerTestHelper::NewMessagePortMessageFilter() { | |
569 scoped_refptr<MessagePortMessageFilter> filter( | |
570 new MockMessagePortMessageFilter); | |
571 message_port_message_filters_.push_back(filter); | |
572 return filter.get(); | |
573 } | |
574 | |
575 std::unique_ptr<service_manager::InterfaceRegistry> | 542 std::unique_ptr<service_manager::InterfaceRegistry> |
576 EmbeddedWorkerTestHelper::CreateInterfaceRegistry(MockRenderProcessHost* rph) { | 543 EmbeddedWorkerTestHelper::CreateInterfaceRegistry(MockRenderProcessHost* rph) { |
577 auto registry = | 544 auto registry = |
578 base::MakeUnique<service_manager::InterfaceRegistry>(std::string()); | 545 base::MakeUnique<service_manager::InterfaceRegistry>(std::string()); |
579 registry->AddInterface( | 546 registry->AddInterface( |
580 base::Bind(&MockEmbeddedWorkerInstanceClient::Bind, AsWeakPtr())); | 547 base::Bind(&MockEmbeddedWorkerInstanceClient::Bind, AsWeakPtr())); |
581 | 548 |
582 service_manager::mojom::InterfaceProviderPtr interfaces; | 549 service_manager::mojom::InterfaceProviderPtr interfaces; |
583 registry->Bind(mojo::MakeRequest(&interfaces), service_manager::Identity(), | 550 registry->Bind(mojo::MakeRequest(&interfaces), service_manager::Identity(), |
584 service_manager::InterfaceProviderSpec(), | 551 service_manager::InterfaceProviderSpec(), |
585 service_manager::Identity(), | 552 service_manager::Identity(), |
586 service_manager::InterfaceProviderSpec()); | 553 service_manager::InterfaceProviderSpec()); |
587 | 554 |
588 std::unique_ptr<service_manager::InterfaceProvider> remote_interfaces( | 555 std::unique_ptr<service_manager::InterfaceProvider> remote_interfaces( |
589 new service_manager::InterfaceProvider); | 556 new service_manager::InterfaceProvider); |
590 remote_interfaces->Bind(std::move(interfaces)); | 557 remote_interfaces->Bind(std::move(interfaces)); |
591 rph->SetRemoteInterfaces(std::move(remote_interfaces)); | 558 rph->SetRemoteInterfaces(std::move(remote_interfaces)); |
592 return registry; | 559 return registry; |
593 } | 560 } |
594 | 561 |
595 } // namespace content | 562 } // namespace content |
OLD | NEW |