| 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 <stddef.h> | 5 #include <stddef.h> |
| 6 #include <stdint.h> | 6 #include <stdint.h> |
| 7 #include <map> | 7 #include <map> |
| 8 #include <utility> | 8 #include <utility> |
| 9 | 9 |
| 10 #include "base/bind.h" | 10 #include "base/bind.h" |
| 11 #include "base/callback.h" | 11 #include "base/callback.h" |
| 12 #include "base/command_line.h" | 12 #include "base/command_line.h" |
| 13 #include "base/macros.h" | 13 #include "base/macros.h" |
| 14 #include "base/memory/ref_counted.h" |
| 15 #include "base/memory/scoped_ptr.h" |
| 14 #include "base/run_loop.h" | 16 #include "base/run_loop.h" |
| 15 #include "base/single_thread_task_runner.h" | 17 #include "base/single_thread_task_runner.h" |
| 18 #include "base/strings/string16.h" |
| 19 #include "base/strings/stringprintf.h" |
| 16 #include "base/strings/utf_string_conversions.h" | 20 #include "base/strings/utf_string_conversions.h" |
| 17 #include "base/thread_task_runner_handle.h" | 21 #include "base/thread_task_runner_handle.h" |
| 18 #include "build/build_config.h" | 22 #include "build/build_config.h" |
| 19 #include "content/browser/fileapi/chrome_blob_storage_context.h" | 23 #include "content/browser/fileapi/chrome_blob_storage_context.h" |
| 20 #include "content/browser/service_worker/embedded_worker_instance.h" | 24 #include "content/browser/service_worker/embedded_worker_instance.h" |
| 21 #include "content/browser/service_worker/embedded_worker_registry.h" | 25 #include "content/browser/service_worker/embedded_worker_registry.h" |
| 22 #include "content/browser/service_worker/service_worker_context_core.h" | 26 #include "content/browser/service_worker/service_worker_context_core.h" |
| 23 #include "content/browser/service_worker/service_worker_context_observer.h" | 27 #include "content/browser/service_worker/service_worker_context_observer.h" |
| 24 #include "content/browser/service_worker/service_worker_context_wrapper.h" | 28 #include "content/browser/service_worker/service_worker_context_wrapper.h" |
| 25 #include "content/browser/service_worker/service_worker_fetch_dispatcher.h" | 29 #include "content/browser/service_worker/service_worker_fetch_dispatcher.h" |
| (...skipping 218 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 244 return std::move(http_response); | 248 return std::move(http_response); |
| 245 } | 249 } |
| 246 | 250 |
| 247 scoped_ptr<net::test_server::HttpResponse> VerifySaveDataHeaderNotInRequest( | 251 scoped_ptr<net::test_server::HttpResponse> VerifySaveDataHeaderNotInRequest( |
| 248 const net::test_server::HttpRequest& request) { | 252 const net::test_server::HttpRequest& request) { |
| 249 auto it = request.headers.find("Save-Data"); | 253 auto it = request.headers.find("Save-Data"); |
| 250 EXPECT_EQ(request.headers.end(), it); | 254 EXPECT_EQ(request.headers.end(), it); |
| 251 return make_scoped_ptr(new net::test_server::BasicHttpResponse()); | 255 return make_scoped_ptr(new net::test_server::BasicHttpResponse()); |
| 252 } | 256 } |
| 253 | 257 |
| 258 scoped_ptr<net::test_server::HttpResponse> |
| 259 VerifySaveDataNotInAccessControlRequestHeader( |
| 260 const net::test_server::HttpRequest& request) { |
| 261 // Save-Data should be present. |
| 262 auto it = request.headers.find("Save-Data"); |
| 263 EXPECT_NE(request.headers.end(), it); |
| 264 EXPECT_EQ("on", it->second); |
| 265 |
| 266 scoped_ptr<net::test_server::BasicHttpResponse> http_response( |
| 267 new net::test_server::BasicHttpResponse()); |
| 268 if (request.method == net::test_server::METHOD_OPTIONS) { |
| 269 // Access-Control-Request-Headers should contain 'X-Custom-Header' and not |
| 270 // contain 'Save-Data'. |
| 271 auto acrh_iter = request.headers.find("Access-Control-Request-Headers"); |
| 272 EXPECT_NE(request.headers.end(), acrh_iter); |
| 273 EXPECT_NE(std::string::npos, acrh_iter->second.find("x-custom-header")); |
| 274 EXPECT_EQ(std::string::npos, acrh_iter->second.find("save-data")); |
| 275 http_response->AddCustomHeader("Access-Control-Allow-Headers", |
| 276 acrh_iter->second); |
| 277 http_response->AddCustomHeader("Access-Control-Allow-Methods", "GET"); |
| 278 http_response->AddCustomHeader("Access-Control-Allow-Origin", "*"); |
| 279 } else { |
| 280 http_response->AddCustomHeader("Access-Control-Allow-Origin", "*"); |
| 281 http_response->set_content("PASS"); |
| 282 } |
| 283 return std::move(http_response); |
| 284 } |
| 285 |
| 254 // The ImportsBustMemcache test requires that the imported script | 286 // The ImportsBustMemcache test requires that the imported script |
| 255 // would naturally be cached in blink's memcache, but the embedded | 287 // would naturally be cached in blink's memcache, but the embedded |
| 256 // test server doesn't produce headers that allow the blink's memcache | 288 // test server doesn't produce headers that allow the blink's memcache |
| 257 // to do that. This interceptor injects headers that give the import | 289 // to do that. This interceptor injects headers that give the import |
| 258 // an experiration far in the future. | 290 // an experiration far in the future. |
| 259 class LongLivedResourceInterceptor : public net::URLRequestInterceptor { | 291 class LongLivedResourceInterceptor : public net::URLRequestInterceptor { |
| 260 public: | 292 public: |
| 261 LongLivedResourceInterceptor(const std::string& body) | 293 LongLivedResourceInterceptor(const std::string& body) |
| 262 : body_(body) {} | 294 : body_(body) {} |
| 263 ~LongLivedResourceInterceptor() override {} | 295 ~LongLivedResourceInterceptor() override {} |
| (...skipping 819 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1083 SetBrowserClientForTesting(old_client); | 1115 SetBrowserClientForTesting(old_client); |
| 1084 shell()->Close(); | 1116 shell()->Close(); |
| 1085 | 1117 |
| 1086 base::RunLoop run_loop; | 1118 base::RunLoop run_loop; |
| 1087 public_context()->UnregisterServiceWorker( | 1119 public_context()->UnregisterServiceWorker( |
| 1088 embedded_test_server()->GetURL(kPageUrl), | 1120 embedded_test_server()->GetURL(kPageUrl), |
| 1089 base::Bind(&ExpectResultAndRun, true, run_loop.QuitClosure())); | 1121 base::Bind(&ExpectResultAndRun, true, run_loop.QuitClosure())); |
| 1090 run_loop.Run(); | 1122 run_loop.Run(); |
| 1091 } | 1123 } |
| 1092 | 1124 |
| 1125 // Tests that when data saver is enabled and a cross-origin fetch by a webpage |
| 1126 // is intercepted by a serviceworker, and the serviceworker does a fetch, the |
| 1127 // preflight request does not have save-data in Access-Control-Request-Headers. |
| 1128 IN_PROC_BROWSER_TEST_F(ServiceWorkerBrowserTest, CrossOriginFetchWithSaveData) { |
| 1129 const char kPageUrl[] = "/service_worker/fetch_cross_origin.html"; |
| 1130 const char kWorkerUrl[] = "/service_worker/fetch_event_pass_through.js"; |
| 1131 net::EmbeddedTestServer cross_origin_server; |
| 1132 cross_origin_server.ServeFilesFromSourceDirectory("content/test/data"); |
| 1133 cross_origin_server.RegisterRequestHandler( |
| 1134 base::Bind(&VerifySaveDataNotInAccessControlRequestHeader)); |
| 1135 cross_origin_server.Start(); |
| 1136 |
| 1137 MockContentBrowserClient content_browser_client; |
| 1138 content_browser_client.set_data_saver_enabled(true); |
| 1139 ContentBrowserClient* old_client = |
| 1140 SetBrowserClientForTesting(&content_browser_client); |
| 1141 shell()->web_contents()->GetRenderViewHost()->OnWebkitPreferencesChanged(); |
| 1142 scoped_refptr<WorkerActivatedObserver> observer = |
| 1143 new WorkerActivatedObserver(wrapper()); |
| 1144 observer->Init(); |
| 1145 public_context()->RegisterServiceWorker( |
| 1146 embedded_test_server()->GetURL(kPageUrl), |
| 1147 embedded_test_server()->GetURL(kWorkerUrl), |
| 1148 base::Bind(&ExpectResultAndRun, true, base::Bind(&base::DoNothing))); |
| 1149 observer->Wait(); |
| 1150 |
| 1151 const base::string16 title = base::ASCIIToUTF16("PASS"); |
| 1152 TitleWatcher title_watcher(shell()->web_contents(), title); |
| 1153 NavigateToURL(shell(), |
| 1154 embedded_test_server()->GetURL(base::StringPrintf( |
| 1155 "%s?%s", kPageUrl, |
| 1156 cross_origin_server.GetURL("/cross_origin_request.html") |
| 1157 .spec() |
| 1158 .c_str()))); |
| 1159 EXPECT_EQ(title, title_watcher.WaitAndGetTitle()); |
| 1160 |
| 1161 SetBrowserClientForTesting(old_client); |
| 1162 shell()->Close(); |
| 1163 |
| 1164 base::RunLoop run_loop; |
| 1165 public_context()->UnregisterServiceWorker( |
| 1166 embedded_test_server()->GetURL(kPageUrl), |
| 1167 base::Bind(&ExpectResultAndRun, true, run_loop.QuitClosure())); |
| 1168 run_loop.Run(); |
| 1169 } |
| 1170 |
| 1093 IN_PROC_BROWSER_TEST_F(ServiceWorkerBrowserTest, | 1171 IN_PROC_BROWSER_TEST_F(ServiceWorkerBrowserTest, |
| 1094 FetchPageWithSaveDataPassThroughOnFetch) { | 1172 FetchPageWithSaveDataPassThroughOnFetch) { |
| 1095 const char kPageUrl[] = "/service_worker/pass_through_fetch.html"; | 1173 const char kPageUrl[] = "/service_worker/pass_through_fetch.html"; |
| 1096 const char kWorkerUrl[] = "/service_worker/fetch_event_pass_through.js"; | 1174 const char kWorkerUrl[] = "/service_worker/fetch_event_pass_through.js"; |
| 1097 MockContentBrowserClient content_browser_client; | 1175 MockContentBrowserClient content_browser_client; |
| 1098 content_browser_client.set_data_saver_enabled(true); | 1176 content_browser_client.set_data_saver_enabled(true); |
| 1099 ContentBrowserClient* old_client = | 1177 ContentBrowserClient* old_client = |
| 1100 SetBrowserClientForTesting(&content_browser_client); | 1178 SetBrowserClientForTesting(&content_browser_client); |
| 1101 shell()->web_contents()->GetRenderViewHost()->OnWebkitPreferencesChanged(); | 1179 shell()->web_contents()->GetRenderViewHost()->OnWebkitPreferencesChanged(); |
| 1102 scoped_refptr<WorkerActivatedObserver> observer = | 1180 scoped_refptr<WorkerActivatedObserver> observer = |
| (...skipping 356 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1459 ASSERT_EQ(SERVICE_WORKER_OK, status); | 1537 ASSERT_EQ(SERVICE_WORKER_OK, status); |
| 1460 // Stop the worker. | 1538 // Stop the worker. |
| 1461 StopWorker(SERVICE_WORKER_OK); | 1539 StopWorker(SERVICE_WORKER_OK); |
| 1462 // Restart the worker. | 1540 // Restart the worker. |
| 1463 StartWorker(SERVICE_WORKER_OK); | 1541 StartWorker(SERVICE_WORKER_OK); |
| 1464 // Stop the worker. | 1542 // Stop the worker. |
| 1465 StopWorker(SERVICE_WORKER_OK); | 1543 StopWorker(SERVICE_WORKER_OK); |
| 1466 } | 1544 } |
| 1467 | 1545 |
| 1468 } // namespace content | 1546 } // namespace content |
| OLD | NEW |