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

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: Only log first background time 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 background_time = in_foreground ? base::TimeDelta::Max() : base::TimeDelta();
Alexei Svitkine (slow) 2015/09/28 21:40:35 Nit: Make this just an if, since base::TimeDelta i
59 }
60
61 PageLoadTracker::~PageLoadTracker() {
62 if (has_commit_)
63 RecordTimingHistograms();
64 }
65
66 void PageLoadTracker::WebContentsHidden() {
67 // Only log the first time we background in a given page load.
68 if (!background_time.is_max())
69 return;
70
71 // This method is similar to how blink converts TimeTicks to epoch time.
72 // There may be slight inaccuracies due to inter-process timestamps, but
73 // this solution is the best we have right now.
74 base::TimeDelta pseudo_wall =
75 base::TimeTicks::Now() - base::TimeTicks::UnixEpoch();
76 background_time = base::Time::FromDoubleT(pseudo_wall.InSecondsF()) -
77 timing_.navigation_start;
78 }
79
80 void PageLoadTracker::Commit() {
81 has_commit_ = true;
82 }
83
84 bool PageLoadTracker::UpdateTiming(const PageLoadTiming& timing) {
85 // Throw away IPCs that are not relevant to the current navigation.
86 if (!timing_.navigation_start.is_null() &&
87 timing_.navigation_start != timing.navigation_start) {
88 // TODO(csharrison) uma log a counter here
89 return false;
90 }
91 if (IsValidPageLoadTiming(timing)) {
92 timing_ = timing;
93 return true;
94 }
95 return false;
96 }
97
98 void PageLoadTracker::RecordTimingHistograms() {
99 DCHECK(has_commit_);
100 if (!timing_.dom_content_loaded_event_start.is_zero()) {
101 if (timing_.dom_content_loaded_event_start < background_time) {
102 PAGE_LOAD_HISTOGRAM(
103 "PageLoad.Timing.NavigationToDOMContentLoadedEventFired",
104 timing_.dom_content_loaded_event_start);
105 } else {
106 PAGE_LOAD_HISTOGRAM(
107 "PageLoad.Timing.NavigationToDOMContentLoadedEventFired.BG",
108 timing_.dom_content_loaded_event_start);
109 }
110 }
111 if (!timing_.load_event_start.is_zero()) {
112 if (timing_.load_event_start < background_time) {
113 PAGE_LOAD_HISTOGRAM("PageLoad.Timing.NavigationToLoadEventFired",
114 timing_.load_event_start);
115 } else {
116 PAGE_LOAD_HISTOGRAM("PageLoad.Timing.NavigationToLoadEventFired.BG",
117 timing_.load_event_start);
118 }
119 }
120 if (!timing_.first_layout.is_zero()) {
121 if (timing_.first_layout < background_time) {
122 PAGE_LOAD_HISTOGRAM("PageLoad.Timing.NavigationToFirstLayout",
123 timing_.first_layout);
124 } else {
125 PAGE_LOAD_HISTOGRAM("PageLoad.Timing.NavigationToFirstLayout.BG",
126 timing_.first_layout);
127 }
128 }
129 }
130
52 MetricsWebContentsObserver::MetricsWebContentsObserver( 131 MetricsWebContentsObserver::MetricsWebContentsObserver(
53 content::WebContents* web_contents) 132 content::WebContents* web_contents)
54 : content::WebContentsObserver(web_contents) {} 133 : content::WebContentsObserver(web_contents), in_foreground_(false) {}
55 134
56 // This object is tied to a single WebContents for its entire lifetime. 135 MetricsWebContentsObserver::~MetricsWebContentsObserver() {}
57 MetricsWebContentsObserver::~MetricsWebContentsObserver() {
58 RecordTimingHistograms();
59 }
60 136
61 bool MetricsWebContentsObserver::OnMessageReceived( 137 bool MetricsWebContentsObserver::OnMessageReceived(
62 const IPC::Message& message, 138 const IPC::Message& message,
63 content::RenderFrameHost* render_frame_host) { 139 content::RenderFrameHost* render_frame_host) {
64 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); 140 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
65 bool handled = true; 141 bool handled = true;
66 IPC_BEGIN_MESSAGE_MAP_WITH_PARAM(MetricsWebContentsObserver, message, 142 IPC_BEGIN_MESSAGE_MAP_WITH_PARAM(MetricsWebContentsObserver, message,
67 render_frame_host) 143 render_frame_host)
68 IPC_MESSAGE_HANDLER(PageLoadMetricsMsg_TimingUpdated, OnTimingUpdated) 144 IPC_MESSAGE_HANDLER(PageLoadMetricsMsg_TimingUpdated, OnTimingUpdated)
69 IPC_MESSAGE_UNHANDLED(handled = false) 145 IPC_MESSAGE_UNHANDLED(handled = false)
70 IPC_END_MESSAGE_MAP() 146 IPC_END_MESSAGE_MAP()
71 return handled; 147 return handled;
72 } 148 }
73 149
74 void MetricsWebContentsObserver::DidFinishNavigation( 150 void MetricsWebContentsObserver::DidFinishNavigation(
75 content::NavigationHandle* navigation_handle) { 151 content::NavigationHandle* navigation_handle) {
152 if (!navigation_handle->IsInMainFrame())
153 return;
154
155 scoped_ptr<PageLoadTracker> finished_nav(
156 provisional_loads_.take_and_erase(navigation_handle));
157
158 // TODO(csharrison) handle the two error cases:
159 // 1. Error pages that replace the previous page.
160 // 2. Error pages that leave the user on the previous page.
161 // For now these cases will be ignored.
76 if (!navigation_handle->HasCommitted()) 162 if (!navigation_handle->HasCommitted())
77 return; 163 return;
78 if (navigation_handle->IsInMainFrame() && !navigation_handle->IsSamePage()) 164
79 RecordTimingHistograms(); 165 if (!IsRelevantNavigation(navigation_handle))
80 if (IsRelevantNavigation(navigation_handle)) 166 return;
81 current_timing_.reset(new PageLoadTiming()); 167
168 // Updating |committed_load_| will trigger RecordTimingHistograms on a
169 // previous PageLoadTracker.
170 committed_load_ = finished_nav.Pass();
171 DCHECK(committed_load_);
172 committed_load_->Commit();
82 } 173 }
83 174
84 // This will occur when the process for the main RenderFrameHost exits. 175 void MetricsWebContentsObserver::DidStartNavigation(
85 // This will happen with a normal exit or a crash. 176 content::NavigationHandle* navigation_handle) {
86 void MetricsWebContentsObserver::RenderProcessGone( 177 if (!navigation_handle->IsInMainFrame())
87 base::TerminationStatus status) { 178 return;
88 RecordTimingHistograms(); 179 // We can have two provisional loads in some cases. E.g. a same-site
180 // navigation can have a concurrent cross-process navigation started
181 // from the omnibox.
182 DCHECK(provisional_loads_.size() < 2);
183 provisional_loads_.insert(
184 navigation_handle, make_scoped_ptr(new PageLoadTracker(in_foreground_)));
89 } 185 }
90 186
91 #define PAGE_LOAD_HISTOGRAM(name, sample) \ 187 void MetricsWebContentsObserver::WasShown() {
92 UMA_HISTOGRAM_CUSTOM_TIMES(name, sample, \ 188 in_foreground_ = true;
93 base::TimeDelta::FromMilliseconds(10), \ 189 }
Alexei Svitkine (slow) 2015/09/28 21:40:35 Nit: Add empty line after.
94 base::TimeDelta::FromMinutes(10), 100); 190 void MetricsWebContentsObserver::WasHidden() {
191 in_foreground_ = false;
192 if (committed_load_)
193 committed_load_->WebContentsHidden();
194 for (const auto& kv : provisional_loads_) {
195 kv.second->WebContentsHidden();
196 }
197 }
95 198
96 void MetricsWebContentsObserver::OnTimingUpdated( 199 void MetricsWebContentsObserver::OnTimingUpdated(
97 content::RenderFrameHost* render_frame_host, 200 content::RenderFrameHost* render_frame_host,
98 const PageLoadTiming& timing) { 201 const PageLoadTiming& timing) {
99 if (!current_timing_)
100 return;
101
102 // We may receive notifications from frames that have been navigated away 202 // We may receive notifications from frames that have been navigated away
103 // from. We simply ignore them. 203 // from. We simply ignore them.
104 if (render_frame_host != web_contents()->GetMainFrame()) 204 if (render_frame_host != web_contents()->GetMainFrame())
105 return; 205 return;
106 206
107 // For urls like chrome://newtab, the renderer and browser disagree, 207 // 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 208 // 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. 209 // bad url like https://www.google.com/_/chrome/newtab.
110 if (!web_contents()->GetLastCommittedURL().SchemeIsHTTPOrHTTPS()) 210 if (!web_contents()->GetLastCommittedURL().SchemeIsHTTPOrHTTPS())
111 return; 211 return;
112 212
113 // Throw away IPCs that are not relevant to the current navigation. 213 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 } 214 }
144 215
145 bool MetricsWebContentsObserver::IsRelevantNavigation( 216 bool MetricsWebContentsObserver::IsRelevantNavigation(
146 content::NavigationHandle* navigation_handle) { 217 content::NavigationHandle* navigation_handle) {
147 // The url we see from the renderer side is not always the same as what 218 // 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 219 // 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. 220 // sure here that we aren't logging UMA for internal pages.
150 const GURL& browser_url = web_contents()->GetLastCommittedURL(); 221 const GURL& browser_url = web_contents()->GetLastCommittedURL();
151 return navigation_handle->IsInMainFrame() && 222 return navigation_handle->IsInMainFrame() &&
152 !navigation_handle->IsSamePage() && 223 !navigation_handle->IsSamePage() &&
153 !navigation_handle->IsErrorPage() && 224 !navigation_handle->IsErrorPage() &&
154 navigation_handle->GetURL().SchemeIsHTTPOrHTTPS() && 225 navigation_handle->GetURL().SchemeIsHTTPOrHTTPS() &&
155 browser_url.SchemeIsHTTPOrHTTPS(); 226 browser_url.SchemeIsHTTPOrHTTPS();
156 } 227 }
157 228
158 } // namespace page_load_metrics 229 } // namespace page_load_metrics
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698