Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2017 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "chrome/browser/predictors/loading_data_collector.h" | |
| 6 #include "chrome/browser/predictors/resource_prefetch_predictor.h" | |
|
Benoit L
2017/05/29 08:55:36
nit: unnecessary include.
trevordixon
2017/06/02 12:10:36
Gone.
| |
| 7 #include "chrome/browser/profiles/profile.h" | |
| 8 #include "components/mime_util/mime_util.h" | |
| 9 #include "content/public/browser/resource_request_info.h" | |
| 10 #include "content/public/common/resource_type.h" | |
| 11 #include "net/url_request/url_request.h" | |
| 12 | |
| 13 namespace predictors { | |
| 14 | |
| 15 namespace { | |
| 16 | |
| 17 bool g_allow_port_in_urls = false; | |
| 18 | |
| 19 } // namespace | |
| 20 | |
| 21 // static | |
| 22 bool LoadingDataCollector::ShouldRecordRequest( | |
| 23 net::URLRequest* request, | |
| 24 content::ResourceType resource_type) { | |
| 25 const content::ResourceRequestInfo* request_info = | |
| 26 content::ResourceRequestInfo::ForRequest(request); | |
| 27 if (!request_info) | |
| 28 return false; | |
| 29 | |
| 30 if (!request_info->IsMainFrame()) | |
| 31 return false; | |
| 32 | |
| 33 return resource_type == content::RESOURCE_TYPE_MAIN_FRAME && | |
| 34 IsHandledMainPage(request); | |
| 35 } | |
| 36 | |
| 37 // static | |
| 38 bool LoadingDataCollector::ShouldRecordResponse(net::URLRequest* response) { | |
| 39 const content::ResourceRequestInfo* request_info = | |
| 40 content::ResourceRequestInfo::ForRequest(response); | |
| 41 if (!request_info) | |
| 42 return false; | |
| 43 | |
| 44 if (!request_info->IsMainFrame()) | |
| 45 return false; | |
| 46 | |
| 47 content::ResourceType resource_type = request_info->GetResourceType(); | |
| 48 return resource_type == content::RESOURCE_TYPE_MAIN_FRAME | |
| 49 ? IsHandledMainPage(response) | |
| 50 : IsHandledSubresource(response, resource_type); | |
| 51 } | |
| 52 | |
| 53 // static | |
| 54 bool LoadingDataCollector::ShouldRecordRedirect(net::URLRequest* response) { | |
| 55 return ShouldRecordResponse(response); | |
| 56 } | |
| 57 | |
| 58 // static | |
| 59 bool LoadingDataCollector::IsHandledMainPage(net::URLRequest* request) { | |
| 60 const GURL& url = request->url(); | |
| 61 bool bad_port = !g_allow_port_in_urls && url.has_port(); | |
| 62 return url.SchemeIsHTTPOrHTTPS() && !bad_port; | |
| 63 } | |
| 64 | |
| 65 // static | |
| 66 bool LoadingDataCollector::IsHandledSubresource( | |
| 67 net::URLRequest* response, | |
| 68 content::ResourceType resource_type) { | |
| 69 const GURL& url = response->url(); | |
| 70 bool bad_port = !g_allow_port_in_urls && url.has_port(); | |
| 71 if (!response->first_party_for_cookies().SchemeIsHTTPOrHTTPS() || | |
| 72 !url.SchemeIsHTTPOrHTTPS() || bad_port) { | |
| 73 return false; | |
| 74 } | |
| 75 | |
| 76 std::string mime_type; | |
| 77 response->GetMimeType(&mime_type); | |
| 78 if (!IsHandledResourceType(resource_type, mime_type)) | |
| 79 return false; | |
| 80 | |
| 81 if (response->method() != "GET") | |
| 82 return false; | |
| 83 | |
| 84 if (response->original_url().spec().length() > | |
| 85 ResourcePrefetchPredictorTables::kMaxStringLength) { | |
| 86 return false; | |
| 87 } | |
| 88 | |
| 89 if (!response->response_info().headers.get()) | |
| 90 return false; | |
| 91 | |
| 92 return true; | |
| 93 } | |
| 94 | |
| 95 // static | |
| 96 bool LoadingDataCollector::IsHandledResourceType( | |
| 97 content::ResourceType resource_type, | |
| 98 const std::string& mime_type) { | |
| 99 content::ResourceType actual_resource_type = | |
| 100 ResourcePrefetchPredictor::GetResourceType(resource_type, mime_type); | |
| 101 return actual_resource_type == content::RESOURCE_TYPE_STYLESHEET || | |
| 102 actual_resource_type == content::RESOURCE_TYPE_SCRIPT || | |
| 103 actual_resource_type == content::RESOURCE_TYPE_IMAGE || | |
| 104 actual_resource_type == content::RESOURCE_TYPE_FONT_RESOURCE; | |
| 105 } | |
| 106 | |
| 107 // static | |
| 108 void LoadingDataCollector::SetAllowPortInUrlsForTesting(bool state) { | |
| 109 g_allow_port_in_urls = state; | |
| 110 } | |
| 111 | |
| 112 LoadingDataCollector::LoadingDataCollector(ResourcePrefetchPredictor* predictor) | |
| 113 : predictor_(predictor) {} | |
| 114 | |
| 115 LoadingDataCollector::~LoadingDataCollector() {} | |
| 116 | |
| 117 void LoadingDataCollector::RecordURLRequest( | |
| 118 const ResourcePrefetchPredictor::URLRequestSummary& request) { | |
| 119 predictor_->RecordURLRequest(request); | |
| 120 } | |
| 121 | |
| 122 void LoadingDataCollector::RecordURLResponse( | |
| 123 const ResourcePrefetchPredictor::URLRequestSummary& response) { | |
| 124 predictor_->RecordURLResponse(response); | |
| 125 } | |
| 126 | |
| 127 void LoadingDataCollector::RecordURLRedirect( | |
| 128 const ResourcePrefetchPredictor::URLRequestSummary& response) { | |
| 129 predictor_->RecordURLRedirect(response); | |
| 130 } | |
| 131 | |
| 132 void LoadingDataCollector::RecordMainFrameLoadComplete( | |
| 133 const NavigationID& navigation_id) { | |
| 134 predictor_->RecordMainFrameLoadComplete(navigation_id); | |
| 135 } | |
| 136 | |
| 137 void LoadingDataCollector::RecordFirstContentfulPaint( | |
| 138 const NavigationID& navigation_id, | |
| 139 const base::TimeTicks& first_contentful_paint) { | |
| 140 predictor_->RecordFirstContentfulPaint(navigation_id, first_contentful_paint); | |
| 141 } | |
| 142 | |
| 143 } // namespace predictors | |
| OLD | NEW |