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

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: fixes 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 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 there are no browser instances left then we should notify that browser
56 // is not visible anymore immediately without waiting.
57 if (BrowserList::GetInstance()->empty()) {
58 CancelVisibilityChange();
59 SendVisibilityChangeEvent(false);
60 }
61 }
62
63 void ChromeVisibilityObserver::InitVisibilityGapTimeout() {
64 const int kDefaultVisibilityGapTimeout = 3;
65
66 int timeout_seconds = kDefaultVisibilityGapTimeout;
67 std::string param_value = variations::GetVariationParamValue(
68 "DesktopEngagement", "visibility_gap_timeout");
69 if (!param_value.empty())
70 base::StringToInt(param_value, &timeout_seconds);
71
72 visibility_gap_timeout_ = base::TimeDelta::FromSeconds(timeout_seconds);
73 }
74
75 } // namespace metrics
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698