Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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 "chrome/browser/predictors/resource_prefetch_predictor.h" | 6 #include "chrome/browser/predictors/resource_prefetch_predictor.h" |
| 7 #include "chrome/browser/predictors/resource_prefetch_predictor_factory.h" | 7 #include "chrome/browser/predictors/resource_prefetch_predictor_factory.h" |
| 8 #include "chrome/browser/predictors/resource_prefetch_predictor_test_util.h" | 8 #include "chrome/browser/predictors/resource_prefetch_predictor_test_util.h" |
| 9 #include "chrome/browser/profiles/profile.h" | 9 #include "chrome/browser/profiles/profile.h" |
| 10 #include "chrome/browser/ui/browser.h" | 10 #include "chrome/browser/ui/browser.h" |
| 11 #include "chrome/browser/ui/tabs/tab_strip_model.h" | 11 #include "chrome/browser/ui/tabs/tab_strip_model.h" |
| 12 #include "chrome/common/chrome_switches.h" | 12 #include "chrome/common/chrome_switches.h" |
| 13 #include "chrome/test/base/in_process_browser_test.h" | 13 #include "chrome/test/base/in_process_browser_test.h" |
| 14 #include "chrome/test/base/ui_test_utils.h" | 14 #include "chrome/test/base/ui_test_utils.h" |
| 15 #include "content/public/browser/render_frame_host.h" | 15 #include "content/public/browser/render_frame_host.h" |
| 16 #include "content/public/browser/render_process_host.h" | 16 #include "content/public/browser/render_process_host.h" |
| 17 #include "net/test/embedded_test_server/http_request.h" | 17 #include "net/test/embedded_test_server/http_request.h" |
| 18 #include "net/test/embedded_test_server/http_response.h" | 18 #include "net/test/embedded_test_server/http_response.h" |
| 19 #include "testing/gmock/include/gmock/gmock.h" | 19 #include "testing/gmock/include/gmock/gmock.h" |
| 20 #include "testing/gtest/include/gtest/gtest.h" | 20 #include "testing/gtest/include/gtest/gtest.h" |
| 21 | 21 |
| 22 namespace predictors { | 22 namespace predictors { |
| 23 | 23 |
| 24 namespace { | 24 namespace { |
| 25 | 25 |
| 26 static const char kImagePath[] = "/predictors/image.png"; | 26 static const char kImagePath[] = "/predictors/image.png"; |
| 27 static const char kImagePath2[] = "/predictors/image2.png"; | |
| 27 static const char kStylePath[] = "/predictors/style.css"; | 28 static const char kStylePath[] = "/predictors/style.css"; |
| 29 static const char kStylePath2[] = "/predictors/style2.css"; | |
|
pasko
2016/11/30 13:52:48
it would be nice (i.e. more readable with less sur
alexilin
2016/11/30 18:43:50
Done.
| |
| 28 static const char kScriptPath[] = "/predictors/script.js"; | 30 static const char kScriptPath[] = "/predictors/script.js"; |
| 31 static const char kScriptPath2[] = "/predictors/script2.js"; | |
| 29 static const char kFontPath[] = "/predictors/font.ttf"; | 32 static const char kFontPath[] = "/predictors/font.ttf"; |
| 30 static const char kHtmlSubresourcesPath[] = | 33 static const char kHtmlSubresourcesPath[] = |
| 31 "/predictors/html_subresources.html"; | 34 "/predictors/html_subresources.html"; |
| 32 static const char kRedirectPath[] = "/predictors/redirect.html"; | 35 static const char kRedirectPath[] = "/predictors/redirect.html"; |
| 33 static const char kRedirectPath2[] = "/predictors/redirect2.html"; | 36 static const char kRedirectPath2[] = "/predictors/redirect2.html"; |
| 34 static const char kRedirectPath3[] = "/predictors/redirect3.html"; | 37 static const char kRedirectPath3[] = "/predictors/redirect3.html"; |
| 38 static const char kHtmlScriptPath[] = "/predictors/html_script.html"; | |
| 39 static const char kHtmlIframePath[] = "/predictors/html_iframe.html"; | |
| 35 | 40 |
| 36 struct ResourceSummary { | 41 struct ResourceSummary { |
| 37 ResourceSummary() : is_no_store(false), version(0) {} | 42 ResourceSummary() |
| 43 : is_no_store(false), version(0), should_be_recorded(true) {} | |
| 38 | 44 |
| 39 ResourcePrefetchPredictor::URLRequestSummary request; | 45 ResourcePrefetchPredictor::URLRequestSummary request; |
| 40 std::string content; | 46 std::string content; |
| 41 bool is_no_store; | 47 bool is_no_store; |
| 42 size_t version; | 48 size_t version; |
| 49 bool should_be_recorded; | |
| 50 | |
| 51 ResourceSummary* WithContent(const std::string& i_content) { | |
|
pasko
2016/11/30 13:52:47
we do not make prefixed names in chromium codebase
alexilin
2016/11/30 18:43:50
Not quite true:
https://cs.chromium.org/chromium/s
| |
| 52 content = i_content; | |
| 53 return this; | |
| 54 } | |
| 55 | |
| 56 ResourceSummary* NotRecorded() { | |
| 57 should_be_recorded = false; | |
| 58 return this; | |
|
pasko
2016/11/30 13:52:48
while I appreciate the added elegance, returning |
alexilin
2016/11/30 18:43:50
Done.
| |
| 59 } | |
| 43 }; | 60 }; |
| 44 | 61 |
| 45 struct RedirectEdge { | 62 struct RedirectEdge { |
| 46 // This response code should be returned by previous url in the chain. | 63 // This response code should be returned by previous url in the chain. |
| 47 net::HttpStatusCode code; | 64 net::HttpStatusCode code; |
| 48 GURL url; | 65 GURL url; |
| 49 }; | 66 }; |
| 50 | 67 |
| 51 class InitializationObserver : public TestObserver { | 68 class InitializationObserver : public TestObserver { |
| 52 public: | 69 public: |
| (...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 145 predictor_ = | 162 predictor_ = |
| 146 ResourcePrefetchPredictorFactory::GetForProfile(browser()->profile()); | 163 ResourcePrefetchPredictorFactory::GetForProfile(browser()->profile()); |
| 147 ASSERT_TRUE(predictor_); | 164 ASSERT_TRUE(predictor_); |
| 148 EnsurePredictorInitialized(); | 165 EnsurePredictorInitialized(); |
| 149 } | 166 } |
| 150 | 167 |
| 151 void NavigateToURLAndCheckSubresources(const GURL& main_frame_url) { | 168 void NavigateToURLAndCheckSubresources(const GURL& main_frame_url) { |
| 152 GURL endpoint_url = GetRedirectEndpoint(main_frame_url); | 169 GURL endpoint_url = GetRedirectEndpoint(main_frame_url); |
| 153 std::vector<URLRequestSummary> url_request_summaries; | 170 std::vector<URLRequestSummary> url_request_summaries; |
| 154 for (const auto& kv : resources_) { | 171 for (const auto& kv : resources_) { |
| 155 if (kv.second.is_no_store) | 172 if (kv.second.is_no_store || !kv.second.should_be_recorded) |
|
pasko
2016/11/30 13:52:47
what does "should_be_recorded" mean? Can we EXPECT
alexilin
2016/11/30 18:43:50
It means that this resource should or should not b
| |
| 156 continue; | 173 continue; |
| 157 url_request_summaries.push_back( | 174 url_request_summaries.push_back( |
| 158 GetURLRequestSummaryForResource(endpoint_url, kv.second)); | 175 GetURLRequestSummaryForResource(endpoint_url, kv.second)); |
| 159 } | 176 } |
| 160 ResourcePrefetchPredictorTestObserver observer( | 177 ResourcePrefetchPredictorTestObserver observer( |
| 161 predictor_, UpdateAndGetVisitCount(main_frame_url), | 178 predictor_, UpdateAndGetVisitCount(main_frame_url), |
| 162 CreatePageRequestSummary(endpoint_url.spec(), main_frame_url.spec(), | 179 CreatePageRequestSummary(endpoint_url.spec(), main_frame_url.spec(), |
| 163 url_request_summaries)); | 180 url_request_summaries)); |
| 164 ui_test_utils::NavigateToURL(browser(), main_frame_url); | 181 ui_test_utils::NavigateToURL(browser(), main_frame_url); |
| 165 observer.Wait(); | 182 observer.Wait(); |
| (...skipping 199 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 365 net::LOWEST); | 382 net::LOWEST); |
| 366 AddResource(https_server()->GetURL(kStylePath), | 383 AddResource(https_server()->GetURL(kStylePath), |
| 367 content::RESOURCE_TYPE_STYLESHEET, net::HIGHEST); | 384 content::RESOURCE_TYPE_STYLESHEET, net::HIGHEST); |
| 368 AddResource(https_server()->GetURL(kScriptPath), | 385 AddResource(https_server()->GetURL(kScriptPath), |
| 369 content::RESOURCE_TYPE_SCRIPT, net::MEDIUM); | 386 content::RESOURCE_TYPE_SCRIPT, net::MEDIUM); |
| 370 AddResource(https_server()->GetURL(kFontPath), | 387 AddResource(https_server()->GetURL(kFontPath), |
| 371 content::RESOURCE_TYPE_FONT_RESOURCE, net::HIGHEST); | 388 content::RESOURCE_TYPE_FONT_RESOURCE, net::HIGHEST); |
| 372 NavigateToURLAndCheckSubresources(GetURL(kRedirectPath)); | 389 NavigateToURLAndCheckSubresources(GetURL(kRedirectPath)); |
| 373 } | 390 } |
| 374 | 391 |
| 392 IN_PROC_BROWSER_TEST_F(ResourcePrefetchPredictorBrowserTest, | |
| 393 LearningJavascriptOriginated) { | |
| 394 AddResource(GetURL(kScriptPath), content::RESOURCE_TYPE_SCRIPT, net::MEDIUM) | |
| 395 ->WithContent( | |
| 396 "document.write(\'<img src=\"image.png\">\');" | |
|
pasko
2016/11/30 13:52:48
Code in one language containing code in another la
alexilin
2016/11/30 18:43:50
Acknowledged.
| |
| 397 "document.write(\'<link rel=\"stylesheet\" href=\"style.css\">\');" | |
|
pasko
2016/11/30 13:52:48
there are other ways for JS to request a resource:
alexilin
2016/11/30 18:43:50
I'm not sure that it's secure to perform DOM manip
| |
| 398 "document.write(\'<script src=\"script2.js\"></script>\');"); | |
|
pasko
2016/11/30 13:52:48
creating a file for this would be preferable. Is t
alexilin
2016/11/30 18:43:50
Done.
| |
| 399 AddResource(GetURL(kImagePath), content::RESOURCE_TYPE_IMAGE, net::LOWEST); | |
| 400 AddResource(GetURL(kStylePath), content::RESOURCE_TYPE_STYLESHEET, | |
| 401 net::HIGHEST); | |
| 402 AddResource(GetURL(kScriptPath2), content::RESOURCE_TYPE_SCRIPT, net::MEDIUM); | |
| 403 NavigateToURLAndCheckSubresources(GetURL(kHtmlScriptPath)); | |
| 404 } | |
| 405 | |
| 406 // Javascript fetch requests are ignored by the ResourcePrefetchPredictor | |
| 407 // because they have unsupported resource type content::RESOURCE_TYPE_XHR. | |
|
pasko
2016/11/30 13:52:47
what's our status on fixing it? is there a bug? If
alexilin
2016/11/30 18:43:50
Well, I just discovered it by writing this test so
| |
| 408 IN_PROC_BROWSER_TEST_F(ResourcePrefetchPredictorBrowserTest, | |
| 409 LearningIgnoringJavascriptFetches) { | |
| 410 AddResource(GetURL(kScriptPath), content::RESOURCE_TYPE_SCRIPT, net::MEDIUM) | |
| 411 ->WithContent( | |
| 412 "fetch(\"image.png\"); fetch(\"style.css\"); fetch(\"script2.js\")"); | |
| 413 AddResource(GetURL(kImagePath), content::RESOURCE_TYPE_IMAGE, net::LOWEST) | |
| 414 ->NotRecorded(); | |
| 415 AddResource(GetURL(kStylePath), content::RESOURCE_TYPE_STYLESHEET, | |
| 416 net::HIGHEST) | |
| 417 ->NotRecorded(); | |
| 418 AddResource(GetURL(kScriptPath2), content::RESOURCE_TYPE_SCRIPT, net::MEDIUM) | |
| 419 ->NotRecorded(); | |
| 420 NavigateToURLAndCheckSubresources(GetURL(kHtmlScriptPath)); | |
| 421 } | |
| 422 | |
| 423 // ResourcePrefetchPredictor ignores all resources requested from subframes. | |
| 424 IN_PROC_BROWSER_TEST_F(ResourcePrefetchPredictorBrowserTest, | |
| 425 LearningWithIframe) { | |
| 426 // Included from html_iframe.html. | |
| 427 AddResource(GetURL(kImagePath2), content::RESOURCE_TYPE_IMAGE, net::LOWEST); | |
| 428 AddResource(GetURL(kStylePath2), content::RESOURCE_TYPE_STYLESHEET, | |
| 429 net::HIGHEST); | |
| 430 AddResource(GetURL(kScriptPath2), content::RESOURCE_TYPE_SCRIPT, net::MEDIUM); | |
| 431 // Included from <iframe src="html_subresources.html"> and not recored. | |
| 432 AddResource(GetURL(kImagePath), content::RESOURCE_TYPE_IMAGE, net::LOWEST) | |
| 433 ->NotRecorded(); | |
| 434 AddResource(GetURL(kStylePath), content::RESOURCE_TYPE_STYLESHEET, | |
| 435 net::HIGHEST) | |
| 436 ->NotRecorded(); | |
| 437 AddResource(GetURL(kScriptPath), content::RESOURCE_TYPE_SCRIPT, net::MEDIUM) | |
| 438 ->NotRecorded(); | |
| 439 AddResource(GetURL(kFontPath), content::RESOURCE_TYPE_FONT_RESOURCE, | |
| 440 net::HIGHEST) | |
| 441 ->NotRecorded(); | |
| 442 NavigateToURLAndCheckSubresources(GetURL(kHtmlIframePath)); | |
| 443 } | |
| 444 | |
| 375 } // namespace predictors | 445 } // namespace predictors |
| OLD | NEW |