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

Unified Diff: content/browser/service_worker/service_worker_dispatcher_host_unittest.cc

Issue 2958753003: Revert "Create ServiceWorkerProviderHost before starting worker" (Closed)
Patch Set: Created 3 years, 6 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 side-by-side diff with in-line comments
Download patch
Index: content/browser/service_worker/service_worker_dispatcher_host_unittest.cc
diff --git a/content/browser/service_worker/service_worker_dispatcher_host_unittest.cc b/content/browser/service_worker/service_worker_dispatcher_host_unittest.cc
index e5f0d75f5f0a460f50ae418d579d01557091c6f1..e2debc26b2ec9973ca0b7dc5c83d157bcc7c40c4 100644
--- a/content/browser/service_worker/service_worker_dispatcher_host_unittest.cc
+++ b/content/browser/service_worker/service_worker_dispatcher_host_unittest.cc
@@ -123,16 +123,14 @@ class FailToStartWorkerTestHelper : public EmbeddedWorkerTestHelper {
public:
FailToStartWorkerTestHelper() : EmbeddedWorkerTestHelper(base::FilePath()) {}
- void OnStartWorker(
- int embedded_worker_id,
- int64_t service_worker_version_id,
- const GURL& scope,
- const GURL& script_url,
- bool pause_after_download,
- mojom::ServiceWorkerEventDispatcherRequest request,
- mojom::EmbeddedWorkerInstanceHostAssociatedPtrInfo instance_host,
- mojom::ServiceWorkerProviderInfoForStartWorkerPtr provider_info)
- override {
+ void OnStartWorker(int embedded_worker_id,
+ int64_t service_worker_version_id,
+ const GURL& scope,
+ const GURL& script_url,
+ bool pause_after_download,
+ mojom::ServiceWorkerEventDispatcherRequest request,
+ mojom::EmbeddedWorkerInstanceHostAssociatedPtrInfo
+ instance_host) override {
mojom::EmbeddedWorkerInstanceHostAssociatedPtr instance_host_ptr;
instance_host_ptr.Bind(std::move(instance_host));
instance_host_ptr->OnStopped();
@@ -198,6 +196,13 @@ class ServiceWorkerDispatcherHostTest : public testing::Test {
EXPECT_EQ(SERVICE_WORKER_OK, status);
}
+ void SendSetHostedVersionId(int provider_id,
+ int64_t version_id,
+ int embedded_worker_id) {
+ dispatcher_host_->OnSetHostedVersionId(provider_id, version_id,
+ embedded_worker_id);
+ }
+
void SendProviderCreated(ServiceWorkerProviderType type,
const GURL& pattern) {
const int64_t kProviderId = 99;
@@ -212,19 +217,6 @@ class ServiceWorkerDispatcherHostTest : public testing::Test {
helper_->mock_render_process_id(), kProviderId);
}
- void PrepareProviderForServiceWorkerContext(ServiceWorkerVersion* version,
- const GURL& pattern) {
- std::unique_ptr<ServiceWorkerProviderHost> host =
- CreateProviderHostForServiceWorkerContext(
- helper_->mock_render_process_id(),
- true /* is_parent_frame_secure */, version,
- helper_->context()->AsWeakPtr(), &remote_endpoint_);
- provider_host_ = host.get();
- helper_->SimulateAddProcessToPattern(pattern,
- helper_->mock_render_process_id());
- context()->AddProviderHost(std::move(host));
- }
-
void SendRegister(int64_t provider_id, GURL pattern, GURL worker_url) {
dispatcher_host_->OnMessageReceived(
ServiceWorkerHostMsg_RegisterServiceWorker(
@@ -768,8 +760,8 @@ TEST_F(ServiceWorkerDispatcherHostTest, DispatchExtendableMessageEvent) {
GURL pattern = GURL("http://www.example.com/");
GURL script_url = GURL("http://www.example.com/service_worker.js");
+ SendProviderCreated(SERVICE_WORKER_PROVIDER_FOR_CONTROLLER, pattern);
SetUpRegistration(pattern, script_url);
- PrepareProviderForServiceWorkerContext(version_.get(), pattern);
// Set the running hosted version so that we can retrieve a valid service
// worker object information for the source attribute of the message event.
@@ -846,6 +838,62 @@ TEST_F(ServiceWorkerDispatcherHostTest, DispatchExtendableMessageEvent_Fail) {
EXPECT_EQ(SERVICE_WORKER_ERROR_START_WORKER_FAILED, status);
}
+TEST_F(ServiceWorkerDispatcherHostTest, OnSetHostedVersionId) {
+ GURL pattern = GURL("http://www.example.com/");
+ GURL script_url = GURL("http://www.example.com/service_worker.js");
+
+ Initialize(base::WrapUnique(new FailToStartWorkerTestHelper));
+ SendProviderCreated(SERVICE_WORKER_PROVIDER_FOR_CONTROLLER, pattern);
+ SetUpRegistration(pattern, script_url);
+
+ const int64_t kProviderId = 99; // Dummy value
+ bool called;
+ ServiceWorkerStatusCode status;
+ // StartWorker puts the worker in STARTING state but it will have no
+ // process id yet.
+ version_->StartWorker(ServiceWorkerMetrics::EventType::UNKNOWN,
+ base::Bind(&SaveStatusCallback, &called, &status));
+ EXPECT_NE(version_->embedded_worker()->process_id(),
+ provider_host_->process_id());
+ // SendSetHostedVersionId should reject because the provider host process id
+ // is different. It should call BadMessageReceived because it's not an
+ // expected error state.
+ SendSetHostedVersionId(kProviderId, version_->version_id(),
+ version_->embedded_worker()->embedded_worker_id());
+ base::RunLoop().RunUntilIdle();
+ EXPECT_FALSE(dispatcher_host_->ipc_sink()->GetUniqueMessageMatching(
+ ServiceWorkerMsg_AssociateRegistration::ID));
+ EXPECT_EQ(1, dispatcher_host_->bad_messages_received_count_);
+}
+
+TEST_F(ServiceWorkerDispatcherHostTest, OnSetHostedVersionId_DetachedWorker) {
+ GURL pattern = GURL("http://www.example.com/");
+ GURL script_url = GURL("http://www.example.com/service_worker.js");
+
+ Initialize(base::WrapUnique(new FailToStartWorkerTestHelper));
+ SendProviderCreated(SERVICE_WORKER_PROVIDER_FOR_CONTROLLER, pattern);
+ SetUpRegistration(pattern, script_url);
+
+ const int64_t kProviderId = 99; // Dummy value
+ bool called;
+ ServiceWorkerStatusCode status;
+ // StartWorker puts the worker in STARTING state.
+ version_->StartWorker(ServiceWorkerMetrics::EventType::UNKNOWN,
+ base::Bind(&SaveStatusCallback, &called, &status));
+
+ // SendSetHostedVersionId should bail because the embedded worker is
+ // different. It shouldn't call BadMessageReceived because receiving a message
+ // for a detached worker is a legitimite possibility.
+ int bad_embedded_worker_id =
+ version_->embedded_worker()->embedded_worker_id() + 1;
+ SendSetHostedVersionId(kProviderId, version_->version_id(),
+ bad_embedded_worker_id);
+ base::RunLoop().RunUntilIdle();
+ EXPECT_FALSE(dispatcher_host_->ipc_sink()->GetUniqueMessageMatching(
+ ServiceWorkerMsg_AssociateRegistration::ID));
+ EXPECT_EQ(0, dispatcher_host_->bad_messages_received_count_);
+}
+
TEST_F(ServiceWorkerDispatcherHostTest, ReceivedTimedOutRequestResponse) {
GURL pattern = GURL("https://www.example.com/");
GURL script_url = GURL("https://www.example.com/service_worker.js");

Powered by Google App Engine
This is Rietveld 408576698