Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(441)

Side by Side Diff: content/browser/service_worker/service_worker_browsertest.cc

Issue 912753002: Stop Service Workers that execute JavaScript for too long. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: rethink Created 5 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 "base/strings/utf_string_conversions.h" 9 #include "base/strings/utf_string_conversions.h"
10 #include "content/browser/fileapi/chrome_blob_storage_context.h" 10 #include "content/browser/fileapi/chrome_blob_storage_context.h"
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after
55 scoped_ptr<storage::BlobDataHandle> blob_data_handle; 55 scoped_ptr<storage::BlobDataHandle> blob_data_handle;
56 }; 56 };
57 57
58 void RunAndQuit(const base::Closure& closure, 58 void RunAndQuit(const base::Closure& closure,
59 const base::Closure& quit, 59 const base::Closure& quit,
60 base::MessageLoopProxy* original_message_loop) { 60 base::MessageLoopProxy* original_message_loop) {
61 closure.Run(); 61 closure.Run();
62 original_message_loop->PostTask(FROM_HERE, quit); 62 original_message_loop->PostTask(FROM_HERE, quit);
63 } 63 }
64 64
65 void RunOnIOThread(const base::Closure& closure) { 65 void RunOnIOThreadWithDelay(const base::Closure& closure,
66 base::TimeDelta delay) {
66 base::RunLoop run_loop; 67 base::RunLoop run_loop;
67 BrowserThread::PostTask( 68 BrowserThread::PostDelayedTask(
68 BrowserThread::IO, FROM_HERE, 69 BrowserThread::IO, FROM_HERE,
69 base::Bind(&RunAndQuit, closure, run_loop.QuitClosure(), 70 base::Bind(&RunAndQuit, closure, run_loop.QuitClosure(),
70 base::MessageLoopProxy::current())); 71 base::MessageLoopProxy::current()),
72 delay);
71 run_loop.Run(); 73 run_loop.Run();
72 } 74 }
73 75
76 void RunOnIOThread(const base::Closure& closure) {
77 RunOnIOThreadWithDelay(closure, base::TimeDelta());
78 }
79
74 void RunOnIOThread( 80 void RunOnIOThread(
75 const base::Callback<void(const base::Closure& continuation)>& closure) { 81 const base::Callback<void(const base::Closure& continuation)>& closure) {
76 base::RunLoop run_loop; 82 base::RunLoop run_loop;
77 base::Closure quit_on_original_thread = 83 base::Closure quit_on_original_thread =
78 base::Bind(base::IgnoreResult(&base::MessageLoopProxy::PostTask), 84 base::Bind(base::IgnoreResult(&base::MessageLoopProxy::PostTask),
79 base::MessageLoopProxy::current().get(), 85 base::MessageLoopProxy::current().get(),
80 FROM_HERE, 86 FROM_HERE,
81 run_loop.QuitClosure()); 87 run_loop.QuitClosure());
82 BrowserThread::PostTask(BrowserThread::IO, 88 BrowserThread::PostTask(BrowserThread::IO,
83 FROM_HERE, 89 FROM_HERE,
(...skipping 431 matching lines...) Expand 10 before | Expand all | Expand 10 after
515 wrapper()->context()->storage()->NewRegistrationId(), 521 wrapper()->context()->storage()->NewRegistrationId(),
516 wrapper()->context()->AsWeakPtr()); 522 wrapper()->context()->AsWeakPtr());
517 version_ = new ServiceWorkerVersion( 523 version_ = new ServiceWorkerVersion(
518 registration_.get(), 524 registration_.get(),
519 embedded_test_server()->GetURL(worker_url), 525 embedded_test_server()->GetURL(worker_url),
520 wrapper()->context()->storage()->NewVersionId(), 526 wrapper()->context()->storage()->NewVersionId(),
521 wrapper()->context()->AsWeakPtr()); 527 wrapper()->context()->AsWeakPtr());
522 AssociateRendererProcessToPattern(pattern); 528 AssociateRendererProcessToPattern(pattern);
523 } 529 }
524 530
531 void TimeoutWorkerOnIOThread() {
532 ASSERT_TRUE(BrowserThread::CurrentlyOn(BrowserThread::IO));
533 version_->PingWorker();
534 version_->OnPingTimeout();
535 }
536
525 void StartOnIOThread(const base::Closure& done, 537 void StartOnIOThread(const base::Closure& done,
526 ServiceWorkerStatusCode* result) { 538 ServiceWorkerStatusCode* result) {
527 ASSERT_TRUE(BrowserThread::CurrentlyOn(BrowserThread::IO)); 539 ASSERT_TRUE(BrowserThread::CurrentlyOn(BrowserThread::IO));
528 version_->StartWorker(CreateReceiver(BrowserThread::UI, done, result)); 540 version_->StartWorker(CreateReceiver(BrowserThread::UI, done, result));
529 } 541 }
530 542
531 void InstallOnIOThread(const base::Closure& done, 543 void InstallOnIOThread(const base::Closure& done,
532 ServiceWorkerStatusCode* result) { 544 ServiceWorkerStatusCode* result) {
533 ASSERT_TRUE(BrowserThread::CurrentlyOn(BrowserThread::IO)); 545 ASSERT_TRUE(BrowserThread::CurrentlyOn(BrowserThread::IO));
534 version_->SetStatus(ServiceWorkerVersion::INSTALLING); 546 version_->SetStatus(ServiceWorkerVersion::INSTALLING);
(...skipping 157 matching lines...) Expand 10 before | Expand all | Expand 10 after
692 ActivateTestHelper("/service_worker/worker_activate_rejected.js", 704 ActivateTestHelper("/service_worker/worker_activate_rejected.js",
693 SERVICE_WORKER_ERROR_ACTIVATE_WORKER_FAILED); 705 SERVICE_WORKER_ERROR_ACTIVATE_WORKER_FAILED);
694 } 706 }
695 707
696 IN_PROC_BROWSER_TEST_F(ServiceWorkerVersionBrowserTest, 708 IN_PROC_BROWSER_TEST_F(ServiceWorkerVersionBrowserTest,
697 InstallWithWaitUntil_Rejected) { 709 InstallWithWaitUntil_Rejected) {
698 InstallTestHelper("/service_worker/worker_install_rejected.js", 710 InstallTestHelper("/service_worker/worker_install_rejected.js",
699 SERVICE_WORKER_ERROR_INSTALL_WORKER_FAILED); 711 SERVICE_WORKER_ERROR_INSTALL_WORKER_FAILED);
700 } 712 }
701 713
714 class WaitForLoaded : public EmbeddedWorkerInstance::Listener {
715 public:
716 WaitForLoaded(const base::Closure& quit) : quit_(quit) {}
717
718 void OnScriptLoaded() override {
719 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, quit_);
720 }
721 bool OnMessageReceived(const IPC::Message& message) override { return false; }
722
723 private:
724 base::Closure quit_;
725 };
726
727 IN_PROC_BROWSER_TEST_F(ServiceWorkerVersionBrowserTest, TimeoutStartingWorker) {
728 RunOnIOThread(base::Bind(&self::SetUpRegistrationOnIOThread, this,
729 "/service_worker/while_true_worker.js"));
730
731 // Start a worker, waiting until the script is loaded.
732 ServiceWorkerStatusCode status = SERVICE_WORKER_ERROR_FAILED;
733 base::RunLoop start_run_loop;
734 base::RunLoop load_run_loop;
735 WaitForLoaded wait_for_load(load_run_loop.QuitClosure());
736 version_->embedded_worker()->AddListener(&wait_for_load);
737 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE,
738 base::Bind(&self::StartOnIOThread, this,
739 start_run_loop.QuitClosure(), &status));
740 load_run_loop.Run();
741 version_->embedded_worker()->RemoveListener(&wait_for_load);
742
743 // The script has loaded but start has not completed yet.
744 ASSERT_EQ(SERVICE_WORKER_ERROR_FAILED, status);
745 EXPECT_EQ(ServiceWorkerVersion::STARTING, version_->running_status());
746
747 // Simulate execution timeout. Use a delay to prevent killing the worker
748 // before it's started execution.
749 EXPECT_TRUE(version_->ping_worker_timer_.IsRunning());
750 RunOnIOThreadWithDelay(base::Bind(&self::TimeoutWorkerOnIOThread, this),
751 base::TimeDelta::FromMilliseconds(100));
752 start_run_loop.Run();
753
754 EXPECT_EQ(SERVICE_WORKER_ERROR_START_WORKER_FAILED, status);
755 }
756
757 IN_PROC_BROWSER_TEST_F(ServiceWorkerVersionBrowserTest, TimeoutWorkerInEvent) {
758 RunOnIOThread(base::Bind(&self::SetUpRegistrationOnIOThread, this,
759 "/service_worker/while_true_in_install_worker.js"));
760
761 // Start a worker.
762 ServiceWorkerStatusCode status = SERVICE_WORKER_ERROR_FAILED;
763 base::RunLoop start_run_loop;
764 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE,
765 base::Bind(&self::StartOnIOThread, this,
766 start_run_loop.QuitClosure(), &status));
767 start_run_loop.Run();
768 ASSERT_EQ(SERVICE_WORKER_OK, status);
769
770 // Dispatch an event.
771 base::RunLoop install_run_loop;
772 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE,
773 base::Bind(&self::InstallOnIOThread, this,
774 install_run_loop.QuitClosure(), &status));
775
776 // Simulate execution timeout. Use a delay to prevent killing the worker
777 // before it's started execution.
778 EXPECT_TRUE(version_->ping_worker_timer_.IsRunning());
779 RunOnIOThreadWithDelay(base::Bind(&self::TimeoutWorkerOnIOThread, this),
780 base::TimeDelta::FromMilliseconds(100));
781 install_run_loop.Run();
782
783 EXPECT_EQ(SERVICE_WORKER_ERROR_INSTALL_WORKER_FAILED, status);
784 }
785
702 IN_PROC_BROWSER_TEST_F(ServiceWorkerVersionBrowserTest, FetchEvent_Response) { 786 IN_PROC_BROWSER_TEST_F(ServiceWorkerVersionBrowserTest, FetchEvent_Response) {
703 ServiceWorkerFetchEventResult result; 787 ServiceWorkerFetchEventResult result;
704 ServiceWorkerResponse response; 788 ServiceWorkerResponse response;
705 scoped_ptr<storage::BlobDataHandle> blob_data_handle; 789 scoped_ptr<storage::BlobDataHandle> blob_data_handle;
706 FetchTestHelper("/service_worker/fetch_event.js", 790 FetchTestHelper("/service_worker/fetch_event.js",
707 &result, &response, &blob_data_handle); 791 &result, &response, &blob_data_handle);
708 ASSERT_EQ(SERVICE_WORKER_FETCH_EVENT_RESULT_RESPONSE, result); 792 ASSERT_EQ(SERVICE_WORKER_FETCH_EVENT_RESULT_RESPONSE, result);
709 EXPECT_EQ(301, response.status_code); 793 EXPECT_EQ(301, response.status_code);
710 EXPECT_EQ("Moved Permanently", response.status_text); 794 EXPECT_EQ("Moved Permanently", response.status_text);
711 ServiceWorkerHeaderMap expected_headers; 795 ServiceWorkerHeaderMap expected_headers;
(...skipping 328 matching lines...) Expand 10 before | Expand all | Expand 10 after
1040 const base::string16 kOKTitle2(base::ASCIIToUTF16("OK_2")); 1124 const base::string16 kOKTitle2(base::ASCIIToUTF16("OK_2"));
1041 const base::string16 kFailTitle2(base::ASCIIToUTF16("FAIL_2")); 1125 const base::string16 kFailTitle2(base::ASCIIToUTF16("FAIL_2"));
1042 content::TitleWatcher title_watcher2(shell()->web_contents(), kOKTitle2); 1126 content::TitleWatcher title_watcher2(shell()->web_contents(), kOKTitle2);
1043 title_watcher2.AlsoWaitForTitle(kFailTitle2); 1127 title_watcher2.AlsoWaitForTitle(kFailTitle2);
1044 1128
1045 NavigateToURL(shell(), embedded_test_server()->GetURL(kConfirmPageUrl)); 1129 NavigateToURL(shell(), embedded_test_server()->GetURL(kConfirmPageUrl));
1046 EXPECT_EQ(kOKTitle2, title_watcher2.WaitAndGetTitle()); 1130 EXPECT_EQ(kOKTitle2, title_watcher2.WaitAndGetTitle());
1047 } 1131 }
1048 1132
1049 } // namespace content 1133 } // namespace content
OLDNEW
« no previous file with comments | « content/browser/service_worker/embedded_worker_instance.cc ('k') | content/browser/service_worker/service_worker_version.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698