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