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 <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 "base/time/time.h" | 22 #include "base/time/time.h" |
| 19 #include "build/build_config.h" | 23 #include "build/build_config.h" |
| 20 #include "content/browser/fileapi/chrome_blob_storage_context.h" | 24 #include "content/browser/fileapi/chrome_blob_storage_context.h" |
| 21 #include "content/browser/service_worker/embedded_worker_instance.h" | 25 #include "content/browser/service_worker/embedded_worker_instance.h" |
| 22 #include "content/browser/service_worker/embedded_worker_registry.h" | 26 #include "content/browser/service_worker/embedded_worker_registry.h" |
| 23 #include "content/browser/service_worker/service_worker_context_core.h" | 27 #include "content/browser/service_worker/service_worker_context_core.h" |
| 24 #include "content/browser/service_worker/service_worker_context_observer.h" | 28 #include "content/browser/service_worker/service_worker_context_observer.h" |
| 25 #include "content/browser/service_worker/service_worker_context_wrapper.h" | 29 #include "content/browser/service_worker/service_worker_context_wrapper.h" |
| (...skipping 184 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 210 return std::move(http_response); | 214 return std::move(http_response); |
| 211 } | 215 } |
| 212 | 216 |
| 213 scoped_ptr<net::test_server::HttpResponse> VerifySaveDataHeaderNotInRequest( | 217 scoped_ptr<net::test_server::HttpResponse> VerifySaveDataHeaderNotInRequest( |
| 214 const net::test_server::HttpRequest& request) { | 218 const net::test_server::HttpRequest& request) { |
| 215 auto it = request.headers.find("Save-Data"); | 219 auto it = request.headers.find("Save-Data"); |
| 216 EXPECT_EQ(request.headers.end(), it); | 220 EXPECT_EQ(request.headers.end(), it); |
| 217 return make_scoped_ptr(new net::test_server::BasicHttpResponse()); | 221 return make_scoped_ptr(new net::test_server::BasicHttpResponse()); |
| 218 } | 222 } |
| 219 | 223 |
| 224 scoped_ptr<net::test_server::HttpResponse> | |
| 225 VerifySaveDataNotInAccessControlRequestHeader( | |
| 226 const net::test_server::HttpRequest& request) { | |
| 227 // Save-Data should be present. | |
| 228 auto it = request.headers.find("Save-Data"); | |
| 229 EXPECT_NE(request.headers.end(), it); | |
| 230 EXPECT_EQ("on", it->second); | |
| 231 | |
| 232 scoped_ptr<net::test_server::BasicHttpResponse> http_response( | |
| 233 new net::test_server::BasicHttpResponse()); | |
| 234 if (request.method == net::test_server::METHOD_OPTIONS) { | |
| 235 // Access-Control-Request-Headers should contain 'X-Custom-Header' and not | |
| 236 // contain 'Save-Data'. | |
| 237 auto acrh_iter = request.headers.find("Access-Control-Request-Headers"); | |
| 238 EXPECT_NE(request.headers.end(), acrh_iter); | |
| 239 EXPECT_NE(std::string::npos, acrh_iter->second.find("x-custom-header")); | |
| 240 EXPECT_EQ(std::string::npos, acrh_iter->second.find("save-data")); | |
| 241 http_response->AddCustomHeader("Access-Control-Allow-Headers", | |
| 242 acrh_iter->second); | |
| 243 http_response->AddCustomHeader("Access-Control-Allow-Methods", "GET"); | |
| 244 http_response->AddCustomHeader("Access-Control-Allow-Origin", "*"); | |
| 245 } else { | |
| 246 http_response->AddCustomHeader("Access-Control-Allow-Origin", "*"); | |
| 247 http_response->set_content("PASS"); | |
| 248 } | |
| 249 return std::move(http_response); | |
| 250 } | |
| 251 | |
| 220 // The ImportsBustMemcache test requires that the imported script | 252 // The ImportsBustMemcache test requires that the imported script |
| 221 // would naturally be cached in blink's memcache, but the embedded | 253 // would naturally be cached in blink's memcache, but the embedded |
| 222 // test server doesn't produce headers that allow the blink's memcache | 254 // test server doesn't produce headers that allow the blink's memcache |
| 223 // to do that. This interceptor injects headers that give the import | 255 // to do that. This interceptor injects headers that give the import |
| 224 // an experiration far in the future. | 256 // an experiration far in the future. |
| 225 class LongLivedResourceInterceptor : public net::URLRequestInterceptor { | 257 class LongLivedResourceInterceptor : public net::URLRequestInterceptor { |
| 226 public: | 258 public: |
| 227 explicit LongLivedResourceInterceptor(const std::string& body) | 259 explicit LongLivedResourceInterceptor(const std::string& body) |
| 228 : body_(body) {} | 260 : body_(body) {} |
| 229 ~LongLivedResourceInterceptor() override {} | 261 ~LongLivedResourceInterceptor() override {} |
| (...skipping 879 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1109 SetBrowserClientForTesting(old_client); | 1141 SetBrowserClientForTesting(old_client); |
| 1110 shell()->Close(); | 1142 shell()->Close(); |
| 1111 | 1143 |
| 1112 base::RunLoop run_loop; | 1144 base::RunLoop run_loop; |
| 1113 public_context()->UnregisterServiceWorker( | 1145 public_context()->UnregisterServiceWorker( |
| 1114 embedded_test_server()->GetURL(kPageUrl), | 1146 embedded_test_server()->GetURL(kPageUrl), |
| 1115 base::Bind(&ExpectResultAndRun, true, run_loop.QuitClosure())); | 1147 base::Bind(&ExpectResultAndRun, true, run_loop.QuitClosure())); |
| 1116 run_loop.Run(); | 1148 run_loop.Run(); |
| 1117 } | 1149 } |
| 1118 | 1150 |
| 1151 // Tests that when data saver is enabled and a cross-origin fetch by a webpage | |
| 1152 // is intercepted by a serviceworker, and the serviceworker does a fetch, the | |
| 1153 // preflight request does not have save-data in Access-Control-Request-Headers. | |
| 1154 IN_PROC_BROWSER_TEST_F(ServiceWorkerBrowserTest, CrossOriginFetchWithSaveData) { | |
| 1155 const char kPageUrl[] = "/service_worker/fetch_cross_origin.html"; | |
| 1156 const char kWorkerUrl[] = "/service_worker/fetch_event_pass_through.js"; | |
| 1157 net::EmbeddedTestServer cross_origin_server; | |
| 1158 cross_origin_server.ServeFilesFromSourceDirectory("content/test/data"); | |
| 1159 cross_origin_server.RegisterRequestHandler( | |
| 1160 base::Bind(&VerifySaveDataNotInAccessControlRequestHeader)); | |
| 1161 cross_origin_server.Start(); | |
| 1162 | |
| 1163 MockContentBrowserClient content_browser_client; | |
| 1164 content_browser_client.set_data_saver_enabled(true); | |
| 1165 ContentBrowserClient* old_client = | |
| 1166 SetBrowserClientForTesting(&content_browser_client); | |
| 1167 shell()->web_contents()->GetRenderViewHost()->OnWebkitPreferencesChanged(); | |
| 1168 scoped_refptr<WorkerActivatedObserver> observer = | |
| 1169 new WorkerActivatedObserver(wrapper()); | |
| 1170 observer->Init(); | |
| 1171 public_context()->RegisterServiceWorker( | |
| 1172 embedded_test_server()->GetURL(kPageUrl), | |
| 1173 embedded_test_server()->GetURL(kWorkerUrl), | |
| 1174 base::Bind(&ExpectResultAndRun, true, base::Bind(&base::DoNothing))); | |
| 1175 observer->Wait(); | |
| 1176 | |
| 1177 NavigateToURL(shell(), | |
| 1178 embedded_test_server()->GetURL(base::StringPrintf( | |
| 1179 "%s?%s", kPageUrl, | |
| 1180 cross_origin_server.GetURL("/cross_origin_request.html") | |
| 1181 .spec() | |
| 1182 .c_str()))); | |
| 1183 const base::string16 title = base::ASCIIToUTF16("PASS"); | |
| 1184 TitleWatcher title_watcher(shell()->web_contents(), title); | |
|
Paweł Hajdan Jr.
2016/04/06 20:15:46
Shouldn't TitleWatcher be instantiated *before* th
Raj
2016/04/06 22:04:10
Done.
| |
| 1185 EXPECT_EQ(title, title_watcher.WaitAndGetTitle()); | |
| 1186 | |
| 1187 SetBrowserClientForTesting(old_client); | |
| 1188 shell()->Close(); | |
| 1189 | |
| 1190 base::RunLoop run_loop; | |
| 1191 public_context()->UnregisterServiceWorker( | |
| 1192 embedded_test_server()->GetURL(kPageUrl), | |
| 1193 base::Bind(&ExpectResultAndRun, true, run_loop.QuitClosure())); | |
| 1194 run_loop.Run(); | |
| 1195 } | |
| 1196 | |
| 1119 IN_PROC_BROWSER_TEST_F(ServiceWorkerBrowserTest, | 1197 IN_PROC_BROWSER_TEST_F(ServiceWorkerBrowserTest, |
| 1120 FetchPageWithSaveDataPassThroughOnFetch) { | 1198 FetchPageWithSaveDataPassThroughOnFetch) { |
| 1121 const char kPageUrl[] = "/service_worker/pass_through_fetch.html"; | 1199 const char kPageUrl[] = "/service_worker/pass_through_fetch.html"; |
| 1122 const char kWorkerUrl[] = "/service_worker/fetch_event_pass_through.js"; | 1200 const char kWorkerUrl[] = "/service_worker/fetch_event_pass_through.js"; |
| 1123 MockContentBrowserClient content_browser_client; | 1201 MockContentBrowserClient content_browser_client; |
| 1124 content_browser_client.set_data_saver_enabled(true); | 1202 content_browser_client.set_data_saver_enabled(true); |
| 1125 ContentBrowserClient* old_client = | 1203 ContentBrowserClient* old_client = |
| 1126 SetBrowserClientForTesting(&content_browser_client); | 1204 SetBrowserClientForTesting(&content_browser_client); |
| 1127 shell()->web_contents()->GetRenderViewHost()->OnWebkitPreferencesChanged(); | 1205 shell()->web_contents()->GetRenderViewHost()->OnWebkitPreferencesChanged(); |
| 1128 scoped_refptr<WorkerActivatedObserver> observer = | 1206 scoped_refptr<WorkerActivatedObserver> observer = |
| (...skipping 356 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1485 ASSERT_EQ(SERVICE_WORKER_OK, status); | 1563 ASSERT_EQ(SERVICE_WORKER_OK, status); |
| 1486 // Stop the worker. | 1564 // Stop the worker. |
| 1487 StopWorker(SERVICE_WORKER_OK); | 1565 StopWorker(SERVICE_WORKER_OK); |
| 1488 // Restart the worker. | 1566 // Restart the worker. |
| 1489 StartWorker(SERVICE_WORKER_OK); | 1567 StartWorker(SERVICE_WORKER_OK); |
| 1490 // Stop the worker. | 1568 // Stop the worker. |
| 1491 StopWorker(SERVICE_WORKER_OK); | 1569 StopWorker(SERVICE_WORKER_OK); |
| 1492 } | 1570 } |
| 1493 | 1571 |
| 1494 } // namespace content | 1572 } // namespace content |
| OLD | NEW |