Chromium Code Reviews| 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> | |
| 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" | |
|
Benoit L
2016/12/06 16:09:22
nit: Is this header needed?
ahemery
2016/12/07 09:17:05
Removed
| |
| 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" | |
|
Benoit L
2016/12/06 16:09:22
nit: Would a simple forward declaration be enough
ahemery
2016/12/07 09:17:05
Moved to forward declaration
| |
| 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 = | |
| 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 16 matching lines...) Expand all Loading... | |
| 44 stat, | 52 stat, |
| 45 REQUEST_STATS_MAX); | 53 REQUEST_STATS_MAX); |
| 46 } | 54 } |
| 47 | 55 |
| 48 void ReportMainFrameRequestStats(MainFrameRequestStats stat) { | 56 void ReportMainFrameRequestStats(MainFrameRequestStats stat) { |
| 49 UMA_HISTOGRAM_ENUMERATION("ResourcePrefetchPredictor.MainFrameRequestStats", | 57 UMA_HISTOGRAM_ENUMERATION("ResourcePrefetchPredictor.MainFrameRequestStats", |
| 50 stat, | 58 stat, |
| 51 MAIN_FRAME_REQUEST_STATS_MAX); | 59 MAIN_FRAME_REQUEST_STATS_MAX); |
| 52 } | 60 } |
| 53 | 61 |
| 62 bool TryToFillNavigationID( | |
| 63 predictors::NavigationID* navigation_id, | |
| 64 const content::ResourceRequestInfo::WebContentsGetter& | |
| 65 web_contents_getter, | |
| 66 const GURL& main_frame_url, | |
| 67 const base::TimeTicks& creation_time) { | |
| 68 DCHECK_CURRENTLY_ON(BrowserThread::UI); | |
| 69 content::WebContents* web_contents = web_contents_getter.Run(); | |
| 70 if (!web_contents) | |
| 71 return false; | |
| 72 *navigation_id = predictors::NavigationID(web_contents, | |
|
alexilin
2016/12/06 15:51:54
Would you run git cl format?
ahemery
2016/12/07 09:17:05
Done
| |
| 73 main_frame_url, | |
| 74 creation_time); | |
| 75 return true; | |
| 76 } | |
| 77 | |
| 54 } // namespace | 78 } // namespace |
| 55 | 79 |
| 56 namespace chrome_browser_net { | 80 namespace chrome_browser_net { |
| 57 | 81 |
| 58 ResourcePrefetchPredictorObserver::ResourcePrefetchPredictorObserver( | 82 ResourcePrefetchPredictorObserver::ResourcePrefetchPredictorObserver( |
| 59 ResourcePrefetchPredictor* predictor) | 83 ResourcePrefetchPredictor* predictor) |
| 60 : predictor_(predictor->AsWeakPtr()) { | 84 : predictor_(predictor->AsWeakPtr()) { |
| 61 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 85 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 62 } | 86 } |
| 63 | 87 |
| 64 ResourcePrefetchPredictorObserver::~ResourcePrefetchPredictorObserver() { | 88 ResourcePrefetchPredictorObserver::~ResourcePrefetchPredictorObserver() { |
| 65 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI) || | 89 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI) || |
| 66 BrowserThread::CurrentlyOn(BrowserThread::IO)); | 90 BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 67 } | 91 } |
| 68 | 92 |
| 69 void ResourcePrefetchPredictorObserver::OnRequestStarted( | 93 void ResourcePrefetchPredictorObserver::OnRequestStarted( |
| 70 net::URLRequest* request, | 94 net::URLRequest* request, |
| 71 content::ResourceType resource_type, | 95 content::ResourceType resource_type, |
| 72 int child_id, | 96 const content::ResourceRequestInfo::WebContentsGetter& |
| 73 int frame_id) { | 97 web_contents_getter) { |
| 74 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 98 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 75 | 99 |
| 76 if (resource_type == content::RESOURCE_TYPE_MAIN_FRAME) | 100 if (resource_type == content::RESOURCE_TYPE_MAIN_FRAME) |
| 77 ReportMainFrameRequestStats(MAIN_FRAME_REQUEST_STATS_TOTAL_REQUESTS); | 101 ReportMainFrameRequestStats(MAIN_FRAME_REQUEST_STATS_TOTAL_REQUESTS); |
| 78 | 102 |
| 79 if (!ResourcePrefetchPredictor::ShouldRecordRequest(request, resource_type)) | 103 if (!ResourcePrefetchPredictor::ShouldRecordRequest(request, resource_type)) |
| 80 return; | 104 return; |
| 81 | 105 |
| 82 ResourcePrefetchPredictor::URLRequestSummary summary; | 106 auto summary = base::MakeUnique<URLRequestSummary>(); |
| 83 summary.navigation_id.render_process_id = child_id; | 107 summary->resource_url = request->original_url(); |
| 84 summary.navigation_id.render_frame_id = frame_id; | 108 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 | 109 |
| 90 BrowserThread::PostTask( | 110 BrowserThread::PostTask( |
| 91 BrowserThread::UI, | 111 BrowserThread::UI, FROM_HERE, |
| 92 FROM_HERE, | 112 base::Bind(&ResourcePrefetchPredictorObserver::OnRequestStartedOnUIThread, |
| 93 base::Bind(&ResourcePrefetchPredictor::RecordURLRequest, | 113 base::Unretained(this), base::Passed(std::move(summary)), |
| 94 predictor_, | 114 web_contents_getter, request->first_party_for_cookies(), |
| 95 summary)); | 115 request->creation_time())); |
| 96 | 116 |
| 97 if (resource_type == content::RESOURCE_TYPE_MAIN_FRAME) | 117 if (resource_type == content::RESOURCE_TYPE_MAIN_FRAME) |
| 98 ReportMainFrameRequestStats(MAIN_FRAME_REQUEST_STATS_PROCESSED_REQUESTS); | 118 ReportMainFrameRequestStats(MAIN_FRAME_REQUEST_STATS_PROCESSED_REQUESTS); |
| 99 } | 119 } |
| 100 | 120 |
| 101 void ResourcePrefetchPredictorObserver::OnRequestRedirected( | 121 void ResourcePrefetchPredictorObserver::OnRequestRedirected( |
| 122 net::URLRequest* request, | |
| 102 const GURL& redirect_url, | 123 const GURL& redirect_url, |
| 103 net::URLRequest* request) { | 124 const content::ResourceRequestInfo::WebContentsGetter& |
| 125 web_contents_getter) { | |
| 104 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 126 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 105 | 127 |
| 106 const content::ResourceRequestInfo* request_info = | 128 const content::ResourceRequestInfo* request_info = |
| 107 content::ResourceRequestInfo::ForRequest(request); | 129 content::ResourceRequestInfo::ForRequest(request); |
| 108 if (request_info && | 130 if (request_info && |
| 109 request_info->GetResourceType() == content::RESOURCE_TYPE_MAIN_FRAME) { | 131 request_info->GetResourceType() == content::RESOURCE_TYPE_MAIN_FRAME) { |
| 110 ReportMainFrameRequestStats(MAIN_FRAME_REQUEST_STATS_TOTAL_REDIRECTS); | 132 ReportMainFrameRequestStats(MAIN_FRAME_REQUEST_STATS_TOTAL_REDIRECTS); |
| 111 } | 133 } |
| 112 | 134 |
| 113 if (!ResourcePrefetchPredictor::ShouldRecordRedirect(request)) | 135 if (!ResourcePrefetchPredictor::ShouldRecordRedirect(request)) |
| 114 return; | 136 return; |
| 115 | 137 |
| 116 ResourcePrefetchPredictor::URLRequestSummary summary; | 138 auto summary = base::MakeUnique<URLRequestSummary>(); |
| 117 if (!ResourcePrefetchPredictor::URLRequestSummary::SummarizeResponse( | 139 if (!ResourcePrefetchPredictor::URLRequestSummary::SummarizeResponse( |
| 118 *request, &summary)) { | 140 *request, summary.get())) { |
| 119 return; | 141 return; |
| 120 } | 142 } |
| 143 summary->redirect_url = redirect_url; | |
| 121 | 144 |
| 122 summary.redirect_url = redirect_url; | |
| 123 | 145 |
| 124 BrowserThread::PostTask( | 146 BrowserThread::PostTask( |
| 125 BrowserThread::UI, | 147 BrowserThread::UI, FROM_HERE, |
| 126 FROM_HERE, | 148 base::Bind( |
| 127 base::Bind(&ResourcePrefetchPredictor::RecordURLRedirect, | 149 &ResourcePrefetchPredictorObserver::OnRequestRedirectedOnUIThread, |
| 128 predictor_, | 150 base::Unretained(this), base::Passed(std::move(summary)), |
| 129 summary)); | 151 web_contents_getter, request->first_party_for_cookies(), |
| 152 request->creation_time())); | |
| 130 | 153 |
| 131 if (request_info && | 154 if (request_info && |
| 132 request_info->GetResourceType() == content::RESOURCE_TYPE_MAIN_FRAME) { | 155 request_info->GetResourceType() == content::RESOURCE_TYPE_MAIN_FRAME) { |
| 133 ReportMainFrameRequestStats(MAIN_FRAME_REQUEST_STATS_PROCESSED_REDIRECTS); | 156 ReportMainFrameRequestStats(MAIN_FRAME_REQUEST_STATS_PROCESSED_REDIRECTS); |
| 134 } | 157 } |
| 135 } | 158 } |
| 136 | 159 |
| 137 void ResourcePrefetchPredictorObserver::OnResponseStarted( | 160 void ResourcePrefetchPredictorObserver::OnResponseStarted( |
| 138 net::URLRequest* request) { | 161 net::URLRequest* request, |
| 162 const content::ResourceRequestInfo::WebContentsGetter& | |
| 163 web_contents_getter) { | |
| 139 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 164 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 140 | 165 |
| 141 ReportRequestStats(REQUEST_STATS_TOTAL_RESPONSES); | 166 ReportRequestStats(REQUEST_STATS_TOTAL_RESPONSES); |
| 142 | 167 |
| 143 const content::ResourceRequestInfo* request_info = | 168 const content::ResourceRequestInfo* request_info = |
| 144 content::ResourceRequestInfo::ForRequest(request); | 169 content::ResourceRequestInfo::ForRequest(request); |
| 145 if (request_info && | 170 if (request_info && |
| 146 request_info->GetResourceType() == content::RESOURCE_TYPE_MAIN_FRAME) { | 171 request_info->GetResourceType() == content::RESOURCE_TYPE_MAIN_FRAME) { |
| 147 ReportMainFrameRequestStats(MAIN_FRAME_REQUEST_STATS_TOTAL_RESPONSES); | 172 ReportMainFrameRequestStats(MAIN_FRAME_REQUEST_STATS_TOTAL_RESPONSES); |
| 148 } | 173 } |
| 149 | 174 |
| 150 if (!ResourcePrefetchPredictor::ShouldRecordResponse(request)) | 175 if (!ResourcePrefetchPredictor::ShouldRecordResponse(request)) |
| 151 return; | 176 return; |
| 152 ResourcePrefetchPredictor::URLRequestSummary summary; | 177 auto summary = base::MakeUnique<URLRequestSummary>(); |
| 153 if (!ResourcePrefetchPredictor::URLRequestSummary::SummarizeResponse( | 178 if (!ResourcePrefetchPredictor::URLRequestSummary::SummarizeResponse( |
| 154 *request, &summary)) { | 179 *request, summary.get())) { |
| 155 return; | 180 return; |
| 156 } | 181 } |
| 157 | 182 |
| 158 BrowserThread::PostTask( | 183 BrowserThread::PostTask( |
| 159 BrowserThread::UI, | 184 BrowserThread::UI, FROM_HERE, |
| 160 FROM_HERE, | 185 base::Bind( |
| 161 base::Bind(&ResourcePrefetchPredictor::RecordURLResponse, | 186 &ResourcePrefetchPredictorObserver::OnResponseStartedOnUIThread, |
|
Benoit L
2016/12/06 16:09:22
Question: is it necessary to fully qualify the nam
| |
| 162 predictor_, | 187 base::Unretained(this), base::Passed(std::move(summary)), |
| 163 summary)); | 188 web_contents_getter, request->first_party_for_cookies(), |
| 189 request->creation_time())); | |
| 164 | 190 |
| 165 ReportRequestStats(REQUEST_STATS_TOTAL_PROCESSED_RESPONSES); | 191 ReportRequestStats(REQUEST_STATS_TOTAL_PROCESSED_RESPONSES); |
| 166 if (request_info && | 192 if (request_info && |
| 167 request_info->GetResourceType() == content::RESOURCE_TYPE_MAIN_FRAME) { | 193 request_info->GetResourceType() == content::RESOURCE_TYPE_MAIN_FRAME) { |
| 168 ReportMainFrameRequestStats(MAIN_FRAME_REQUEST_STATS_PROCESSED_RESPONSES); | 194 ReportMainFrameRequestStats(MAIN_FRAME_REQUEST_STATS_PROCESSED_RESPONSES); |
| 169 } | 195 } |
| 170 } | 196 } |
| 171 | 197 |
| 198 void ResourcePrefetchPredictorObserver::OnRequestStartedOnUIThread( | |
| 199 std::unique_ptr<URLRequestSummary> summary, | |
| 200 const content::ResourceRequestInfo::WebContentsGetter& | |
| 201 web_contents_getter, | |
| 202 const GURL& main_frame_url, | |
| 203 const base::TimeTicks& creation_time) const { | |
| 204 DCHECK_CURRENTLY_ON(BrowserThread::UI); | |
| 205 if (!TryToFillNavigationID(&summary->navigation_id, | |
| 206 web_contents_getter, main_frame_url, | |
| 207 creation_time)) | |
|
alexilin
2016/12/06 15:51:54
nit: git cl format here too
+ There is no strict r
ahemery
2016/12/07 09:17:05
Modified to prefered format
| |
| 208 return; | |
| 209 predictor_->RecordURLRequest(*summary); | |
| 210 } | |
| 211 | |
| 212 void ResourcePrefetchPredictorObserver::OnRequestRedirectedOnUIThread( | |
| 213 std::unique_ptr<URLRequestSummary> summary, | |
| 214 const content::ResourceRequestInfo::WebContentsGetter& | |
| 215 web_contents_getter, | |
| 216 const GURL& main_frame_url, | |
| 217 const base::TimeTicks& creation_time) const { | |
| 218 DCHECK_CURRENTLY_ON(BrowserThread::UI); | |
| 219 if (!TryToFillNavigationID(&summary->navigation_id, | |
|
alexilin
2016/12/06 15:51:54
ditto
| |
| 220 web_contents_getter, main_frame_url, | |
| 221 creation_time)) | |
| 222 return; | |
| 223 predictor_->RecordURLRedirect(*summary); | |
| 224 } | |
| 225 | |
| 226 void ResourcePrefetchPredictorObserver::OnResponseStartedOnUIThread( | |
| 227 std::unique_ptr<URLRequestSummary> summary, | |
| 228 const content::ResourceRequestInfo::WebContentsGetter& | |
| 229 web_contents_getter, | |
| 230 const GURL& main_frame_url, | |
| 231 const base::TimeTicks& creation_time) const { | |
| 232 DCHECK_CURRENTLY_ON(BrowserThread::UI); | |
| 233 if (!TryToFillNavigationID(&summary->navigation_id, | |
|
alexilin
2016/12/06 15:51:54
ditto
| |
| 234 web_contents_getter, main_frame_url, | |
| 235 creation_time)) | |
| 236 return; | |
| 237 predictor_->RecordURLResponse(*summary); | |
| 238 } | |
| 239 | |
| 172 } // namespace chrome_browser_net | 240 } // namespace chrome_browser_net |
| OLD | NEW |