Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(69)

Side by Side Diff: components/page_load_metrics/browser/metrics_web_contents_observer.cc

Issue 1357403003: Separate page load metrics for backgrounded pages (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Can't record raw delta before we have nav_start. Be more lazy. Created 5 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 31 matching lines...) Expand 10 before | Expand all | Expand 10 after
42 !timing.load_event_start.is_zero(), 42 !timing.load_event_start.is_zero(),
43 !timing.dom_content_loaded_event_start.is_zero() && 43 !timing.dom_content_loaded_event_start.is_zero() &&
44 timing.response_start <= timing.load_event_start && 44 timing.response_start <= timing.load_event_start &&
45 timing.dom_content_loaded_event_start <= timing.load_event_start); 45 timing.dom_content_loaded_event_start <= timing.load_event_start);
46 46
47 return true; 47 return true;
48 } 48 }
49 49
50 } // namespace 50 } // namespace
51 51
52 #define PAGE_LOAD_HISTOGRAM(name, sample) \
53 UMA_HISTOGRAM_CUSTOM_TIMES(name, sample, \
54 base::TimeDelta::FromMilliseconds(10), \
55 base::TimeDelta::FromMinutes(10), 100)
56
57 PageLoadTracker::PageLoadTracker(bool in_foreground) : has_commit_(false) {
58 if (in_foreground)
59 background_time_ = base::Time::Max();
60 }
61
62 PageLoadTracker::~PageLoadTracker() {
63 if (has_commit_)
64 RecordTimingHistograms();
65 }
66
67 void PageLoadTracker::WebContentsHidden() {
68 // Only log the first time we background in a given page load.
69 if (!background_time_.is_max())
70 return;
71
72 // This method is similar to how blink converts TimeTicks to epoch time.
73 // There may be slight inaccuracies due to inter-process timestamps, but
74 // this solution is the best we have right now.
75 background_time_ = base::Time::FromDoubleT(
76 (base::TimeTicks::Now() - base::TimeTicks::UnixEpoch()).InSecondsF());
77 }
78
79 void PageLoadTracker::Commit() {
80 has_commit_ = true;
81 }
82
83 bool PageLoadTracker::UpdateTiming(const PageLoadTiming& timing) {
84 // Throw away IPCs that are not relevant to the current navigation.
85 if (!timing_.navigation_start.is_null() &&
86 timing_.navigation_start != timing.navigation_start) {
87 // TODO(csharrison) uma log a counter here
88 return false;
89 }
90 if (IsValidPageLoadTiming(timing)) {
91 timing_ = timing;
92 return true;
93 }
94 return false;
95 }
96
97 void PageLoadTracker::RecordTimingHistograms() {
98 DCHECK(has_commit_);
99 // If we start in the foreground, |background_delta| will be extremely large
100 // (Max() - nav_start), and if we start in the background, |background_delta|
101 // will be extremely small (0 - nav_start).
102 base::TimeDelta background_delta =
Bryan McQuade 2015/09/29 00:32:19 Let's be a little more explicit about the handling
Bryan McQuade 2015/09/29 12:38:52 Actually, there are two cases we should handle exp
Charlie Harrison 2015/09/29 14:13:07 Good idea. Done.
103 background_time_ - timing_.navigation_start;
104 if (!timing_.dom_content_loaded_event_start.is_zero()) {
105 if (timing_.dom_content_loaded_event_start < background_delta) {
106 PAGE_LOAD_HISTOGRAM(
107 "PageLoad.Timing.NavigationToDOMContentLoadedEventFired",
108 timing_.dom_content_loaded_event_start);
109 } else {
110 PAGE_LOAD_HISTOGRAM(
111 "PageLoad.Timing.NavigationToDOMContentLoadedEventFired.BG",
112 timing_.dom_content_loaded_event_start);
113 }
114 }
115 if (!timing_.load_event_start.is_zero()) {
116 if (timing_.load_event_start < background_delta) {
117 PAGE_LOAD_HISTOGRAM("PageLoad.Timing.NavigationToLoadEventFired",
118 timing_.load_event_start);
119 } else {
120 PAGE_LOAD_HISTOGRAM("PageLoad.Timing.NavigationToLoadEventFired.BG",
121 timing_.load_event_start);
122 }
123 }
124 if (!timing_.first_layout.is_zero()) {
125 if (timing_.first_layout < background_delta) {
126 PAGE_LOAD_HISTOGRAM("PageLoad.Timing.NavigationToFirstLayout",
127 timing_.first_layout);
128 } else {
129 PAGE_LOAD_HISTOGRAM("PageLoad.Timing.NavigationToFirstLayout.BG",
130 timing_.first_layout);
131 }
132 }
133 }
134
52 MetricsWebContentsObserver::MetricsWebContentsObserver( 135 MetricsWebContentsObserver::MetricsWebContentsObserver(
53 content::WebContents* web_contents) 136 content::WebContents* web_contents)
54 : content::WebContentsObserver(web_contents) {} 137 : content::WebContentsObserver(web_contents), in_foreground_(false) {}
55 138
56 // This object is tied to a single WebContents for its entire lifetime. 139 MetricsWebContentsObserver::~MetricsWebContentsObserver() {}
57 MetricsWebContentsObserver::~MetricsWebContentsObserver() {
58 RecordTimingHistograms();
59 }
60 140
61 bool MetricsWebContentsObserver::OnMessageReceived( 141 bool MetricsWebContentsObserver::OnMessageReceived(
62 const IPC::Message& message, 142 const IPC::Message& message,
63 content::RenderFrameHost* render_frame_host) { 143 content::RenderFrameHost* render_frame_host) {
64 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); 144 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
65 bool handled = true; 145 bool handled = true;
66 IPC_BEGIN_MESSAGE_MAP_WITH_PARAM(MetricsWebContentsObserver, message, 146 IPC_BEGIN_MESSAGE_MAP_WITH_PARAM(MetricsWebContentsObserver, message,
67 render_frame_host) 147 render_frame_host)
68 IPC_MESSAGE_HANDLER(PageLoadMetricsMsg_TimingUpdated, OnTimingUpdated) 148 IPC_MESSAGE_HANDLER(PageLoadMetricsMsg_TimingUpdated, OnTimingUpdated)
69 IPC_MESSAGE_UNHANDLED(handled = false) 149 IPC_MESSAGE_UNHANDLED(handled = false)
70 IPC_END_MESSAGE_MAP() 150 IPC_END_MESSAGE_MAP()
71 return handled; 151 return handled;
72 } 152 }
73 153
74 void MetricsWebContentsObserver::DidFinishNavigation( 154 void MetricsWebContentsObserver::DidFinishNavigation(
75 content::NavigationHandle* navigation_handle) { 155 content::NavigationHandle* navigation_handle) {
156 if (!navigation_handle->IsInMainFrame())
157 return;
158
159 scoped_ptr<PageLoadTracker> finished_nav(
160 provisional_loads_.take_and_erase(navigation_handle));
161
Bryan McQuade 2015/09/29 00:32:19 let's move the DCHECK from below up here: DCHECK(f
Charlie Harrison 2015/09/29 14:13:07 Done.
162 // TODO(csharrison) handle the two error cases:
163 // 1. Error pages that replace the previous page.
164 // 2. Error pages that leave the user on the previous page.
165 // For now these cases will be ignored.
76 if (!navigation_handle->HasCommitted()) 166 if (!navigation_handle->HasCommitted())
Bryan McQuade 2015/09/29 00:32:19 what's the difference between testing !HasCommitte
Charlie Harrison 2015/09/29 14:13:07 IsErrorPage is true when HasCommitted is true and
77 return; 167 return;
78 if (navigation_handle->IsInMainFrame() && !navigation_handle->IsSamePage()) 168
79 RecordTimingHistograms(); 169 if (!IsRelevantNavigation(navigation_handle))
Bryan McQuade 2015/09/29 00:32:19 thinking about this more, I do think we should log
Charlie Harrison 2015/09/29 14:13:07 I have no objections to that! Done.
80 if (IsRelevantNavigation(navigation_handle)) 170 return;
81 current_timing_.reset(new PageLoadTiming()); 171
172 // Updating |committed_load_| will trigger RecordTimingHistograms on a
173 // previous PageLoadTracker.
174 committed_load_ = finished_nav.Pass();
175 DCHECK(committed_load_);
176 committed_load_->Commit();
82 } 177 }
83 178
84 // This will occur when the process for the main RenderFrameHost exits. 179 void MetricsWebContentsObserver::DidStartNavigation(
Bryan McQuade 2015/09/29 00:32:19 let's put DidStartNavigation impl before DidFinish
Charlie Harrison 2015/09/29 14:13:07 Done.
85 // This will happen with a normal exit or a crash. 180 content::NavigationHandle* navigation_handle) {
86 void MetricsWebContentsObserver::RenderProcessGone( 181 if (!navigation_handle->IsInMainFrame())
Bryan McQuade 2015/09/29 00:32:19 if removing this causes us to log metrics less eag
Charlie Harrison 2015/09/29 14:13:07 Yeah I think it could. Done.
87 base::TerminationStatus status) { 182 return;
88 RecordTimingHistograms(); 183 // We can have two provisional loads in some cases. E.g. a same-site
184 // navigation can have a concurrent cross-process navigation started
185 // from the omnibox.
186 DCHECK(provisional_loads_.size() < 2);
187 provisional_loads_.insert(
188 navigation_handle, make_scoped_ptr(new PageLoadTracker(in_foreground_)));
89 } 189 }
90 190
91 #define PAGE_LOAD_HISTOGRAM(name, sample) \ 191 void MetricsWebContentsObserver::WasShown() {
92 UMA_HISTOGRAM_CUSTOM_TIMES(name, sample, \ 192 in_foreground_ = true;
93 base::TimeDelta::FromMilliseconds(10), \ 193 }
94 base::TimeDelta::FromMinutes(10), 100); 194
195 void MetricsWebContentsObserver::WasHidden() {
196 in_foreground_ = false;
197 if (committed_load_)
198 committed_load_->WebContentsHidden();
199 for (const auto& kv : provisional_loads_) {
200 kv.second->WebContentsHidden();
201 }
202 }
95 203
96 void MetricsWebContentsObserver::OnTimingUpdated( 204 void MetricsWebContentsObserver::OnTimingUpdated(
97 content::RenderFrameHost* render_frame_host, 205 content::RenderFrameHost* render_frame_host,
98 const PageLoadTiming& timing) { 206 const PageLoadTiming& timing) {
Bryan McQuade 2015/09/29 00:32:19 we used to have: if (!current_timing) return; w
Charlie Harrison 2015/09/29 14:13:07 I removed it under the assumption that DidStartNav
99 if (!current_timing_)
100 return;
101
102 // We may receive notifications from frames that have been navigated away 207 // We may receive notifications from frames that have been navigated away
103 // from. We simply ignore them. 208 // from. We simply ignore them.
104 if (render_frame_host != web_contents()->GetMainFrame()) 209 if (render_frame_host != web_contents()->GetMainFrame())
105 return; 210 return;
106 211
107 // For urls like chrome://newtab, the renderer and browser disagree, 212 // For urls like chrome://newtab, the renderer and browser disagree,
108 // so we have to double check that the renderer isn't sending data from a 213 // so we have to double check that the renderer isn't sending data from a
109 // bad url like https://www.google.com/_/chrome/newtab. 214 // bad url like https://www.google.com/_/chrome/newtab.
110 if (!web_contents()->GetLastCommittedURL().SchemeIsHTTPOrHTTPS()) 215 if (!web_contents()->GetLastCommittedURL().SchemeIsHTTPOrHTTPS())
111 return; 216 return;
112 217
113 // Throw away IPCs that are not relevant to the current navigation. 218 committed_load_->UpdateTiming(timing);
114 if (!current_timing_->navigation_start.is_null() &&
115 timing.navigation_start != current_timing_->navigation_start) {
116 // TODO(csharrison) uma log a counter here
117 return;
118 }
119
120 *current_timing_ = timing;
121 }
122
123 void MetricsWebContentsObserver::RecordTimingHistograms() {
124 if (!current_timing_ || !IsValidPageLoadTiming(*current_timing_))
125 return;
126
127 if (!current_timing_->dom_content_loaded_event_start.is_zero()) {
128 PAGE_LOAD_HISTOGRAM(
129 "PageLoad.Timing.NavigationToDOMContentLoadedEventFired",
130 current_timing_->dom_content_loaded_event_start);
131 }
132
133 if (!current_timing_->load_event_start.is_zero()) {
134 PAGE_LOAD_HISTOGRAM("PageLoad.Timing.NavigationToLoadEventFired",
135 current_timing_->load_event_start);
136 }
137
138 if (!current_timing_->first_layout.is_zero()) {
139 PAGE_LOAD_HISTOGRAM("PageLoad.Timing.NavigationToFirstLayout",
140 current_timing_->first_layout);
141 }
142 current_timing_.reset();
143 } 219 }
144 220
145 bool MetricsWebContentsObserver::IsRelevantNavigation( 221 bool MetricsWebContentsObserver::IsRelevantNavigation(
146 content::NavigationHandle* navigation_handle) { 222 content::NavigationHandle* navigation_handle) {
147 // The url we see from the renderer side is not always the same as what 223 // The url we see from the renderer side is not always the same as what
148 // we see from the browser side (e.g. chrome://newtab). We want to be 224 // we see from the browser side (e.g. chrome://newtab). We want to be
149 // sure here that we aren't logging UMA for internal pages. 225 // sure here that we aren't logging UMA for internal pages.
150 const GURL& browser_url = web_contents()->GetLastCommittedURL(); 226 const GURL& browser_url = web_contents()->GetLastCommittedURL();
151 return navigation_handle->IsInMainFrame() && 227 return navigation_handle->IsInMainFrame() &&
152 !navigation_handle->IsSamePage() && 228 !navigation_handle->IsSamePage() &&
153 !navigation_handle->IsErrorPage() && 229 !navigation_handle->IsErrorPage() &&
154 navigation_handle->GetURL().SchemeIsHTTPOrHTTPS() && 230 navigation_handle->GetURL().SchemeIsHTTPOrHTTPS() &&
155 browser_url.SchemeIsHTTPOrHTTPS(); 231 browser_url.SchemeIsHTTPOrHTTPS();
156 } 232 }
157 233
158 } // namespace page_load_metrics 234 } // namespace page_load_metrics
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698