Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 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/desktop_engagement/chrome_visibility_observer.h " | |
| 6 | |
| 7 #include "base/memory/singleton.h" | |
| 8 #include "base/strings/string_number_conversions.h" | |
| 9 #include "base/threading/thread_task_runner_handle.h" | |
| 10 #include "chrome/browser/metrics/desktop_engagement/desktop_engagement_service.h " | |
| 11 #include "chrome/browser/ui/browser.h" | |
| 12 #include "chrome/browser/ui/browser_list.h" | |
| 13 #include "components/variations/variations_associated_data.h" | |
| 14 | |
| 15 namespace metrics { | |
| 16 | |
| 17 ChromeVisibilityObserver::ChromeVisibilityObserver() : weak_factory_(this) { | |
| 18 BrowserList::AddObserver(this); | |
| 19 InitVisibilityGapTimeout(); | |
| 20 } | |
| 21 | |
| 22 ChromeVisibilityObserver::~ChromeVisibilityObserver() { | |
| 23 BrowserList::RemoveObserver(this); | |
| 24 } | |
| 25 | |
| 26 void ChromeVisibilityObserver::SendVisibilityChangeEvent(bool active) { | |
| 27 DesktopEngagementService::Get()->OnVisibilityChanged(active); | |
| 28 } | |
| 29 | |
| 30 void ChromeVisibilityObserver::CancelVisibilityChange() { | |
| 31 weak_factory_.InvalidateWeakPtrs(); | |
| 32 } | |
| 33 | |
| 34 // static | |
| 35 void ChromeVisibilityObserver::Initialize() { | |
| 36 ChromeVisibilityObserver observer; | |
| 37 } | |
| 38 | |
| 39 void ChromeVisibilityObserver::OnBrowserSetLastActive(Browser* browser) { | |
| 40 if (weak_factory_.HasWeakPtrs()) | |
| 41 CancelVisibilityChange(); | |
| 42 else | |
| 43 SendVisibilityChangeEvent(true); | |
| 44 } | |
| 45 | |
| 46 void ChromeVisibilityObserver::OnBrowserNoLongerActive(Browser* browser) { | |
| 47 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask( | |
| 48 FROM_HERE, | |
| 49 base::Bind(&ChromeVisibilityObserver::SendVisibilityChangeEvent, | |
| 50 weak_factory_.GetWeakPtr(), false), | |
| 51 visibility_gap_timeout_); | |
| 52 } | |
| 53 | |
| 54 void ChromeVisibilityObserver::OnBrowserRemoved(Browser* browser) { | |
| 55 if (BrowserList::GetInstance()->empty()) { | |
|
Alexei Svitkine (slow)
2016/07/26 22:00:40
Add a comment explaining this logic.
gayane -on leave until 09-2017
2016/07/27 21:10:24
Done.
| |
| 56 CancelVisibilityChange(); | |
| 57 SendVisibilityChangeEvent(false); | |
| 58 } | |
| 59 } | |
| 60 | |
| 61 void ChromeVisibilityObserver::InitVisibilityGapTimeout() { | |
| 62 const int kDefaultVisibilityGapTimeout = 3; | |
| 63 | |
| 64 int timeout_seconds = kDefaultVisibilityGapTimeout; | |
| 65 std::string param_value = variations::GetVariationParamValue( | |
| 66 "DesktopEngagement", "visibility_gap_timeout"); | |
| 67 if (!param_value.empty()) | |
| 68 base::StringToInt(param_value, &timeout_seconds); | |
| 69 | |
| 70 visibility_gap_timeout_ = base::TimeDelta::FromSeconds(timeout_seconds); | |
| 71 } | |
|
Alexei Svitkine (slow)
2016/07/26 22:00:40
Add an empty line below this.
gayane -on leave until 09-2017
2016/07/27 21:10:24
Done.
| |
| 72 } // namespace metrics | |
| OLD | NEW |