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 156 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
228 mock_render_process_id_(render_process_host_->GetID()), | 205 mock_render_process_id_(render_process_host_->GetID()), |
229 new_mock_render_process_id_(new_render_process_host_->GetID()), | 206 new_mock_render_process_id_(new_render_process_host_->GetID()), |
230 weak_factory_(this) { | 207 weak_factory_(this) { |
231 std::unique_ptr<MockServiceWorkerDatabaseTaskManager> database_task_manager( | 208 std::unique_ptr<MockServiceWorkerDatabaseTaskManager> database_task_manager( |
232 new MockServiceWorkerDatabaseTaskManager( | 209 new MockServiceWorkerDatabaseTaskManager( |
233 base::ThreadTaskRunnerHandle::Get())); | 210 base::ThreadTaskRunnerHandle::Get())); |
234 wrapper_->InitInternal(user_data_directory, std::move(database_task_manager), | 211 wrapper_->InitInternal(user_data_directory, std::move(database_task_manager), |
235 base::ThreadTaskRunnerHandle::Get(), nullptr, nullptr); | 212 base::ThreadTaskRunnerHandle::Get(), nullptr, nullptr); |
236 wrapper_->process_manager()->SetProcessIdForTest(mock_render_process_id()); | 213 wrapper_->process_manager()->SetProcessIdForTest(mock_render_process_id()); |
237 wrapper_->process_manager()->SetNewProcessIdForTest(new_render_process_id()); | 214 wrapper_->process_manager()->SetNewProcessIdForTest(new_render_process_id()); |
238 registry()->AddChildProcessSender(mock_render_process_id_, this, | 215 registry()->AddChildProcessSender(mock_render_process_id_, this); |
239 NewMessagePortMessageFilter()); | |
240 | 216 |
241 // Setup process level interface registry. | 217 // Setup process level interface registry. |
242 render_process_interface_registry_ = | 218 render_process_interface_registry_ = |
243 CreateInterfaceRegistry(render_process_host_.get()); | 219 CreateInterfaceRegistry(render_process_host_.get()); |
244 new_render_process_interface_registry_ = | 220 new_render_process_interface_registry_ = |
245 CreateInterfaceRegistry(new_render_process_host_.get()); | 221 CreateInterfaceRegistry(new_render_process_host_.get()); |
246 } | 222 } |
247 | 223 |
248 EmbeddedWorkerTestHelper::~EmbeddedWorkerTestHelper() { | 224 EmbeddedWorkerTestHelper::~EmbeddedWorkerTestHelper() { |
249 if (wrapper_.get()) | 225 if (wrapper_.get()) |
250 wrapper_->Shutdown(); | 226 wrapper_->Shutdown(); |
251 } | 227 } |
252 | 228 |
253 void EmbeddedWorkerTestHelper::SimulateAddProcessToPattern(const GURL& pattern, | 229 void EmbeddedWorkerTestHelper::SimulateAddProcessToPattern(const GURL& pattern, |
254 int process_id) { | 230 int process_id) { |
255 registry()->AddChildProcessSender(process_id, this, | 231 registry()->AddChildProcessSender(process_id, this); |
256 NewMessagePortMessageFilter()); | |
257 wrapper_->process_manager()->AddProcessReferenceToPattern(pattern, | 232 wrapper_->process_manager()->AddProcessReferenceToPattern(pattern, |
258 process_id); | 233 process_id); |
259 } | 234 } |
260 | 235 |
261 bool EmbeddedWorkerTestHelper::Send(IPC::Message* message) { | 236 bool EmbeddedWorkerTestHelper::Send(IPC::Message* message) { |
262 OnMessageReceived(*message); | 237 OnMessageReceived(*message); |
263 delete message; | 238 delete message; |
264 return true; | 239 return true; |
265 } | 240 } |
266 | 241 |
(...skipping 354 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
621 FROM_HERE, | 596 FROM_HERE, |
622 base::Bind(&EmbeddedWorkerTestHelper::OnPaymentRequestEvent, AsWeakPtr(), | 597 base::Bind(&EmbeddedWorkerTestHelper::OnPaymentRequestEvent, AsWeakPtr(), |
623 base::Passed(std::move(app_request)), callback)); | 598 base::Passed(std::move(app_request)), callback)); |
624 } | 599 } |
625 | 600 |
626 EmbeddedWorkerRegistry* EmbeddedWorkerTestHelper::registry() { | 601 EmbeddedWorkerRegistry* EmbeddedWorkerTestHelper::registry() { |
627 DCHECK(context()); | 602 DCHECK(context()); |
628 return context()->embedded_worker_registry(); | 603 return context()->embedded_worker_registry(); |
629 } | 604 } |
630 | 605 |
631 MessagePortMessageFilter* | |
632 EmbeddedWorkerTestHelper::NewMessagePortMessageFilter() { | |
633 scoped_refptr<MessagePortMessageFilter> filter( | |
634 new MockMessagePortMessageFilter); | |
635 message_port_message_filters_.push_back(filter); | |
636 return filter.get(); | |
637 } | |
638 | |
639 std::unique_ptr<service_manager::InterfaceRegistry> | 606 std::unique_ptr<service_manager::InterfaceRegistry> |
640 EmbeddedWorkerTestHelper::CreateInterfaceRegistry(MockRenderProcessHost* rph) { | 607 EmbeddedWorkerTestHelper::CreateInterfaceRegistry(MockRenderProcessHost* rph) { |
641 auto registry = | 608 auto registry = |
642 base::MakeUnique<service_manager::InterfaceRegistry>(std::string()); | 609 base::MakeUnique<service_manager::InterfaceRegistry>(std::string()); |
643 registry->AddInterface( | 610 registry->AddInterface( |
644 base::Bind(&MockEmbeddedWorkerInstanceClient::Bind, AsWeakPtr())); | 611 base::Bind(&MockEmbeddedWorkerInstanceClient::Bind, AsWeakPtr())); |
645 | 612 |
646 service_manager::mojom::InterfaceProviderPtr interfaces; | 613 service_manager::mojom::InterfaceProviderPtr interfaces; |
647 registry->Bind(mojo::MakeRequest(&interfaces), service_manager::Identity(), | 614 registry->Bind(mojo::MakeRequest(&interfaces), service_manager::Identity(), |
648 service_manager::InterfaceProviderSpec(), | 615 service_manager::InterfaceProviderSpec(), |
649 service_manager::Identity(), | 616 service_manager::Identity(), |
650 service_manager::InterfaceProviderSpec()); | 617 service_manager::InterfaceProviderSpec()); |
651 | 618 |
652 std::unique_ptr<service_manager::InterfaceProvider> remote_interfaces( | 619 std::unique_ptr<service_manager::InterfaceProvider> remote_interfaces( |
653 new service_manager::InterfaceProvider); | 620 new service_manager::InterfaceProvider); |
654 remote_interfaces->Bind(std::move(interfaces)); | 621 remote_interfaces->Bind(std::move(interfaces)); |
655 rph->SetRemoteInterfaces(std::move(remote_interfaces)); | 622 rph->SetRemoteInterfaces(std::move(remote_interfaces)); |
656 return registry; | 623 return registry; |
657 } | 624 } |
658 | 625 |
659 } // namespace content | 626 } // namespace content |
OLD | NEW |