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 "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" |
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/service_worker_context_core.h" | 13 #include "content/browser/service_worker/service_worker_context_core.h" |
14 #include "content/browser/service_worker/service_worker_context_observer.h" | 14 #include "content/browser/service_worker/service_worker_context_observer.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/browser/service_worker/service_worker_registration.h" | 16 #include "content/browser/service_worker/service_worker_registration.h" |
17 #include "content/browser/service_worker/service_worker_test_utils.h" | 17 #include "content/browser/service_worker/service_worker_test_utils.h" |
18 #include "content/browser/service_worker/service_worker_version.h" | 18 #include "content/browser/service_worker/service_worker_version.h" |
19 #include "content/common/service_worker/service_worker_messages.h" | 19 #include "content/common/service_worker/service_worker_messages.h" |
20 #include "content/common/service_worker/service_worker_status_code.h" | 20 #include "content/common/service_worker/service_worker_status_code.h" |
21 #include "content/common/service_worker/service_worker_types.h" | 21 #include "content/common/service_worker/service_worker_types.h" |
22 #include "content/public/browser/browser_context.h" | 22 #include "content/public/browser/browser_context.h" |
23 #include "content/public/browser/browser_thread.h" | 23 #include "content/public/browser/browser_thread.h" |
24 #include "content/public/browser/render_process_host.h" | 24 #include "content/public/browser/render_process_host.h" |
25 #include "content/public/browser/storage_partition.h" | 25 #include "content/public/browser/storage_partition.h" |
26 #include "content/public/browser/web_contents.h" | 26 #include "content/public/browser/web_contents.h" |
27 #include "content/public/common/content_switches.h" | 27 #include "content/public/common/content_switches.h" |
| 28 #include "content/public/test/browser_test_utils.h" |
28 #include "content/public/test/content_browser_test.h" | 29 #include "content/public/test/content_browser_test.h" |
29 #include "content/public/test/content_browser_test_utils.h" | 30 #include "content/public/test/content_browser_test_utils.h" |
30 #include "content/shell/browser/shell.h" | 31 #include "content/shell/browser/shell.h" |
31 #include "net/test/embedded_test_server/embedded_test_server.h" | 32 #include "net/test/embedded_test_server/embedded_test_server.h" |
32 #include "net/test/embedded_test_server/http_request.h" | 33 #include "net/test/embedded_test_server/http_request.h" |
33 #include "net/test/embedded_test_server/http_response.h" | 34 #include "net/test/embedded_test_server/http_response.h" |
| 35 #include "net/url_request/url_request_filter.h" |
| 36 #include "net/url_request/url_request_interceptor.h" |
| 37 #include "net/url_request/url_request_test_job.h" |
34 #include "webkit/browser/blob/blob_data_handle.h" | 38 #include "webkit/browser/blob/blob_data_handle.h" |
35 #include "webkit/browser/blob/blob_storage_context.h" | 39 #include "webkit/browser/blob/blob_storage_context.h" |
36 #include "webkit/common/blob/blob_data.h" | 40 #include "webkit/common/blob/blob_data.h" |
37 | 41 |
38 namespace content { | 42 namespace content { |
39 | 43 |
40 namespace { | 44 namespace { |
41 | 45 |
42 struct FetchResult { | 46 struct FetchResult { |
43 ServiceWorkerStatusCode status; | 47 ServiceWorkerStatusCode status; |
(...skipping 120 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
164 request.headers.find("Service-Worker"); | 168 request.headers.find("Service-Worker"); |
165 EXPECT_TRUE(it != request.headers.end()); | 169 EXPECT_TRUE(it != request.headers.end()); |
166 EXPECT_EQ("script", it->second); | 170 EXPECT_EQ("script", it->second); |
167 | 171 |
168 scoped_ptr<net::test_server::BasicHttpResponse> http_response( | 172 scoped_ptr<net::test_server::BasicHttpResponse> http_response( |
169 new net::test_server::BasicHttpResponse()); | 173 new net::test_server::BasicHttpResponse()); |
170 http_response->set_content_type("text/javascript"); | 174 http_response->set_content_type("text/javascript"); |
171 return http_response.PassAs<net::test_server::HttpResponse>(); | 175 return http_response.PassAs<net::test_server::HttpResponse>(); |
172 } | 176 } |
173 | 177 |
| 178 // The ImportsBustMemcache test requires that the imported script |
| 179 // would naturally be cached in blink's memcache, but the embedded |
| 180 // test server doesn't produce headers that allow the blink's memcache |
| 181 // to do that. This interceptor injects headers that give the import |
| 182 // an experiration far in the future. |
| 183 class LongLivedResourceInterceptor : public net::URLRequestInterceptor { |
| 184 public: |
| 185 LongLivedResourceInterceptor(const std::string& body) |
| 186 : body_(body) {} |
| 187 virtual ~LongLivedResourceInterceptor() {} |
| 188 |
| 189 // net::URLRequestInterceptor implementation |
| 190 virtual net::URLRequestJob* MaybeInterceptRequest( |
| 191 net::URLRequest* request, |
| 192 net::NetworkDelegate* network_delegate) const OVERRIDE { |
| 193 const char kHeaders[] = |
| 194 "HTTP/1.1 200 OK\0" |
| 195 "Expires: Thu, 1 Jan 2100 20:00:00 GMT\0" |
| 196 "\0"; |
| 197 std::string headers(kHeaders, arraysize(kHeaders)); |
| 198 return new net::URLRequestTestJob( |
| 199 request, network_delegate, headers, body_, true); |
| 200 } |
| 201 |
| 202 private: |
| 203 std::string body_; |
| 204 DISALLOW_COPY_AND_ASSIGN(LongLivedResourceInterceptor); |
| 205 }; |
| 206 |
| 207 void CreateLongLivedResourceInterceptors( |
| 208 const GURL& worker_url, const GURL& import_url) { |
| 209 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 210 scoped_ptr<net::URLRequestInterceptor> interceptor; |
| 211 |
| 212 interceptor.reset(new LongLivedResourceInterceptor( |
| 213 "importScripts('long_lived_import.js');")); |
| 214 net::URLRequestFilter::GetInstance()->AddUrlInterceptor( |
| 215 worker_url, interceptor.Pass()); |
| 216 |
| 217 interceptor.reset(new LongLivedResourceInterceptor( |
| 218 "// the imported script does nothing")); |
| 219 net::URLRequestFilter::GetInstance()->AddUrlInterceptor( |
| 220 import_url, interceptor.Pass()); |
| 221 } |
| 222 |
| 223 void CountScriptResources( |
| 224 ServiceWorkerContextWrapper* wrapper, |
| 225 const GURL& scope, |
| 226 int* num_resources) { |
| 227 *num_resources = -1; |
| 228 |
| 229 std::vector<ServiceWorkerRegistrationInfo> infos = |
| 230 wrapper->context()->GetAllLiveRegistrationInfo(); |
| 231 if (infos.empty()) |
| 232 return; |
| 233 |
| 234 int version_id; |
| 235 size_t index = infos.size() - 1; |
| 236 if (!infos[index].installing_version.is_null) |
| 237 version_id = infos[index].installing_version.version_id; |
| 238 else if (!infos[index].waiting_version.is_null) |
| 239 version_id = infos[1].waiting_version.version_id; |
| 240 else if (!infos[index].active_version.is_null) |
| 241 version_id = infos[index].active_version.version_id; |
| 242 else |
| 243 return; |
| 244 |
| 245 ServiceWorkerVersion* version = |
| 246 wrapper->context()->GetLiveVersion(version_id); |
| 247 *num_resources = static_cast<int>(version->script_cache_map()->size()); |
| 248 } |
| 249 |
174 } // namespace | 250 } // namespace |
175 | 251 |
176 class ServiceWorkerBrowserTest : public ContentBrowserTest { | 252 class ServiceWorkerBrowserTest : public ContentBrowserTest { |
177 protected: | 253 protected: |
178 typedef ServiceWorkerBrowserTest self; | 254 typedef ServiceWorkerBrowserTest self; |
179 | 255 |
180 virtual void SetUpCommandLine(CommandLine* command_line) OVERRIDE { | 256 virtual void SetUpCommandLine(CommandLine* command_line) OVERRIDE { |
181 command_line->AppendSwitch( | 257 command_line->AppendSwitch( |
182 switches::kEnableExperimentalWebPlatformFeatures); | 258 switches::kEnableExperimentalWebPlatformFeatures); |
183 } | 259 } |
(...skipping 508 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
692 shell()->Close(); | 768 shell()->Close(); |
693 { | 769 { |
694 base::RunLoop run_loop; | 770 base::RunLoop run_loop; |
695 public_context()->UnregisterServiceWorker( | 771 public_context()->UnregisterServiceWorker( |
696 embedded_test_server()->GetURL(kPageUrl), | 772 embedded_test_server()->GetURL(kPageUrl), |
697 base::Bind(&ExpectResultAndRun, true, run_loop.QuitClosure())); | 773 base::Bind(&ExpectResultAndRun, true, run_loop.QuitClosure())); |
698 run_loop.Run(); | 774 run_loop.Run(); |
699 } | 775 } |
700 } | 776 } |
701 | 777 |
| 778 IN_PROC_BROWSER_TEST_F(ServiceWorkerBrowserTest, ImportsBustMemcache) { |
| 779 const std::string kScopeUrl = "/service_worker/imports_bust_memcache_scope/*"; |
| 780 const std::string kPageUrl = "/service_worker/imports_bust_memcache.html"; |
| 781 const std::string kScriptUrl = "/service_worker/worker_with_one_import.js"; |
| 782 const std::string kImportUrl = "/service_worker/long_lived_import.js"; |
| 783 const base::string16 kOKTitle(base::ASCIIToUTF16("OK")); |
| 784 const base::string16 kFailTitle(base::ASCIIToUTF16("FAIL")); |
| 785 |
| 786 RunOnIOThread( |
| 787 base::Bind(&CreateLongLivedResourceInterceptors, |
| 788 embedded_test_server()->GetURL(kScriptUrl), |
| 789 embedded_test_server()->GetURL(kImportUrl))); |
| 790 |
| 791 TitleWatcher title_watcher(shell()->web_contents(), kOKTitle); |
| 792 title_watcher.AlsoWaitForTitle(kFailTitle); |
| 793 NavigateToURL(shell(), embedded_test_server()->GetURL(kPageUrl)); |
| 794 base::string16 title = title_watcher.WaitAndGetTitle(); |
| 795 EXPECT_EQ(kOKTitle, title); |
| 796 |
| 797 // Verify the number of resources in the implicit script cache is correct. |
| 798 const size_t kExpectedNumResources = 2u; |
| 799 int num_resources = 0; |
| 800 RunOnIOThread( |
| 801 base::Bind(&CountScriptResources, |
| 802 base::Unretained(wrapper()), |
| 803 embedded_test_server()->GetURL(kScopeUrl), |
| 804 &num_resources)); |
| 805 EXPECT_EQ(kExpectedNumResources, num_resources); |
| 806 } |
| 807 |
702 class ServiceWorkerBlackBoxBrowserTest : public ServiceWorkerBrowserTest { | 808 class ServiceWorkerBlackBoxBrowserTest : public ServiceWorkerBrowserTest { |
703 public: | 809 public: |
704 typedef ServiceWorkerBlackBoxBrowserTest self; | 810 typedef ServiceWorkerBlackBoxBrowserTest self; |
705 | 811 |
706 void FindRegistrationOnIO(const GURL& document_url, | 812 void FindRegistrationOnIO(const GURL& document_url, |
707 ServiceWorkerStatusCode* status, | 813 ServiceWorkerStatusCode* status, |
708 GURL* script_url, | 814 GURL* script_url, |
709 const base::Closure& continuation) { | 815 const base::Closure& continuation) { |
710 wrapper()->context()->storage()->FindRegistrationForDocument( | 816 wrapper()->context()->storage()->FindRegistrationForDocument( |
711 document_url, | 817 document_url, |
(...skipping 109 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
821 base::Bind(&ServiceWorkerBlackBoxBrowserTest::FindRegistrationOnIO, | 927 base::Bind(&ServiceWorkerBlackBoxBrowserTest::FindRegistrationOnIO, |
822 this, | 928 this, |
823 embedded_test_server()->GetURL("/service_worker/empty.html"), | 929 embedded_test_server()->GetURL("/service_worker/empty.html"), |
824 &status, | 930 &status, |
825 &script_url)); | 931 &script_url)); |
826 EXPECT_EQ(SERVICE_WORKER_ERROR_NOT_FOUND, status); | 932 EXPECT_EQ(SERVICE_WORKER_ERROR_NOT_FOUND, status); |
827 } | 933 } |
828 } | 934 } |
829 | 935 |
830 } // namespace content | 936 } // namespace content |
OLD | NEW |