| 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" | |
| 12 #include "content/public/browser/resource_request_info.h" | 14 #include "content/public/browser/resource_request_info.h" |
| 13 #include "net/url_request/url_request.h" | 15 #include "net/url_request/url_request.h" |
| 14 #include "url/gurl.h" | 16 #include "url/gurl.h" |
| 15 | 17 |
| 18 namespace content { |
| 19 class WebContents; |
| 20 } |
| 21 |
| 16 using content::BrowserThread; | 22 using content::BrowserThread; |
| 17 using predictors::ResourcePrefetchPredictor; | 23 using predictors::ResourcePrefetchPredictor; |
| 24 using URLRequestSummary = |
| 25 predictors::ResourcePrefetchPredictor::URLRequestSummary; |
| 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& web_contents_getter, |
| 65 const GURL& main_frame_url, |
| 66 const base::TimeTicks& creation_time) { |
| 67 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 68 content::WebContents* web_contents = web_contents_getter.Run(); |
| 69 if (!web_contents) |
| 70 return false; |
| 71 *navigation_id = |
| 72 predictors::NavigationID(web_contents, main_frame_url, creation_time); |
| 73 return true; |
| 74 } |
| 75 |
| 54 } // namespace | 76 } // namespace |
| 55 | 77 |
| 56 namespace chrome_browser_net { | 78 namespace chrome_browser_net { |
| 57 | 79 |
| 58 ResourcePrefetchPredictorObserver::ResourcePrefetchPredictorObserver( | 80 ResourcePrefetchPredictorObserver::ResourcePrefetchPredictorObserver( |
| 59 ResourcePrefetchPredictor* predictor) | 81 ResourcePrefetchPredictor* predictor) |
| 60 : predictor_(predictor->AsWeakPtr()) { | 82 : predictor_(predictor->AsWeakPtr()) { |
| 61 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 83 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 62 } | 84 } |
| 63 | 85 |
| 64 ResourcePrefetchPredictorObserver::~ResourcePrefetchPredictorObserver() { | 86 ResourcePrefetchPredictorObserver::~ResourcePrefetchPredictorObserver() { |
| 65 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI) || | 87 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI) || |
| 66 BrowserThread::CurrentlyOn(BrowserThread::IO)); | 88 BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 67 } | 89 } |
| 68 | 90 |
| 69 void ResourcePrefetchPredictorObserver::OnRequestStarted( | 91 void ResourcePrefetchPredictorObserver::OnRequestStarted( |
| 70 net::URLRequest* request, | 92 net::URLRequest* request, |
| 71 content::ResourceType resource_type, | 93 content::ResourceType resource_type, |
| 72 int child_id, | 94 const content::ResourceRequestInfo::WebContentsGetter& |
| 73 int frame_id) { | 95 web_contents_getter) { |
| 74 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 96 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 75 | 97 |
| 76 if (resource_type == content::RESOURCE_TYPE_MAIN_FRAME) | 98 if (resource_type == content::RESOURCE_TYPE_MAIN_FRAME) |
| 77 ReportMainFrameRequestStats(MAIN_FRAME_REQUEST_STATS_TOTAL_REQUESTS); | 99 ReportMainFrameRequestStats(MAIN_FRAME_REQUEST_STATS_TOTAL_REQUESTS); |
| 78 | 100 |
| 79 if (!ResourcePrefetchPredictor::ShouldRecordRequest(request, resource_type)) | 101 if (!ResourcePrefetchPredictor::ShouldRecordRequest(request, resource_type)) |
| 80 return; | 102 return; |
| 81 | 103 |
| 82 ResourcePrefetchPredictor::URLRequestSummary summary; | 104 auto summary = base::MakeUnique<URLRequestSummary>(); |
| 83 summary.navigation_id.render_process_id = child_id; | 105 summary->resource_url = request->original_url(); |
| 84 summary.navigation_id.render_frame_id = frame_id; | 106 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 | 107 |
| 90 BrowserThread::PostTask( | 108 BrowserThread::PostTask( |
| 91 BrowserThread::UI, | 109 BrowserThread::UI, FROM_HERE, |
| 92 FROM_HERE, | 110 base::Bind(&ResourcePrefetchPredictorObserver::OnRequestStartedOnUIThread, |
| 93 base::Bind(&ResourcePrefetchPredictor::RecordURLRequest, | 111 base::Unretained(this), base::Passed(std::move(summary)), |
| 94 predictor_, | 112 web_contents_getter, request->first_party_for_cookies(), |
| 95 summary)); | 113 request->creation_time())); |
| 96 | 114 |
| 97 if (resource_type == content::RESOURCE_TYPE_MAIN_FRAME) | 115 if (resource_type == content::RESOURCE_TYPE_MAIN_FRAME) |
| 98 ReportMainFrameRequestStats(MAIN_FRAME_REQUEST_STATS_PROCESSED_REQUESTS); | 116 ReportMainFrameRequestStats(MAIN_FRAME_REQUEST_STATS_PROCESSED_REQUESTS); |
| 99 } | 117 } |
| 100 | 118 |
| 101 void ResourcePrefetchPredictorObserver::OnRequestRedirected( | 119 void ResourcePrefetchPredictorObserver::OnRequestRedirected( |
| 120 net::URLRequest* request, |
| 102 const GURL& redirect_url, | 121 const GURL& redirect_url, |
| 103 net::URLRequest* request) { | 122 const content::ResourceRequestInfo::WebContentsGetter& |
| 123 web_contents_getter) { |
| 104 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 124 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 105 | 125 |
| 106 const content::ResourceRequestInfo* request_info = | 126 const content::ResourceRequestInfo* request_info = |
| 107 content::ResourceRequestInfo::ForRequest(request); | 127 content::ResourceRequestInfo::ForRequest(request); |
| 108 if (request_info && | 128 if (request_info && |
| 109 request_info->GetResourceType() == content::RESOURCE_TYPE_MAIN_FRAME) { | 129 request_info->GetResourceType() == content::RESOURCE_TYPE_MAIN_FRAME) { |
| 110 ReportMainFrameRequestStats(MAIN_FRAME_REQUEST_STATS_TOTAL_REDIRECTS); | 130 ReportMainFrameRequestStats(MAIN_FRAME_REQUEST_STATS_TOTAL_REDIRECTS); |
| 111 } | 131 } |
| 112 | 132 |
| 113 if (!ResourcePrefetchPredictor::ShouldRecordRedirect(request)) | 133 if (!ResourcePrefetchPredictor::ShouldRecordRedirect(request)) |
| 114 return; | 134 return; |
| 115 | 135 |
| 116 ResourcePrefetchPredictor::URLRequestSummary summary; | 136 auto summary = base::MakeUnique<URLRequestSummary>(); |
| 117 if (!ResourcePrefetchPredictor::URLRequestSummary::SummarizeResponse( | 137 if (!ResourcePrefetchPredictor::URLRequestSummary::SummarizeResponse( |
| 118 *request, &summary)) { | 138 *request, summary.get())) { |
| 119 return; | 139 return; |
| 120 } | 140 } |
| 121 | 141 summary->redirect_url = redirect_url; |
| 122 summary.redirect_url = redirect_url; | |
| 123 | 142 |
| 124 BrowserThread::PostTask( | 143 BrowserThread::PostTask( |
| 125 BrowserThread::UI, | 144 BrowserThread::UI, FROM_HERE, |
| 126 FROM_HERE, | 145 base::Bind( |
| 127 base::Bind(&ResourcePrefetchPredictor::RecordURLRedirect, | 146 &ResourcePrefetchPredictorObserver::OnRequestRedirectedOnUIThread, |
| 128 predictor_, | 147 base::Unretained(this), base::Passed(std::move(summary)), |
| 129 summary)); | 148 web_contents_getter, request->first_party_for_cookies(), |
| 149 request->creation_time())); |
| 130 | 150 |
| 131 if (request_info && | 151 if (request_info && |
| 132 request_info->GetResourceType() == content::RESOURCE_TYPE_MAIN_FRAME) { | 152 request_info->GetResourceType() == content::RESOURCE_TYPE_MAIN_FRAME) { |
| 133 ReportMainFrameRequestStats(MAIN_FRAME_REQUEST_STATS_PROCESSED_REDIRECTS); | 153 ReportMainFrameRequestStats(MAIN_FRAME_REQUEST_STATS_PROCESSED_REDIRECTS); |
| 134 } | 154 } |
| 135 } | 155 } |
| 136 | 156 |
| 137 void ResourcePrefetchPredictorObserver::OnResponseStarted( | 157 void ResourcePrefetchPredictorObserver::OnResponseStarted( |
| 138 net::URLRequest* request) { | 158 net::URLRequest* request, |
| 159 const content::ResourceRequestInfo::WebContentsGetter& |
| 160 web_contents_getter) { |
| 139 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 161 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 140 | 162 |
| 141 ReportRequestStats(REQUEST_STATS_TOTAL_RESPONSES); | 163 ReportRequestStats(REQUEST_STATS_TOTAL_RESPONSES); |
| 142 | 164 |
| 143 const content::ResourceRequestInfo* request_info = | 165 const content::ResourceRequestInfo* request_info = |
| 144 content::ResourceRequestInfo::ForRequest(request); | 166 content::ResourceRequestInfo::ForRequest(request); |
| 145 if (request_info && | 167 if (request_info && |
| 146 request_info->GetResourceType() == content::RESOURCE_TYPE_MAIN_FRAME) { | 168 request_info->GetResourceType() == content::RESOURCE_TYPE_MAIN_FRAME) { |
| 147 ReportMainFrameRequestStats(MAIN_FRAME_REQUEST_STATS_TOTAL_RESPONSES); | 169 ReportMainFrameRequestStats(MAIN_FRAME_REQUEST_STATS_TOTAL_RESPONSES); |
| 148 } | 170 } |
| 149 | 171 |
| 150 if (!ResourcePrefetchPredictor::ShouldRecordResponse(request)) | 172 if (!ResourcePrefetchPredictor::ShouldRecordResponse(request)) |
| 151 return; | 173 return; |
| 152 ResourcePrefetchPredictor::URLRequestSummary summary; | 174 auto summary = base::MakeUnique<URLRequestSummary>(); |
| 153 if (!ResourcePrefetchPredictor::URLRequestSummary::SummarizeResponse( | 175 if (!ResourcePrefetchPredictor::URLRequestSummary::SummarizeResponse( |
| 154 *request, &summary)) { | 176 *request, summary.get())) { |
| 155 return; | 177 return; |
| 156 } | 178 } |
| 157 | 179 |
| 158 BrowserThread::PostTask( | 180 BrowserThread::PostTask( |
| 159 BrowserThread::UI, | 181 BrowserThread::UI, FROM_HERE, |
| 160 FROM_HERE, | 182 base::Bind( |
| 161 base::Bind(&ResourcePrefetchPredictor::RecordURLResponse, | 183 &ResourcePrefetchPredictorObserver::OnResponseStartedOnUIThread, |
| 162 predictor_, | 184 base::Unretained(this), base::Passed(std::move(summary)), |
| 163 summary)); | 185 web_contents_getter, request->first_party_for_cookies(), |
| 186 request->creation_time())); |
| 164 | 187 |
| 165 ReportRequestStats(REQUEST_STATS_TOTAL_PROCESSED_RESPONSES); | 188 ReportRequestStats(REQUEST_STATS_TOTAL_PROCESSED_RESPONSES); |
| 166 if (request_info && | 189 if (request_info && |
| 167 request_info->GetResourceType() == content::RESOURCE_TYPE_MAIN_FRAME) { | 190 request_info->GetResourceType() == content::RESOURCE_TYPE_MAIN_FRAME) { |
| 168 ReportMainFrameRequestStats(MAIN_FRAME_REQUEST_STATS_PROCESSED_RESPONSES); | 191 ReportMainFrameRequestStats(MAIN_FRAME_REQUEST_STATS_PROCESSED_RESPONSES); |
| 169 } | 192 } |
| 170 } | 193 } |
| 171 | 194 |
| 195 void ResourcePrefetchPredictorObserver::OnRequestStartedOnUIThread( |
| 196 std::unique_ptr<URLRequestSummary> summary, |
| 197 const content::ResourceRequestInfo::WebContentsGetter& web_contents_getter, |
| 198 const GURL& main_frame_url, |
| 199 const base::TimeTicks& creation_time) const { |
| 200 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 201 if (!TryToFillNavigationID(&summary->navigation_id, web_contents_getter, |
| 202 main_frame_url, creation_time)) { |
| 203 return; |
| 204 } |
| 205 predictor_->RecordURLRequest(*summary); |
| 206 } |
| 207 |
| 208 void ResourcePrefetchPredictorObserver::OnRequestRedirectedOnUIThread( |
| 209 std::unique_ptr<URLRequestSummary> summary, |
| 210 const content::ResourceRequestInfo::WebContentsGetter& web_contents_getter, |
| 211 const GURL& main_frame_url, |
| 212 const base::TimeTicks& creation_time) const { |
| 213 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 214 if (!TryToFillNavigationID(&summary->navigation_id, web_contents_getter, |
| 215 main_frame_url, creation_time)) { |
| 216 return; |
| 217 } |
| 218 predictor_->RecordURLRedirect(*summary); |
| 219 } |
| 220 |
| 221 void ResourcePrefetchPredictorObserver::OnResponseStartedOnUIThread( |
| 222 std::unique_ptr<URLRequestSummary> summary, |
| 223 const content::ResourceRequestInfo::WebContentsGetter& web_contents_getter, |
| 224 const GURL& main_frame_url, |
| 225 const base::TimeTicks& creation_time) const { |
| 226 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 227 if (!TryToFillNavigationID(&summary->navigation_id, web_contents_getter, |
| 228 main_frame_url, creation_time)) { |
| 229 return; |
| 230 } |
| 231 predictor_->RecordURLResponse(*summary); |
| 232 } |
| 233 |
| 172 } // namespace chrome_browser_net | 234 } // namespace chrome_browser_net |
| OLD | NEW |