Chromium Code Reviews| Index: components/page_load_metrics/browser/page_load_metrics_web_contents_observer.cc |
| diff --git a/components/page_load_metrics/browser/page_load_metrics_web_contents_observer.cc b/components/page_load_metrics/browser/page_load_metrics_web_contents_observer.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..e1e9263e95f2b8267ee13e4f36cde05534b49f38 |
| --- /dev/null |
| +++ b/components/page_load_metrics/browser/page_load_metrics_web_contents_observer.cc |
| @@ -0,0 +1,152 @@ |
| +// Copyright 2015 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "components/page_load_metrics/browser/page_load_metrics_web_contents_observer.h" |
| + |
| +#include "base/logging.h" |
| +#include "base/metrics/histogram.h" |
| +#include "components/page_load_metrics/common/page_load_metrics_messages.h" |
| +#include "components/page_load_metrics/common/page_load_timing.h" |
| +#include "content/public/browser/navigation_details.h" |
| +#include "content/public/browser/navigation_handle.h" |
| +#include "content/public/browser/render_frame_host.h" |
| +#include "content/public/browser/web_contents.h" |
| +#include "content/public/browser/web_contents_observer.h" |
| +#include "content/public/browser/web_contents_user_data.h" |
| +#include "ipc/ipc_message.h" |
| +#include "ipc/ipc_message_macros.h" |
| + |
| +DEFINE_WEB_CONTENTS_USER_DATA_KEY(page_load_metrics::WebContentsObserver); |
| + |
| +namespace page_load_metrics { |
| + |
| +namespace { |
| + |
| +// True if the timing structs have the same navigation start. |
| +bool PageLoadTimingIsChild(const PageLoadTiming& timing, |
| + const PageLoadTiming& parent) { |
| + return (parent.navigation_start.is_null() || |
| + timing.navigation_start == parent.navigation_start); |
| +} |
| + |
| +bool PageLoadTimingIsComplete(const PageLoadTiming& timing) { |
| + return !timing.navigation_start.is_null() && |
| + !timing.response_start.is_zero() && |
| + !timing.dom_content_loaded_event_start.is_zero() && |
| + !timing.load_event_start.is_zero() && |
| + !timing.first_layout.is_zero(); |
| +} |
| + |
| +bool PageLoadTimingIsEmpty(const PageLoadTiming& timing) { |
|
Bryan McQuade
2015/09/04 12:29:26
this one method actually seems reasonable to keep
Charlie Harrison
2015/09/04 15:19:40
Done.
|
| + return timing.navigation_start.is_null() && |
| + timing.response_start.is_zero() && |
| + timing.dom_content_loaded_event_start.is_zero() && |
| + timing.load_event_start.is_zero() && |
| + timing.first_layout.is_zero(); |
| +} |
| + |
| +// Checks guaranteed ordering for performance events. |
| +// Assumes IsComplete(). |
| +// Order: navigation_start => response_start => |
| +// dom_content_loaded_event_start => load_event_start |
| +bool PageLoadTimingIsOrdered(const PageLoadTiming& timing) { |
| + return !timing.response_start.is_zero() && |
| + timing.response_start < timing.dom_content_loaded_event_start && |
| + timing.dom_content_loaded_event_start < timing.load_event_start; |
| +} |
| + |
| +} // namespace |
| +WebContentsObserver::WebContentsObserver(content::WebContents* web_contents) |
| + : content::WebContentsObserver(web_contents) {} |
| + |
| +WebContentsObserver::~WebContentsObserver() { |
| + RecordTimingHistograms(); |
| +} |
| + |
| +bool WebContentsObserver::OnMessageReceived( |
| + const IPC::Message& message, |
| + content::RenderFrameHost* render_frame_host) { |
| + bool handled = true; |
| + IPC_BEGIN_MESSAGE_MAP(WebContentsObserver, message) |
| + IPC_MESSAGE_HANDLER(PageLoadMetricsMsg_TimingUpdated, OnTimingUpdated) |
| + IPC_MESSAGE_UNHANDLED(handled = false) |
| + IPC_END_MESSAGE_MAP() |
| + return handled; |
| +} |
| + |
| +void WebContentsObserver::DidCommitNavigation( |
| + content::NavigationHandle* navigation_handle) { |
| + if (IsRelevantNavigation(navigation_handle)) { |
| + RecordTimingHistograms(); |
|
Bryan McQuade
2015/09/04 12:29:26
I think we probably want to commit the histograms
Charlie Harrison
2015/09/04 15:19:40
yes! This is a mistake.
|
| + current_timing_ = PageLoadTiming(); |
| + current_navigation_ = navigation_handle; |
| + } |
| +} |
| + |
| +void WebContentsObserver::DidFinishNavigation( |
| + content::NavigationHandle* navigation_handle) { |
| + if (IsRelevantNavigation(navigation_handle)) { |
| + // TODO(csharrison) track aborts and log those histograms here |
| + DCHECK(navigation_handle == current_navigation_); |
| + current_navigation_ = nullptr; |
| + } |
| +} |
| + |
| +void WebContentsObserver::RenderProcessGone(base::TerminationStatus status) { |
| + RecordTimingHistograms(); |
|
Bryan McQuade
2015/09/04 12:29:26
you may also want to do current_timing_ = PageLoad
Charlie Harrison
2015/09/04 15:19:40
Done.
|
| +} |
| + |
| +#define PAGE_LOAD_HISTOGRAM(name, sample) \ |
| + UMA_HISTOGRAM_CUSTOM_TIMES(name, sample, \ |
| + base::TimeDelta::FromMilliseconds(10), \ |
| + base::TimeDelta::FromMinutes(10), 100); |
| + |
| +void WebContentsObserver::OnTimingUpdated(const PageLoadTiming& timing) { |
| + // See above comment about browser/renderer url disagreement (newtab). |
| + if (!GetLastCommittedURL().SchemeIsHTTPOrHTTPS()) |
| + return; |
| + |
| + if (PageLoadTimingIsChild(timing, current_timing_)) |
| + current_timing_ = timing; |
| +} |
| + |
| +void WebContentsObserver::RecordTimingHistograms() { |
| + if (PageLoadTimingIsEmpty(current_timing_)) |
| + return; |
| + else if (PageLoadTimingIsComplete(current_timing_)) |
| + DCHECK(PageLoadTimingIsOrdered(current_timing_)); |
| + |
| + if (!current_timing_.dom_content_loaded_event_start.is_zero()) { |
| + PAGE_LOAD_HISTOGRAM("PageLoad.Timing.DOMContentLoadedEventFired", |
| + current_timing_.dom_content_loaded_event_start); |
| + } |
| + |
| + if (!current_timing_.load_event_start.is_zero()) { |
| + PAGE_LOAD_HISTOGRAM("PageLoad.Timing.LoadEventFired", |
| + current_timing_.load_event_start); |
| + } |
| + |
| + if (!current_timing_.first_layout.is_zero()) { |
| + PAGE_LOAD_HISTOGRAM("PageLoad.Timing.FirstLayout", |
| + current_timing_.first_layout); |
| + } |
| +} |
| + |
| +bool WebContentsObserver::IsRelevantNavigation( |
| + content::NavigationHandle* navigation_handle) { |
| + // The url we see from the renderer side is not always the same as what |
| + // we see from the browser side (e.g. chrome://newtab). We wan't to be |
| + // sure here that we aren't logging UMA for internal pages |
| + const GURL& browser_url = GetLastCommittedURL(); |
| + return navigation_handle->IsInMainFrame() && |
| + navigation_handle->HasCommittedDocument() && |
| + navigation_handle->GetURL().SchemeIsHTTPOrHTTPS() && |
| + browser_url.SchemeIsHTTPOrHTTPS(); |
| +} |
| + |
| +const GURL& WebContentsObserver::GetLastCommittedURL() { |
| + return web_contents()->GetLastCommittedURL(); |
| +} |
| + |
| +} // namespace page_load_metrics |