OLD | NEW |
(Empty) | |
| 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. |
| 4 |
| 5 #ifndef CHROME_BROWSER_SIGNIN_CROSS_DEVICE_PROMO_H_ |
| 6 #define CHROME_BROWSER_SIGNIN_CROSS_DEVICE_PROMO_H_ |
| 7 |
| 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" |
| 13 |
| 14 class PrefService; |
| 15 class SigninClient; |
| 16 class SigninManager; |
| 17 |
| 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 { |
| 25 public: |
| 26 class Observer { |
| 27 public: |
| 28 // Called if the state changes. |
| 29 virtual void OnPromoActivationChanged(bool active) = 0; |
| 30 }; |
| 31 |
| 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; |
| 38 |
| 39 // KeyedService: |
| 40 void Shutdown() override; |
| 41 |
| 42 void AddObserver(CrossDevicePromo::Observer* observer); |
| 43 void RemoveObserver(CrossDevicePromo::Observer* observer); |
| 44 |
| 45 // GaiaCookieManagerService::Observer: |
| 46 void OnGaiaAccountsInCookieUpdated( |
| 47 const std::vector<std::pair<std::string, bool>>& accounts, |
| 48 const GoogleServiceAuthError& error) override; |
| 49 |
| 50 // DeviceActivityFetcher::Observer: |
| 51 void OnFetchDeviceActivitySuccess( |
| 52 const std::vector<DeviceActivityFetcher::DeviceActivity>& devices) |
| 53 override; |
| 54 void OnFetchDeviceActivityFailure() override; |
| 55 |
| 56 // Profile should be shown the promo. |
| 57 bool IsPromoActive(); |
| 58 |
| 59 // User elects to opt out of this promo. |
| 60 void OptOut(); |
| 61 |
| 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); |
| 65 |
| 66 // Call this only in tests, please! |
| 67 bool CheckPromoEligibilityForTesting() { return CheckPromoEligibility(); } |
| 68 |
| 69 private: |
| 70 // Load configuration parameters from the Variations Seed. |
| 71 void Init(); |
| 72 |
| 73 // Set whether the promo is active or inactive. |
| 74 void MarkPromoActive(); |
| 75 void MarkPromoInactive(); |
| 76 |
| 77 // Perform checks if the promo should be displayed to this profile. |
| 78 bool CheckPromoEligibility(); |
| 79 |
| 80 // Helper to get the time value stored in a pref. |
| 81 base::Time GetTimePref(const std::string& pref); |
| 82 |
| 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(); |
| 87 |
| 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(); |
| 92 |
| 93 // Begin authenticating and then fetching the devices with the same account. |
| 94 void GetDevicesActivityForAccountInCookie(); |
| 95 |
| 96 // Has the service been initialized. If false, the promo is inactive. |
| 97 bool initialized_; |
| 98 |
| 99 // ProfileKeyedServices and the PrefService are weak pointers. |
| 100 SigninManager* signin_manager_; |
| 101 GaiaCookieManagerService* cookie_manager_service_; |
| 102 PrefService* prefs_; |
| 103 SigninClient* signin_client_; |
| 104 |
| 105 scoped_ptr<DeviceActivityFetcher> device_activity_fetcher_; |
| 106 ObserverList<CrossDevicePromo::Observer> observer_list_; |
| 107 |
| 108 // Maximum time since activity seen on another device that activity is |
| 109 // considered within a context switch. |
| 110 base::TimeDelta context_switch_duration_; |
| 111 |
| 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_; |
| 116 |
| 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_; |
| 120 |
| 121 // Time between noted browsing activity to determine when a new Browsing |
| 122 // Session has started. |
| 123 base::TimeDelta inactivity_between_browsing_sessions_; |
| 124 |
| 125 // Throttles some portion of RPCs so they never get executed, based on the |
| 126 // variations configuration. |
| 127 bool is_throttled_; |
| 128 |
| 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_; |
| 132 |
| 133 // Used to delay the check of Device Activity. |
| 134 base::OneShotTimer<CrossDevicePromo> device_activity_timer_; |
| 135 |
| 136 DISALLOW_COPY_AND_ASSIGN(CrossDevicePromo); |
| 137 }; |
| 138 |
| 139 #endif // CHROME_BROWSER_SIGNIN_CROSS_DEVICE_PROMO_H_ |
OLD | NEW |