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/browser_commands.h" | |
11 #include "chrome/browser/ui/tabs/tab_strip_model.h" | 12 #include "chrome/browser/ui/tabs/tab_strip_model.h" |
12 #include "chrome/common/chrome_switches.h" | 13 #include "chrome/common/chrome_switches.h" |
13 #include "chrome/test/base/in_process_browser_test.h" | 14 #include "chrome/test/base/in_process_browser_test.h" |
14 #include "chrome/test/base/ui_test_utils.h" | 15 #include "chrome/test/base/ui_test_utils.h" |
15 #include "content/public/browser/render_frame_host.h" | 16 #include "net/dns/mock_host_resolver.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 const char kImageMime[] = "image/png"; | 26 const char kImageMime[] = "image/png"; |
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
84 | 84 |
85 private: | 85 private: |
86 base::RunLoop run_loop_; | 86 base::RunLoop run_loop_; |
87 | 87 |
88 DISALLOW_COPY_AND_ASSIGN(InitializationObserver); | 88 DISALLOW_COPY_AND_ASSIGN(InitializationObserver); |
89 }; | 89 }; |
90 | 90 |
91 using PageRequestSummary = ResourcePrefetchPredictor::PageRequestSummary; | 91 using PageRequestSummary = ResourcePrefetchPredictor::PageRequestSummary; |
92 using URLRequestSummary = ResourcePrefetchPredictor::URLRequestSummary; | 92 using URLRequestSummary = ResourcePrefetchPredictor::URLRequestSummary; |
93 | 93 |
94 struct ResourceComparisonParameters { | |
ahemery
2016/12/19 14:53:42
Just improving modularity in case we need to add o
| |
95 bool match_navigation_id; | |
96 | |
97 ResourceComparisonParameters() : match_navigation_id(true) {} | |
98 }; | |
99 | |
94 void RemoveDuplicateSubresources(std::vector<URLRequestSummary>* subresources) { | 100 void RemoveDuplicateSubresources(std::vector<URLRequestSummary>* subresources) { |
95 std::stable_sort(subresources->begin(), subresources->end(), | 101 std::stable_sort(subresources->begin(), subresources->end(), |
96 [](const URLRequestSummary& x, const URLRequestSummary& y) { | 102 [](const URLRequestSummary& x, const URLRequestSummary& y) { |
97 return x.resource_url < y.resource_url; | 103 return x.resource_url < y.resource_url; |
98 }); | 104 }); |
99 subresources->erase( | 105 subresources->erase( |
100 std::unique(subresources->begin(), subresources->end(), | 106 std::unique(subresources->begin(), subresources->end(), |
101 [](const URLRequestSummary& x, const URLRequestSummary& y) { | 107 [](const URLRequestSummary& x, const URLRequestSummary& y) { |
102 return x.resource_url == y.resource_url; | 108 return x.resource_url == y.resource_url; |
103 }), | 109 }), |
104 subresources->end()); | 110 subresources->end()); |
105 } | 111 } |
106 | 112 |
107 // Fill a NavigationID with "empty" data that does not trigger | 113 // Fill a NavigationID with "empty" data that does not trigger |
108 // the is_valid DCHECK(). Allows comparing. | 114 // the is_valid DCHECK(). Allows comparing. |
109 void SetValidNavigationID(NavigationID* navigation_id) { | 115 void SetValidNavigationID(NavigationID* navigation_id) { |
110 navigation_id->render_process_id = 0; | 116 navigation_id->session_id.set_id(0); |
111 navigation_id->render_frame_id = 0; | 117 navigation_id->frame_tree_node_id = -1; |
112 navigation_id->main_frame_url = GURL("http://127.0.0.1"); | 118 navigation_id->main_frame_url = GURL("http://127.0.0.1"); |
113 } | 119 } |
114 | 120 |
115 void ModifySubresourceForComparison(URLRequestSummary* subresource, | 121 void ModifySubresourceForComparison( |
116 bool match_navigation_id) { | 122 URLRequestSummary* subresource, |
117 if (!match_navigation_id) | 123 const ResourceComparisonParameters& params) { |
124 if (!params.match_navigation_id) | |
118 SetValidNavigationID(&subresource->navigation_id); | 125 SetValidNavigationID(&subresource->navigation_id); |
119 if (subresource->resource_type == content::RESOURCE_TYPE_IMAGE && | 126 if (subresource->resource_type == content::RESOURCE_TYPE_IMAGE && |
120 subresource->priority == net::LOWEST) { | 127 subresource->priority == net::LOWEST) { |
121 // Fuzzy comparison for images because an image priority can be | 128 // Fuzzy comparison for images because an image priority can be |
122 // boosted during layout via | 129 // boosted during layout via |
123 // ResourceFetcher::updateAllImageResourcePriorities(). | 130 // ResourceFetcher::updateAllImageResourcePriorities(). |
124 subresource->priority = net::MEDIUM; | 131 subresource->priority = net::MEDIUM; |
125 } | 132 } |
126 } | 133 } |
127 | 134 |
128 // Does a custom comparison of subresources of URLRequestSummary | 135 // Does a custom comparison of subresources of URLRequestSummary |
129 // and fail the test if the expectation is not met. | 136 // and fail the test if the expectation is not met. |
130 void CompareSubresources(std::vector<URLRequestSummary> actual_subresources, | 137 void CompareSubresources(std::vector<URLRequestSummary> actual_subresources, |
131 std::vector<URLRequestSummary> expected_subresources, | 138 std::vector<URLRequestSummary> expected_subresources, |
132 bool match_navigation_id) { | 139 const ResourceComparisonParameters& params) { |
133 // Duplicate resources can be observed in a single navigation but | 140 // Duplicate resources can be observed in a single navigation but |
134 // ResourcePrefetchPredictor only cares about the first occurrence of each. | 141 // ResourcePrefetchPredictor only cares about the first occurrence of each. |
135 RemoveDuplicateSubresources(&actual_subresources); | 142 RemoveDuplicateSubresources(&actual_subresources); |
136 | 143 |
137 for (auto& subresource : actual_subresources) | 144 for (auto& subresource : actual_subresources) |
138 ModifySubresourceForComparison(&subresource, match_navigation_id); | 145 ModifySubresourceForComparison(&subresource, params); |
139 for (auto& subresource : expected_subresources) | 146 for (auto& subresource : expected_subresources) |
140 ModifySubresourceForComparison(&subresource, match_navigation_id); | 147 ModifySubresourceForComparison(&subresource, params); |
141 | 148 |
142 EXPECT_THAT(actual_subresources, | 149 EXPECT_THAT(actual_subresources, |
143 testing::UnorderedElementsAreArray(expected_subresources)); | 150 testing::UnorderedElementsAreArray(expected_subresources)); |
144 } | 151 } |
145 | 152 |
146 } // namespace | 153 } // namespace |
147 | 154 |
148 // Helper class to track and allow waiting for ResourcePrefetchPredictor events. | 155 // Helper class to track and allow waiting for ResourcePrefetchPredictor events. |
149 // These events are also used to verify that ResourcePrefetchPredictor works as | 156 // These events are also used to verify that ResourcePrefetchPredictor works as |
150 // expected. | 157 // expected. |
151 class ResourcePrefetchPredictorTestObserver : public TestObserver { | 158 class ResourcePrefetchPredictorTestObserver : public TestObserver { |
152 public: | 159 public: |
153 using PageRequestSummary = ResourcePrefetchPredictor::PageRequestSummary; | 160 using PageRequestSummary = ResourcePrefetchPredictor::PageRequestSummary; |
154 | 161 |
155 explicit ResourcePrefetchPredictorTestObserver( | 162 explicit ResourcePrefetchPredictorTestObserver( |
156 ResourcePrefetchPredictor* predictor, | 163 ResourcePrefetchPredictor* predictor, |
157 const size_t expected_url_visit_count, | 164 const size_t expected_url_visit_count, |
158 const PageRequestSummary& expected_summary, | 165 const PageRequestSummary& expected_summary, |
159 bool match_navigation_id) | 166 const ResourceComparisonParameters& resource_comparison_params) |
160 : TestObserver(predictor), | 167 : TestObserver(predictor), |
161 url_visit_count_(expected_url_visit_count), | 168 url_visit_count_(expected_url_visit_count), |
162 summary_(expected_summary), | 169 summary_(expected_summary), |
163 match_navigation_id_(match_navigation_id) {} | 170 resource_comparison_params_(resource_comparison_params) {} |
164 | 171 |
165 // TestObserver: | 172 // TestObserver: |
166 void OnNavigationLearned(size_t url_visit_count, | 173 void OnNavigationLearned(size_t url_visit_count, |
167 const PageRequestSummary& summary) override { | 174 const PageRequestSummary& summary) override { |
168 EXPECT_EQ(url_visit_count, url_visit_count_); | 175 EXPECT_EQ(url_visit_count, url_visit_count_); |
169 EXPECT_EQ(summary.main_frame_url, summary_.main_frame_url); | 176 EXPECT_EQ(summary.main_frame_url, summary_.main_frame_url); |
170 EXPECT_EQ(summary.initial_url, summary_.initial_url); | 177 EXPECT_EQ(summary.initial_url, summary_.initial_url); |
178 for (const auto& resource : summary.subresource_requests) | |
ahemery
2016/12/19 14:53:42
Tracking what NavigationID's have been used. Requi
| |
179 ++current_navigation_ids_[resource.navigation_id]; | |
171 CompareSubresources(summary.subresource_requests, | 180 CompareSubresources(summary.subresource_requests, |
172 summary_.subresource_requests, match_navigation_id_); | 181 summary_.subresource_requests, |
182 resource_comparison_params_); | |
173 run_loop_.Quit(); | 183 run_loop_.Quit(); |
174 } | 184 } |
175 | 185 |
176 void Wait() { run_loop_.Run(); } | 186 void Wait() { run_loop_.Run(); } |
177 | 187 |
188 std::map<NavigationID, int>* GetNavigationIds() { | |
189 return ¤t_navigation_ids_; | |
190 } | |
191 | |
178 private: | 192 private: |
179 base::RunLoop run_loop_; | 193 base::RunLoop run_loop_; |
180 size_t url_visit_count_; | 194 size_t url_visit_count_; |
181 PageRequestSummary summary_; | 195 PageRequestSummary summary_; |
182 bool match_navigation_id_; | 196 ResourceComparisonParameters resource_comparison_params_; |
197 std::map<NavigationID, int> current_navigation_ids_; | |
183 | 198 |
184 DISALLOW_COPY_AND_ASSIGN(ResourcePrefetchPredictorTestObserver); | 199 DISALLOW_COPY_AND_ASSIGN(ResourcePrefetchPredictorTestObserver); |
185 }; | 200 }; |
186 | 201 |
187 class ResourcePrefetchPredictorBrowserTest : public InProcessBrowserTest { | 202 class ResourcePrefetchPredictorBrowserTest : public InProcessBrowserTest { |
188 protected: | 203 protected: |
189 using URLRequestSummary = ResourcePrefetchPredictor::URLRequestSummary; | 204 using URLRequestSummary = ResourcePrefetchPredictor::URLRequestSummary; |
190 | 205 |
191 void SetUpCommandLine(base::CommandLine* command_line) override { | 206 void SetUpCommandLine(base::CommandLine* command_line) override { |
192 command_line->AppendSwitchASCII( | 207 command_line->AppendSwitchASCII( |
193 switches::kSpeculativeResourcePrefetching, | 208 switches::kSpeculativeResourcePrefetching, |
194 switches::kSpeculativeResourcePrefetchingEnabled); | 209 switches::kSpeculativeResourcePrefetchingEnabled); |
195 } | 210 } |
196 | 211 |
197 void SetUpOnMainThread() override { | 212 void SetUpOnMainThread() override { |
213 SetCurrentHostName("127.0.0.1"); | |
ahemery
2016/12/19 14:53:42
Resolving everything to localhost to be able to us
| |
214 host_resolver()->AddRule("*", "127.0.0.1"); | |
215 embedded_test_server()->AddDefaultHandlers( | |
216 base::FilePath(FILE_PATH_LITERAL("chrome/test/data"))); | |
198 embedded_test_server()->RegisterRequestHandler( | 217 embedded_test_server()->RegisterRequestHandler( |
199 base::Bind(&ResourcePrefetchPredictorBrowserTest::HandleRedirectRequest, | 218 base::Bind(&ResourcePrefetchPredictorBrowserTest::HandleRedirectRequest, |
200 base::Unretained(this))); | 219 base::Unretained(this))); |
201 embedded_test_server()->RegisterRequestHandler( | 220 embedded_test_server()->RegisterRequestHandler( |
202 base::Bind(&ResourcePrefetchPredictorBrowserTest::HandleResourceRequest, | 221 base::Bind(&ResourcePrefetchPredictorBrowserTest::HandleResourceRequest, |
203 base::Unretained(this))); | 222 base::Unretained(this))); |
204 ASSERT_TRUE(embedded_test_server()->Start()); | 223 ASSERT_TRUE(embedded_test_server()->Start()); |
205 predictor_ = | 224 predictor_ = |
206 ResourcePrefetchPredictorFactory::GetForProfile(browser()->profile()); | 225 ResourcePrefetchPredictorFactory::GetForProfile(browser()->profile()); |
207 ASSERT_TRUE(predictor_); | 226 ASSERT_TRUE(predictor_); |
208 EnsurePredictorInitialized(); | 227 EnsurePredictorInitialized(); |
209 } | 228 } |
210 | 229 |
211 void NavigateToURLAndCheckSubresources( | 230 void NavigateToURLAndCheckSubresources( |
212 const GURL& main_frame_url, | 231 const GURL& main_frame_url, |
213 WindowOpenDisposition disposition = WindowOpenDisposition::CURRENT_TAB) { | 232 WindowOpenDisposition disposition = WindowOpenDisposition::CURRENT_TAB) { |
214 GURL endpoint_url = GetRedirectEndpoint(main_frame_url); | 233 GURL endpoint_url = GetRedirectEndpoint(main_frame_url); |
215 std::vector<URLRequestSummary> url_request_summaries; | 234 std::vector<URLRequestSummary> url_request_summaries; |
216 for (const auto& kv : resources_) { | 235 for (const auto& kv : resources_) { |
217 if (kv.second.is_no_store || !kv.second.should_be_recorded) | 236 if (kv.second.is_no_store || !kv.second.should_be_recorded) |
218 continue; | 237 continue; |
219 url_request_summaries.push_back( | 238 url_request_summaries.push_back( |
220 GetURLRequestSummaryForResource(endpoint_url, kv.second)); | 239 GetURLRequestSummaryForResource(endpoint_url, kv.second)); |
221 } | 240 } |
241 | |
242 ResourceComparisonParameters resource_comparison_params; | |
243 if (disposition != WindowOpenDisposition::CURRENT_TAB) | |
244 resource_comparison_params.match_navigation_id = false; | |
245 | |
222 ResourcePrefetchPredictorTestObserver observer( | 246 ResourcePrefetchPredictorTestObserver observer( |
223 predictor_, UpdateAndGetVisitCount(main_frame_url), | 247 predictor_, UpdateAndGetVisitCount(main_frame_url), |
224 CreatePageRequestSummary(endpoint_url.spec(), main_frame_url.spec(), | 248 CreatePageRequestSummary(endpoint_url.spec(), main_frame_url.spec(), |
225 url_request_summaries), | 249 url_request_summaries), |
226 true); // Matching navigation id by default | 250 resource_comparison_params); |
227 ui_test_utils::NavigateToURLWithDisposition( | 251 ui_test_utils::NavigateToURLWithDisposition( |
228 browser(), main_frame_url, disposition, | 252 browser(), main_frame_url, disposition, |
229 ui_test_utils::BROWSER_TEST_NONE); | 253 ui_test_utils::BROWSER_TEST_NONE); |
230 observer.Wait(); | 254 observer.Wait(); |
255 for (auto& kv : resources_) { | |
ahemery
2016/12/19 14:53:42
Copy of Alex modifications in https://codereview.c
| |
256 if (!kv.second.is_no_store && kv.second.should_be_recorded) | |
257 kv.second.request.was_cached = true; | |
258 } | |
259 for (const auto& nav : *observer.GetNavigationIds()) | |
ahemery
2016/12/19 14:53:41
Aggregating every observer navigationID map into t
| |
260 ++navigation_id_history_[nav.first]; | |
231 } | 261 } |
232 | 262 |
233 ResourceSummary* AddResource(const GURL& resource_url, | 263 ResourceSummary* AddResource(const GURL& resource_url, |
234 content::ResourceType resource_type, | 264 content::ResourceType resource_type, |
235 net::RequestPriority priority) { | 265 net::RequestPriority priority) { |
236 auto pair_and_whether_inserted = | 266 auto pair_and_whether_inserted = |
237 resources_.insert(std::make_pair(resource_url, ResourceSummary())); | 267 resources_.insert(std::make_pair(resource_url, ResourceSummary())); |
238 EXPECT_TRUE(pair_and_whether_inserted.second) << resource_url | 268 EXPECT_TRUE(pair_and_whether_inserted.second) << resource_url |
239 << " was inserted twice"; | 269 << " was inserted twice"; |
240 ResourceSummary* resource = &pair_and_whether_inserted.first->second; | 270 ResourceSummary* resource = &pair_and_whether_inserted.first->second; |
(...skipping 25 matching lines...) Expand all Loading... | |
266 const std::vector<RedirectEdge>& redirect_chain) { | 296 const std::vector<RedirectEdge>& redirect_chain) { |
267 ASSERT_FALSE(redirect_chain.empty()); | 297 ASSERT_FALSE(redirect_chain.empty()); |
268 GURL current = initial_url; | 298 GURL current = initial_url; |
269 for (const auto& edge : redirect_chain) { | 299 for (const auto& edge : redirect_chain) { |
270 auto result = redirects_.insert(std::make_pair(current, edge)); | 300 auto result = redirects_.insert(std::make_pair(current, edge)); |
271 EXPECT_TRUE(result.second) << current << " already has a redirect."; | 301 EXPECT_TRUE(result.second) << current << " already has a redirect."; |
272 current = edge.url; | 302 current = edge.url; |
273 } | 303 } |
274 } | 304 } |
275 | 305 |
306 void ClearResources() { resources_.clear(); } | |
ahemery
2016/12/19 14:53:42
Used for clearing resources before hostname change
| |
307 | |
308 void ClearCache() { | |
ahemery
2016/12/19 14:53:42
Copy of Alex modifications. See line 255 in this f
| |
309 chrome::ClearCache(browser()); | |
310 for (auto& kv : resources_) | |
311 kv.second.request.was_cached = false; | |
312 } | |
313 | |
276 // Shortcut for convenience. | 314 // Shortcut for convenience. |
277 GURL GetURL(const std::string& path) const { | 315 GURL GetURL(const std::string& path) const { |
278 return embedded_test_server()->GetURL(path); | 316 return embedded_test_server()->GetURL(current_host_name_, path); |
279 } | 317 } |
280 | 318 |
281 void EnableHttpsServer() { | 319 void EnableHttpsServer() { |
282 ASSERT_FALSE(https_server_); | 320 ASSERT_FALSE(https_server_); |
283 https_server_ = base::MakeUnique<net::EmbeddedTestServer>( | 321 https_server_ = base::MakeUnique<net::EmbeddedTestServer>( |
284 net::EmbeddedTestServer::TYPE_HTTPS); | 322 net::EmbeddedTestServer::TYPE_HTTPS); |
285 https_server()->AddDefaultHandlers( | 323 https_server()->AddDefaultHandlers( |
286 base::FilePath(FILE_PATH_LITERAL("chrome/test/data"))); | 324 base::FilePath(FILE_PATH_LITERAL("chrome/test/data"))); |
287 https_server()->RegisterRequestHandler( | 325 https_server()->RegisterRequestHandler( |
288 base::Bind(&ResourcePrefetchPredictorBrowserTest::HandleRedirectRequest, | 326 base::Bind(&ResourcePrefetchPredictorBrowserTest::HandleRedirectRequest, |
289 base::Unretained(this))); | 327 base::Unretained(this))); |
290 https_server()->RegisterRequestHandler( | 328 https_server()->RegisterRequestHandler( |
291 base::Bind(&ResourcePrefetchPredictorBrowserTest::HandleResourceRequest, | 329 base::Bind(&ResourcePrefetchPredictorBrowserTest::HandleResourceRequest, |
292 base::Unretained(this))); | 330 base::Unretained(this))); |
293 ASSERT_TRUE(https_server()->Start()); | 331 ASSERT_TRUE(https_server()->Start()); |
294 } | 332 } |
295 | 333 |
296 // Returns the embedded test server working over HTTPS. Must be enabled by | 334 // Returns the embedded test server working over HTTPS. Must be enabled by |
297 // calling EnableHttpsServer() before use. | 335 // calling EnableHttpsServer() before use. |
298 const net::EmbeddedTestServer* https_server() const { | 336 const net::EmbeddedTestServer* https_server() const { |
299 return https_server_.get(); | 337 return https_server_.get(); |
300 } | 338 } |
301 | 339 |
302 net::EmbeddedTestServer* https_server() { return https_server_.get(); } | 340 net::EmbeddedTestServer* https_server() { return https_server_.get(); } |
303 | 341 |
342 void SetCurrentHostName(const std::string& new_host) { | |
ahemery
2016/12/19 14:53:41
Will be used to resolve every URL using the getURL
| |
343 current_host_name_ = new_host; | |
344 } | |
345 | |
346 unsigned int GetHistoryCount() { return navigation_id_history_.size(); } | |
ahemery
2016/12/19 14:53:42
Returns the number of navigations that have been u
| |
347 | |
304 private: | 348 private: |
305 // ResourcePrefetchPredictor needs to be initialized before the navigation | 349 // ResourcePrefetchPredictor needs to be initialized before the navigation |
306 // happens otherwise this navigation will be ignored by predictor. | 350 // happens otherwise this navigation will be ignored by predictor. |
307 void EnsurePredictorInitialized() { | 351 void EnsurePredictorInitialized() { |
308 if (predictor_->initialization_state_ == | 352 if (predictor_->initialization_state_ == |
309 ResourcePrefetchPredictor::INITIALIZED) { | 353 ResourcePrefetchPredictor::INITIALIZED) { |
310 return; | 354 return; |
311 } | 355 } |
312 | 356 |
313 InitializationObserver observer(predictor_); | 357 InitializationObserver observer(predictor_); |
314 if (predictor_->initialization_state_ == | 358 if (predictor_->initialization_state_ == |
315 ResourcePrefetchPredictor::NOT_INITIALIZED) { | 359 ResourcePrefetchPredictor::NOT_INITIALIZED) { |
316 predictor_->StartInitialization(); | 360 predictor_->StartInitialization(); |
317 } | 361 } |
318 observer.Wait(); | 362 observer.Wait(); |
319 } | 363 } |
320 | 364 |
321 URLRequestSummary GetURLRequestSummaryForResource( | 365 URLRequestSummary GetURLRequestSummaryForResource( |
322 const GURL& main_frame_url, | 366 const GURL& main_frame_url, |
323 const ResourceSummary& resource_summary) const { | 367 const ResourceSummary& resource_summary) const { |
324 URLRequestSummary summary(resource_summary.request); | 368 URLRequestSummary summary(resource_summary.request); |
325 content::WebContents* web_contents = | 369 content::WebContents* web_contents = |
326 browser()->tab_strip_model()->GetActiveWebContents(); | 370 browser()->tab_strip_model()->GetActiveWebContents(); |
327 int process_id = web_contents->GetRenderProcessHost()->GetID(); | |
328 int frame_id = web_contents->GetMainFrame()->GetRoutingID(); | |
329 summary.navigation_id = | 371 summary.navigation_id = |
330 CreateNavigationID(process_id, frame_id, main_frame_url.spec()); | 372 NavigationID(web_contents, main_frame_url, base::TimeTicks::Now()); |
ahemery
2016/12/19 14:53:41
Using constructor directly with web_contents. Forg
| |
331 return summary; | 373 return summary; |
332 } | 374 } |
333 | 375 |
334 GURL GetRedirectEndpoint(const GURL& initial_url) const { | 376 GURL GetRedirectEndpoint(const GURL& initial_url) const { |
335 GURL current = initial_url; | 377 GURL current = initial_url; |
336 while (true) { | 378 while (true) { |
337 auto it = redirects_.find(current); | 379 auto it = redirects_.find(current); |
338 if (it == redirects_.end()) | 380 if (it == redirects_.end()) |
339 break; | 381 break; |
340 current = it->second.url; | 382 current = it->second.url; |
341 } | 383 } |
342 return current; | 384 return current; |
343 } | 385 } |
344 | 386 |
345 std::unique_ptr<net::test_server::HttpResponse> HandleResourceRequest( | 387 std::unique_ptr<net::test_server::HttpResponse> HandleResourceRequest( |
346 const net::test_server::HttpRequest& request) const { | 388 const net::test_server::HttpRequest& request) const { |
347 auto resource_it = resources_.find(request.GetURL()); | 389 GURL resource_url = request.GetURL(); |
ahemery
2016/12/19 14:53:42
At this stage, URL has been resolved to 127.0.0.1,
| |
390 GURL::Replacements replace_host; | |
391 replace_host.SetHostStr(current_host_name_); | |
392 resource_url = resource_url.ReplaceComponents(replace_host); | |
393 | |
394 auto resource_it = resources_.find(resource_url); | |
348 if (resource_it == resources_.end()) | 395 if (resource_it == resources_.end()) |
349 return nullptr; | 396 return nullptr; |
350 | 397 |
351 const ResourceSummary& summary = resource_it->second; | 398 const ResourceSummary& summary = resource_it->second; |
352 if (summary.is_external) | 399 if (summary.is_external) |
353 return nullptr; | 400 return nullptr; |
354 | 401 |
355 auto http_response = | 402 auto http_response = |
356 base::MakeUnique<net::test_server::BasicHttpResponse>(); | 403 base::MakeUnique<net::test_server::BasicHttpResponse>(); |
357 http_response->set_code(net::HTTP_OK); | 404 http_response->set_code(net::HTTP_OK); |
(...skipping 25 matching lines...) Expand all Loading... | |
383 base::MakeUnique<net::test_server::BasicHttpResponse>(); | 430 base::MakeUnique<net::test_server::BasicHttpResponse>(); |
384 http_response->set_code(redirect_it->second.code); | 431 http_response->set_code(redirect_it->second.code); |
385 http_response->AddCustomHeader("Location", redirect_it->second.url.spec()); | 432 http_response->AddCustomHeader("Location", redirect_it->second.url.spec()); |
386 return std::move(http_response); | 433 return std::move(http_response); |
387 } | 434 } |
388 | 435 |
389 size_t UpdateAndGetVisitCount(const GURL& main_frame_url) { | 436 size_t UpdateAndGetVisitCount(const GURL& main_frame_url) { |
390 return ++visit_count_[main_frame_url]; | 437 return ++visit_count_[main_frame_url]; |
391 } | 438 } |
392 | 439 |
440 std::string current_host_name_; | |
393 ResourcePrefetchPredictor* predictor_; | 441 ResourcePrefetchPredictor* predictor_; |
394 std::unique_ptr<net::EmbeddedTestServer> https_server_; | 442 std::unique_ptr<net::EmbeddedTestServer> https_server_; |
395 std::map<GURL, ResourceSummary> resources_; | 443 std::map<GURL, ResourceSummary> resources_; |
396 std::map<GURL, RedirectEdge> redirects_; | 444 std::map<GURL, RedirectEdge> redirects_; |
397 std::map<GURL, size_t> visit_count_; | 445 std::map<GURL, size_t> visit_count_; |
446 std::map<NavigationID, int> navigation_id_history_; | |
447 }; | |
448 | |
449 // TODO(ahemery): Remove when https://codereview.chromium.org/2553083002/ | |
450 // is landed by Alex. LearningObserver should be used by default. | |
451 // His change will disable actual prefetching by default | |
452 class ResourcePrefetchPredictorLearningBrowserTest | |
453 : public ResourcePrefetchPredictorBrowserTest { | |
454 protected: | |
455 void SetUpCommandLine(base::CommandLine* command_line) override { | |
456 command_line->AppendSwitchASCII( | |
457 switches::kSpeculativeResourcePrefetching, | |
458 switches::kSpeculativeResourcePrefetchingLearning); | |
ahemery
2016/12/19 14:53:41
This is the critical line. Disables actual prefetc
| |
459 } | |
398 }; | 460 }; |
399 | 461 |
400 IN_PROC_BROWSER_TEST_F(ResourcePrefetchPredictorBrowserTest, LearningSimple) { | 462 IN_PROC_BROWSER_TEST_F(ResourcePrefetchPredictorBrowserTest, LearningSimple) { |
401 // These resources have default priorities that correspond to | 463 // These resources have default priorities that correspond to |
402 // blink::typeToPriority function. | 464 // blink::typeToPriority function. |
403 AddResource(GetURL(kImagePath), content::RESOURCE_TYPE_IMAGE, net::LOWEST); | 465 AddResource(GetURL(kImagePath), content::RESOURCE_TYPE_IMAGE, net::LOWEST); |
404 AddResource(GetURL(kStylePath), content::RESOURCE_TYPE_STYLESHEET, | 466 AddResource(GetURL(kStylePath), content::RESOURCE_TYPE_STYLESHEET, |
405 net::HIGHEST); | 467 net::HIGHEST); |
406 AddResource(GetURL(kScriptPath), content::RESOURCE_TYPE_SCRIPT, net::MEDIUM); | 468 AddResource(GetURL(kScriptPath), content::RESOURCE_TYPE_SCRIPT, net::MEDIUM); |
407 AddResource(GetURL(kFontPath), content::RESOURCE_TYPE_FONT_RESOURCE, | 469 AddResource(GetURL(kFontPath), content::RESOURCE_TYPE_FONT_RESOURCE, |
(...skipping 114 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
522 AddResource(GetURL(kImagePath2), content::RESOURCE_TYPE_IMAGE, net::LOWEST); | 584 AddResource(GetURL(kImagePath2), content::RESOURCE_TYPE_IMAGE, net::LOWEST); |
523 AddResource(GetURL(kStylePath2), content::RESOURCE_TYPE_STYLESHEET, | 585 AddResource(GetURL(kStylePath2), content::RESOURCE_TYPE_STYLESHEET, |
524 net::HIGHEST); | 586 net::HIGHEST); |
525 AddResource(GetURL(kScriptPath2), content::RESOURCE_TYPE_SCRIPT, net::MEDIUM); | 587 AddResource(GetURL(kScriptPath2), content::RESOURCE_TYPE_SCRIPT, net::MEDIUM); |
526 // Included from <iframe src="html_subresources.html"> and not recored. | 588 // Included from <iframe src="html_subresources.html"> and not recored. |
527 AddUnrecordedResources({GetURL(kImagePath), GetURL(kStylePath), | 589 AddUnrecordedResources({GetURL(kImagePath), GetURL(kStylePath), |
528 GetURL(kScriptPath), GetURL(kFontPath)}); | 590 GetURL(kScriptPath), GetURL(kFontPath)}); |
529 NavigateToURLAndCheckSubresources(GetURL(kHtmlIframePath)); | 591 NavigateToURLAndCheckSubresources(GetURL(kHtmlIframePath)); |
530 } | 592 } |
531 | 593 |
594 IN_PROC_BROWSER_TEST_F(ResourcePrefetchPredictorBrowserTest, | |
595 CrossSiteLearning) { | |
596 SetCurrentHostName("foo.com"); | |
597 AddResource(GetURL(kImagePath), content::RESOURCE_TYPE_IMAGE, net::LOWEST); | |
598 AddResource(GetURL(kStylePath), content::RESOURCE_TYPE_STYLESHEET, | |
599 net::HIGHEST); | |
600 AddResource(GetURL(kScriptPath), content::RESOURCE_TYPE_SCRIPT, net::MEDIUM); | |
601 AddResource(GetURL(kFontPath), content::RESOURCE_TYPE_FONT_RESOURCE, | |
602 net::HIGHEST); | |
603 NavigateToURLAndCheckSubresources(GetURL(kHtmlSubresourcesPath)); | |
604 ClearResources(); | |
605 | |
606 SetCurrentHostName("bar.com"); | |
607 AddResource(GetURL(kImagePath), content::RESOURCE_TYPE_IMAGE, net::LOWEST); | |
608 AddResource(GetURL(kStylePath), content::RESOURCE_TYPE_STYLESHEET, | |
609 net::HIGHEST); | |
610 AddResource(GetURL(kScriptPath), content::RESOURCE_TYPE_SCRIPT, net::MEDIUM); | |
611 AddResource(GetURL(kFontPath), content::RESOURCE_TYPE_FONT_RESOURCE, | |
612 net::HIGHEST); | |
613 | |
614 NavigateToURLAndCheckSubresources(GetURL(kHtmlSubresourcesPath)); | |
615 } | |
616 | |
617 IN_PROC_BROWSER_TEST_F(ResourcePrefetchPredictorLearningBrowserTest, | |
ahemery
2016/12/19 14:53:41
When we stay in the same tab, tab_id shouldn't be
| |
618 TabIdBehavingAsExpected) { | |
619 AddResource(GetURL(kImagePath), content::RESOURCE_TYPE_IMAGE, net::LOWEST); | |
620 AddResource(GetURL(kStylePath), content::RESOURCE_TYPE_STYLESHEET, | |
621 net::HIGHEST); | |
622 AddResource(GetURL(kScriptPath), content::RESOURCE_TYPE_SCRIPT, net::MEDIUM); | |
623 AddResource(GetURL(kFontPath), content::RESOURCE_TYPE_FONT_RESOURCE, | |
624 net::HIGHEST); | |
625 NavigateToURLAndCheckSubresources(GetURL(kHtmlSubresourcesPath)); | |
626 EXPECT_EQ(GetHistoryCount(), 1U); | |
627 ClearCache(); | |
628 NavigateToURLAndCheckSubresources(GetURL(kHtmlSubresourcesPath)); | |
629 EXPECT_EQ(GetHistoryCount(), 1U); | |
630 ClearCache(); | |
631 NavigateToURLAndCheckSubresources(GetURL(kHtmlSubresourcesPath), | |
632 WindowOpenDisposition::NEW_FOREGROUND_TAB); | |
633 EXPECT_EQ(GetHistoryCount(), 2U); | |
634 ClearCache(); | |
635 NavigateToURLAndCheckSubresources(GetURL(kHtmlSubresourcesPath), | |
636 WindowOpenDisposition::NEW_WINDOW); | |
637 EXPECT_EQ(GetHistoryCount(), 3U); | |
638 ClearCache(); | |
639 NavigateToURLAndCheckSubresources(GetURL(kHtmlSubresourcesPath), | |
640 WindowOpenDisposition::NEW_POPUP); | |
641 EXPECT_EQ(GetHistoryCount(), 4U); | |
642 } | |
643 | |
532 } // namespace predictors | 644 } // namespace predictors |
OLD | NEW |