Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(472)

Unified Diff: components/page_load_metrics/browser/metrics_web_contents_observer.cc

Issue 1357403003: Separate page load metrics for backgrounded pages (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: minor changes per Bryan review Created 5 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: components/page_load_metrics/browser/metrics_web_contents_observer.cc
diff --git a/components/page_load_metrics/browser/metrics_web_contents_observer.cc b/components/page_load_metrics/browser/metrics_web_contents_observer.cc
index 3a1fbd9302c38026492c1c708a2043fa0efe8827..d3c298f553749373f99cc257488c38e8dd208599 100644
--- a/components/page_load_metrics/browser/metrics_web_contents_observer.cc
+++ b/components/page_load_metrics/browser/metrics_web_contents_observer.cc
@@ -49,14 +49,86 @@ bool IsValidPageLoadTiming(const PageLoadTiming& timing) {
} // namespace
+#define PAGE_LOAD_HISTOGRAM(name, sample) \
+ UMA_HISTOGRAM_CUSTOM_TIMES(name, sample, \
+ base::TimeDelta::FromMilliseconds(10), \
+ base::TimeDelta::FromMinutes(10), 100);
Alexei Svitkine (slow) 2015/09/28 18:27:21 Nit: No ; The caller will supply their own after
+
+PageLoadTracker::PageLoadTracker(bool in_foreground)
+ : has_commit_(false),
+ has_finished_(false),
+ page_load_stayed_in_foreground_(in_foreground) {}
+
+PageLoadTracker::~PageLoadTracker() {
+ if (has_commit_)
+ RecordTimingHistograms();
+}
+
+void PageLoadTracker::WebContentsHidden() {
+ if (!has_finished_)
+ page_load_stayed_in_foreground_ = false;
+}
+
+void PageLoadTracker::Commit() {
+ has_commit_ = true;
+}
+
+void PageLoadTracker::Finish() {
+ has_finished_ = true;
+}
+
+bool PageLoadTracker::UpdateTiming(const PageLoadTiming& timing) {
+ // Throw away IPCs that are not relevant to the current navigation.
+ if (!timing_.navigation_start.is_null() &&
+ timing_.navigation_start != timing.navigation_start) {
+ // TODO(csharrison) uma log a counter here
+ return false;
+ }
+ if (IsValidPageLoadTiming(timing)) {
+ timing_ = timing;
+ return true;
+ }
+ return false;
+}
+
+void PageLoadTracker::RecordTimingHistograms() {
+ DCHECK(has_commit_);
+ if (!timing_.dom_content_loaded_event_start.is_zero()) {
+ if (page_load_stayed_in_foreground_) {
+ PAGE_LOAD_HISTOGRAM(
+ "PageLoad.Timing.NavigationToDOMContentLoadedEventFired",
+ timing_.dom_content_loaded_event_start);
+ } else {
+ PAGE_LOAD_HISTOGRAM(
+ "PageLoad.Timing.BG.NavigationToDOMContentLoadedEventFired",
+ timing_.dom_content_loaded_event_start);
+ }
+ }
+ if (!timing_.load_event_start.is_zero()) {
+ if (page_load_stayed_in_foreground_) {
+ PAGE_LOAD_HISTOGRAM("PageLoad.Timing.NavigationToLoadEventFired",
+ timing_.load_event_start);
+ } else {
+ PAGE_LOAD_HISTOGRAM("PageLoad.Timing.BG.NavigationToLoadEventFired",
+ timing_.load_event_start);
+ }
+ }
+ if (!timing_.first_layout.is_zero()) {
+ if (page_load_stayed_in_foreground_) {
+ PAGE_LOAD_HISTOGRAM("PageLoad.Timing.NavigationToFirstLayout",
+ timing_.first_layout);
+ } else {
+ PAGE_LOAD_HISTOGRAM("PageLoad.Timing.BG.NavigationToFirstLayout",
+ timing_.first_layout);
+ }
+ }
+}
+
MetricsWebContentsObserver::MetricsWebContentsObserver(
content::WebContents* web_contents)
- : content::WebContentsObserver(web_contents) {}
+ : content::WebContentsObserver(web_contents), in_foreground_(false) {}
-// This object is tied to a single WebContents for its entire lifetime.
-MetricsWebContentsObserver::~MetricsWebContentsObserver() {
- RecordTimingHistograms();
-}
+MetricsWebContentsObserver::~MetricsWebContentsObserver() {}
bool MetricsWebContentsObserver::OnMessageReceived(
const IPC::Message& message,
@@ -73,32 +145,73 @@ bool MetricsWebContentsObserver::OnMessageReceived(
void MetricsWebContentsObserver::DidFinishNavigation(
content::NavigationHandle* navigation_handle) {
+ if (!navigation_handle->IsInMainFrame())
+ return;
+
+ scoped_ptr<PageLoadTracker> finished_nav(
+ provisional_loads_.take_and_erase(navigation_handle));
+
+ // TODO(csharrison) handle the two error cases:
+ // 1. Error pages that replace the previous page.
+ // 2. Error pages that leave the user on the previous page.
+ // For now these cases will be ignored.
if (!navigation_handle->HasCommitted())
return;
- if (navigation_handle->IsInMainFrame() && !navigation_handle->IsSamePage())
- RecordTimingHistograms();
- if (IsRelevantNavigation(navigation_handle))
- current_timing_.reset(new PageLoadTiming());
+
+ if (!IsRelevantNavigation(navigation_handle))
+ return;
+
+ // Updating |committed_load_| will trigger RecordTimingHistograms on a
+ // previous PageLoadTracker.
+ committed_load_ = finished_nav.Pass();
+ DCHECK(committed_load_);
+ committed_load_->Commit();
}
-// This will occur when the process for the main RenderFrameHost exits.
-// This will happen with a normal exit or a crash.
-void MetricsWebContentsObserver::RenderProcessGone(
- base::TerminationStatus status) {
- RecordTimingHistograms();
+void MetricsWebContentsObserver::DidStartNavigation(
+ content::NavigationHandle* navigation_handle) {
+ if (!navigation_handle->IsInMainFrame())
+ return;
+ // We can have two provisional loads in some cases. E.g. a same-site
+ // navigation can have a concurrent cross-process navigation started
+ // from the omnibox.
+ DCHECK(provisional_loads_.size() < 2);
+ provisional_loads_.insert(
+ navigation_handle, make_scoped_ptr(new PageLoadTracker(in_foreground_)));
}
-#define PAGE_LOAD_HISTOGRAM(name, sample) \
- UMA_HISTOGRAM_CUSTOM_TIMES(name, sample, \
- base::TimeDelta::FromMilliseconds(10), \
- base::TimeDelta::FromMinutes(10), 100);
+void MetricsWebContentsObserver::DidFinishLoad(
+ content::RenderFrameHost* render_frame_host,
+ const GURL& validated_url) {
+ if (render_frame_host == web_contents()->GetMainFrame() && committed_load_)
+ committed_load_->Finish();
+}
+
+void MetricsWebContentsObserver::DidFailLoad(
+ content::RenderFrameHost* render_frame_host,
+ const GURL& validated_url,
+ int error_code,
+ const base::string16& error_description,
+ bool was_ignored_by_handler) {
+ if (render_frame_host == web_contents()->GetMainFrame() && committed_load_)
+ committed_load_->Finish();
+}
+
+void MetricsWebContentsObserver::WasShown() {
+ in_foreground_ = true;
+}
+void MetricsWebContentsObserver::WasHidden() {
+ in_foreground_ = false;
+ if (committed_load_)
+ committed_load_->WebContentsHidden();
+ for (const auto& kv : provisional_loads_) {
+ kv.second->WebContentsHidden();
+ }
+}
void MetricsWebContentsObserver::OnTimingUpdated(
content::RenderFrameHost* render_frame_host,
const PageLoadTiming& timing) {
- if (!current_timing_)
- return;
-
// We may receive notifications from frames that have been navigated away
// from. We simply ignore them.
if (render_frame_host != web_contents()->GetMainFrame())
@@ -110,36 +223,7 @@ void MetricsWebContentsObserver::OnTimingUpdated(
if (!web_contents()->GetLastCommittedURL().SchemeIsHTTPOrHTTPS())
return;
- // Throw away IPCs that are not relevant to the current navigation.
- if (!current_timing_->navigation_start.is_null() &&
- timing.navigation_start != current_timing_->navigation_start) {
- // TODO(csharrison) uma log a counter here
- return;
- }
-
- *current_timing_ = timing;
-}
-
-void MetricsWebContentsObserver::RecordTimingHistograms() {
- if (!current_timing_ || !IsValidPageLoadTiming(*current_timing_))
- return;
-
- if (!current_timing_->dom_content_loaded_event_start.is_zero()) {
- PAGE_LOAD_HISTOGRAM(
- "PageLoad.Timing.NavigationToDOMContentLoadedEventFired",
- current_timing_->dom_content_loaded_event_start);
- }
-
- if (!current_timing_->load_event_start.is_zero()) {
- PAGE_LOAD_HISTOGRAM("PageLoad.Timing.NavigationToLoadEventFired",
- current_timing_->load_event_start);
- }
-
- if (!current_timing_->first_layout.is_zero()) {
- PAGE_LOAD_HISTOGRAM("PageLoad.Timing.NavigationToFirstLayout",
- current_timing_->first_layout);
- }
- current_timing_.reset();
+ committed_load_->UpdateTiming(timing);
}
bool MetricsWebContentsObserver::IsRelevantNavigation(

Powered by Google App Engine
This is Rietveld 408576698