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 void ChromeVisibilityObserver::OnBrowserSetLastActive(Browser* browser) { | |
35 if (weak_factory_.HasWeakPtrs()) | |
36 CancelVisibilityChange(); | |
37 else | |
38 SendVisibilityChangeEvent(true); | |
39 } | |
40 | |
41 void ChromeVisibilityObserver::OnBrowserNoLongerActive(Browser* browser) { | |
42 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask( | |
43 FROM_HERE, | |
44 base::Bind(&ChromeVisibilityObserver::SendVisibilityChangeEvent, | |
45 weak_factory_.GetWeakPtr(), false), | |
46 visibility_gap_timeout_); | |
47 } | |
48 | |
49 void ChromeVisibilityObserver::OnBrowserRemoved(Browser* browser) { | |
50 // If there are no browser instances left then we should notify that browser | |
51 // is not visible anymore immediately without waiting. | |
52 if (BrowserList::GetInstance()->empty()) { | |
53 CancelVisibilityChange(); | |
54 SendVisibilityChangeEvent(false); | |
55 } | |
56 } | |
57 | |
58 void ChromeVisibilityObserver::InitVisibilityGapTimeout() { | |
59 const int kDefaultVisibilityGapTimeout = 3; | |
60 | |
61 int timeout_seconds = kDefaultVisibilityGapTimeout; | |
62 std::string param_value = variations::GetVariationParamValue( | |
63 "DesktopEngagement", "visibility_gap_timeout"); | |
64 if (!param_value.empty()) | |
65 base::StringToInt(param_value, &timeout_seconds); | |
66 | |
67 visibility_gap_timeout_ = base::TimeDelta::FromSeconds(timeout_seconds); | |
68 } | |
69 | |
70 } // namespace metrics | |
OLD | NEW |