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 "content/browser/service_worker/service_worker_provider_host.h" | 5 #include "content/browser/service_worker/service_worker_provider_host.h" |
| 6 | 6 |
| 7 #include "base/macros.h" | 7 #include "base/macros.h" |
| 8 #include "base/memory/ptr_util.h" | 8 #include "base/memory/ptr_util.h" |
| 9 #include "base/memory/weak_ptr.h" | 9 #include "base/memory/weak_ptr.h" |
| 10 #include "base/threading/thread_task_runner_handle.h" | 10 #include "base/threading/thread_task_runner_handle.h" |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 42 old_content_browser_client_ = | 42 old_content_browser_client_ = |
| 43 SetBrowserClientForTesting(&test_content_browser_client_); | 43 SetBrowserClientForTesting(&test_content_browser_client_); |
| 44 | 44 |
| 45 helper_.reset(new EmbeddedWorkerTestHelper(base::FilePath())); | 45 helper_.reset(new EmbeddedWorkerTestHelper(base::FilePath())); |
| 46 context_ = helper_->context(); | 46 context_ = helper_->context(); |
| 47 script_url_ = GURL("https://www.example.com/service_worker.js"); | 47 script_url_ = GURL("https://www.example.com/service_worker.js"); |
| 48 registration1_ = new ServiceWorkerRegistration( | 48 registration1_ = new ServiceWorkerRegistration( |
| 49 GURL("https://www.example.com/"), 1L, context_->AsWeakPtr()); | 49 GURL("https://www.example.com/"), 1L, context_->AsWeakPtr()); |
| 50 registration2_ = new ServiceWorkerRegistration( | 50 registration2_ = new ServiceWorkerRegistration( |
| 51 GURL("https://www.example.com/example"), 2L, context_->AsWeakPtr()); | 51 GURL("https://www.example.com/example"), 2L, context_->AsWeakPtr()); |
| 52 registration3_ = new ServiceWorkerRegistration( | |
| 53 GURL("https://other.example.com/"), 3L, context_->AsWeakPtr()); | |
| 52 | 54 |
| 53 // Prepare provider hosts (for the same process). | 55 // Prepare provider hosts (for the same process). |
| 54 std::unique_ptr<ServiceWorkerProviderHost> host1( | 56 std::unique_ptr<ServiceWorkerProviderHost> host1( |
| 55 new ServiceWorkerProviderHost( | 57 new ServiceWorkerProviderHost( |
| 56 helper_->mock_render_process_id(), MSG_ROUTING_NONE, | 58 helper_->mock_render_process_id(), MSG_ROUTING_NONE, |
| 57 1 /* provider_id */, SERVICE_WORKER_PROVIDER_FOR_WINDOW, | 59 1 /* provider_id */, SERVICE_WORKER_PROVIDER_FOR_WINDOW, |
| 58 ServiceWorkerProviderHost::FrameSecurityLevel::SECURE, | 60 ServiceWorkerProviderHost::FrameSecurityLevel::SECURE, |
| 59 context_->AsWeakPtr(), NULL)); | 61 context_->AsWeakPtr(), NULL)); |
| 60 host1->SetDocumentUrl(GURL("https://www.example.com/example1.html")); | 62 host1->SetDocumentUrl(GURL("https://www.example.com/example1.html")); |
| 61 std::unique_ptr<ServiceWorkerProviderHost> host2( | 63 std::unique_ptr<ServiceWorkerProviderHost> host2( |
| (...skipping 18 matching lines...) Expand all Loading... | |
| 80 | 82 |
| 81 bool PatternHasProcessToRun(const GURL& pattern) const { | 83 bool PatternHasProcessToRun(const GURL& pattern) const { |
| 82 return context_->process_manager()->PatternHasProcessToRun(pattern); | 84 return context_->process_manager()->PatternHasProcessToRun(pattern); |
| 83 } | 85 } |
| 84 | 86 |
| 85 content::TestBrowserThreadBundle thread_bundle_; | 87 content::TestBrowserThreadBundle thread_bundle_; |
| 86 std::unique_ptr<EmbeddedWorkerTestHelper> helper_; | 88 std::unique_ptr<EmbeddedWorkerTestHelper> helper_; |
| 87 ServiceWorkerContextCore* context_; | 89 ServiceWorkerContextCore* context_; |
| 88 scoped_refptr<ServiceWorkerRegistration> registration1_; | 90 scoped_refptr<ServiceWorkerRegistration> registration1_; |
| 89 scoped_refptr<ServiceWorkerRegistration> registration2_; | 91 scoped_refptr<ServiceWorkerRegistration> registration2_; |
| 92 scoped_refptr<ServiceWorkerRegistration> registration3_; | |
| 90 base::WeakPtr<ServiceWorkerProviderHost> provider_host1_; | 93 base::WeakPtr<ServiceWorkerProviderHost> provider_host1_; |
| 91 base::WeakPtr<ServiceWorkerProviderHost> provider_host2_; | 94 base::WeakPtr<ServiceWorkerProviderHost> provider_host2_; |
| 92 GURL script_url_; | 95 GURL script_url_; |
| 93 ServiceWorkerTestContentClient test_content_client_; | 96 ServiceWorkerTestContentClient test_content_client_; |
| 94 TestContentBrowserClient test_content_browser_client_; | 97 TestContentBrowserClient test_content_browser_client_; |
| 95 ContentBrowserClient* old_content_browser_client_; | 98 ContentBrowserClient* old_content_browser_client_; |
| 96 | 99 |
| 97 private: | 100 private: |
| 98 DISALLOW_COPY_AND_ASSIGN(ServiceWorkerProviderHostTest); | 101 DISALLOW_COPY_AND_ASSIGN(ServiceWorkerProviderHostTest); |
| 99 }; | 102 }; |
| 100 | 103 |
| 101 TEST_F(ServiceWorkerProviderHostTest, PotentialRegistration_ProcessStatus) { | 104 TEST_F(ServiceWorkerProviderHostTest, PotentialRegistration_ProcessStatus) { |
| 102 provider_host1_->AddMatchingRegistration(registration1_.get()); | 105 // Matching registrations have already been set by SetDocumentUrl. |
| 103 ASSERT_TRUE(PatternHasProcessToRun(registration1_->pattern())); | 106 ASSERT_TRUE(PatternHasProcessToRun(registration1_->pattern())); |
| 107 // Different matching registrations have already been added. | |
| 108 ASSERT_TRUE(PatternHasProcessToRun(registration2_->pattern())); | |
| 104 | 109 |
| 105 // Adding the same registration twice has no effect. | 110 // Adding the same registration twice has no effect. |
| 106 provider_host1_->AddMatchingRegistration(registration1_.get()); | 111 provider_host1_->AddMatchingRegistration(registration1_.get()); |
| 107 ASSERT_TRUE(PatternHasProcessToRun(registration1_->pattern())); | 112 ASSERT_TRUE(PatternHasProcessToRun(registration1_->pattern())); |
| 108 | 113 |
| 109 // Different matching registrations can be added. | |
| 110 provider_host1_->AddMatchingRegistration(registration2_.get()); | |
| 111 ASSERT_TRUE(PatternHasProcessToRun(registration2_->pattern())); | |
| 112 | |
| 113 // Removing a matching registration will decrease the process refs for its | 114 // Removing a matching registration will decrease the process refs for its |
| 114 // pattern. | 115 // pattern. |
| 115 provider_host1_->RemoveMatchingRegistration(registration1_.get()); | 116 provider_host1_->RemoveMatchingRegistration(registration1_.get()); |
| 117 ASSERT_TRUE(PatternHasProcessToRun(registration1_->pattern())); | |
| 118 provider_host2_->RemoveMatchingRegistration(registration1_.get()); | |
| 116 ASSERT_FALSE(PatternHasProcessToRun(registration1_->pattern())); | 119 ASSERT_FALSE(PatternHasProcessToRun(registration1_->pattern())); |
| 117 | 120 |
| 118 // Multiple provider hosts could add the same matching registration. | 121 // Matching registration will be removed when moving out of scope |
| 119 // The process refs will become 0 after all provider hosts removed them. | 122 ASSERT_TRUE(PatternHasProcessToRun(registration2_->pattern())); // host1,2 |
| 120 provider_host2_->AddMatchingRegistration(registration2_.get()); | 123 ASSERT_FALSE(PatternHasProcessToRun(registration3_->pattern())); // no host |
| 121 provider_host1_->RemoveMatchingRegistration(registration2_.get()); | 124 provider_host1_->SetDocumentUrl(GURL("https://other.example.com/")); |
| 122 ASSERT_TRUE(PatternHasProcessToRun(registration2_->pattern())); | 125 ASSERT_TRUE(PatternHasProcessToRun(registration2_->pattern())); // host2 |
| 123 provider_host2_->RemoveMatchingRegistration(registration2_.get()); | 126 ASSERT_TRUE(PatternHasProcessToRun(registration3_->pattern())); // host1 |
| 124 ASSERT_FALSE(PatternHasProcessToRun(registration2_->pattern())); | 127 provider_host2_->SetDocumentUrl(GURL("https://other.example.com/")); |
| 128 ASSERT_FALSE(PatternHasProcessToRun(registration2_->pattern())); // no host | |
| 129 ASSERT_TRUE(PatternHasProcessToRun(registration3_->pattern())); // host1,2 | |
| 125 } | 130 } |
| 126 | 131 |
| 127 TEST_F(ServiceWorkerProviderHostTest, AssociatedRegistration_ProcessStatus) { | 132 TEST_F(ServiceWorkerProviderHostTest, AssociatedRegistration_ProcessStatus) { |
| 128 // Associating the registration will also increase the process refs for | 133 // Associating the registration will also increase the process refs for |
| 129 // the registration's pattern. | 134 // the registration's pattern. |
| 130 provider_host1_->AssociateRegistration(registration1_.get(), | 135 provider_host1_->AssociateRegistration(registration1_.get(), |
| 131 false /* notify_controllerchange */); | 136 false /* notify_controllerchange */); |
| 132 ASSERT_TRUE(PatternHasProcessToRun(registration1_->pattern())); | 137 ASSERT_TRUE(PatternHasProcessToRun(registration1_->pattern())); |
| 133 | 138 |
| 134 // Disassociating the registration shouldn't affect the process refs for | 139 // Disassociating the registration shouldn't affect the process refs for |
| 135 // the registration's pattern. | 140 // the registration's pattern. |
| 136 provider_host1_->DisassociateRegistration(); | 141 provider_host1_->DisassociateRegistration(); |
| 137 ASSERT_TRUE(PatternHasProcessToRun(registration1_->pattern())); | 142 ASSERT_TRUE(PatternHasProcessToRun(registration1_->pattern())); |
| 138 } | 143 } |
| 139 | 144 |
| 140 TEST_F(ServiceWorkerProviderHostTest, MatchRegistration) { | 145 TEST_F(ServiceWorkerProviderHostTest, MatchRegistration) { |
| 141 provider_host1_->AddMatchingRegistration(registration1_.get()); | |
| 142 provider_host1_->AddMatchingRegistration(registration2_.get()); | |
| 143 | |
| 144 // Match registration should return the longest matching one. | 146 // Match registration should return the longest matching one. |
| 145 ASSERT_EQ(provider_host1_->MatchRegistration(), registration2_); | 147 ASSERT_EQ(provider_host1_->MatchRegistration(), registration2_); |
| 146 provider_host1_->RemoveMatchingRegistration(registration2_.get()); | 148 provider_host1_->RemoveMatchingRegistration(registration2_.get()); |
| 147 ASSERT_EQ(provider_host1_->MatchRegistration(), registration1_); | 149 ASSERT_EQ(provider_host1_->MatchRegistration(), registration1_); |
| 148 | 150 |
| 149 // Should return nullptr after removing all matching registrations. | 151 // Should return nullptr after removing all matching registrations. |
| 150 provider_host1_->RemoveMatchingRegistration(registration1_.get()); | 152 provider_host1_->RemoveMatchingRegistration(registration1_.get()); |
| 151 ASSERT_EQ(provider_host1_->MatchRegistration(), nullptr); | 153 ASSERT_EQ(provider_host1_->MatchRegistration(), nullptr); |
| 154 | |
| 155 // SetDocumentUrl sets all of matching registrations | |
| 156 provider_host1_->SetDocumentUrl(GURL("https://www.example.com/example1")); | |
| 157 ASSERT_EQ(provider_host1_->MatchRegistration(), registration2_); | |
|
nhiroki
2016/08/19 05:26:51
An expected value should be placed at left-side (t
shimazu
2016/08/19 09:43:15
Done.
| |
| 158 provider_host1_->RemoveMatchingRegistration(registration2_.get()); | |
| 159 ASSERT_EQ(provider_host1_->MatchRegistration(), registration1_); | |
| 160 | |
| 161 // SetDocumentUrl with another origin also updates matching registrations | |
| 162 provider_host1_->SetDocumentUrl(GURL("https://other.example.com/example")); | |
| 163 ASSERT_EQ(provider_host1_->MatchRegistration(), registration3_); | |
| 164 provider_host1_->RemoveMatchingRegistration(registration3_.get()); | |
| 165 ASSERT_EQ(provider_host1_->MatchRegistration(), nullptr); | |
| 152 } | 166 } |
| 153 | 167 |
| 154 TEST_F(ServiceWorkerProviderHostTest, ContextSecurity) { | 168 TEST_F(ServiceWorkerProviderHostTest, ContextSecurity) { |
| 155 using FrameSecurityLevel = ServiceWorkerProviderHost::FrameSecurityLevel; | 169 using FrameSecurityLevel = ServiceWorkerProviderHost::FrameSecurityLevel; |
| 156 content::ResetSchemesAndOriginsWhitelistForTesting(); | 170 content::ResetSchemesAndOriginsWhitelistForTesting(); |
| 157 | 171 |
| 158 // Insecure document URL. | 172 // Insecure document URL. |
| 159 provider_host1_->SetDocumentUrl(GURL("http://host")); | 173 provider_host1_->SetDocumentUrl(GURL("http://host")); |
| 160 provider_host1_->parent_frame_security_level_ = FrameSecurityLevel::SECURE; | 174 provider_host1_->parent_frame_security_level_ = FrameSecurityLevel::SECURE; |
| 161 EXPECT_FALSE(provider_host1_->IsContextSecureForServiceWorker()); | 175 EXPECT_FALSE(provider_host1_->IsContextSecureForServiceWorker()); |
| (...skipping 16 matching lines...) Expand all Loading... | |
| 178 EXPECT_FALSE(IsOriginSecure(url)); | 192 EXPECT_FALSE(IsOriginSecure(url)); |
| 179 EXPECT_TRUE(OriginCanAccessServiceWorkers(url)); | 193 EXPECT_TRUE(OriginCanAccessServiceWorkers(url)); |
| 180 EXPECT_TRUE(provider_host1_->IsContextSecureForServiceWorker()); | 194 EXPECT_TRUE(provider_host1_->IsContextSecureForServiceWorker()); |
| 181 | 195 |
| 182 // Exceptional service worker scheme with insecure parent frame. | 196 // Exceptional service worker scheme with insecure parent frame. |
| 183 provider_host1_->parent_frame_security_level_ = FrameSecurityLevel::INSECURE; | 197 provider_host1_->parent_frame_security_level_ = FrameSecurityLevel::INSECURE; |
| 184 EXPECT_FALSE(provider_host1_->IsContextSecureForServiceWorker()); | 198 EXPECT_FALSE(provider_host1_->IsContextSecureForServiceWorker()); |
| 185 } | 199 } |
| 186 | 200 |
| 187 } // namespace content | 201 } // namespace content |
| OLD | NEW |