OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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/chromeos/login/signin/token_handle_fetcher.h" | 5 #include "chrome/browser/chromeos/login/signin/token_handle_fetcher.h" |
6 | 6 |
7 #include "base/metrics/histogram_macros.h" | 7 #include "base/metrics/histogram_macros.h" |
8 #include "chrome/browser/chromeos/login/signin/token_handle_util.h" | 8 #include "chrome/browser/chromeos/login/signin/token_handle_util.h" |
9 #include "chrome/browser/chromeos/profiles/profile_helper.h" | 9 #include "chrome/browser/chromeos/profiles/profile_helper.h" |
10 #include "chrome/browser/profiles/profile.h" | 10 #include "chrome/browser/profiles/profile.h" |
11 #include "chrome/browser/signin/profile_oauth2_token_service_factory.h" | 11 #include "chrome/browser/signin/profile_oauth2_token_service_factory.h" |
12 #include "chrome/browser/signin/signin_manager_factory.h" | 12 #include "chrome/browser/signin/signin_manager_factory.h" |
13 #include "chrome/browser/signin/signin_manager_factory.h" | 13 #include "chrome/browser/signin/signin_manager_factory.h" |
| 14 #include "components/keyed_service/content/browser_context_keyed_service_shutdow
n_notifier_factory.h" |
14 #include "components/signin/core/browser/profile_oauth2_token_service.h" | 15 #include "components/signin/core/browser/profile_oauth2_token_service.h" |
15 #include "components/signin/core/browser/signin_manager.h" | 16 #include "components/signin/core/browser/signin_manager.h" |
16 #include "google_apis/gaia/gaia_constants.h" | 17 #include "google_apis/gaia/gaia_constants.h" |
17 | 18 |
18 namespace { | 19 namespace { |
19 const int kMaxRetries = 3; | 20 const int kMaxRetries = 3; |
| 21 |
| 22 class ShutdownNotifierFactory |
| 23 : public BrowserContextKeyedServiceShutdownNotifierFactory { |
| 24 public: |
| 25 static ShutdownNotifierFactory* GetInstance() { |
| 26 return Singleton<ShutdownNotifierFactory>::get(); |
| 27 } |
| 28 |
| 29 private: |
| 30 friend struct DefaultSingletonTraits<ShutdownNotifierFactory>; |
| 31 |
| 32 ShutdownNotifierFactory() |
| 33 : BrowserContextKeyedServiceShutdownNotifierFactory( |
| 34 "TokenHandleFetcher") { |
| 35 DependsOn(ProfileOAuth2TokenServiceFactory::GetInstance()); |
| 36 } |
| 37 ~ShutdownNotifierFactory() override {} |
| 38 |
| 39 DISALLOW_COPY_AND_ASSIGN(ShutdownNotifierFactory); |
| 40 }; |
| 41 |
20 } // namespace | 42 } // namespace |
21 | 43 |
22 TokenHandleFetcher::TokenHandleFetcher(TokenHandleUtil* util, | 44 TokenHandleFetcher::TokenHandleFetcher(TokenHandleUtil* util, |
23 const user_manager::UserID& user_id) | 45 const user_manager::UserID& user_id) |
24 : OAuth2TokenService::Consumer("user_session_manager"), | 46 : OAuth2TokenService::Consumer("user_session_manager"), |
25 token_handle_util_(util), | 47 token_handle_util_(util), |
26 user_id_(user_id), | 48 user_id_(user_id), |
27 token_service_(nullptr), | 49 token_service_(nullptr), |
28 waiting_for_refresh_token_(false), | 50 waiting_for_refresh_token_(false), |
29 profile_(nullptr), | 51 profile_(nullptr), |
30 tokeninfo_response_start_time_(base::TimeTicks()) { | 52 tokeninfo_response_start_time_(base::TimeTicks()) { |
31 } | 53 } |
32 | 54 |
33 TokenHandleFetcher::~TokenHandleFetcher() { | 55 TokenHandleFetcher::~TokenHandleFetcher() { |
34 if (waiting_for_refresh_token_) | 56 if (waiting_for_refresh_token_) |
35 token_service_->RemoveObserver(this); | 57 token_service_->RemoveObserver(this); |
36 } | 58 } |
37 | 59 |
38 void TokenHandleFetcher::BackfillToken(Profile* profile, | 60 void TokenHandleFetcher::BackfillToken(Profile* profile, |
39 const TokenFetchingCallback& callback) { | 61 const TokenFetchingCallback& callback) { |
40 profile_ = profile; | 62 profile_ = profile; |
41 callback_ = callback; | 63 callback_ = callback; |
42 | 64 |
43 token_service_ = ProfileOAuth2TokenServiceFactory::GetForProfile(profile); | 65 token_service_ = ProfileOAuth2TokenServiceFactory::GetForProfile(profile); |
44 SigninManagerBase* signin_manager = | 66 SigninManagerBase* signin_manager = |
45 SigninManagerFactory::GetForProfile(profile); | 67 SigninManagerFactory::GetForProfile(profile); |
46 std::string account_id = signin_manager->GetAuthenticatedAccountId(); | 68 std::string account_id = signin_manager->GetAuthenticatedAccountId(); |
47 if (!token_service_->RefreshTokenIsAvailable(account_id)) { | 69 if (!token_service_->RefreshTokenIsAvailable(account_id)) { |
48 account_without_token_ = account_id; | 70 account_without_token_ = account_id; |
| 71 profile_shutdown_notification_ = |
| 72 ShutdownNotifierFactory::GetInstance()->Get(profile)->Subscribe( |
| 73 base::Bind(&TokenHandleFetcher::OnProfileDestroyed, |
| 74 base::Unretained(this))); |
| 75 |
49 token_service_->AddObserver(this); | 76 token_service_->AddObserver(this); |
50 waiting_for_refresh_token_ = true; | 77 waiting_for_refresh_token_ = true; |
51 return; | 78 return; |
52 } | 79 } |
53 RequestAccessToken(account_id); | 80 RequestAccessToken(account_id); |
54 } | 81 } |
55 | 82 |
56 void TokenHandleFetcher::OnRefreshTokenAvailable( | 83 void TokenHandleFetcher::OnRefreshTokenAvailable( |
57 const std::string& account_id) { | 84 const std::string& account_id) { |
58 if (account_without_token_ != account_id) | 85 if (account_without_token_ != account_id) |
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
117 if (token_info->GetString("token_handle", &handle)) { | 144 if (token_info->GetString("token_handle", &handle)) { |
118 success = true; | 145 success = true; |
119 token_handle_util_->StoreTokenHandle(user_id_, handle); | 146 token_handle_util_->StoreTokenHandle(user_id_, handle); |
120 } | 147 } |
121 } | 148 } |
122 const base::TimeDelta duration = | 149 const base::TimeDelta duration = |
123 base::TimeTicks::Now() - tokeninfo_response_start_time_; | 150 base::TimeTicks::Now() - tokeninfo_response_start_time_; |
124 UMA_HISTOGRAM_TIMES("Login.TokenObtainResponseTime", duration); | 151 UMA_HISTOGRAM_TIMES("Login.TokenObtainResponseTime", duration); |
125 callback_.Run(user_id_, success); | 152 callback_.Run(user_id_, success); |
126 } | 153 } |
| 154 |
| 155 void TokenHandleFetcher::OnProfileDestroyed() { |
| 156 callback_.Run(user_id_, false); |
| 157 } |
OLD | NEW |