| 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_util.h" | 5 #include "chrome/browser/chromeos/login/signin/token_handle_util.h" |
| 6 | 6 |
| 7 #include "base/memory/weak_ptr.h" | 7 #include "base/memory/weak_ptr.h" |
| 8 #include "base/metrics/histogram_macros.h" | 8 #include "base/metrics/histogram_macros.h" |
| 9 #include "base/values.h" | 9 #include "base/values.h" |
| 10 #include "chrome/browser/chromeos/profiles/profile_helper.h" | 10 #include "chrome/browser/chromeos/profiles/profile_helper.h" |
| 11 #include "chrome/browser/profiles/profile.h" | 11 #include "chrome/browser/profiles/profile.h" |
| 12 #include "components/user_manager/user_manager.h" | 12 #include "components/user_manager/known_user.h" |
| 13 #include "google_apis/gaia/gaia_oauth_client.h" | 13 #include "google_apis/gaia/gaia_oauth_client.h" |
| 14 | 14 |
| 15 namespace { | 15 namespace { |
| 16 | 16 |
| 17 const char kTokenHandlePref[] = "PasswordTokenHandle"; | 17 const char kTokenHandlePref[] = "PasswordTokenHandle"; |
| 18 const char kTokenHandleStatusPref[] = "TokenHandleStatus"; | 18 const char kTokenHandleStatusPref[] = "TokenHandleStatus"; |
| 19 | 19 |
| 20 const char kHandleStatusValid[] = "valid"; | 20 const char kHandleStatusValid[] = "valid"; |
| 21 const char kHandleStatusInvalid[] = "invalid"; | 21 const char kHandleStatusInvalid[] = "invalid"; |
| 22 const char* kDefaultHandleStatus = kHandleStatusValid; | 22 const char* kDefaultHandleStatus = kHandleStatusValid; |
| 23 | 23 |
| 24 static const int kMaxRetries = 3; | 24 static const int kMaxRetries = 3; |
| 25 | 25 |
| 26 } // namespace | 26 } // namespace |
| 27 | 27 |
| 28 TokenHandleUtil::TokenHandleUtil(user_manager::UserManager* user_manager) | 28 TokenHandleUtil::TokenHandleUtil() : weak_factory_(this) {} |
| 29 : user_manager_(user_manager), weak_factory_(this) { | |
| 30 } | |
| 31 | 29 |
| 32 TokenHandleUtil::~TokenHandleUtil() { | 30 TokenHandleUtil::~TokenHandleUtil() { |
| 33 weak_factory_.InvalidateWeakPtrs(); | 31 weak_factory_.InvalidateWeakPtrs(); |
| 34 gaia_client_.reset(); | 32 gaia_client_.reset(); |
| 35 } | 33 } |
| 36 | 34 |
| 37 bool TokenHandleUtil::HasToken(const AccountId& account_id) { | 35 bool TokenHandleUtil::HasToken(const AccountId& account_id) { |
| 38 const base::DictionaryValue* dict = nullptr; | 36 const base::DictionaryValue* dict = nullptr; |
| 39 std::string token; | 37 std::string token; |
| 40 if (!user_manager_->FindKnownUserPrefs(account_id, &dict)) | 38 if (!user_manager::known_user::FindKnownUserPrefs(account_id, &dict)) |
| 41 return false; | 39 return false; |
| 42 if (!dict->GetString(kTokenHandlePref, &token)) | 40 if (!dict->GetString(kTokenHandlePref, &token)) |
| 43 return false; | 41 return false; |
| 44 return !token.empty(); | 42 return !token.empty(); |
| 45 } | 43 } |
| 46 | 44 |
| 47 bool TokenHandleUtil::ShouldObtainHandle(const AccountId& account_id) { | 45 bool TokenHandleUtil::ShouldObtainHandle(const AccountId& account_id) { |
| 48 const base::DictionaryValue* dict = nullptr; | 46 const base::DictionaryValue* dict = nullptr; |
| 49 std::string token; | 47 std::string token; |
| 50 if (!user_manager_->FindKnownUserPrefs(account_id, &dict)) | 48 if (!user_manager::known_user::FindKnownUserPrefs(account_id, &dict)) |
| 51 return true; | 49 return true; |
| 52 if (!dict->GetString(kTokenHandlePref, &token)) | 50 if (!dict->GetString(kTokenHandlePref, &token)) |
| 53 return true; | 51 return true; |
| 54 if (token.empty()) | 52 if (token.empty()) |
| 55 return true; | 53 return true; |
| 56 std::string status(kDefaultHandleStatus); | 54 std::string status(kDefaultHandleStatus); |
| 57 dict->GetString(kTokenHandleStatusPref, &status); | 55 dict->GetString(kTokenHandleStatusPref, &status); |
| 58 return kHandleStatusInvalid == status; | 56 return kHandleStatusInvalid == status; |
| 59 } | 57 } |
| 60 | 58 |
| 61 void TokenHandleUtil::DeleteHandle(const AccountId& account_id) { | 59 void TokenHandleUtil::DeleteHandle(const AccountId& account_id) { |
| 62 const base::DictionaryValue* dict = nullptr; | 60 const base::DictionaryValue* dict = nullptr; |
| 63 if (!user_manager_->FindKnownUserPrefs(account_id, &dict)) | 61 if (!user_manager::known_user::FindKnownUserPrefs(account_id, &dict)) |
| 64 return; | 62 return; |
| 65 scoped_ptr<base::DictionaryValue> dict_copy(dict->DeepCopy()); | 63 scoped_ptr<base::DictionaryValue> dict_copy(dict->DeepCopy()); |
| 66 dict_copy->Remove(kTokenHandlePref, nullptr); | 64 dict_copy->Remove(kTokenHandlePref, nullptr); |
| 67 dict_copy->Remove(kTokenHandleStatusPref, nullptr); | 65 dict_copy->Remove(kTokenHandleStatusPref, nullptr); |
| 68 user_manager_->UpdateKnownUserPrefs(account_id, *dict_copy.get(), | 66 user_manager::known_user::UpdateKnownUserPrefs(account_id, *dict_copy.get(), |
| 69 /* replace values */ true); | 67 /* replace values */ true); |
| 70 } | 68 } |
| 71 | 69 |
| 72 void TokenHandleUtil::MarkHandleInvalid(const AccountId& account_id) { | 70 void TokenHandleUtil::MarkHandleInvalid(const AccountId& account_id) { |
| 73 user_manager_->SetKnownUserStringPref(account_id, kTokenHandleStatusPref, | 71 user_manager::known_user::SetKnownUserStringPref( |
| 74 kHandleStatusInvalid); | 72 account_id, kTokenHandleStatusPref, kHandleStatusInvalid); |
| 75 } | 73 } |
| 76 | 74 |
| 77 void TokenHandleUtil::CheckToken(const AccountId& account_id, | 75 void TokenHandleUtil::CheckToken(const AccountId& account_id, |
| 78 const TokenValidationCallback& callback) { | 76 const TokenValidationCallback& callback) { |
| 79 const base::DictionaryValue* dict = nullptr; | 77 const base::DictionaryValue* dict = nullptr; |
| 80 std::string token; | 78 std::string token; |
| 81 if (!user_manager_->FindKnownUserPrefs(account_id, &dict)) { | 79 if (!user_manager::known_user::FindKnownUserPrefs(account_id, &dict)) { |
| 82 callback.Run(account_id, UNKNOWN); | 80 callback.Run(account_id, UNKNOWN); |
| 83 return; | 81 return; |
| 84 } | 82 } |
| 85 if (!dict->GetString(kTokenHandlePref, &token)) { | 83 if (!dict->GetString(kTokenHandlePref, &token)) { |
| 86 callback.Run(account_id, UNKNOWN); | 84 callback.Run(account_id, UNKNOWN); |
| 87 return; | 85 return; |
| 88 } | 86 } |
| 89 | 87 |
| 90 if (!gaia_client_.get()) { | 88 if (!gaia_client_.get()) { |
| 91 auto request_context = | 89 auto request_context = |
| 92 chromeos::ProfileHelper::Get()->GetSigninProfile()->GetRequestContext(); | 90 chromeos::ProfileHelper::Get()->GetSigninProfile()->GetRequestContext(); |
| 93 gaia_client_.reset(new gaia::GaiaOAuthClient(request_context)); | 91 gaia_client_.reset(new gaia::GaiaOAuthClient(request_context)); |
| 94 } | 92 } |
| 95 | 93 |
| 96 validation_delegates_.set( | 94 validation_delegates_.set( |
| 97 token, scoped_ptr<TokenDelegate>(new TokenDelegate( | 95 token, scoped_ptr<TokenDelegate>(new TokenDelegate( |
| 98 weak_factory_.GetWeakPtr(), account_id, token, callback))); | 96 weak_factory_.GetWeakPtr(), account_id, token, callback))); |
| 99 gaia_client_->GetTokenHandleInfo(token, kMaxRetries, | 97 gaia_client_->GetTokenHandleInfo(token, kMaxRetries, |
| 100 validation_delegates_.get(token)); | 98 validation_delegates_.get(token)); |
| 101 } | 99 } |
| 102 | 100 |
| 103 void TokenHandleUtil::StoreTokenHandle(const AccountId& account_id, | 101 void TokenHandleUtil::StoreTokenHandle(const AccountId& account_id, |
| 104 const std::string& handle) { | 102 const std::string& handle) { |
| 105 user_manager_->SetKnownUserStringPref(account_id, kTokenHandlePref, handle); | 103 user_manager::known_user::SetKnownUserStringPref(account_id, kTokenHandlePref, |
| 106 user_manager_->SetKnownUserStringPref(account_id, kTokenHandleStatusPref, | 104 handle); |
| 107 kHandleStatusValid); | 105 user_manager::known_user::SetKnownUserStringPref( |
| 106 account_id, kTokenHandleStatusPref, kHandleStatusValid); |
| 108 } | 107 } |
| 109 | 108 |
| 110 void TokenHandleUtil::OnValidationComplete(const std::string& token) { | 109 void TokenHandleUtil::OnValidationComplete(const std::string& token) { |
| 111 validation_delegates_.erase(token); | 110 validation_delegates_.erase(token); |
| 112 } | 111 } |
| 113 | 112 |
| 114 void TokenHandleUtil::OnObtainTokenComplete(const AccountId& account_id) { | 113 void TokenHandleUtil::OnObtainTokenComplete(const AccountId& account_id) { |
| 115 obtain_delegates_.erase(account_id); | 114 obtain_delegates_.erase(account_id); |
| 116 } | 115 } |
| 117 | 116 |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 153 if (token_info->GetInteger("expires_in", &expires_in)) | 152 if (token_info->GetInteger("expires_in", &expires_in)) |
| 154 outcome = (expires_in < 0) ? INVALID : VALID; | 153 outcome = (expires_in < 0) ? INVALID : VALID; |
| 155 } | 154 } |
| 156 | 155 |
| 157 const base::TimeDelta duration = | 156 const base::TimeDelta duration = |
| 158 base::TimeTicks::Now() - tokeninfo_response_start_time_; | 157 base::TimeTicks::Now() - tokeninfo_response_start_time_; |
| 159 UMA_HISTOGRAM_TIMES("Login.TokenCheckResponseTime", duration); | 158 UMA_HISTOGRAM_TIMES("Login.TokenCheckResponseTime", duration); |
| 160 callback_.Run(account_id_, outcome); | 159 callback_.Run(account_id_, outcome); |
| 161 NotifyDone(); | 160 NotifyDone(); |
| 162 } | 161 } |
| OLD | NEW |