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