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.
5 #include "components/page_load_metrics/browser/metrics_web_contents_observer.h"
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"
21 DEFINE_WEB_CONTENTS_USER_DATA_KEY(
22 page_load_metrics::MetricsWebContentsObserver
);
24 namespace page_load_metrics
{
28 bool IsValidPageLoadTiming(const PageLoadTiming
& timing
) {
32 // If we have a non-empty timing, it should always have a navigation start.
33 DCHECK(!timing
.navigation_start
.is_null());
35 // If we have a DOM content loaded event, we should have a response start.
37 !timing
.dom_content_loaded_event_start
.is_zero(),
38 timing
.response_start
<= timing
.dom_content_loaded_event_start
);
40 // If we have a load event, we should have both a response start and a DCL.
42 !timing
.load_event_start
.is_zero(),
43 !timing
.dom_content_loaded_event_start
.is_zero() &&
44 timing
.response_start
<= timing
.load_event_start
&&
45 timing
.dom_content_loaded_event_start
<= timing
.load_event_start
);
52 MetricsWebContentsObserver::MetricsWebContentsObserver(
53 content::WebContents
* web_contents
)
54 : content::WebContentsObserver(web_contents
) {}
56 // This object is tied to a single WebContents for its entire lifetime.
57 MetricsWebContentsObserver::~MetricsWebContentsObserver() {
58 RecordTimingHistograms();
61 bool MetricsWebContentsObserver::OnMessageReceived(
62 const IPC::Message
& message
,
63 content::RenderFrameHost
* render_frame_host
) {
64 DCHECK_CURRENTLY_ON(content::BrowserThread::UI
);
66 IPC_BEGIN_MESSAGE_MAP_WITH_PARAM(MetricsWebContentsObserver
, message
,
68 IPC_MESSAGE_HANDLER(PageLoadMetricsMsg_TimingUpdated
, OnTimingUpdated
)
69 IPC_MESSAGE_UNHANDLED(handled
= false)
74 void MetricsWebContentsObserver::DidCommitNavigation(
75 content::NavigationHandle
* navigation_handle
) {
76 if (navigation_handle
->IsInMainFrame() && !navigation_handle
->IsSamePage())
77 RecordTimingHistograms();
78 if (IsRelevantNavigation(navigation_handle
))
79 current_timing_
.reset(new PageLoadTiming());
82 // This will occur when the process for the main RenderFrameHost exits.
83 // This will happen with a normal exit or a crash.
84 void MetricsWebContentsObserver::RenderProcessGone(
85 base::TerminationStatus status
) {
86 RecordTimingHistograms();
89 #define PAGE_LOAD_HISTOGRAM(name, sample) \
90 UMA_HISTOGRAM_CUSTOM_TIMES(name, sample, \
91 base::TimeDelta::FromMilliseconds(10), \
92 base::TimeDelta::FromMinutes(10), 100);
94 void MetricsWebContentsObserver::OnTimingUpdated(
95 content::RenderFrameHost
* render_frame_host
,
96 const PageLoadTiming
& timing
) {
100 // We may receive notifications from frames that have been navigated away
101 // from. We simply ignore them.
102 if (render_frame_host
!= web_contents()->GetMainFrame())
105 // For urls like chrome://newtab, the renderer and browser disagree,
106 // so we have to double check that the renderer isn't sending data from a
107 // bad url like https://www.google.com/_/chrome/newtab.
108 if (!web_contents()->GetLastCommittedURL().SchemeIsHTTPOrHTTPS())
111 // Throw away IPCs that are not relevant to the current navigation.
112 if (!current_timing_
->navigation_start
.is_null() &&
113 timing
.navigation_start
!= current_timing_
->navigation_start
) {
114 // TODO(csharrison) uma log a counter here
118 *current_timing_
= timing
;
121 void MetricsWebContentsObserver::RecordTimingHistograms() {
122 if (!current_timing_
|| !IsValidPageLoadTiming(*current_timing_
))
125 if (!current_timing_
->dom_content_loaded_event_start
.is_zero()) {
127 "PageLoad.Timing.NavigationToDOMContentLoadedEventFired",
128 current_timing_
->dom_content_loaded_event_start
);
131 if (!current_timing_
->load_event_start
.is_zero()) {
132 PAGE_LOAD_HISTOGRAM("PageLoad.Timing.NavigationToLoadEventFired",
133 current_timing_
->load_event_start
);
136 if (!current_timing_
->first_layout
.is_zero()) {
137 PAGE_LOAD_HISTOGRAM("PageLoad.Timing.NavigationToFirstLayout",
138 current_timing_
->first_layout
);
140 current_timing_
.reset();
143 bool MetricsWebContentsObserver::IsRelevantNavigation(
144 content::NavigationHandle
* navigation_handle
) {
145 // The url we see from the renderer side is not always the same as what
146 // we see from the browser side (e.g. chrome://newtab). We want to be
147 // sure here that we aren't logging UMA for internal pages.
148 const GURL
& browser_url
= web_contents()->GetLastCommittedURL();
149 return navigation_handle
->IsInMainFrame() &&
150 !navigation_handle
->IsSamePage() &&
151 navigation_handle
->HasCommittedDocument() &&
152 navigation_handle
->GetURL().SchemeIsHTTPOrHTTPS() &&
153 browser_url
.SchemeIsHTTPOrHTTPS();
156 } // namespace page_load_metrics