| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/metrics/desktop_engagement/chrome_visibility_observer.h
" | 5 #include "chrome/browser/metrics/desktop_engagement/chrome_visibility_observer.h
" |
| 6 | 6 |
| 7 #include "base/memory/singleton.h" | 7 #include "base/memory/singleton.h" |
| 8 #include "base/strings/string_number_conversions.h" | 8 #include "base/strings/string_number_conversions.h" |
| 9 #include "base/threading/thread_task_runner_handle.h" | 9 #include "base/threading/thread_task_runner_handle.h" |
| 10 #include "chrome/browser/metrics/desktop_engagement/desktop_engagement_service.h
" | 10 #include "chrome/browser/metrics/desktop_engagement/desktop_engagement_service.h
" |
| 11 #include "chrome/browser/ui/browser.h" | 11 #include "chrome/browser/ui/browser.h" |
| 12 #include "chrome/browser/ui/browser_list.h" | 12 #include "chrome/browser/ui/browser_list.h" |
| 13 #include "components/variations/variations_associated_data.h" | 13 #include "components/variations/variations_associated_data.h" |
| 14 | 14 |
| 15 namespace metrics { | 15 namespace metrics { |
| 16 | 16 |
| 17 namespace { |
| 18 |
| 19 const base::TimeDelta kZeroTime = base::TimeDelta::FromSeconds(0); |
| 20 |
| 21 } // namespace |
| 22 |
| 23 |
| 17 ChromeVisibilityObserver::ChromeVisibilityObserver() : weak_factory_(this) { | 24 ChromeVisibilityObserver::ChromeVisibilityObserver() : weak_factory_(this) { |
| 18 BrowserList::AddObserver(this); | 25 BrowserList::AddObserver(this); |
| 19 InitVisibilityGapTimeout(); | 26 InitVisibilityGapTimeout(); |
| 20 } | 27 } |
| 21 | 28 |
| 22 ChromeVisibilityObserver::~ChromeVisibilityObserver() { | 29 ChromeVisibilityObserver::~ChromeVisibilityObserver() { |
| 23 BrowserList::RemoveObserver(this); | 30 BrowserList::RemoveObserver(this); |
| 24 } | 31 } |
| 25 | 32 |
| 26 void ChromeVisibilityObserver::SendVisibilityChangeEvent(bool active) { | 33 void ChromeVisibilityObserver::SendVisibilityChangeEvent( |
| 27 DesktopEngagementService::Get()->OnVisibilityChanged(active); | 34 bool active, |
| 35 base::TimeDelta time_ago) { |
| 36 DesktopEngagementService::Get()->OnVisibilityChanged(active, time_ago); |
| 28 } | 37 } |
| 29 | 38 |
| 30 void ChromeVisibilityObserver::CancelVisibilityChange() { | 39 void ChromeVisibilityObserver::CancelVisibilityChange() { |
| 31 weak_factory_.InvalidateWeakPtrs(); | 40 weak_factory_.InvalidateWeakPtrs(); |
| 32 } | 41 } |
| 33 | 42 |
| 34 void ChromeVisibilityObserver::OnBrowserSetLastActive(Browser* browser) { | 43 void ChromeVisibilityObserver::OnBrowserSetLastActive(Browser* browser) { |
| 35 if (weak_factory_.HasWeakPtrs()) | 44 if (weak_factory_.HasWeakPtrs()) |
| 36 CancelVisibilityChange(); | 45 CancelVisibilityChange(); |
| 37 else | 46 else |
| 38 SendVisibilityChangeEvent(true); | 47 SendVisibilityChangeEvent(true, kZeroTime); |
| 39 } | 48 } |
| 40 | 49 |
| 41 void ChromeVisibilityObserver::OnBrowserNoLongerActive(Browser* browser) { | 50 void ChromeVisibilityObserver::OnBrowserNoLongerActive(Browser* browser) { |
| 42 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask( | 51 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask( |
| 43 FROM_HERE, | 52 FROM_HERE, |
| 44 base::Bind(&ChromeVisibilityObserver::SendVisibilityChangeEvent, | 53 base::Bind(&ChromeVisibilityObserver::SendVisibilityChangeEvent, |
| 45 weak_factory_.GetWeakPtr(), false), | 54 weak_factory_.GetWeakPtr(), false, visibility_gap_timeout_), |
| 46 visibility_gap_timeout_); | 55 visibility_gap_timeout_); |
| 47 } | 56 } |
| 48 | 57 |
| 49 void ChromeVisibilityObserver::OnBrowserRemoved(Browser* browser) { | 58 void ChromeVisibilityObserver::OnBrowserRemoved(Browser* browser) { |
| 50 // If there are no browser instances left then we should notify that browser | 59 // If there are no browser instances left then we should notify that browser |
| 51 // is not visible anymore immediately without waiting. | 60 // is not visible anymore immediately without waiting. |
| 52 if (BrowserList::GetInstance()->empty()) { | 61 if (BrowserList::GetInstance()->empty()) { |
| 53 CancelVisibilityChange(); | 62 CancelVisibilityChange(); |
| 54 SendVisibilityChangeEvent(false); | 63 SendVisibilityChangeEvent(false, kZeroTime); |
| 55 } | 64 } |
| 56 } | 65 } |
| 57 | 66 |
| 58 void ChromeVisibilityObserver::InitVisibilityGapTimeout() { | 67 void ChromeVisibilityObserver::InitVisibilityGapTimeout() { |
| 59 const int kDefaultVisibilityGapTimeout = 3; | 68 const int kDefaultVisibilityGapTimeout = 3; |
| 60 | 69 |
| 61 int timeout_seconds = kDefaultVisibilityGapTimeout; | 70 int timeout_seconds = kDefaultVisibilityGapTimeout; |
| 62 std::string param_value = variations::GetVariationParamValue( | 71 std::string param_value = variations::GetVariationParamValue( |
| 63 "DesktopEngagement", "visibility_gap_timeout"); | 72 "DesktopEngagement", "visibility_gap_timeout"); |
| 64 if (!param_value.empty()) | 73 if (!param_value.empty()) |
| 65 base::StringToInt(param_value, &timeout_seconds); | 74 base::StringToInt(param_value, &timeout_seconds); |
| 66 | 75 |
| 67 visibility_gap_timeout_ = base::TimeDelta::FromSeconds(timeout_seconds); | 76 visibility_gap_timeout_ = base::TimeDelta::FromSeconds(timeout_seconds); |
| 68 } | 77 } |
| 69 | 78 |
| 70 } // namespace metrics | 79 } // namespace metrics |
| OLD | NEW |