| 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/strings/string16.h" | 6 #include "base/strings/string16.h" |
| 7 #include "base/strings/string_split.h" | 7 #include "base/strings/string_split.h" |
| 8 #include "base/task_scheduler/post_task.h" | 8 #include "base/task_scheduler/post_task.h" |
| 9 #include "base/threading/platform_thread.h" // For |Sleep()|. | |
| 10 #include "chrome/browser/prerender/prerender_manager.h" | 9 #include "chrome/browser/prerender/prerender_manager.h" |
| 11 #include "chrome/browser/prerender/prerender_manager_factory.h" | 10 #include "chrome/browser/prerender/prerender_manager_factory.h" |
| 12 #include "chrome/browser/prerender/prerender_test_utils.h" | 11 #include "chrome/browser/prerender/prerender_test_utils.h" |
| 13 #include "chrome/browser/profiles/profile.h" | 12 #include "chrome/browser/profiles/profile.h" |
| 14 #include "chrome/browser/task_manager/task_manager_browsertest_util.h" | 13 #include "chrome/browser/task_manager/task_manager_browsertest_util.h" |
| 15 #include "chrome/browser/ui/browser.h" | 14 #include "chrome/browser/ui/browser.h" |
| 16 #include "chrome/browser/ui/browser_commands.h" | 15 #include "chrome/browser/ui/browser_commands.h" |
| 17 #include "chrome/browser/ui/tabs/tab_strip_model.h" | 16 #include "chrome/browser/ui/tabs/tab_strip_model.h" |
| 18 #include "chrome/common/chrome_switches.h" | 17 #include "chrome/common/chrome_switches.h" |
| 19 #include "chrome/grit/generated_resources.h" | 18 #include "chrome/grit/generated_resources.h" |
| (...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 70 } // namespace | 69 } // namespace |
| 71 | 70 |
| 72 namespace prerender { | 71 namespace prerender { |
| 73 | 72 |
| 74 // These URLs used for test resources must be relative with the exception of | 73 // These URLs used for test resources must be relative with the exception of |
| 75 // |PrefetchLoaderPath|, which is only used in |PrerenderTestURLImpl()|. | 74 // |PrefetchLoaderPath|, which is only used in |PrerenderTestURLImpl()|. |
| 76 const char kPrefetchImagePage[] = "prerender/prefetch_image.html"; | 75 const char kPrefetchImagePage[] = "prerender/prefetch_image.html"; |
| 77 const char kPrefetchJpeg[] = "prerender/image.jpeg"; | 76 const char kPrefetchJpeg[] = "prerender/image.jpeg"; |
| 78 const char kPrefetchLoaderPath[] = "/prerender/prefetch_loader.html"; | 77 const char kPrefetchLoaderPath[] = "/prerender/prefetch_loader.html"; |
| 79 const char kPrefetchLoopPage[] = "prerender/prefetch_loop.html"; | 78 const char kPrefetchLoopPage[] = "prerender/prefetch_loop.html"; |
| 79 const char kPrefetchMetaCSP[] = "prerender/prefetch_meta_csp.html"; |
| 80 const char kPrefetchPage[] = "prerender/prefetch_page.html"; | 80 const char kPrefetchPage[] = "prerender/prefetch_page.html"; |
| 81 const char kPrefetchPage2[] = "prerender/prefetch_page2.html"; | 81 const char kPrefetchPage2[] = "prerender/prefetch_page2.html"; |
| 82 const char kPrefetchPng[] = "prerender/image.png"; | 82 const char kPrefetchPng[] = "prerender/image.png"; |
| 83 const char kPrefetchResponseHeaderCSP[] = |
| 84 "prerender/prefetch_response_csp.html"; |
| 83 const char kPrefetchScript[] = "prerender/prefetch.js"; | 85 const char kPrefetchScript[] = "prerender/prefetch.js"; |
| 84 const char kPrefetchScript2[] = "prerender/prefetch2.js"; | 86 const char kPrefetchScript2[] = "prerender/prefetch2.js"; |
| 85 const char kPrefetchSubresourceRedirectPage[] = | 87 const char kPrefetchSubresourceRedirectPage[] = |
| 86 "prerender/prefetch_subresource_redirect.html"; | 88 "prerender/prefetch_subresource_redirect.html"; |
| 87 | 89 |
| 88 const char kPageBool[] = "pageBool"; | 90 const char kPageBool[] = "pageBool"; |
| 89 const char kScriptBool[] = "scriptBool"; | 91 const char kScriptBool[] = "scriptBool"; |
| 90 | 92 |
| 91 class NoStatePrefetchBrowserTest | 93 class NoStatePrefetchBrowserTest |
| 92 : public test_utils::PrerenderInProcessBrowserTest { | 94 : public test_utils::PrerenderInProcessBrowserTest { |
| (...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 141 GetPrerenderManager()->SetTimeOverride(std::move(test_time)); | 143 GetPrerenderManager()->SetTimeOverride(std::move(test_time)); |
| 142 } | 144 } |
| 143 | 145 |
| 144 // Set up a request counter for |path|, which is also the location of the data | 146 // Set up a request counter for |path|, which is also the location of the data |
| 145 // served by the request. | 147 // served by the request. |
| 146 void CountRequestFor(const std::string& path_str, RequestCounter* counter) { | 148 void CountRequestFor(const std::string& path_str, RequestCounter* counter) { |
| 147 url::StringPieceReplacements<base::FilePath::StringType> replacement; | 149 url::StringPieceReplacements<base::FilePath::StringType> replacement; |
| 148 base::FilePath file_path = base::FilePath::FromUTF8Unsafe(path_str); | 150 base::FilePath file_path = base::FilePath::FromUTF8Unsafe(path_str); |
| 149 replacement.SetPathStr(file_path.value()); | 151 replacement.SetPathStr(file_path.value()); |
| 150 const GURL url = src_server()->base_url().ReplaceComponents(replacement); | 152 const GURL url = src_server()->base_url().ReplaceComponents(replacement); |
| 151 CountRequestFor(url, path_str, counter); | 153 CountRequestForUrl(url, path_str, counter); |
| 152 } | 154 } |
| 153 | 155 |
| 154 // As above, but specify the data path and URL separately. | 156 // As above, but specify the data path and URL separately. |
| 155 void CountRequestFor(const GURL& url, | 157 void CountRequestForUrl(const GURL& url, |
| 156 const std::string& path_str, | 158 const std::string& path_str, |
| 157 RequestCounter* counter) { | 159 RequestCounter* counter) { |
| 158 base::FilePath url_file = ui_test_utils::GetTestFilePath( | 160 base::FilePath url_file = ui_test_utils::GetTestFilePath( |
| 159 base::FilePath(), base::FilePath::FromUTF8Unsafe(path_str)); | 161 base::FilePath(), base::FilePath::FromUTF8Unsafe(path_str)); |
| 160 content::BrowserThread::PostTask( | 162 content::BrowserThread::PostTask( |
| 161 content::BrowserThread::IO, FROM_HERE, | 163 content::BrowserThread::IO, FROM_HERE, |
| 162 base::Bind(&CreateCountingInterceptorOnIO, url, url_file, | 164 base::Bind(&CreateCountingInterceptorOnIO, url, url_file, |
| 163 counter->AsWeakPtr())); | 165 counter->AsWeakPtr())); |
| 164 } | 166 } |
| 165 | 167 |
| 166 BrowserTestTime* GetTimeOverride() const { return browser_test_time_; } | 168 BrowserTestTime* GetTimeOverride() const { return browser_test_time_; } |
| 167 | 169 |
| (...skipping 97 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 265 } | 267 } |
| 266 | 268 |
| 267 // Checks that a cross-domain prefetching works correctly. | 269 // Checks that a cross-domain prefetching works correctly. |
| 268 IN_PROC_BROWSER_TEST_F(NoStatePrefetchBrowserTest, PrefetchCrossDomain) { | 270 IN_PROC_BROWSER_TEST_F(NoStatePrefetchBrowserTest, PrefetchCrossDomain) { |
| 269 static const std::string secondary_domain = "www.foo.com"; | 271 static const std::string secondary_domain = "www.foo.com"; |
| 270 host_resolver()->AddRule(secondary_domain, "127.0.0.1"); | 272 host_resolver()->AddRule(secondary_domain, "127.0.0.1"); |
| 271 GURL cross_domain_url(base::StringPrintf( | 273 GURL cross_domain_url(base::StringPrintf( |
| 272 "http://%s:%d/%s", secondary_domain.c_str(), | 274 "http://%s:%d/%s", secondary_domain.c_str(), |
| 273 embedded_test_server()->host_port_pair().port(), kPrefetchPage)); | 275 embedded_test_server()->host_port_pair().port(), kPrefetchPage)); |
| 274 RequestCounter cross_domain_counter; | 276 RequestCounter cross_domain_counter; |
| 275 CountRequestFor(cross_domain_url, kPrefetchPage, &cross_domain_counter); | 277 CountRequestForUrl(cross_domain_url, kPrefetchPage, &cross_domain_counter); |
| 276 PrerenderTestURL(cross_domain_url, FINAL_STATUS_APP_TERMINATING, 1); | 278 PrerenderTestURL(cross_domain_url, FINAL_STATUS_APP_TERMINATING, 1); |
| 277 cross_domain_counter.WaitForCount(1); | 279 cross_domain_counter.WaitForCount(1); |
| 278 } | 280 } |
| 279 | 281 |
| 282 // Checks that response header CSP is respected. |
| 283 IN_PROC_BROWSER_TEST_F(NoStatePrefetchBrowserTest, ResponseHeaderCSP) { |
| 284 static const std::string secondary_domain = "foo.bar"; |
| 285 host_resolver()->AddRule(secondary_domain, "127.0.0.1"); |
| 286 RequestCounter main_page; |
| 287 CountRequestFor(kPrefetchResponseHeaderCSP, &main_page); |
| 288 RequestCounter first_script; |
| 289 CountRequestFor(kPrefetchScript, &first_script); |
| 290 RequestCounter second_script; |
| 291 GURL second_script_url(std::string("http://foo.bar/") + kPrefetchScript2); |
| 292 CountRequestForUrl(second_script_url, kPrefetchScript2, &second_script); |
| 293 PrerenderTestURL(kPrefetchResponseHeaderCSP, FINAL_STATUS_APP_TERMINATING, 1); |
| 294 // The second script is in the correct domain for CSP, but the first script is |
| 295 // not. |
| 296 main_page.WaitForCount(1); |
| 297 second_script.WaitForCount(1); |
| 298 // TODO(pasko): wait for prefetch to be finished before checking the counts. |
| 299 first_script.WaitForCount(0); |
| 300 } |
| 301 |
| 302 // Checks that CSP in the meta tag cancels the prefetch. |
| 303 // TODO(mattcary): probably this behavior should be consistent with |
| 304 // response-header CSP. See crbug/656581. |
| 305 IN_PROC_BROWSER_TEST_F(NoStatePrefetchBrowserTest, MetaTagCSP) { |
| 306 static const std::string secondary_domain = "foo.bar"; |
| 307 host_resolver()->AddRule(secondary_domain, "127.0.0.1"); |
| 308 RequestCounter main_page; |
| 309 CountRequestFor(kPrefetchMetaCSP, &main_page); |
| 310 RequestCounter first_script; |
| 311 CountRequestFor(kPrefetchScript, &first_script); |
| 312 RequestCounter second_script; |
| 313 GURL second_script_url(std::string("http://foo.bar/") + kPrefetchScript2); |
| 314 CountRequestForUrl(second_script_url, kPrefetchScript2, &second_script); |
| 315 PrerenderTestURL(kPrefetchMetaCSP, FINAL_STATUS_APP_TERMINATING, 1); |
| 316 // TODO(mattcary): See test comment above. If the meta CSP tag were parsed, |
| 317 // |second_script| would be loaded. Instead as the background scanner bails as |
| 318 // soon as the meta CSP tag is seen, only |main_page| is fetched. |
| 319 main_page.WaitForCount(1); |
| 320 // TODO(pasko): wait for prefetch to be finished before checking the counts. |
| 321 second_script.WaitForCount(0); |
| 322 first_script.WaitForCount(0); |
| 323 } |
| 324 |
| 280 // Checks simultaneous prefetch. | 325 // Checks simultaneous prefetch. |
| 281 IN_PROC_BROWSER_TEST_F(NoStatePrefetchBrowserTest, PrefetchSimultaneous) { | 326 IN_PROC_BROWSER_TEST_F(NoStatePrefetchBrowserTest, PrefetchSimultaneous) { |
| 282 RequestCounter first_main_counter; | 327 RequestCounter first_main_counter; |
| 283 CountRequestFor(kPrefetchPage, &first_main_counter); | 328 CountRequestFor(kPrefetchPage, &first_main_counter); |
| 284 RequestCounter second_main_counter; | 329 RequestCounter second_main_counter; |
| 285 CountRequestFor(kPrefetchPage2, &second_main_counter); | 330 CountRequestFor(kPrefetchPage2, &second_main_counter); |
| 286 RequestCounter first_script_counter; | 331 RequestCounter first_script_counter; |
| 287 CountRequestFor(kPrefetchScript, &first_script_counter); | 332 CountRequestFor(kPrefetchScript, &first_script_counter); |
| 288 RequestCounter second_script_counter; | 333 RequestCounter second_script_counter; |
| 289 CountRequestFor(kPrefetchScript2, &second_script_counter); | 334 CountRequestFor(kPrefetchScript2, &second_script_counter); |
| (...skipping 197 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 487 // special way. | 532 // special way. |
| 488 // TODO(mattcary): since the prerender will count itself as loaded even if the | 533 // TODO(mattcary): since the prerender will count itself as loaded even if the |
| 489 // fetch of the main resource fails, the test doesn't actually confirm what we | 534 // fetch of the main resource fails, the test doesn't actually confirm what we |
| 490 // want it to confirm. This may be fixed by planned changes to the prerender | 535 // want it to confirm. This may be fixed by planned changes to the prerender |
| 491 // lifecycle. | 536 // lifecycle. |
| 492 std::unique_ptr<TestPrerender> prerender = | 537 std::unique_ptr<TestPrerender> prerender = |
| 493 PrerenderTestURL(kPrefetchPage, FINAL_STATUS_SAFE_BROWSING, 1); | 538 PrerenderTestURL(kPrefetchPage, FINAL_STATUS_SAFE_BROWSING, 1); |
| 494 } | 539 } |
| 495 | 540 |
| 496 } // namespace prerender | 541 } // namespace prerender |
| OLD | NEW |