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