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

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

Issue 625533002: Respect content settings for Service Worker registration (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@testing
Patch Set: sync Created 6 years, 2 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 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 "base/command_line.h" 7 #include "base/command_line.h"
8 #include "base/files/file_path.h" 8 #include "base/files/file_path.h"
9 #include "base/run_loop.h" 9 #include "base/run_loop.h"
10 #include "content/browser/browser_thread_impl.h" 10 #include "content/browser/browser_thread_impl.h"
11 #include "content/browser/service_worker/embedded_worker_instance.h" 11 #include "content/browser/service_worker/embedded_worker_instance.h"
12 #include "content/browser/service_worker/embedded_worker_registry.h" 12 #include "content/browser/service_worker/embedded_worker_registry.h"
13 #include "content/browser/service_worker/embedded_worker_test_helper.h" 13 #include "content/browser/service_worker/embedded_worker_test_helper.h"
14 #include "content/browser/service_worker/service_worker_context_core.h" 14 #include "content/browser/service_worker/service_worker_context_core.h"
15 #include "content/browser/service_worker/service_worker_context_wrapper.h" 15 #include "content/browser/service_worker/service_worker_context_wrapper.h"
16 #include "content/common/service_worker/embedded_worker_messages.h" 16 #include "content/common/service_worker/embedded_worker_messages.h"
17 #include "content/common/service_worker/service_worker_messages.h" 17 #include "content/common/service_worker/service_worker_messages.h"
18 #include "content/public/common/content_switches.h" 18 #include "content/public/common/content_switches.h"
19 #include "content/public/test/mock_resource_context.h"
19 #include "content/public/test/test_browser_thread_bundle.h" 20 #include "content/public/test/test_browser_thread_bundle.h"
21 #include "content/test/test_content_browser_client.h"
20 #include "testing/gtest/include/gtest/gtest.h" 22 #include "testing/gtest/include/gtest/gtest.h"
21 23
22 namespace content { 24 namespace content {
23 25
24 static const int kRenderProcessId = 1; 26 static const int kRenderProcessId = 1;
25 27
26 class TestingServiceWorkerDispatcherHost : public ServiceWorkerDispatcherHost { 28 class TestingServiceWorkerDispatcherHost : public ServiceWorkerDispatcherHost {
27 public: 29 public:
28 TestingServiceWorkerDispatcherHost( 30 TestingServiceWorkerDispatcherHost(
29 int process_id, 31 int process_id,
30 ServiceWorkerContextWrapper* context_wrapper, 32 ServiceWorkerContextWrapper* context_wrapper,
33 ResourceContext* resource_context,
31 EmbeddedWorkerTestHelper* helper) 34 EmbeddedWorkerTestHelper* helper)
32 : ServiceWorkerDispatcherHost(process_id, NULL), 35 : ServiceWorkerDispatcherHost(process_id, NULL, resource_context),
33 bad_messages_received_count_(0), 36 bad_messages_received_count_(0),
34 helper_(helper) { 37 helper_(helper) {
35 Init(context_wrapper); 38 Init(context_wrapper);
36 } 39 }
37 40
38 virtual bool Send(IPC::Message* message) OVERRIDE { 41 virtual bool Send(IPC::Message* message) OVERRIDE {
39 return helper_->Send(message); 42 return helper_->Send(message);
40 } 43 }
41 44
42 IPC::TestSink* ipc_sink() { return helper_->ipc_sink(); } 45 IPC::TestSink* ipc_sink() { return helper_->ipc_sink(); }
(...skipping 10 matching lines...) Expand all
53 }; 56 };
54 57
55 class ServiceWorkerDispatcherHostTest : public testing::Test { 58 class ServiceWorkerDispatcherHostTest : public testing::Test {
56 protected: 59 protected:
57 ServiceWorkerDispatcherHostTest() 60 ServiceWorkerDispatcherHostTest()
58 : browser_thread_bundle_(TestBrowserThreadBundle::IO_MAINLOOP) {} 61 : browser_thread_bundle_(TestBrowserThreadBundle::IO_MAINLOOP) {}
59 62
60 virtual void SetUp() { 63 virtual void SetUp() {
61 helper_.reset(new EmbeddedWorkerTestHelper(kRenderProcessId)); 64 helper_.reset(new EmbeddedWorkerTestHelper(kRenderProcessId));
62 dispatcher_host_ = new TestingServiceWorkerDispatcherHost( 65 dispatcher_host_ = new TestingServiceWorkerDispatcherHost(
63 kRenderProcessId, context_wrapper(), helper_.get()); 66 kRenderProcessId, context_wrapper(), &resource_context_, helper_.get());
64 } 67 }
65 68
66 virtual void TearDown() { 69 virtual void TearDown() {
67 helper_.reset(); 70 helper_.reset();
68 } 71 }
69 72
70 ServiceWorkerContextCore* context() { return helper_->context(); } 73 ServiceWorkerContextCore* context() { return helper_->context(); }
71 ServiceWorkerContextWrapper* context_wrapper() { 74 ServiceWorkerContextWrapper* context_wrapper() {
72 return helper_->context_wrapper(); 75 return helper_->context_wrapper();
73 } 76 }
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after
113 void GetRegistration(int64 provider_id, 116 void GetRegistration(int64 provider_id,
114 GURL document_url, 117 GURL document_url,
115 uint32 expected_message) { 118 uint32 expected_message) {
116 SendGetRegistration(provider_id, document_url); 119 SendGetRegistration(provider_id, document_url);
117 EXPECT_TRUE(dispatcher_host_->ipc_sink()->GetUniqueMessageMatching( 120 EXPECT_TRUE(dispatcher_host_->ipc_sink()->GetUniqueMessageMatching(
118 expected_message)); 121 expected_message));
119 dispatcher_host_->ipc_sink()->ClearMessages(); 122 dispatcher_host_->ipc_sink()->ClearMessages();
120 } 123 }
121 124
122 TestBrowserThreadBundle browser_thread_bundle_; 125 TestBrowserThreadBundle browser_thread_bundle_;
126 content::MockResourceContext resource_context_;
123 scoped_ptr<EmbeddedWorkerTestHelper> helper_; 127 scoped_ptr<EmbeddedWorkerTestHelper> helper_;
124 scoped_refptr<TestingServiceWorkerDispatcherHost> dispatcher_host_; 128 scoped_refptr<TestingServiceWorkerDispatcherHost> dispatcher_host_;
125 }; 129 };
126 130
131 class ServiceWorkerTestContentBrowserClient : public TestContentBrowserClient {
132 public:
133 ServiceWorkerTestContentBrowserClient() {}
134 virtual bool AllowServiceWorker(const GURL& scope,
135 const GURL& first_party,
136 content::ResourceContext* context) OVERRIDE {
137 return false;
138 }
139 };
140
141 TEST_F(ServiceWorkerDispatcherHostTest,
142 Register_ContentSettingsDisallowsServiceWorker) {
143 ServiceWorkerTestContentBrowserClient test_browser_client;
144 ContentBrowserClient* old_browser_client =
145 SetBrowserClientForTesting(&test_browser_client);
146
147 const int64 kProviderId = 99; // Dummy value
148 scoped_ptr<ServiceWorkerProviderHost> host(new ServiceWorkerProviderHost(
149 kRenderProcessId, kProviderId, context()->AsWeakPtr(), NULL));
150 host->SetDocumentUrl(GURL("https://www.example.com/foo"));
151 context()->AddProviderHost(host.Pass());
152
153 Register(kProviderId,
154 GURL("https://www.example.com/"),
155 GURL("https://www.example.com/bar"),
156 ServiceWorkerMsg_ServiceWorkerRegistrationError::ID);
157 Unregister(kProviderId,
158 GURL("https://www.example.com/"),
159 ServiceWorkerMsg_ServiceWorkerUnregistrationError::ID);
160 GetRegistration(kProviderId,
161 GURL("https://www.example.com/"),
162 ServiceWorkerMsg_ServiceWorkerGetRegistrationError::ID);
163
164 SetBrowserClientForTesting(old_browser_client);
165 }
166
127 TEST_F(ServiceWorkerDispatcherHostTest, Register_HTTPS) { 167 TEST_F(ServiceWorkerDispatcherHostTest, Register_HTTPS) {
128 const int64 kProviderId = 99; // Dummy value 168 const int64 kProviderId = 99; // Dummy value
129 scoped_ptr<ServiceWorkerProviderHost> host(new ServiceWorkerProviderHost( 169 scoped_ptr<ServiceWorkerProviderHost> host(new ServiceWorkerProviderHost(
130 kRenderProcessId, kProviderId, context()->AsWeakPtr(), NULL)); 170 kRenderProcessId, kProviderId, context()->AsWeakPtr(), NULL));
131 host->SetDocumentUrl(GURL("https://www.example.com/foo")); 171 host->SetDocumentUrl(GURL("https://www.example.com/foo"));
132 context()->AddProviderHost(host.Pass()); 172 context()->AddProviderHost(host.Pass());
133 173
134 Register(kProviderId, 174 Register(kProviderId,
135 GURL("https://www.example.com/"), 175 GURL("https://www.example.com/"),
136 GURL("https://www.example.com/bar"), 176 GURL("https://www.example.com/bar"),
(...skipping 200 matching lines...) Expand 10 before | Expand all | Expand 10 after
337 377
338 // Let the shutdown reach the simulated IO thread. 378 // Let the shutdown reach the simulated IO thread.
339 base::RunLoop().RunUntilIdle(); 379 base::RunLoop().RunUntilIdle();
340 380
341 GetRegistration(-1, 381 GetRegistration(-1,
342 GURL(), 382 GURL(),
343 ServiceWorkerMsg_ServiceWorkerGetRegistrationError::ID); 383 ServiceWorkerMsg_ServiceWorkerGetRegistrationError::ID);
344 } 384 }
345 385
346 } // namespace content 386 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698