Chromium Code Reviews| 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 "base/bind.h" | 5 #include "base/bind.h" |
| 6 #include "base/callback.h" | 6 #include "base/callback.h" |
| 7 #include "base/command_line.h" | 7 #include "base/command_line.h" |
| 8 #include "base/run_loop.h" | 8 #include "base/run_loop.h" |
| 9 #include "content/browser/service_worker/embedded_worker_instance.h" | 9 #include "content/browser/service_worker/embedded_worker_instance.h" |
| 10 #include "content/browser/service_worker/embedded_worker_registry.h" | 10 #include "content/browser/service_worker/embedded_worker_registry.h" |
| 11 #include "content/browser/service_worker/service_worker_context_core.h" | 11 #include "content/browser/service_worker/service_worker_context_core.h" |
| 12 #include "content/browser/service_worker/service_worker_context_wrapper.h" | 12 #include "content/browser/service_worker/service_worker_context_wrapper.h" |
| 13 #include "content/browser/service_worker/service_worker_registration.h" | 13 #include "content/browser/service_worker/service_worker_registration.h" |
| 14 #include "content/browser/service_worker/service_worker_test_utils.h" | 14 #include "content/browser/service_worker/service_worker_test_utils.h" |
| 15 #include "content/browser/service_worker/service_worker_version.h" | 15 #include "content/browser/service_worker/service_worker_version.h" |
| 16 #include "content/common/service_worker/service_worker_messages.h" | 16 #include "content/common/service_worker/service_worker_messages.h" |
| 17 #include "content/common/service_worker/service_worker_status_code.h" | |
| 18 #include "content/common/service_worker/service_worker_types.h" | |
| 17 #include "content/public/browser/browser_context.h" | 19 #include "content/public/browser/browser_context.h" |
| 18 #include "content/public/browser/browser_thread.h" | 20 #include "content/public/browser/browser_thread.h" |
| 19 #include "content/public/browser/render_process_host.h" | 21 #include "content/public/browser/render_process_host.h" |
| 20 #include "content/public/browser/storage_partition.h" | 22 #include "content/public/browser/storage_partition.h" |
| 21 #include "content/public/browser/web_contents.h" | 23 #include "content/public/browser/web_contents.h" |
| 22 #include "content/public/common/content_switches.h" | 24 #include "content/public/common/content_switches.h" |
| 23 #include "content/shell/browser/shell.h" | 25 #include "content/shell/browser/shell.h" |
| 24 #include "content/test/content_browser_test.h" | 26 #include "content/test/content_browser_test.h" |
| 25 #include "content/test/content_browser_test_utils.h" | 27 #include "content/test/content_browser_test_utils.h" |
| 26 #include "net/test/embedded_test_server/embedded_test_server.h" | 28 #include "net/test/embedded_test_server/embedded_test_server.h" |
| (...skipping 11 matching lines...) Expand all Loading... | |
| 38 | 40 |
| 39 void RunOnIOThread(const base::Closure& closure) { | 41 void RunOnIOThread(const base::Closure& closure) { |
| 40 base::RunLoop run_loop; | 42 base::RunLoop run_loop; |
| 41 BrowserThread::PostTask( | 43 BrowserThread::PostTask( |
| 42 BrowserThread::IO, FROM_HERE, | 44 BrowserThread::IO, FROM_HERE, |
| 43 base::Bind(&RunAndQuit, closure, run_loop.QuitClosure(), | 45 base::Bind(&RunAndQuit, closure, run_loop.QuitClosure(), |
| 44 base::MessageLoopProxy::current())); | 46 base::MessageLoopProxy::current())); |
| 45 run_loop.Run(); | 47 run_loop.Run(); |
| 46 } | 48 } |
| 47 | 49 |
| 50 struct FetchResult { | |
|
Charlie Reis
2014/03/05 21:00:34
Style: structs should go above functions.
falken
2014/03/06 15:58:54
Done.
| |
| 51 ServiceWorkerStatusCode status; | |
| 52 ServiceWorkerFetchEventResult result; | |
| 53 ServiceWorkerResponse response; | |
| 54 }; | |
| 55 | |
| 48 void ReceiveFetchResult(BrowserThread::ID run_quit_thread, | 56 void ReceiveFetchResult(BrowserThread::ID run_quit_thread, |
| 49 const base::Closure& quit, | 57 const base::Closure& quit, |
| 50 ServiceWorkerStatusCode* out_status, | 58 FetchResult* out_result, |
|
Charlie Reis
2014/03/05 21:00:34
Style: output parameters should come after input p
falken
2014/03/06 15:58:54
I think I need this ordering so Bind can be used i
Charlie Reis
2014/03/06 18:59:05
Ah, ok. Maybe call that out in a comment?
falken
2014/03/07 01:56:51
Done.
| |
| 51 ServiceWorkerFetchResponse* out_response, | |
| 52 ServiceWorkerStatusCode actual_status, | 59 ServiceWorkerStatusCode actual_status, |
| 53 const ServiceWorkerFetchResponse& actual_response) { | 60 ServiceWorkerFetchEventResult actual_result, |
| 54 *out_status = actual_status; | 61 const ServiceWorkerResponse& actual_response) { |
| 55 *out_response = actual_response; | 62 out_result->status = actual_status; |
| 63 out_result->result = actual_result; | |
| 64 out_result->response = actual_response; | |
| 56 if (!quit.is_null()) | 65 if (!quit.is_null()) |
| 57 BrowserThread::PostTask(run_quit_thread, FROM_HERE, quit); | 66 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, quit); |
| 58 } | 67 } |
| 59 | 68 |
| 60 ServiceWorkerVersion::FetchCallback CreateFetchResponseReceiver( | 69 ServiceWorkerVersion::FetchCallback CreateResponseReceiver( |
| 61 BrowserThread::ID run_quit_thread, | 70 BrowserThread::ID run_quit_thread, |
| 62 const base::Closure& quit, | 71 const base::Closure& quit, |
| 63 ServiceWorkerStatusCode* out_status, | 72 FetchResult* result) { |
| 64 ServiceWorkerFetchResponse* out_response) { | 73 return base::Bind(&ReceiveFetchResult, run_quit_thread, quit, result); |
| 65 return base::Bind( | |
| 66 &ReceiveFetchResult, run_quit_thread, quit, out_status, out_response); | |
| 67 } | 74 } |
| 68 | 75 |
| 69 } // namespace | 76 } // namespace |
| 70 | 77 |
| 71 class ServiceWorkerBrowserTest : public ContentBrowserTest { | 78 class ServiceWorkerBrowserTest : public ContentBrowserTest { |
| 72 protected: | 79 protected: |
| 73 typedef ServiceWorkerBrowserTest self; | 80 typedef ServiceWorkerBrowserTest self; |
| 74 | 81 |
| 75 virtual void SetUpCommandLine(CommandLine* command_line) OVERRIDE { | 82 virtual void SetUpCommandLine(CommandLine* command_line) OVERRIDE { |
| 76 command_line->AppendSwitch(switches::kEnableServiceWorker); | 83 command_line->AppendSwitch(switches::kEnableServiceWorker); |
| (...skipping 148 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 225 status = SERVICE_WORKER_ERROR_FAILED; | 232 status = SERVICE_WORKER_ERROR_FAILED; |
| 226 base::RunLoop stop_run_loop; | 233 base::RunLoop stop_run_loop; |
| 227 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, | 234 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, |
| 228 base::Bind(&self::StopOnIOThread, this, | 235 base::Bind(&self::StopOnIOThread, this, |
| 229 stop_run_loop.QuitClosure(), | 236 stop_run_loop.QuitClosure(), |
| 230 &status)); | 237 &status)); |
| 231 stop_run_loop.Run(); | 238 stop_run_loop.Run(); |
| 232 ASSERT_EQ(SERVICE_WORKER_OK, status); | 239 ASSERT_EQ(SERVICE_WORKER_OK, status); |
| 233 } | 240 } |
| 234 | 241 |
| 242 void FetchTestHelper(const std::string& worker_url, | |
| 243 ServiceWorkerFetchEventResult* result, | |
| 244 ServiceWorkerResponse* response) { | |
| 245 RunOnIOThread( | |
| 246 base::Bind(&self::SetUpRegistrationOnIOThread, this, worker_url)); | |
| 247 | |
| 248 FetchResult fetch_result; | |
| 249 fetch_result.status = SERVICE_WORKER_ERROR_FAILED; | |
| 250 base::RunLoop fetch_run_loop; | |
| 251 BrowserThread::PostTask(BrowserThread::IO, | |
| 252 FROM_HERE, | |
| 253 base::Bind(&self::FetchOnIOThread, | |
| 254 this, | |
| 255 fetch_run_loop.QuitClosure(), | |
| 256 &fetch_result)); | |
| 257 fetch_run_loop.Run(); | |
| 258 ASSERT_EQ(SERVICE_WORKER_OK, fetch_result.status); | |
| 259 *result = fetch_result.result; | |
| 260 *response = fetch_result.response; | |
| 261 } | |
| 262 | |
| 235 void SetUpRegistrationOnIOThread(const std::string& worker_url) { | 263 void SetUpRegistrationOnIOThread(const std::string& worker_url) { |
| 236 const int64 version_id = 1L; | 264 const int64 version_id = 1L; |
| 237 registration_ = new ServiceWorkerRegistration( | 265 registration_ = new ServiceWorkerRegistration( |
| 238 embedded_test_server()->GetURL("/*"), | 266 embedded_test_server()->GetURL("/*"), |
| 239 embedded_test_server()->GetURL(worker_url), | 267 embedded_test_server()->GetURL(worker_url), |
| 240 next_registration_id_++); | 268 next_registration_id_++); |
| 241 version_ = new ServiceWorkerVersion( | 269 version_ = new ServiceWorkerVersion( |
| 242 registration_, | 270 registration_, |
| 243 wrapper()->context()->embedded_worker_registry(), | 271 wrapper()->context()->embedded_worker_registry(), |
| 244 version_id); | 272 version_id); |
| 245 AssociateRendererProcessToWorker(version_->embedded_worker()); | 273 AssociateRendererProcessToWorker(version_->embedded_worker()); |
| 246 } | 274 } |
| 247 | 275 |
| 248 void StartOnIOThread(const base::Closure& done, | 276 void StartOnIOThread(const base::Closure& done, |
| 249 ServiceWorkerStatusCode* result) { | 277 ServiceWorkerStatusCode* result) { |
| 250 ASSERT_TRUE(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 278 ASSERT_TRUE(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 251 version_->StartWorker(CreateReceiver(BrowserThread::UI, done, result)); | 279 version_->StartWorker(CreateReceiver(BrowserThread::UI, done, result)); |
| 252 } | 280 } |
| 253 | 281 |
| 254 void InstallOnIOThread(const base::Closure& done, | 282 void InstallOnIOThread(const base::Closure& done, |
| 255 ServiceWorkerStatusCode* result) { | 283 ServiceWorkerStatusCode* result) { |
| 256 ASSERT_TRUE(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 284 ASSERT_TRUE(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 257 version_->DispatchInstallEvent( | 285 version_->DispatchInstallEvent( |
| 258 -1, CreateReceiver(BrowserThread::UI, done, result)); | 286 -1, CreateReceiver(BrowserThread::UI, done, result)); |
| 259 } | 287 } |
| 260 | 288 |
| 261 void FetchOnIOThread(const base::Closure& done, | 289 void FetchOnIOThread(const base::Closure& done, FetchResult* result) { |
| 262 ServiceWorkerStatusCode* result, | |
| 263 ServiceWorkerFetchResponse* message) { | |
| 264 ASSERT_TRUE(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 290 ASSERT_TRUE(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 265 ServiceWorkerFetchRequest request( | 291 ServiceWorkerFetchRequest request( |
| 266 embedded_test_server()->GetURL("/service_worker/empty.html"), | 292 embedded_test_server()->GetURL("/service_worker/empty.html"), |
| 267 "GET", | 293 "GET", |
| 268 std::map<std::string, std::string>()); | 294 std::map<std::string, std::string>()); |
| 269 version_->DispatchFetchEvent( | 295 version_->DispatchFetchEvent( |
| 270 request, | 296 request, CreateResponseReceiver(BrowserThread::UI, done, result)); |
| 271 CreateFetchResponseReceiver(BrowserThread::UI, done, result, message)); | |
| 272 } | 297 } |
| 273 | 298 |
| 274 | |
| 275 void StopOnIOThread(const base::Closure& done, | 299 void StopOnIOThread(const base::Closure& done, |
| 276 ServiceWorkerStatusCode* result) { | 300 ServiceWorkerStatusCode* result) { |
| 277 ASSERT_TRUE(version_); | 301 ASSERT_TRUE(version_); |
| 278 version_->StopWorker(CreateReceiver(BrowserThread::UI, done, result)); | 302 version_->StopWorker(CreateReceiver(BrowserThread::UI, done, result)); |
| 279 } | 303 } |
| 280 | 304 |
| 281 protected: | 305 protected: |
| 282 int64 next_registration_id_; | 306 int64 next_registration_id_; |
| 283 scoped_refptr<ServiceWorkerRegistration> registration_; | 307 scoped_refptr<ServiceWorkerRegistration> registration_; |
| 284 scoped_refptr<ServiceWorkerVersion> version_; | 308 scoped_refptr<ServiceWorkerVersion> version_; |
| (...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 353 InstallTestHelper("/service_worker/worker_install_fulfilled.js"); | 377 InstallTestHelper("/service_worker/worker_install_fulfilled.js"); |
| 354 } | 378 } |
| 355 | 379 |
| 356 IN_PROC_BROWSER_TEST_F(ServiceWorkerVersionBrowserTest, | 380 IN_PROC_BROWSER_TEST_F(ServiceWorkerVersionBrowserTest, |
| 357 InstallWithWaitUntil_Rejected) { | 381 InstallWithWaitUntil_Rejected) { |
| 358 // TODO(kinuko): This should also report back an error, but we | 382 // TODO(kinuko): This should also report back an error, but we |
| 359 // don't have plumbing for it yet. | 383 // don't have plumbing for it yet. |
| 360 InstallTestHelper("/service_worker/worker_install_rejected.js"); | 384 InstallTestHelper("/service_worker/worker_install_rejected.js"); |
| 361 } | 385 } |
| 362 | 386 |
| 363 IN_PROC_BROWSER_TEST_F(ServiceWorkerVersionBrowserTest, Fetch) { | 387 IN_PROC_BROWSER_TEST_F(ServiceWorkerVersionBrowserTest, FetchEvent_Response) { |
| 364 RunOnIOThread(base::Bind(&self::SetUpRegistrationOnIOThread, this, | 388 ServiceWorkerFetchEventResult result; |
| 365 "/service_worker/worker.js")); | 389 ServiceWorkerResponse response; |
| 366 | 390 FetchTestHelper("/service_worker/fetch_event.js", &result, &response); |
| 367 ServiceWorkerStatusCode status = SERVICE_WORKER_ERROR_FAILED; | 391 ASSERT_EQ(SERVICE_WORKER_FETCH_RESULT_RESPONSE, result); |
| 368 ServiceWorkerFetchResponse response; | |
| 369 base::RunLoop fetch_run_loop; | |
| 370 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, | |
| 371 base::Bind(&self::FetchOnIOThread, this, | |
| 372 fetch_run_loop.QuitClosure(), | |
| 373 &status, &response)); | |
| 374 fetch_run_loop.Run(); | |
| 375 ASSERT_EQ(SERVICE_WORKER_OK, status); | |
| 376 ASSERT_EQ(200, response.status_code); | 392 ASSERT_EQ(200, response.status_code); |
| 377 ASSERT_EQ("OK", response.status_text); | 393 ASSERT_EQ("OK", response.status_text); |
| 378 ASSERT_EQ("GET", response.method); | 394 ASSERT_EQ("GET", response.method); |
| 379 std::map<std::string, std::string> expected_headers; | 395 std::map<std::string, std::string> expected_headers; |
| 380 ASSERT_EQ(expected_headers, response.headers); | 396 ASSERT_EQ(expected_headers, response.headers); |
| 381 } | 397 } |
| 382 | 398 |
| 399 IN_PROC_BROWSER_TEST_F(ServiceWorkerVersionBrowserTest, | |
| 400 FetchEvent_FallbackToNative) { | |
| 401 ServiceWorkerFetchEventResult result; | |
| 402 ServiceWorkerResponse response; | |
| 403 FetchTestHelper( | |
| 404 "/service_worker/fetch_event_fallback.js", &result, &response); | |
| 405 ASSERT_EQ(SERVICE_WORKER_FETCH_RESULT_FALLBACK, result); | |
| 406 } | |
| 407 | |
| 408 IN_PROC_BROWSER_TEST_F(ServiceWorkerVersionBrowserTest, FetchEvent_Rejected) { | |
| 409 ServiceWorkerFetchEventResult result; | |
| 410 ServiceWorkerResponse response; | |
| 411 FetchTestHelper("/service_worker/fetch_event_error.js", &result, &response); | |
| 412 ASSERT_EQ(SERVICE_WORKER_FETCH_RESULT_FALLBACK, result); | |
| 413 } | |
| 414 | |
| 383 } // namespace content | 415 } // namespace content |
| OLD | NEW |