Commit c10b27b4 authored by Peiyong Lin's avatar Peiyong Lin Committed by Commit Bot

[TooManyTabs] Report UKM when background tab is closed or foregrounded.

Adds UKM to report the duration in millisecond from when a tab is backgrounded
to it is closed or foregrounded.

BUG=753486

Change-Id: I995b4002c80c1eb91f3ed36556e23c5b06183edf
Reviewed-on: https://chromium-review.googlesource.com/654144
Commit-Queue: lpy <lpy@chromium.org>
Reviewed-by: default avatarChris Hamilton <chrisha@chromium.org>
Reviewed-by: default avatarSteven Holte <holte@chromium.org>
Cr-Commit-Position: refs/heads/master@{#506772}
parent 645830fb
......@@ -14,6 +14,7 @@
#include "content/public/browser/browser_thread.h"
#include "content/public/browser/navigation_handle.h"
#include "content/public/browser/web_contents.h"
#include "services/metrics/public/cpp/ukm_builders.h"
using base::TimeTicks;
using content::WebContents;
......@@ -27,7 +28,8 @@ TabManager::WebContentsData::WebContentsData(content::WebContents* web_contents)
: WebContentsObserver(web_contents),
test_tick_clock_(nullptr),
time_to_purge_(base::TimeDelta::FromMinutes(30)),
is_purged_(false) {}
is_purged_(false),
ukm_source_id_(0) {}
TabManager::WebContentsData::~WebContentsData() {}
......@@ -69,6 +71,21 @@ void TabManager::WebContentsData::DidFinishNavigation(
content::NavigationHandle* navigation_handle) {
SetIsInSessionRestore(false);
g_browser_process->GetTabManager()->OnDidFinishNavigation(navigation_handle);
if (!navigation_handle->HasCommitted() || navigation_handle->IsErrorPage() ||
navigation_handle->IsSameDocument() ||
!navigation_handle->IsInMainFrame()) {
return;
}
ukm_source_id_ = ukm::ConvertToSourceId(navigation_handle->GetNavigationId(),
ukm::SourceIdType::NAVIGATION_ID);
}
void TabManager::WebContentsData::WasShown() {
if (tab_data_.last_inactive_time == base::TimeTicks::UnixEpoch())
return;
ReportUKMWhenBackgroundTabIsClosedOrForegrounded(true);
}
void TabManager::WebContentsData::WebContentsDestroyed() {
......@@ -87,6 +104,11 @@ void TabManager::WebContentsData::WebContentsDestroyed() {
base::TimeDelta::FromDays(1), 100);
}
if (!web_contents()->IsVisible() &&
tab_data_.last_inactive_time != base::TimeTicks::UnixEpoch()) {
ReportUKMWhenBackgroundTabIsClosedOrForegrounded(false);
}
SetTabLoadingState(TAB_IS_NOT_LOADING);
SetIsInSessionRestore(false);
g_browser_process->GetTabManager()->OnWebContentsDestroyed(web_contents());
......@@ -205,6 +227,17 @@ TimeTicks TabManager::WebContentsData::NowTicks() const {
return test_tick_clock_->NowTicks();
}
void TabManager::WebContentsData::
ReportUKMWhenBackgroundTabIsClosedOrForegrounded(bool is_foregrounded) {
if (!ukm_source_id_)
return;
auto duration = NowTicks() - tab_data_.last_inactive_time;
ukm::builders::TabManager_Background_ForegroundedOrClosed(ukm_source_id_)
.SetTimeFromBackgrounded(duration.InMilliseconds())
.SetIsForegrounded(is_foregrounded)
.Record(g_browser_process->ukm_recorder());
}
TabManager::WebContentsData::Data::Data()
: is_discarded(false),
discard_count(0),
......
......@@ -11,6 +11,7 @@
#include "content/public/browser/navigation_handle.h"
#include "content/public/browser/web_contents_observer.h"
#include "content/public/browser/web_contents_user_data.h"
#include "services/metrics/public/cpp/ukm_source_id.h"
namespace base {
class TickClock;
......@@ -59,6 +60,7 @@ class TabManager::WebContentsData
content::NavigationHandle* navigation_handle) override;
void DidFinishNavigation(
content::NavigationHandle* navigation_handle) override;
void WasShown() override;
void WebContentsDestroyed() override;
// Tab signal received from GRC.
......@@ -193,6 +195,8 @@ class TabManager::WebContentsData
// for more details.
base::TimeTicks NowTicks() const;
void ReportUKMWhenBackgroundTabIsClosedOrForegrounded(bool is_foregrounded);
// Contains all the needed data for the tab.
Data tab_data_;
......@@ -206,6 +210,8 @@ class TabManager::WebContentsData
// True if the tab has been purged.
bool is_purged_;
ukm::SourceId ukm_source_id_;
DISALLOW_COPY_AND_ASSIGN(WebContentsData);
};
......
......@@ -1343,4 +1343,25 @@ be describing additional metrics about the same event.
</metric>
</event>
<event name="TabManager.Background.ForegroundedOrClosed">
<owner>chrisha@chromium.org</owner>
<owner>lpy@chromium.org</owner>
<summary>
Collects the duration in MS from when the tab is backgrounded to when it is
brought to foreground or closed.
</summary>
<metric name="IsForegrounded">
<summary>
Boolean value indicating whether or not it's triggered because the tab is
brought to foreground again.
</summary>
</metric>
<metric name="TimeFromBackgrounded">
<summary>
Duration in MS from when the tab is backgrounded to when it is brought to
foreground or closed.
</summary>
</metric>
</event>
</ukm-configuration>
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment