1 // Copyright 2014 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 COMPONENTS_GCM_DRIVER_GCM_ACCOUNT_TRACKER_H_
6 #define COMPONENTS_GCM_DRIVER_GCM_ACCOUNT_TRACKER_H_
11 #include "base/memory/scoped_vector.h"
12 #include "components/gcm_driver/gcm_client.h"
13 #include "components/gcm_driver/gcm_connection_observer.h"
14 #include "google_apis/gaia/account_tracker.h"
15 #include "google_apis/gaia/oauth2_token_service.h"
25 // Class for reporting back which accounts are signed into. It is only meant to
26 // be used when the user is signed into sync.
28 // This class makes a check for tokens periodically, to make sure the user is
29 // still logged into the profile, so that in the case that the user is not, we
30 // can immediately report that to the GCM and stop messages addressed to that
31 // user from ever reaching Chrome.
32 class GCMAccountTracker
: public gaia::AccountTracker::Observer
,
33 public OAuth2TokenService::Consumer
,
34 public GCMConnectionObserver
{
36 // State of the account.
37 // Allowed transitions:
38 // TOKEN_NEEDED - account info was created.
39 // TOKEN_NEEDED -> GETTING_TOKEN - access token was requested.
40 // GETTING_TOKEN -> TOKEN_NEEDED - access token fetching failed.
41 // GETTING_TOKEN -> TOKEN_PRESENT - access token fetching succeeded.
42 // GETTING_TOKEN -> ACCOUNT_REMOVED - account was removed.
43 // TOKEN_NEEDED -> ACCOUNT_REMOVED - account was removed.
44 // TOKEN_PRESENT -> ACCOUNT_REMOVED - account was removed.
46 TOKEN_NEEDED
, // Needs a token (AccountInfo was recently created or
47 // token request failed).
48 GETTING_TOKEN
, // There is a pending token request.
49 TOKEN_PRESENT
, // We have a token for the account.
50 ACCOUNT_REMOVED
, // Account was removed, and we didn't report it yet.
53 // Stores necessary account information and state of token fetching.
55 AccountInfo(const std::string
& email
, AccountState state
);
58 // Email address of the tracked account.
60 // OAuth2 access token, when |state| is TOKEN_PRESENT.
61 std::string access_token
;
62 // Expiration time of the access tokens.
63 base::Time expiration_time
;
64 // Status of the token fetching.
68 // |account_tracker| is used to deliver information about the accounts present
69 // in the browser context to |driver|.
70 GCMAccountTracker(scoped_ptr
<gaia::AccountTracker
> account_tracker
,
72 ~GCMAccountTracker() override
;
74 // Shuts down the tracker ensuring a proper clean up. After Shutdown() is
75 // called Start() and Stop() should no longer be used. Must be called before
79 // Starts tracking accounts.
82 // Gets the number of pending token requests. Only used for testing.
83 size_t get_pending_token_request_count() const {
84 return pending_token_requests_
.size();
88 friend class GCMAccountTrackerTest
;
90 // Maps account keys to account states. Keyed by account_ids as used by
91 // OAuth2TokenService.
92 typedef std::map
<std::string
, AccountInfo
> AccountInfos
;
94 // AccountTracker::Observer overrides.
95 void OnAccountAdded(const gaia::AccountIds
& ids
) override
;
96 void OnAccountRemoved(const gaia::AccountIds
& ids
) override
;
97 void OnAccountSignInChanged(const gaia::AccountIds
& ids
,
98 bool is_signed_in
) override
;
100 // OAuth2TokenService::Consumer overrides.
101 void OnGetTokenSuccess(const OAuth2TokenService::Request
* request
,
102 const std::string
& access_token
,
103 const base::Time
& expiration_time
) override
;
104 void OnGetTokenFailure(const OAuth2TokenService::Request
* request
,
105 const GoogleServiceAuthError
& error
) override
;
107 // GCMConnectionObserver overrides.
108 void OnConnected(const net::IPEndPoint
& ip_endpoint
) override
;
109 void OnDisconnected() override
;
111 // Schedules token reporting.
112 void ScheduleReportTokens();
113 // Report the list of accounts with OAuth2 tokens back using the |callback_|
114 // function. If there are token requests in progress, do nothing.
116 // Verify that all of the tokens are ready to be passed down to the GCM
117 // Driver, e.g. none of them has expired or is missing. Returns true if not
118 // all tokens are valid and a fetching yet more tokens is required.
119 void SanitizeTokens();
120 // Indicates whether token reporting is required, either because it is due, or
121 // some of the accounts were removed.
122 bool IsTokenReportingRequired() const;
123 // Indicates whether there are tokens that still need fetching.
124 bool IsTokenFetchingRequired() const;
125 // Gets the time until next token reporting.
126 base::TimeDelta
GetTimeToNextTokenReporting() const;
127 // Deletes a token request. Should be called from OnGetTokenSuccess(..) or
128 // OnGetTokenFailure(..).
129 void DeleteTokenRequest(const OAuth2TokenService::Request
* request
);
130 // Checks on all known accounts, and calls GetToken(..) for those with
131 // |state == TOKEN_NEEDED|.
132 void GetAllNeededTokens();
133 // Starts fetching the OAuth2 token for the GCM group scope.
134 void GetToken(AccountInfos::iterator
& account_iter
);
136 // Handling of actual sign in and sign out for accounts.
137 void OnAccountSignedIn(const gaia::AccountIds
& ids
);
138 void OnAccountSignedOut(const gaia::AccountIds
& ids
);
140 OAuth2TokenService
* GetTokenService();
143 scoped_ptr
<gaia::AccountTracker
> account_tracker_
;
145 // GCM Driver. Not owned.
148 // State of the account.
149 AccountInfos account_infos_
;
151 // Indicates whether shutdown has been called.
152 bool shutdown_called_
;
154 ScopedVector
<OAuth2TokenService::Request
> pending_token_requests_
;
156 // Creates weak pointers used to postpone reporting tokens. See
157 // ScheduleReportTokens.
158 base::WeakPtrFactory
<GCMAccountTracker
> reporting_weak_ptr_factory_
;
160 DISALLOW_COPY_AND_ASSIGN(GCMAccountTracker
);
165 #endif // COMPONENTS_GCM_DRIVER_GCM_ACCOUNT_TRACKER_H_