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/fileapi/chrome_blob_storage_context.h" | 9 #include "content/browser/fileapi/chrome_blob_storage_context.h" |
10 #include "content/browser/service_worker/embedded_worker_instance.h" | 10 #include "content/browser/service_worker/embedded_worker_instance.h" |
(...skipping 21 matching lines...) Expand all Loading... |
32 #include "webkit/common/blob/blob_data.h" | 32 #include "webkit/common/blob/blob_data.h" |
33 | 33 |
34 namespace content { | 34 namespace content { |
35 | 35 |
36 namespace { | 36 namespace { |
37 | 37 |
38 struct FetchResult { | 38 struct FetchResult { |
39 ServiceWorkerStatusCode status; | 39 ServiceWorkerStatusCode status; |
40 ServiceWorkerFetchEventResult result; | 40 ServiceWorkerFetchEventResult result; |
41 ServiceWorkerResponse response; | 41 ServiceWorkerResponse response; |
| 42 scoped_ptr<webkit_blob::BlobDataHandle> blob_data_handle; |
42 }; | 43 }; |
43 | 44 |
44 void RunAndQuit(const base::Closure& closure, | 45 void RunAndQuit(const base::Closure& closure, |
45 const base::Closure& quit, | 46 const base::Closure& quit, |
46 base::MessageLoopProxy* original_message_loop) { | 47 base::MessageLoopProxy* original_message_loop) { |
47 closure.Run(); | 48 closure.Run(); |
48 original_message_loop->PostTask(FROM_HERE, quit); | 49 original_message_loop->PostTask(FROM_HERE, quit); |
49 } | 50 } |
50 | 51 |
51 void RunOnIOThread(const base::Closure& closure) { | 52 void RunOnIOThread(const base::Closure& closure) { |
(...skipping 17 matching lines...) Expand all Loading... |
69 FROM_HERE, | 70 FROM_HERE, |
70 base::Bind(closure, quit_on_original_thread)); | 71 base::Bind(closure, quit_on_original_thread)); |
71 run_loop.Run(); | 72 run_loop.Run(); |
72 } | 73 } |
73 | 74 |
74 // Contrary to the style guide, the output parameter of this function comes | 75 // Contrary to the style guide, the output parameter of this function comes |
75 // before input parameters so Bind can be used on it to create a FetchCallback | 76 // before input parameters so Bind can be used on it to create a FetchCallback |
76 // to pass to DispatchFetchEvent. | 77 // to pass to DispatchFetchEvent. |
77 void ReceiveFetchResult(BrowserThread::ID run_quit_thread, | 78 void ReceiveFetchResult(BrowserThread::ID run_quit_thread, |
78 const base::Closure& quit, | 79 const base::Closure& quit, |
| 80 ChromeBlobStorageContext* blob_context, |
79 FetchResult* out_result, | 81 FetchResult* out_result, |
80 ServiceWorkerStatusCode actual_status, | 82 ServiceWorkerStatusCode actual_status, |
81 ServiceWorkerFetchEventResult actual_result, | 83 ServiceWorkerFetchEventResult actual_result, |
82 const ServiceWorkerResponse& actual_response) { | 84 const ServiceWorkerResponse& actual_response) { |
83 out_result->status = actual_status; | 85 out_result->status = actual_status; |
84 out_result->result = actual_result; | 86 out_result->result = actual_result; |
85 out_result->response = actual_response; | 87 out_result->response = actual_response; |
| 88 if (!actual_response.blob_uuid.empty()) { |
| 89 out_result->blob_data_handle = |
| 90 blob_context->context()->GetBlobDataFromUUID( |
| 91 actual_response.blob_uuid); |
| 92 } |
86 if (!quit.is_null()) | 93 if (!quit.is_null()) |
87 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, quit); | 94 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, quit); |
88 } | 95 } |
89 | 96 |
90 ServiceWorkerVersion::FetchCallback CreateResponseReceiver( | 97 ServiceWorkerVersion::FetchCallback CreateResponseReceiver( |
91 BrowserThread::ID run_quit_thread, | 98 BrowserThread::ID run_quit_thread, |
92 const base::Closure& quit, | 99 const base::Closure& quit, |
| 100 ChromeBlobStorageContext* blob_context, |
93 FetchResult* result) { | 101 FetchResult* result) { |
94 return base::Bind(&ReceiveFetchResult, run_quit_thread, quit, result); | 102 return base::Bind(&ReceiveFetchResult, run_quit_thread, quit, |
| 103 make_scoped_refptr<ChromeBlobStorageContext>(blob_context), |
| 104 result); |
95 } | 105 } |
96 | 106 |
97 void ReadResponseBody(std::string* body, | 107 void ReadResponseBody(std::string* body, |
98 scoped_refptr<ChromeBlobStorageContext> context, | 108 webkit_blob::BlobDataHandle* blob_data_handle) { |
99 std::string blob_uuid) { | 109 ASSERT_TRUE(blob_data_handle); |
100 scoped_ptr<webkit_blob::BlobDataHandle> blob_data_handle = | |
101 context->context()->GetBlobDataFromUUID(blob_uuid); | |
102 ASSERT_EQ(1U, blob_data_handle->data()->items().size()); | 110 ASSERT_EQ(1U, blob_data_handle->data()->items().size()); |
103 *body = std::string(blob_data_handle->data()->items()[0].bytes(), | 111 *body = std::string(blob_data_handle->data()->items()[0].bytes(), |
104 blob_data_handle->data()->items()[0].length()); | 112 blob_data_handle->data()->items()[0].length()); |
105 } | 113 } |
106 | 114 |
107 } // namespace | 115 } // namespace |
108 | 116 |
109 class ServiceWorkerBrowserTest : public ContentBrowserTest { | 117 class ServiceWorkerBrowserTest : public ContentBrowserTest { |
110 protected: | 118 protected: |
111 typedef ServiceWorkerBrowserTest self; | 119 typedef ServiceWorkerBrowserTest self; |
(...skipping 181 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
293 base::RunLoop run_loop; | 301 base::RunLoop run_loop; |
294 BrowserThread::PostTask( | 302 BrowserThread::PostTask( |
295 BrowserThread::IO, | 303 BrowserThread::IO, |
296 FROM_HERE, | 304 FROM_HERE, |
297 base::Bind( | 305 base::Bind( |
298 &self::ActivateOnIOThread, this, run_loop.QuitClosure(), &status)); | 306 &self::ActivateOnIOThread, this, run_loop.QuitClosure(), &status)); |
299 run_loop.Run(); | 307 run_loop.Run(); |
300 ASSERT_EQ(expected_status, status); | 308 ASSERT_EQ(expected_status, status); |
301 } | 309 } |
302 | 310 |
303 void FetchOnRegisteredWorker(ServiceWorkerFetchEventResult* result, | 311 void FetchOnRegisteredWorker( |
304 ServiceWorkerResponse* response) { | 312 ServiceWorkerFetchEventResult* result, |
| 313 ServiceWorkerResponse* response, |
| 314 scoped_ptr<webkit_blob::BlobDataHandle>* blob_data_handle) { |
| 315 blob_context_ = ChromeBlobStorageContext::GetFor( |
| 316 shell()->web_contents()->GetBrowserContext()); |
305 FetchResult fetch_result; | 317 FetchResult fetch_result; |
306 fetch_result.status = SERVICE_WORKER_ERROR_FAILED; | 318 fetch_result.status = SERVICE_WORKER_ERROR_FAILED; |
307 base::RunLoop fetch_run_loop; | 319 base::RunLoop fetch_run_loop; |
308 BrowserThread::PostTask(BrowserThread::IO, | 320 BrowserThread::PostTask(BrowserThread::IO, |
309 FROM_HERE, | 321 FROM_HERE, |
310 base::Bind(&self::FetchOnIOThread, | 322 base::Bind(&self::FetchOnIOThread, |
311 this, | 323 this, |
312 fetch_run_loop.QuitClosure(), | 324 fetch_run_loop.QuitClosure(), |
313 &fetch_result)); | 325 &fetch_result)); |
314 fetch_run_loop.Run(); | 326 fetch_run_loop.Run(); |
315 *result = fetch_result.result; | 327 *result = fetch_result.result; |
316 *response = fetch_result.response; | 328 *response = fetch_result.response; |
| 329 *blob_data_handle = fetch_result.blob_data_handle.Pass(); |
317 ASSERT_EQ(SERVICE_WORKER_OK, fetch_result.status); | 330 ASSERT_EQ(SERVICE_WORKER_OK, fetch_result.status); |
318 } | 331 } |
319 | 332 |
320 void FetchTestHelper(const std::string& worker_url, | 333 void FetchTestHelper( |
321 ServiceWorkerFetchEventResult* result, | 334 const std::string& worker_url, |
322 ServiceWorkerResponse* response) { | 335 ServiceWorkerFetchEventResult* result, |
| 336 ServiceWorkerResponse* response, |
| 337 scoped_ptr<webkit_blob::BlobDataHandle>* blob_data_handle) { |
323 RunOnIOThread( | 338 RunOnIOThread( |
324 base::Bind(&self::SetUpRegistrationOnIOThread, this, worker_url)); | 339 base::Bind(&self::SetUpRegistrationOnIOThread, this, worker_url)); |
325 | 340 FetchOnRegisteredWorker(result, response, blob_data_handle); |
326 FetchOnRegisteredWorker(result, response); | |
327 } | 341 } |
328 | 342 |
329 void SetUpRegistrationOnIOThread(const std::string& worker_url) { | 343 void SetUpRegistrationOnIOThread(const std::string& worker_url) { |
330 registration_ = new ServiceWorkerRegistration( | 344 registration_ = new ServiceWorkerRegistration( |
331 embedded_test_server()->GetURL("/*"), | 345 embedded_test_server()->GetURL("/*"), |
332 embedded_test_server()->GetURL(worker_url), | 346 embedded_test_server()->GetURL(worker_url), |
333 wrapper()->context()->storage()->NewRegistrationId(), | 347 wrapper()->context()->storage()->NewRegistrationId(), |
334 wrapper()->context()->AsWeakPtr()); | 348 wrapper()->context()->AsWeakPtr()); |
335 version_ = new ServiceWorkerVersion( | 349 version_ = new ServiceWorkerVersion( |
336 registration_, | 350 registration_, |
(...skipping 24 matching lines...) Expand all Loading... |
361 } | 375 } |
362 | 376 |
363 void FetchOnIOThread(const base::Closure& done, FetchResult* result) { | 377 void FetchOnIOThread(const base::Closure& done, FetchResult* result) { |
364 ASSERT_TRUE(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 378 ASSERT_TRUE(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
365 ServiceWorkerFetchRequest request( | 379 ServiceWorkerFetchRequest request( |
366 embedded_test_server()->GetURL("/service_worker/empty.html"), | 380 embedded_test_server()->GetURL("/service_worker/empty.html"), |
367 "GET", | 381 "GET", |
368 std::map<std::string, std::string>()); | 382 std::map<std::string, std::string>()); |
369 version_->SetStatus(ServiceWorkerVersion::ACTIVE); | 383 version_->SetStatus(ServiceWorkerVersion::ACTIVE); |
370 version_->DispatchFetchEvent( | 384 version_->DispatchFetchEvent( |
371 request, CreateResponseReceiver(BrowserThread::UI, done, result)); | 385 request, CreateResponseReceiver(BrowserThread::UI, done, |
| 386 blob_context_, result)); |
372 } | 387 } |
373 | 388 |
374 void StopOnIOThread(const base::Closure& done, | 389 void StopOnIOThread(const base::Closure& done, |
375 ServiceWorkerStatusCode* result) { | 390 ServiceWorkerStatusCode* result) { |
376 ASSERT_TRUE(version_); | 391 ASSERT_TRUE(version_); |
377 version_->StopWorker(CreateReceiver(BrowserThread::UI, done, result)); | 392 version_->StopWorker(CreateReceiver(BrowserThread::UI, done, result)); |
378 } | 393 } |
379 | 394 |
380 void SyncEventOnIOThread(const base::Closure& done, | 395 void SyncEventOnIOThread(const base::Closure& done, |
381 ServiceWorkerStatusCode* result) { | 396 ServiceWorkerStatusCode* result) { |
382 ASSERT_TRUE(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 397 ASSERT_TRUE(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
383 version_->SetStatus(ServiceWorkerVersion::ACTIVE); | 398 version_->SetStatus(ServiceWorkerVersion::ACTIVE); |
384 version_->DispatchSyncEvent( | 399 version_->DispatchSyncEvent( |
385 CreateReceiver(BrowserThread::UI, done, result)); | 400 CreateReceiver(BrowserThread::UI, done, result)); |
386 } | 401 } |
387 | 402 |
388 protected: | 403 protected: |
389 scoped_refptr<ServiceWorkerRegistration> registration_; | 404 scoped_refptr<ServiceWorkerRegistration> registration_; |
390 scoped_refptr<ServiceWorkerVersion> version_; | 405 scoped_refptr<ServiceWorkerVersion> version_; |
| 406 scoped_refptr<ChromeBlobStorageContext> blob_context_; |
391 }; | 407 }; |
392 | 408 |
393 IN_PROC_BROWSER_TEST_F(EmbeddedWorkerBrowserTest, StartAndStop) { | 409 IN_PROC_BROWSER_TEST_F(EmbeddedWorkerBrowserTest, StartAndStop) { |
394 // Start a worker and wait until OnStarted() is called. | 410 // Start a worker and wait until OnStarted() is called. |
395 base::RunLoop start_run_loop; | 411 base::RunLoop start_run_loop; |
396 done_closure_ = start_run_loop.QuitClosure(); | 412 done_closure_ = start_run_loop.QuitClosure(); |
397 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, | 413 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, |
398 base::Bind(&self::StartOnIOThread, this)); | 414 base::Bind(&self::StartOnIOThread, this)); |
399 start_run_loop.Run(); | 415 start_run_loop.Run(); |
400 | 416 |
(...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
473 | 489 |
474 IN_PROC_BROWSER_TEST_F(ServiceWorkerVersionBrowserTest, | 490 IN_PROC_BROWSER_TEST_F(ServiceWorkerVersionBrowserTest, |
475 InstallWithWaitUntil_Rejected) { | 491 InstallWithWaitUntil_Rejected) { |
476 InstallTestHelper("/service_worker/worker_install_rejected.js", | 492 InstallTestHelper("/service_worker/worker_install_rejected.js", |
477 SERVICE_WORKER_ERROR_INSTALL_WORKER_FAILED); | 493 SERVICE_WORKER_ERROR_INSTALL_WORKER_FAILED); |
478 } | 494 } |
479 | 495 |
480 IN_PROC_BROWSER_TEST_F(ServiceWorkerVersionBrowserTest, FetchEvent_Response) { | 496 IN_PROC_BROWSER_TEST_F(ServiceWorkerVersionBrowserTest, FetchEvent_Response) { |
481 ServiceWorkerFetchEventResult result; | 497 ServiceWorkerFetchEventResult result; |
482 ServiceWorkerResponse response; | 498 ServiceWorkerResponse response; |
483 FetchTestHelper("/service_worker/fetch_event.js", &result, &response); | 499 scoped_ptr<webkit_blob::BlobDataHandle> blob_data_handle; |
| 500 FetchTestHelper("/service_worker/fetch_event.js", |
| 501 &result, &response, &blob_data_handle); |
484 ASSERT_EQ(SERVICE_WORKER_FETCH_EVENT_RESULT_RESPONSE, result); | 502 ASSERT_EQ(SERVICE_WORKER_FETCH_EVENT_RESULT_RESPONSE, result); |
485 EXPECT_EQ(301, response.status_code); | 503 EXPECT_EQ(301, response.status_code); |
486 EXPECT_EQ("Moved Permanently", response.status_text); | 504 EXPECT_EQ("Moved Permanently", response.status_text); |
487 std::map<std::string, std::string> expected_headers; | 505 std::map<std::string, std::string> expected_headers; |
488 expected_headers["Content-Language"] = "fi"; | 506 expected_headers["Content-Language"] = "fi"; |
489 expected_headers["Content-Type"] = "text/html; charset=UTF-8"; | 507 expected_headers["Content-Type"] = "text/html; charset=UTF-8"; |
490 EXPECT_EQ(expected_headers, response.headers); | 508 EXPECT_EQ(expected_headers, response.headers); |
491 | 509 |
492 scoped_refptr<ChromeBlobStorageContext> context = | |
493 ChromeBlobStorageContext::GetFor( | |
494 shell()->web_contents()->GetBrowserContext()); | |
495 std::string body; | 510 std::string body; |
496 RunOnIOThread( | 511 RunOnIOThread( |
497 base::Bind(&ReadResponseBody, &body, context, response.blob_uuid)); | 512 base::Bind(&ReadResponseBody, |
| 513 &body, base::Owned(blob_data_handle.release()))); |
498 EXPECT_EQ("This resource is gone. Gone, gone, gone.", body); | 514 EXPECT_EQ("This resource is gone. Gone, gone, gone.", body); |
499 } | 515 } |
500 | 516 |
501 IN_PROC_BROWSER_TEST_F(ServiceWorkerVersionBrowserTest, | 517 IN_PROC_BROWSER_TEST_F(ServiceWorkerVersionBrowserTest, |
502 SyncAbortedWithoutFlag) { | 518 SyncAbortedWithoutFlag) { |
503 RunOnIOThread(base::Bind( | 519 RunOnIOThread(base::Bind( |
504 &self::SetUpRegistrationOnIOThread, this, "/service_worker/sync.js")); | 520 &self::SetUpRegistrationOnIOThread, this, "/service_worker/sync.js")); |
505 | 521 |
506 // Run the sync event. | 522 // Run the sync event. |
507 ServiceWorkerStatusCode status = SERVICE_WORKER_ERROR_FAILED; | 523 ServiceWorkerStatusCode status = SERVICE_WORKER_ERROR_FAILED; |
508 base::RunLoop sync_run_loop; | 524 base::RunLoop sync_run_loop; |
509 BrowserThread::PostTask(BrowserThread::IO, | 525 BrowserThread::PostTask(BrowserThread::IO, |
510 FROM_HERE, | 526 FROM_HERE, |
511 base::Bind(&self::SyncEventOnIOThread, | 527 base::Bind(&self::SyncEventOnIOThread, |
512 this, | 528 this, |
513 sync_run_loop.QuitClosure(), | 529 sync_run_loop.QuitClosure(), |
514 &status)); | 530 &status)); |
515 sync_run_loop.Run(); | 531 sync_run_loop.Run(); |
516 ASSERT_EQ(SERVICE_WORKER_ERROR_ABORT, status); | 532 ASSERT_EQ(SERVICE_WORKER_ERROR_ABORT, status); |
517 } | 533 } |
518 | 534 |
519 IN_PROC_BROWSER_TEST_F(ServiceWorkerVersionBrowserTest, SyncEventHandled) { | 535 IN_PROC_BROWSER_TEST_F(ServiceWorkerVersionBrowserTest, SyncEventHandled) { |
520 CommandLine* command_line = CommandLine::ForCurrentProcess(); | 536 CommandLine* command_line = CommandLine::ForCurrentProcess(); |
521 command_line->AppendSwitch(switches::kEnableServiceWorkerSync); | 537 command_line->AppendSwitch(switches::kEnableServiceWorkerSync); |
522 | 538 |
523 RunOnIOThread(base::Bind( | 539 RunOnIOThread(base::Bind( |
524 &self::SetUpRegistrationOnIOThread, this, "/service_worker/sync.js")); | 540 &self::SetUpRegistrationOnIOThread, this, "/service_worker/sync.js")); |
525 ServiceWorkerFetchEventResult result; | 541 ServiceWorkerFetchEventResult result; |
526 ServiceWorkerResponse response; | 542 ServiceWorkerResponse response; |
527 | 543 scoped_ptr<webkit_blob::BlobDataHandle> blob_data_handle; |
528 // Should 404 before sync event. | 544 // Should 404 before sync event. |
529 FetchOnRegisteredWorker(&result, &response); | 545 FetchOnRegisteredWorker(&result, &response, &blob_data_handle); |
530 EXPECT_EQ(404, response.status_code); | 546 EXPECT_EQ(404, response.status_code); |
531 | 547 |
532 // Run the sync event. | 548 // Run the sync event. |
533 ServiceWorkerStatusCode status = SERVICE_WORKER_ERROR_FAILED; | 549 ServiceWorkerStatusCode status = SERVICE_WORKER_ERROR_FAILED; |
534 base::RunLoop sync_run_loop; | 550 base::RunLoop sync_run_loop; |
535 BrowserThread::PostTask(BrowserThread::IO, | 551 BrowserThread::PostTask(BrowserThread::IO, |
536 FROM_HERE, | 552 FROM_HERE, |
537 base::Bind(&self::SyncEventOnIOThread, | 553 base::Bind(&self::SyncEventOnIOThread, |
538 this, | 554 this, |
539 sync_run_loop.QuitClosure(), | 555 sync_run_loop.QuitClosure(), |
540 &status)); | 556 &status)); |
541 sync_run_loop.Run(); | 557 sync_run_loop.Run(); |
542 ASSERT_EQ(SERVICE_WORKER_OK, status); | 558 ASSERT_EQ(SERVICE_WORKER_OK, status); |
543 | 559 |
544 // Should 200 after sync event. | 560 // Should 200 after sync event. |
545 FetchOnRegisteredWorker(&result, &response); | 561 FetchOnRegisteredWorker(&result, &response, &blob_data_handle); |
546 EXPECT_EQ(200, response.status_code); | 562 EXPECT_EQ(200, response.status_code); |
547 } | 563 } |
548 | 564 |
549 class ServiceWorkerBlackBoxBrowserTest : public ServiceWorkerBrowserTest { | 565 class ServiceWorkerBlackBoxBrowserTest : public ServiceWorkerBrowserTest { |
550 public: | 566 public: |
551 typedef ServiceWorkerBlackBoxBrowserTest self; | 567 typedef ServiceWorkerBlackBoxBrowserTest self; |
552 | 568 |
553 static void ExpectResultAndRun(bool expected, | 569 static void ExpectResultAndRun(bool expected, |
554 const base::Closure& continuation, | 570 const base::Closure& continuation, |
555 bool actual) { | 571 bool actual) { |
(...skipping 123 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
679 base::Bind(&ServiceWorkerBlackBoxBrowserTest::FindRegistrationOnIO, | 695 base::Bind(&ServiceWorkerBlackBoxBrowserTest::FindRegistrationOnIO, |
680 this, | 696 this, |
681 embedded_test_server()->GetURL("/service_worker/empty.html"), | 697 embedded_test_server()->GetURL("/service_worker/empty.html"), |
682 &status, | 698 &status, |
683 &script_url)); | 699 &script_url)); |
684 EXPECT_EQ(SERVICE_WORKER_ERROR_NOT_FOUND, status); | 700 EXPECT_EQ(SERVICE_WORKER_ERROR_NOT_FOUND, status); |
685 } | 701 } |
686 } | 702 } |
687 | 703 |
688 } // namespace content | 704 } // namespace content |
OLD | NEW |