| 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 |
| 11 #include "base/command_line.h" | |
| 12 #include "base/macros.h" | 11 #include "base/macros.h" |
| 13 #include "base/run_loop.h" | 12 #include "base/run_loop.h" |
| 14 #include "base/stl_util.h" | 13 #include "base/stl_util.h" |
| 15 #include "content/browser/service_worker/embedded_worker_registry.h" | 14 #include "content/browser/service_worker/embedded_worker_registry.h" |
| 16 #include "content/browser/service_worker/embedded_worker_status.h" | 15 #include "content/browser/service_worker/embedded_worker_status.h" |
| 17 #include "content/browser/service_worker/embedded_worker_test_helper.h" | 16 #include "content/browser/service_worker/embedded_worker_test_helper.h" |
| 18 #include "content/browser/service_worker/service_worker_context_core.h" | 17 #include "content/browser/service_worker/service_worker_context_core.h" |
| 19 #include "content/browser/service_worker/service_worker_context_wrapper.h" | 18 #include "content/browser/service_worker/service_worker_context_wrapper.h" |
| 20 #include "content/common/service_worker/embedded_worker.mojom.h" | |
| 21 #include "content/common/service_worker/embedded_worker_messages.h" | 19 #include "content/common/service_worker/embedded_worker_messages.h" |
| 22 #include "content/common/service_worker/embedded_worker_start_params.h" | |
| 23 #include "content/public/common/child_process_host.h" | 20 #include "content/public/common/child_process_host.h" |
| 24 #include "content/public/common/content_switches.h" | |
| 25 #include "content/public/test/test_browser_thread_bundle.h" | 21 #include "content/public/test/test_browser_thread_bundle.h" |
| 26 #include "mojo/public/cpp/bindings/strong_binding.h" | |
| 27 #include "testing/gmock/include/gmock/gmock.h" | 22 #include "testing/gmock/include/gmock/gmock.h" |
| 28 #include "testing/gtest/include/gtest/gtest.h" | 23 #include "testing/gtest/include/gtest/gtest.h" |
| 29 | 24 |
| 30 namespace content { | 25 namespace content { |
| 31 | 26 |
| 32 namespace { | 27 namespace { |
| 33 | 28 |
| 34 void SaveStatusAndCall(ServiceWorkerStatusCode* out, | 29 void SaveStatusAndCall(ServiceWorkerStatusCode* out, |
| 35 const base::Closure& callback, | 30 const base::Closure& callback, |
| 36 ServiceWorkerStatusCode status) { | 31 ServiceWorkerStatusCode status) { |
| 37 *out = status; | 32 *out = status; |
| 38 callback.Run(); | 33 callback.Run(); |
| 39 } | 34 } |
| 40 | 35 |
| 41 std::unique_ptr<EmbeddedWorkerStartParams> | 36 std::unique_ptr<EmbeddedWorkerMsg_StartWorker_Params> |
| 42 CreateStartParams(int version_id, const GURL& scope, const GURL& script_url) { | 37 CreateStartParams(int version_id, const GURL& scope, const GURL& script_url) { |
| 43 std::unique_ptr<EmbeddedWorkerStartParams> params( | 38 std::unique_ptr<EmbeddedWorkerMsg_StartWorker_Params> params( |
| 44 new EmbeddedWorkerStartParams); | 39 new EmbeddedWorkerMsg_StartWorker_Params); |
| 45 params->service_worker_version_id = version_id; | 40 params->service_worker_version_id = version_id; |
| 46 params->scope = scope; | 41 params->scope = scope; |
| 47 params->script_url = script_url; | 42 params->script_url = script_url; |
| 48 params->pause_after_download = false; | 43 params->pause_after_download = false; |
| 49 params->is_installed = false; | |
| 50 return params; | 44 return params; |
| 51 } | 45 } |
| 52 | 46 |
| 53 } // namespace | 47 } // namespace |
| 54 | 48 |
| 55 class EmbeddedWorkerInstanceTest : public testing::Test, | 49 class EmbeddedWorkerInstanceTest : public testing::Test, |
| 56 public EmbeddedWorkerInstance::Listener { | 50 public EmbeddedWorkerInstance::Listener { |
| 57 protected: | 51 protected: |
| 58 EmbeddedWorkerInstanceTest() | 52 EmbeddedWorkerInstanceTest() |
| 59 : thread_bundle_(TestBrowserThreadBundle::IO_MAINLOOP) {} | 53 : thread_bundle_(TestBrowserThreadBundle::IO_MAINLOOP) {} |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 96 helper_.reset(new EmbeddedWorkerTestHelper(base::FilePath())); | 90 helper_.reset(new EmbeddedWorkerTestHelper(base::FilePath())); |
| 97 } | 91 } |
| 98 | 92 |
| 99 void TearDown() override { helper_.reset(); } | 93 void TearDown() override { helper_.reset(); } |
| 100 | 94 |
| 101 ServiceWorkerStatusCode StartWorker(EmbeddedWorkerInstance* worker, | 95 ServiceWorkerStatusCode StartWorker(EmbeddedWorkerInstance* worker, |
| 102 int id, const GURL& pattern, | 96 int id, const GURL& pattern, |
| 103 const GURL& url) { | 97 const GURL& url) { |
| 104 ServiceWorkerStatusCode status; | 98 ServiceWorkerStatusCode status; |
| 105 base::RunLoop run_loop; | 99 base::RunLoop run_loop; |
| 106 std::unique_ptr<EmbeddedWorkerStartParams> params = | 100 std::unique_ptr<EmbeddedWorkerMsg_StartWorker_Params> params = |
| 107 CreateStartParams(id, pattern, url); | 101 CreateStartParams(id, pattern, url); |
| 108 worker->Start(std::move(params), base::Bind(&SaveStatusAndCall, &status, | 102 worker->Start(std::move(params), base::Bind(&SaveStatusAndCall, &status, |
| 109 run_loop.QuitClosure())); | 103 run_loop.QuitClosure())); |
| 110 run_loop.Run(); | 104 run_loop.Run(); |
| 111 return status; | 105 return status; |
| 112 } | 106 } |
| 113 | 107 |
| 114 ServiceWorkerContextCore* context() { return helper_->context(); } | 108 ServiceWorkerContextCore* context() { return helper_->context(); } |
| 115 | 109 |
| 116 EmbeddedWorkerRegistry* embedded_worker_registry() { | 110 EmbeddedWorkerRegistry* embedded_worker_registry() { |
| 117 DCHECK(context()); | 111 DCHECK(context()); |
| 118 return context()->embedded_worker_registry(); | 112 return context()->embedded_worker_registry(); |
| 119 } | 113 } |
| 120 | 114 |
| 121 IPC::TestSink* ipc_sink() { return helper_->ipc_sink(); } | 115 IPC::TestSink* ipc_sink() { return helper_->ipc_sink(); } |
| 122 | 116 |
| 123 TestBrowserThreadBundle thread_bundle_; | 117 TestBrowserThreadBundle thread_bundle_; |
| 124 std::unique_ptr<EmbeddedWorkerTestHelper> helper_; | 118 std::unique_ptr<EmbeddedWorkerTestHelper> helper_; |
| 125 std::vector<EventLog> events_; | 119 std::vector<EventLog> events_; |
| 126 | 120 |
| 127 private: | 121 private: |
| 128 DISALLOW_COPY_AND_ASSIGN(EmbeddedWorkerInstanceTest); | 122 DISALLOW_COPY_AND_ASSIGN(EmbeddedWorkerInstanceTest); |
| 129 }; | 123 }; |
| 130 | 124 |
| 131 class EmbeddedWorkerInstanceTestP : public EmbeddedWorkerInstanceTest, | |
| 132 public testing::WithParamInterface<bool> { | |
| 133 protected: | |
| 134 void SetUp() override { | |
| 135 is_mojo_enabled_ = GetParam(); | |
| 136 if (is_mojo_enabled()) { | |
| 137 base::CommandLine::ForCurrentProcess()->AppendSwitch( | |
| 138 switches::kMojoServiceWorker); | |
| 139 } | |
| 140 EmbeddedWorkerInstanceTest::SetUp(); | |
| 141 } | |
| 142 | |
| 143 bool is_mojo_enabled() { return is_mojo_enabled_; } | |
| 144 | |
| 145 private: | |
| 146 bool is_mojo_enabled_ = false; | |
| 147 }; | |
| 148 | |
| 149 // A helper to simulate the start worker sequence is stalled in a worker | 125 // A helper to simulate the start worker sequence is stalled in a worker |
| 150 // process. | 126 // process. |
| 151 class StalledInStartWorkerHelper : public EmbeddedWorkerTestHelper { | 127 class StalledInStartWorkerHelper : public EmbeddedWorkerTestHelper { |
| 152 public: | 128 public: |
| 153 StalledInStartWorkerHelper() : EmbeddedWorkerTestHelper(base::FilePath()) {} | 129 StalledInStartWorkerHelper() : EmbeddedWorkerTestHelper(base::FilePath()) {} |
| 154 ~StalledInStartWorkerHelper() override{}; | 130 ~StalledInStartWorkerHelper() override{}; |
| 155 | 131 |
| 156 void OnStartWorker(int embedded_worker_id, | 132 void OnStartWorker(int embedded_worker_id, |
| 157 int64_t service_worker_version_id, | 133 int64_t service_worker_version_id, |
| 158 const GURL& scope, | 134 const GURL& scope, |
| (...skipping 20 matching lines...) Expand all Loading... |
| 179 public: | 155 public: |
| 180 FailToSendIPCHelper() : EmbeddedWorkerTestHelper(base::FilePath()) {} | 156 FailToSendIPCHelper() : EmbeddedWorkerTestHelper(base::FilePath()) {} |
| 181 ~FailToSendIPCHelper() override {} | 157 ~FailToSendIPCHelper() override {} |
| 182 | 158 |
| 183 bool Send(IPC::Message* message) override { | 159 bool Send(IPC::Message* message) override { |
| 184 delete message; | 160 delete message; |
| 185 return false; | 161 return false; |
| 186 } | 162 } |
| 187 }; | 163 }; |
| 188 | 164 |
| 189 TEST_P(EmbeddedWorkerInstanceTestP, StartAndStop) { | 165 TEST_F(EmbeddedWorkerInstanceTest, StartAndStop) { |
| 190 std::unique_ptr<EmbeddedWorkerInstance> worker = | 166 std::unique_ptr<EmbeddedWorkerInstance> worker = |
| 191 embedded_worker_registry()->CreateWorker(); | 167 embedded_worker_registry()->CreateWorker(); |
| 192 EXPECT_EQ(EmbeddedWorkerStatus::STOPPED, worker->status()); | 168 EXPECT_EQ(EmbeddedWorkerStatus::STOPPED, worker->status()); |
| 193 worker->AddListener(this); | |
| 194 | 169 |
| 195 const int64_t service_worker_version_id = 55L; | 170 const int64_t service_worker_version_id = 55L; |
| 196 const GURL pattern("http://example.com/"); | 171 const GURL pattern("http://example.com/"); |
| 197 const GURL url("http://example.com/worker.js"); | 172 const GURL url("http://example.com/worker.js"); |
| 198 | 173 |
| 199 // Simulate adding one process to the pattern. | 174 // Simulate adding one process to the pattern. |
| 200 helper_->SimulateAddProcessToPattern(pattern, | 175 helper_->SimulateAddProcessToPattern(pattern, |
| 201 helper_->mock_render_process_id()); | 176 helper_->mock_render_process_id()); |
| 202 | 177 |
| 203 // Start should succeed. | 178 // Start should succeed. |
| 204 ServiceWorkerStatusCode status; | 179 ServiceWorkerStatusCode status; |
| 205 base::RunLoop run_loop; | 180 base::RunLoop run_loop; |
| 206 std::unique_ptr<EmbeddedWorkerStartParams> params = | 181 std::unique_ptr<EmbeddedWorkerMsg_StartWorker_Params> params = |
| 207 CreateStartParams(service_worker_version_id, pattern, url); | 182 CreateStartParams(service_worker_version_id, pattern, url); |
| 208 worker->Start(std::move(params), base::Bind(&SaveStatusAndCall, &status, | 183 worker->Start(std::move(params), base::Bind(&SaveStatusAndCall, &status, |
| 209 run_loop.QuitClosure())); | 184 run_loop.QuitClosure())); |
| 210 EXPECT_EQ(EmbeddedWorkerStatus::STARTING, worker->status()); | 185 EXPECT_EQ(EmbeddedWorkerStatus::STARTING, worker->status()); |
| 211 run_loop.Run(); | 186 run_loop.Run(); |
| 212 EXPECT_EQ(SERVICE_WORKER_OK, status); | 187 EXPECT_EQ(SERVICE_WORKER_OK, status); |
| 213 | 188 |
| 214 // The 'WorkerStarted' message should have been sent by | 189 // The 'WorkerStarted' message should have been sent by |
| 215 // EmbeddedWorkerTestHelper. | 190 // EmbeddedWorkerTestHelper. |
| 216 EXPECT_EQ(EmbeddedWorkerStatus::RUNNING, worker->status()); | 191 EXPECT_EQ(EmbeddedWorkerStatus::RUNNING, worker->status()); |
| 217 EXPECT_EQ(helper_->mock_render_process_id(), worker->process_id()); | 192 EXPECT_EQ(helper_->mock_render_process_id(), worker->process_id()); |
| 218 | 193 |
| 219 // Stop the worker. | 194 // Stop the worker. |
| 220 EXPECT_EQ(SERVICE_WORKER_OK, worker->Stop()); | 195 EXPECT_EQ(SERVICE_WORKER_OK, worker->Stop()); |
| 221 EXPECT_EQ(EmbeddedWorkerStatus::STOPPING, worker->status()); | 196 EXPECT_EQ(EmbeddedWorkerStatus::STOPPING, worker->status()); |
| 222 base::RunLoop().RunUntilIdle(); | 197 base::RunLoop().RunUntilIdle(); |
| 223 | 198 |
| 224 // The 'WorkerStopped' message should have been sent by | 199 // The 'WorkerStopped' message should have been sent by |
| 225 // EmbeddedWorkerTestHelper. | 200 // EmbeddedWorkerTestHelper. |
| 226 EXPECT_EQ(EmbeddedWorkerStatus::STOPPED, worker->status()); | 201 EXPECT_EQ(EmbeddedWorkerStatus::STOPPED, worker->status()); |
| 227 | 202 |
| 228 if (!is_mojo_enabled()) { | 203 // Verify that we've sent two messages to start and terminate the worker. |
| 229 // Verify that we've sent two messages to start and terminate the worker. | 204 ASSERT_TRUE( |
| 230 ASSERT_TRUE(ipc_sink()->GetUniqueMessageMatching( | 205 ipc_sink()->GetUniqueMessageMatching(EmbeddedWorkerMsg_StartWorker::ID)); |
| 231 EmbeddedWorkerMsg_StartWorker::ID)); | |
| 232 } | |
| 233 // StopWorker should be sent in either case. | |
| 234 ASSERT_TRUE(ipc_sink()->GetUniqueMessageMatching( | 206 ASSERT_TRUE(ipc_sink()->GetUniqueMessageMatching( |
| 235 EmbeddedWorkerMsg_StopWorker::ID)); | 207 EmbeddedWorkerMsg_StopWorker::ID)); |
| 236 | |
| 237 // Check if the IPCs are fired in expected order. | |
| 238 ASSERT_EQ(4u, events_.size()); | |
| 239 EXPECT_EQ(PROCESS_ALLOCATED, events_[0].type); | |
| 240 EXPECT_EQ(START_WORKER_MESSAGE_SENT, events_[1].type); | |
| 241 EXPECT_EQ(STARTED, events_[2].type); | |
| 242 EXPECT_EQ(STOPPED, events_[3].type); | |
| 243 } | 208 } |
| 244 | 209 |
| 245 // Test that a worker that failed twice will use a new render process | 210 // Test that a worker that failed twice will use a new render process |
| 246 // on the next attempt. | 211 // on the next attempt. |
| 247 TEST_F(EmbeddedWorkerInstanceTest, ForceNewProcess) { | 212 TEST_F(EmbeddedWorkerInstanceTest, ForceNewProcess) { |
| 248 std::unique_ptr<EmbeddedWorkerInstance> worker = | 213 std::unique_ptr<EmbeddedWorkerInstance> worker = |
| 249 embedded_worker_registry()->CreateWorker(); | 214 embedded_worker_registry()->CreateWorker(); |
| 250 EXPECT_EQ(EmbeddedWorkerStatus::STOPPED, worker->status()); | 215 EXPECT_EQ(EmbeddedWorkerStatus::STOPPED, worker->status()); |
| 251 | 216 |
| 252 const int64_t service_worker_version_id = 55L; | 217 const int64_t service_worker_version_id = 55L; |
| 253 const GURL pattern("http://example.com/"); | 218 const GURL pattern("http://example.com/"); |
| 254 const GURL url("http://example.com/worker.js"); | 219 const GURL url("http://example.com/worker.js"); |
| 255 | 220 |
| 256 // Simulate adding one process to the pattern. | 221 // Simulate adding one process to the pattern. |
| 257 helper_->SimulateAddProcessToPattern(pattern, | 222 helper_->SimulateAddProcessToPattern(pattern, |
| 258 helper_->mock_render_process_id()); | 223 helper_->mock_render_process_id()); |
| 259 | 224 |
| 260 // Also simulate adding a "newly created" process to the pattern because | 225 // Also simulate adding a "newly created" process to the pattern because |
| 261 // unittests can't actually create a new process itself. | 226 // unittests can't actually create a new process itself. |
| 262 // ServiceWorkerProcessManager only chooses this process id in unittests if | 227 // ServiceWorkerProcessManager only chooses this process id in unittests if |
| 263 // can_use_existing_process is false. | 228 // can_use_existing_process is false. |
| 264 helper_->SimulateAddProcessToPattern(pattern, | 229 helper_->SimulateAddProcessToPattern(pattern, |
| 265 helper_->new_render_process_id()); | 230 helper_->new_render_process_id()); |
| 266 | 231 |
| 267 { | 232 { |
| 268 // Start once normally. | 233 // Start once normally. |
| 269 ServiceWorkerStatusCode status = SERVICE_WORKER_ERROR_MAX_VALUE; | 234 ServiceWorkerStatusCode status = SERVICE_WORKER_ERROR_MAX_VALUE; |
| 270 base::RunLoop run_loop; | 235 base::RunLoop run_loop; |
| 271 std::unique_ptr<EmbeddedWorkerStartParams> params( | 236 std::unique_ptr<EmbeddedWorkerMsg_StartWorker_Params> params( |
| 272 CreateStartParams(service_worker_version_id, pattern, url)); | 237 CreateStartParams(service_worker_version_id, pattern, url)); |
| 273 worker->Start(std::move(params), base::Bind(&SaveStatusAndCall, &status, | 238 worker->Start(std::move(params), base::Bind(&SaveStatusAndCall, &status, |
| 274 run_loop.QuitClosure())); | 239 run_loop.QuitClosure())); |
| 275 run_loop.Run(); | 240 run_loop.Run(); |
| 276 EXPECT_EQ(SERVICE_WORKER_OK, status); | 241 EXPECT_EQ(SERVICE_WORKER_OK, status); |
| 277 EXPECT_EQ(EmbeddedWorkerStatus::RUNNING, worker->status()); | 242 EXPECT_EQ(EmbeddedWorkerStatus::RUNNING, worker->status()); |
| 278 // The worker should be using the default render process. | 243 // The worker should be using the default render process. |
| 279 EXPECT_EQ(helper_->mock_render_process_id(), worker->process_id()); | 244 EXPECT_EQ(helper_->mock_render_process_id(), worker->process_id()); |
| 280 | 245 |
| 281 EXPECT_EQ(SERVICE_WORKER_OK, worker->Stop()); | 246 EXPECT_EQ(SERVICE_WORKER_OK, worker->Stop()); |
| 282 base::RunLoop().RunUntilIdle(); | 247 base::RunLoop().RunUntilIdle(); |
| 283 } | 248 } |
| 284 | 249 |
| 285 // Fail twice. | 250 // Fail twice. |
| 286 context()->UpdateVersionFailureCount(service_worker_version_id, | 251 context()->UpdateVersionFailureCount(service_worker_version_id, |
| 287 SERVICE_WORKER_ERROR_FAILED); | 252 SERVICE_WORKER_ERROR_FAILED); |
| 288 context()->UpdateVersionFailureCount(service_worker_version_id, | 253 context()->UpdateVersionFailureCount(service_worker_version_id, |
| 289 SERVICE_WORKER_ERROR_FAILED); | 254 SERVICE_WORKER_ERROR_FAILED); |
| 290 | 255 |
| 291 { | 256 { |
| 292 // Start again. | 257 // Start again. |
| 293 ServiceWorkerStatusCode status; | 258 ServiceWorkerStatusCode status; |
| 294 base::RunLoop run_loop; | 259 base::RunLoop run_loop; |
| 295 std::unique_ptr<EmbeddedWorkerStartParams> params( | 260 std::unique_ptr<EmbeddedWorkerMsg_StartWorker_Params> params( |
| 296 CreateStartParams(service_worker_version_id, pattern, url)); | 261 CreateStartParams(service_worker_version_id, pattern, url)); |
| 297 worker->Start(std::move(params), base::Bind(&SaveStatusAndCall, &status, | 262 worker->Start(std::move(params), base::Bind(&SaveStatusAndCall, &status, |
| 298 run_loop.QuitClosure())); | 263 run_loop.QuitClosure())); |
| 299 EXPECT_EQ(EmbeddedWorkerStatus::STARTING, worker->status()); | 264 EXPECT_EQ(EmbeddedWorkerStatus::STARTING, worker->status()); |
| 300 run_loop.Run(); | 265 run_loop.Run(); |
| 301 EXPECT_EQ(SERVICE_WORKER_OK, status); | 266 EXPECT_EQ(SERVICE_WORKER_OK, status); |
| 302 | 267 |
| 303 EXPECT_EQ(EmbeddedWorkerStatus::RUNNING, worker->status()); | 268 EXPECT_EQ(EmbeddedWorkerStatus::RUNNING, worker->status()); |
| 304 // The worker should be using the new render process. | 269 // The worker should be using the new render process. |
| 305 EXPECT_EQ(helper_->new_render_process_id(), worker->process_id()); | 270 EXPECT_EQ(helper_->new_render_process_id(), worker->process_id()); |
| (...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 365 const int64_t version_id2 = 56L; | 330 const int64_t version_id2 = 56L; |
| 366 const GURL pattern("http://example.com/"); | 331 const GURL pattern("http://example.com/"); |
| 367 const GURL url("http://example.com/worker.js"); | 332 const GURL url("http://example.com/worker.js"); |
| 368 int process_id = helper_->mock_render_process_id(); | 333 int process_id = helper_->mock_render_process_id(); |
| 369 | 334 |
| 370 helper_->SimulateAddProcessToPattern(pattern, process_id); | 335 helper_->SimulateAddProcessToPattern(pattern, process_id); |
| 371 { | 336 { |
| 372 // Start worker1. | 337 // Start worker1. |
| 373 ServiceWorkerStatusCode status; | 338 ServiceWorkerStatusCode status; |
| 374 base::RunLoop run_loop; | 339 base::RunLoop run_loop; |
| 375 std::unique_ptr<EmbeddedWorkerStartParams> params( | 340 std::unique_ptr<EmbeddedWorkerMsg_StartWorker_Params> params( |
| 376 CreateStartParams(version_id1, pattern, url)); | 341 CreateStartParams(version_id1, pattern, url)); |
| 377 worker1->Start(std::move(params), base::Bind(&SaveStatusAndCall, &status, | 342 worker1->Start(std::move(params), base::Bind(&SaveStatusAndCall, &status, |
| 378 run_loop.QuitClosure())); | 343 run_loop.QuitClosure())); |
| 379 run_loop.Run(); | 344 run_loop.Run(); |
| 380 EXPECT_EQ(SERVICE_WORKER_OK, status); | 345 EXPECT_EQ(SERVICE_WORKER_OK, status); |
| 381 } | 346 } |
| 382 | 347 |
| 383 { | 348 { |
| 384 // Start worker2. | 349 // Start worker2. |
| 385 ServiceWorkerStatusCode status; | 350 ServiceWorkerStatusCode status; |
| 386 base::RunLoop run_loop; | 351 base::RunLoop run_loop; |
| 387 std::unique_ptr<EmbeddedWorkerStartParams> params( | 352 std::unique_ptr<EmbeddedWorkerMsg_StartWorker_Params> params( |
| 388 CreateStartParams(version_id2, pattern, url)); | 353 CreateStartParams(version_id2, pattern, url)); |
| 389 worker2->Start(std::move(params), base::Bind(&SaveStatusAndCall, &status, | 354 worker2->Start(std::move(params), base::Bind(&SaveStatusAndCall, &status, |
| 390 run_loop.QuitClosure())); | 355 run_loop.QuitClosure())); |
| 391 run_loop.Run(); | 356 run_loop.Run(); |
| 392 EXPECT_EQ(SERVICE_WORKER_OK, status); | 357 EXPECT_EQ(SERVICE_WORKER_OK, status); |
| 393 } | 358 } |
| 394 | 359 |
| 395 // The two workers share the same process. | 360 // The two workers share the same process. |
| 396 EXPECT_EQ(worker1->process_id(), worker2->process_id()); | 361 EXPECT_EQ(worker1->process_id(), worker2->process_id()); |
| 397 | 362 |
| (...skipping 16 matching lines...) Expand all Loading... |
| 414 const int64_t version_id = 55L; | 379 const int64_t version_id = 55L; |
| 415 const GURL scope("http://example.com/"); | 380 const GURL scope("http://example.com/"); |
| 416 const GURL url("http://example.com/worker.js"); | 381 const GURL url("http://example.com/worker.js"); |
| 417 | 382 |
| 418 std::unique_ptr<EmbeddedWorkerInstance> worker = | 383 std::unique_ptr<EmbeddedWorkerInstance> worker = |
| 419 embedded_worker_registry()->CreateWorker(); | 384 embedded_worker_registry()->CreateWorker(); |
| 420 worker->AddListener(this); | 385 worker->AddListener(this); |
| 421 | 386 |
| 422 // Run the start worker sequence and detach during process allocation. | 387 // Run the start worker sequence and detach during process allocation. |
| 423 ServiceWorkerStatusCode status = SERVICE_WORKER_ERROR_MAX_VALUE; | 388 ServiceWorkerStatusCode status = SERVICE_WORKER_ERROR_MAX_VALUE; |
| 424 std::unique_ptr<EmbeddedWorkerStartParams> params( | 389 std::unique_ptr<EmbeddedWorkerMsg_StartWorker_Params> params( |
| 425 CreateStartParams(version_id, scope, url)); | 390 CreateStartParams(version_id, scope, url)); |
| 426 worker->Start(std::move(params), base::Bind(&SaveStatusAndCall, &status, | 391 worker->Start(std::move(params), base::Bind(&SaveStatusAndCall, &status, |
| 427 base::Bind(&base::DoNothing))); | 392 base::Bind(&base::DoNothing))); |
| 428 worker->Detach(); | 393 worker->Detach(); |
| 429 base::RunLoop().RunUntilIdle(); | 394 base::RunLoop().RunUntilIdle(); |
| 430 | 395 |
| 431 EXPECT_EQ(EmbeddedWorkerStatus::STOPPED, worker->status()); | 396 EXPECT_EQ(EmbeddedWorkerStatus::STOPPED, worker->status()); |
| 432 EXPECT_EQ(ChildProcessHost::kInvalidUniqueID, worker->process_id()); | 397 EXPECT_EQ(ChildProcessHost::kInvalidUniqueID, worker->process_id()); |
| 433 | 398 |
| 434 // The start callback should not be aborted by detach (see a comment on the | 399 // The start callback should not be aborted by detach (see a comment on the |
| (...skipping 11 matching lines...) Expand all Loading... |
| 446 const GURL scope("http://example.com/"); | 411 const GURL scope("http://example.com/"); |
| 447 const GURL url("http://example.com/worker.js"); | 412 const GURL url("http://example.com/worker.js"); |
| 448 | 413 |
| 449 helper_.reset(new StalledInStartWorkerHelper()); | 414 helper_.reset(new StalledInStartWorkerHelper()); |
| 450 std::unique_ptr<EmbeddedWorkerInstance> worker = | 415 std::unique_ptr<EmbeddedWorkerInstance> worker = |
| 451 embedded_worker_registry()->CreateWorker(); | 416 embedded_worker_registry()->CreateWorker(); |
| 452 worker->AddListener(this); | 417 worker->AddListener(this); |
| 453 | 418 |
| 454 // Run the start worker sequence until a start worker message is sent. | 419 // Run the start worker sequence until a start worker message is sent. |
| 455 ServiceWorkerStatusCode status = SERVICE_WORKER_ERROR_MAX_VALUE; | 420 ServiceWorkerStatusCode status = SERVICE_WORKER_ERROR_MAX_VALUE; |
| 456 std::unique_ptr<EmbeddedWorkerStartParams> params( | 421 std::unique_ptr<EmbeddedWorkerMsg_StartWorker_Params> params( |
| 457 CreateStartParams(version_id, scope, url)); | 422 CreateStartParams(version_id, scope, url)); |
| 458 worker->Start(std::move(params), base::Bind(&SaveStatusAndCall, &status, | 423 worker->Start(std::move(params), base::Bind(&SaveStatusAndCall, &status, |
| 459 base::Bind(&base::DoNothing))); | 424 base::Bind(&base::DoNothing))); |
| 460 base::RunLoop().RunUntilIdle(); | 425 base::RunLoop().RunUntilIdle(); |
| 461 | 426 |
| 462 ASSERT_EQ(2u, events_.size()); | 427 ASSERT_EQ(2u, events_.size()); |
| 463 EXPECT_EQ(PROCESS_ALLOCATED, events_[0].type); | 428 EXPECT_EQ(PROCESS_ALLOCATED, events_[0].type); |
| 464 EXPECT_EQ(START_WORKER_MESSAGE_SENT, events_[1].type); | 429 EXPECT_EQ(START_WORKER_MESSAGE_SENT, events_[1].type); |
| 465 events_.clear(); | 430 events_.clear(); |
| 466 | 431 |
| (...skipping 18 matching lines...) Expand all Loading... |
| 485 const GURL scope("http://example.com/"); | 450 const GURL scope("http://example.com/"); |
| 486 const GURL url("http://example.com/worker.js"); | 451 const GURL url("http://example.com/worker.js"); |
| 487 | 452 |
| 488 std::unique_ptr<EmbeddedWorkerInstance> worker = | 453 std::unique_ptr<EmbeddedWorkerInstance> worker = |
| 489 embedded_worker_registry()->CreateWorker(); | 454 embedded_worker_registry()->CreateWorker(); |
| 490 worker->AddListener(this); | 455 worker->AddListener(this); |
| 491 | 456 |
| 492 // Stop the start worker sequence before a process is allocated. | 457 // Stop the start worker sequence before a process is allocated. |
| 493 ServiceWorkerStatusCode status = SERVICE_WORKER_ERROR_MAX_VALUE; | 458 ServiceWorkerStatusCode status = SERVICE_WORKER_ERROR_MAX_VALUE; |
| 494 | 459 |
| 495 std::unique_ptr<EmbeddedWorkerStartParams> params( | 460 std::unique_ptr<EmbeddedWorkerMsg_StartWorker_Params> params( |
| 496 CreateStartParams(version_id, scope, url)); | 461 CreateStartParams(version_id, scope, url)); |
| 497 worker->Start(std::move(params), base::Bind(&SaveStatusAndCall, &status, | 462 worker->Start(std::move(params), base::Bind(&SaveStatusAndCall, &status, |
| 498 base::Bind(&base::DoNothing))); | 463 base::Bind(&base::DoNothing))); |
| 499 worker->Stop(); | 464 worker->Stop(); |
| 500 base::RunLoop().RunUntilIdle(); | 465 base::RunLoop().RunUntilIdle(); |
| 501 | 466 |
| 502 EXPECT_EQ(EmbeddedWorkerStatus::STOPPED, worker->status()); | 467 EXPECT_EQ(EmbeddedWorkerStatus::STOPPED, worker->status()); |
| 503 EXPECT_EQ(ChildProcessHost::kInvalidUniqueID, worker->process_id()); | 468 EXPECT_EQ(ChildProcessHost::kInvalidUniqueID, worker->process_id()); |
| 504 | 469 |
| 505 // The start callback should not be aborted by stop (see a comment on the dtor | 470 // The start callback should not be aborted by stop (see a comment on the dtor |
| (...skipping 29 matching lines...) Expand all Loading... |
| 535 const GURL scope("http://example.com/"); | 500 const GURL scope("http://example.com/"); |
| 536 const GURL url("http://example.com/worker.js"); | 501 const GURL url("http://example.com/worker.js"); |
| 537 | 502 |
| 538 std::unique_ptr<EmbeddedWorkerInstance> worker = | 503 std::unique_ptr<EmbeddedWorkerInstance> worker = |
| 539 embedded_worker_registry()->CreateWorker(); | 504 embedded_worker_registry()->CreateWorker(); |
| 540 worker->AddListener(this); | 505 worker->AddListener(this); |
| 541 | 506 |
| 542 // Run the start worker sequence until pause after download. | 507 // Run the start worker sequence until pause after download. |
| 543 ServiceWorkerStatusCode status = SERVICE_WORKER_ERROR_MAX_VALUE; | 508 ServiceWorkerStatusCode status = SERVICE_WORKER_ERROR_MAX_VALUE; |
| 544 | 509 |
| 545 std::unique_ptr<EmbeddedWorkerStartParams> params( | 510 std::unique_ptr<EmbeddedWorkerMsg_StartWorker_Params> params( |
| 546 CreateStartParams(version_id, scope, url)); | 511 CreateStartParams(version_id, scope, url)); |
| 547 params->pause_after_download = true; | 512 params->pause_after_download = true; |
| 548 worker->Start(std::move(params), base::Bind(&SaveStatusAndCall, &status, | 513 worker->Start(std::move(params), base::Bind(&SaveStatusAndCall, &status, |
| 549 base::Bind(&base::DoNothing))); | 514 base::Bind(&base::DoNothing))); |
| 550 base::RunLoop().RunUntilIdle(); | 515 base::RunLoop().RunUntilIdle(); |
| 551 | 516 |
| 552 // Make the worker stopping and attempt to send a resume after download | 517 // Make the worker stopping and attempt to send a resume after download |
| 553 // message. | 518 // message. |
| 554 worker->Stop(); | 519 worker->Stop(); |
| 555 worker->ResumeAfterDownload(); | 520 worker->ResumeAfterDownload(); |
| (...skipping 10 matching lines...) Expand all Loading... |
| 566 const GURL scope("http://example.com/"); | 531 const GURL scope("http://example.com/"); |
| 567 const GURL url("http://example.com/worker.js"); | 532 const GURL url("http://example.com/worker.js"); |
| 568 | 533 |
| 569 helper_.reset(new StalledInStartWorkerHelper); | 534 helper_.reset(new StalledInStartWorkerHelper); |
| 570 std::unique_ptr<EmbeddedWorkerInstance> worker = | 535 std::unique_ptr<EmbeddedWorkerInstance> worker = |
| 571 embedded_worker_registry()->CreateWorker(); | 536 embedded_worker_registry()->CreateWorker(); |
| 572 worker->AddListener(this); | 537 worker->AddListener(this); |
| 573 | 538 |
| 574 // Run the start worker sequence until a start worker message is sent. | 539 // Run the start worker sequence until a start worker message is sent. |
| 575 ServiceWorkerStatusCode status = SERVICE_WORKER_ERROR_MAX_VALUE; | 540 ServiceWorkerStatusCode status = SERVICE_WORKER_ERROR_MAX_VALUE; |
| 576 std::unique_ptr<EmbeddedWorkerStartParams> params( | 541 std::unique_ptr<EmbeddedWorkerMsg_StartWorker_Params> params( |
| 577 CreateStartParams(version_id, scope, url)); | 542 CreateStartParams(version_id, scope, url)); |
| 578 worker->Start(std::move(params), base::Bind(&SaveStatusAndCall, &status, | 543 worker->Start(std::move(params), base::Bind(&SaveStatusAndCall, &status, |
| 579 base::Bind(&base::DoNothing))); | 544 base::Bind(&base::DoNothing))); |
| 580 base::RunLoop().RunUntilIdle(); | 545 base::RunLoop().RunUntilIdle(); |
| 581 | 546 |
| 582 ASSERT_EQ(2u, events_.size()); | 547 ASSERT_EQ(2u, events_.size()); |
| 583 EXPECT_EQ(PROCESS_ALLOCATED, events_[0].type); | 548 EXPECT_EQ(PROCESS_ALLOCATED, events_[0].type); |
| 584 EXPECT_EQ(START_WORKER_MESSAGE_SENT, events_[1].type); | 549 EXPECT_EQ(START_WORKER_MESSAGE_SENT, events_[1].type); |
| 585 events_.clear(); | 550 events_.clear(); |
| 586 | 551 |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 628 const GURL url("http://example.com/worker.js"); | 593 const GURL url("http://example.com/worker.js"); |
| 629 std::unique_ptr<EmbeddedWorkerInstance> worker = | 594 std::unique_ptr<EmbeddedWorkerInstance> worker = |
| 630 embedded_worker_registry()->CreateWorker(); | 595 embedded_worker_registry()->CreateWorker(); |
| 631 helper_->SimulateAddProcessToPattern(pattern, | 596 helper_->SimulateAddProcessToPattern(pattern, |
| 632 helper_->mock_render_process_id()); | 597 helper_->mock_render_process_id()); |
| 633 ServiceWorkerStatusCode status = SERVICE_WORKER_ERROR_FAILED; | 598 ServiceWorkerStatusCode status = SERVICE_WORKER_ERROR_FAILED; |
| 634 worker->AddListener(this); | 599 worker->AddListener(this); |
| 635 | 600 |
| 636 // Start the worker. | 601 // Start the worker. |
| 637 base::RunLoop run_loop; | 602 base::RunLoop run_loop; |
| 638 std::unique_ptr<EmbeddedWorkerStartParams> params( | 603 std::unique_ptr<EmbeddedWorkerMsg_StartWorker_Params> params( |
| 639 CreateStartParams(version_id, pattern, url)); | 604 CreateStartParams(version_id, pattern, url)); |
| 640 worker->Start(std::move(params), base::Bind(&SaveStatusAndCall, &status, | 605 worker->Start(std::move(params), base::Bind(&SaveStatusAndCall, &status, |
| 641 run_loop.QuitClosure())); | 606 run_loop.QuitClosure())); |
| 642 run_loop.Run(); | 607 run_loop.Run(); |
| 643 | 608 |
| 644 // Detach. | 609 // Detach. |
| 645 int process_id = worker->process_id(); | 610 int process_id = worker->process_id(); |
| 646 worker->Detach(); | 611 worker->Detach(); |
| 647 EXPECT_EQ(EmbeddedWorkerStatus::STOPPED, worker->status()); | 612 EXPECT_EQ(EmbeddedWorkerStatus::STOPPED, worker->status()); |
| 648 | 613 |
| (...skipping 14 matching lines...) Expand all Loading... |
| 663 | 628 |
| 664 std::unique_ptr<EmbeddedWorkerInstance> worker = | 629 std::unique_ptr<EmbeddedWorkerInstance> worker = |
| 665 embedded_worker_registry()->CreateWorker(); | 630 embedded_worker_registry()->CreateWorker(); |
| 666 helper_->SimulateAddProcessToPattern(pattern, | 631 helper_->SimulateAddProcessToPattern(pattern, |
| 667 helper_->mock_render_process_id()); | 632 helper_->mock_render_process_id()); |
| 668 ServiceWorkerStatusCode status = SERVICE_WORKER_ERROR_FAILED; | 633 ServiceWorkerStatusCode status = SERVICE_WORKER_ERROR_FAILED; |
| 669 worker->AddListener(this); | 634 worker->AddListener(this); |
| 670 | 635 |
| 671 // Attempt to start the worker. | 636 // Attempt to start the worker. |
| 672 base::RunLoop run_loop; | 637 base::RunLoop run_loop; |
| 673 std::unique_ptr<EmbeddedWorkerStartParams> params( | 638 std::unique_ptr<EmbeddedWorkerMsg_StartWorker_Params> params( |
| 674 CreateStartParams(version_id, pattern, url)); | 639 CreateStartParams(version_id, pattern, url)); |
| 675 worker->Start(std::move(params), base::Bind(&SaveStatusAndCall, &status, | 640 worker->Start(std::move(params), base::Bind(&SaveStatusAndCall, &status, |
| 676 run_loop.QuitClosure())); | 641 run_loop.QuitClosure())); |
| 677 run_loop.Run(); | 642 run_loop.Run(); |
| 678 | 643 |
| 679 // The callback should have run, and we should have got an OnStopped message. | 644 // The callback should have run, and we should have got an OnStopped message. |
| 680 EXPECT_EQ(SERVICE_WORKER_ERROR_IPC_FAILED, status); | 645 EXPECT_EQ(SERVICE_WORKER_ERROR_IPC_FAILED, status); |
| 681 ASSERT_EQ(2u, events_.size()); | 646 ASSERT_EQ(2u, events_.size()); |
| 682 EXPECT_EQ(PROCESS_ALLOCATED, events_[0].type); | 647 EXPECT_EQ(PROCESS_ALLOCATED, events_[0].type); |
| 683 EXPECT_EQ(STOPPED, events_[1].type); | 648 EXPECT_EQ(STOPPED, events_[1].type); |
| 684 EXPECT_EQ(EmbeddedWorkerStatus::STARTING, events_[1].status); | 649 EXPECT_EQ(EmbeddedWorkerStatus::STARTING, events_[1].status); |
| 685 } | 650 } |
| 686 | 651 |
| 687 INSTANTIATE_TEST_CASE_P(EmbeddedWorkerInstanceTest, | |
| 688 EmbeddedWorkerInstanceTestP, | |
| 689 ::testing::Values(false, true)); | |
| 690 | |
| 691 } // namespace content | 652 } // namespace content |
| OLD | NEW |