Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(765)

Side by Side Diff: chrome/browser/predictors/loading_test_util.cc

Issue 2896713003: Create LoadingDataCollector class and have observers rely on it instead of ResourcePrefetchPredictor (Closed)
Patch Set: Rebase, merge loading_stats_collector. Created 3 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 "chrome/browser/predictors/resource_prefetch_predictor_test_util.h" 5 #include "chrome/browser/predictors/loading_test_util.h"
6 6
7 #include <cmath> 7 #include <cmath>
8 #include <memory>
9
10 #include "content/public/browser/resource_request_info.h"
11 #include "net/http/http_response_headers.h"
12 #include "net/url_request/url_request_test_util.h"
8 13
9 namespace { 14 namespace {
10 15
16 predictors::EmptyURLRequestDelegate empty_url_request_delegate;
17
11 bool AlmostEqual(const double x, const double y) { 18 bool AlmostEqual(const double x, const double y) {
12 return std::fabs(x - y) <= 1e-6; // Arbitrary but close enough. 19 return std::fabs(x - y) <= 1e-6; // Arbitrary but close enough.
13 } 20 }
14 21
15 } // namespace 22 } // namespace
16 23
17 namespace predictors { 24 namespace predictors {
18 25
19 using URLRequestSummary = ResourcePrefetchPredictor::URLRequestSummary; 26 using URLRequestSummary = ResourcePrefetchPredictor::URLRequestSummary;
20 using PageRequestSummary = ResourcePrefetchPredictor::PageRequestSummary; 27 using PageRequestSummary = ResourcePrefetchPredictor::PageRequestSummary;
(...skipping 183 matching lines...) Expand 10 before | Expand all | Expand 10 after
204 config->max_consecutive_misses = 2; 211 config->max_consecutive_misses = 2;
205 config->max_redirect_consecutive_misses = 2; 212 config->max_redirect_consecutive_misses = 2;
206 config->min_resource_confidence_to_trigger_prefetch = 0.5; 213 config->min_resource_confidence_to_trigger_prefetch = 0.5;
207 } 214 }
208 config->is_url_learning_enabled = true; 215 config->is_url_learning_enabled = true;
209 config->is_manifests_enabled = true; 216 config->is_manifests_enabled = true;
210 config->is_origin_learning_enabled = true; 217 config->is_origin_learning_enabled = true;
211 config->mode = LoadingPredictorConfig::LEARNING; 218 config->mode = LoadingPredictorConfig::LEARNING;
212 } 219 }
213 220
221 scoped_refptr<net::HttpResponseHeaders> MakeResponseHeaders(
222 const char* headers) {
223 return make_scoped_refptr(new net::HttpResponseHeaders(
224 net::HttpUtil::AssembleRawHeaders(headers, strlen(headers))));
225 }
226
227 MockURLRequestJob::MockURLRequestJob(net::URLRequest* request,
228 const net::HttpResponseInfo& response_info,
229 const std::string& mime_type)
230 : net::URLRequestJob(request, nullptr),
231 response_info_(response_info),
232 mime_type_(mime_type) {}
233
234 bool MockURLRequestJob::GetMimeType(std::string* mime_type) const {
235 *mime_type = mime_type_;
236 return true;
237 }
238
239 void MockURLRequestJob::Start() {
240 NotifyHeadersComplete();
241 }
242
243 void MockURLRequestJob::GetResponseInfo(net::HttpResponseInfo* info) {
244 *info = response_info_;
245 }
246
247 MockURLRequestJobFactory::MockURLRequestJobFactory() {}
248 MockURLRequestJobFactory::~MockURLRequestJobFactory() {}
249
250 void MockURLRequestJobFactory::Reset() {
251 response_info_ = net::HttpResponseInfo();
252 mime_type_ = std::string();
253 }
254
255 net::URLRequestJob* MockURLRequestJobFactory::MaybeCreateJobWithProtocolHandler(
256 const std::string& scheme,
257 net::URLRequest* request,
258 net::NetworkDelegate* network_delegate) const {
259 return new MockURLRequestJob(request, response_info_, mime_type_);
260 }
261
262 net::URLRequestJob* MockURLRequestJobFactory::MaybeInterceptRedirect(
263 net::URLRequest* request,
264 net::NetworkDelegate* network_delegate,
265 const GURL& location) const {
266 return nullptr;
267 }
268
269 net::URLRequestJob* MockURLRequestJobFactory::MaybeInterceptResponse(
270 net::URLRequest* request,
271 net::NetworkDelegate* network_delegate) const {
272 return nullptr;
273 }
274
275 bool MockURLRequestJobFactory::IsHandledProtocol(
276 const std::string& scheme) const {
277 return true;
278 }
279
280 bool MockURLRequestJobFactory::IsSafeRedirectTarget(
281 const GURL& location) const {
282 return true;
283 }
284
285 std::unique_ptr<net::URLRequest> CreateURLRequest(
286 const net::TestURLRequestContext& url_request_context,
287 const GURL& url,
288 net::RequestPriority priority,
289 content::ResourceType resource_type,
290 bool is_main_frame) {
291 std::unique_ptr<net::URLRequest> request = url_request_context.CreateRequest(
292 url, priority, &empty_url_request_delegate);
293 request->set_first_party_for_cookies(url);
294 content::ResourceRequestInfo::AllocateForTesting(
295 request.get(), resource_type, nullptr, -1, -1, -1, is_main_frame, false,
296 false, true, content::PREVIEWS_OFF);
297 request->Start();
298 return request;
299 }
300
214 std::ostream& operator<<(std::ostream& os, const PrefetchData& data) { 301 std::ostream& operator<<(std::ostream& os, const PrefetchData& data) {
215 os << "[" << data.primary_key() << "," << data.last_visit_time() << "]" 302 os << "[" << data.primary_key() << "," << data.last_visit_time() << "]"
216 << std::endl; 303 << std::endl;
217 for (const ResourceData& resource : data.resources()) 304 for (const ResourceData& resource : data.resources())
218 os << "\t\t" << resource << std::endl; 305 os << "\t\t" << resource << std::endl;
219 return os; 306 return os;
220 } 307 }
221 308
222 std::ostream& operator<<(std::ostream& os, const ResourceData& resource) { 309 std::ostream& operator<<(std::ostream& os, const ResourceData& resource) {
223 return os << "[" << resource.resource_url() << "," << resource.resource_type() 310 return os << "[" << resource.resource_url() << "," << resource.resource_type()
(...skipping 172 matching lines...) Expand 10 before | Expand all | Expand 10 after
396 483
397 return equal; 484 return equal;
398 } 485 }
399 486
400 bool operator==(const PrecacheResource& lhs, const PrecacheResource& rhs) { 487 bool operator==(const PrecacheResource& lhs, const PrecacheResource& rhs) {
401 return lhs.url() == rhs.url() && 488 return lhs.url() == rhs.url() &&
402 AlmostEqual(lhs.weight_ratio(), rhs.weight_ratio()); 489 AlmostEqual(lhs.weight_ratio(), rhs.weight_ratio());
403 } 490 }
404 491
405 } // namespace precache 492 } // namespace precache
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698