Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(208)

Side by Side Diff: chrome/browser/extensions/api/identity/account_tracker.cc

Issue 398573007: Cleanup: remove IdentityAccountTracker (replaced by gaia::AccountTracker) (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: rebase to ToT Created 6 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(Empty)
1 // Copyright 2013 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 #include "chrome/browser/extensions/api/identity/account_tracker.h"
6
7 #include "base/logging.h"
8 #include "base/stl_util.h"
9 #include "chrome/browser/browser_process.h"
10 #include "chrome/browser/chrome_notification_types.h"
11 #include "chrome/browser/profiles/profile.h"
12 #include "chrome/browser/signin/profile_oauth2_token_service_factory.h"
13 #include "chrome/browser/signin/signin_manager_factory.h"
14 #include "components/signin/core/browser/profile_oauth2_token_service.h"
15 #include "components/signin/core/browser/signin_manager.h"
16 #include "content/public/browser/notification_details.h"
17 #include "extensions/browser/extension_system.h"
18
19 namespace extensions {
20
21 AccountTracker::AccountTracker(Profile* profile) : profile_(profile) {
22 ProfileOAuth2TokenService* service =
23 ProfileOAuth2TokenServiceFactory::GetForProfile(profile_);
24 service->AddObserver(this);
25 service->signin_error_controller()->AddProvider(this);
26 SigninManagerFactory::GetForProfile(profile_)->AddObserver(this);
27 }
28
29 AccountTracker::~AccountTracker() {}
30
31 void AccountTracker::ReportAuthError(const std::string& account_id,
32 const GoogleServiceAuthError& error) {
33 account_errors_.insert(make_pair(account_id, error));
34 ProfileOAuth2TokenServiceFactory::GetForProfile(profile_)->
35 signin_error_controller()->AuthStatusChanged();
36 UpdateSignInState(account_id, false);
37 }
38
39 void AccountTracker::Shutdown() {
40 STLDeleteValues(&user_info_requests_);
41 SigninManagerFactory::GetForProfile(profile_)->RemoveObserver(this);
42 ProfileOAuth2TokenService* service =
43 ProfileOAuth2TokenServiceFactory::GetForProfile(profile_);
44 service->signin_error_controller()->RemoveProvider(this);
45 service->RemoveObserver(this);
46 }
47
48 void AccountTracker::AddObserver(Observer* observer) {
49 observer_list_.AddObserver(observer);
50 }
51
52 void AccountTracker::RemoveObserver(Observer* observer) {
53 observer_list_.RemoveObserver(observer);
54 }
55
56 std::vector<AccountIds> AccountTracker::GetAccounts() const {
57 const std::string primary_account_id = signin_manager_account_id();
58 std::vector<AccountIds> accounts;
59
60 for (std::map<std::string, AccountState>::const_iterator it =
61 accounts_.begin();
62 it != accounts_.end();
63 ++it) {
64 const AccountState& state = it->second;
65 bool is_visible = state.is_signed_in && !state.ids.gaia.empty();
66
67 if (it->first == primary_account_id) {
68 if (is_visible)
69 accounts.insert(accounts.begin(), state.ids);
70 else
71 return std::vector<AccountIds>();
72
73 } else if (is_visible) {
74 accounts.push_back(state.ids);
75 }
76 }
77 return accounts;
78 }
79
80 std::string AccountTracker::FindAccountKeyByGaiaId(const std::string& gaia_id) {
81 for (std::map<std::string, AccountState>::const_iterator it =
82 accounts_.begin();
83 it != accounts_.end();
84 ++it) {
85 const AccountState& state = it->second;
86 if (state.ids.gaia == gaia_id) {
87 return state.ids.account_key;
88 }
89 }
90
91 return std::string();
92 }
93
94 void AccountTracker::OnRefreshTokenAvailable(const std::string& account_id) {
95 // Ignore refresh tokens if there is no primary account ID at all.
96 if (signin_manager_account_id().empty())
97 return;
98
99 DVLOG(1) << "AVAILABLE " << account_id;
100 ClearAuthError(account_id);
101 UpdateSignInState(account_id, true);
102 }
103
104 void AccountTracker::OnRefreshTokenRevoked(const std::string& account_id) {
105 DVLOG(1) << "REVOKED " << account_id;
106 UpdateSignInState(account_id, false);
107 }
108
109 void AccountTracker::GoogleSigninSucceeded(const std::string& username,
110 const std::string& password) {
111 std::vector<std::string> accounts =
112 ProfileOAuth2TokenServiceFactory::GetForProfile(profile_)->GetAccounts();
113
114 for (std::vector<std::string>::const_iterator it = accounts.begin();
115 it != accounts.end();
116 ++it) {
117 OnRefreshTokenAvailable(*it);
118 }
119 }
120
121 void AccountTracker::GoogleSignedOut(const std::string& username) {
122 if (username == signin_manager_account_id() ||
123 signin_manager_account_id().empty()) {
124 StopTrackingAllAccounts();
125 } else {
126 StopTrackingAccount(username);
127 }
128 }
129
130 void AccountTracker::SetAccountStateForTest(AccountIds ids, bool is_signed_in) {
131 accounts_[ids.account_key].ids = ids;
132 accounts_[ids.account_key].is_signed_in = is_signed_in;
133
134 DVLOG(1) << "SetAccountStateForTest " << ids.account_key << ":"
135 << is_signed_in;
136
137 if (VLOG_IS_ON(1)) {
138 for (std::map<std::string, AccountState>::const_iterator it =
139 accounts_.begin();
140 it != accounts_.end();
141 ++it) {
142 DVLOG(1) << it->first << ":" << it->second.is_signed_in;
143 }
144 }
145 }
146
147 const std::string AccountTracker::signin_manager_account_id() const {
148 return SigninManagerFactory::GetForProfile(profile_)
149 ->GetAuthenticatedAccountId();
150 }
151
152 void AccountTracker::NotifyAccountAdded(const AccountState& account) {
153 DCHECK(!account.ids.gaia.empty());
154 FOR_EACH_OBSERVER(
155 Observer, observer_list_, OnAccountAdded(account.ids));
156 }
157
158 void AccountTracker::NotifyAccountRemoved(const AccountState& account) {
159 DCHECK(!account.ids.gaia.empty());
160 FOR_EACH_OBSERVER(
161 Observer, observer_list_, OnAccountRemoved(account.ids));
162 }
163
164 void AccountTracker::NotifySignInChanged(const AccountState& account) {
165 DCHECK(!account.ids.gaia.empty());
166 FOR_EACH_OBSERVER(Observer,
167 observer_list_,
168 OnAccountSignInChanged(account.ids, account.is_signed_in));
169 }
170
171 void AccountTracker::ClearAuthError(const std::string& account_key) {
172 account_errors_.erase(account_key);
173 ProfileOAuth2TokenServiceFactory::GetForProfile(profile_)->
174 signin_error_controller()->AuthStatusChanged();
175 }
176
177 void AccountTracker::UpdateSignInState(const std::string& account_key,
178 bool is_signed_in) {
179 StartTrackingAccount(account_key);
180 AccountState& account = accounts_[account_key];
181 bool needs_gaia_id = account.ids.gaia.empty();
182 bool was_signed_in = account.is_signed_in;
183 account.is_signed_in = is_signed_in;
184
185 if (needs_gaia_id && is_signed_in)
186 StartFetchingUserInfo(account_key);
187
188 if (!needs_gaia_id && (was_signed_in != is_signed_in))
189 NotifySignInChanged(account);
190 }
191
192 void AccountTracker::StartTrackingAccount(const std::string& account_key) {
193 if (!ContainsKey(accounts_, account_key)) {
194 DVLOG(1) << "StartTracking " << account_key;
195 AccountState account_state;
196 account_state.ids.account_key = account_key;
197 account_state.ids.email = account_key;
198 account_state.is_signed_in = false;
199 accounts_.insert(make_pair(account_key, account_state));
200 }
201 }
202
203 void AccountTracker::StopTrackingAccount(const std::string& account_key) {
204 if (ContainsKey(accounts_, account_key)) {
205 AccountState& account = accounts_[account_key];
206 if (!account.ids.gaia.empty()) {
207 UpdateSignInState(account_key, false);
208 NotifyAccountRemoved(account);
209 }
210 accounts_.erase(account_key);
211 }
212
213 ClearAuthError(account_key);
214
215 if (ContainsKey(user_info_requests_, account_key))
216 DeleteFetcher(user_info_requests_[account_key]);
217 }
218
219 void AccountTracker::StopTrackingAllAccounts() {
220 while (!accounts_.empty())
221 StopTrackingAccount(accounts_.begin()->first);
222 }
223
224 void AccountTracker::StartFetchingUserInfo(const std::string& account_key) {
225 if (ContainsKey(user_info_requests_, account_key))
226 DeleteFetcher(user_info_requests_[account_key]);
227
228 DVLOG(1) << "StartFetching " << account_key;
229 AccountIdFetcher* fetcher =
230 new AccountIdFetcher(profile_, this, account_key);
231 user_info_requests_[account_key] = fetcher;
232 fetcher->Start();
233 }
234
235 void AccountTracker::OnUserInfoFetchSuccess(AccountIdFetcher* fetcher,
236 const std::string& gaia_id) {
237 const std::string& account_key = fetcher->account_key();
238 DCHECK(ContainsKey(accounts_, account_key));
239 AccountState& account = accounts_[account_key];
240
241 account.ids.gaia = gaia_id;
242 NotifyAccountAdded(account);
243
244 if (account.is_signed_in)
245 NotifySignInChanged(account);
246
247 DeleteFetcher(fetcher);
248 }
249
250 void AccountTracker::OnUserInfoFetchFailure(AccountIdFetcher* fetcher) {
251 LOG(WARNING) << "Failed to get UserInfo for " << fetcher->account_key();
252 std::string key = fetcher->account_key();
253 DeleteFetcher(fetcher);
254 StopTrackingAccount(key);
255 }
256
257 std::string AccountTracker::GetAccountId() const {
258 if (account_errors_.size() == 0)
259 return std::string();
260 else
261 return account_errors_.begin()->first;
262 }
263
264 std::string AccountTracker::GetUsername() const {
265 std::string id = GetAccountId();
266 if (!id.empty()) {
267 std::map<std::string, AccountState>::const_iterator it =
268 accounts_.find(id);
269 if (it != accounts_.end())
270 return it->second.ids.email;
271 }
272 return std::string();
273 }
274
275 GoogleServiceAuthError AccountTracker::GetAuthStatus() const {
276 if (account_errors_.size() == 0)
277 return GoogleServiceAuthError::AuthErrorNone();
278 else
279 return account_errors_.begin()->second;
280 }
281
282 void AccountTracker::DeleteFetcher(AccountIdFetcher* fetcher) {
283 const std::string& account_key = fetcher->account_key();
284 DCHECK(ContainsKey(user_info_requests_, account_key));
285 DCHECK_EQ(fetcher, user_info_requests_[account_key]);
286 user_info_requests_.erase(account_key);
287 delete fetcher;
288 }
289
290 AccountIdFetcher::AccountIdFetcher(Profile* profile,
291 AccountTracker* tracker,
292 const std::string& account_key)
293 : OAuth2TokenService::Consumer("extensions_account_tracker"),
294 profile_(profile),
295 tracker_(tracker),
296 account_key_(account_key) {}
297
298 AccountIdFetcher::~AccountIdFetcher() {}
299
300 void AccountIdFetcher::Start() {
301 ProfileOAuth2TokenService* service =
302 ProfileOAuth2TokenServiceFactory::GetForProfile(profile_);
303 login_token_request_ = service->StartRequest(
304 account_key_, OAuth2TokenService::ScopeSet(), this);
305 }
306
307 void AccountIdFetcher::OnGetTokenSuccess(
308 const OAuth2TokenService::Request* request,
309 const std::string& access_token,
310 const base::Time& expiration_time) {
311 DCHECK_EQ(request, login_token_request_.get());
312
313 gaia_oauth_client_.reset(new gaia::GaiaOAuthClient(
314 g_browser_process->system_request_context()));
315
316 const int kMaxGetUserIdRetries = 3;
317 gaia_oauth_client_->GetUserId(access_token, kMaxGetUserIdRetries, this);
318 }
319
320 void AccountIdFetcher::OnGetTokenFailure(
321 const OAuth2TokenService::Request* request,
322 const GoogleServiceAuthError& error) {
323 LOG(ERROR) << "OnGetTokenFailure: " << error.ToString();
324 DCHECK_EQ(request, login_token_request_.get());
325 tracker_->OnUserInfoFetchFailure(this);
326 }
327
328 void AccountIdFetcher::OnGetUserIdResponse(const std::string& gaia_id) {
329 tracker_->OnUserInfoFetchSuccess(this, gaia_id);
330 }
331
332 void AccountIdFetcher::OnOAuthError() {
333 LOG(ERROR) << "OnOAuthError";
334 tracker_->OnUserInfoFetchFailure(this);
335 }
336
337 void AccountIdFetcher::OnNetworkError(int response_code) {
338 LOG(ERROR) << "OnNetworkError " << response_code;
339 tracker_->OnUserInfoFetchFailure(this);
340 }
341
342 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698