Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "components/page_load_metrics/browser/metrics_web_contents_observer.h" | 5 #include "components/page_load_metrics/browser/metrics_web_contents_observer.h" |
| 6 | 6 |
| 7 #include "base/logging.h" | 7 #include "base/logging.h" |
| 8 #include "base/metrics/histogram.h" | 8 #include "base/metrics/histogram.h" |
| 9 #include "components/page_load_metrics/common/page_load_metrics_messages.h" | 9 #include "components/page_load_metrics/common/page_load_metrics_messages.h" |
| 10 #include "components/page_load_metrics/common/page_load_timing.h" | 10 #include "components/page_load_metrics/common/page_load_timing.h" |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 42 !timing.load_event_start.is_zero(), | 42 !timing.load_event_start.is_zero(), |
| 43 !timing.dom_content_loaded_event_start.is_zero() && | 43 !timing.dom_content_loaded_event_start.is_zero() && |
| 44 timing.response_start <= timing.load_event_start && | 44 timing.response_start <= timing.load_event_start && |
| 45 timing.dom_content_loaded_event_start <= timing.load_event_start); | 45 timing.dom_content_loaded_event_start <= timing.load_event_start); |
| 46 | 46 |
| 47 return true; | 47 return true; |
| 48 } | 48 } |
| 49 | 49 |
| 50 } // namespace | 50 } // namespace |
| 51 | 51 |
| 52 #define PAGE_LOAD_HISTOGRAM(name, sample) \ | |
| 53 UMA_HISTOGRAM_CUSTOM_TIMES(name, sample, \ | |
| 54 base::TimeDelta::FromMilliseconds(10), \ | |
| 55 base::TimeDelta::FromMinutes(10), 100); | |
|
Alexei Svitkine (slow)
2015/09/28 18:27:21
Nit: No ;
The caller will supply their own after
| |
| 56 | |
| 57 PageLoadTracker::PageLoadTracker(bool in_foreground) | |
| 58 : has_commit_(false), | |
| 59 has_finished_(false), | |
| 60 page_load_stayed_in_foreground_(in_foreground) {} | |
| 61 | |
| 62 PageLoadTracker::~PageLoadTracker() { | |
| 63 if (has_commit_) | |
| 64 RecordTimingHistograms(); | |
| 65 } | |
| 66 | |
| 67 void PageLoadTracker::WebContentsHidden() { | |
| 68 if (!has_finished_) | |
| 69 page_load_stayed_in_foreground_ = false; | |
| 70 } | |
| 71 | |
| 72 void PageLoadTracker::Commit() { | |
| 73 has_commit_ = true; | |
| 74 } | |
| 75 | |
| 76 void PageLoadTracker::Finish() { | |
| 77 has_finished_ = true; | |
| 78 } | |
| 79 | |
| 80 bool PageLoadTracker::UpdateTiming(const PageLoadTiming& timing) { | |
| 81 // Throw away IPCs that are not relevant to the current navigation. | |
| 82 if (!timing_.navigation_start.is_null() && | |
| 83 timing_.navigation_start != timing.navigation_start) { | |
| 84 // TODO(csharrison) uma log a counter here | |
| 85 return false; | |
| 86 } | |
| 87 if (IsValidPageLoadTiming(timing)) { | |
| 88 timing_ = timing; | |
| 89 return true; | |
| 90 } | |
| 91 return false; | |
| 92 } | |
| 93 | |
| 94 void PageLoadTracker::RecordTimingHistograms() { | |
| 95 DCHECK(has_commit_); | |
| 96 if (!timing_.dom_content_loaded_event_start.is_zero()) { | |
| 97 if (page_load_stayed_in_foreground_) { | |
| 98 PAGE_LOAD_HISTOGRAM( | |
| 99 "PageLoad.Timing.NavigationToDOMContentLoadedEventFired", | |
| 100 timing_.dom_content_loaded_event_start); | |
| 101 } else { | |
| 102 PAGE_LOAD_HISTOGRAM( | |
| 103 "PageLoad.Timing.BG.NavigationToDOMContentLoadedEventFired", | |
| 104 timing_.dom_content_loaded_event_start); | |
| 105 } | |
| 106 } | |
| 107 if (!timing_.load_event_start.is_zero()) { | |
| 108 if (page_load_stayed_in_foreground_) { | |
| 109 PAGE_LOAD_HISTOGRAM("PageLoad.Timing.NavigationToLoadEventFired", | |
| 110 timing_.load_event_start); | |
| 111 } else { | |
| 112 PAGE_LOAD_HISTOGRAM("PageLoad.Timing.BG.NavigationToLoadEventFired", | |
| 113 timing_.load_event_start); | |
| 114 } | |
| 115 } | |
| 116 if (!timing_.first_layout.is_zero()) { | |
| 117 if (page_load_stayed_in_foreground_) { | |
| 118 PAGE_LOAD_HISTOGRAM("PageLoad.Timing.NavigationToFirstLayout", | |
| 119 timing_.first_layout); | |
| 120 } else { | |
| 121 PAGE_LOAD_HISTOGRAM("PageLoad.Timing.BG.NavigationToFirstLayout", | |
| 122 timing_.first_layout); | |
| 123 } | |
| 124 } | |
| 125 } | |
| 126 | |
| 52 MetricsWebContentsObserver::MetricsWebContentsObserver( | 127 MetricsWebContentsObserver::MetricsWebContentsObserver( |
| 53 content::WebContents* web_contents) | 128 content::WebContents* web_contents) |
| 54 : content::WebContentsObserver(web_contents) {} | 129 : content::WebContentsObserver(web_contents), in_foreground_(false) {} |
| 55 | 130 |
| 56 // This object is tied to a single WebContents for its entire lifetime. | 131 MetricsWebContentsObserver::~MetricsWebContentsObserver() {} |
| 57 MetricsWebContentsObserver::~MetricsWebContentsObserver() { | |
| 58 RecordTimingHistograms(); | |
| 59 } | |
| 60 | 132 |
| 61 bool MetricsWebContentsObserver::OnMessageReceived( | 133 bool MetricsWebContentsObserver::OnMessageReceived( |
| 62 const IPC::Message& message, | 134 const IPC::Message& message, |
| 63 content::RenderFrameHost* render_frame_host) { | 135 content::RenderFrameHost* render_frame_host) { |
| 64 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 136 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 65 bool handled = true; | 137 bool handled = true; |
| 66 IPC_BEGIN_MESSAGE_MAP_WITH_PARAM(MetricsWebContentsObserver, message, | 138 IPC_BEGIN_MESSAGE_MAP_WITH_PARAM(MetricsWebContentsObserver, message, |
| 67 render_frame_host) | 139 render_frame_host) |
| 68 IPC_MESSAGE_HANDLER(PageLoadMetricsMsg_TimingUpdated, OnTimingUpdated) | 140 IPC_MESSAGE_HANDLER(PageLoadMetricsMsg_TimingUpdated, OnTimingUpdated) |
| 69 IPC_MESSAGE_UNHANDLED(handled = false) | 141 IPC_MESSAGE_UNHANDLED(handled = false) |
| 70 IPC_END_MESSAGE_MAP() | 142 IPC_END_MESSAGE_MAP() |
| 71 return handled; | 143 return handled; |
| 72 } | 144 } |
| 73 | 145 |
| 74 void MetricsWebContentsObserver::DidFinishNavigation( | 146 void MetricsWebContentsObserver::DidFinishNavigation( |
| 75 content::NavigationHandle* navigation_handle) { | 147 content::NavigationHandle* navigation_handle) { |
| 148 if (!navigation_handle->IsInMainFrame()) | |
| 149 return; | |
| 150 | |
| 151 scoped_ptr<PageLoadTracker> finished_nav( | |
| 152 provisional_loads_.take_and_erase(navigation_handle)); | |
| 153 | |
| 154 // TODO(csharrison) handle the two error cases: | |
| 155 // 1. Error pages that replace the previous page. | |
| 156 // 2. Error pages that leave the user on the previous page. | |
| 157 // For now these cases will be ignored. | |
| 76 if (!navigation_handle->HasCommitted()) | 158 if (!navigation_handle->HasCommitted()) |
| 77 return; | 159 return; |
| 78 if (navigation_handle->IsInMainFrame() && !navigation_handle->IsSamePage()) | 160 |
| 79 RecordTimingHistograms(); | 161 if (!IsRelevantNavigation(navigation_handle)) |
| 80 if (IsRelevantNavigation(navigation_handle)) | 162 return; |
| 81 current_timing_.reset(new PageLoadTiming()); | 163 |
| 164 // Updating |committed_load_| will trigger RecordTimingHistograms on a | |
| 165 // previous PageLoadTracker. | |
| 166 committed_load_ = finished_nav.Pass(); | |
| 167 DCHECK(committed_load_); | |
| 168 committed_load_->Commit(); | |
| 82 } | 169 } |
| 83 | 170 |
| 84 // This will occur when the process for the main RenderFrameHost exits. | 171 void MetricsWebContentsObserver::DidStartNavigation( |
| 85 // This will happen with a normal exit or a crash. | 172 content::NavigationHandle* navigation_handle) { |
| 86 void MetricsWebContentsObserver::RenderProcessGone( | 173 if (!navigation_handle->IsInMainFrame()) |
| 87 base::TerminationStatus status) { | 174 return; |
| 88 RecordTimingHistograms(); | 175 // We can have two provisional loads in some cases. E.g. a same-site |
| 176 // navigation can have a concurrent cross-process navigation started | |
| 177 // from the omnibox. | |
| 178 DCHECK(provisional_loads_.size() < 2); | |
| 179 provisional_loads_.insert( | |
| 180 navigation_handle, make_scoped_ptr(new PageLoadTracker(in_foreground_))); | |
| 89 } | 181 } |
| 90 | 182 |
| 91 #define PAGE_LOAD_HISTOGRAM(name, sample) \ | 183 void MetricsWebContentsObserver::DidFinishLoad( |
| 92 UMA_HISTOGRAM_CUSTOM_TIMES(name, sample, \ | 184 content::RenderFrameHost* render_frame_host, |
| 93 base::TimeDelta::FromMilliseconds(10), \ | 185 const GURL& validated_url) { |
| 94 base::TimeDelta::FromMinutes(10), 100); | 186 if (render_frame_host == web_contents()->GetMainFrame() && committed_load_) |
| 187 committed_load_->Finish(); | |
| 188 } | |
| 189 | |
| 190 void MetricsWebContentsObserver::DidFailLoad( | |
| 191 content::RenderFrameHost* render_frame_host, | |
| 192 const GURL& validated_url, | |
| 193 int error_code, | |
| 194 const base::string16& error_description, | |
| 195 bool was_ignored_by_handler) { | |
| 196 if (render_frame_host == web_contents()->GetMainFrame() && committed_load_) | |
| 197 committed_load_->Finish(); | |
| 198 } | |
| 199 | |
| 200 void MetricsWebContentsObserver::WasShown() { | |
| 201 in_foreground_ = true; | |
| 202 } | |
| 203 void MetricsWebContentsObserver::WasHidden() { | |
| 204 in_foreground_ = false; | |
| 205 if (committed_load_) | |
| 206 committed_load_->WebContentsHidden(); | |
| 207 for (const auto& kv : provisional_loads_) { | |
| 208 kv.second->WebContentsHidden(); | |
| 209 } | |
| 210 } | |
| 95 | 211 |
| 96 void MetricsWebContentsObserver::OnTimingUpdated( | 212 void MetricsWebContentsObserver::OnTimingUpdated( |
| 97 content::RenderFrameHost* render_frame_host, | 213 content::RenderFrameHost* render_frame_host, |
| 98 const PageLoadTiming& timing) { | 214 const PageLoadTiming& timing) { |
| 99 if (!current_timing_) | |
| 100 return; | |
| 101 | |
| 102 // We may receive notifications from frames that have been navigated away | 215 // We may receive notifications from frames that have been navigated away |
| 103 // from. We simply ignore them. | 216 // from. We simply ignore them. |
| 104 if (render_frame_host != web_contents()->GetMainFrame()) | 217 if (render_frame_host != web_contents()->GetMainFrame()) |
| 105 return; | 218 return; |
| 106 | 219 |
| 107 // For urls like chrome://newtab, the renderer and browser disagree, | 220 // For urls like chrome://newtab, the renderer and browser disagree, |
| 108 // so we have to double check that the renderer isn't sending data from a | 221 // so we have to double check that the renderer isn't sending data from a |
| 109 // bad url like https://www.google.com/_/chrome/newtab. | 222 // bad url like https://www.google.com/_/chrome/newtab. |
| 110 if (!web_contents()->GetLastCommittedURL().SchemeIsHTTPOrHTTPS()) | 223 if (!web_contents()->GetLastCommittedURL().SchemeIsHTTPOrHTTPS()) |
| 111 return; | 224 return; |
| 112 | 225 |
| 113 // Throw away IPCs that are not relevant to the current navigation. | 226 committed_load_->UpdateTiming(timing); |
| 114 if (!current_timing_->navigation_start.is_null() && | |
| 115 timing.navigation_start != current_timing_->navigation_start) { | |
| 116 // TODO(csharrison) uma log a counter here | |
| 117 return; | |
| 118 } | |
| 119 | |
| 120 *current_timing_ = timing; | |
| 121 } | |
| 122 | |
| 123 void MetricsWebContentsObserver::RecordTimingHistograms() { | |
| 124 if (!current_timing_ || !IsValidPageLoadTiming(*current_timing_)) | |
| 125 return; | |
| 126 | |
| 127 if (!current_timing_->dom_content_loaded_event_start.is_zero()) { | |
| 128 PAGE_LOAD_HISTOGRAM( | |
| 129 "PageLoad.Timing.NavigationToDOMContentLoadedEventFired", | |
| 130 current_timing_->dom_content_loaded_event_start); | |
| 131 } | |
| 132 | |
| 133 if (!current_timing_->load_event_start.is_zero()) { | |
| 134 PAGE_LOAD_HISTOGRAM("PageLoad.Timing.NavigationToLoadEventFired", | |
| 135 current_timing_->load_event_start); | |
| 136 } | |
| 137 | |
| 138 if (!current_timing_->first_layout.is_zero()) { | |
| 139 PAGE_LOAD_HISTOGRAM("PageLoad.Timing.NavigationToFirstLayout", | |
| 140 current_timing_->first_layout); | |
| 141 } | |
| 142 current_timing_.reset(); | |
| 143 } | 227 } |
| 144 | 228 |
| 145 bool MetricsWebContentsObserver::IsRelevantNavigation( | 229 bool MetricsWebContentsObserver::IsRelevantNavigation( |
| 146 content::NavigationHandle* navigation_handle) { | 230 content::NavigationHandle* navigation_handle) { |
| 147 // The url we see from the renderer side is not always the same as what | 231 // The url we see from the renderer side is not always the same as what |
| 148 // we see from the browser side (e.g. chrome://newtab). We want to be | 232 // we see from the browser side (e.g. chrome://newtab). We want to be |
| 149 // sure here that we aren't logging UMA for internal pages. | 233 // sure here that we aren't logging UMA for internal pages. |
| 150 const GURL& browser_url = web_contents()->GetLastCommittedURL(); | 234 const GURL& browser_url = web_contents()->GetLastCommittedURL(); |
| 151 return navigation_handle->IsInMainFrame() && | 235 return navigation_handle->IsInMainFrame() && |
| 152 !navigation_handle->IsSamePage() && | 236 !navigation_handle->IsSamePage() && |
| 153 !navigation_handle->IsErrorPage() && | 237 !navigation_handle->IsErrorPage() && |
| 154 navigation_handle->GetURL().SchemeIsHTTPOrHTTPS() && | 238 navigation_handle->GetURL().SchemeIsHTTPOrHTTPS() && |
| 155 browser_url.SchemeIsHTTPOrHTTPS(); | 239 browser_url.SchemeIsHTTPOrHTTPS(); |
| 156 } | 240 } |
| 157 | 241 |
| 158 } // namespace page_load_metrics | 242 } // namespace page_load_metrics |
| OLD | NEW |