Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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. | |
| 4 | |
| 5 #include "components/page_load_metrics/browser/metrics_web_contents_observer.h" | |
| 6 | |
| 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" | |
| 20 | |
| 21 DEFINE_WEB_CONTENTS_USER_DATA_KEY( | |
| 22 page_load_metrics::MetricsWebContentsObserver); | |
| 23 | |
| 24 namespace page_load_metrics { | |
| 25 | |
| 26 namespace { | |
| 27 | |
| 28 bool IsValidPageLoadTiming(const PageLoadTiming& timing) { | |
| 29 if (timing.IsEmpty()) | |
| 30 return false; | |
| 31 | |
| 32 // If we have a non-empty timing, it should always have a navigation start. | |
| 33 if (timing.navigation_start.is_null()) | |
| 34 return false; | |
| 35 | |
| 36 // If we have a dom content loaded event, we should have a response start. | |
| 37 if (!timing.dom_content_loaded_event_start.is_zero()) { | |
| 38 if (timing.response_start.is_zero() || | |
| 39 timing.dom_content_loaded_event_start < timing.response_start) { | |
| 40 return false; | |
| 41 } | |
| 42 } | |
| 43 | |
| 44 // If we have a load event, we should have both a response start and a DCL. | |
| 45 if (!timing.load_event_start.is_zero()) { | |
| 46 if (timing.response_start.is_zero() || | |
| 47 timing.dom_content_loaded_event_start.is_zero() || | |
| 48 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.
| |
| 49 return false; | |
| 50 } | |
| 51 } | |
| 52 | |
| 53 return true; | |
| 54 } | |
| 55 | |
| 56 } // namespace | |
| 57 MetricsWebContentsObserver::MetricsWebContentsObserver( | |
| 58 content::WebContents* web_contents) | |
| 59 : content::WebContentsObserver(web_contents) {} | |
| 60 | |
| 61 // As a tab helper, this object is tied to a single WebContent | |
| 62 // for its entire lifetime. | |
| 63 MetricsWebContentsObserver::~MetricsWebContentsObserver() { | |
| 64 RecordTimingHistograms(); | |
| 65 } | |
| 66 | |
| 67 bool MetricsWebContentsObserver::OnMessageReceived( | |
| 68 const IPC::Message& message, | |
| 69 content::RenderFrameHost* render_frame_host) { | |
| 70 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | |
| 71 bool handled = true; | |
| 72 IPC_BEGIN_MESSAGE_MAP_WITH_PARAM(MetricsWebContentsObserver, message, | |
| 73 render_frame_host) | |
| 74 IPC_MESSAGE_HANDLER(PageLoadMetricsMsg_TimingUpdated, OnTimingUpdated) | |
| 75 IPC_MESSAGE_UNHANDLED(handled = false) | |
| 76 IPC_END_MESSAGE_MAP() | |
| 77 return handled; | |
| 78 } | |
| 79 | |
| 80 void MetricsWebContentsObserver::DidCommitNavigation( | |
| 81 content::NavigationHandle* navigation_handle) { | |
| 82 if (navigation_handle->IsInMainFrame() && !navigation_handle->IsSamePage()) | |
| 83 RecordTimingHistograms(); | |
| 84 if (IsRelevantNavigation(navigation_handle)) { | |
| 85 current_timing_.reset(new PageLoadTiming()); | |
| 86 } | |
| 87 } | |
| 88 | |
| 89 // This will occur when the process for the main RenderFrameHost exits. | |
| 90 // This will happen with a normal exit or a crash. | |
| 91 void MetricsWebContentsObserver::RenderProcessGone( | |
| 92 base::TerminationStatus status) { | |
| 93 RecordTimingHistograms(); | |
| 94 } | |
| 95 | |
| 96 #define PAGE_LOAD_HISTOGRAM(name, sample) \ | |
| 97 UMA_HISTOGRAM_CUSTOM_TIMES(name, sample, \ | |
| 98 base::TimeDelta::FromMilliseconds(10), \ | |
| 99 base::TimeDelta::FromMinutes(10), 100); | |
| 100 | |
| 101 void MetricsWebContentsObserver::OnTimingUpdated( | |
| 102 content::RenderFrameHost* render_frame_host, | |
| 103 const PageLoadTiming& timing) { | |
| 104 if (!current_timing_) | |
| 105 return; | |
| 106 | |
| 107 // We may receive notifications from frames that have been navigated away | |
| 108 // from. We simply ignore them. | |
| 109 if (!IsCurrentMainFrame(render_frame_host)) | |
| 110 return; | |
| 111 | |
| 112 // See comment in IsRelevantNavigation about | |
| 113 // browser/renderer url disagreement (newtab). | |
| 114 if (!GetLastCommittedURL().SchemeIsHTTPOrHTTPS()) | |
| 115 return; | |
| 116 | |
| 117 // Throw away IPCs that are not relevant to the current navigation. | |
| 118 if (!current_timing_->navigation_start.is_null() && | |
| 119 timing.navigation_start != current_timing_->navigation_start) { | |
| 120 // TODO(csharrison) uma log a counter here | |
| 121 return; | |
| 122 } | |
| 123 | |
| 124 if (current_timing_->IsEmpty()) | |
| 125 current_host_ = render_frame_host; | |
| 126 else | |
| 127 DCHECK(render_frame_host == current_host_); | |
| 128 *current_timing_ = timing; | |
| 129 } | |
| 130 | |
| 131 void MetricsWebContentsObserver::RecordTimingHistograms() { | |
| 132 if (!current_timing_ || !IsValidPageLoadTiming(*current_timing_)) | |
| 133 return; | |
| 134 | |
| 135 if (!current_timing_->dom_content_loaded_event_start.is_zero()) { | |
| 136 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.
| |
| 137 current_timing_->dom_content_loaded_event_start); | |
| 138 } | |
| 139 | |
| 140 if (!current_timing_->load_event_start.is_zero()) { | |
| 141 PAGE_LOAD_HISTOGRAM("PageLoad.Timing.LoadEventFired", | |
|
Bryan McQuade
2015/09/10 00:45:58
same
| |
| 142 current_timing_->load_event_start); | |
| 143 } | |
| 144 | |
| 145 if (!current_timing_->first_layout.is_zero()) { | |
| 146 PAGE_LOAD_HISTOGRAM("PageLoad.Timing.FirstLayout", | |
|
Bryan McQuade
2015/09/10 00:45:58
same
| |
| 147 current_timing_->first_layout); | |
| 148 } | |
| 149 current_timing_.reset(); | |
| 150 } | |
| 151 | |
| 152 bool MetricsWebContentsObserver::IsRelevantNavigation( | |
| 153 content::NavigationHandle* navigation_handle) { | |
| 154 // The url we see from the renderer side is not always the same as what | |
| 155 // we see from the browser side (e.g. chrome://newtab). We wan't to be | |
| 156 // sure here that we aren't logging UMA for internal pages | |
| 157 const GURL& browser_url = GetLastCommittedURL(); | |
| 158 return navigation_handle->IsInMainFrame() && | |
| 159 !navigation_handle->IsSamePage() && | |
| 160 navigation_handle->HasCommittedDocument() && | |
| 161 navigation_handle->GetURL().SchemeIsHTTPOrHTTPS() && | |
| 162 browser_url.SchemeIsHTTPOrHTTPS(); | |
| 163 } | |
| 164 | |
| 165 const GURL& MetricsWebContentsObserver::GetLastCommittedURL() { | |
| 166 return web_contents()->GetLastCommittedURL(); | |
| 167 } | |
| 168 | |
| 169 bool MetricsWebContentsObserver::IsCurrentMainFrame( | |
| 170 content::RenderFrameHost* render_frame_host) { | |
| 171 return render_frame_host == web_contents()->GetMainFrame(); | |
| 172 } | |
| 173 | |
| 174 } // namespace page_load_metrics | |
| OLD | NEW |