| 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 = 3; // seconds, can make it a finch param. |
| 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() { |
| 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 } // namespace metrics |
| OLD | NEW |