OLD | NEW |
| (Empty) |
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. | |
4 | |
5 #ifndef CHROME_BROWSER_SERVICES_GCM_GCM_ACCOUNT_TRACKER_H_ | |
6 #define CHROME_BROWSER_SERVICES_GCM_GCM_ACCOUNT_TRACKER_H_ | |
7 | |
8 #include <map> | |
9 #include <string> | |
10 | |
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" | |
16 | |
17 namespace base { | |
18 class Time; | |
19 } | |
20 | |
21 namespace gcm { | |
22 | |
23 class GCMDriver; | |
24 | |
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. | |
27 // | |
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 { | |
35 public: | |
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. | |
45 enum AccountState { | |
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. | |
51 }; | |
52 | |
53 // Stores necessary account information and state of token fetching. | |
54 struct AccountInfo { | |
55 AccountInfo(const std::string& email, AccountState state); | |
56 ~AccountInfo(); | |
57 | |
58 // Email address of the tracked account. | |
59 std::string email; | |
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. | |
65 AccountState state; | |
66 }; | |
67 | |
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, | |
71 GCMDriver* driver); | |
72 ~GCMAccountTracker() override; | |
73 | |
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 | |
76 // destruction. | |
77 void Shutdown(); | |
78 | |
79 // Starts tracking accounts. | |
80 void Start(); | |
81 | |
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(); | |
85 } | |
86 | |
87 private: | |
88 friend class GCMAccountTrackerTest; | |
89 | |
90 // Maps account keys to account states. Keyed by account_ids as used by | |
91 // OAuth2TokenService. | |
92 typedef std::map<std::string, AccountInfo> AccountInfos; | |
93 | |
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; | |
99 | |
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; | |
106 | |
107 // GCMConnectionObserver overrides. | |
108 void OnConnected(const net::IPEndPoint& ip_endpoint) override; | |
109 void OnDisconnected() override; | |
110 | |
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. | |
115 void ReportTokens(); | |
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); | |
135 | |
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); | |
139 | |
140 OAuth2TokenService* GetTokenService(); | |
141 | |
142 // Account tracker. | |
143 scoped_ptr<gaia::AccountTracker> account_tracker_; | |
144 | |
145 // GCM Driver. Not owned. | |
146 GCMDriver* driver_; | |
147 | |
148 // State of the account. | |
149 AccountInfos account_infos_; | |
150 | |
151 // Indicates whether shutdown has been called. | |
152 bool shutdown_called_; | |
153 | |
154 ScopedVector<OAuth2TokenService::Request> pending_token_requests_; | |
155 | |
156 // Creates weak pointers used to postpone reporting tokens. See | |
157 // ScheduleReportTokens. | |
158 base::WeakPtrFactory<GCMAccountTracker> reporting_weak_ptr_factory_; | |
159 | |
160 DISALLOW_COPY_AND_ASSIGN(GCMAccountTracker); | |
161 }; | |
162 | |
163 } // namespace gcm | |
164 | |
165 #endif // CHROME_BROWSER_SERVICES_GCM_GCM_ACCOUNT_TRACKER_H_ | |
OLD | NEW |