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 #include "chrome/browser/services/gcm/gcm_account_tracker.h" | 5 #include "chrome/browser/services/gcm/gcm_account_tracker.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 #include <vector> | 8 #include <vector> |
9 | 9 |
10 #include "base/time/time.h" | 10 #include "base/time/time.h" |
11 #include "google_apis/gaia/google_service_auth_error.h" | 11 #include "google_apis/gaia/google_service_auth_error.h" |
12 | 12 |
13 namespace gcm { | 13 namespace gcm { |
14 | 14 |
15 namespace { | 15 namespace { |
16 const char kGCMGroupServerScope[] = | 16 const char kGCMGroupServerScope[] = "https://www.googleapis.com/auth/gcm"; |
Nicolas Zea
2014/07/10 20:57:32
Why is this changing?
fgorski
2014/07/11 22:51:20
I am correcting the scope here, now that I was abl
| |
17 "oauth2:https://www.googleapis.com/auth/gcm"; | |
18 const char kGCMAccountTrackerName[] = "gcm_account_tracker"; | 17 const char kGCMAccountTrackerName[] = "gcm_account_tracker"; |
19 } // namespace | 18 } // namespace |
20 | 19 |
21 GCMAccountTracker::AccountInfo::AccountInfo(const std::string& email, | 20 GCMAccountTracker::AccountInfo::AccountInfo(const std::string& email, |
22 AccountState state) | 21 AccountState state) |
23 : email(email), state(state) { | 22 : email(email), state(state) { |
24 } | 23 } |
25 | 24 |
26 GCMAccountTracker::AccountInfo::~AccountInfo() { | 25 GCMAccountTracker::AccountInfo::~AccountInfo() { |
27 } | 26 } |
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
68 GetAllNeededTokens(); | 67 GetAllNeededTokens(); |
69 } | 68 } |
70 | 69 |
71 void GCMAccountTracker::Stop() { | 70 void GCMAccountTracker::Stop() { |
72 DCHECK(!shutdown_called_); | 71 DCHECK(!shutdown_called_); |
73 account_tracker_->RemoveObserver(this); | 72 account_tracker_->RemoveObserver(this); |
74 pending_token_requests_.clear(); | 73 pending_token_requests_.clear(); |
75 } | 74 } |
76 | 75 |
77 void GCMAccountTracker::OnAccountAdded(const gaia::AccountIds& ids) { | 76 void GCMAccountTracker::OnAccountAdded(const gaia::AccountIds& ids) { |
77 DVLOG(1) << "Account added: " << ids.email; | |
78 // We listen for the account signing in, which happens after account is added. | 78 // We listen for the account signing in, which happens after account is added. |
79 } | 79 } |
80 | 80 |
81 void GCMAccountTracker::OnAccountRemoved(const gaia::AccountIds& ids) { | 81 void GCMAccountTracker::OnAccountRemoved(const gaia::AccountIds& ids) { |
82 DVLOG(1) << "Account removed: " << ids.email; | |
82 // We listen for the account signing out, which happens before account is | 83 // We listen for the account signing out, which happens before account is |
83 // removed. | 84 // removed. |
84 } | 85 } |
85 | 86 |
86 void GCMAccountTracker::OnAccountSignInChanged(const gaia::AccountIds& ids, | 87 void GCMAccountTracker::OnAccountSignInChanged(const gaia::AccountIds& ids, |
87 bool is_signed_in) { | 88 bool is_signed_in) { |
88 if (is_signed_in) | 89 if (is_signed_in) |
89 OnAccountSignedIn(ids); | 90 OnAccountSignedIn(ids); |
90 else | 91 else |
91 OnAccountSignedOut(ids); | 92 OnAccountSignedOut(ids); |
92 } | 93 } |
93 | 94 |
94 void GCMAccountTracker::OnGetTokenSuccess( | 95 void GCMAccountTracker::OnGetTokenSuccess( |
95 const OAuth2TokenService::Request* request, | 96 const OAuth2TokenService::Request* request, |
96 const std::string& access_token, | 97 const std::string& access_token, |
97 const base::Time& expiration_time) { | 98 const base::Time& expiration_time) { |
98 DCHECK(request); | 99 DCHECK(request); |
99 DCHECK(!request->GetAccountId().empty()); | 100 DCHECK(!request->GetAccountId().empty()); |
101 DVLOG(1) << "Get token success: " << request->GetAccountId(); | |
100 | 102 |
101 AccountInfos::iterator iter = account_infos_.find(request->GetAccountId()); | 103 AccountInfos::iterator iter = account_infos_.find(request->GetAccountId()); |
102 DCHECK(iter != account_infos_.end()); | 104 DCHECK(iter != account_infos_.end()); |
103 if (iter != account_infos_.end()) { | 105 if (iter != account_infos_.end()) { |
104 DCHECK(iter->second.state == GETTING_TOKEN || | 106 DCHECK(iter->second.state == GETTING_TOKEN || |
105 iter->second.state == ACCOUNT_REMOVED); | 107 iter->second.state == ACCOUNT_REMOVED); |
106 // If OnAccountSignedOut(..) was called most recently, account is kept in | 108 // If OnAccountSignedOut(..) was called most recently, account is kept in |
107 // ACCOUNT_REMOVED state. | 109 // ACCOUNT_REMOVED state. |
108 if (iter->second.state == GETTING_TOKEN) { | 110 if (iter->second.state == GETTING_TOKEN) { |
109 iter->second.state = TOKEN_PRESENT; | 111 iter->second.state = TOKEN_PRESENT; |
110 iter->second.access_token = access_token; | 112 iter->second.access_token = access_token; |
111 } | 113 } |
112 } | 114 } |
113 | 115 |
114 DeleteTokenRequest(request); | 116 DeleteTokenRequest(request); |
115 CompleteCollectingTokens(); | 117 CompleteCollectingTokens(); |
116 } | 118 } |
117 | 119 |
118 void GCMAccountTracker::OnGetTokenFailure( | 120 void GCMAccountTracker::OnGetTokenFailure( |
119 const OAuth2TokenService::Request* request, | 121 const OAuth2TokenService::Request* request, |
120 const GoogleServiceAuthError& error) { | 122 const GoogleServiceAuthError& error) { |
121 DCHECK(request); | 123 DCHECK(request); |
122 DCHECK(!request->GetAccountId().empty()); | 124 DCHECK(!request->GetAccountId().empty()); |
125 DVLOG(1) << "Get token failure: " << request->GetAccountId(); | |
123 | 126 |
124 AccountInfos::iterator iter = account_infos_.find(request->GetAccountId()); | 127 AccountInfos::iterator iter = account_infos_.find(request->GetAccountId()); |
125 DCHECK(iter != account_infos_.end()); | 128 DCHECK(iter != account_infos_.end()); |
126 if (iter != account_infos_.end()) { | 129 if (iter != account_infos_.end()) { |
127 DCHECK(iter->second.state == GETTING_TOKEN || | 130 DCHECK(iter->second.state == GETTING_TOKEN || |
128 iter->second.state == ACCOUNT_REMOVED); | 131 iter->second.state == ACCOUNT_REMOVED); |
129 // If OnAccountSignedOut(..) was called most recently, account is kept in | 132 // If OnAccountSignedOut(..) was called most recently, account is kept in |
130 // ACCOUNT_REMOVED state. | 133 // ACCOUNT_REMOVED state. |
131 if (iter->second.state == GETTING_TOKEN) | 134 if (iter->second.state == GETTING_TOKEN) |
132 iter->second.state = TOKEN_NEEDED; | 135 iter->second.state = TOKEN_NEEDED; |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
174 | 177 |
175 case TOKEN_NEEDED: | 178 case TOKEN_NEEDED: |
176 // We failed to fetch an access token for the account, but it has not | 179 // We failed to fetch an access token for the account, but it has not |
177 // been signed out (perhaps there is a network issue). We don't report | 180 // been signed out (perhaps there is a network issue). We don't report |
178 // it, but next time there is a sign-in change we will update its state. | 181 // it, but next time there is a sign-in change we will update its state. |
179 ++iter; | 182 ++iter; |
180 break; | 183 break; |
181 } | 184 } |
182 } | 185 } |
183 | 186 |
184 callback_.Run(account_tokens, account_removed); | 187 // Make sure that there is something to report, otherwise bail out. |
188 if (!account_tokens.empty() || account_removed) { | |
189 DVLOG(1) << "Calling callback: " << account_tokens.size() << ", " | |
190 << (account_removed ? "" : "no ") << "account removed."; | |
191 callback_.Run(account_tokens, account_removed); | |
192 } else { | |
193 DVLOG(1) << "Skipping calling callback."; | |
194 } | |
185 } | 195 } |
186 | 196 |
187 void GCMAccountTracker::DeleteTokenRequest( | 197 void GCMAccountTracker::DeleteTokenRequest( |
188 const OAuth2TokenService::Request* request) { | 198 const OAuth2TokenService::Request* request) { |
189 ScopedVector<OAuth2TokenService::Request>::iterator iter = std::find( | 199 ScopedVector<OAuth2TokenService::Request>::iterator iter = std::find( |
190 pending_token_requests_.begin(), pending_token_requests_.end(), request); | 200 pending_token_requests_.begin(), pending_token_requests_.end(), request); |
191 if (iter != pending_token_requests_.end()) | 201 if (iter != pending_token_requests_.end()) |
192 pending_token_requests_.erase(iter); | 202 pending_token_requests_.erase(iter); |
193 } | 203 } |
194 | 204 |
(...skipping 13 matching lines...) Expand all Loading... | |
208 OAuth2TokenService::ScopeSet scopes; | 218 OAuth2TokenService::ScopeSet scopes; |
209 scopes.insert(kGCMGroupServerScope); | 219 scopes.insert(kGCMGroupServerScope); |
210 scoped_ptr<OAuth2TokenService::Request> request = | 220 scoped_ptr<OAuth2TokenService::Request> request = |
211 GetTokenService()->StartRequest(account_iter->first, scopes, this); | 221 GetTokenService()->StartRequest(account_iter->first, scopes, this); |
212 | 222 |
213 pending_token_requests_.push_back(request.release()); | 223 pending_token_requests_.push_back(request.release()); |
214 account_iter->second.state = GETTING_TOKEN; | 224 account_iter->second.state = GETTING_TOKEN; |
215 } | 225 } |
216 | 226 |
217 void GCMAccountTracker::OnAccountSignedIn(const gaia::AccountIds& ids) { | 227 void GCMAccountTracker::OnAccountSignedIn(const gaia::AccountIds& ids) { |
228 DVLOG(1) << "Account signed in: " << ids.email; | |
218 AccountInfos::iterator iter = account_infos_.find(ids.account_key); | 229 AccountInfos::iterator iter = account_infos_.find(ids.account_key); |
219 if (iter == account_infos_.end()) { | 230 if (iter == account_infos_.end()) { |
220 DCHECK(!ids.email.empty()); | 231 DCHECK(!ids.email.empty()); |
221 account_infos_.insert( | 232 account_infos_.insert( |
222 std::make_pair(ids.account_key, AccountInfo(ids.email, TOKEN_NEEDED))); | 233 std::make_pair(ids.account_key, AccountInfo(ids.email, TOKEN_NEEDED))); |
223 } else if (iter->second.state == ACCOUNT_REMOVED) { | 234 } else if (iter->second.state == ACCOUNT_REMOVED) { |
224 iter->second.state = TOKEN_NEEDED; | 235 iter->second.state = TOKEN_NEEDED; |
225 } | 236 } |
226 | 237 |
227 GetAllNeededTokens(); | 238 GetAllNeededTokens(); |
228 } | 239 } |
229 | 240 |
230 void GCMAccountTracker::OnAccountSignedOut(const gaia::AccountIds& ids) { | 241 void GCMAccountTracker::OnAccountSignedOut(const gaia::AccountIds& ids) { |
242 DVLOG(1) << "Account signed out: " << ids.email; | |
231 AccountInfos::iterator iter = account_infos_.find(ids.account_key); | 243 AccountInfos::iterator iter = account_infos_.find(ids.account_key); |
232 if (iter == account_infos_.end()) | 244 if (iter == account_infos_.end()) |
233 return; | 245 return; |
234 | 246 |
235 iter->second.access_token.clear(); | 247 iter->second.access_token.clear(); |
236 iter->second.state = ACCOUNT_REMOVED; | 248 iter->second.state = ACCOUNT_REMOVED; |
237 CompleteCollectingTokens(); | 249 CompleteCollectingTokens(); |
238 } | 250 } |
239 | 251 |
240 OAuth2TokenService* GCMAccountTracker::GetTokenService() { | 252 OAuth2TokenService* GCMAccountTracker::GetTokenService() { |
241 DCHECK(account_tracker_->identity_provider()); | 253 DCHECK(account_tracker_->identity_provider()); |
242 return account_tracker_->identity_provider()->GetTokenService(); | 254 return account_tracker_->identity_provider()->GetTokenService(); |
243 } | 255 } |
244 | 256 |
245 } // namespace gcm | 257 } // namespace gcm |
OLD | NEW |