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 <utility> | 8 #include <utility> |
8 | 9 |
9 #include "base/bind.h" | 10 #include "base/bind.h" |
10 #include "base/callback.h" | 11 #include "base/callback.h" |
11 #include "base/command_line.h" | 12 #include "base/command_line.h" |
12 #include "base/macros.h" | 13 #include "base/macros.h" |
13 #include "base/run_loop.h" | 14 #include "base/run_loop.h" |
14 #include "base/single_thread_task_runner.h" | 15 #include "base/single_thread_task_runner.h" |
15 #include "base/strings/utf_string_conversions.h" | 16 #include "base/strings/utf_string_conversions.h" |
16 #include "base/thread_task_runner_handle.h" | 17 #include "base/thread_task_runner_handle.h" |
17 #include "base/time/time.h" | 18 #include "base/time/time.h" |
18 #include "build/build_config.h" | 19 #include "build/build_config.h" |
19 #include "content/browser/fileapi/chrome_blob_storage_context.h" | 20 #include "content/browser/fileapi/chrome_blob_storage_context.h" |
20 #include "content/browser/service_worker/embedded_worker_instance.h" | 21 #include "content/browser/service_worker/embedded_worker_instance.h" |
21 #include "content/browser/service_worker/embedded_worker_registry.h" | 22 #include "content/browser/service_worker/embedded_worker_registry.h" |
22 #include "content/browser/service_worker/service_worker_context_core.h" | 23 #include "content/browser/service_worker/service_worker_context_core.h" |
23 #include "content/browser/service_worker/service_worker_context_observer.h" | 24 #include "content/browser/service_worker/service_worker_context_observer.h" |
24 #include "content/browser/service_worker/service_worker_context_wrapper.h" | 25 #include "content/browser/service_worker/service_worker_context_wrapper.h" |
25 #include "content/browser/service_worker/service_worker_fetch_dispatcher.h" | 26 #include "content/browser/service_worker/service_worker_fetch_dispatcher.h" |
26 #include "content/browser/service_worker/service_worker_registration.h" | 27 #include "content/browser/service_worker/service_worker_registration.h" |
27 #include "content/browser/service_worker/service_worker_test_utils.h" | 28 #include "content/browser/service_worker/service_worker_test_utils.h" |
28 #include "content/browser/service_worker/service_worker_version.h" | 29 #include "content/browser/service_worker/service_worker_version.h" |
29 #include "content/common/service_worker/service_worker_messages.h" | 30 #include "content/common/service_worker/service_worker_messages.h" |
30 #include "content/common/service_worker/service_worker_status_code.h" | 31 #include "content/common/service_worker/service_worker_status_code.h" |
31 #include "content/common/service_worker/service_worker_types.h" | 32 #include "content/common/service_worker/service_worker_types.h" |
32 #include "content/public/browser/browser_context.h" | 33 #include "content/public/browser/browser_context.h" |
33 #include "content/public/browser/browser_thread.h" | 34 #include "content/public/browser/browser_thread.h" |
35 #include "content/public/browser/content_browser_client.h" | |
34 #include "content/public/browser/navigation_entry.h" | 36 #include "content/public/browser/navigation_entry.h" |
35 #include "content/public/browser/render_process_host.h" | 37 #include "content/public/browser/render_process_host.h" |
38 #include "content/public/browser/render_view_host.h" | |
36 #include "content/public/browser/storage_partition.h" | 39 #include "content/public/browser/storage_partition.h" |
37 #include "content/public/browser/web_contents.h" | 40 #include "content/public/browser/web_contents.h" |
41 #include "content/public/common/content_client.h" | |
38 #include "content/public/common/content_switches.h" | 42 #include "content/public/common/content_switches.h" |
39 #include "content/public/common/referrer.h" | 43 #include "content/public/common/referrer.h" |
40 #include "content/public/common/security_style.h" | 44 #include "content/public/common/security_style.h" |
41 #include "content/public/common/ssl_status.h" | 45 #include "content/public/common/ssl_status.h" |
46 #include "content/public/common/web_preferences.h" | |
42 #include "content/public/test/browser_test_utils.h" | 47 #include "content/public/test/browser_test_utils.h" |
43 #include "content/public/test/content_browser_test.h" | 48 #include "content/public/test/content_browser_test.h" |
44 #include "content/public/test/content_browser_test_utils.h" | 49 #include "content/public/test/content_browser_test_utils.h" |
45 #include "content/shell/browser/shell.h" | 50 #include "content/shell/browser/shell.h" |
46 #include "content/shell/browser/shell_content_browser_client.h" | 51 #include "content/shell/browser/shell_content_browser_client.h" |
52 #include "content/test/test_content_browser_client.h" | |
47 #include "net/test/embedded_test_server/embedded_test_server.h" | 53 #include "net/test/embedded_test_server/embedded_test_server.h" |
48 #include "net/test/embedded_test_server/http_request.h" | 54 #include "net/test/embedded_test_server/http_request.h" |
49 #include "net/test/embedded_test_server/http_response.h" | 55 #include "net/test/embedded_test_server/http_response.h" |
50 #include "net/url_request/url_request_filter.h" | 56 #include "net/url_request/url_request_filter.h" |
51 #include "net/url_request/url_request_interceptor.h" | 57 #include "net/url_request/url_request_interceptor.h" |
52 #include "net/url_request/url_request_test_job.h" | 58 #include "net/url_request/url_request_test_job.h" |
53 #include "storage/browser/blob/blob_data_handle.h" | 59 #include "storage/browser/blob/blob_data_handle.h" |
54 #include "storage/browser/blob/blob_data_snapshot.h" | 60 #include "storage/browser/blob/blob_data_snapshot.h" |
55 #include "storage/browser/blob/blob_storage_context.h" | 61 #include "storage/browser/blob/blob_storage_context.h" |
56 | 62 |
(...skipping 130 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
187 auto it = request.headers.find("Service-Worker"); | 193 auto it = request.headers.find("Service-Worker"); |
188 EXPECT_TRUE(it != request.headers.end()); | 194 EXPECT_TRUE(it != request.headers.end()); |
189 EXPECT_EQ("script", it->second); | 195 EXPECT_EQ("script", it->second); |
190 | 196 |
191 scoped_ptr<net::test_server::BasicHttpResponse> http_response( | 197 scoped_ptr<net::test_server::BasicHttpResponse> http_response( |
192 new net::test_server::BasicHttpResponse()); | 198 new net::test_server::BasicHttpResponse()); |
193 http_response->set_content_type("text/javascript"); | 199 http_response->set_content_type("text/javascript"); |
194 return std::move(http_response); | 200 return std::move(http_response); |
195 } | 201 } |
196 | 202 |
203 scoped_ptr<net::test_server::HttpResponse> VerifySaveDataHeaderInRequest( | |
204 const net::test_server::HttpRequest& request) { | |
205 auto it = request.headers.find("Save-Data"); | |
206 EXPECT_NE(request.headers.end(), it); | |
207 EXPECT_EQ("on", it->second); | |
208 | |
209 scoped_ptr<net::test_server::BasicHttpResponse> http_response( | |
210 new net::test_server::BasicHttpResponse()); | |
211 http_response->set_content_type("text/javascript"); | |
212 return std::move(http_response); | |
213 } | |
214 | |
215 scoped_ptr<net::test_server::HttpResponse> VerifySaveDataHeaderNotInRequest( | |
216 const net::test_server::HttpRequest& request) { | |
217 auto it = request.headers.find("Save-Data"); | |
218 EXPECT_EQ(request.headers.end(), it); | |
219 return make_scoped_ptr(new net::test_server::BasicHttpResponse()); | |
220 } | |
221 | |
197 // The ImportsBustMemcache test requires that the imported script | 222 // The ImportsBustMemcache test requires that the imported script |
198 // would naturally be cached in blink's memcache, but the embedded | 223 // would naturally be cached in blink's memcache, but the embedded |
199 // test server doesn't produce headers that allow the blink's memcache | 224 // test server doesn't produce headers that allow the blink's memcache |
200 // to do that. This interceptor injects headers that give the import | 225 // to do that. This interceptor injects headers that give the import |
201 // an experiration far in the future. | 226 // an experiration far in the future. |
202 class LongLivedResourceInterceptor : public net::URLRequestInterceptor { | 227 class LongLivedResourceInterceptor : public net::URLRequestInterceptor { |
203 public: | 228 public: |
204 LongLivedResourceInterceptor(const std::string& body) | 229 LongLivedResourceInterceptor(const std::string& body) |
205 : body_(body) {} | 230 : body_(body) {} |
206 ~LongLivedResourceInterceptor() override {} | 231 ~LongLivedResourceInterceptor() override {} |
(...skipping 788 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
995 console_listener.messages()[0].find(expected1)); | 1020 console_listener.messages()[0].find(expected1)); |
996 ASSERT_EQ(0u, console_listener.messages()[1].find(expected2)); | 1021 ASSERT_EQ(0u, console_listener.messages()[1].find(expected2)); |
997 version_->embedded_worker()->RemoveListener(&console_listener); | 1022 version_->embedded_worker()->RemoveListener(&console_listener); |
998 | 1023 |
999 ASSERT_EQ(SERVICE_WORKER_FETCH_EVENT_RESULT_RESPONSE, result); | 1024 ASSERT_EQ(SERVICE_WORKER_FETCH_EVENT_RESULT_RESPONSE, result); |
1000 EXPECT_EQ(0, response.status_code); | 1025 EXPECT_EQ(0, response.status_code); |
1001 | 1026 |
1002 ASSERT_FALSE(blob_data_handle); | 1027 ASSERT_FALSE(blob_data_handle); |
1003 } | 1028 } |
1004 | 1029 |
1030 class MockContentBrowserClient : public TestContentBrowserClient { | |
1031 public: | |
1032 MockContentBrowserClient() | |
1033 : TestContentBrowserClient(), data_saver_enabled_(false) {} | |
1034 | |
1035 ~MockContentBrowserClient() override {} | |
1036 | |
1037 void set_data_saver_enabled(bool enabled) { data_saver_enabled_ = enabled; } | |
1038 | |
1039 // ContentBrowserClient overrides: | |
1040 bool IsDataSaverEnabled(BrowserContext* context) override { | |
1041 return data_saver_enabled_; | |
1042 } | |
1043 | |
1044 void OverrideWebkitPrefs(RenderViewHost* render_view_host, | |
1045 WebPreferences* prefs) override { | |
1046 prefs->data_saver_enabled = data_saver_enabled_; | |
1047 } | |
1048 | |
1049 private: | |
1050 bool data_saver_enabled_; | |
1051 }; | |
1052 | |
1053 IN_PROC_BROWSER_TEST_F(ServiceWorkerVersionBrowserTest, FetchWithSaveData) { | |
1054 embedded_test_server()->RegisterRequestHandler( | |
1055 base::Bind(&VerifySaveDataHeaderInRequest)); | |
1056 MockContentBrowserClient content_browser_client; | |
1057 content_browser_client.set_data_saver_enabled(true); | |
1058 ContentBrowserClient* old_client = | |
1059 SetBrowserClientForTesting(&content_browser_client); | |
1060 InstallTestHelper("/service_worker/fetch_in_install.js", SERVICE_WORKER_OK); | |
1061 SetBrowserClientForTesting(old_client); | |
1062 } | |
1063 | |
1064 IN_PROC_BROWSER_TEST_F(ServiceWorkerVersionBrowserTest, | |
1065 RequestWorkerScriptWithDataSaver) { | |
falken
2016/03/09 02:05:36
naming nit: XXXWithSaveData to be consistent with
bengr
2016/03/17 22:24:37
Done.
| |
1066 embedded_test_server()->RegisterRequestHandler( | |
1067 base::Bind(&VerifySaveDataHeaderInRequest)); | |
1068 MockContentBrowserClient content_browser_client; | |
1069 content_browser_client.set_data_saver_enabled(true); | |
1070 ContentBrowserClient* old_client = | |
1071 SetBrowserClientForTesting(&content_browser_client); | |
1072 InstallTestHelper("/service_worker/generated_sw.js", SERVICE_WORKER_OK); | |
1073 SetBrowserClientForTesting(old_client); | |
1074 } | |
1075 | |
1076 IN_PROC_BROWSER_TEST_F(ServiceWorkerVersionBrowserTest, FetchWithoutSaveData) { | |
1077 embedded_test_server()->RegisterRequestHandler( | |
1078 base::Bind(&VerifySaveDataHeaderNotInRequest)); | |
1079 MockContentBrowserClient content_browser_client; | |
1080 ContentBrowserClient* old_client = | |
1081 SetBrowserClientForTesting(&content_browser_client); | |
1082 InstallTestHelper("/service_worker/fetch_in_install.js", SERVICE_WORKER_OK); | |
1083 SetBrowserClientForTesting(old_client); | |
1084 } | |
1085 | |
1086 IN_PROC_BROWSER_TEST_F(ServiceWorkerBrowserTest, FetchPageWithSaveData) { | |
1087 const char kPageUrl[] = "/service_worker/handle_fetch.html"; | |
1088 const char kWorkerUrl[] = "/service_worker/add_save_data_to_title.js"; | |
1089 MockContentBrowserClient content_browser_client; | |
1090 content_browser_client.set_data_saver_enabled(true); | |
1091 ContentBrowserClient* old_client = | |
1092 SetBrowserClientForTesting(&content_browser_client); | |
1093 shell()->web_contents()->GetRenderViewHost()->OnWebkitPreferencesChanged(); | |
1094 scoped_refptr<WorkerActivatedObserver> observer = | |
1095 new WorkerActivatedObserver(wrapper()); | |
1096 observer->Init(); | |
1097 public_context()->RegisterServiceWorker( | |
1098 embedded_test_server()->GetURL(kPageUrl), | |
1099 embedded_test_server()->GetURL(kWorkerUrl), | |
1100 base::Bind(&ExpectResultAndRun, true, base::Bind(&base::DoNothing))); | |
1101 observer->Wait(); | |
1102 | |
1103 const base::string16 title1 = base::ASCIIToUTF16("save-data=on"); | |
1104 TitleWatcher title_watcher1(shell()->web_contents(), title1); | |
1105 NavigateToURL(shell(), embedded_test_server()->GetURL(kPageUrl)); | |
1106 EXPECT_EQ(title1, title_watcher1.WaitAndGetTitle()); | |
1107 | |
1108 SetBrowserClientForTesting(old_client); | |
1109 shell()->Close(); | |
1110 | |
1111 base::RunLoop run_loop; | |
1112 public_context()->UnregisterServiceWorker( | |
1113 embedded_test_server()->GetURL(kPageUrl), | |
1114 base::Bind(&ExpectResultAndRun, true, run_loop.QuitClosure())); | |
1115 run_loop.Run(); | |
1116 } | |
1117 | |
1118 IN_PROC_BROWSER_TEST_F(ServiceWorkerBrowserTest, | |
1119 FetchPageWithSaveDataPassThroughOnFetch) { | |
1120 const char kPageUrl[] = "/service_worker/pass_through_fetch.html"; | |
1121 const char kWorkerUrl[] = "/service_worker/fetch_event_pass_through.js"; | |
1122 MockContentBrowserClient content_browser_client; | |
1123 content_browser_client.set_data_saver_enabled(true); | |
1124 ContentBrowserClient* old_client = | |
1125 SetBrowserClientForTesting(&content_browser_client); | |
1126 shell()->web_contents()->GetRenderViewHost()->OnWebkitPreferencesChanged(); | |
1127 scoped_refptr<WorkerActivatedObserver> observer = | |
1128 new WorkerActivatedObserver(wrapper()); | |
1129 observer->Init(); | |
1130 public_context()->RegisterServiceWorker( | |
1131 embedded_test_server()->GetURL(kPageUrl), | |
1132 embedded_test_server()->GetURL(kWorkerUrl), | |
1133 base::Bind(&ExpectResultAndRun, true, base::Bind(&base::DoNothing))); | |
1134 observer->Wait(); | |
1135 | |
1136 embedded_test_server()->RegisterRequestHandler( | |
1137 base::Bind(&VerifySaveDataHeaderInRequest)); | |
1138 | |
1139 NavigateToURLBlockUntilNavigationsComplete( | |
1140 shell(), embedded_test_server()->GetURL(kPageUrl), 1); | |
1141 | |
1142 SetBrowserClientForTesting(old_client); | |
1143 shell()->Close(); | |
1144 | |
1145 base::RunLoop run_loop; | |
1146 public_context()->UnregisterServiceWorker( | |
1147 embedded_test_server()->GetURL(kPageUrl), | |
1148 base::Bind(&ExpectResultAndRun, true, run_loop.QuitClosure())); | |
1149 run_loop.Run(); | |
1150 } | |
1151 | |
1005 IN_PROC_BROWSER_TEST_F(ServiceWorkerBrowserTest, Reload) { | 1152 IN_PROC_BROWSER_TEST_F(ServiceWorkerBrowserTest, Reload) { |
1006 const char kPageUrl[] = "/service_worker/reload.html"; | 1153 const char kPageUrl[] = "/service_worker/reload.html"; |
1007 const char kWorkerUrl[] = "/service_worker/fetch_event_reload.js"; | 1154 const char kWorkerUrl[] = "/service_worker/fetch_event_reload.js"; |
1008 scoped_refptr<WorkerActivatedObserver> observer = | 1155 scoped_refptr<WorkerActivatedObserver> observer = |
1009 new WorkerActivatedObserver(wrapper()); | 1156 new WorkerActivatedObserver(wrapper()); |
1010 observer->Init(); | 1157 observer->Init(); |
1011 public_context()->RegisterServiceWorker( | 1158 public_context()->RegisterServiceWorker( |
1012 embedded_test_server()->GetURL(kPageUrl), | 1159 embedded_test_server()->GetURL(kPageUrl), |
1013 embedded_test_server()->GetURL(kWorkerUrl), | 1160 embedded_test_server()->GetURL(kWorkerUrl), |
1014 base::Bind(&ExpectResultAndRun, true, base::Bind(&base::DoNothing))); | 1161 base::Bind(&ExpectResultAndRun, true, base::Bind(&base::DoNothing))); |
(...skipping 322 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1337 ASSERT_EQ(SERVICE_WORKER_OK, status); | 1484 ASSERT_EQ(SERVICE_WORKER_OK, status); |
1338 // Stop the worker. | 1485 // Stop the worker. |
1339 StopWorker(SERVICE_WORKER_OK); | 1486 StopWorker(SERVICE_WORKER_OK); |
1340 // Restart the worker. | 1487 // Restart the worker. |
1341 StartWorker(SERVICE_WORKER_OK); | 1488 StartWorker(SERVICE_WORKER_OK); |
1342 // Stop the worker. | 1489 // Stop the worker. |
1343 StopWorker(SERVICE_WORKER_OK); | 1490 StopWorker(SERVICE_WORKER_OK); |
1344 } | 1491 } |
1345 | 1492 |
1346 } // namespace content | 1493 } // namespace content |
OLD | NEW |