| 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/service_worker/embedded_worker_instance.h" | 18 #include "content/browser/service_worker/embedded_worker_instance.h" |
| 19 #include "content/browser/service_worker/embedded_worker_registry.h" | 19 #include "content/browser/service_worker/embedded_worker_registry.h" |
| 20 #include "content/browser/service_worker/embedded_worker_status.h" | 20 #include "content/browser/service_worker/embedded_worker_status.h" |
| 21 #include "content/browser/service_worker/service_worker_context_core.h" | 21 #include "content/browser/service_worker/service_worker_context_core.h" |
| 22 #include "content/browser/service_worker/service_worker_context_wrapper.h" | 22 #include "content/browser/service_worker/service_worker_context_wrapper.h" |
| 23 #include "content/browser/service_worker/service_worker_dispatcher_host.h" |
| 23 #include "content/common/service_worker/embedded_worker_messages.h" | 24 #include "content/common/service_worker/embedded_worker_messages.h" |
| 24 #include "content/common/service_worker/embedded_worker_start_params.h" | 25 #include "content/common/service_worker/embedded_worker_start_params.h" |
| 25 #include "content/common/service_worker/service_worker_messages.h" | 26 #include "content/common/service_worker/service_worker_messages.h" |
| 26 #include "content/common/service_worker/service_worker_utils.h" | 27 #include "content/common/service_worker/service_worker_utils.h" |
| 27 #include "content/public/common/push_event_payload.h" | 28 #include "content/public/common/push_event_payload.h" |
| 28 #include "content/public/test/mock_render_process_host.h" | 29 #include "content/public/test/mock_render_process_host.h" |
| 29 #include "content/public/test/test_browser_context.h" | 30 #include "content/public/test/test_browser_context.h" |
| 30 #include "mojo/public/cpp/bindings/interface_request.h" | 31 #include "mojo/public/cpp/bindings/interface_request.h" |
| 31 #include "mojo/public/cpp/bindings/strong_binding.h" | 32 #include "mojo/public/cpp/bindings/strong_binding.h" |
| 32 #include "services/service_manager/public/cpp/interface_provider.h" | 33 #include "services/service_manager/public/cpp/interface_provider.h" |
| 33 #include "services/service_manager/public/cpp/interface_registry.h" | 34 #include "services/service_manager/public/cpp/interface_registry.h" |
| 34 #include "services/service_manager/public/interfaces/interface_provider.mojom.h" | 35 #include "services/service_manager/public/interfaces/interface_provider.mojom.h" |
| 35 #include "testing/gtest/include/gtest/gtest.h" | 36 #include "testing/gtest/include/gtest/gtest.h" |
| 36 | 37 |
| 37 namespace content { | 38 namespace content { |
| 38 | 39 |
| 40 namespace { |
| 41 |
| 42 class MockServiceWorkerDispatcherHost : public ServiceWorkerDispatcherHost { |
| 43 public: |
| 44 MockServiceWorkerDispatcherHost(int process_id, |
| 45 ResourceContext* resource_context, |
| 46 IPC::Sender* sender) |
| 47 : ServiceWorkerDispatcherHost(process_id, resource_context), |
| 48 sender_(sender) {} |
| 49 |
| 50 bool Send(IPC::Message* message) override { return sender_->Send(message); } |
| 51 |
| 52 protected: |
| 53 ~MockServiceWorkerDispatcherHost() override {} |
| 54 |
| 55 private: |
| 56 IPC::Sender* sender_; |
| 57 |
| 58 DISALLOW_COPY_AND_ASSIGN(MockServiceWorkerDispatcherHost); |
| 59 }; |
| 60 |
| 61 } // namespace |
| 62 |
| 39 EmbeddedWorkerTestHelper::MockEmbeddedWorkerInstanceClient:: | 63 EmbeddedWorkerTestHelper::MockEmbeddedWorkerInstanceClient:: |
| 40 MockEmbeddedWorkerInstanceClient( | 64 MockEmbeddedWorkerInstanceClient( |
| 41 base::WeakPtr<EmbeddedWorkerTestHelper> helper) | 65 base::WeakPtr<EmbeddedWorkerTestHelper> helper) |
| 42 : helper_(helper), binding_(this) {} | 66 : helper_(helper), binding_(this) {} |
| 43 | 67 |
| 44 EmbeddedWorkerTestHelper::MockEmbeddedWorkerInstanceClient:: | 68 EmbeddedWorkerTestHelper::MockEmbeddedWorkerInstanceClient:: |
| 45 ~MockEmbeddedWorkerInstanceClient() {} | 69 ~MockEmbeddedWorkerInstanceClient() {} |
| 46 | 70 |
| 47 void EmbeddedWorkerTestHelper::MockEmbeddedWorkerInstanceClient::StartWorker( | 71 void EmbeddedWorkerTestHelper::MockEmbeddedWorkerInstanceClient::StartWorker( |
| 48 const EmbeddedWorkerStartParams& params, | 72 const EmbeddedWorkerStartParams& params, |
| (...skipping 163 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 212 mock_render_process_id_(render_process_host_->GetID()), | 236 mock_render_process_id_(render_process_host_->GetID()), |
| 213 new_mock_render_process_id_(new_render_process_host_->GetID()), | 237 new_mock_render_process_id_(new_render_process_host_->GetID()), |
| 214 weak_factory_(this) { | 238 weak_factory_(this) { |
| 215 std::unique_ptr<MockServiceWorkerDatabaseTaskManager> database_task_manager( | 239 std::unique_ptr<MockServiceWorkerDatabaseTaskManager> database_task_manager( |
| 216 new MockServiceWorkerDatabaseTaskManager( | 240 new MockServiceWorkerDatabaseTaskManager( |
| 217 base::ThreadTaskRunnerHandle::Get())); | 241 base::ThreadTaskRunnerHandle::Get())); |
| 218 wrapper_->InitInternal(user_data_directory, std::move(database_task_manager), | 242 wrapper_->InitInternal(user_data_directory, std::move(database_task_manager), |
| 219 base::ThreadTaskRunnerHandle::Get(), nullptr, nullptr); | 243 base::ThreadTaskRunnerHandle::Get(), nullptr, nullptr); |
| 220 wrapper_->process_manager()->SetProcessIdForTest(mock_render_process_id()); | 244 wrapper_->process_manager()->SetProcessIdForTest(mock_render_process_id()); |
| 221 wrapper_->process_manager()->SetNewProcessIdForTest(new_render_process_id()); | 245 wrapper_->process_manager()->SetNewProcessIdForTest(new_render_process_id()); |
| 222 registry()->AddChildProcessSender(mock_render_process_id_, this); | 246 |
| 247 scoped_refptr<ServiceWorkerDispatcherHost> dispatcher_host( |
| 248 new MockServiceWorkerDispatcherHost( |
| 249 mock_render_process_id_, browser_context_->GetResourceContext(), |
| 250 this)); |
| 251 wrapper_->context()->AddDispatcherHost(mock_render_process_id_, |
| 252 dispatcher_host.get()); |
| 253 dispatcher_hosts_[mock_render_process_id_] = std::move(dispatcher_host); |
| 223 | 254 |
| 224 // Setup process level interface registry. | 255 // Setup process level interface registry. |
| 225 render_process_interface_registry_ = | 256 render_process_interface_registry_ = |
| 226 CreateInterfaceRegistry(render_process_host_.get()); | 257 CreateInterfaceRegistry(render_process_host_.get()); |
| 227 new_render_process_interface_registry_ = | 258 new_render_process_interface_registry_ = |
| 228 CreateInterfaceRegistry(new_render_process_host_.get()); | 259 CreateInterfaceRegistry(new_render_process_host_.get()); |
| 229 } | 260 } |
| 230 | 261 |
| 231 EmbeddedWorkerTestHelper::~EmbeddedWorkerTestHelper() { | 262 EmbeddedWorkerTestHelper::~EmbeddedWorkerTestHelper() { |
| 232 if (wrapper_.get()) | 263 if (wrapper_.get()) |
| 233 wrapper_->Shutdown(); | 264 wrapper_->Shutdown(); |
| 234 } | 265 } |
| 235 | 266 |
| 236 void EmbeddedWorkerTestHelper::SimulateAddProcessToPattern(const GURL& pattern, | 267 void EmbeddedWorkerTestHelper::SimulateAddProcessToPattern(const GURL& pattern, |
| 237 int process_id) { | 268 int process_id) { |
| 238 registry()->AddChildProcessSender(process_id, this); | 269 if (!context()->GetDispatcherHost(process_id)) { |
| 270 scoped_refptr<ServiceWorkerDispatcherHost> dispatcher_host( |
| 271 new MockServiceWorkerDispatcherHost( |
| 272 process_id, browser_context_->GetResourceContext(), this)); |
| 273 wrapper_->context()->AddDispatcherHost(process_id, dispatcher_host.get()); |
| 274 dispatcher_hosts_[process_id] = std::move(dispatcher_host); |
| 275 } |
| 239 wrapper_->process_manager()->AddProcessReferenceToPattern(pattern, | 276 wrapper_->process_manager()->AddProcessReferenceToPattern(pattern, |
| 240 process_id); | 277 process_id); |
| 241 } | 278 } |
| 242 | 279 |
| 243 bool EmbeddedWorkerTestHelper::Send(IPC::Message* message) { | 280 bool EmbeddedWorkerTestHelper::Send(IPC::Message* message) { |
| 244 OnMessageReceived(*message); | 281 OnMessageReceived(*message); |
| 245 delete message; | 282 delete message; |
| 246 return true; | 283 return true; |
| 247 } | 284 } |
| 248 | 285 |
| (...skipping 375 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 624 service_manager::InterfaceProviderSpec()); | 661 service_manager::InterfaceProviderSpec()); |
| 625 | 662 |
| 626 std::unique_ptr<service_manager::InterfaceProvider> remote_interfaces( | 663 std::unique_ptr<service_manager::InterfaceProvider> remote_interfaces( |
| 627 new service_manager::InterfaceProvider); | 664 new service_manager::InterfaceProvider); |
| 628 remote_interfaces->Bind(std::move(interfaces)); | 665 remote_interfaces->Bind(std::move(interfaces)); |
| 629 rph->SetRemoteInterfaces(std::move(remote_interfaces)); | 666 rph->SetRemoteInterfaces(std::move(remote_interfaces)); |
| 630 return registry; | 667 return registry; |
| 631 } | 668 } |
| 632 | 669 |
| 633 } // namespace content | 670 } // namespace content |
| OLD | NEW |