Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / google_apis / gaia / account_tracker.cc
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 #include "google_apis/gaia/account_tracker.h"
6
7 #include "base/debug/trace_event.h"
8 #include "base/logging.h"
9 #include "base/profiler/scoped_tracker.h"
10 #include "base/stl_util.h"
11 #include "net/url_request/url_request_context_getter.h"
12
13 namespace gaia {
14
15 AccountTracker::AccountTracker(
16     IdentityProvider* identity_provider,
17     net::URLRequestContextGetter* request_context_getter)
18     : identity_provider_(identity_provider),
19       request_context_getter_(request_context_getter),
20       shutdown_called_(false) {
21   identity_provider_->AddObserver(this);
22   identity_provider_->GetTokenService()->AddObserver(this);
23 }
24
25 AccountTracker::~AccountTracker() {
26   DCHECK(shutdown_called_);
27 }
28
29 void AccountTracker::Shutdown() {
30   shutdown_called_ = true;
31   STLDeleteValues(&user_info_requests_);
32   identity_provider_->GetTokenService()->RemoveObserver(this);
33   identity_provider_->RemoveObserver(this);
34 }
35
36 bool AccountTracker::IsAllUserInfoFetched() const {
37   return user_info_requests_.empty();
38 }
39
40 void AccountTracker::AddObserver(Observer* observer) {
41   observer_list_.AddObserver(observer);
42 }
43
44 void AccountTracker::RemoveObserver(Observer* observer) {
45   observer_list_.RemoveObserver(observer);
46 }
47
48 std::vector<AccountIds> AccountTracker::GetAccounts() const {
49   const std::string active_account_id =
50       identity_provider_->GetActiveAccountId();
51   std::vector<AccountIds> accounts;
52
53   for (std::map<std::string, AccountState>::const_iterator it =
54            accounts_.begin();
55        it != accounts_.end();
56        ++it) {
57     const AccountState& state = it->second;
58     bool is_visible = state.is_signed_in && !state.ids.gaia.empty();
59
60     if (it->first == active_account_id) {
61       if (is_visible)
62         accounts.insert(accounts.begin(), state.ids);
63       else
64         return std::vector<AccountIds>();
65
66     } else if (is_visible) {
67       accounts.push_back(state.ids);
68     }
69   }
70   return accounts;
71 }
72
73 AccountIds AccountTracker::FindAccountIdsByGaiaId(const std::string& gaia_id) {
74   for (std::map<std::string, AccountState>::const_iterator it =
75            accounts_.begin();
76        it != accounts_.end();
77        ++it) {
78     const AccountState& state = it->second;
79     if (state.ids.gaia == gaia_id) {
80       return state.ids;
81     }
82   }
83
84   return AccountIds();
85 }
86
87 void AccountTracker::OnRefreshTokenAvailable(const std::string& account_id) {
88   // TODO(vadimt): Remove ScopedTracker below once crbug.com/422460 is fixed.
89   tracked_objects::ScopedTracker tracking_profile(
90       FROM_HERE_WITH_EXPLICIT_FUNCTION(
91           "422460 AccountTracker::OnRefreshTokenAvailable"));
92
93   TRACE_EVENT1("identity",
94                "AccountTracker::OnRefreshTokenAvailable",
95                "account_key",
96                account_id);
97
98   // Ignore refresh tokens if there is no active account ID at all.
99   if (identity_provider_->GetActiveAccountId().empty())
100     return;
101
102   DVLOG(1) << "AVAILABLE " << account_id;
103   UpdateSignInState(account_id, true);
104 }
105
106 void AccountTracker::OnRefreshTokenRevoked(const std::string& account_id) {
107   TRACE_EVENT1("identity",
108                "AccountTracker::OnRefreshTokenRevoked",
109                "account_key",
110                account_id);
111
112   DVLOG(1) << "REVOKED " << account_id;
113   UpdateSignInState(account_id, false);
114 }
115
116 void AccountTracker::OnActiveAccountLogin() {
117   TRACE_EVENT0("identity", "AccountTracker::OnActiveAccountLogin");
118
119   std::vector<std::string> accounts =
120       identity_provider_->GetTokenService()->GetAccounts();
121
122   DVLOG(1) << "LOGIN " << accounts.size() << " accounts available.";
123
124   for (std::vector<std::string>::const_iterator it = accounts.begin();
125        it != accounts.end();
126        ++it) {
127     OnRefreshTokenAvailable(*it);
128   }
129 }
130
131 void AccountTracker::OnActiveAccountLogout() {
132   TRACE_EVENT0("identity", "AccountTracker::OnActiveAccountLogout");
133   DVLOG(1) << "LOGOUT";
134   StopTrackingAllAccounts();
135 }
136
137 void AccountTracker::SetAccountStateForTest(AccountIds ids, bool is_signed_in) {
138   accounts_[ids.account_key].ids = ids;
139   accounts_[ids.account_key].is_signed_in = is_signed_in;
140
141   DVLOG(1) << "SetAccountStateForTest " << ids.account_key << ":"
142            << is_signed_in;
143
144   if (VLOG_IS_ON(1)) {
145     for (std::map<std::string, AccountState>::const_iterator it =
146              accounts_.begin();
147          it != accounts_.end();
148          ++it) {
149       DVLOG(1) << it->first << ":" << it->second.is_signed_in;
150     }
151   }
152 }
153
154 void AccountTracker::NotifyAccountAdded(const AccountState& account) {
155   DCHECK(!account.ids.gaia.empty());
156   FOR_EACH_OBSERVER(
157       Observer, observer_list_, OnAccountAdded(account.ids));
158 }
159
160 void AccountTracker::NotifyAccountRemoved(const AccountState& account) {
161   DCHECK(!account.ids.gaia.empty());
162   FOR_EACH_OBSERVER(
163       Observer, observer_list_, OnAccountRemoved(account.ids));
164 }
165
166 void AccountTracker::NotifySignInChanged(const AccountState& account) {
167   // TODO(vadimt): Remove ScopedTracker below once crbug.com/422460 is fixed.
168   tracked_objects::ScopedTracker tracking_profile(
169       FROM_HERE_WITH_EXPLICIT_FUNCTION(
170           "422460 AccountTracker::NotifySignInChanged"));
171
172   DCHECK(!account.ids.gaia.empty());
173   FOR_EACH_OBSERVER(Observer,
174                     observer_list_,
175                     OnAccountSignInChanged(account.ids, account.is_signed_in));
176 }
177
178 void AccountTracker::UpdateSignInState(const std::string account_key,
179                                        bool is_signed_in) {
180   tracked_objects::ScopedTracker tracking_profile(
181       FROM_HERE_WITH_EXPLICIT_FUNCTION(
182           "422460 AccountTracker::UpdateSignInState"));
183
184   StartTrackingAccount(account_key);
185   AccountState& account = accounts_[account_key];
186   bool needs_gaia_id = account.ids.gaia.empty();
187   bool was_signed_in = account.is_signed_in;
188   account.is_signed_in = is_signed_in;
189
190   if (needs_gaia_id && is_signed_in)
191     StartFetchingUserInfo(account_key);
192
193   if (!needs_gaia_id && (was_signed_in != is_signed_in))
194     NotifySignInChanged(account);
195 }
196
197 void AccountTracker::StartTrackingAccount(const std::string account_key) {
198   // TODO(vadimt): Remove ScopedTracker below once crbug.com/422460 is fixed.
199   tracked_objects::ScopedTracker tracking_profile(
200       FROM_HERE_WITH_EXPLICIT_FUNCTION(
201           "422460 AccountTracker::StartTrackingAccount"));
202
203   if (!ContainsKey(accounts_, account_key)) {
204     DVLOG(1) << "StartTracking " << account_key;
205     AccountState account_state;
206     account_state.ids.account_key = account_key;
207     account_state.ids.email = account_key;
208     account_state.is_signed_in = false;
209     accounts_.insert(make_pair(account_key, account_state));
210   }
211 }
212
213 void AccountTracker::StopTrackingAccount(const std::string account_key) {
214   DVLOG(1) << "StopTracking " << account_key;
215   if (ContainsKey(accounts_, account_key)) {
216     AccountState& account = accounts_[account_key];
217     if (!account.ids.gaia.empty()) {
218       UpdateSignInState(account_key, false);
219       NotifyAccountRemoved(account);
220     }
221     accounts_.erase(account_key);
222   }
223
224   if (ContainsKey(user_info_requests_, account_key))
225     DeleteFetcher(user_info_requests_[account_key]);
226 }
227
228 void AccountTracker::StopTrackingAllAccounts() {
229   while (!accounts_.empty())
230     StopTrackingAccount(accounts_.begin()->first);
231 }
232
233 void AccountTracker::StartFetchingUserInfo(const std::string account_key) {
234   // TODO(vadimt): Remove ScopedTracker below once crbug.com/422460 is fixed.
235   tracked_objects::ScopedTracker tracking_profile(
236       FROM_HERE_WITH_EXPLICIT_FUNCTION(
237           "422460 AccountTracker::StartFetchingUserInfo"));
238
239   if (ContainsKey(user_info_requests_, account_key)) {
240     // TODO(vadimt): Remove ScopedTracker below once crbug.com/422460 is fixed.
241     tracked_objects::ScopedTracker tracking_profile1(
242         FROM_HERE_WITH_EXPLICIT_FUNCTION(
243             "422460 AccountTracker::StartFetchingUserInfo 1"));
244
245     DeleteFetcher(user_info_requests_[account_key]);
246   }
247
248   DVLOG(1) << "StartFetching " << account_key;
249   // TODO(vadimt): Remove ScopedTracker below once crbug.com/422460 is fixed.
250   tracked_objects::ScopedTracker tracking_profile2(
251       FROM_HERE_WITH_EXPLICIT_FUNCTION(
252           "422460 AccountTracker::StartFetchingUserInfo 2"));
253
254   AccountIdFetcher* fetcher =
255       new AccountIdFetcher(identity_provider_->GetTokenService(),
256                            request_context_getter_.get(),
257                            this,
258                            account_key);
259   user_info_requests_[account_key] = fetcher;
260
261   // TODO(vadimt): Remove ScopedTracker below once crbug.com/422460 is fixed.
262   tracked_objects::ScopedTracker tracking_profile3(
263       FROM_HERE_WITH_EXPLICIT_FUNCTION(
264           "422460 AccountTracker::StartFetchingUserInfo 3"));
265
266   fetcher->Start();
267 }
268
269 void AccountTracker::OnUserInfoFetchSuccess(AccountIdFetcher* fetcher,
270                                             const std::string& gaia_id) {
271   const std::string& account_key = fetcher->account_key();
272   DCHECK(ContainsKey(accounts_, account_key));
273   AccountState& account = accounts_[account_key];
274
275   account.ids.gaia = gaia_id;
276   NotifyAccountAdded(account);
277
278   if (account.is_signed_in)
279     NotifySignInChanged(account);
280
281   DeleteFetcher(fetcher);
282 }
283
284 void AccountTracker::OnUserInfoFetchFailure(AccountIdFetcher* fetcher) {
285   LOG(WARNING) << "Failed to get UserInfo for " << fetcher->account_key();
286   std::string key = fetcher->account_key();
287   DeleteFetcher(fetcher);
288   StopTrackingAccount(key);
289 }
290
291 void AccountTracker::DeleteFetcher(AccountIdFetcher* fetcher) {
292   DVLOG(1) << "DeleteFetcher " << fetcher->account_key();
293   const std::string& account_key = fetcher->account_key();
294   DCHECK(ContainsKey(user_info_requests_, account_key));
295   DCHECK_EQ(fetcher, user_info_requests_[account_key]);
296   user_info_requests_.erase(account_key);
297   delete fetcher;
298 }
299
300 AccountIdFetcher::AccountIdFetcher(
301     OAuth2TokenService* token_service,
302     net::URLRequestContextGetter* request_context_getter,
303     AccountTracker* tracker,
304     const std::string& account_key)
305     : OAuth2TokenService::Consumer("gaia_account_tracker"),
306       token_service_(token_service),
307       request_context_getter_(request_context_getter),
308       tracker_(tracker),
309       account_key_(account_key) {
310   TRACE_EVENT_ASYNC_BEGIN1(
311       "identity", "AccountIdFetcher", this, "account_key", account_key);
312 }
313
314 AccountIdFetcher::~AccountIdFetcher() {
315   TRACE_EVENT_ASYNC_END0("identity", "AccountIdFetcher", this);
316 }
317
318 void AccountIdFetcher::Start() {
319   OAuth2TokenService::ScopeSet scopes;
320   scopes.insert("https://www.googleapis.com/auth/userinfo.profile");
321   login_token_request_ = token_service_->StartRequest(
322       account_key_, scopes, this);
323 }
324
325 void AccountIdFetcher::OnGetTokenSuccess(
326     const OAuth2TokenService::Request* request,
327     const std::string& access_token,
328     const base::Time& expiration_time) {
329   TRACE_EVENT_ASYNC_STEP_PAST0(
330       "identity", "AccountIdFetcher", this, "OnGetTokenSuccess");
331   DCHECK_EQ(request, login_token_request_.get());
332
333   gaia_oauth_client_.reset(new gaia::GaiaOAuthClient(request_context_getter_));
334
335   const int kMaxGetUserIdRetries = 3;
336   gaia_oauth_client_->GetUserId(access_token, kMaxGetUserIdRetries, this);
337 }
338
339 void AccountIdFetcher::OnGetTokenFailure(
340     const OAuth2TokenService::Request* request,
341     const GoogleServiceAuthError& error) {
342   TRACE_EVENT_ASYNC_STEP_PAST1("identity",
343                                "AccountIdFetcher",
344                                this,
345                                "OnGetTokenFailure",
346                                "google_service_auth_error",
347                                error.ToString());
348   LOG(ERROR) << "OnGetTokenFailure: " << error.ToString();
349   DCHECK_EQ(request, login_token_request_.get());
350   tracker_->OnUserInfoFetchFailure(this);
351 }
352
353 void AccountIdFetcher::OnGetUserIdResponse(const std::string& gaia_id) {
354   TRACE_EVENT_ASYNC_STEP_PAST1("identity",
355                                "AccountIdFetcher",
356                                this,
357                                "OnGetUserIdResponse",
358                                "gaia_id",
359                                gaia_id);
360   tracker_->OnUserInfoFetchSuccess(this, gaia_id);
361 }
362
363 void AccountIdFetcher::OnOAuthError() {
364   TRACE_EVENT_ASYNC_STEP_PAST0(
365       "identity", "AccountIdFetcher", this, "OnOAuthError");
366   LOG(ERROR) << "OnOAuthError";
367   tracker_->OnUserInfoFetchFailure(this);
368 }
369
370 void AccountIdFetcher::OnNetworkError(int response_code) {
371   TRACE_EVENT_ASYNC_STEP_PAST1("identity",
372                                "AccountIdFetcher",
373                                this,
374                                "OnNetworkError",
375                                "response_code",
376                                response_code);
377   LOG(ERROR) << "OnNetworkError " << response_code;
378   tracker_->OnUserInfoFetchFailure(this);
379 }
380
381 }  // namespace gaia