OLD | NEW |
---|---|
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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 "components/page_load_metrics/browser/metrics_web_contents_observer.h" | 5 #include "components/page_load_metrics/browser/metrics_web_contents_observer.h" |
6 | 6 |
7 #include "base/logging.h" | 7 #include "base/logging.h" |
8 #include "base/metrics/histogram.h" | 8 #include "base/metrics/histogram.h" |
9 #include "components/page_load_metrics/common/page_load_metrics_messages.h" | 9 #include "components/page_load_metrics/common/page_load_metrics_messages.h" |
10 #include "components/page_load_metrics/common/page_load_timing.h" | 10 #include "components/page_load_metrics/common/page_load_timing.h" |
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
57 } | 57 } |
58 | 58 |
59 } // namespace | 59 } // namespace |
60 | 60 |
61 #define PAGE_LOAD_HISTOGRAM(name, sample) \ | 61 #define PAGE_LOAD_HISTOGRAM(name, sample) \ |
62 UMA_HISTOGRAM_CUSTOM_TIMES(name, sample, \ | 62 UMA_HISTOGRAM_CUSTOM_TIMES(name, sample, \ |
63 base::TimeDelta::FromMilliseconds(10), \ | 63 base::TimeDelta::FromMilliseconds(10), \ |
64 base::TimeDelta::FromMinutes(10), 100) | 64 base::TimeDelta::FromMinutes(10), 100) |
65 | 65 |
66 PageLoadTracker::PageLoadTracker(bool in_foreground) | 66 PageLoadTracker::PageLoadTracker(bool in_foreground) |
67 : has_commit_(false), started_in_foreground_(in_foreground) { | 67 : has_commit_(false), started_in_foreground_(in_foreground) {} |
68 RecordEvent(PAGE_LOAD_STARTED); | |
69 } | |
70 | 68 |
71 PageLoadTracker::~PageLoadTracker() { | 69 PageLoadTracker::~PageLoadTracker() { |
72 // Even a load that failed a provisional load should log | |
73 // that it aborted before first layout. | |
74 if (timing_.first_layout.is_zero()) | |
75 RecordEvent(PAGE_LOAD_ABORTED_BEFORE_FIRST_LAYOUT); | |
76 | |
77 if (has_commit_) | 70 if (has_commit_) |
78 RecordTimingHistograms(); | 71 RecordTimingHistograms(); |
79 } | 72 } |
80 | 73 |
81 void PageLoadTracker::WebContentsHidden() { | 74 void PageLoadTracker::WebContentsHidden() { |
82 // Only log the first time we background in a given page load. | 75 // Only log the first time we background in a given page load. |
83 if (background_time_.is_null()) { | 76 if (started_in_foreground_ && background_time_.is_null()) |
84 background_time_ = base::TimeTicks::Now(); | 77 background_time_ = base::TimeTicks::Now(); |
85 } | 78 } |
79 | |
80 void PageLoadTracker::WebContentsShown() { | |
81 // Only log the first time we foreground in a given page load. | |
82 // Don't log foreground time if we started foregrounded. | |
83 if (!started_in_foreground_ && foreground_time_.is_null()) | |
84 foreground_time_ = base::TimeTicks::Now(); | |
86 } | 85 } |
87 | 86 |
88 void PageLoadTracker::Commit() { | 87 void PageLoadTracker::Commit() { |
89 has_commit_ = true; | 88 has_commit_ = true; |
89 // This is one special case where we aren't checking if we backgrounded NOW, | |
90 // but at the start of the navigation. | |
Bryan McQuade
2015/10/12 16:39:22
let's add a little more detail to explain why we c
Charlie Harrison
2015/10/12 18:39:51
Done.
| |
91 RecordCommittedEvent(COMMITTED_LOAD_STARTED, !started_in_foreground_); | |
90 } | 92 } |
91 | 93 |
92 bool PageLoadTracker::UpdateTiming(const PageLoadTiming& timing) { | 94 bool PageLoadTracker::UpdateTiming(const PageLoadTiming& new_timing) { |
93 // Throw away IPCs that are not relevant to the current navigation. | 95 // Throw away IPCs that are not relevant to the current navigation. |
94 if (!timing_.navigation_start.is_null() && | 96 // Two timing structures cannot refer to the same navigation if they indicate |
95 timing_.navigation_start != timing.navigation_start) { | 97 // that a navigation started at different times, so a new timing struct with a |
96 // TODO(csharrison) uma log a counter here | 98 // different start time from an earlier struct is considered invalid. |
97 return false; | 99 bool valid_timing_descendent = |
98 } | 100 timing_.navigation_start.is_null() || |
99 if (IsValidPageLoadTiming(timing)) { | 101 timing_.navigation_start == new_timing.navigation_start; |
100 timing_ = timing; | 102 if (IsValidPageLoadTiming(new_timing) && valid_timing_descendent) { |
103 timing_ = new_timing; | |
101 return true; | 104 return true; |
102 } | 105 } |
Bryan McQuade
2015/10/12 16:39:23
can we add an else along with a new error enum ent
Charlie Harrison
2015/10/12 18:39:51
We do this one frame up the stack, per Randy's rev
| |
103 return false; | 106 return false; |
104 } | 107 } |
105 | 108 |
109 bool PageLoadTracker::HasBackgrounded() { | |
110 return !(started_in_foreground_ && background_time_.is_null()); | |
111 } | |
112 | |
106 void PageLoadTracker::RecordTimingHistograms() { | 113 void PageLoadTracker::RecordTimingHistograms() { |
107 DCHECK(has_commit_); | 114 DCHECK(has_commit_); |
108 // This method is similar to how blink converts TimeTicks to epoch time. | 115 // This method is similar to how blink converts TimeTicks to epoch time. |
109 // There may be slight inaccuracies due to inter-process timestamps, but | 116 // There may be slight inaccuracies due to inter-process timestamps, but |
110 // this solution is the best we have right now. | 117 // this solution is the best we have right now. |
111 base::TimeDelta background_delta; | 118 base::TimeDelta background_delta; |
112 if (started_in_foreground_) { | 119 if (started_in_foreground_) { |
113 background_delta = background_time_.is_null() | 120 background_delta = background_time_.is_null() |
114 ? base::TimeDelta::Max() | 121 ? base::TimeDelta::Max() |
115 : WallTimeFromTimeTicks(background_time_) - timing_.navigation_start; | 122 : WallTimeFromTimeTicks(background_time_) - timing_.navigation_start; |
(...skipping 12 matching lines...) Expand all Loading... | |
128 } | 135 } |
129 if (!timing_.load_event_start.is_zero()) { | 136 if (!timing_.load_event_start.is_zero()) { |
130 if (timing_.load_event_start < background_delta) { | 137 if (timing_.load_event_start < background_delta) { |
131 PAGE_LOAD_HISTOGRAM("PageLoad.Timing2.NavigationToLoadEventFired", | 138 PAGE_LOAD_HISTOGRAM("PageLoad.Timing2.NavigationToLoadEventFired", |
132 timing_.load_event_start); | 139 timing_.load_event_start); |
133 } else { | 140 } else { |
134 PAGE_LOAD_HISTOGRAM("PageLoad.Timing2.NavigationToLoadEventFired.BG", | 141 PAGE_LOAD_HISTOGRAM("PageLoad.Timing2.NavigationToLoadEventFired.BG", |
135 timing_.load_event_start); | 142 timing_.load_event_start); |
136 } | 143 } |
137 } | 144 } |
138 if (!timing_.first_layout.is_zero()) { | 145 if (timing_.first_layout.is_zero()) { |
146 RecordCommittedEvent(COMMITTED_LOAD_ABORTED_BEFORE_FIRST_LAYOUT, | |
147 HasBackgrounded()); | |
148 } else { | |
139 if (timing_.first_layout < background_delta) { | 149 if (timing_.first_layout < background_delta) { |
140 PAGE_LOAD_HISTOGRAM("PageLoad.Timing2.NavigationToFirstLayout", | 150 PAGE_LOAD_HISTOGRAM("PageLoad.Timing2.NavigationToFirstLayout", |
141 timing_.first_layout); | 151 timing_.first_layout); |
142 RecordEvent(PAGE_LOAD_SUCCESSFUL_FIRST_LAYOUT_FOREGROUND); | 152 RecordCommittedEvent(COMMITTED_LOAD_SUCCESSFUL_FIRST_LAYOUT, false); |
143 } else { | 153 } else { |
144 PAGE_LOAD_HISTOGRAM("PageLoad.Timing2.NavigationToFirstLayout.BG", | 154 PAGE_LOAD_HISTOGRAM("PageLoad.Timing2.NavigationToFirstLayout.BG", |
145 timing_.first_layout); | 155 timing_.first_layout); |
146 RecordEvent(PAGE_LOAD_SUCCESSFUL_FIRST_LAYOUT_BACKGROUND); | 156 RecordCommittedEvent(COMMITTED_LOAD_SUCCESSFUL_FIRST_LAYOUT, true); |
147 } | 157 } |
148 } | 158 } |
149 if (!timing_.first_text_paint.is_zero()) { | 159 if (!timing_.first_text_paint.is_zero()) { |
150 if (timing_.first_text_paint < background_delta) { | 160 if (timing_.first_text_paint < background_delta) { |
151 PAGE_LOAD_HISTOGRAM("PageLoad.Timing2.NavigationToFirstTextPaint", | 161 PAGE_LOAD_HISTOGRAM("PageLoad.Timing2.NavigationToFirstTextPaint", |
152 timing_.first_text_paint); | 162 timing_.first_text_paint); |
153 } else { | 163 } else { |
154 PAGE_LOAD_HISTOGRAM("PageLoad.Timing2.NavigationToFirstTextPaint.BG", | 164 PAGE_LOAD_HISTOGRAM("PageLoad.Timing2.NavigationToFirstTextPaint.BG", |
155 timing_.first_text_paint); | 165 timing_.first_text_paint); |
156 } | 166 } |
157 } | 167 } |
168 // Log time to first foreground / time to first background. Log counts that we | |
169 // started a relevant page load in the foreground / background. | |
170 if (!background_time_.is_null()) { | |
171 PAGE_LOAD_HISTOGRAM("PageLoad.Timing2.NavigationToFirstBackground", | |
172 background_delta); | |
173 } else if (!foreground_time_.is_null()) { | |
174 PAGE_LOAD_HISTOGRAM( | |
175 "PageLoad.Timing2.NavigationToFirstForeground", | |
176 WallTimeFromTimeTicks(foreground_time_) - timing_.navigation_start); | |
177 } | |
158 } | 178 } |
159 | 179 |
160 void PageLoadTracker::RecordEvent(PageLoadEvent event) { | 180 void PageLoadTracker::RecordProvisionalEvent(ProvisionalLoadEvent event) { |
161 UMA_HISTOGRAM_ENUMERATION( | 181 if (HasBackgrounded()) { |
162 "PageLoad.EventCounts", event, PAGE_LOAD_LAST_ENTRY); | 182 UMA_HISTOGRAM_ENUMERATION("PageLoad.Events.Provisional.BG", event, |
183 PROVISIONAL_LOAD_LAST_ENTRY); | |
184 } else { | |
185 UMA_HISTOGRAM_ENUMERATION("PageLoad.Events.Provisional", event, | |
186 PROVISIONAL_LOAD_LAST_ENTRY); | |
187 } | |
188 } | |
189 | |
190 // RecordCommittedEvent needs a backgrounded input because we need to special | |
191 // case a few events that need either precise timing measurements, or different | |
192 // logic than simply "Did I background before logging this event?" | |
193 void PageLoadTracker::RecordCommittedEvent(CommittedLoadEvent event, | |
194 bool backgrounded) { | |
195 if (backgrounded) { | |
196 UMA_HISTOGRAM_ENUMERATION("PageLoad.Events.Committed.BG", event, | |
197 COMMITTED_LOAD_LAST_ENTRY); | |
198 } else { | |
199 UMA_HISTOGRAM_ENUMERATION("PageLoad.Events.Committed", event, | |
200 COMMITTED_LOAD_LAST_ENTRY); | |
201 } | |
163 } | 202 } |
164 | 203 |
165 MetricsWebContentsObserver::MetricsWebContentsObserver( | 204 MetricsWebContentsObserver::MetricsWebContentsObserver( |
166 content::WebContents* web_contents) | 205 content::WebContents* web_contents) |
167 : content::WebContentsObserver(web_contents), in_foreground_(false) {} | 206 : content::WebContentsObserver(web_contents), in_foreground_(false) {} |
168 | 207 |
169 MetricsWebContentsObserver::~MetricsWebContentsObserver() {} | 208 MetricsWebContentsObserver::~MetricsWebContentsObserver() {} |
170 | 209 |
171 bool MetricsWebContentsObserver::OnMessageReceived( | 210 bool MetricsWebContentsObserver::OnMessageReceived( |
172 const IPC::Message& message, | 211 const IPC::Message& message, |
(...skipping 25 matching lines...) Expand all Loading... | |
198 if (!navigation_handle->IsInMainFrame()) | 237 if (!navigation_handle->IsInMainFrame()) |
199 return; | 238 return; |
200 | 239 |
201 scoped_ptr<PageLoadTracker> finished_nav( | 240 scoped_ptr<PageLoadTracker> finished_nav( |
202 provisional_loads_.take_and_erase(navigation_handle)); | 241 provisional_loads_.take_and_erase(navigation_handle)); |
203 DCHECK(finished_nav); | 242 DCHECK(finished_nav); |
204 | 243 |
205 // Handle a pre-commit error here. Navigations that result in an error page | 244 // Handle a pre-commit error here. Navigations that result in an error page |
206 // will be ignored. | 245 // will be ignored. |
207 if (!navigation_handle->HasCommitted()) { | 246 if (!navigation_handle->HasCommitted()) { |
208 finished_nav->RecordEvent(PAGE_LOAD_FAILED_BEFORE_COMMIT); | |
209 if (navigation_handle->GetNetErrorCode() == net::ERR_ABORTED) | 247 if (navigation_handle->GetNetErrorCode() == net::ERR_ABORTED) |
210 finished_nav->RecordEvent(PAGE_LOAD_ABORTED_BEFORE_COMMIT); | 248 finished_nav->RecordProvisionalEvent(PROVISIONAL_LOAD_ABORTED); |
249 else | |
250 finished_nav->RecordProvisionalEvent(PROVISIONAL_LOAD_FAILED_NON_ABORT); | |
211 return; | 251 return; |
212 } | 252 } |
253 finished_nav->RecordProvisionalEvent(PROVISIONAL_LOAD_COMMITTED); | |
213 | 254 |
214 // Don't treat a same-page nav as a new page load. | 255 // Don't treat a same-page nav as a new page load. |
215 if (navigation_handle->IsSamePage()) | 256 if (navigation_handle->IsSamePage()) |
216 return; | 257 return; |
217 | 258 |
218 // Eagerly log the previous UMA even if we don't care about the current | 259 // Eagerly log the previous UMA even if we don't care about the current |
219 // navigation. | 260 // navigation. |
220 committed_load_.reset(); | 261 committed_load_.reset(); |
221 | 262 |
222 if (!IsRelevantNavigation(navigation_handle)) | 263 if (!IsRelevantNavigation(navigation_handle)) |
223 return; | 264 return; |
224 | 265 |
225 committed_load_ = finished_nav.Pass(); | 266 committed_load_ = finished_nav.Pass(); |
226 committed_load_->Commit(); | 267 committed_load_->Commit(); |
227 } | 268 } |
228 | 269 |
229 void MetricsWebContentsObserver::WasShown() { | 270 void MetricsWebContentsObserver::WasShown() { |
230 in_foreground_ = true; | 271 in_foreground_ = true; |
272 if (committed_load_) | |
273 committed_load_->WebContentsShown(); | |
274 for (const auto& kv : provisional_loads_) { | |
275 kv.second->WebContentsShown(); | |
276 } | |
231 } | 277 } |
232 | 278 |
233 void MetricsWebContentsObserver::WasHidden() { | 279 void MetricsWebContentsObserver::WasHidden() { |
234 in_foreground_ = false; | 280 in_foreground_ = false; |
235 if (committed_load_) | 281 if (committed_load_) |
236 committed_load_->WebContentsHidden(); | 282 committed_load_->WebContentsHidden(); |
237 for (const auto& kv : provisional_loads_) { | 283 for (const auto& kv : provisional_loads_) { |
238 kv.second->WebContentsHidden(); | 284 kv.second->WebContentsHidden(); |
239 } | 285 } |
240 } | 286 } |
241 | 287 |
242 // This will occur when the process for the main RenderFrameHost exits, either | 288 // This will occur when the process for the main RenderFrameHost exits, either |
243 // normally or from a crash. We eagerly log data from the last committed load if | 289 // normally or from a crash. We eagerly log data from the last committed load if |
244 // we have one. | 290 // we have one. |
245 void MetricsWebContentsObserver::RenderProcessGone( | 291 void MetricsWebContentsObserver::RenderProcessGone( |
246 base::TerminationStatus status) { | 292 base::TerminationStatus status) { |
247 committed_load_.reset(); | 293 committed_load_.reset(); |
248 } | 294 } |
249 | 295 |
250 void MetricsWebContentsObserver::OnTimingUpdated( | 296 void MetricsWebContentsObserver::OnTimingUpdated( |
251 content::RenderFrameHost* render_frame_host, | 297 content::RenderFrameHost* render_frame_host, |
252 const PageLoadTiming& timing) { | 298 const PageLoadTiming& timing) { |
253 if (!committed_load_) | 299 bool error = false; |
254 return; | 300 if (!committed_load_) { |
301 UMA_HISTOGRAM_ENUMERATION("PageLoad.Events.Error", | |
Bryan McQuade
2015/10/12 16:39:22
Is there a name we could use to convey that these
| |
302 ERR_IPC_WITH_NO_COMMITTED_LOAD, ERR_LAST_ENTRY); | |
303 error = true; | |
304 } | |
255 | 305 |
256 // We may receive notifications from frames that have been navigated away | 306 // We may receive notifications from frames that have been navigated away |
257 // from. We simply ignore them. | 307 // from. We simply ignore them. |
258 if (render_frame_host != web_contents()->GetMainFrame()) | 308 if (render_frame_host != web_contents()->GetMainFrame()) { |
259 return; | 309 UMA_HISTOGRAM_ENUMERATION("PageLoad.Events.Error", ERR_IPC_FROM_WRONG_FRAME, |
310 ERR_LAST_ENTRY); | |
311 error = true; | |
312 } | |
260 | 313 |
261 // For urls like chrome://newtab, the renderer and browser disagree, | 314 // For urls like chrome://newtab, the renderer and browser disagree, |
262 // so we have to double check that the renderer isn't sending data from a | 315 // so we have to double check that the renderer isn't sending data from a |
263 // bad url like https://www.google.com/_/chrome/newtab. | 316 // bad url like https://www.google.com/_/chrome/newtab. |
264 if (!web_contents()->GetLastCommittedURL().SchemeIsHTTPOrHTTPS()) | 317 if (!web_contents()->GetLastCommittedURL().SchemeIsHTTPOrHTTPS()) { |
318 UMA_HISTOGRAM_ENUMERATION("PageLoad.Events.Error", | |
319 ERR_IPC_FROM_BAD_URL_SCHEME, ERR_LAST_ENTRY); | |
320 error = true; | |
321 } | |
322 | |
323 if (error) | |
265 return; | 324 return; |
266 | 325 |
267 committed_load_->UpdateTiming(timing); | 326 if (!committed_load_->UpdateTiming(timing)) { |
327 // If the page load tracker cannot update its timing, something is wrong | |
328 // with the IPC (it's from another load, or it's invalid in some other way). | |
329 // We expect this to be a rare occurrence. | |
330 UMA_HISTOGRAM_ENUMERATION("PageLoad.Events.Error", ERR_BAD_TIMING_IPC, | |
331 ERR_LAST_ENTRY); | |
332 } | |
268 } | 333 } |
269 | 334 |
270 bool MetricsWebContentsObserver::IsRelevantNavigation( | 335 bool MetricsWebContentsObserver::IsRelevantNavigation( |
271 content::NavigationHandle* navigation_handle) { | 336 content::NavigationHandle* navigation_handle) { |
272 // The url we see from the renderer side is not always the same as what | 337 // The url we see from the renderer side is not always the same as what |
273 // we see from the browser side (e.g. chrome://newtab). We want to be | 338 // we see from the browser side (e.g. chrome://newtab). We want to be |
274 // sure here that we aren't logging UMA for internal pages. | 339 // sure here that we aren't logging UMA for internal pages. |
275 const GURL& browser_url = web_contents()->GetLastCommittedURL(); | 340 const GURL& browser_url = web_contents()->GetLastCommittedURL(); |
276 return navigation_handle->IsInMainFrame() && | 341 return navigation_handle->IsInMainFrame() && |
277 !navigation_handle->IsSamePage() && | 342 !navigation_handle->IsSamePage() && |
278 !navigation_handle->IsErrorPage() && | 343 !navigation_handle->IsErrorPage() && |
279 navigation_handle->GetURL().SchemeIsHTTPOrHTTPS() && | 344 navigation_handle->GetURL().SchemeIsHTTPOrHTTPS() && |
280 browser_url.SchemeIsHTTPOrHTTPS(); | 345 browser_url.SchemeIsHTTPOrHTTPS(); |
281 } | 346 } |
282 | 347 |
283 } // namespace page_load_metrics | 348 } // namespace page_load_metrics |
OLD | NEW |