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..924a9429e3439eee0d93f78d02f00780d349d160 |
| --- /dev/null |
| +++ b/components/page_load_metrics/browser/page_load_metrics_web_contents_observer.cc |
| @@ -0,0 +1,107 @@ |
| +// 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 { |
| + |
| +WebContentsObserver::WebContentsObserver(content::WebContents* web_contents) |
| + : content::WebContentsObserver(web_contents) {} |
| + |
| +WebContentsObserver::~WebContentsObserver() { |
| + RecordTimingHistograms(); |
|
Bryan McQuade
2015/09/02 18:26:45
can we add a short comment here noting why it's co
Charlie Harrison
2015/09/03 14:00:52
This gets invoked when a tab is closed, and when w
|
| +} |
| + |
| +bool WebContentsObserver::OnMessageReceived( |
| + const IPC::Message& message, |
| + content::RenderFrameHost* render_frame_host) { |
| + bool handled = true; |
|
Bryan McQuade
2015/09/02 18:26:45
just to sanity check, can we add a DCHECK that thi
Bryan McQuade
2015/09/02 18:38:31
actually, if you confirm that this does get invoke
Charlie Harrison
2015/09/03 14:00:52
Let's add a DCHECK. Do you know an easy way to see
Bryan McQuade
2015/09/03 20:23:37
I think you can do:
DCHECK(BrowserThread::Currentl
|
| + 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 (!navigation_handle->IsInMainFrame()) |
| + return; |
| + |
| + if (!navigation_handle->HasCommittedDocument()) |
| + return; |
| + |
| + const GURL& url = navigation_handle->GetURL(); |
| + const GURL& browser_url = GetLastCommittedURL(); |
| + if (!url.SchemeIsHTTPOrHTTPS() && !browser_url.SchemeIsHTTPOrHTTPS()) |
|
Bryan McQuade
2015/09/02 18:26:45
does this want to be || rather than &&?
in partic
Charlie Harrison
2015/09/03 14:00:52
Done. Will draft a bug soon.
|
| + return; |
| + RecordTimingHistograms(); |
| + current_timing_ = pending_timing_; |
| + pending_timing_ = PageLoadTiming(); |
| +} |
| + |
| +void WebContentsObserver::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 WebContentsObserver::OnTimingUpdated(const PageLoadTiming& timing) { |
| + if (!GetLastCommittedURL().SchemeIsHTTPOrHTTPS()) |
|
Bryan McQuade
2015/09/02 18:26:45
let's add a short comment here to note the specifi
Charlie Harrison
2015/09/03 14:00:52
Done.
|
| + return; |
| + |
| + if (timing.IsChild(current_timing_)) { |
|
Bryan McQuade
2015/09/02 18:26:45
in general (throughout this change), you can remov
|
| + current_timing_ = timing; |
| + } else if (timing.IsChild(pending_timing_)) { |
|
Bryan McQuade
2015/09/02 18:26:45
I'm not following what's going on with these if/el
Charlie Harrison
2015/09/03 14:00:52
I think I'm going to remove the pending_timing fol
|
| + pending_timing_ = timing; |
| + } else { |
| + NOTREACHED(); |
| + } |
| +} |
| + |
| +void WebContentsObserver::RecordTimingHistograms() { |
| + PageLoadTiming timing = current_timing_; |
|
Bryan McQuade
2015/09/02 18:26:45
I don't think you need to take a function-scoped c
Charlie Harrison
2015/09/03 14:00:52
Done.
|
| + if (timing.IsComplete()) |
| + DCHECK(timing.IsOrdered()); |
| + else if (timing.IsEmpty()) |
|
Bryan McQuade
2015/09/02 18:26:45
nit: I'd do the test for timing.IsEmpty() before t
Charlie Harrison
2015/09/03 14:00:52
Done.
|
| + return; |
| + |
| + if (!timing.dom_content_loaded_event_start.is_zero()) { |
| + PAGE_LOAD_HISTOGRAM("PageLoad.Timing.DOMContentLoadedEventFired", |
| + timing.dom_content_loaded_event_start); |
| + } |
| + |
| + if (!timing.load_event_start.is_zero()) { |
| + PAGE_LOAD_HISTOGRAM("PageLoad.Timing.LoadEventFired", |
| + timing.load_event_start); |
| + } |
| + |
| + if (!timing.first_layout.is_zero()) { |
| + PAGE_LOAD_HISTOGRAM("PageLoad.Timing.FirstLayout", timing.first_layout); |
| + } |
| +} |
| + |
| +const GURL& WebContentsObserver::GetLastCommittedURL() { |
|
Bryan McQuade
2015/09/02 18:26:45
unless you need to override the implementation of
Charlie Harrison
2015/09/02 18:38:24
The reason is so we don't have to mock an entire W
Bryan McQuade
2015/09/02 18:40:27
Ok, that seems like a good reason - in that case y
Bryan McQuade
2015/09/03 12:04:29
Actually, it's been a while since I used private v
|
| + return web_contents()->GetLastCommittedURL(); |
| +} |
| + |
| +} // namespace page_load_metrics |