Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright (c) 2016 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "chrome/browser/metrics/chrome_visibility_observer.h" | |
| 6 | |
| 7 #include "base/memory/singleton.h" | |
| 8 #include "chrome/browser/metrics/desktop_engagement_service.h" | |
| 9 #include "chrome/browser/ui/browser.h" | |
| 10 #include "chrome/browser/ui/browser_list.h" | |
| 11 | |
| 12 namespace metrics { | |
| 13 | |
| 14 namespace { | |
| 15 | |
| 16 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/
| |
| 17 | |
| 18 } // namespace | |
| 19 | |
| 20 ChromeVisibilityObserver::ChromeVisibilityObserver() | |
| 21 : weak_factory_(this) {} | |
| 22 | |
| 23 ChromeVisibilityObserver::~ChromeVisibilityObserver() { | |
| 24 BrowserList::RemoveObserver(this); | |
| 25 } | |
| 26 | |
| 27 void ChromeVisibilityObserver::SendVisibilityChangeEvent(bool active) { | |
| 28 DesktopEngagementService::Get()->OnVisibilityChanged(active); | |
| 29 } | |
| 30 | |
| 31 void ChromeVisibilityObserver::CancelVisibilityChange() { | |
| 32 weak_factory_.InvalidateWeakPtrs(); | |
| 33 } | |
| 34 | |
| 35 // static | |
| 36 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
| |
| 37 return base::Singleton<ChromeVisibilityObserver>::get(); | |
| 38 } | |
| 39 | |
| 40 // static | |
| 41 void ChromeVisibilityObserver::Initialize() { | |
| 42 BrowserList::AddObserver(GetInstance()); | |
| 43 } | |
| 44 | |
| 45 void ChromeVisibilityObserver::OnBrowserSetLastActive(Browser* browser) { | |
| 46 if (weak_factory_.HasWeakPtrs()) | |
| 47 CancelVisibilityChange(); | |
| 48 else | |
| 49 SendVisibilityChangeEvent(true); | |
| 50 } | |
| 51 | |
| 52 void ChromeVisibilityObserver::OnBrowserNoLongerActive(Browser* browser) { | |
| 53 base::MessageLoop::current()->PostDelayedTask( | |
| 54 FROM_HERE, | |
| 55 base::Bind(&ChromeVisibilityObserver::SendVisibilityChangeEvent, | |
| 56 weak_factory_.GetWeakPtr(), false), | |
| 57 base::TimeDelta::FromSeconds(kVisibilityGapTolerance)); | |
| 58 } | |
| 59 | |
| 60 void ChromeVisibilityObserver::OnBrowserRemoved(Browser* browser) { | |
| 61 if (BrowserList::GetInstance()->empty()) { | |
| 62 CancelVisibilityChange(); | |
| 63 SendVisibilityChangeEvent(false); | |
| 64 } | |
| 65 } | |
| 66 | |
| 67 } // namespace metrics | |
| OLD | NEW |