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 // Paths to resources handled by a custom request handler. They return empty |
| 27 static const char kStylePath[] = "/predictors/style.css"; | 27 // responses with controllable response headers. |
| 28 static const char kScriptPath[] = "/predictors/script.js"; | 28 static const char kImagePath[] = "/handled-by-test/image.png"; |
| 29 static const char kFontPath[] = "/predictors/font.ttf"; | 29 static const char kImagePath2[] = "/handled-by-test/image2.png"; |
| 30 static const char kStylePath[] = "/handled-by-test/style.css"; | |
| 31 static const char kStylePath2[] = "/handled-by-test/style2.css"; | |
| 32 static const char kScriptPath[] = "/handled-by-test/script.js"; | |
| 33 static const char kScriptPath2[] = "/handled-by-test/script2.js"; | |
| 34 static const char kFontPath[] = "/handled-by-test/font.ttf"; | |
| 35 static const char kRedirectPath[] = "/handled-by-test/redirect.html"; | |
| 36 static const char kRedirectPath2[] = "/handled-by-test/redirect2.html"; | |
| 37 static const char kRedirectPath3[] = "/handled-by-test/redirect3.html"; | |
| 38 | |
| 39 // These are loaded from a file by the test server. | |
| 30 static const char kHtmlSubresourcesPath[] = | 40 static const char kHtmlSubresourcesPath[] = |
| 31 "/predictors/html_subresources.html"; | 41 "/predictors/html_subresources.html"; |
| 32 static const char kRedirectPath[] = "/predictors/redirect.html"; | 42 static const char kHtmlDocumentWritePath[] = "/predictors/document_write.html"; |
| 33 static const char kRedirectPath2[] = "/predictors/redirect2.html"; | 43 static const char kScriptDocumentWritePath[] = "/predictors/document_write.js"; |
| 34 static const char kRedirectPath3[] = "/predictors/redirect3.html"; | 44 static const char kHtmlAppendChildPath[] = "/predictors/append_child.html"; |
| 45 static const char kScriptAppendChildPath[] = "/predictors/append_child.js"; | |
| 46 static const char kHtmlInnerHtmlPath[] = "/predictors/inner_html.html"; | |
| 47 static const char kScriptInnerHtmlPath[] = "/predictors/inner_html.js"; | |
| 48 static const char kHtmlFetchPath[] = "/predictors/fetch.html"; | |
| 49 static const char kScriptFetchPath[] = "/predictors/fetch.js"; | |
| 50 static const char kHtmlIframePath[] = "/predictors/html_iframe.html"; | |
| 35 | 51 |
| 36 struct ResourceSummary { | 52 struct ResourceSummary { |
| 37 ResourceSummary() : is_no_store(false), version(0) {} | 53 ResourceSummary() |
| 54 : is_no_store(false), | |
| 55 version(0), | |
| 56 is_external(false), | |
| 57 should_be_recorded(true) {} | |
| 38 | 58 |
| 39 ResourcePrefetchPredictor::URLRequestSummary request; | 59 ResourcePrefetchPredictor::URLRequestSummary request; |
| 40 std::string content; | 60 std::string content; |
| 41 bool is_no_store; | 61 bool is_no_store; |
| 42 size_t version; | 62 size_t version; |
| 63 bool is_external; | |
| 64 bool should_be_recorded; | |
| 43 }; | 65 }; |
| 44 | 66 |
| 45 struct RedirectEdge { | 67 struct RedirectEdge { |
| 46 // This response code should be returned by previous url in the chain. | 68 // This response code should be returned by previous url in the chain. |
| 47 net::HttpStatusCode code; | 69 net::HttpStatusCode code; |
| 48 GURL url; | 70 GURL url; |
| 49 }; | 71 }; |
| 50 | 72 |
| 51 class InitializationObserver : public TestObserver { | 73 class InitializationObserver : public TestObserver { |
| 52 public: | 74 public: |
| (...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 145 predictor_ = | 167 predictor_ = |
| 146 ResourcePrefetchPredictorFactory::GetForProfile(browser()->profile()); | 168 ResourcePrefetchPredictorFactory::GetForProfile(browser()->profile()); |
| 147 ASSERT_TRUE(predictor_); | 169 ASSERT_TRUE(predictor_); |
| 148 EnsurePredictorInitialized(); | 170 EnsurePredictorInitialized(); |
| 149 } | 171 } |
| 150 | 172 |
| 151 void NavigateToURLAndCheckSubresources(const GURL& main_frame_url) { | 173 void NavigateToURLAndCheckSubresources(const GURL& main_frame_url) { |
| 152 GURL endpoint_url = GetRedirectEndpoint(main_frame_url); | 174 GURL endpoint_url = GetRedirectEndpoint(main_frame_url); |
| 153 std::vector<URLRequestSummary> url_request_summaries; | 175 std::vector<URLRequestSummary> url_request_summaries; |
| 154 for (const auto& kv : resources_) { | 176 for (const auto& kv : resources_) { |
| 155 if (kv.second.is_no_store) | 177 if (kv.second.is_no_store || !kv.second.should_be_recorded) |
| 156 continue; | 178 continue; |
| 157 url_request_summaries.push_back( | 179 url_request_summaries.push_back( |
| 158 GetURLRequestSummaryForResource(endpoint_url, kv.second)); | 180 GetURLRequestSummaryForResource(endpoint_url, kv.second)); |
| 159 } | 181 } |
| 160 ResourcePrefetchPredictorTestObserver observer( | 182 ResourcePrefetchPredictorTestObserver observer( |
| 161 predictor_, UpdateAndGetVisitCount(main_frame_url), | 183 predictor_, UpdateAndGetVisitCount(main_frame_url), |
| 162 CreatePageRequestSummary(endpoint_url.spec(), main_frame_url.spec(), | 184 CreatePageRequestSummary(endpoint_url.spec(), main_frame_url.spec(), |
| 163 url_request_summaries)); | 185 url_request_summaries)); |
| 164 ui_test_utils::NavigateToURL(browser(), main_frame_url); | 186 ui_test_utils::NavigateToURL(browser(), main_frame_url); |
| 165 observer.Wait(); | 187 observer.Wait(); |
| 166 } | 188 } |
| 167 | 189 |
| 168 ResourceSummary* AddResource(const GURL& resource_url, | 190 ResourceSummary* AddResource(const GURL& resource_url, |
| 169 content::ResourceType resource_type, | 191 content::ResourceType resource_type, |
| 170 net::RequestPriority priority) { | 192 net::RequestPriority priority) { |
| 171 auto pair_and_whether_inserted = | 193 auto pair_and_whether_inserted = |
| 172 resources_.insert(std::make_pair(resource_url, ResourceSummary())); | 194 resources_.insert(std::make_pair(resource_url, ResourceSummary())); |
| 173 EXPECT_TRUE(pair_and_whether_inserted.second) << resource_url | 195 EXPECT_TRUE(pair_and_whether_inserted.second) << resource_url |
| 174 << " was inserted twice"; | 196 << " was inserted twice"; |
| 175 ResourceSummary* resource = &pair_and_whether_inserted.first->second; | 197 ResourceSummary* resource = &pair_and_whether_inserted.first->second; |
| 176 resource->request.resource_url = resource_url; | 198 resource->request.resource_url = resource_url; |
| 177 resource->request.resource_type = resource_type; | 199 resource->request.resource_type = resource_type; |
| 178 resource->request.priority = priority; | 200 resource->request.priority = priority; |
| 179 resource->request.has_validators = true; | 201 resource->request.has_validators = true; |
| 180 return resource; | 202 return resource; |
| 181 } | 203 } |
| 182 | 204 |
| 205 ResourceSummary* AddExternalResource(const GURL& resource_url, | |
| 206 content::ResourceType resource_type, | |
| 207 net::RequestPriority priority) { | |
| 208 auto resource = AddResource(resource_url, resource_type, priority); | |
| 209 resource->is_external = true; | |
| 210 return resource; | |
| 211 } | |
| 212 | |
| 213 void AddUnrecordedResources(const std::vector<GURL>& resource_urls) { | |
| 214 for (const GURL& resource_url : resource_urls) { | |
| 215 auto resource = | |
| 216 AddResource(resource_url, content::RESOURCE_TYPE_SUB_RESOURCE, | |
| 217 net::DEFAULT_PRIORITY); | |
| 218 resource->should_be_recorded = false; | |
| 219 } | |
| 220 } | |
| 221 | |
| 183 void AddRedirectChain(const GURL& initial_url, | 222 void AddRedirectChain(const GURL& initial_url, |
| 184 const std::vector<RedirectEdge>& redirect_chain) { | 223 const std::vector<RedirectEdge>& redirect_chain) { |
| 185 ASSERT_FALSE(redirect_chain.empty()); | 224 ASSERT_FALSE(redirect_chain.empty()); |
| 186 GURL current = initial_url; | 225 GURL current = initial_url; |
| 187 for (const auto& edge : redirect_chain) { | 226 for (const auto& edge : redirect_chain) { |
| 188 auto result = redirects_.insert(std::make_pair(current, edge)); | 227 auto result = redirects_.insert(std::make_pair(current, edge)); |
| 189 EXPECT_TRUE(result.second) << current << " already has a redirect."; | 228 EXPECT_TRUE(result.second) << current << " already has a redirect."; |
| 190 current = edge.url; | 229 current = edge.url; |
| 191 } | 230 } |
| 192 } | 231 } |
| (...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 260 return current; | 299 return current; |
| 261 } | 300 } |
| 262 | 301 |
| 263 std::unique_ptr<net::test_server::HttpResponse> HandleResourceRequest( | 302 std::unique_ptr<net::test_server::HttpResponse> HandleResourceRequest( |
| 264 const net::test_server::HttpRequest& request) const { | 303 const net::test_server::HttpRequest& request) const { |
| 265 auto resource_it = resources_.find(request.GetURL()); | 304 auto resource_it = resources_.find(request.GetURL()); |
| 266 if (resource_it == resources_.end()) | 305 if (resource_it == resources_.end()) |
| 267 return nullptr; | 306 return nullptr; |
| 268 | 307 |
| 269 const ResourceSummary& summary = resource_it->second; | 308 const ResourceSummary& summary = resource_it->second; |
| 309 if (summary.is_external) | |
| 310 return nullptr; | |
| 311 | |
| 270 auto http_response = | 312 auto http_response = |
| 271 base::MakeUnique<net::test_server::BasicHttpResponse>(); | 313 base::MakeUnique<net::test_server::BasicHttpResponse>(); |
| 272 http_response->set_code(net::HTTP_OK); | 314 http_response->set_code(net::HTTP_OK); |
| 273 if (!summary.request.mime_type.empty()) | 315 if (!summary.request.mime_type.empty()) |
| 274 http_response->set_content_type(summary.request.mime_type); | 316 http_response->set_content_type(summary.request.mime_type); |
| 275 if (!summary.content.empty()) | 317 if (!summary.content.empty()) |
| 276 http_response->set_content(summary.content); | 318 http_response->set_content(summary.content); |
| 277 if (summary.is_no_store) | 319 if (summary.is_no_store) |
| 278 http_response->AddCustomHeader("Cache-Control", "no-store"); | 320 http_response->AddCustomHeader("Cache-Control", "no-store"); |
| 279 if (summary.request.has_validators) { | 321 if (summary.request.has_validators) { |
| (...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 363 net::LOWEST); | 405 net::LOWEST); |
| 364 AddResource(https_server()->GetURL(kStylePath), | 406 AddResource(https_server()->GetURL(kStylePath), |
| 365 content::RESOURCE_TYPE_STYLESHEET, net::HIGHEST); | 407 content::RESOURCE_TYPE_STYLESHEET, net::HIGHEST); |
| 366 AddResource(https_server()->GetURL(kScriptPath), | 408 AddResource(https_server()->GetURL(kScriptPath), |
| 367 content::RESOURCE_TYPE_SCRIPT, net::MEDIUM); | 409 content::RESOURCE_TYPE_SCRIPT, net::MEDIUM); |
| 368 AddResource(https_server()->GetURL(kFontPath), | 410 AddResource(https_server()->GetURL(kFontPath), |
| 369 content::RESOURCE_TYPE_FONT_RESOURCE, net::HIGHEST); | 411 content::RESOURCE_TYPE_FONT_RESOURCE, net::HIGHEST); |
| 370 NavigateToURLAndCheckSubresources(GetURL(kRedirectPath)); | 412 NavigateToURLAndCheckSubresources(GetURL(kRedirectPath)); |
| 371 } | 413 } |
| 372 | 414 |
| 415 IN_PROC_BROWSER_TEST_F(ResourcePrefetchPredictorBrowserTest, | |
| 416 LearningJavascriptDocumentWrite) { | |
| 417 auto externalScript = | |
| 418 AddExternalResource(GetURL(kScriptDocumentWritePath), | |
| 419 content::RESOURCE_TYPE_SCRIPT, net::MEDIUM); | |
| 420 externalScript->request.mime_type = "application/javascript"; | |
| 421 AddResource(GetURL(kImagePath), content::RESOURCE_TYPE_IMAGE, net::LOWEST); | |
| 422 AddResource(GetURL(kStylePath), content::RESOURCE_TYPE_STYLESHEET, | |
| 423 net::HIGHEST); | |
| 424 AddResource(GetURL(kScriptPath), content::RESOURCE_TYPE_SCRIPT, net::MEDIUM); | |
| 425 NavigateToURLAndCheckSubresources(GetURL(kHtmlDocumentWritePath)); | |
| 426 } | |
| 427 | |
| 428 IN_PROC_BROWSER_TEST_F(ResourcePrefetchPredictorBrowserTest, | |
| 429 LearningJavascriptAppendChild) { | |
| 430 auto externalScript = | |
| 431 AddExternalResource(GetURL(kScriptAppendChildPath), | |
| 432 content::RESOURCE_TYPE_SCRIPT, net::MEDIUM); | |
| 433 externalScript->request.mime_type = "application/javascript"; | |
| 434 AddResource(GetURL(kImagePath), content::RESOURCE_TYPE_IMAGE, net::LOWEST); | |
| 435 AddResource(GetURL(kStylePath), content::RESOURCE_TYPE_STYLESHEET, | |
| 436 net::HIGHEST); | |
| 437 // This script has net::LOWEST priority because it's executed asynchronously. | |
| 438 AddResource(GetURL(kScriptPath), content::RESOURCE_TYPE_SCRIPT, net::LOWEST); | |
| 439 NavigateToURLAndCheckSubresources(GetURL(kHtmlAppendChildPath)); | |
| 440 } | |
| 441 | |
| 442 IN_PROC_BROWSER_TEST_F(ResourcePrefetchPredictorBrowserTest, | |
| 443 LearningJavascriptInnerHtml) { | |
| 444 auto externalScript = AddExternalResource( | |
| 445 GetURL(kScriptInnerHtmlPath), content::RESOURCE_TYPE_SCRIPT, net::MEDIUM); | |
| 446 externalScript->request.mime_type = "application/javascript"; | |
| 447 AddResource(GetURL(kImagePath), content::RESOURCE_TYPE_IMAGE, net::LOWEST); | |
| 448 AddResource(GetURL(kStylePath), content::RESOURCE_TYPE_STYLESHEET, | |
| 449 net::HIGHEST); | |
| 450 // The script isn't requested. Need furhter investigation. | |
|
Benoit L
2016/12/05 14:03:30
nit: further.
alexilin
2016/12/06 09:10:44
Done.
| |
| 451 AddUnrecordedResources({GetURL(kScriptPath)}); | |
| 452 NavigateToURLAndCheckSubresources(GetURL(kHtmlInnerHtmlPath)); | |
| 453 } | |
| 454 | |
| 455 // Javascript fetch requests are ignored by the ResourcePrefetchPredictor | |
| 456 // because they have unsupported resource type content::RESOURCE_TYPE_XHR. | |
|
Benoit L
2016/12/05 14:03:30
Oh, that's a good find!
I wonder what we should do
alexilin
2016/12/06 09:10:44
Already done:
https://codereview.chromium.org/2539
| |
| 457 IN_PROC_BROWSER_TEST_F(ResourcePrefetchPredictorBrowserTest, | |
| 458 LearningJavascriptFetchIgnored) { | |
| 459 auto externalScript = AddExternalResource( | |
| 460 GetURL(kScriptFetchPath), content::RESOURCE_TYPE_SCRIPT, net::MEDIUM); | |
| 461 externalScript->request.mime_type = "application/javascript"; | |
| 462 AddUnrecordedResources( | |
| 463 {GetURL(kImagePath), GetURL(kStylePath), GetURL(kScriptPath)}); | |
| 464 NavigateToURLAndCheckSubresources(GetURL(kHtmlFetchPath)); | |
| 465 } | |
| 466 | |
| 467 // ResourcePrefetchPredictor ignores all resources requested from subframes. | |
| 468 IN_PROC_BROWSER_TEST_F(ResourcePrefetchPredictorBrowserTest, | |
| 469 LearningWithIframe) { | |
| 470 // Included from html_iframe.html. | |
| 471 AddResource(GetURL(kImagePath2), content::RESOURCE_TYPE_IMAGE, net::LOWEST); | |
| 472 AddResource(GetURL(kStylePath2), content::RESOURCE_TYPE_STYLESHEET, | |
| 473 net::HIGHEST); | |
| 474 AddResource(GetURL(kScriptPath2), content::RESOURCE_TYPE_SCRIPT, net::MEDIUM); | |
| 475 // Included from <iframe src="html_subresources.html"> and not recored. | |
| 476 AddUnrecordedResources({GetURL(kImagePath), GetURL(kStylePath), | |
| 477 GetURL(kScriptPath), GetURL(kFontPath)}); | |
| 478 NavigateToURLAndCheckSubresources(GetURL(kHtmlIframePath)); | |
| 479 } | |
| 480 | |
| 373 } // namespace predictors | 481 } // namespace predictors |
| OLD | NEW |