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

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

Issue 1312213010: PageLoadMetrics renderer and browser implementation. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Integrate DidChangePerformanceTiming, simplify unneeded methods for testing Created 5 years, 3 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
(Empty)
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
3 // found in the LICENSE file.
4
5 #include "components/page_load_metrics/browser/metrics_web_contents_observer.h"
6
7 #include "base/logging.h"
8 #include "base/metrics/histogram.h"
9 #include "components/page_load_metrics/common/page_load_metrics_messages.h"
10 #include "components/page_load_metrics/common/page_load_timing.h"
11 #include "content/public/browser/browser_thread.h"
12 #include "content/public/browser/navigation_details.h"
13 #include "content/public/browser/navigation_handle.h"
14 #include "content/public/browser/render_frame_host.h"
15 #include "content/public/browser/web_contents.h"
16 #include "content/public/browser/web_contents_observer.h"
17 #include "content/public/browser/web_contents_user_data.h"
18 #include "ipc/ipc_message.h"
19 #include "ipc/ipc_message_macros.h"
20
21 DEFINE_WEB_CONTENTS_USER_DATA_KEY(
22 page_load_metrics::MetricsWebContentsObserver);
23
24 namespace page_load_metrics {
25
26 namespace {
27
28 bool IsValidPageLoadTiming(const PageLoadTiming& timing) {
29 if (timing.IsEmpty())
30 return false;
31
32 // If we have a non-empty timing, it should always have a navigation start.
33 if (timing.navigation_start.is_null())
34 return false;
35
36 // If we have a dom content loaded event, we should have a response start.
37 if (!timing.dom_content_loaded_event_start.is_zero()) {
38 if (timing.response_start.is_zero() ||
39 timing.dom_content_loaded_event_start < timing.response_start) {
40 return false;
41 }
42 }
43
44 // If we have a load event, we should have both a response start and a DCL.
45 if (!timing.load_event_start.is_zero()) {
46 if (timing.response_start.is_zero() ||
47 timing.dom_content_loaded_event_start.is_zero() ||
48 timing.load_event_start < timing.dom_content_loaded_event_start ||
49 timing.load_event_start < timing.response_start) {
50 return false;
51 }
52 }
53
54 return true;
55 }
56
57 } // namespace
58 MetricsWebContentsObserver::MetricsWebContentsObserver(
59 content::WebContents* web_contents)
60 : content::WebContentsObserver(web_contents) {}
Bryan McQuade 2015/09/10 22:32:53 should add: current_host_(nullptr) in the initiali
Charlie Harrison 2015/09/11 14:40:52 Done.
61
62 // As a tab helper, this object is tied to a single WebContent
63 // for its entire lifetime.
64 MetricsWebContentsObserver::~MetricsWebContentsObserver() {
65 RecordTimingHistograms();
66 }
67
68 bool MetricsWebContentsObserver::OnMessageReceived(
69 const IPC::Message& message,
70 content::RenderFrameHost* render_frame_host) {
71 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
72 bool handled = true;
73 IPC_BEGIN_MESSAGE_MAP_WITH_PARAM(MetricsWebContentsObserver, message,
74 render_frame_host)
75 IPC_MESSAGE_HANDLER(PageLoadMetricsMsg_TimingUpdated, OnTimingUpdated)
76 IPC_MESSAGE_UNHANDLED(handled = false)
77 IPC_END_MESSAGE_MAP()
78 return handled;
79 }
80
81 void MetricsWebContentsObserver::DidCommitNavigation(
82 content::NavigationHandle* navigation_handle) {
83 if (navigation_handle->IsInMainFrame() && !navigation_handle->IsSamePage())
84 RecordTimingHistograms();
85 if (IsRelevantNavigation(navigation_handle)) {
86 current_timing_.reset(new PageLoadTiming());
87 }
88 }
89
90 // This will occur when the process for the main RenderFrameHost exits.
91 // This will happen with a normal exit or a crash.
92 void MetricsWebContentsObserver::RenderProcessGone(
93 base::TerminationStatus status) {
94 RecordTimingHistograms();
95 }
96
97 #define PAGE_LOAD_HISTOGRAM(name, sample) \
98 UMA_HISTOGRAM_CUSTOM_TIMES(name, sample, \
99 base::TimeDelta::FromMilliseconds(10), \
100 base::TimeDelta::FromMinutes(10), 100);
101
102 void MetricsWebContentsObserver::OnTimingUpdated(
103 content::RenderFrameHost* render_frame_host,
104 const PageLoadTiming& timing) {
105 if (!current_timing_)
106 return;
107
108 // We may receive notifications from frames that have been navigated away
109 // from. We simply ignore them.
110 if (render_frame_host != web_contents()->GetMainFrame())
111 return;
112
113 // See comment in IsRelevantNavigation about
114 // browser/renderer url disagreement (newtab).
115 if (!web_contents()->GetLastCommittedURL().SchemeIsHTTPOrHTTPS())
116 return;
117
118 // Throw away IPCs that are not relevant to the current navigation.
119 if (!current_timing_->navigation_start.is_null() &&
120 timing.navigation_start != current_timing_->navigation_start) {
121 // TODO(csharrison) uma log a counter here
122 return;
123 }
124
125 if (current_timing_->IsEmpty())
126 current_host_ = render_frame_host;
Bryan McQuade 2015/09/10 22:32:53 We should be resetting current_host_ = nullptr bef
Charlie Harrison 2015/09/11 14:40:52 Done.
127 else
128 DCHECK_EQ(render_frame_host, current_host_);
129 *current_timing_ = timing;
130 }
131
132 void MetricsWebContentsObserver::RecordTimingHistograms() {
133 if (!current_timing_ || !IsValidPageLoadTiming(*current_timing_))
134 return;
135
136 if (!current_timing_->dom_content_loaded_event_start.is_zero()) {
137 PAGE_LOAD_HISTOGRAM(
138 "PageLoadTiming.Navigation.To.DOMContentLoadedEventFired",
139 current_timing_->dom_content_loaded_event_start);
140 }
141
142 if (!current_timing_->load_event_start.is_zero()) {
143 PAGE_LOAD_HISTOGRAM(
144 "PageLoadTiming.Navigation.To.LoadEventFired",
145 current_timing_->load_event_start);
146 }
147
148 if (!current_timing_->first_layout.is_zero()) {
149 PAGE_LOAD_HISTOGRAM(
150 "PageLoadTiming.Navigation.To.FirstLayout",
151 current_timing_->first_layout);
152 }
153 current_timing_.reset();
154 }
155
156 bool MetricsWebContentsObserver::IsRelevantNavigation(
157 content::NavigationHandle* navigation_handle) {
158 // The url we see from the renderer side is not always the same as what
159 // we see from the browser side (e.g. chrome://newtab). We want to be
160 // sure here that we aren't logging UMA for internal pages.
161 const GURL& browser_url = web_contents()->GetLastCommittedURL();
162 return navigation_handle->IsInMainFrame() &&
163 !navigation_handle->IsSamePage() &&
164 navigation_handle->HasCommittedDocument() &&
165 navigation_handle->GetURL().SchemeIsHTTPOrHTTPS() &&
166 browser_url.SchemeIsHTTPOrHTTPS();
167 }
168
169 } // namespace page_load_metrics
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698