OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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_instance.h" | 5 #include "content/browser/service_worker/embedded_worker_instance.h" |
6 | 6 |
7 #include <stdint.h> | 7 #include <stdint.h> |
8 #include <utility> | 8 #include <utility> |
9 #include <vector> | 9 #include <vector> |
10 | 10 |
(...skipping 135 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
146 DISALLOW_COPY_AND_ASSIGN(EmbeddedWorkerInstanceTest); | 146 DISALLOW_COPY_AND_ASSIGN(EmbeddedWorkerInstanceTest); |
147 }; | 147 }; |
148 | 148 |
149 // A helper to simulate the start worker sequence is stalled in a worker | 149 // A helper to simulate the start worker sequence is stalled in a worker |
150 // process. | 150 // process. |
151 class StalledInStartWorkerHelper : public EmbeddedWorkerTestHelper { | 151 class StalledInStartWorkerHelper : public EmbeddedWorkerTestHelper { |
152 public: | 152 public: |
153 StalledInStartWorkerHelper() : EmbeddedWorkerTestHelper(base::FilePath()) {} | 153 StalledInStartWorkerHelper() : EmbeddedWorkerTestHelper(base::FilePath()) {} |
154 ~StalledInStartWorkerHelper() override{}; | 154 ~StalledInStartWorkerHelper() override{}; |
155 | 155 |
156 void OnStartWorker( | 156 void OnStartWorker(int embedded_worker_id, |
157 int embedded_worker_id, | 157 int64_t service_worker_version_id, |
158 int64_t service_worker_version_id, | 158 const GURL& scope, |
159 const GURL& scope, | 159 const GURL& script_url, |
160 const GURL& script_url, | 160 bool pause_after_download, |
161 bool pause_after_download, | 161 mojom::ServiceWorkerEventDispatcherRequest request, |
162 mojom::ServiceWorkerEventDispatcherRequest request) override { | 162 mojom::EmbeddedWorkerInstanceHostAssociatedPtrInfo |
| 163 instance_host) override { |
163 if (force_stall_in_start_) { | 164 if (force_stall_in_start_) { |
| 165 // Prepare for OnStopWorker(). |
| 166 instance_host_ptr_map_[embedded_worker_id].Bind(std::move(instance_host)); |
164 // Do nothing to simulate a stall in the worker process. | 167 // Do nothing to simulate a stall in the worker process. |
165 return; | 168 return; |
166 } | 169 } |
167 EmbeddedWorkerTestHelper::OnStartWorker( | 170 EmbeddedWorkerTestHelper::OnStartWorker( |
168 embedded_worker_id, service_worker_version_id, scope, script_url, | 171 embedded_worker_id, service_worker_version_id, scope, script_url, |
169 pause_after_download, std::move(request)); | 172 pause_after_download, std::move(request), std::move(instance_host)); |
| 173 } |
| 174 |
| 175 void OnStopWorker(int embedded_worker_id) override { |
| 176 if (instance_host_ptr_map_[embedded_worker_id]) { |
| 177 instance_host_ptr_map_[embedded_worker_id]->OnStopped(); |
| 178 base::RunLoop().RunUntilIdle(); |
| 179 return; |
| 180 } |
| 181 EmbeddedWorkerTestHelper::OnStopWorker(embedded_worker_id); |
170 } | 182 } |
171 | 183 |
172 void set_force_stall_in_start(bool force_stall_in_start) { | 184 void set_force_stall_in_start(bool force_stall_in_start) { |
173 force_stall_in_start_ = force_stall_in_start; | 185 force_stall_in_start_ = force_stall_in_start; |
174 } | 186 } |
175 | 187 |
176 private: | 188 private: |
177 bool force_stall_in_start_ = true; | 189 bool force_stall_in_start_ = true; |
| 190 |
| 191 std::map< |
| 192 int /* embedded_worker_id */, |
| 193 mojom::EmbeddedWorkerInstanceHostAssociatedPtr /* instance_host_ptr */> |
| 194 instance_host_ptr_map_; |
178 }; | 195 }; |
179 | 196 |
180 TEST_F(EmbeddedWorkerInstanceTest, StartAndStop) { | 197 TEST_F(EmbeddedWorkerInstanceTest, StartAndStop) { |
181 std::unique_ptr<EmbeddedWorkerInstance> worker = | 198 std::unique_ptr<EmbeddedWorkerInstance> worker = |
182 embedded_worker_registry()->CreateWorker(); | 199 embedded_worker_registry()->CreateWorker(); |
183 EXPECT_EQ(EmbeddedWorkerStatus::STOPPED, worker->status()); | 200 EXPECT_EQ(EmbeddedWorkerStatus::STOPPED, worker->status()); |
184 worker->AddListener(this); | 201 worker->AddListener(this); |
185 | 202 |
186 const int64_t service_worker_version_id = 55L; | 203 const int64_t service_worker_version_id = 55L; |
187 const GURL pattern("http://example.com/"); | 204 const GURL pattern("http://example.com/"); |
(...skipping 513 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
701 class FailEmbeddedWorkerInstanceClientImpl | 718 class FailEmbeddedWorkerInstanceClientImpl |
702 : public EmbeddedWorkerTestHelper::MockEmbeddedWorkerInstanceClient { | 719 : public EmbeddedWorkerTestHelper::MockEmbeddedWorkerInstanceClient { |
703 public: | 720 public: |
704 explicit FailEmbeddedWorkerInstanceClientImpl( | 721 explicit FailEmbeddedWorkerInstanceClientImpl( |
705 base::WeakPtr<EmbeddedWorkerTestHelper> helper) | 722 base::WeakPtr<EmbeddedWorkerTestHelper> helper) |
706 : EmbeddedWorkerTestHelper::MockEmbeddedWorkerInstanceClient(helper) {} | 723 : EmbeddedWorkerTestHelper::MockEmbeddedWorkerInstanceClient(helper) {} |
707 | 724 |
708 private: | 725 private: |
709 void StartWorker( | 726 void StartWorker( |
710 const EmbeddedWorkerStartParams& /* unused */, | 727 const EmbeddedWorkerStartParams& /* unused */, |
711 mojom::ServiceWorkerEventDispatcherRequest /* unused */) override { | 728 mojom::ServiceWorkerEventDispatcherRequest /* unused */, |
| 729 mojom::EmbeddedWorkerInstanceHostAssociatedPtrInfo /* unused */) |
| 730 override { |
712 helper_->mock_instance_clients()->clear(); | 731 helper_->mock_instance_clients()->clear(); |
713 } | 732 } |
714 }; | 733 }; |
715 | 734 |
716 TEST_F(EmbeddedWorkerInstanceTest, RemoveRemoteInterface) { | 735 TEST_F(EmbeddedWorkerInstanceTest, RemoveRemoteInterface) { |
717 const int64_t version_id = 55L; | 736 const int64_t version_id = 55L; |
718 const GURL pattern("http://example.com/"); | 737 const GURL pattern("http://example.com/"); |
719 const GURL url("http://example.com/worker.js"); | 738 const GURL url("http://example.com/worker.js"); |
720 | 739 |
721 // Let StartWorker fail; binding is discarded in the middle of IPC | 740 // Let StartWorker fail; binding is discarded in the middle of IPC |
(...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
810 ASSERT_EQ(1UL, instance_client_rawptr->message().size()); | 829 ASSERT_EQ(1UL, instance_client_rawptr->message().size()); |
811 EXPECT_EQ(test_message, instance_client_rawptr->message()[0]); | 830 EXPECT_EQ(test_message, instance_client_rawptr->message()[0]); |
812 | 831 |
813 // Ensure the worker is stopped. | 832 // Ensure the worker is stopped. |
814 worker->Stop(); | 833 worker->Stop(); |
815 base::RunLoop().RunUntilIdle(); | 834 base::RunLoop().RunUntilIdle(); |
816 EXPECT_EQ(EmbeddedWorkerStatus::STOPPED, worker->status()); | 835 EXPECT_EQ(EmbeddedWorkerStatus::STOPPED, worker->status()); |
817 } | 836 } |
818 | 837 |
819 } // namespace content | 838 } // namespace content |
OLD | NEW |