1 // Copyright 2015 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.
5 #ifndef CHROME_BROWSER_SIGNIN_CROSS_DEVICE_PROMO_H_
6 #define CHROME_BROWSER_SIGNIN_CROSS_DEVICE_PROMO_H_
8 #include "base/observer_list.h"
9 #include "base/timer/timer.h"
10 #include "components/keyed_service/core/keyed_service.h"
11 #include "components/signin/core/browser/device_activity_fetcher.h"
12 #include "components/signin/core/browser/gaia_cookie_manager_service.h"
18 // This defines whether the cross device signin promo should be displayed for
19 // this profile, and owns whether the user has opted out of the promo. The promo
20 // targets those who sign into Chrome on other devices, who are not signed in
21 // locally but use only one account in the content area.
22 class CrossDevicePromo
: public KeyedService
,
23 public GaiaCookieManagerService::Observer
,
24 public DeviceActivityFetcher::Observer
{
28 // Called if the state changes.
29 virtual void OnPromoActivationChanged(bool active
) = 0;
32 // Constructor accepts weak pointers to required services.
33 explicit CrossDevicePromo(SigninManager
* signin_manager
,
34 GaiaCookieManagerService
* cookie_manager_service
,
35 SigninClient
* signin_client
,
36 PrefService
* pref_service
);
37 ~CrossDevicePromo() override
;
40 void Shutdown() override
;
42 void AddObserver(CrossDevicePromo::Observer
* observer
);
43 void RemoveObserver(CrossDevicePromo::Observer
* observer
);
45 // GaiaCookieManagerService::Observer:
46 void OnGaiaAccountsInCookieUpdated(
47 const std::vector
<gaia::ListedAccount
>& accounts
,
48 const GoogleServiceAuthError
& error
) override
;
50 // DeviceActivityFetcher::Observer:
51 void OnFetchDeviceActivitySuccess(
52 const std::vector
<DeviceActivityFetcher::DeviceActivity
>& devices
)
54 void OnFetchDeviceActivityFailure() override
;
56 // Profile should be shown the promo.
59 // User elects to opt out of this promo.
62 // Called whenever the Last Active Time changes. This is used to determine
63 // when a new browsing session occurs.
64 void MaybeBrowsingSessionStarted(const base::Time
& previous_last_active
);
66 // Call this only in tests, please!
67 bool CheckPromoEligibilityForTesting() { return CheckPromoEligibility(); }
70 // Load configuration parameters from the Variations Seed.
73 // Set whether the promo is active or inactive.
74 void MarkPromoActive();
75 void MarkPromoInactive();
77 // Perform checks if the promo should be displayed to this profile.
78 bool CheckPromoEligibility();
80 // Helper to get the time value stored in a pref.
81 base::Time
GetTimePref(const std::string
& pref
);
83 // Perform checks if the promo should be displayed to this profile. This will
84 // not write any prefs or initiate any checks that are otherwise called in
85 // CheckPromoEligibility. Records no metrics. Used for DCHECKs.
86 bool VerifyPromoEligibleReadOnly();
88 // Track that there is exactly one account in the cookie jar for a period
89 // of time in order to activate the promo.
90 void RegisterForCookieChanges();
91 void UnregisterForCookieChanges();
93 // Begin authenticating and then fetching the devices with the same account.
94 void GetDevicesActivityForAccountInCookie();
96 // Has the service been initialized. If false, the promo is inactive.
99 // ProfileKeyedServices and the PrefService are weak pointers.
100 SigninManager
* signin_manager_
;
101 GaiaCookieManagerService
* cookie_manager_service_
;
103 SigninClient
* signin_client_
;
105 scoped_ptr
<DeviceActivityFetcher
> device_activity_fetcher_
;
106 base::ObserverList
<CrossDevicePromo::Observer
> observer_list_
;
108 // Maximum time since activity seen on another device that activity is
109 // considered within a context switch.
110 base::TimeDelta context_switch_duration_
;
112 // Max time until the next Device Activity check. For the first test, this
113 // will be a uniformly random time between now and the max delay specified
114 // from Variations; otherwise we use the max delay as read from variations.
115 base::TimeDelta delay_until_next_list_devices_
;
117 // Minimum time a single account must be in the cookie jar to consider the
118 // machine as used by only one person.
119 base::TimeDelta single_account_duration_threshold_
;
121 // Time between noted browsing activity to determine when a new Browsing
122 // Session has started.
123 base::TimeDelta inactivity_between_browsing_sessions_
;
125 // Throttles some portion of RPCs so they never get executed, based on the
126 // variations configuration.
129 // Metric to help us track how long a browsing session is. Useful for
130 // configurigng the promo.
131 base::Time start_last_browsing_session_
;
133 // Used to delay the check of Device Activity.
134 base::OneShotTimer
<CrossDevicePromo
> device_activity_timer_
;
136 DISALLOW_COPY_AND_ASSIGN(CrossDevicePromo
);
139 #endif // CHROME_BROWSER_SIGNIN_CROSS_DEVICE_PROMO_H_