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

Unified Diff: chrome/browser/metrics/desktop_engagement/chrome_visibility_observer.cc

Issue 2142983002: Add desktop engagement metrics. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 4 years, 4 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: chrome/browser/metrics/desktop_engagement/chrome_visibility_observer.cc
diff --git a/chrome/browser/metrics/desktop_engagement/chrome_visibility_observer.cc b/chrome/browser/metrics/desktop_engagement/chrome_visibility_observer.cc
new file mode 100644
index 0000000000000000000000000000000000000000..fcd72072ec2c82120767b5f5fe32cb5d8c54cdd1
--- /dev/null
+++ b/chrome/browser/metrics/desktop_engagement/chrome_visibility_observer.cc
@@ -0,0 +1,70 @@
+// Copyright 2016 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "chrome/browser/metrics/desktop_engagement/chrome_visibility_observer.h"
+
+#include "base/memory/singleton.h"
+#include "base/strings/string_number_conversions.h"
+#include "base/threading/thread_task_runner_handle.h"
+#include "chrome/browser/metrics/desktop_engagement/desktop_engagement_service.h"
+#include "chrome/browser/ui/browser.h"
+#include "chrome/browser/ui/browser_list.h"
+#include "components/variations/variations_associated_data.h"
+
+namespace metrics {
+
+ChromeVisibilityObserver::ChromeVisibilityObserver() : weak_factory_(this) {
+ BrowserList::AddObserver(this);
+ InitVisibilityGapTimeout();
+}
+
+ChromeVisibilityObserver::~ChromeVisibilityObserver() {
+ BrowserList::RemoveObserver(this);
+}
+
+void ChromeVisibilityObserver::SendVisibilityChangeEvent(bool active) {
+ DesktopEngagementService::Get()->OnVisibilityChanged(active);
+}
+
+void ChromeVisibilityObserver::CancelVisibilityChange() {
+ weak_factory_.InvalidateWeakPtrs();
+}
+
+void ChromeVisibilityObserver::OnBrowserSetLastActive(Browser* browser) {
+ if (weak_factory_.HasWeakPtrs())
+ CancelVisibilityChange();
+ else
+ SendVisibilityChangeEvent(true);
+}
+
+void ChromeVisibilityObserver::OnBrowserNoLongerActive(Browser* browser) {
+ base::ThreadTaskRunnerHandle::Get()->PostDelayedTask(
+ FROM_HERE,
+ base::Bind(&ChromeVisibilityObserver::SendVisibilityChangeEvent,
+ weak_factory_.GetWeakPtr(), false),
+ visibility_gap_timeout_);
+}
+
+void ChromeVisibilityObserver::OnBrowserRemoved(Browser* browser) {
+ // If there are no browser instances left then we should notify that browser
+ // is not visible anymore immediately without waiting.
+ if (BrowserList::GetInstance()->empty()) {
+ CancelVisibilityChange();
+ SendVisibilityChangeEvent(false);
+ }
+}
+
+void ChromeVisibilityObserver::InitVisibilityGapTimeout() {
+ const int kDefaultVisibilityGapTimeout = 3;
+
+ int timeout_seconds = kDefaultVisibilityGapTimeout;
+ std::string param_value = variations::GetVariationParamValue(
+ "DesktopEngagement", "visibility_gap_timeout");
+ if (!param_value.empty())
+ base::StringToInt(param_value, &timeout_seconds);
+
+ visibility_gap_timeout_ = base::TimeDelta::FromSeconds(timeout_seconds);
+}
+
+} // namespace metrics

Powered by Google App Engine
This is Rietveld 408576698