Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(72)

Side by Side Diff: chrome/browser/metrics/desktop_engagement/chrome_visibility_observer.cc

Issue 2142983002: Add desktop engagement metrics. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 4 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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 namespace {
18 ChromeVisibilityObserver* g_instance = nullptr;
19 }
20
21 ChromeVisibilityObserver::ChromeVisibilityObserver() : weak_factory_(this) {
22 BrowserList::AddObserver(this);
23 InitVisibilityGapTimeout();
24 }
25
26 ChromeVisibilityObserver::~ChromeVisibilityObserver() {
27 BrowserList::RemoveObserver(this);
28 }
29
30 void ChromeVisibilityObserver::SendVisibilityChangeEvent(bool active) {
31 DesktopEngagementService::Get()->OnVisibilityChanged(active);
32 }
33
34 void ChromeVisibilityObserver::CancelVisibilityChange() {
35 weak_factory_.InvalidateWeakPtrs();
36 }
37
38 // static
39 void ChromeVisibilityObserver::Initialize() {
Alexei Svitkine (slow) 2016/07/28 21:31:45 Sorry, I missed this before, but can you change th
gayane -on leave until 09-2017 2016/07/29 19:37:39 Done.
40 g_instance = new ChromeVisibilityObserver();
41 }
42
43 void ChromeVisibilityObserver::OnBrowserSetLastActive(Browser* browser) {
44 if (weak_factory_.HasWeakPtrs())
45 CancelVisibilityChange();
46 else
47 SendVisibilityChangeEvent(true);
48 }
49
50 void ChromeVisibilityObserver::OnBrowserNoLongerActive(Browser* browser) {
51 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask(
52 FROM_HERE,
53 base::Bind(&ChromeVisibilityObserver::SendVisibilityChangeEvent,
54 weak_factory_.GetWeakPtr(), false),
55 visibility_gap_timeout_);
56 }
57
58 void ChromeVisibilityObserver::OnBrowserRemoved(Browser* browser) {
59 // If there are no browser instances left then we should notify that browser
60 // is not visible anymore immediately without waiting.
61 if (BrowserList::GetInstance()->empty()) {
62 CancelVisibilityChange();
63 SendVisibilityChangeEvent(false);
64 }
65 }
66
67 void ChromeVisibilityObserver::InitVisibilityGapTimeout() {
68 const int kDefaultVisibilityGapTimeout = 3;
69
70 int timeout_seconds = kDefaultVisibilityGapTimeout;
71 std::string param_value = variations::GetVariationParamValue(
72 "DesktopEngagement", "visibility_gap_timeout");
73 if (!param_value.empty())
74 base::StringToInt(param_value, &timeout_seconds);
75
76 visibility_gap_timeout_ = base::TimeDelta::FromSeconds(timeout_seconds);
77 }
78
79 } // namespace metrics
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698