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..dcd1494b2007e5f80420fee898c4123b0e17e588 |
--- /dev/null |
+++ b/components/page_load_metrics/browser/page_load_metrics_web_contents_observer.cc |
@@ -0,0 +1,158 @@ |
+// 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/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::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(); |
+} |
+ |
+// 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(); |
Bryan McQuade
2015/09/04 20:53:17
let's add a short comment here to note the cases w
Charlie Harrison
2015/09/08 23:05:15
Done.
|
+} |
+ |
+bool WebContentsObserver::OnMessageReceived( |
+ const IPC::Message& message, |
+ content::RenderFrameHost* render_frame_host) { |
+ DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
+ return HandleMessageReceived(message, render_frame_host); |
+} |
+ |
+void WebContentsObserver::DidCommitNavigation( |
+ content::NavigationHandle* navigation_handle) { |
+ RecordTimingHistograms(); |
Bryan McQuade
2015/09/04 20:53:17
we only want to RecordTimingHistograms() if this i
Charlie Harrison
2015/09/08 23:05:15
Done.
|
+ if (IsRelevantNavigation(navigation_handle)) { |
+ current_timing_.reset(new PageLoadTiming()); |
+ current_navigation_ = navigation_handle; |
Bryan McQuade
2015/09/04 20:53:17
looks like we're no longer using current_navigatio
Charlie Harrison
2015/09/08 23:05:15
Done.
|
+ } |
+} |
+ |
+void WebContentsObserver::RenderProcessGone(base::TerminationStatus status) { |
+ RecordTimingHistograms(); |
Bryan McQuade
2015/09/04 20:53:17
let's add a short comment here to note where this
Charlie Harrison
2015/09/08 23:05:15
Done.
|
+} |
+ |
+bool WebContentsObserver::HandleMessageReceived( |
Bryan McQuade
2015/09/04 20:53:17
minor suggestion: even though it makes the method
Charlie Harrison
2015/09/08 23:05:15
Done.
|
+ const IPC::Message& message, |
+ content::RenderFrameHost* render_frame_host) { |
+ bool handled = true; |
+ IPC_BEGIN_MESSAGE_MAP_WITH_PARAM(WebContentsObserver, message, |
+ render_frame_host) |
+ IPC_MESSAGE_HANDLER(PageLoadMetricsMsg_TimingUpdated, |
+ OnTimingUpdated) |
+ IPC_MESSAGE_UNHANDLED(handled = false) |
+ IPC_END_MESSAGE_MAP() |
+ return handled; |
+} |
+ |
+#define PAGE_LOAD_HISTOGRAM(name, sample) \ |
+ UMA_HISTOGRAM_CUSTOM_TIMES(name, sample, \ |
+ base::TimeDelta::FromMilliseconds(10), \ |
+ base::TimeDelta::FromMinutes(10), 100); |
+ |
+void WebContentsObserver::OnTimingUpdated( |
+ content::RenderFrameHost* render_frame_host, |
+ const PageLoadTiming& timing) { |
+ if (!current_timing_) |
+ return; |
+ |
+ // See above comment about browser/renderer url disagreement (newtab). |
Bryan McQuade
2015/09/04 20:53:17
can we add:
// We may receive notifications from f
Bryan McQuade
2015/09/04 20:53:17
maybe change to "See comment in IsRelevantNavigati
|
+ if (!GetLastCommittedURL().SchemeIsHTTPOrHTTPS()) |
+ return; |
+ |
+ // Throw away IPCs that are not relevant to the current navigation. |
+ if (PageLoadTimingIsChild(timing, *current_timing_)) { |
Bryan McQuade
2015/09/04 20:53:17
given that this is the only place we do this 'is c
|
+ if (current_timing_->IsEmpty()) |
+ current_host_ = render_frame_host; |
Bryan McQuade
2015/09/04 20:53:17
seems like the canonical source for the current re
Charlie Harrison
2015/09/08 23:05:14
Which side of the equality would you propose chang
|
+ else |
+ DCHECK(render_frame_host == current_host_); |
Bryan McQuade
2015/09/04 20:53:17
IIRC creis said that this can actually happen some
Charlie Harrison
2015/09/08 23:05:15
Shouldn't that be handled by the !IsCurrentMainFra
|
+ current_timing_.reset(new PageLoadTiming(timing)); |
Bryan McQuade
2015/09/04 20:53:17
can you simplify to:
*current_timing_ = timing;
Charlie Harrison
2015/09/08 23:05:15
Done.
|
+ } |
+} |
+ |
+void WebContentsObserver::RecordTimingHistograms() { |
+ if (!current_timing_ || current_timing_->IsEmpty()) |
+ return; |
+ else if (PageLoadTimingIsComplete(*current_timing_)) |
Bryan McQuade
2015/09/04 20:53:17
testing validiting of the PageLoadHistograms seems
Bryan McQuade
2015/09/08 14:09:46
oh, and I forgot, we should also test ordering bet
Charlie Harrison
2015/09/08 23:05:15
Is a PageLoadTiming with null nav_start really val
|
+ 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); |
+ } |
+ current_timing_.reset(); |
+} |
+ |
+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->IsSamePage() && |
+ navigation_handle->HasCommittedDocument() && |
+ navigation_handle->GetURL().SchemeIsHTTPOrHTTPS() && |
+ browser_url.SchemeIsHTTPOrHTTPS(); |
+} |
+ |
+const GURL& WebContentsObserver::GetLastCommittedURL() { |
+ return web_contents()->GetLastCommittedURL(); |
+} |
+ |
+} // namespace page_load_metrics |