| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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.h" | 5 #include "chrome/browser/predictors/resource_prefetch_predictor.h" |
| 6 | 6 |
| 7 #include <map> | 7 #include <map> |
| 8 #include <set> | 8 #include <set> |
| 9 #include <utility> | 9 #include <utility> |
| 10 | 10 |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 48 "font/ttf", | 48 "font/ttf", |
| 49 "application/x-font-otf", | 49 "application/x-font-otf", |
| 50 "x-font/woff", | 50 "x-font/woff", |
| 51 "application/font-sfnt", | 51 "application/font-sfnt", |
| 52 "application/font-ttf"}; | 52 "application/font-ttf"}; |
| 53 | 53 |
| 54 const size_t kMaxManifestByteSize = 16 * 1024; | 54 const size_t kMaxManifestByteSize = 16 * 1024; |
| 55 const size_t kNumSampleHosts = 50; | 55 const size_t kNumSampleHosts = 50; |
| 56 const size_t kReportReadinessThreshold = 50; | 56 const size_t kReportReadinessThreshold = 50; |
| 57 | 57 |
| 58 bool g_allow_port_in_urls = false; | |
| 59 | |
| 60 // For reporting events of interest that are not tied to any navigation. | 58 // For reporting events of interest that are not tied to any navigation. |
| 61 enum ReportingEvent { | 59 enum ReportingEvent { |
| 62 REPORTING_EVENT_ALL_HISTORY_CLEARED = 0, | 60 REPORTING_EVENT_ALL_HISTORY_CLEARED = 0, |
| 63 REPORTING_EVENT_PARTIAL_HISTORY_CLEARED = 1, | 61 REPORTING_EVENT_PARTIAL_HISTORY_CLEARED = 1, |
| 64 REPORTING_EVENT_COUNT = 2 | 62 REPORTING_EVENT_COUNT = 2 |
| 65 }; | 63 }; |
| 66 | 64 |
| 67 float ComputeRedirectConfidence(const predictors::RedirectStat& redirect) { | 65 float ComputeRedirectConfidence(const predictors::RedirectStat& redirect) { |
| 68 return (redirect.number_of_hits() + 0.0) / | 66 return (redirect.number_of_hits() + 0.0) / |
| 69 (redirect.number_of_hits() + redirect.number_of_misses()); | 67 (redirect.number_of_hits() + redirect.number_of_misses()); |
| (...skipping 95 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 165 const precache::PrecacheManifest& rhs) const { | 163 const precache::PrecacheManifest& rhs) const { |
| 166 return lhs.id().id() < rhs.id().id(); | 164 return lhs.id().id() < rhs.id().id(); |
| 167 } | 165 } |
| 168 | 166 |
| 169 } // namespace internal | 167 } // namespace internal |
| 170 | 168 |
| 171 //////////////////////////////////////////////////////////////////////////////// | 169 //////////////////////////////////////////////////////////////////////////////// |
| 172 // ResourcePrefetchPredictor static functions. | 170 // ResourcePrefetchPredictor static functions. |
| 173 | 171 |
| 174 // static | 172 // static |
| 175 bool ResourcePrefetchPredictor::ShouldRecordRequest( | |
| 176 net::URLRequest* request, | |
| 177 content::ResourceType resource_type) { | |
| 178 const content::ResourceRequestInfo* request_info = | |
| 179 content::ResourceRequestInfo::ForRequest(request); | |
| 180 if (!request_info) | |
| 181 return false; | |
| 182 | |
| 183 if (!request_info->IsMainFrame()) | |
| 184 return false; | |
| 185 | |
| 186 return resource_type == content::RESOURCE_TYPE_MAIN_FRAME && | |
| 187 IsHandledMainPage(request); | |
| 188 } | |
| 189 | |
| 190 // static | |
| 191 bool ResourcePrefetchPredictor::ShouldRecordResponse( | |
| 192 net::URLRequest* response) { | |
| 193 const content::ResourceRequestInfo* request_info = | |
| 194 content::ResourceRequestInfo::ForRequest(response); | |
| 195 if (!request_info) | |
| 196 return false; | |
| 197 | |
| 198 if (!request_info->IsMainFrame()) | |
| 199 return false; | |
| 200 | |
| 201 content::ResourceType resource_type = request_info->GetResourceType(); | |
| 202 return resource_type == content::RESOURCE_TYPE_MAIN_FRAME | |
| 203 ? IsHandledMainPage(response) | |
| 204 : IsHandledSubresource(response, resource_type); | |
| 205 } | |
| 206 | |
| 207 // static | |
| 208 bool ResourcePrefetchPredictor::ShouldRecordRedirect( | |
| 209 net::URLRequest* response) { | |
| 210 return ShouldRecordResponse(response); | |
| 211 } | |
| 212 | |
| 213 // static | |
| 214 bool ResourcePrefetchPredictor::IsHandledMainPage(net::URLRequest* request) { | |
| 215 const GURL& url = request->url(); | |
| 216 bool bad_port = !g_allow_port_in_urls && url.has_port(); | |
| 217 return url.SchemeIsHTTPOrHTTPS() && !bad_port; | |
| 218 } | |
| 219 | |
| 220 // static | |
| 221 bool ResourcePrefetchPredictor::IsHandledSubresource( | |
| 222 net::URLRequest* response, | |
| 223 content::ResourceType resource_type) { | |
| 224 const GURL& url = response->url(); | |
| 225 bool bad_port = !g_allow_port_in_urls && url.has_port(); | |
| 226 if (!response->first_party_for_cookies().SchemeIsHTTPOrHTTPS() || | |
| 227 !url.SchemeIsHTTPOrHTTPS() || bad_port) { | |
| 228 return false; | |
| 229 } | |
| 230 | |
| 231 std::string mime_type; | |
| 232 response->GetMimeType(&mime_type); | |
| 233 if (!IsHandledResourceType(resource_type, mime_type)) | |
| 234 return false; | |
| 235 | |
| 236 if (response->method() != "GET") | |
| 237 return false; | |
| 238 | |
| 239 if (response->original_url().spec().length() > | |
| 240 ResourcePrefetchPredictorTables::kMaxStringLength) { | |
| 241 return false; | |
| 242 } | |
| 243 | |
| 244 if (!response->response_info().headers.get()) | |
| 245 return false; | |
| 246 | |
| 247 return true; | |
| 248 } | |
| 249 | |
| 250 // static | |
| 251 bool ResourcePrefetchPredictor::IsHandledResourceType( | |
| 252 content::ResourceType resource_type, | |
| 253 const std::string& mime_type) { | |
| 254 content::ResourceType actual_resource_type = | |
| 255 GetResourceType(resource_type, mime_type); | |
| 256 return actual_resource_type == content::RESOURCE_TYPE_STYLESHEET || | |
| 257 actual_resource_type == content::RESOURCE_TYPE_SCRIPT || | |
| 258 actual_resource_type == content::RESOURCE_TYPE_IMAGE || | |
| 259 actual_resource_type == content::RESOURCE_TYPE_FONT_RESOURCE; | |
| 260 } | |
| 261 | |
| 262 // static | |
| 263 content::ResourceType ResourcePrefetchPredictor::GetResourceType( | 173 content::ResourceType ResourcePrefetchPredictor::GetResourceType( |
| 264 content::ResourceType resource_type, | 174 content::ResourceType resource_type, |
| 265 const std::string& mime_type) { | 175 const std::string& mime_type) { |
| 266 // Restricts content::RESOURCE_TYPE_{PREFETCH,SUB_RESOURCE,XHR} to a small set | 176 // Restricts content::RESOURCE_TYPE_{PREFETCH,SUB_RESOURCE,XHR} to a small set |
| 267 // of mime types, because these resource types don't communicate how the | 177 // of mime types, because these resource types don't communicate how the |
| 268 // resources will be used. | 178 // resources will be used. |
| 269 if (resource_type == content::RESOURCE_TYPE_PREFETCH || | 179 if (resource_type == content::RESOURCE_TYPE_PREFETCH || |
| 270 resource_type == content::RESOURCE_TYPE_SUB_RESOURCE || | 180 resource_type == content::RESOURCE_TYPE_SUB_RESOURCE || |
| 271 resource_type == content::RESOURCE_TYPE_XHR) { | 181 resource_type == content::RESOURCE_TYPE_XHR) { |
| 272 return GetResourceTypeFromMimeType(mime_type, | 182 return GetResourceTypeFromMimeType(mime_type, |
| (...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 346 kMinRedirectConfidenceToTriggerPrefetch || | 256 kMinRedirectConfidenceToTriggerPrefetch || |
| 347 (redirect.number_of_hits() < kMinRedirectHitsToTriggerPrefetch && | 257 (redirect.number_of_hits() < kMinRedirectHitsToTriggerPrefetch && |
| 348 redirect.url() != entry_point)) { | 258 redirect.url() != entry_point)) { |
| 349 return false; | 259 return false; |
| 350 } | 260 } |
| 351 | 261 |
| 352 *redirect_endpoint = redirect.url(); | 262 *redirect_endpoint = redirect.url(); |
| 353 return true; | 263 return true; |
| 354 } | 264 } |
| 355 | 265 |
| 356 // static | |
| 357 void ResourcePrefetchPredictor::SetAllowPortInUrlsForTesting(bool state) { | |
| 358 g_allow_port_in_urls = state; | |
| 359 } | |
| 360 | |
| 361 //////////////////////////////////////////////////////////////////////////////// | 266 //////////////////////////////////////////////////////////////////////////////// |
| 362 // ResourcePrefetchPredictor nested types. | 267 // ResourcePrefetchPredictor nested types. |
| 363 | 268 |
| 364 ResourcePrefetchPredictor::OriginRequestSummary::OriginRequestSummary() | 269 ResourcePrefetchPredictor::OriginRequestSummary::OriginRequestSummary() |
| 365 : origin(), | 270 : origin(), |
| 366 always_access_network(false), | 271 always_access_network(false), |
| 367 accessed_network(false), | 272 accessed_network(false), |
| 368 first_occurrence(0) {} | 273 first_occurrence(0) {} |
| 369 | 274 |
| 370 ResourcePrefetchPredictor::OriginRequestSummary::OriginRequestSummary( | 275 ResourcePrefetchPredictor::OriginRequestSummary::OriginRequestSummary( |
| (...skipping 1064 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1435 TestObserver::~TestObserver() { | 1340 TestObserver::~TestObserver() { |
| 1436 predictor_->SetObserverForTesting(nullptr); | 1341 predictor_->SetObserverForTesting(nullptr); |
| 1437 } | 1342 } |
| 1438 | 1343 |
| 1439 TestObserver::TestObserver(ResourcePrefetchPredictor* predictor) | 1344 TestObserver::TestObserver(ResourcePrefetchPredictor* predictor) |
| 1440 : predictor_(predictor) { | 1345 : predictor_(predictor) { |
| 1441 predictor_->SetObserverForTesting(this); | 1346 predictor_->SetObserverForTesting(this); |
| 1442 } | 1347 } |
| 1443 | 1348 |
| 1444 } // namespace predictors | 1349 } // namespace predictors |
| OLD | NEW |