| 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 <string> | 7 #include <string> |
| 8 | 8 |
| 9 #include "base/metrics/histogram.h" | 9 #include "base/metrics/histogram.h" |
| 10 #include "content/public/browser/browser_thread.h" | 10 #include "content/public/browser/browser_thread.h" |
| 11 #include "content/public/browser/render_frame_host.h" | 11 #include "content/public/browser/render_frame_host.h" |
| 12 #include "content/public/browser/resource_request_info.h" | 12 #include "content/public/browser/resource_request_info.h" |
| 13 #include "net/url_request/url_request.h" | 13 #include "net/url_request/url_request.h" |
| 14 #include "url/gurl.h" | 14 #include "url/gurl.h" |
| 15 | 15 |
| 16 using content::BrowserThread; | 16 using content::BrowserThread; |
| 17 using predictors::ResourcePrefetchPredictor; | 17 using predictors::ResourcePrefetchPredictor; |
| 18 | 18 |
| 19 namespace { | 19 namespace { |
| 20 | 20 |
| 21 // Enum for measuring statistics pertaining to observed request, responses and | 21 // Enum for measuring statistics pertaining to observed request, responses and |
| 22 // redirects. | 22 // redirects. |
| 23 enum RequestStats { | 23 enum RequestStats { |
| 24 REQUEST_STATS_TOTAL_RESPONSES = 0, | 24 REQUEST_STATS_TOTAL_RESPONSES = 0, |
| 25 REQUEST_STATS_TOTAL_PROCESSED_RESPONSES = 1, | 25 REQUEST_STATS_TOTAL_PROCESSED_RESPONSES = 1, |
| 26 REQUEST_STATS_NO_RESOURCE_REQUEST_INFO = 2, | 26 REQUEST_STATS_NO_RESOURCE_REQUEST_INFO = 2, // Not recorded (never was). |
| 27 REQUEST_STATS_NO_RENDER_FRAME_ID_FROM_REQUEST_INFO = 3, | 27 REQUEST_STATS_NO_RENDER_FRAME_ID_FROM_REQUEST_INFO = 3, // Not recorded. |
| 28 REQUEST_STATS_MAX = 4, | 28 REQUEST_STATS_MAX = 4, |
| 29 }; | 29 }; |
| 30 | 30 |
| 31 // Specific to main frame requests. | 31 // Specific to main frame requests. |
| 32 enum MainFrameRequestStats { | 32 enum MainFrameRequestStats { |
| 33 MAIN_FRAME_REQUEST_STATS_TOTAL_REQUESTS = 0, | 33 MAIN_FRAME_REQUEST_STATS_TOTAL_REQUESTS = 0, |
| 34 MAIN_FRAME_REQUEST_STATS_PROCESSED_REQUESTS = 1, | 34 MAIN_FRAME_REQUEST_STATS_PROCESSED_REQUESTS = 1, |
| 35 MAIN_FRAME_REQUEST_STATS_TOTAL_REDIRECTS = 2, | 35 MAIN_FRAME_REQUEST_STATS_TOTAL_REDIRECTS = 2, |
| 36 MAIN_FRAME_REQUEST_STATS_PROCESSED_REDIRECTS = 3, | 36 MAIN_FRAME_REQUEST_STATS_PROCESSED_REDIRECTS = 3, |
| 37 MAIN_FRAME_REQUEST_STATS_TOTAL_RESPONSES = 4, | 37 MAIN_FRAME_REQUEST_STATS_TOTAL_RESPONSES = 4, |
| 38 MAIN_FRAME_REQUEST_STATS_PROCESSED_RESPONSES = 5, | 38 MAIN_FRAME_REQUEST_STATS_PROCESSED_RESPONSES = 5, |
| 39 MAIN_FRAME_REQUEST_STATS_MAX = 6, | 39 MAIN_FRAME_REQUEST_STATS_MAX = 6, |
| 40 }; | 40 }; |
| 41 | 41 |
| 42 void ReportRequestStats(RequestStats stat) { | 42 void ReportRequestStats(RequestStats stat) { |
| 43 UMA_HISTOGRAM_ENUMERATION("ResourcePrefetchPredictor.RequestStats", | 43 UMA_HISTOGRAM_ENUMERATION("ResourcePrefetchPredictor.RequestStats", |
| 44 stat, | 44 stat, |
| 45 REQUEST_STATS_MAX); | 45 REQUEST_STATS_MAX); |
| 46 } | 46 } |
| 47 | 47 |
| 48 void ReportMainFrameRequestStats(MainFrameRequestStats stat) { | 48 void ReportMainFrameRequestStats(MainFrameRequestStats stat) { |
| 49 UMA_HISTOGRAM_ENUMERATION("ResourcePrefetchPredictor.MainFrameRequestStats", | 49 UMA_HISTOGRAM_ENUMERATION("ResourcePrefetchPredictor.MainFrameRequestStats", |
| 50 stat, | 50 stat, |
| 51 MAIN_FRAME_REQUEST_STATS_MAX); | 51 MAIN_FRAME_REQUEST_STATS_MAX); |
| 52 } | 52 } |
| 53 | 53 |
| 54 bool SummarizeResponse(net::URLRequest* request, | |
| 55 ResourcePrefetchPredictor::URLRequestSummary* summary) { | |
| 56 const content::ResourceRequestInfo* info = | |
| 57 content::ResourceRequestInfo::ForRequest(request); | |
| 58 if (!info) { | |
| 59 ReportRequestStats(REQUEST_STATS_NO_RESOURCE_REQUEST_INFO); | |
| 60 return false; | |
| 61 } | |
| 62 | |
| 63 int render_process_id, render_frame_id; | |
| 64 if (!info->GetAssociatedRenderFrame(&render_process_id, &render_frame_id)) { | |
| 65 ReportRequestStats(REQUEST_STATS_NO_RENDER_FRAME_ID_FROM_REQUEST_INFO); | |
| 66 return false; | |
| 67 } | |
| 68 | |
| 69 summary->navigation_id.render_process_id = render_process_id; | |
| 70 summary->navigation_id.render_frame_id = render_frame_id; | |
| 71 summary->navigation_id.main_frame_url = request->first_party_for_cookies(); | |
| 72 summary->navigation_id.creation_time = request->creation_time(); | |
| 73 summary->resource_url = request->original_url(); | |
| 74 summary->resource_type = info->GetResourceType(); | |
| 75 summary->priority = request->priority(); | |
| 76 request->GetMimeType(&summary->mime_type); | |
| 77 summary->was_cached = request->was_cached(); | |
| 78 | |
| 79 // Use the mime_type to determine the resource type for subresources since | |
| 80 // types such as PREFETCH, SUB_RESOURCE, etc are not useful. | |
| 81 if (summary->resource_type != content::RESOURCE_TYPE_MAIN_FRAME) { | |
| 82 summary->resource_type = | |
| 83 ResourcePrefetchPredictor::GetResourceTypeFromMimeType( | |
| 84 summary->mime_type, | |
| 85 summary->resource_type); | |
| 86 } | |
| 87 return true; | |
| 88 } | |
| 89 | |
| 90 } // namespace | 54 } // namespace |
| 91 | 55 |
| 92 namespace chrome_browser_net { | 56 namespace chrome_browser_net { |
| 93 | 57 |
| 94 ResourcePrefetchPredictorObserver::ResourcePrefetchPredictorObserver( | 58 ResourcePrefetchPredictorObserver::ResourcePrefetchPredictorObserver( |
| 95 ResourcePrefetchPredictor* predictor) | 59 ResourcePrefetchPredictor* predictor) |
| 96 : predictor_(predictor->AsWeakPtr()) { | 60 : predictor_(predictor->AsWeakPtr()) { |
| 97 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 61 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 98 } | 62 } |
| 99 | 63 |
| (...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 143 content::ResourceRequestInfo::ForRequest(request); | 107 content::ResourceRequestInfo::ForRequest(request); |
| 144 if (request_info && | 108 if (request_info && |
| 145 request_info->GetResourceType() == content::RESOURCE_TYPE_MAIN_FRAME) { | 109 request_info->GetResourceType() == content::RESOURCE_TYPE_MAIN_FRAME) { |
| 146 ReportMainFrameRequestStats(MAIN_FRAME_REQUEST_STATS_TOTAL_REDIRECTS); | 110 ReportMainFrameRequestStats(MAIN_FRAME_REQUEST_STATS_TOTAL_REDIRECTS); |
| 147 } | 111 } |
| 148 | 112 |
| 149 if (!ResourcePrefetchPredictor::ShouldRecordRedirect(request)) | 113 if (!ResourcePrefetchPredictor::ShouldRecordRedirect(request)) |
| 150 return; | 114 return; |
| 151 | 115 |
| 152 ResourcePrefetchPredictor::URLRequestSummary summary; | 116 ResourcePrefetchPredictor::URLRequestSummary summary; |
| 153 if (!SummarizeResponse(request, &summary)) | 117 if (!ResourcePrefetchPredictor::URLRequestSummary::SummarizeResponse( |
| 118 request, &summary)) { |
| 154 return; | 119 return; |
| 120 } |
| 155 | 121 |
| 156 summary.redirect_url = redirect_url; | 122 summary.redirect_url = redirect_url; |
| 157 | 123 |
| 158 BrowserThread::PostTask( | 124 BrowserThread::PostTask( |
| 159 BrowserThread::UI, | 125 BrowserThread::UI, |
| 160 FROM_HERE, | 126 FROM_HERE, |
| 161 base::Bind(&ResourcePrefetchPredictor::RecordURLRedirect, | 127 base::Bind(&ResourcePrefetchPredictor::RecordURLRedirect, |
| 162 predictor_, | 128 predictor_, |
| 163 summary)); | 129 summary)); |
| 164 | 130 |
| (...skipping 12 matching lines...) Expand all Loading... |
| 177 const content::ResourceRequestInfo* request_info = | 143 const content::ResourceRequestInfo* request_info = |
| 178 content::ResourceRequestInfo::ForRequest(request); | 144 content::ResourceRequestInfo::ForRequest(request); |
| 179 if (request_info && | 145 if (request_info && |
| 180 request_info->GetResourceType() == content::RESOURCE_TYPE_MAIN_FRAME) { | 146 request_info->GetResourceType() == content::RESOURCE_TYPE_MAIN_FRAME) { |
| 181 ReportMainFrameRequestStats(MAIN_FRAME_REQUEST_STATS_TOTAL_RESPONSES); | 147 ReportMainFrameRequestStats(MAIN_FRAME_REQUEST_STATS_TOTAL_RESPONSES); |
| 182 } | 148 } |
| 183 | 149 |
| 184 if (!ResourcePrefetchPredictor::ShouldRecordResponse(request)) | 150 if (!ResourcePrefetchPredictor::ShouldRecordResponse(request)) |
| 185 return; | 151 return; |
| 186 ResourcePrefetchPredictor::URLRequestSummary summary; | 152 ResourcePrefetchPredictor::URLRequestSummary summary; |
| 187 if (!SummarizeResponse(request, &summary)) | 153 if (!ResourcePrefetchPredictor::URLRequestSummary::SummarizeResponse( |
| 154 request, &summary)) { |
| 188 return; | 155 return; |
| 156 } |
| 189 | 157 |
| 190 BrowserThread::PostTask( | 158 BrowserThread::PostTask( |
| 191 BrowserThread::UI, | 159 BrowserThread::UI, |
| 192 FROM_HERE, | 160 FROM_HERE, |
| 193 base::Bind(&ResourcePrefetchPredictor::RecordURLResponse, | 161 base::Bind(&ResourcePrefetchPredictor::RecordURLResponse, |
| 194 predictor_, | 162 predictor_, |
| 195 summary)); | 163 summary)); |
| 196 | 164 |
| 197 ReportRequestStats(REQUEST_STATS_TOTAL_PROCESSED_RESPONSES); | 165 ReportRequestStats(REQUEST_STATS_TOTAL_PROCESSED_RESPONSES); |
| 198 if (request_info && | 166 if (request_info && |
| 199 request_info->GetResourceType() == content::RESOURCE_TYPE_MAIN_FRAME) { | 167 request_info->GetResourceType() == content::RESOURCE_TYPE_MAIN_FRAME) { |
| 200 ReportMainFrameRequestStats(MAIN_FRAME_REQUEST_STATS_PROCESSED_RESPONSES); | 168 ReportMainFrameRequestStats(MAIN_FRAME_REQUEST_STATS_PROCESSED_RESPONSES); |
| 201 } | 169 } |
| 202 } | 170 } |
| 203 | 171 |
| 204 } // namespace chrome_browser_net | 172 } // namespace chrome_browser_net |
| OLD | NEW |