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

Side by Side Diff: chrome/browser/net/loading_predictor_observer.cc

Issue 2887133003: predictors: Refactor resource_prefetch_predictor triggering. (Closed)
Patch Set: . 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 2014 The Chromium Authors. All rights reserved. 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 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/net/resource_prefetch_predictor_observer.h" 5 #include "chrome/browser/net/loading_predictor_observer.h"
6 6
7 #include <memory> 7 #include <memory>
8 #include <string> 8 #include <string>
9 #include <utility> 9 #include <utility>
10 10
11 #include "base/memory/ptr_util.h" 11 #include "base/memory/ptr_util.h"
12 #include "base/metrics/histogram_macros.h" 12 #include "base/metrics/histogram_macros.h"
13 #include "content/public/browser/browser_thread.h" 13 #include "content/public/browser/browser_thread.h"
14 #include "content/public/browser/resource_request_info.h" 14 #include "content/public/browser/resource_request_info.h"
15 #include "net/url_request/url_request.h" 15 #include "net/url_request/url_request.h"
16 #include "url/gurl.h" 16 #include "url/gurl.h"
17 17
18 namespace content { 18 namespace content {
19 class WebContents; 19 class WebContents;
20 } 20 }
21 21
22 using content::BrowserThread; 22 using content::BrowserThread;
23 using predictors::LoadingPredictor;
23 using predictors::ResourcePrefetchPredictor; 24 using predictors::ResourcePrefetchPredictor;
24 using URLRequestSummary = 25 using URLRequestSummary =
25 predictors::ResourcePrefetchPredictor::URLRequestSummary; 26 predictors::ResourcePrefetchPredictor::URLRequestSummary;
26 27
27 namespace { 28 namespace {
28 29
29 // Enum for measuring statistics pertaining to observed request, responses and 30 // Enum for measuring statistics pertaining to observed request, responses and
30 // redirects. 31 // redirects.
31 enum RequestStats { 32 enum RequestStats {
32 REQUEST_STATS_TOTAL_RESPONSES = 0, 33 REQUEST_STATS_TOTAL_RESPONSES = 0,
33 REQUEST_STATS_TOTAL_PROCESSED_RESPONSES = 1, 34 REQUEST_STATS_TOTAL_PROCESSED_RESPONSES = 1,
34 REQUEST_STATS_NO_RESOURCE_REQUEST_INFO = 2, // Not recorded (never was). 35 REQUEST_STATS_NO_RESOURCE_REQUEST_INFO = 2, // Not recorded (never was).
35 REQUEST_STATS_NO_RENDER_FRAME_ID_FROM_REQUEST_INFO = 3, // Not recorded. 36 REQUEST_STATS_NO_RENDER_FRAME_ID_FROM_REQUEST_INFO = 3, // Not recorded.
36 REQUEST_STATS_MAX = 4, 37 REQUEST_STATS_MAX = 4,
37 }; 38 };
38 39
39 // Specific to main frame requests. 40 // Specific to main frame requests.
40 enum MainFrameRequestStats { 41 enum MainFrameRequestStats {
41 MAIN_FRAME_REQUEST_STATS_TOTAL_REQUESTS = 0, 42 MAIN_FRAME_REQUEST_STATS_TOTAL_REQUESTS = 0,
42 MAIN_FRAME_REQUEST_STATS_PROCESSED_REQUESTS = 1, 43 MAIN_FRAME_REQUEST_STATS_PROCESSED_REQUESTS = 1,
43 MAIN_FRAME_REQUEST_STATS_TOTAL_REDIRECTS = 2, 44 MAIN_FRAME_REQUEST_STATS_TOTAL_REDIRECTS = 2,
44 MAIN_FRAME_REQUEST_STATS_PROCESSED_REDIRECTS = 3, 45 MAIN_FRAME_REQUEST_STATS_PROCESSED_REDIRECTS = 3,
45 MAIN_FRAME_REQUEST_STATS_TOTAL_RESPONSES = 4, 46 MAIN_FRAME_REQUEST_STATS_TOTAL_RESPONSES = 4,
46 MAIN_FRAME_REQUEST_STATS_PROCESSED_RESPONSES = 5, 47 MAIN_FRAME_REQUEST_STATS_PROCESSED_RESPONSES = 5,
47 MAIN_FRAME_REQUEST_STATS_MAX = 6, 48 MAIN_FRAME_REQUEST_STATS_MAX = 6,
48 }; 49 };
49 50
50 void ReportRequestStats(RequestStats stat) { 51 void ReportRequestStats(RequestStats stat) {
51 UMA_HISTOGRAM_ENUMERATION("ResourcePrefetchPredictor.RequestStats", 52 UMA_HISTOGRAM_ENUMERATION("ResourcePrefetchPredictor.RequestStats", stat,
52 stat,
53 REQUEST_STATS_MAX); 53 REQUEST_STATS_MAX);
54 } 54 }
55 55
56 void ReportMainFrameRequestStats(MainFrameRequestStats stat) { 56 void ReportMainFrameRequestStats(MainFrameRequestStats stat) {
57 UMA_HISTOGRAM_ENUMERATION("ResourcePrefetchPredictor.MainFrameRequestStats", 57 UMA_HISTOGRAM_ENUMERATION("ResourcePrefetchPredictor.MainFrameRequestStats",
58 stat, 58 stat, MAIN_FRAME_REQUEST_STATS_MAX);
59 MAIN_FRAME_REQUEST_STATS_MAX);
60 } 59 }
61 60
62 bool TryToFillNavigationID( 61 bool TryToFillNavigationID(
63 predictors::NavigationID* navigation_id, 62 predictors::NavigationID* navigation_id,
64 const content::ResourceRequestInfo::WebContentsGetter& web_contents_getter, 63 const content::ResourceRequestInfo::WebContentsGetter& web_contents_getter,
65 const GURL& main_frame_url, 64 const GURL& main_frame_url,
66 const base::TimeTicks& creation_time) { 65 const base::TimeTicks& creation_time) {
67 DCHECK_CURRENTLY_ON(BrowserThread::UI); 66 DCHECK_CURRENTLY_ON(BrowserThread::UI);
68 content::WebContents* web_contents = web_contents_getter.Run(); 67 content::WebContents* web_contents = web_contents_getter.Run();
69 if (!web_contents) 68 if (!web_contents)
70 return false; 69 return false;
71 *navigation_id = 70 *navigation_id =
72 predictors::NavigationID(web_contents, main_frame_url, creation_time); 71 predictors::NavigationID(web_contents, main_frame_url, creation_time);
73 // A WebContents might be associated with something that is not a tab. 72 // A WebContents might be associated with something that is not a tab.
74 // In this case tab_id will be -1 and is_valid() will return false. 73 // In this case tab_id will be -1 and is_valid() will return false.
75 return navigation_id->is_valid(); 74 return navigation_id->is_valid();
76 } 75 }
77 76
78 } // namespace 77 } // namespace
79 78
80 namespace chrome_browser_net { 79 namespace chrome_browser_net {
81 80
82 ResourcePrefetchPredictorObserver::ResourcePrefetchPredictorObserver( 81 LoadingPredictorObserver::LoadingPredictorObserver(LoadingPredictor* predictor)
83 ResourcePrefetchPredictor* predictor)
84 : predictor_(predictor->AsWeakPtr()) { 82 : predictor_(predictor->AsWeakPtr()) {
85 DCHECK_CURRENTLY_ON(BrowserThread::UI); 83 DCHECK_CURRENTLY_ON(BrowserThread::UI);
86 } 84 }
87 85
88 ResourcePrefetchPredictorObserver::~ResourcePrefetchPredictorObserver() { 86 LoadingPredictorObserver::~LoadingPredictorObserver() {
89 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI) || 87 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI) ||
90 BrowserThread::CurrentlyOn(BrowserThread::IO)); 88 BrowserThread::CurrentlyOn(BrowserThread::IO));
91 } 89 }
92 90
93 void ResourcePrefetchPredictorObserver::OnRequestStarted( 91 void LoadingPredictorObserver::OnRequestStarted(
94 net::URLRequest* request, 92 net::URLRequest* request,
95 content::ResourceType resource_type, 93 content::ResourceType resource_type,
96 const content::ResourceRequestInfo::WebContentsGetter& 94 const content::ResourceRequestInfo::WebContentsGetter&
97 web_contents_getter) { 95 web_contents_getter) {
98 DCHECK_CURRENTLY_ON(BrowserThread::IO); 96 DCHECK_CURRENTLY_ON(BrowserThread::IO);
99 97
100 if (resource_type == content::RESOURCE_TYPE_MAIN_FRAME) 98 if (resource_type == content::RESOURCE_TYPE_MAIN_FRAME)
101 ReportMainFrameRequestStats(MAIN_FRAME_REQUEST_STATS_TOTAL_REQUESTS); 99 ReportMainFrameRequestStats(MAIN_FRAME_REQUEST_STATS_TOTAL_REQUESTS);
102 100
103 if (!ResourcePrefetchPredictor::ShouldRecordRequest(request, resource_type)) 101 if (!ResourcePrefetchPredictor::ShouldRecordRequest(request, resource_type))
104 return; 102 return;
105 103
106 auto summary = base::MakeUnique<URLRequestSummary>(); 104 auto summary = base::MakeUnique<URLRequestSummary>();
107 summary->resource_url = request->original_url(); 105 summary->resource_url = request->original_url();
108 summary->resource_type = resource_type; 106 summary->resource_type = resource_type;
109 107
110 BrowserThread::PostTask( 108 BrowserThread::PostTask(
111 BrowserThread::UI, FROM_HERE, 109 BrowserThread::UI, FROM_HERE,
112 base::BindOnce( 110 base::BindOnce(&LoadingPredictorObserver::OnRequestStartedOnUIThread,
113 &ResourcePrefetchPredictorObserver::OnRequestStartedOnUIThread, 111 base::Unretained(this), base::Passed(std::move(summary)),
114 base::Unretained(this), base::Passed(std::move(summary)), 112 web_contents_getter, request->first_party_for_cookies(),
115 web_contents_getter, request->first_party_for_cookies(), 113 request->creation_time()));
116 request->creation_time()));
117 114
118 if (resource_type == content::RESOURCE_TYPE_MAIN_FRAME) 115 if (resource_type == content::RESOURCE_TYPE_MAIN_FRAME)
119 ReportMainFrameRequestStats(MAIN_FRAME_REQUEST_STATS_PROCESSED_REQUESTS); 116 ReportMainFrameRequestStats(MAIN_FRAME_REQUEST_STATS_PROCESSED_REQUESTS);
120 } 117 }
121 118
122 void ResourcePrefetchPredictorObserver::OnRequestRedirected( 119 void LoadingPredictorObserver::OnRequestRedirected(
123 net::URLRequest* request, 120 net::URLRequest* request,
124 const GURL& redirect_url, 121 const GURL& redirect_url,
125 const content::ResourceRequestInfo::WebContentsGetter& 122 const content::ResourceRequestInfo::WebContentsGetter&
126 web_contents_getter) { 123 web_contents_getter) {
127 DCHECK_CURRENTLY_ON(BrowserThread::IO); 124 DCHECK_CURRENTLY_ON(BrowserThread::IO);
128 125
129 const content::ResourceRequestInfo* request_info = 126 const content::ResourceRequestInfo* request_info =
130 content::ResourceRequestInfo::ForRequest(request); 127 content::ResourceRequestInfo::ForRequest(request);
131 if (request_info && 128 if (request_info &&
132 request_info->GetResourceType() == content::RESOURCE_TYPE_MAIN_FRAME) { 129 request_info->GetResourceType() == content::RESOURCE_TYPE_MAIN_FRAME) {
133 ReportMainFrameRequestStats(MAIN_FRAME_REQUEST_STATS_TOTAL_REDIRECTS); 130 ReportMainFrameRequestStats(MAIN_FRAME_REQUEST_STATS_TOTAL_REDIRECTS);
134 } 131 }
135 132
136 if (!ResourcePrefetchPredictor::ShouldRecordRedirect(request)) 133 if (!ResourcePrefetchPredictor::ShouldRecordRedirect(request))
137 return; 134 return;
138 135
139 auto summary = base::MakeUnique<URLRequestSummary>(); 136 auto summary = base::MakeUnique<URLRequestSummary>();
140 if (!ResourcePrefetchPredictor::URLRequestSummary::SummarizeResponse( 137 if (!ResourcePrefetchPredictor::URLRequestSummary::SummarizeResponse(
141 *request, summary.get())) { 138 *request, summary.get())) {
142 return; 139 return;
143 } 140 }
144 summary->redirect_url = redirect_url; 141 summary->redirect_url = redirect_url;
145 142
146 BrowserThread::PostTask( 143 BrowserThread::PostTask(
147 BrowserThread::UI, FROM_HERE, 144 BrowserThread::UI, FROM_HERE,
148 base::BindOnce( 145 base::BindOnce(&LoadingPredictorObserver::OnRequestRedirectedOnUIThread,
149 &ResourcePrefetchPredictorObserver::OnRequestRedirectedOnUIThread, 146 base::Unretained(this), base::Passed(std::move(summary)),
150 base::Unretained(this), base::Passed(std::move(summary)), 147 web_contents_getter, request->first_party_for_cookies(),
151 web_contents_getter, request->first_party_for_cookies(), 148 request->creation_time()));
152 request->creation_time()));
153 149
154 if (request_info && 150 if (request_info &&
155 request_info->GetResourceType() == content::RESOURCE_TYPE_MAIN_FRAME) { 151 request_info->GetResourceType() == content::RESOURCE_TYPE_MAIN_FRAME) {
156 ReportMainFrameRequestStats(MAIN_FRAME_REQUEST_STATS_PROCESSED_REDIRECTS); 152 ReportMainFrameRequestStats(MAIN_FRAME_REQUEST_STATS_PROCESSED_REDIRECTS);
157 } 153 }
158 } 154 }
159 155
160 void ResourcePrefetchPredictorObserver::OnResponseStarted( 156 void LoadingPredictorObserver::OnResponseStarted(
161 net::URLRequest* request, 157 net::URLRequest* request,
162 const content::ResourceRequestInfo::WebContentsGetter& 158 const content::ResourceRequestInfo::WebContentsGetter&
163 web_contents_getter) { 159 web_contents_getter) {
164 DCHECK_CURRENTLY_ON(BrowserThread::IO); 160 DCHECK_CURRENTLY_ON(BrowserThread::IO);
165 161
166 ReportRequestStats(REQUEST_STATS_TOTAL_RESPONSES); 162 ReportRequestStats(REQUEST_STATS_TOTAL_RESPONSES);
167 163
168 const content::ResourceRequestInfo* request_info = 164 const content::ResourceRequestInfo* request_info =
169 content::ResourceRequestInfo::ForRequest(request); 165 content::ResourceRequestInfo::ForRequest(request);
170 if (request_info && 166 if (request_info &&
171 request_info->GetResourceType() == content::RESOURCE_TYPE_MAIN_FRAME) { 167 request_info->GetResourceType() == content::RESOURCE_TYPE_MAIN_FRAME) {
172 ReportMainFrameRequestStats(MAIN_FRAME_REQUEST_STATS_TOTAL_RESPONSES); 168 ReportMainFrameRequestStats(MAIN_FRAME_REQUEST_STATS_TOTAL_RESPONSES);
173 } 169 }
174 170
175 if (!ResourcePrefetchPredictor::ShouldRecordResponse(request)) 171 if (!ResourcePrefetchPredictor::ShouldRecordResponse(request))
176 return; 172 return;
177 auto summary = base::MakeUnique<URLRequestSummary>(); 173 auto summary = base::MakeUnique<URLRequestSummary>();
178 if (!ResourcePrefetchPredictor::URLRequestSummary::SummarizeResponse( 174 if (!ResourcePrefetchPredictor::URLRequestSummary::SummarizeResponse(
179 *request, summary.get())) { 175 *request, summary.get())) {
180 return; 176 return;
181 } 177 }
182 178
183 BrowserThread::PostTask( 179 BrowserThread::PostTask(
184 BrowserThread::UI, FROM_HERE, 180 BrowserThread::UI, FROM_HERE,
185 base::BindOnce( 181 base::BindOnce(&LoadingPredictorObserver::OnResponseStartedOnUIThread,
186 &ResourcePrefetchPredictorObserver::OnResponseStartedOnUIThread, 182 base::Unretained(this), base::Passed(std::move(summary)),
187 base::Unretained(this), base::Passed(std::move(summary)), 183 web_contents_getter, request->first_party_for_cookies(),
188 web_contents_getter, request->first_party_for_cookies(), 184 request->creation_time()));
189 request->creation_time()));
190 185
191 ReportRequestStats(REQUEST_STATS_TOTAL_PROCESSED_RESPONSES); 186 ReportRequestStats(REQUEST_STATS_TOTAL_PROCESSED_RESPONSES);
192 if (request_info && 187 if (request_info &&
193 request_info->GetResourceType() == content::RESOURCE_TYPE_MAIN_FRAME) { 188 request_info->GetResourceType() == content::RESOURCE_TYPE_MAIN_FRAME) {
194 ReportMainFrameRequestStats(MAIN_FRAME_REQUEST_STATS_PROCESSED_RESPONSES); 189 ReportMainFrameRequestStats(MAIN_FRAME_REQUEST_STATS_PROCESSED_RESPONSES);
195 } 190 }
196 } 191 }
197 192
198 void ResourcePrefetchPredictorObserver::OnRequestStartedOnUIThread( 193 void LoadingPredictorObserver::OnRequestStartedOnUIThread(
199 std::unique_ptr<URLRequestSummary> summary, 194 std::unique_ptr<URLRequestSummary> summary,
200 const content::ResourceRequestInfo::WebContentsGetter& web_contents_getter, 195 const content::ResourceRequestInfo::WebContentsGetter& web_contents_getter,
201 const GURL& main_frame_url, 196 const GURL& main_frame_url,
202 const base::TimeTicks& creation_time) const { 197 const base::TimeTicks& creation_time) const {
203 DCHECK_CURRENTLY_ON(BrowserThread::UI); 198 DCHECK_CURRENTLY_ON(BrowserThread::UI);
204 if (!TryToFillNavigationID(&summary->navigation_id, web_contents_getter, 199 if (!TryToFillNavigationID(&summary->navigation_id, web_contents_getter,
205 main_frame_url, creation_time)) { 200 main_frame_url, creation_time)) {
206 return; 201 return;
207 } 202 }
208 predictor_->RecordURLRequest(*summary); 203 if (summary->resource_type == content::RESOURCE_TYPE_MAIN_FRAME)
204 predictor_->OnMainFrameRequest(*summary);
205 predictor_->resource_prefetch_predictor()->RecordURLRequest(*summary);
209 } 206 }
210 207
211 void ResourcePrefetchPredictorObserver::OnRequestRedirectedOnUIThread( 208 void LoadingPredictorObserver::OnRequestRedirectedOnUIThread(
212 std::unique_ptr<URLRequestSummary> summary, 209 std::unique_ptr<URLRequestSummary> summary,
213 const content::ResourceRequestInfo::WebContentsGetter& web_contents_getter, 210 const content::ResourceRequestInfo::WebContentsGetter& web_contents_getter,
214 const GURL& main_frame_url, 211 const GURL& main_frame_url,
215 const base::TimeTicks& creation_time) const { 212 const base::TimeTicks& creation_time) const {
216 DCHECK_CURRENTLY_ON(BrowserThread::UI); 213 DCHECK_CURRENTLY_ON(BrowserThread::UI);
217 if (!TryToFillNavigationID(&summary->navigation_id, web_contents_getter, 214 if (!TryToFillNavigationID(&summary->navigation_id, web_contents_getter,
218 main_frame_url, creation_time)) { 215 main_frame_url, creation_time)) {
219 return; 216 return;
220 } 217 }
221 predictor_->RecordURLRedirect(*summary); 218 if (summary->resource_type == content::RESOURCE_TYPE_MAIN_FRAME)
219 predictor_->OnMainFrameRedirect(*summary);
220 predictor_->resource_prefetch_predictor()->RecordURLRedirect(*summary);
222 } 221 }
223 222
224 void ResourcePrefetchPredictorObserver::OnResponseStartedOnUIThread( 223 void LoadingPredictorObserver::OnResponseStartedOnUIThread(
225 std::unique_ptr<URLRequestSummary> summary, 224 std::unique_ptr<URLRequestSummary> summary,
226 const content::ResourceRequestInfo::WebContentsGetter& web_contents_getter, 225 const content::ResourceRequestInfo::WebContentsGetter& web_contents_getter,
227 const GURL& main_frame_url, 226 const GURL& main_frame_url,
228 const base::TimeTicks& creation_time) const { 227 const base::TimeTicks& creation_time) const {
229 DCHECK_CURRENTLY_ON(BrowserThread::UI); 228 DCHECK_CURRENTLY_ON(BrowserThread::UI);
230 if (!TryToFillNavigationID(&summary->navigation_id, web_contents_getter, 229 if (!TryToFillNavigationID(&summary->navigation_id, web_contents_getter,
231 main_frame_url, creation_time)) { 230 main_frame_url, creation_time)) {
232 return; 231 return;
233 } 232 }
234 predictor_->RecordURLResponse(*summary); 233 if (summary->resource_type == content::RESOURCE_TYPE_MAIN_FRAME)
234 predictor_->OnMainFrameResponse(*summary);
235 predictor_->resource_prefetch_predictor()->RecordURLResponse(*summary);
235 } 236 }
236 237
237 } // namespace chrome_browser_net 238 } // namespace chrome_browser_net
OLDNEW
« no previous file with comments | « chrome/browser/net/loading_predictor_observer.h ('k') | chrome/browser/net/resource_prefetch_predictor_observer.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698