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