| 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/service_worker_dispatcher_host.h" |    5 #include "content/browser/service_worker/service_worker_dispatcher_host.h" | 
|    6  |    6  | 
|    7 #include <stdint.h> |    7 #include <stdint.h> | 
|    8  |    8  | 
|    9 #include <utility> |    9 #include <utility> | 
|   10  |   10  | 
| (...skipping 105 matching lines...) Expand 10 before | Expand all | Expand 10 after  Loading... | 
|  116  |  116  | 
|  117  protected: |  117  protected: | 
|  118   EmbeddedWorkerTestHelper* helper_; |  118   EmbeddedWorkerTestHelper* helper_; | 
|  119   ~TestingServiceWorkerDispatcherHost() override {} |  119   ~TestingServiceWorkerDispatcherHost() override {} | 
|  120 }; |  120 }; | 
|  121  |  121  | 
|  122 class FailToStartWorkerTestHelper : public EmbeddedWorkerTestHelper { |  122 class FailToStartWorkerTestHelper : public EmbeddedWorkerTestHelper { | 
|  123  public: |  123  public: | 
|  124   FailToStartWorkerTestHelper() : EmbeddedWorkerTestHelper(base::FilePath()) {} |  124   FailToStartWorkerTestHelper() : EmbeddedWorkerTestHelper(base::FilePath()) {} | 
|  125  |  125  | 
|  126   void OnStartWorker(int embedded_worker_id, |  126   void OnStartWorker( | 
|  127                      int64_t service_worker_version_id, |  127       int embedded_worker_id, | 
|  128                      const GURL& scope, |  128       int64_t service_worker_version_id, | 
|  129                      const GURL& script_url, |  129       const GURL& scope, | 
|  130                      bool pause_after_download, |  130       const GURL& script_url, | 
|  131                      mojom::ServiceWorkerEventDispatcherRequest request, |  131       bool pause_after_download, | 
|  132                      mojom::EmbeddedWorkerInstanceHostAssociatedPtrInfo |  132       mojom::ServiceWorkerEventDispatcherRequest request, | 
|  133                          instance_host) override { |  133       mojom::EmbeddedWorkerInstanceHostAssociatedPtrInfo instance_host, | 
 |  134       mojom::ServiceWorkerProviderInfoForStartWorkerPtr provider_info) | 
 |  135       override { | 
|  134     mojom::EmbeddedWorkerInstanceHostAssociatedPtr instance_host_ptr; |  136     mojom::EmbeddedWorkerInstanceHostAssociatedPtr instance_host_ptr; | 
|  135     instance_host_ptr.Bind(std::move(instance_host)); |  137     instance_host_ptr.Bind(std::move(instance_host)); | 
|  136     instance_host_ptr->OnStopped(); |  138     instance_host_ptr->OnStopped(); | 
|  137     base::RunLoop().RunUntilIdle(); |  139     base::RunLoop().RunUntilIdle(); | 
|  138   } |  140   } | 
|  139 }; |  141 }; | 
|  140  |  142  | 
|  141 class ServiceWorkerDispatcherHostTest : public testing::Test { |  143 class ServiceWorkerDispatcherHostTest : public testing::Test { | 
|  142  protected: |  144  protected: | 
|  143   ServiceWorkerDispatcherHostTest() |  145   ServiceWorkerDispatcherHostTest() | 
| (...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after  Loading... | 
|  189     bool called = false; |  191     bool called = false; | 
|  190     ServiceWorkerStatusCode status = SERVICE_WORKER_ERROR_MAX_VALUE; |  192     ServiceWorkerStatusCode status = SERVICE_WORKER_ERROR_MAX_VALUE; | 
|  191     helper_->context()->storage()->StoreRegistration( |  193     helper_->context()->storage()->StoreRegistration( | 
|  192         registration_.get(), version_.get(), |  194         registration_.get(), version_.get(), | 
|  193         base::Bind(&SaveStatusCallback, &called, &status)); |  195         base::Bind(&SaveStatusCallback, &called, &status)); | 
|  194     base::RunLoop().RunUntilIdle(); |  196     base::RunLoop().RunUntilIdle(); | 
|  195     EXPECT_TRUE(called); |  197     EXPECT_TRUE(called); | 
|  196     EXPECT_EQ(SERVICE_WORKER_OK, status); |  198     EXPECT_EQ(SERVICE_WORKER_OK, status); | 
|  197   } |  199   } | 
|  198  |  200  | 
|  199   void SendSetHostedVersionId(int provider_id, |  | 
|  200                               int64_t version_id, |  | 
|  201                               int embedded_worker_id) { |  | 
|  202     dispatcher_host_->OnSetHostedVersionId(provider_id, version_id, |  | 
|  203                                            embedded_worker_id); |  | 
|  204   } |  | 
|  205  |  | 
|  206   void SendProviderCreated(ServiceWorkerProviderType type, |  201   void SendProviderCreated(ServiceWorkerProviderType type, | 
|  207                            const GURL& pattern) { |  202                            const GURL& pattern) { | 
|  208     const int64_t kProviderId = 99; |  203     const int64_t kProviderId = 99; | 
|  209     ServiceWorkerProviderHostInfo info(kProviderId, MSG_ROUTING_NONE, type, |  204     ServiceWorkerProviderHostInfo info(kProviderId, MSG_ROUTING_NONE, type, | 
|  210                                        true /* is_parent_frame_secure */); |  205                                        true /* is_parent_frame_secure */); | 
|  211     remote_endpoint_.BindWithProviderHostInfo(&info); |  206     remote_endpoint_.BindWithProviderHostInfo(&info); | 
|  212  |  207  | 
|  213     dispatcher_host_->OnProviderCreated(std::move(info)); |  208     dispatcher_host_->OnProviderCreated(std::move(info)); | 
|  214     helper_->SimulateAddProcessToPattern(pattern, |  209     helper_->SimulateAddProcessToPattern(pattern, | 
|  215                                          helper_->mock_render_process_id()); |  210                                          helper_->mock_render_process_id()); | 
|  216     provider_host_ = context()->GetProviderHost( |  211     provider_host_ = context()->GetProviderHost( | 
|  217         helper_->mock_render_process_id(), kProviderId); |  212         helper_->mock_render_process_id(), kProviderId); | 
|  218   } |  213   } | 
|  219  |  214  | 
 |  215   void PrepareProviderForServiceWorkerContext(ServiceWorkerVersion* version, | 
 |  216                                               const GURL& pattern) { | 
 |  217     std::unique_ptr<ServiceWorkerProviderHost> host = | 
 |  218         CreateProviderHostForServiceWorkerContext( | 
 |  219             helper_->mock_render_process_id(), | 
 |  220             true /* is_parent_frame_secure */, version, | 
 |  221             helper_->context()->AsWeakPtr(), &remote_endpoint_); | 
 |  222     provider_host_ = host.get(); | 
 |  223     helper_->SimulateAddProcessToPattern(pattern, | 
 |  224                                          helper_->mock_render_process_id()); | 
 |  225     context()->AddProviderHost(std::move(host)); | 
 |  226   } | 
 |  227  | 
|  220   void SendRegister(int64_t provider_id, GURL pattern, GURL worker_url) { |  228   void SendRegister(int64_t provider_id, GURL pattern, GURL worker_url) { | 
|  221     dispatcher_host_->OnMessageReceived( |  229     dispatcher_host_->OnMessageReceived( | 
|  222         ServiceWorkerHostMsg_RegisterServiceWorker( |  230         ServiceWorkerHostMsg_RegisterServiceWorker( | 
|  223             -1, -1, provider_id, pattern, worker_url)); |  231             -1, -1, provider_id, pattern, worker_url)); | 
|  224     base::RunLoop().RunUntilIdle(); |  232     base::RunLoop().RunUntilIdle(); | 
|  225   } |  233   } | 
|  226  |  234  | 
|  227   void Register(int64_t provider_id, |  235   void Register(int64_t provider_id, | 
|  228                 GURL pattern, |  236                 GURL pattern, | 
|  229                 GURL worker_url, |  237                 GURL worker_url, | 
| (...skipping 523 matching lines...) Expand 10 before | Expand all | Expand 10 after  Loading... | 
|  753   ServiceWorkerRemoteProviderEndpoint remote_endpoint; |  761   ServiceWorkerRemoteProviderEndpoint remote_endpoint; | 
|  754   remote_endpoint.BindWithProviderHostInfo(&host_info); |  762   remote_endpoint.BindWithProviderHostInfo(&host_info); | 
|  755   new_dispatcher_host->OnProviderCreated(std::move(host_info)); |  763   new_dispatcher_host->OnProviderCreated(std::move(host_info)); | 
|  756   EXPECT_EQ(0, new_dispatcher_host->bad_messages_received_count_); |  764   EXPECT_EQ(0, new_dispatcher_host->bad_messages_received_count_); | 
|  757 } |  765 } | 
|  758  |  766  | 
|  759 TEST_F(ServiceWorkerDispatcherHostTest, DispatchExtendableMessageEvent) { |  767 TEST_F(ServiceWorkerDispatcherHostTest, DispatchExtendableMessageEvent) { | 
|  760   GURL pattern = GURL("http://www.example.com/"); |  768   GURL pattern = GURL("http://www.example.com/"); | 
|  761   GURL script_url = GURL("http://www.example.com/service_worker.js"); |  769   GURL script_url = GURL("http://www.example.com/service_worker.js"); | 
|  762  |  770  | 
|  763   SendProviderCreated(SERVICE_WORKER_PROVIDER_FOR_CONTROLLER, pattern); |  | 
|  764   SetUpRegistration(pattern, script_url); |  771   SetUpRegistration(pattern, script_url); | 
 |  772   PrepareProviderForServiceWorkerContext(version_.get(), pattern); | 
|  765  |  773  | 
|  766   // Set the running hosted version so that we can retrieve a valid service |  774   // Set the running hosted version so that we can retrieve a valid service | 
|  767   // worker object information for the source attribute of the message event. |  775   // worker object information for the source attribute of the message event. | 
|  768   provider_host_->running_hosted_version_ = version_; |  776   provider_host_->running_hosted_version_ = version_; | 
|  769  |  777  | 
|  770   // Set aside the initial refcount of the worker handle. |  778   // Set aside the initial refcount of the worker handle. | 
|  771   provider_host_->GetOrCreateServiceWorkerHandle(version_.get()); |  779   provider_host_->GetOrCreateServiceWorkerHandle(version_.get()); | 
|  772   ServiceWorkerHandle* sender_worker_handle = |  780   ServiceWorkerHandle* sender_worker_handle = | 
|  773       dispatcher_host_->FindServiceWorkerHandle(provider_host_->provider_id(), |  781       dispatcher_host_->FindServiceWorkerHandle(provider_host_->provider_id(), | 
|  774                                                 version_->version_id()); |  782                                                 version_->version_id()); | 
| (...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after  Loading... | 
|  831   bool called = false; |  839   bool called = false; | 
|  832   ServiceWorkerStatusCode status = SERVICE_WORKER_ERROR_MAX_VALUE; |  840   ServiceWorkerStatusCode status = SERVICE_WORKER_ERROR_MAX_VALUE; | 
|  833   DispatchExtendableMessageEvent( |  841   DispatchExtendableMessageEvent( | 
|  834       version_, base::string16(), url::Origin(version_->scope().GetOrigin()), |  842       version_, base::string16(), url::Origin(version_->scope().GetOrigin()), | 
|  835       ports, provider_host_, base::Bind(&SaveStatusCallback, &called, &status)); |  843       ports, provider_host_, base::Bind(&SaveStatusCallback, &called, &status)); | 
|  836   base::RunLoop().RunUntilIdle(); |  844   base::RunLoop().RunUntilIdle(); | 
|  837   EXPECT_TRUE(called); |  845   EXPECT_TRUE(called); | 
|  838   EXPECT_EQ(SERVICE_WORKER_ERROR_START_WORKER_FAILED, status); |  846   EXPECT_EQ(SERVICE_WORKER_ERROR_START_WORKER_FAILED, status); | 
|  839 } |  847 } | 
|  840  |  848  | 
|  841 TEST_F(ServiceWorkerDispatcherHostTest, OnSetHostedVersionId) { |  | 
|  842   GURL pattern = GURL("http://www.example.com/"); |  | 
|  843   GURL script_url = GURL("http://www.example.com/service_worker.js"); |  | 
|  844  |  | 
|  845   Initialize(base::WrapUnique(new FailToStartWorkerTestHelper)); |  | 
|  846   SendProviderCreated(SERVICE_WORKER_PROVIDER_FOR_CONTROLLER, pattern); |  | 
|  847   SetUpRegistration(pattern, script_url); |  | 
|  848  |  | 
|  849   const int64_t kProviderId = 99;  // Dummy value |  | 
|  850   bool called; |  | 
|  851   ServiceWorkerStatusCode status; |  | 
|  852   // StartWorker puts the worker in STARTING state but it will have no |  | 
|  853   // process id yet. |  | 
|  854   version_->StartWorker(ServiceWorkerMetrics::EventType::UNKNOWN, |  | 
|  855                         base::Bind(&SaveStatusCallback, &called, &status)); |  | 
|  856   EXPECT_NE(version_->embedded_worker()->process_id(), |  | 
|  857             provider_host_->process_id()); |  | 
|  858   // SendSetHostedVersionId should reject because the provider host process id |  | 
|  859   // is different. It should call BadMessageReceived because it's not an |  | 
|  860   // expected error state. |  | 
|  861   SendSetHostedVersionId(kProviderId, version_->version_id(), |  | 
|  862                          version_->embedded_worker()->embedded_worker_id()); |  | 
|  863   base::RunLoop().RunUntilIdle(); |  | 
|  864   EXPECT_FALSE(dispatcher_host_->ipc_sink()->GetUniqueMessageMatching( |  | 
|  865       ServiceWorkerMsg_AssociateRegistration::ID)); |  | 
|  866   EXPECT_EQ(1, dispatcher_host_->bad_messages_received_count_); |  | 
|  867 } |  | 
|  868  |  | 
|  869 TEST_F(ServiceWorkerDispatcherHostTest, OnSetHostedVersionId_DetachedWorker) { |  | 
|  870   GURL pattern = GURL("http://www.example.com/"); |  | 
|  871   GURL script_url = GURL("http://www.example.com/service_worker.js"); |  | 
|  872  |  | 
|  873   Initialize(base::WrapUnique(new FailToStartWorkerTestHelper)); |  | 
|  874   SendProviderCreated(SERVICE_WORKER_PROVIDER_FOR_CONTROLLER, pattern); |  | 
|  875   SetUpRegistration(pattern, script_url); |  | 
|  876  |  | 
|  877   const int64_t kProviderId = 99;  // Dummy value |  | 
|  878   bool called; |  | 
|  879   ServiceWorkerStatusCode status; |  | 
|  880   // StartWorker puts the worker in STARTING state. |  | 
|  881   version_->StartWorker(ServiceWorkerMetrics::EventType::UNKNOWN, |  | 
|  882                         base::Bind(&SaveStatusCallback, &called, &status)); |  | 
|  883  |  | 
|  884   // SendSetHostedVersionId should bail because the embedded worker is |  | 
|  885   // different. It shouldn't call BadMessageReceived because receiving a message |  | 
|  886   // for a detached worker is a legitimite possibility. |  | 
|  887   int bad_embedded_worker_id = |  | 
|  888       version_->embedded_worker()->embedded_worker_id() + 1; |  | 
|  889   SendSetHostedVersionId(kProviderId, version_->version_id(), |  | 
|  890                          bad_embedded_worker_id); |  | 
|  891   base::RunLoop().RunUntilIdle(); |  | 
|  892   EXPECT_FALSE(dispatcher_host_->ipc_sink()->GetUniqueMessageMatching( |  | 
|  893       ServiceWorkerMsg_AssociateRegistration::ID)); |  | 
|  894   EXPECT_EQ(0, dispatcher_host_->bad_messages_received_count_); |  | 
|  895 } |  | 
|  896  |  | 
|  897 TEST_F(ServiceWorkerDispatcherHostTest, ReceivedTimedOutRequestResponse) { |  849 TEST_F(ServiceWorkerDispatcherHostTest, ReceivedTimedOutRequestResponse) { | 
|  898   GURL pattern = GURL("https://www.example.com/"); |  850   GURL pattern = GURL("https://www.example.com/"); | 
|  899   GURL script_url = GURL("https://www.example.com/service_worker.js"); |  851   GURL script_url = GURL("https://www.example.com/service_worker.js"); | 
|  900  |  852  | 
|  901   SendProviderCreated(SERVICE_WORKER_PROVIDER_FOR_WINDOW, pattern); |  853   SendProviderCreated(SERVICE_WORKER_PROVIDER_FOR_WINDOW, pattern); | 
|  902   SetUpRegistration(pattern, script_url); |  854   SetUpRegistration(pattern, script_url); | 
|  903  |  855  | 
|  904   version_->StartWorker(ServiceWorkerMetrics::EventType::UNKNOWN, |  856   version_->StartWorker(ServiceWorkerMetrics::EventType::UNKNOWN, | 
|  905                         base::Bind(&ServiceWorkerUtils::NoOpStatusCallback)); |  857                         base::Bind(&ServiceWorkerUtils::NoOpStatusCallback)); | 
|  906   base::RunLoop().RunUntilIdle(); |  858   base::RunLoop().RunUntilIdle(); | 
|  907  |  859  | 
|  908   // Set the worker status to STOPPING. |  860   // Set the worker status to STOPPING. | 
|  909   version_->embedded_worker()->Stop(); |  861   version_->embedded_worker()->Stop(); | 
|  910   EXPECT_EQ(EmbeddedWorkerStatus::STOPPING, version_->running_status()); |  862   EXPECT_EQ(EmbeddedWorkerStatus::STOPPING, version_->running_status()); | 
|  911  |  863  | 
|  912   // Receive a response for a timed out request. The bad message count should |  864   // Receive a response for a timed out request. The bad message count should | 
|  913   // not increase. |  865   // not increase. | 
|  914   const int kFetchEventId = 91;  // Dummy value |  866   const int kFetchEventId = 91;  // Dummy value | 
|  915   dispatcher_host_->OnMessageReceived(ServiceWorkerHostMsg_FetchEventResponse( |  867   dispatcher_host_->OnMessageReceived(ServiceWorkerHostMsg_FetchEventResponse( | 
|  916       version_->embedded_worker()->embedded_worker_id(), kFetchEventId, |  868       version_->embedded_worker()->embedded_worker_id(), kFetchEventId, | 
|  917       ServiceWorkerResponse(), base::Time::Now())); |  869       ServiceWorkerResponse(), base::Time::Now())); | 
|  918  |  870  | 
|  919   base::RunLoop().RunUntilIdle(); |  871   base::RunLoop().RunUntilIdle(); | 
|  920   EXPECT_EQ(0, dispatcher_host_->bad_messages_received_count_); |  872   EXPECT_EQ(0, dispatcher_host_->bad_messages_received_count_); | 
|  921 } |  873 } | 
|  922  |  874  | 
|  923 }  // namespace content |  875 }  // namespace content | 
| OLD | NEW |