Chromium Code Reviews| 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..e2a74e66124e002bbe0e912bd7d47ba61d3f29be |
| --- /dev/null |
| +++ b/chrome/browser/metrics/desktop_engagement/chrome_visibility_observer.cc |
| @@ -0,0 +1,72 @@ |
| +// 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(); |
| +} |
| + |
| +// static |
| +void ChromeVisibilityObserver::Initialize() { |
| + ChromeVisibilityObserver observer; |
| +} |
| + |
| +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 (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.
|
| + 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); |
| +} |
|
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.
|
| +} // namespace metrics |