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 DCHECK(!timing.navigation_start.is_null()); |
| 34 |
| 35 // If we have a DOM content loaded event, we should have a response start. |
| 36 DCHECK_IMPLIES( |
| 37 !timing.dom_content_loaded_event_start.is_zero(), |
| 38 !timing.response_start.is_zero() && |
| 39 timing.response_start <= timing.dom_content_loaded_event_start); |
| 40 |
| 41 // If we have a load event, we should have both a response start and a DCL. |
| 42 DCHECK_IMPLIES( |
| 43 !timing.load_event_start.is_zero(), |
| 44 !timing.dom_content_loaded_event_start.is_zero() && |
| 45 timing.response_start <= timing.load_event_start && |
| 46 timing.dom_content_loaded_event_start <= timing.load_event_start); |
| 47 |
| 48 return true; |
| 49 } |
| 50 |
| 51 } // namespace |
| 52 |
| 53 MetricsWebContentsObserver::MetricsWebContentsObserver( |
| 54 content::WebContents* web_contents) |
| 55 : content::WebContentsObserver(web_contents) {} |
| 56 |
| 57 // This object is tied to a single WebContents for its entire lifetime. |
| 58 MetricsWebContentsObserver::~MetricsWebContentsObserver() { |
| 59 RecordTimingHistograms(); |
| 60 } |
| 61 |
| 62 bool MetricsWebContentsObserver::OnMessageReceived( |
| 63 const IPC::Message& message, |
| 64 content::RenderFrameHost* render_frame_host) { |
| 65 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 66 bool handled = true; |
| 67 IPC_BEGIN_MESSAGE_MAP_WITH_PARAM(MetricsWebContentsObserver, message, |
| 68 render_frame_host) |
| 69 IPC_MESSAGE_HANDLER(PageLoadMetricsMsg_TimingUpdated, OnTimingUpdated) |
| 70 IPC_MESSAGE_UNHANDLED(handled = false) |
| 71 IPC_END_MESSAGE_MAP() |
| 72 return handled; |
| 73 } |
| 74 |
| 75 void MetricsWebContentsObserver::DidCommitNavigation( |
| 76 content::NavigationHandle* navigation_handle) { |
| 77 if (navigation_handle->IsInMainFrame() && !navigation_handle->IsSamePage()) |
| 78 RecordTimingHistograms(); |
| 79 if (IsRelevantNavigation(navigation_handle)) |
| 80 current_timing_.reset(new PageLoadTiming()); |
| 81 } |
| 82 |
| 83 // This will occur when the process for the main RenderFrameHost exits. |
| 84 // This will happen with a normal exit or a crash. |
| 85 void MetricsWebContentsObserver::RenderProcessGone( |
| 86 base::TerminationStatus status) { |
| 87 RecordTimingHistograms(); |
| 88 } |
| 89 |
| 90 #define PAGE_LOAD_HISTOGRAM(name, sample) \ |
| 91 UMA_HISTOGRAM_CUSTOM_TIMES(name, sample, \ |
| 92 base::TimeDelta::FromMilliseconds(10), \ |
| 93 base::TimeDelta::FromMinutes(10), 100); |
| 94 |
| 95 void MetricsWebContentsObserver::OnTimingUpdated( |
| 96 content::RenderFrameHost* render_frame_host, |
| 97 const PageLoadTiming& timing) { |
| 98 if (!current_timing_) |
| 99 return; |
| 100 |
| 101 // We may receive notifications from frames that have been navigated away |
| 102 // from. We simply ignore them. |
| 103 if (render_frame_host != web_contents()->GetMainFrame()) |
| 104 return; |
| 105 |
| 106 // For urls like chrome://newtab, the renderer and browser disagree, |
| 107 // so we have to double check that the renderer isn't sending data from a |
| 108 // bad url like https://www.google.com/_/chrome/newtab. |
| 109 if (!web_contents()->GetLastCommittedURL().SchemeIsHTTPOrHTTPS()) |
| 110 return; |
| 111 |
| 112 // Throw away IPCs that are not relevant to the current navigation. |
| 113 if (!current_timing_->navigation_start.is_null() && |
| 114 timing.navigation_start != current_timing_->navigation_start) { |
| 115 // TODO(csharrison) uma log a counter here |
| 116 return; |
| 117 } |
| 118 |
| 119 *current_timing_ = timing; |
| 120 } |
| 121 |
| 122 void MetricsWebContentsObserver::RecordTimingHistograms() { |
| 123 if (!current_timing_ || !IsValidPageLoadTiming(*current_timing_)) |
| 124 return; |
| 125 |
| 126 if (!current_timing_->dom_content_loaded_event_start.is_zero()) { |
| 127 PAGE_LOAD_HISTOGRAM( |
| 128 "PageLoad.Timing.Navigation.To.DOMContentLoadedEventFired", |
| 129 current_timing_->dom_content_loaded_event_start); |
| 130 } |
| 131 |
| 132 if (!current_timing_->load_event_start.is_zero()) { |
| 133 PAGE_LOAD_HISTOGRAM("PageLoad.Timing.Navigation.To.LoadEventFired", |
| 134 current_timing_->load_event_start); |
| 135 } |
| 136 |
| 137 if (!current_timing_->first_layout.is_zero()) { |
| 138 PAGE_LOAD_HISTOGRAM("PageLoad.Timing.Navigation.To.FirstLayout", |
| 139 current_timing_->first_layout); |
| 140 } |
| 141 current_timing_.reset(); |
| 142 } |
| 143 |
| 144 bool MetricsWebContentsObserver::IsRelevantNavigation( |
| 145 content::NavigationHandle* navigation_handle) { |
| 146 // The url we see from the renderer side is not always the same as what |
| 147 // we see from the browser side (e.g. chrome://newtab). We want to be |
| 148 // sure here that we aren't logging UMA for internal pages. |
| 149 const GURL& browser_url = web_contents()->GetLastCommittedURL(); |
| 150 return navigation_handle->IsInMainFrame() && |
| 151 !navigation_handle->IsSamePage() && |
| 152 navigation_handle->HasCommittedDocument() && |
| 153 navigation_handle->GetURL().SchemeIsHTTPOrHTTPS() && |
| 154 browser_url.SchemeIsHTTPOrHTTPS(); |
| 155 } |
| 156 |
| 157 } // namespace page_load_metrics |
OLD | NEW |