OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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/command_line.h" | 5 #include "base/command_line.h" |
6 #include "base/memory/ptr_util.h" | 6 #include "base/memory/ptr_util.h" |
7 #include "base/memory/ref_counted.h" | 7 #include "base/memory/ref_counted.h" |
8 #include "base/strings/string16.h" | 8 #include "base/strings/string16.h" |
9 #include "base/strings/string_split.h" | 9 #include "base/strings/string_split.h" |
10 #include "base/strings/stringprintf.h" | 10 #include "base/strings/stringprintf.h" |
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
66 | 66 |
67 class NoStatePrefetchBrowserTest | 67 class NoStatePrefetchBrowserTest |
68 : public test_utils::PrerenderInProcessBrowserTest { | 68 : public test_utils::PrerenderInProcessBrowserTest { |
69 public: | 69 public: |
70 NoStatePrefetchBrowserTest() {} | 70 NoStatePrefetchBrowserTest() {} |
71 | 71 |
72 void SetUpOnMainThread() override { | 72 void SetUpOnMainThread() override { |
73 test_utils::PrerenderInProcessBrowserTest::SetUpOnMainThread(); | 73 test_utils::PrerenderInProcessBrowserTest::SetUpOnMainThread(); |
74 PrerenderManager::SetMode( | 74 PrerenderManager::SetMode( |
75 PrerenderManager::PRERENDER_MODE_NOSTATE_PREFETCH); | 75 PrerenderManager::PRERENDER_MODE_NOSTATE_PREFETCH); |
| 76 host_resolver()->AddRule("*", "127.0.0.1"); |
76 } | 77 } |
77 | 78 |
78 // Set up a request counter for |path|, which is also the location of the data | 79 // Set up a request counter for |path|, which is also the location of the data |
79 // served by the request. | 80 // served by the request. |
80 void CountRequestFor(const std::string& path_str, RequestCounter* counter) { | 81 void CountRequestFor(const std::string& path_str, RequestCounter* counter) { |
81 url::StringPieceReplacements<base::FilePath::StringType> replacement; | 82 url::StringPieceReplacements<base::FilePath::StringType> replacement; |
82 base::FilePath file_path = base::FilePath::FromUTF8Unsafe(path_str); | 83 base::FilePath file_path = base::FilePath::FromUTF8Unsafe(path_str); |
83 replacement.SetPathStr(file_path.value()); | 84 replacement.SetPathStr(file_path.value()); |
84 const GURL url = src_server()->base_url().ReplaceComponents(replacement); | 85 const GURL url = src_server()->base_url().ReplaceComponents(replacement); |
85 CountRequestForUrl(url, path_str, counter); | 86 CountRequestForUrl(url, path_str, counter); |
(...skipping 175 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
261 GURL main_page_url = | 262 GURL main_page_url = |
262 GetURLWithReplacement(kPrefetchImagePage, "REPLACE_WITH_IMAGE_URL", | 263 GetURLWithReplacement(kPrefetchImagePage, "REPLACE_WITH_IMAGE_URL", |
263 MakeAbsolute(kPrefetchJpeg)); | 264 MakeAbsolute(kPrefetchJpeg)); |
264 PrefetchFromURL(main_page_url, FINAL_STATUS_NOSTATE_PREFETCH_FINISHED); | 265 PrefetchFromURL(main_page_url, FINAL_STATUS_NOSTATE_PREFETCH_FINISHED); |
265 image_counter.WaitForCount(1); | 266 image_counter.WaitForCount(1); |
266 } | 267 } |
267 | 268 |
268 // Checks that a cross-domain prefetching works correctly. | 269 // Checks that a cross-domain prefetching works correctly. |
269 IN_PROC_BROWSER_TEST_F(NoStatePrefetchBrowserTest, PrefetchCrossDomain) { | 270 IN_PROC_BROWSER_TEST_F(NoStatePrefetchBrowserTest, PrefetchCrossDomain) { |
270 static const std::string secondary_domain = "www.foo.com"; | 271 static const std::string secondary_domain = "www.foo.com"; |
271 host_resolver()->AddRule(secondary_domain, "127.0.0.1"); | |
272 GURL cross_domain_url(base::StringPrintf( | 272 GURL cross_domain_url(base::StringPrintf( |
273 "http://%s:%d/%s", secondary_domain.c_str(), | 273 "http://%s:%d/%s", secondary_domain.c_str(), |
274 embedded_test_server()->host_port_pair().port(), kPrefetchPage)); | 274 embedded_test_server()->host_port_pair().port(), kPrefetchPage)); |
275 RequestCounter cross_domain_counter; | 275 RequestCounter cross_domain_counter; |
276 CountRequestForUrl(cross_domain_url, kPrefetchPage, &cross_domain_counter); | 276 CountRequestForUrl(cross_domain_url, kPrefetchPage, &cross_domain_counter); |
277 PrefetchFromURL(cross_domain_url, FINAL_STATUS_NOSTATE_PREFETCH_FINISHED); | 277 PrefetchFromURL(cross_domain_url, FINAL_STATUS_NOSTATE_PREFETCH_FINISHED); |
278 cross_domain_counter.WaitForCount(1); | 278 cross_domain_counter.WaitForCount(1); |
279 } | 279 } |
280 | 280 |
281 // Checks that response header CSP is respected. | 281 // Checks that response header CSP is respected. |
282 IN_PROC_BROWSER_TEST_F(NoStatePrefetchBrowserTest, ResponseHeaderCSP) { | 282 IN_PROC_BROWSER_TEST_F(NoStatePrefetchBrowserTest, ResponseHeaderCSP) { |
283 static const std::string secondary_domain = "foo.bar"; | 283 static const std::string secondary_domain = "foo.bar"; |
284 host_resolver()->AddRule(secondary_domain, "127.0.0.1"); | |
285 RequestCounter main_page; | 284 RequestCounter main_page; |
286 CountRequestFor(kPrefetchResponseHeaderCSP, &main_page); | 285 CountRequestFor(kPrefetchResponseHeaderCSP, &main_page); |
287 RequestCounter first_script; | 286 RequestCounter first_script; |
288 CountRequestFor(kPrefetchScript, &first_script); | 287 CountRequestFor(kPrefetchScript, &first_script); |
289 RequestCounter second_script; | 288 RequestCounter second_script; |
290 GURL second_script_url(std::string("http://foo.bar/") + kPrefetchScript2); | 289 GURL second_script_url(std::string("http://foo.bar/") + kPrefetchScript2); |
291 CountRequestForUrl(second_script_url, kPrefetchScript2, &second_script); | 290 CountRequestForUrl(second_script_url, kPrefetchScript2, &second_script); |
292 PrefetchFromFile(kPrefetchResponseHeaderCSP, | 291 PrefetchFromFile(kPrefetchResponseHeaderCSP, |
293 FINAL_STATUS_NOSTATE_PREFETCH_FINISHED); | 292 FINAL_STATUS_NOSTATE_PREFETCH_FINISHED); |
294 // The second script is in the correct domain for CSP, but the first script is | 293 // The second script is in the correct domain for CSP, but the first script is |
295 // not. | 294 // not. |
296 main_page.WaitForCount(1); | 295 main_page.WaitForCount(1); |
297 second_script.WaitForCount(1); | 296 second_script.WaitForCount(1); |
298 first_script.WaitForCount(0); | 297 first_script.WaitForCount(0); |
299 } | 298 } |
300 | 299 |
301 // Checks that CSP in the meta tag cancels the prefetch. | 300 // Checks that CSP in the meta tag cancels the prefetch. |
302 // TODO(mattcary): probably this behavior should be consistent with | 301 // TODO(mattcary): probably this behavior should be consistent with |
303 // response-header CSP. See crbug/656581. | 302 // response-header CSP. See crbug/656581. |
304 IN_PROC_BROWSER_TEST_F(NoStatePrefetchBrowserTest, MetaTagCSP) { | 303 IN_PROC_BROWSER_TEST_F(NoStatePrefetchBrowserTest, MetaTagCSP) { |
305 static const std::string secondary_domain = "foo.bar"; | 304 static const std::string secondary_domain = "foo.bar"; |
306 host_resolver()->AddRule(secondary_domain, "127.0.0.1"); | |
307 RequestCounter main_page; | 305 RequestCounter main_page; |
308 CountRequestFor(kPrefetchMetaCSP, &main_page); | 306 CountRequestFor(kPrefetchMetaCSP, &main_page); |
309 RequestCounter first_script; | 307 RequestCounter first_script; |
310 CountRequestFor(kPrefetchScript, &first_script); | 308 CountRequestFor(kPrefetchScript, &first_script); |
311 RequestCounter second_script; | 309 RequestCounter second_script; |
312 GURL second_script_url(std::string("http://foo.bar/") + kPrefetchScript2); | 310 GURL second_script_url(std::string("http://foo.bar/") + kPrefetchScript2); |
313 CountRequestForUrl(second_script_url, kPrefetchScript2, &second_script); | 311 CountRequestForUrl(second_script_url, kPrefetchScript2, &second_script); |
314 PrefetchFromFile(kPrefetchMetaCSP, FINAL_STATUS_NOSTATE_PREFETCH_FINISHED); | 312 PrefetchFromFile(kPrefetchMetaCSP, FINAL_STATUS_NOSTATE_PREFETCH_FINISHED); |
315 // TODO(mattcary): See test comment above. If the meta CSP tag were parsed, | 313 // TODO(mattcary): See test comment above. If the meta CSP tag were parsed, |
316 // |second_script| would be loaded. Instead as the background scanner bails as | 314 // |second_script| would be loaded. Instead as the background scanner bails as |
(...skipping 432 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
749 CountRequestFor(kPrefetchPage, &page_counter); | 747 CountRequestFor(kPrefetchPage, &page_counter); |
750 RequestCounter script_counter; | 748 RequestCounter script_counter; |
751 CountRequestFor(kPrefetchScript, &script_counter); | 749 CountRequestFor(kPrefetchScript, &script_counter); |
752 PrefetchFromURL(prefetch_page_url, FINAL_STATUS_NOSTATE_PREFETCH_FINISHED); | 750 PrefetchFromURL(prefetch_page_url, FINAL_STATUS_NOSTATE_PREFETCH_FINISHED); |
753 // Neither the page nor the script should be prefetched. | 751 // Neither the page nor the script should be prefetched. |
754 script_counter.WaitForCount(0); | 752 script_counter.WaitForCount(0); |
755 page_counter.WaitForCount(0); | 753 page_counter.WaitForCount(0); |
756 } | 754 } |
757 | 755 |
758 } // namespace prerender | 756 } // namespace prerender |
OLD | NEW |