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