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> | |
ahemery
2016/12/05 09:38:44
for unique_ptr
| |
7 #include <string> | 8 #include <string> |
8 | 9 |
10 #include "base/memory/ptr_util.h" | |
ahemery
2016/12/05 09:38:45
For MakeUnique and base::Passed
| |
9 #include "base/metrics/histogram_macros.h" | 11 #include "base/metrics/histogram_macros.h" |
10 #include "content/public/browser/browser_thread.h" | 12 #include "content/public/browser/browser_thread.h" |
11 #include "content/public/browser/render_frame_host.h" | 13 #include "content/public/browser/render_frame_host.h" |
14 #include "content/public/browser/render_process_host.h" | |
12 #include "content/public/browser/resource_request_info.h" | 15 #include "content/public/browser/resource_request_info.h" |
16 #include "content/public/browser/web_contents.h" | |
13 #include "net/url_request/url_request.h" | 17 #include "net/url_request/url_request.h" |
14 #include "url/gurl.h" | 18 #include "url/gurl.h" |
15 | 19 |
16 using content::BrowserThread; | 20 using content::BrowserThread; |
17 using predictors::ResourcePrefetchPredictor; | 21 using predictors::ResourcePrefetchPredictor; |
18 | 22 |
19 namespace { | 23 namespace { |
20 | 24 |
21 // Enum for measuring statistics pertaining to observed request, responses and | 25 // Enum for measuring statistics pertaining to observed request, responses and |
22 // redirects. | 26 // redirects. |
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
62 } | 66 } |
63 | 67 |
64 ResourcePrefetchPredictorObserver::~ResourcePrefetchPredictorObserver() { | 68 ResourcePrefetchPredictorObserver::~ResourcePrefetchPredictorObserver() { |
65 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI) || | 69 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI) || |
66 BrowserThread::CurrentlyOn(BrowserThread::IO)); | 70 BrowserThread::CurrentlyOn(BrowserThread::IO)); |
67 } | 71 } |
68 | 72 |
69 void ResourcePrefetchPredictorObserver::OnRequestStarted( | 73 void ResourcePrefetchPredictorObserver::OnRequestStarted( |
70 net::URLRequest* request, | 74 net::URLRequest* request, |
71 content::ResourceType resource_type, | 75 content::ResourceType resource_type, |
72 int child_id, | 76 const content::ResourceRequestInfo::WebContentsGetter& |
73 int frame_id) { | 77 web_contents_getter) { |
74 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 78 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
75 | 79 |
76 if (resource_type == content::RESOURCE_TYPE_MAIN_FRAME) | 80 if (resource_type == content::RESOURCE_TYPE_MAIN_FRAME) |
77 ReportMainFrameRequestStats(MAIN_FRAME_REQUEST_STATS_TOTAL_REQUESTS); | 81 ReportMainFrameRequestStats(MAIN_FRAME_REQUEST_STATS_TOTAL_REQUESTS); |
78 | 82 |
79 if (!ResourcePrefetchPredictor::ShouldRecordRequest(request, resource_type)) | 83 if (!ResourcePrefetchPredictor::ShouldRecordRequest(request, resource_type)) |
80 return; | 84 return; |
81 | 85 |
82 ResourcePrefetchPredictor::URLRequestSummary summary; | 86 auto summary = base::MakeUnique<URLRequestSummary>(); |
ahemery
2016/12/05 09:38:44
Now using a pointer as we are passing it to a diff
| |
83 summary.navigation_id.render_process_id = child_id; | 87 summary->navigation_id.main_frame_url = request->first_party_for_cookies(); |
84 summary.navigation_id.render_frame_id = frame_id; | 88 summary->navigation_id.creation_time = request->creation_time(); |
85 summary.navigation_id.main_frame_url = request->first_party_for_cookies(); | 89 summary->resource_url = request->original_url(); |
86 summary.navigation_id.creation_time = request->creation_time(); | 90 summary->resource_type = resource_type; |
87 summary.resource_url = request->original_url(); | |
88 summary.resource_type = resource_type; | |
89 | 91 |
90 BrowserThread::PostTask( | 92 BrowserThread::PostTask( |
91 BrowserThread::UI, | 93 BrowserThread::UI, FROM_HERE, |
92 FROM_HERE, | 94 base::Bind(&ResourcePrefetchPredictorObserver::OnRequestStartedOnUIThread, |
93 base::Bind(&ResourcePrefetchPredictor::RecordURLRequest, | 95 base::Unretained(this), base::Passed(std::move(summary)), |
94 predictor_, | 96 web_contents_getter)); |
95 summary)); | |
96 | 97 |
97 if (resource_type == content::RESOURCE_TYPE_MAIN_FRAME) | 98 if (resource_type == content::RESOURCE_TYPE_MAIN_FRAME) |
98 ReportMainFrameRequestStats(MAIN_FRAME_REQUEST_STATS_PROCESSED_REQUESTS); | 99 ReportMainFrameRequestStats(MAIN_FRAME_REQUEST_STATS_PROCESSED_REQUESTS); |
99 } | 100 } |
ahemery
2016/12/06 14:31:11
Private methods moved at the end of the file
alexilin
2016/12/06 15:51:54
Thanks!
Could you reply directly under initial com
| |
100 | 101 |
102 void ResourcePrefetchPredictorObserver::OnRequestStartedOnUIThread( | |
alexilin
2016/12/05 13:20:00
nit: Could you put all definitions of private func
| |
103 std::unique_ptr<URLRequestSummary> summary, | |
104 const content::ResourceRequestInfo::WebContentsGetter& | |
105 web_contents_getter) const { | |
106 DCHECK_CURRENTLY_ON(BrowserThread::UI); | |
107 RetrieveNavigationID(summary->navigation_id, web_contents_getter); | |
108 predictor_->RecordURLRequest(*summary); | |
109 } | |
110 | |
101 void ResourcePrefetchPredictorObserver::OnRequestRedirected( | 111 void ResourcePrefetchPredictorObserver::OnRequestRedirected( |
112 net::URLRequest* request, | |
102 const GURL& redirect_url, | 113 const GURL& redirect_url, |
103 net::URLRequest* request) { | 114 const content::ResourceRequestInfo::WebContentsGetter& |
115 web_contents_getter) { | |
104 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 116 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
105 | 117 |
106 const content::ResourceRequestInfo* request_info = | 118 const content::ResourceRequestInfo* request_info = |
107 content::ResourceRequestInfo::ForRequest(request); | 119 content::ResourceRequestInfo::ForRequest(request); |
108 if (request_info && | 120 if (request_info && |
109 request_info->GetResourceType() == content::RESOURCE_TYPE_MAIN_FRAME) { | 121 request_info->GetResourceType() == content::RESOURCE_TYPE_MAIN_FRAME) { |
110 ReportMainFrameRequestStats(MAIN_FRAME_REQUEST_STATS_TOTAL_REDIRECTS); | 122 ReportMainFrameRequestStats(MAIN_FRAME_REQUEST_STATS_TOTAL_REDIRECTS); |
111 } | 123 } |
112 | 124 |
113 if (!ResourcePrefetchPredictor::ShouldRecordRedirect(request)) | 125 if (!ResourcePrefetchPredictor::ShouldRecordRedirect(request)) |
114 return; | 126 return; |
115 | 127 |
116 ResourcePrefetchPredictor::URLRequestSummary summary; | 128 auto summary = base::MakeUnique<URLRequestSummary>(); |
129 summary->navigation_id.main_frame_url = request->first_party_for_cookies(); | |
ahemery
2016/12/05 09:38:45
main_frame_url and creation_time is not done in Su
| |
130 summary->navigation_id.creation_time = request->creation_time(); | |
131 summary->redirect_url = redirect_url; | |
117 if (!ResourcePrefetchPredictor::URLRequestSummary::SummarizeResponse( | 132 if (!ResourcePrefetchPredictor::URLRequestSummary::SummarizeResponse( |
118 *request, &summary)) { | 133 *request, summary.get())) { |
119 return; | 134 return; |
120 } | 135 } |
121 | 136 |
122 summary.redirect_url = redirect_url; | |
123 | |
124 BrowserThread::PostTask( | 137 BrowserThread::PostTask( |
125 BrowserThread::UI, | 138 BrowserThread::UI, FROM_HERE, |
126 FROM_HERE, | 139 base::Bind( |
127 base::Bind(&ResourcePrefetchPredictor::RecordURLRedirect, | 140 &ResourcePrefetchPredictorObserver::OnRequestRedirectedOnUIThread, |
128 predictor_, | 141 base::Unretained(this), base::Passed(std::move(summary)), |
129 summary)); | 142 web_contents_getter)); |
130 | 143 |
131 if (request_info && | 144 if (request_info && |
132 request_info->GetResourceType() == content::RESOURCE_TYPE_MAIN_FRAME) { | 145 request_info->GetResourceType() == content::RESOURCE_TYPE_MAIN_FRAME) { |
133 ReportMainFrameRequestStats(MAIN_FRAME_REQUEST_STATS_PROCESSED_REDIRECTS); | 146 ReportMainFrameRequestStats(MAIN_FRAME_REQUEST_STATS_PROCESSED_REDIRECTS); |
134 } | 147 } |
135 } | 148 } |
136 | 149 |
150 void ResourcePrefetchPredictorObserver::OnRequestRedirectedOnUIThread( | |
151 std::unique_ptr<URLRequestSummary> summary, | |
152 const content::ResourceRequestInfo::WebContentsGetter& web_contents_getter) | |
153 const { | |
154 DCHECK_CURRENTLY_ON(BrowserThread::UI); | |
155 RetrieveNavigationID(summary->navigation_id, web_contents_getter); | |
156 predictor_->RecordURLRedirect(*summary); | |
157 } | |
158 | |
137 void ResourcePrefetchPredictorObserver::OnResponseStarted( | 159 void ResourcePrefetchPredictorObserver::OnResponseStarted( |
138 net::URLRequest* request) { | 160 net::URLRequest* request, |
161 const content::ResourceRequestInfo::WebContentsGetter& | |
162 web_contents_getter) { | |
139 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 163 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
140 | 164 |
141 ReportRequestStats(REQUEST_STATS_TOTAL_RESPONSES); | 165 ReportRequestStats(REQUEST_STATS_TOTAL_RESPONSES); |
142 | 166 |
143 const content::ResourceRequestInfo* request_info = | 167 const content::ResourceRequestInfo* request_info = |
144 content::ResourceRequestInfo::ForRequest(request); | 168 content::ResourceRequestInfo::ForRequest(request); |
145 if (request_info && | 169 if (request_info && |
146 request_info->GetResourceType() == content::RESOURCE_TYPE_MAIN_FRAME) { | 170 request_info->GetResourceType() == content::RESOURCE_TYPE_MAIN_FRAME) { |
147 ReportMainFrameRequestStats(MAIN_FRAME_REQUEST_STATS_TOTAL_RESPONSES); | 171 ReportMainFrameRequestStats(MAIN_FRAME_REQUEST_STATS_TOTAL_RESPONSES); |
148 } | 172 } |
149 | 173 |
150 if (!ResourcePrefetchPredictor::ShouldRecordResponse(request)) | 174 if (!ResourcePrefetchPredictor::ShouldRecordResponse(request)) |
151 return; | 175 return; |
152 ResourcePrefetchPredictor::URLRequestSummary summary; | 176 auto summary = base::MakeUnique<URLRequestSummary>(); |
177 summary->navigation_id.main_frame_url = request->first_party_for_cookies(); | |
178 summary->navigation_id.creation_time = request->creation_time(); | |
153 if (!ResourcePrefetchPredictor::URLRequestSummary::SummarizeResponse( | 179 if (!ResourcePrefetchPredictor::URLRequestSummary::SummarizeResponse( |
154 *request, &summary)) { | 180 *request, summary.get())) { |
155 return; | 181 return; |
156 } | 182 } |
157 | 183 |
158 BrowserThread::PostTask( | 184 BrowserThread::PostTask( |
159 BrowserThread::UI, | 185 BrowserThread::UI, FROM_HERE, |
160 FROM_HERE, | 186 base::Bind( |
161 base::Bind(&ResourcePrefetchPredictor::RecordURLResponse, | 187 &ResourcePrefetchPredictorObserver::OnResponseStartedOnUIThread, |
162 predictor_, | 188 base::Unretained(this), base::Passed(std::move(summary)), |
163 summary)); | 189 web_contents_getter)); |
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::OnResponseStartedOnUIThread( | |
199 std::unique_ptr<URLRequestSummary> summary, | |
200 const content::ResourceRequestInfo::WebContentsGetter& | |
201 web_contents_getter) const { | |
202 DCHECK_CURRENTLY_ON(BrowserThread::UI); | |
203 RetrieveNavigationID(summary->navigation_id, web_contents_getter); | |
204 predictor_->RecordURLResponse(*summary); | |
205 } | |
206 | |
207 void ResourcePrefetchPredictorObserver::RetrieveNavigationID( | |
Benoit L
2016/12/05 13:25:56
Actually, considering the remark below, this funct
| |
208 predictors::NavigationID& navigation_id, | |
alexilin
2016/12/05 13:20:00
Pass an object by pointer if you want to change it
Benoit L
2016/12/05 13:25:56
We don't use non-const references in Chrome.
See h
| |
209 const content::ResourceRequestInfo::WebContentsGetter& | |
210 web_contents_getter) const { | |
211 DCHECK_CURRENTLY_ON(BrowserThread::UI); | |
212 content::WebContents* web_contents = web_contents_getter.Run(); | |
213 DCHECK(web_contents); | |
alexilin
2016/12/05 13:20:00
Based on comment to GetWebContentsGetterForRequest
Benoit L
2016/12/05 13:25:56
A DCHECK() is not correct here, as the returned We
| |
214 navigation_id.render_process_id = | |
alexilin
2016/12/05 13:20:00
nit:
I'm wondering if we can eliminate the mess wi
Benoit L
2016/12/05 13:25:56
The partial initialization of NavigationID here is
| |
215 web_contents->GetRenderProcessHost()->GetID(); | |
216 navigation_id.render_frame_id = | |
217 web_contents->GetMainFrame()->GetRoutingID(); | |
218 } | |
219 | |
172 } // namespace chrome_browser_net | 220 } // namespace chrome_browser_net |
OLD | NEW |