Chromium Code Reviews| Index: components/page_load_metrics/browser/metrics_web_contents_observer.cc |
| diff --git a/components/page_load_metrics/browser/metrics_web_contents_observer.cc b/components/page_load_metrics/browser/metrics_web_contents_observer.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..649dd9247137748cd9618c841b73bfb2474c3501 |
| --- /dev/null |
| +++ b/components/page_load_metrics/browser/metrics_web_contents_observer.cc |
| @@ -0,0 +1,174 @@ |
| +// 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/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/browser_thread.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::MetricsWebContentsObserver); |
| + |
| +namespace page_load_metrics { |
| + |
| +namespace { |
| + |
| +bool IsValidPageLoadTiming(const PageLoadTiming& timing) { |
| + if (timing.IsEmpty()) |
| + return false; |
| + |
| + // If we have a non-empty timing, it should always have a navigation start. |
| + if (timing.navigation_start.is_null()) |
| + return false; |
| + |
| + // If we have a dom content loaded event, we should have a response start. |
| + if (!timing.dom_content_loaded_event_start.is_zero()) { |
| + if (timing.response_start.is_zero() || |
| + timing.dom_content_loaded_event_start < timing.response_start) { |
| + return false; |
| + } |
| + } |
| + |
| + // If we have a load event, we should have both a response start and a DCL. |
| + if (!timing.load_event_start.is_zero()) { |
| + if (timing.response_start.is_zero() || |
| + timing.dom_content_loaded_event_start.is_zero() || |
| + timing.dom_content_loaded_event_start < timing.response_start) { |
|
Bryan McQuade
2015/09/10 00:45:58
this probably wants to be:
timing.load_event_start
Bryan McQuade
2015/09/10 00:45:58
for completeness, we could also add:
timing.load_e
Charlie Harrison
2015/09/10 19:22:41
Done.
Charlie Harrison
2015/09/10 19:22:41
Done.
|
| + return false; |
| + } |
| + } |
| + |
| + return true; |
| +} |
| + |
| +} // namespace |
| +MetricsWebContentsObserver::MetricsWebContentsObserver( |
| + content::WebContents* web_contents) |
| + : content::WebContentsObserver(web_contents) {} |
| + |
| +// As a tab helper, this object is tied to a single WebContent |
| +// for its entire lifetime. |
| +MetricsWebContentsObserver::~MetricsWebContentsObserver() { |
| + RecordTimingHistograms(); |
| +} |
| + |
| +bool MetricsWebContentsObserver::OnMessageReceived( |
| + const IPC::Message& message, |
| + content::RenderFrameHost* render_frame_host) { |
| + DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| + bool handled = true; |
| + IPC_BEGIN_MESSAGE_MAP_WITH_PARAM(MetricsWebContentsObserver, message, |
| + render_frame_host) |
| + IPC_MESSAGE_HANDLER(PageLoadMetricsMsg_TimingUpdated, OnTimingUpdated) |
| + IPC_MESSAGE_UNHANDLED(handled = false) |
| + IPC_END_MESSAGE_MAP() |
| + return handled; |
| +} |
| + |
| +void MetricsWebContentsObserver::DidCommitNavigation( |
| + content::NavigationHandle* navigation_handle) { |
| + if (navigation_handle->IsInMainFrame() && !navigation_handle->IsSamePage()) |
| + RecordTimingHistograms(); |
| + if (IsRelevantNavigation(navigation_handle)) { |
| + current_timing_.reset(new PageLoadTiming()); |
| + } |
| +} |
| + |
| +// This will occur when the process for the main RenderFrameHost exits. |
| +// This will happen with a normal exit or a crash. |
| +void MetricsWebContentsObserver::RenderProcessGone( |
| + base::TerminationStatus status) { |
| + RecordTimingHistograms(); |
| +} |
| + |
| +#define PAGE_LOAD_HISTOGRAM(name, sample) \ |
| + UMA_HISTOGRAM_CUSTOM_TIMES(name, sample, \ |
| + base::TimeDelta::FromMilliseconds(10), \ |
| + base::TimeDelta::FromMinutes(10), 100); |
| + |
| +void MetricsWebContentsObserver::OnTimingUpdated( |
| + content::RenderFrameHost* render_frame_host, |
| + const PageLoadTiming& timing) { |
| + if (!current_timing_) |
| + return; |
| + |
| + // We may receive notifications from frames that have been navigated away |
| + // from. We simply ignore them. |
| + if (!IsCurrentMainFrame(render_frame_host)) |
| + return; |
| + |
| + // See comment in IsRelevantNavigation about |
| + // browser/renderer url disagreement (newtab). |
| + if (!GetLastCommittedURL().SchemeIsHTTPOrHTTPS()) |
| + return; |
| + |
| + // Throw away IPCs that are not relevant to the current navigation. |
| + if (!current_timing_->navigation_start.is_null() && |
| + timing.navigation_start != current_timing_->navigation_start) { |
| + // TODO(csharrison) uma log a counter here |
| + return; |
| + } |
| + |
| + if (current_timing_->IsEmpty()) |
| + current_host_ = render_frame_host; |
| + else |
| + DCHECK(render_frame_host == current_host_); |
| + *current_timing_ = timing; |
| +} |
| + |
| +void MetricsWebContentsObserver::RecordTimingHistograms() { |
| + if (!current_timing_ || !IsValidPageLoadTiming(*current_timing_)) |
| + return; |
| + |
| + if (!current_timing_->dom_content_loaded_event_start.is_zero()) { |
| + PAGE_LOAD_HISTOGRAM("PageLoad.Timing.DOMContentLoadedEventFired", |
|
Bryan McQuade
2015/09/10 00:45:58
Thinking about UMA names more, I anticipate that w
Charlie Harrison
2015/09/10 19:22:41
Done.
|
| + current_timing_->dom_content_loaded_event_start); |
| + } |
| + |
| + if (!current_timing_->load_event_start.is_zero()) { |
| + PAGE_LOAD_HISTOGRAM("PageLoad.Timing.LoadEventFired", |
|
Bryan McQuade
2015/09/10 00:45:58
same
|
| + current_timing_->load_event_start); |
| + } |
| + |
| + if (!current_timing_->first_layout.is_zero()) { |
| + PAGE_LOAD_HISTOGRAM("PageLoad.Timing.FirstLayout", |
|
Bryan McQuade
2015/09/10 00:45:58
same
|
| + current_timing_->first_layout); |
| + } |
| + current_timing_.reset(); |
| +} |
| + |
| +bool MetricsWebContentsObserver::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->IsSamePage() && |
| + navigation_handle->HasCommittedDocument() && |
| + navigation_handle->GetURL().SchemeIsHTTPOrHTTPS() && |
| + browser_url.SchemeIsHTTPOrHTTPS(); |
| +} |
| + |
| +const GURL& MetricsWebContentsObserver::GetLastCommittedURL() { |
| + return web_contents()->GetLastCommittedURL(); |
| +} |
| + |
| +bool MetricsWebContentsObserver::IsCurrentMainFrame( |
| + content::RenderFrameHost* render_frame_host) { |
| + return render_frame_host == web_contents()->GetMainFrame(); |
| +} |
| + |
| +} // namespace page_load_metrics |