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/net/resource_prefetch_predictor_observer.h" | 5 #include "chrome/browser/net/resource_prefetch_predictor_observer.h" |
6 | 6 |
7 #include <memory> | |
7 #include <string> | 8 #include <string> |
9 #include <utility> | |
ahemery
2016/12/06 14:31:11
For std::move
| |
8 | 10 |
11 #include "base/memory/ptr_util.h" | |
9 #include "base/metrics/histogram_macros.h" | 12 #include "base/metrics/histogram_macros.h" |
10 #include "content/public/browser/browser_thread.h" | 13 #include "content/public/browser/browser_thread.h" |
11 #include "content/public/browser/render_frame_host.h" | 14 #include "content/public/browser/render_frame_host.h" |
15 #include "content/public/browser/render_process_host.h" | |
12 #include "content/public/browser/resource_request_info.h" | 16 #include "content/public/browser/resource_request_info.h" |
17 #include "content/public/browser/web_contents.h" | |
13 #include "net/url_request/url_request.h" | 18 #include "net/url_request/url_request.h" |
14 #include "url/gurl.h" | 19 #include "url/gurl.h" |
15 | 20 |
16 using content::BrowserThread; | 21 using content::BrowserThread; |
17 using predictors::ResourcePrefetchPredictor; | 22 using predictors::ResourcePrefetchPredictor; |
23 using URLRequestSummary = | |
ahemery
2016/12/06 14:31:11
Added in .cc file because removed from .h
| |
24 predictors::ResourcePrefetchPredictor::URLRequestSummary; | |
25 | |
18 | 26 |
19 namespace { | 27 namespace { |
20 | 28 |
21 // Enum for measuring statistics pertaining to observed request, responses and | 29 // Enum for measuring statistics pertaining to observed request, responses and |
22 // redirects. | 30 // redirects. |
23 enum RequestStats { | 31 enum RequestStats { |
24 REQUEST_STATS_TOTAL_RESPONSES = 0, | 32 REQUEST_STATS_TOTAL_RESPONSES = 0, |
25 REQUEST_STATS_TOTAL_PROCESSED_RESPONSES = 1, | 33 REQUEST_STATS_TOTAL_PROCESSED_RESPONSES = 1, |
26 REQUEST_STATS_NO_RESOURCE_REQUEST_INFO = 2, // Not recorded (never was). | 34 REQUEST_STATS_NO_RESOURCE_REQUEST_INFO = 2, // Not recorded (never was). |
27 REQUEST_STATS_NO_RENDER_FRAME_ID_FROM_REQUEST_INFO = 3, // Not recorded. | 35 REQUEST_STATS_NO_RENDER_FRAME_ID_FROM_REQUEST_INFO = 3, // Not recorded. |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
62 } | 70 } |
63 | 71 |
64 ResourcePrefetchPredictorObserver::~ResourcePrefetchPredictorObserver() { | 72 ResourcePrefetchPredictorObserver::~ResourcePrefetchPredictorObserver() { |
65 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI) || | 73 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI) || |
66 BrowserThread::CurrentlyOn(BrowserThread::IO)); | 74 BrowserThread::CurrentlyOn(BrowserThread::IO)); |
67 } | 75 } |
68 | 76 |
69 void ResourcePrefetchPredictorObserver::OnRequestStarted( | 77 void ResourcePrefetchPredictorObserver::OnRequestStarted( |
70 net::URLRequest* request, | 78 net::URLRequest* request, |
71 content::ResourceType resource_type, | 79 content::ResourceType resource_type, |
72 int child_id, | 80 const content::ResourceRequestInfo::WebContentsGetter& |
73 int frame_id) { | 81 web_contents_getter) { |
74 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 82 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
75 | 83 |
76 if (resource_type == content::RESOURCE_TYPE_MAIN_FRAME) | 84 if (resource_type == content::RESOURCE_TYPE_MAIN_FRAME) |
77 ReportMainFrameRequestStats(MAIN_FRAME_REQUEST_STATS_TOTAL_REQUESTS); | 85 ReportMainFrameRequestStats(MAIN_FRAME_REQUEST_STATS_TOTAL_REQUESTS); |
78 | 86 |
79 if (!ResourcePrefetchPredictor::ShouldRecordRequest(request, resource_type)) | 87 if (!ResourcePrefetchPredictor::ShouldRecordRequest(request, resource_type)) |
80 return; | 88 return; |
81 | 89 |
82 ResourcePrefetchPredictor::URLRequestSummary summary; | 90 auto summary = base::MakeUnique<URLRequestSummary>(); |
83 summary.navigation_id.render_process_id = child_id; | 91 summary->resource_url = request->original_url(); |
84 summary.navigation_id.render_frame_id = frame_id; | 92 summary->resource_type = resource_type; |
85 summary.navigation_id.main_frame_url = request->first_party_for_cookies(); | |
86 summary.navigation_id.creation_time = request->creation_time(); | |
87 summary.resource_url = request->original_url(); | |
88 summary.resource_type = resource_type; | |
89 | 93 |
90 BrowserThread::PostTask( | 94 BrowserThread::PostTask( |
91 BrowserThread::UI, | 95 BrowserThread::UI, FROM_HERE, |
92 FROM_HERE, | 96 base::Bind(&ResourcePrefetchPredictorObserver::OnRequestStartedOnUIThread, |
93 base::Bind(&ResourcePrefetchPredictor::RecordURLRequest, | 97 base::Unretained(this), base::Passed(std::move(summary)), |
94 predictor_, | 98 web_contents_getter, request->first_party_for_cookies(), |
ahemery
2016/12/06 14:31:11
The new parameters that get passed all the way to
| |
95 summary)); | 99 request->creation_time())); |
96 | 100 |
97 if (resource_type == content::RESOURCE_TYPE_MAIN_FRAME) | 101 if (resource_type == content::RESOURCE_TYPE_MAIN_FRAME) |
98 ReportMainFrameRequestStats(MAIN_FRAME_REQUEST_STATS_PROCESSED_REQUESTS); | 102 ReportMainFrameRequestStats(MAIN_FRAME_REQUEST_STATS_PROCESSED_REQUESTS); |
99 } | 103 } |
100 | 104 |
101 void ResourcePrefetchPredictorObserver::OnRequestRedirected( | 105 void ResourcePrefetchPredictorObserver::OnRequestRedirected( |
106 net::URLRequest* request, | |
102 const GURL& redirect_url, | 107 const GURL& redirect_url, |
103 net::URLRequest* request) { | 108 const content::ResourceRequestInfo::WebContentsGetter& |
109 web_contents_getter) { | |
104 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 110 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
105 | 111 |
106 const content::ResourceRequestInfo* request_info = | 112 const content::ResourceRequestInfo* request_info = |
107 content::ResourceRequestInfo::ForRequest(request); | 113 content::ResourceRequestInfo::ForRequest(request); |
108 if (request_info && | 114 if (request_info && |
109 request_info->GetResourceType() == content::RESOURCE_TYPE_MAIN_FRAME) { | 115 request_info->GetResourceType() == content::RESOURCE_TYPE_MAIN_FRAME) { |
110 ReportMainFrameRequestStats(MAIN_FRAME_REQUEST_STATS_TOTAL_REDIRECTS); | 116 ReportMainFrameRequestStats(MAIN_FRAME_REQUEST_STATS_TOTAL_REDIRECTS); |
111 } | 117 } |
112 | 118 |
113 if (!ResourcePrefetchPredictor::ShouldRecordRedirect(request)) | 119 if (!ResourcePrefetchPredictor::ShouldRecordRedirect(request)) |
114 return; | 120 return; |
115 | 121 |
116 ResourcePrefetchPredictor::URLRequestSummary summary; | 122 auto summary = base::MakeUnique<URLRequestSummary>(); |
117 if (!ResourcePrefetchPredictor::URLRequestSummary::SummarizeResponse( | 123 if (!ResourcePrefetchPredictor::URLRequestSummary::SummarizeResponse( |
118 *request, &summary)) { | 124 *request, summary.get())) { |
119 return; | 125 return; |
120 } | 126 } |
127 summary->redirect_url = redirect_url; | |
ahemery
2016/12/06 14:31:11
Moved from above, no modification
| |
121 | 128 |
122 summary.redirect_url = redirect_url; | |
123 | 129 |
124 BrowserThread::PostTask( | 130 BrowserThread::PostTask( |
125 BrowserThread::UI, | 131 BrowserThread::UI, FROM_HERE, |
126 FROM_HERE, | 132 base::Bind( |
127 base::Bind(&ResourcePrefetchPredictor::RecordURLRedirect, | 133 &ResourcePrefetchPredictorObserver::OnRequestRedirectedOnUIThread, |
128 predictor_, | 134 base::Unretained(this), base::Passed(std::move(summary)), |
129 summary)); | 135 web_contents_getter, request->first_party_for_cookies(), |
136 request->creation_time())); | |
130 | 137 |
131 if (request_info && | 138 if (request_info && |
132 request_info->GetResourceType() == content::RESOURCE_TYPE_MAIN_FRAME) { | 139 request_info->GetResourceType() == content::RESOURCE_TYPE_MAIN_FRAME) { |
133 ReportMainFrameRequestStats(MAIN_FRAME_REQUEST_STATS_PROCESSED_REDIRECTS); | 140 ReportMainFrameRequestStats(MAIN_FRAME_REQUEST_STATS_PROCESSED_REDIRECTS); |
134 } | 141 } |
135 } | 142 } |
136 | 143 |
137 void ResourcePrefetchPredictorObserver::OnResponseStarted( | 144 void ResourcePrefetchPredictorObserver::OnResponseStarted( |
138 net::URLRequest* request) { | 145 net::URLRequest* request, |
146 const content::ResourceRequestInfo::WebContentsGetter& | |
147 web_contents_getter) { | |
139 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 148 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
140 | 149 |
141 ReportRequestStats(REQUEST_STATS_TOTAL_RESPONSES); | 150 ReportRequestStats(REQUEST_STATS_TOTAL_RESPONSES); |
142 | 151 |
143 const content::ResourceRequestInfo* request_info = | 152 const content::ResourceRequestInfo* request_info = |
144 content::ResourceRequestInfo::ForRequest(request); | 153 content::ResourceRequestInfo::ForRequest(request); |
145 if (request_info && | 154 if (request_info && |
146 request_info->GetResourceType() == content::RESOURCE_TYPE_MAIN_FRAME) { | 155 request_info->GetResourceType() == content::RESOURCE_TYPE_MAIN_FRAME) { |
147 ReportMainFrameRequestStats(MAIN_FRAME_REQUEST_STATS_TOTAL_RESPONSES); | 156 ReportMainFrameRequestStats(MAIN_FRAME_REQUEST_STATS_TOTAL_RESPONSES); |
148 } | 157 } |
149 | 158 |
150 if (!ResourcePrefetchPredictor::ShouldRecordResponse(request)) | 159 if (!ResourcePrefetchPredictor::ShouldRecordResponse(request)) |
151 return; | 160 return; |
152 ResourcePrefetchPredictor::URLRequestSummary summary; | 161 auto summary = base::MakeUnique<URLRequestSummary>(); |
153 if (!ResourcePrefetchPredictor::URLRequestSummary::SummarizeResponse( | 162 if (!ResourcePrefetchPredictor::URLRequestSummary::SummarizeResponse( |
154 *request, &summary)) { | 163 *request, summary.get())) { |
155 return; | 164 return; |
156 } | 165 } |
157 | 166 |
158 BrowserThread::PostTask( | 167 BrowserThread::PostTask( |
159 BrowserThread::UI, | 168 BrowserThread::UI, FROM_HERE, |
160 FROM_HERE, | 169 base::Bind( |
161 base::Bind(&ResourcePrefetchPredictor::RecordURLResponse, | 170 &ResourcePrefetchPredictorObserver::OnResponseStartedOnUIThread, |
162 predictor_, | 171 base::Unretained(this), base::Passed(std::move(summary)), |
163 summary)); | 172 web_contents_getter, request->first_party_for_cookies(), |
173 request->creation_time())); | |
164 | 174 |
165 ReportRequestStats(REQUEST_STATS_TOTAL_PROCESSED_RESPONSES); | 175 ReportRequestStats(REQUEST_STATS_TOTAL_PROCESSED_RESPONSES); |
166 if (request_info && | 176 if (request_info && |
167 request_info->GetResourceType() == content::RESOURCE_TYPE_MAIN_FRAME) { | 177 request_info->GetResourceType() == content::RESOURCE_TYPE_MAIN_FRAME) { |
168 ReportMainFrameRequestStats(MAIN_FRAME_REQUEST_STATS_PROCESSED_RESPONSES); | 178 ReportMainFrameRequestStats(MAIN_FRAME_REQUEST_STATS_PROCESSED_RESPONSES); |
169 } | 179 } |
170 } | 180 } |
171 | 181 |
182 void ResourcePrefetchPredictorObserver::OnRequestStartedOnUIThread( | |
183 std::unique_ptr<URLRequestSummary> summary, | |
184 const content::ResourceRequestInfo::WebContentsGetter& | |
185 web_contents_getter, | |
186 const GURL& main_frame_url, | |
187 const base::TimeTicks& creation_time) const { | |
188 DCHECK_CURRENTLY_ON(BrowserThread::UI); | |
189 if (!TryToFillNavigationID(&summary->navigation_id, | |
ahemery
2016/12/06 14:31:11
Now checking for failure. Returning without any fu
| |
190 web_contents_getter, main_frame_url, | |
191 creation_time)) | |
192 return; | |
193 predictor_->RecordURLRequest(*summary); | |
194 } | |
195 | |
196 void ResourcePrefetchPredictorObserver::OnRequestRedirectedOnUIThread( | |
197 std::unique_ptr<URLRequestSummary> summary, | |
198 const content::ResourceRequestInfo::WebContentsGetter& | |
199 web_contents_getter, | |
200 const GURL& main_frame_url, | |
201 const base::TimeTicks& creation_time) const { | |
202 DCHECK_CURRENTLY_ON(BrowserThread::UI); | |
203 if (!TryToFillNavigationID(&summary->navigation_id, | |
204 web_contents_getter, main_frame_url, | |
205 creation_time)) | |
206 return; | |
207 predictor_->RecordURLRedirect(*summary); | |
208 } | |
209 | |
210 void ResourcePrefetchPredictorObserver::OnResponseStartedOnUIThread( | |
211 std::unique_ptr<URLRequestSummary> summary, | |
212 const content::ResourceRequestInfo::WebContentsGetter& | |
213 web_contents_getter, | |
214 const GURL& main_frame_url, | |
215 const base::TimeTicks& creation_time) const { | |
216 DCHECK_CURRENTLY_ON(BrowserThread::UI); | |
217 if (!TryToFillNavigationID(&summary->navigation_id, | |
218 web_contents_getter, main_frame_url, | |
219 creation_time)) | |
220 return; | |
221 predictor_->RecordURLResponse(*summary); | |
222 } | |
223 | |
224 bool ResourcePrefetchPredictorObserver::TryToFillNavigationID( | |
ahemery
2016/12/06 14:31:11
Modified name to make it clearer that it can fail
| |
225 predictors::NavigationID* navigation_id, | |
226 const content::ResourceRequestInfo::WebContentsGetter& | |
227 web_contents_getter, | |
228 const GURL& main_frame_url, | |
229 const base::TimeTicks& creation_time) const { | |
230 DCHECK_CURRENTLY_ON(BrowserThread::UI); | |
231 content::WebContents* web_contents = web_contents_getter.Run(); | |
232 if (!web_contents) | |
233 return false; | |
234 *navigation_id = predictors::NavigationID(web_contents, | |
ahemery
2016/12/06 14:31:11
Using a direct constructor here
| |
235 main_frame_url, | |
236 creation_time); | |
237 return true; | |
238 } | |
239 | |
172 } // namespace chrome_browser_net | 240 } // namespace chrome_browser_net |
OLD | NEW |