Chromium Code Reviews| Index: chrome/browser/metrics/chrome_visibility_observer.cc |
| diff --git a/chrome/browser/metrics/chrome_visibility_observer.cc b/chrome/browser/metrics/chrome_visibility_observer.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..3c072e542daec408915570d5f0d860ae252b11a4 |
| --- /dev/null |
| +++ b/chrome/browser/metrics/chrome_visibility_observer.cc |
| @@ -0,0 +1,67 @@ |
| +// Copyright (c) 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/chrome_visibility_observer.h" |
| + |
| +#include "base/memory/singleton.h" |
| +#include "chrome/browser/metrics/desktop_engagement_service.h" |
| +#include "chrome/browser/ui/browser.h" |
| +#include "chrome/browser/ui/browser_list.h" |
| + |
| +namespace metrics { |
| + |
| +namespace { |
| + |
| +const int kVisibilityGapTolerance = 5; // seconds, can make it a finch param. |
|
Patrick Monette
2016/07/13 16:06:24
constexpr base::TimeDelta kVisibilityGapTolerance
gayane -on leave until 09-2017
2016/07/20 20:22:02
Logic changed a bit. Please have a look/
|
| + |
| +} // namespace |
| + |
| +ChromeVisibilityObserver::ChromeVisibilityObserver() |
| + : weak_factory_(this) {} |
| + |
| +ChromeVisibilityObserver::~ChromeVisibilityObserver() { |
| + BrowserList::RemoveObserver(this); |
| +} |
| + |
| +void ChromeVisibilityObserver::SendVisibilityChangeEvent(bool active) { |
| + DesktopEngagementService::Get()->OnVisibilityChanged(active); |
| +} |
| + |
| +void ChromeVisibilityObserver::CancelVisibilityChange() { |
| + weak_factory_.InvalidateWeakPtrs(); |
| +} |
| + |
| +// static |
| +ChromeVisibilityObserver* ChromeVisibilityObserver::GetInstance() { |
|
chrisha
2016/07/14 13:58:48
Does this need to be a singleton? Maybe this can s
gayane -on leave until 09-2017
2016/07/20 20:22:02
Agreed. It made more sense before. I have removed
|
| + return base::Singleton<ChromeVisibilityObserver>::get(); |
| +} |
| + |
| +// static |
| +void ChromeVisibilityObserver::Initialize() { |
| + BrowserList::AddObserver(GetInstance()); |
| +} |
| + |
| +void ChromeVisibilityObserver::OnBrowserSetLastActive(Browser* browser) { |
| + if (weak_factory_.HasWeakPtrs()) |
| + CancelVisibilityChange(); |
| + else |
| + SendVisibilityChangeEvent(true); |
| +} |
| + |
| +void ChromeVisibilityObserver::OnBrowserNoLongerActive(Browser* browser) { |
| + base::MessageLoop::current()->PostDelayedTask( |
| + FROM_HERE, |
| + base::Bind(&ChromeVisibilityObserver::SendVisibilityChangeEvent, |
| + weak_factory_.GetWeakPtr(), false), |
| + base::TimeDelta::FromSeconds(kVisibilityGapTolerance)); |
| +} |
| + |
| +void ChromeVisibilityObserver::OnBrowserRemoved(Browser* browser) { |
| + if (BrowserList::GetInstance()->empty()) { |
| + CancelVisibilityChange(); |
| + SendVisibilityChangeEvent(false); |
| + } |
| +} |
| + |
| +} // namespace metrics |