OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef CHROME_BROWSER_SERVICES_GCM_GCM_ACCOUNT_TRACKER_H_ | 5 #ifndef CHROME_BROWSER_SERVICES_GCM_GCM_ACCOUNT_TRACKER_H_ |
6 #define CHROME_BROWSER_SERVICES_GCM_GCM_ACCOUNT_TRACKER_H_ | 6 #define CHROME_BROWSER_SERVICES_GCM_GCM_ACCOUNT_TRACKER_H_ |
7 | 7 |
8 #include <map> | 8 #include <map> |
9 #include <string> | 9 #include <string> |
10 | 10 |
11 #include "base/memory/scoped_vector.h" | 11 #include "base/memory/scoped_vector.h" |
12 #include "components/gcm_driver/gcm_client.h" | 12 #include "components/gcm_driver/gcm_client.h" |
13 #include "components/gcm_driver/gcm_connection_observer.h" | 13 #include "components/gcm_driver/gcm_connection_observer.h" |
14 #include "google_apis/gaia/account_tracker.h" | 14 #include "google_apis/gaia/account_tracker.h" |
15 #include "google_apis/gaia/oauth2_token_service.h" | 15 #include "google_apis/gaia/oauth2_token_service.h" |
16 | 16 |
17 namespace base { | |
18 class Time; | |
19 } | |
20 | |
21 namespace gcm { | 17 namespace gcm { |
22 | 18 |
23 class GCMDriver; | 19 class GCMDriver; |
24 | 20 |
25 // Class for reporting back which accounts are signed into. It is only meant to | 21 // Class for reporting back which accounts are signed into. It is only meant to |
26 // be used when the user is signed into sync. | 22 // 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, | 23 class GCMAccountTracker : public gaia::AccountTracker::Observer, |
33 public OAuth2TokenService::Consumer, | 24 public OAuth2TokenService::Consumer, |
34 public GCMConnectionObserver { | 25 public GCMConnectionObserver { |
35 public: | 26 public: |
36 // State of the account. | 27 // State of the account. |
37 // Allowed transitions: | 28 // Allowed transitions: |
38 // TOKEN_NEEDED - account info was created. | 29 // TOKEN_NEEDED - account info was created. |
39 // TOKEN_NEEDED -> GETTING_TOKEN - access token was requested. | 30 // TOKEN_NEEDED -> GETTING_TOKEN - access token was requested. |
40 // GETTING_TOKEN -> TOKEN_NEEDED - access token fetching failed. | 31 // GETTING_TOKEN -> TOKEN_NEEDED - access token fetching failed. |
41 // GETTING_TOKEN -> TOKEN_PRESENT - access token fetching succeeded. | 32 // GETTING_TOKEN -> TOKEN_PRESENT - access token fetching succeeded. |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
78 | 69 |
79 // Starts tracking accounts. | 70 // Starts tracking accounts. |
80 void Start(); | 71 void Start(); |
81 | 72 |
82 // Gets the number of pending token requests. Only used for testing. | 73 // Gets the number of pending token requests. Only used for testing. |
83 size_t get_pending_token_request_count() const { | 74 size_t get_pending_token_request_count() const { |
84 return pending_token_requests_.size(); | 75 return pending_token_requests_.size(); |
85 } | 76 } |
86 | 77 |
87 private: | 78 private: |
88 friend class GCMAccountTrackerTest; | |
89 | |
90 // Maps account keys to account states. Keyed by account_ids as used by | 79 // Maps account keys to account states. Keyed by account_ids as used by |
91 // OAuth2TokenService. | 80 // OAuth2TokenService. |
92 typedef std::map<std::string, AccountInfo> AccountInfos; | 81 typedef std::map<std::string, AccountInfo> AccountInfos; |
93 | 82 |
94 // AccountTracker::Observer overrides. | 83 // AccountTracker::Observer overrides. |
95 void OnAccountAdded(const gaia::AccountIds& ids) override; | 84 void OnAccountAdded(const gaia::AccountIds& ids) override; |
96 void OnAccountRemoved(const gaia::AccountIds& ids) override; | 85 void OnAccountRemoved(const gaia::AccountIds& ids) override; |
97 void OnAccountSignInChanged(const gaia::AccountIds& ids, | 86 void OnAccountSignInChanged(const gaia::AccountIds& ids, |
98 bool is_signed_in) override; | 87 bool is_signed_in) override; |
99 | 88 |
100 // OAuth2TokenService::Consumer overrides. | 89 // OAuth2TokenService::Consumer overrides. |
101 void OnGetTokenSuccess(const OAuth2TokenService::Request* request, | 90 void OnGetTokenSuccess(const OAuth2TokenService::Request* request, |
102 const std::string& access_token, | 91 const std::string& access_token, |
103 const base::Time& expiration_time) override; | 92 const base::Time& expiration_time) override; |
104 void OnGetTokenFailure(const OAuth2TokenService::Request* request, | 93 void OnGetTokenFailure(const OAuth2TokenService::Request* request, |
105 const GoogleServiceAuthError& error) override; | 94 const GoogleServiceAuthError& error) override; |
106 | 95 |
107 // GCMConnectionObserver overrides. | 96 // GCMConnectionObserver overrides. |
108 void OnConnected(const net::IPEndPoint& ip_endpoint) override; | 97 void OnConnected(const net::IPEndPoint& ip_endpoint) override; |
109 void OnDisconnected() override; | 98 void OnDisconnected() override; |
110 | 99 |
111 // Schedules token reporting. | |
112 void ScheduleReportTokens(); | |
113 // Report the list of accounts with OAuth2 tokens back using the |callback_| | 100 // Report the list of accounts with OAuth2 tokens back using the |callback_| |
114 // function. If there are token requests in progress, do nothing. | 101 // function. If there are token requests in progress, do nothing. |
115 void ReportTokens(); | 102 void CompleteCollectingTokens(); |
116 // Verify that all of the tokens are ready to be passed down to the GCM | 103 // 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 | 104 // 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. | 105 // all tokens are valid and a fetching yet more tokens is required. |
119 void SanitizeTokens(); | 106 bool 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 | 107 // Deletes a token request. Should be called from OnGetTokenSuccess(..) or |
128 // OnGetTokenFailure(..). | 108 // OnGetTokenFailure(..). |
129 void DeleteTokenRequest(const OAuth2TokenService::Request* request); | 109 void DeleteTokenRequest(const OAuth2TokenService::Request* request); |
130 // Checks on all known accounts, and calls GetToken(..) for those with | 110 // Checks on all known accounts, and calls GetToken(..) for those with |
131 // |state == TOKEN_NEEDED|. | 111 // |state == TOKEN_NEEDED|. |
132 void GetAllNeededTokens(); | 112 void GetAllNeededTokens(); |
133 // Starts fetching the OAuth2 token for the GCM group scope. | 113 // Starts fetching the OAuth2 token for the GCM group scope. |
134 void GetToken(AccountInfos::iterator& account_iter); | 114 void GetToken(AccountInfos::iterator& account_iter); |
135 | 115 |
136 // Handling of actual sign in and sign out for accounts. | 116 // Handling of actual sign in and sign out for accounts. |
137 void OnAccountSignedIn(const gaia::AccountIds& ids); | 117 void OnAccountSignedIn(const gaia::AccountIds& ids); |
138 void OnAccountSignedOut(const gaia::AccountIds& ids); | 118 void OnAccountSignedOut(const gaia::AccountIds& ids); |
139 | 119 |
140 OAuth2TokenService* GetTokenService(); | 120 OAuth2TokenService* GetTokenService(); |
141 | 121 |
142 // Account tracker. | 122 // Account tracker. |
143 scoped_ptr<gaia::AccountTracker> account_tracker_; | 123 scoped_ptr<gaia::AccountTracker> account_tracker_; |
144 | 124 |
145 // GCM Driver. Not owned. | 125 // GCM Driver. Not owned. |
146 GCMDriver* driver_; | 126 GCMDriver* driver_; |
147 | 127 |
148 // State of the account. | 128 // State of the account. |
149 AccountInfos account_infos_; | 129 AccountInfos account_infos_; |
150 | 130 |
151 // Indicates whether shutdown has been called. | 131 // Indicates whether shutdown has been called. |
152 bool shutdown_called_; | 132 bool shutdown_called_; |
153 | 133 |
154 ScopedVector<OAuth2TokenService::Request> pending_token_requests_; | 134 ScopedVector<OAuth2TokenService::Request> pending_token_requests_; |
155 | 135 |
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); | 136 DISALLOW_COPY_AND_ASSIGN(GCMAccountTracker); |
161 }; | 137 }; |
162 | 138 |
163 } // namespace gcm | 139 } // namespace gcm |
164 | 140 |
165 #endif // CHROME_BROWSER_SERVICES_GCM_GCM_ACCOUNT_TRACKER_H_ | 141 #endif // CHROME_BROWSER_SERVICES_GCM_GCM_ACCOUNT_TRACKER_H_ |
OLD | NEW |