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

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

Issue 2896713003: Create LoadingDataCollector class and have observers rely on it instead of ResourcePrefetchPredictor (Closed)
Patch Set: Created 3 years, 7 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 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/net/resource_prefetch_predictor_observer.h" 5 #include "chrome/browser/net/resource_prefetch_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::ResourcePrefetchPredictor;
24 using URLRequestSummary = 23 using URLRequestSummary =
25 predictors::ResourcePrefetchPredictor::URLRequestSummary; 24 predictors::ResourcePrefetchPredictor::URLRequestSummary;
26 25
27 namespace { 26 namespace {
28 27
29 // Enum for measuring statistics pertaining to observed request, responses and 28 // Enum for measuring statistics pertaining to observed request, responses and
30 // redirects. 29 // redirects.
31 enum RequestStats { 30 enum RequestStats {
32 REQUEST_STATS_TOTAL_RESPONSES = 0, 31 REQUEST_STATS_TOTAL_RESPONSES = 0,
33 REQUEST_STATS_TOTAL_PROCESSED_RESPONSES = 1, 32 REQUEST_STATS_TOTAL_PROCESSED_RESPONSES = 1,
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after
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 ResourcePrefetchPredictorObserver::ResourcePrefetchPredictorObserver(
83 ResourcePrefetchPredictor* predictor) 82 predictors::GlowplugCollector* collector)
84 : predictor_(predictor->AsWeakPtr()) { 83 : collector_(collector->AsWeakPtr()) {
85 DCHECK_CURRENTLY_ON(BrowserThread::UI); 84 DCHECK_CURRENTLY_ON(BrowserThread::UI);
86 } 85 }
87 86
88 ResourcePrefetchPredictorObserver::~ResourcePrefetchPredictorObserver() { 87 ResourcePrefetchPredictorObserver::~ResourcePrefetchPredictorObserver() {
89 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI) || 88 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI) ||
90 BrowserThread::CurrentlyOn(BrowserThread::IO)); 89 BrowserThread::CurrentlyOn(BrowserThread::IO));
91 } 90 }
92 91
93 void ResourcePrefetchPredictorObserver::OnRequestStarted( 92 void ResourcePrefetchPredictorObserver::OnRequestStarted(
94 net::URLRequest* request, 93 net::URLRequest* request,
95 content::ResourceType resource_type, 94 content::ResourceType resource_type,
96 const content::ResourceRequestInfo::WebContentsGetter& 95 const content::ResourceRequestInfo::WebContentsGetter&
97 web_contents_getter) { 96 web_contents_getter) {
98 DCHECK_CURRENTLY_ON(BrowserThread::IO); 97 DCHECK_CURRENTLY_ON(BrowserThread::IO);
99 98
100 if (resource_type == content::RESOURCE_TYPE_MAIN_FRAME) 99 if (resource_type == content::RESOURCE_TYPE_MAIN_FRAME)
101 ReportMainFrameRequestStats(MAIN_FRAME_REQUEST_STATS_TOTAL_REQUESTS); 100 ReportMainFrameRequestStats(MAIN_FRAME_REQUEST_STATS_TOTAL_REQUESTS);
102 101
103 if (!ResourcePrefetchPredictor::ShouldRecordRequest(request, resource_type)) 102 if (!predictors::GlowplugCollector::ShouldRecordRequest(request,
Benoit L 2017/05/22 13:37:16 nit: why not "using predictors::LoadingDataCollect
trevordixon 2017/05/23 13:05:15 Done.
103 resource_type))
104 return; 104 return;
105 105
106 auto summary = base::MakeUnique<URLRequestSummary>(); 106 auto summary = base::MakeUnique<URLRequestSummary>();
107 summary->resource_url = request->original_url(); 107 summary->resource_url = request->original_url();
108 summary->resource_type = resource_type; 108 summary->resource_type = resource_type;
109 109
110 BrowserThread::PostTask( 110 BrowserThread::PostTask(
111 BrowserThread::UI, FROM_HERE, 111 BrowserThread::UI, FROM_HERE,
112 base::BindOnce( 112 base::BindOnce(
113 &ResourcePrefetchPredictorObserver::OnRequestStartedOnUIThread, 113 &ResourcePrefetchPredictorObserver::OnRequestStartedOnUIThread,
(...skipping 12 matching lines...) Expand all
126 web_contents_getter) { 126 web_contents_getter) {
127 DCHECK_CURRENTLY_ON(BrowserThread::IO); 127 DCHECK_CURRENTLY_ON(BrowserThread::IO);
128 128
129 const content::ResourceRequestInfo* request_info = 129 const content::ResourceRequestInfo* request_info =
130 content::ResourceRequestInfo::ForRequest(request); 130 content::ResourceRequestInfo::ForRequest(request);
131 if (request_info && 131 if (request_info &&
132 request_info->GetResourceType() == content::RESOURCE_TYPE_MAIN_FRAME) { 132 request_info->GetResourceType() == content::RESOURCE_TYPE_MAIN_FRAME) {
133 ReportMainFrameRequestStats(MAIN_FRAME_REQUEST_STATS_TOTAL_REDIRECTS); 133 ReportMainFrameRequestStats(MAIN_FRAME_REQUEST_STATS_TOTAL_REDIRECTS);
134 } 134 }
135 135
136 if (!ResourcePrefetchPredictor::ShouldRecordRedirect(request)) 136 if (!predictors::GlowplugCollector::ShouldRecordRedirect(request))
Benoit L 2017/05/22 13:37:16 nit: ditto.
trevordixon 2017/05/23 13:05:15 Done.
137 return; 137 return;
138 138
139 auto summary = base::MakeUnique<URLRequestSummary>(); 139 auto summary = base::MakeUnique<URLRequestSummary>();
140 if (!ResourcePrefetchPredictor::URLRequestSummary::SummarizeResponse( 140 if (!URLRequestSummary::SummarizeResponse(*request, summary.get())) {
141 *request, summary.get())) {
142 return; 141 return;
143 } 142 }
144 summary->redirect_url = redirect_url; 143 summary->redirect_url = redirect_url;
145 144
146 BrowserThread::PostTask( 145 BrowserThread::PostTask(
147 BrowserThread::UI, FROM_HERE, 146 BrowserThread::UI, FROM_HERE,
148 base::BindOnce( 147 base::BindOnce(
149 &ResourcePrefetchPredictorObserver::OnRequestRedirectedOnUIThread, 148 &ResourcePrefetchPredictorObserver::OnRequestRedirectedOnUIThread,
150 base::Unretained(this), base::Passed(std::move(summary)), 149 base::Unretained(this), base::Passed(std::move(summary)),
151 web_contents_getter, request->first_party_for_cookies(), 150 web_contents_getter, request->first_party_for_cookies(),
(...skipping 13 matching lines...) Expand all
165 164
166 ReportRequestStats(REQUEST_STATS_TOTAL_RESPONSES); 165 ReportRequestStats(REQUEST_STATS_TOTAL_RESPONSES);
167 166
168 const content::ResourceRequestInfo* request_info = 167 const content::ResourceRequestInfo* request_info =
169 content::ResourceRequestInfo::ForRequest(request); 168 content::ResourceRequestInfo::ForRequest(request);
170 if (request_info && 169 if (request_info &&
171 request_info->GetResourceType() == content::RESOURCE_TYPE_MAIN_FRAME) { 170 request_info->GetResourceType() == content::RESOURCE_TYPE_MAIN_FRAME) {
172 ReportMainFrameRequestStats(MAIN_FRAME_REQUEST_STATS_TOTAL_RESPONSES); 171 ReportMainFrameRequestStats(MAIN_FRAME_REQUEST_STATS_TOTAL_RESPONSES);
173 } 172 }
174 173
175 if (!ResourcePrefetchPredictor::ShouldRecordResponse(request)) 174 if (!predictors::GlowplugCollector::ShouldRecordResponse(request))
176 return; 175 return;
177 auto summary = base::MakeUnique<URLRequestSummary>(); 176 auto summary = base::MakeUnique<URLRequestSummary>();
178 if (!ResourcePrefetchPredictor::URLRequestSummary::SummarizeResponse( 177 if (!URLRequestSummary::SummarizeResponse(*request, summary.get())) {
179 *request, summary.get())) {
180 return; 178 return;
181 } 179 }
182 180
183 BrowserThread::PostTask( 181 BrowserThread::PostTask(
184 BrowserThread::UI, FROM_HERE, 182 BrowserThread::UI, FROM_HERE,
185 base::BindOnce( 183 base::BindOnce(
186 &ResourcePrefetchPredictorObserver::OnResponseStartedOnUIThread, 184 &ResourcePrefetchPredictorObserver::OnResponseStartedOnUIThread,
187 base::Unretained(this), base::Passed(std::move(summary)), 185 base::Unretained(this), base::Passed(std::move(summary)),
188 web_contents_getter, request->first_party_for_cookies(), 186 web_contents_getter, request->first_party_for_cookies(),
189 request->creation_time())); 187 request->creation_time()));
190 188
191 ReportRequestStats(REQUEST_STATS_TOTAL_PROCESSED_RESPONSES); 189 ReportRequestStats(REQUEST_STATS_TOTAL_PROCESSED_RESPONSES);
192 if (request_info && 190 if (request_info &&
193 request_info->GetResourceType() == content::RESOURCE_TYPE_MAIN_FRAME) { 191 request_info->GetResourceType() == content::RESOURCE_TYPE_MAIN_FRAME) {
194 ReportMainFrameRequestStats(MAIN_FRAME_REQUEST_STATS_PROCESSED_RESPONSES); 192 ReportMainFrameRequestStats(MAIN_FRAME_REQUEST_STATS_PROCESSED_RESPONSES);
195 } 193 }
196 } 194 }
197 195
198 void ResourcePrefetchPredictorObserver::OnRequestStartedOnUIThread( 196 void ResourcePrefetchPredictorObserver::OnRequestStartedOnUIThread(
199 std::unique_ptr<URLRequestSummary> summary, 197 std::unique_ptr<URLRequestSummary> summary,
200 const content::ResourceRequestInfo::WebContentsGetter& web_contents_getter, 198 const content::ResourceRequestInfo::WebContentsGetter& web_contents_getter,
201 const GURL& main_frame_url, 199 const GURL& main_frame_url,
202 const base::TimeTicks& creation_time) const { 200 const base::TimeTicks& creation_time) const {
203 DCHECK_CURRENTLY_ON(BrowserThread::UI); 201 DCHECK_CURRENTLY_ON(BrowserThread::UI);
204 if (!TryToFillNavigationID(&summary->navigation_id, web_contents_getter, 202 if (!TryToFillNavigationID(&summary->navigation_id, web_contents_getter,
205 main_frame_url, creation_time)) { 203 main_frame_url, creation_time)) {
206 return; 204 return;
207 } 205 }
208 predictor_->RecordURLRequest(*summary); 206 collector_->RecordURLRequest(*summary);
209 } 207 }
210 208
211 void ResourcePrefetchPredictorObserver::OnRequestRedirectedOnUIThread( 209 void ResourcePrefetchPredictorObserver::OnRequestRedirectedOnUIThread(
212 std::unique_ptr<URLRequestSummary> summary, 210 std::unique_ptr<URLRequestSummary> summary,
213 const content::ResourceRequestInfo::WebContentsGetter& web_contents_getter, 211 const content::ResourceRequestInfo::WebContentsGetter& web_contents_getter,
214 const GURL& main_frame_url, 212 const GURL& main_frame_url,
215 const base::TimeTicks& creation_time) const { 213 const base::TimeTicks& creation_time) const {
216 DCHECK_CURRENTLY_ON(BrowserThread::UI); 214 DCHECK_CURRENTLY_ON(BrowserThread::UI);
217 if (!TryToFillNavigationID(&summary->navigation_id, web_contents_getter, 215 if (!TryToFillNavigationID(&summary->navigation_id, web_contents_getter,
218 main_frame_url, creation_time)) { 216 main_frame_url, creation_time)) {
219 return; 217 return;
220 } 218 }
221 predictor_->RecordURLRedirect(*summary); 219 collector_->RecordURLRedirect(*summary);
222 } 220 }
223 221
224 void ResourcePrefetchPredictorObserver::OnResponseStartedOnUIThread( 222 void ResourcePrefetchPredictorObserver::OnResponseStartedOnUIThread(
225 std::unique_ptr<URLRequestSummary> summary, 223 std::unique_ptr<URLRequestSummary> summary,
226 const content::ResourceRequestInfo::WebContentsGetter& web_contents_getter, 224 const content::ResourceRequestInfo::WebContentsGetter& web_contents_getter,
227 const GURL& main_frame_url, 225 const GURL& main_frame_url,
228 const base::TimeTicks& creation_time) const { 226 const base::TimeTicks& creation_time) const {
229 DCHECK_CURRENTLY_ON(BrowserThread::UI); 227 DCHECK_CURRENTLY_ON(BrowserThread::UI);
230 if (!TryToFillNavigationID(&summary->navigation_id, web_contents_getter, 228 if (!TryToFillNavigationID(&summary->navigation_id, web_contents_getter,
231 main_frame_url, creation_time)) { 229 main_frame_url, creation_time)) {
232 return; 230 return;
233 } 231 }
234 predictor_->RecordURLResponse(*summary); 232 collector_->RecordURLResponse(*summary);
235 } 233 }
236 234
237 } // namespace chrome_browser_net 235 } // namespace chrome_browser_net
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698