Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | |
|
Nikita (slow)
2015/03/20 18:06:49
nit: 2015
Denis Kuznetsov (DE-MUC)
2015/03/20 19:50:29
Done.
| |
| 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/chromeos/login/signin/token_handler_util.h" | |
| 6 | |
| 7 #include "base/memory/weak_ptr.h" | |
| 8 #include "base/values.h" | |
| 9 #include "chrome/browser/chromeos/profiles/profile_helper.h" | |
| 10 #include "chrome/browser/profiles/profile.h" | |
| 11 #include "components/user_manager/user_id.h" | |
| 12 #include "components/user_manager/user_manager.h" | |
| 13 #include "google_apis/gaia/gaia_oauth_client.h" | |
| 14 | |
| 15 namespace { | |
| 16 | |
| 17 const char kTokenHandlePref[] = "PasswordTokenHandle"; | |
| 18 static const int kMaxRetries = 3; | |
| 19 } | |
| 20 | |
| 21 TokenHandlerUtil::TokenHandlerUtil(user_manager::UserManager* user_manager) | |
| 22 : user_manager_(user_manager), weak_factory_(this) { | |
| 23 } | |
| 24 | |
| 25 TokenHandlerUtil::~TokenHandlerUtil() { | |
| 26 weak_factory_.InvalidateWeakPtrs(); | |
| 27 gaia_client_.reset(); | |
| 28 } | |
| 29 | |
| 30 bool TokenHandlerUtil::HasToken(const user_manager::UserID& user_id) { | |
| 31 const base::DictionaryValue* dict = nullptr; | |
| 32 std::string token; | |
| 33 if (!user_manager_->FindKnowUserPrefs(user_id, &dict)) | |
| 34 return false; | |
| 35 if (!dict->GetString(kTokenHandlePref, &token)) | |
| 36 return false; | |
| 37 return !token.empty(); | |
| 38 } | |
| 39 | |
| 40 void TokenHandlerUtil::DeleteToken(const user_manager::UserID& user_id) { | |
| 41 const base::DictionaryValue* dict = nullptr; | |
| 42 if (!user_manager_->FindKnowUserPrefs(user_id, &dict)) | |
| 43 return; | |
| 44 scoped_ptr<base::DictionaryValue> dict_copy(dict->DeepCopy()); | |
| 45 if (!dict_copy->Remove(kTokenHandlePref, nullptr)) | |
| 46 return; | |
| 47 user_manager_->UpdateKnowUserPrefs(user_id, *dict_copy.get(), true); | |
|
Nikita (slow)
2015/03/20 18:06:49
nit: comment for true
Denis Kuznetsov (DE-MUC)
2015/03/20 19:50:29
Done.
| |
| 48 } | |
| 49 | |
| 50 void TokenHandlerUtil::CheckToken(const user_manager::UserID& user_id, | |
| 51 const TokenValidationCallback& callback) { | |
| 52 const base::DictionaryValue* dict = nullptr; | |
| 53 std::string token; | |
| 54 if (!user_manager_->FindKnowUserPrefs(user_id, &dict)) { | |
| 55 callback.Run(user_id, UNKNOWN); | |
| 56 return; | |
| 57 } | |
| 58 if (!dict->GetString(kTokenHandlePref, &token)) { | |
| 59 callback.Run(user_id, UNKNOWN); | |
| 60 return; | |
| 61 } | |
| 62 | |
| 63 if (!gaia_client_.get()) { | |
| 64 auto request_context = | |
| 65 chromeos::ProfileHelper::Get()->GetSigninProfile()->GetRequestContext(); | |
| 66 gaia_client_.reset(new gaia::GaiaOAuthClient(request_context)); | |
| 67 } | |
| 68 | |
| 69 validation_delegates_.set( | |
| 70 token, scoped_ptr<TokenValidationDelegate>(new TokenValidationDelegate( | |
| 71 weak_factory_.GetWeakPtr(), user_id, token, callback))); | |
| 72 gaia_client_->GetTokenHandleInfo(token, kMaxRetries, | |
| 73 validation_delegates_.get(token)); | |
| 74 } | |
| 75 | |
| 76 void TokenHandlerUtil::OnValidationComplete(const std::string& token) { | |
| 77 validation_delegates_.erase(token); | |
| 78 } | |
| 79 | |
| 80 TokenHandlerUtil::TokenValidationDelegate::TokenValidationDelegate( | |
| 81 const base::WeakPtr<TokenHandlerUtil>& owner, | |
| 82 const user_manager::UserID& user_id, | |
| 83 const std::string& token, | |
| 84 const TokenValidationCallback& callback) | |
| 85 : owner_(owner), user_id_(user_id), token_(token), callback_(callback) { | |
| 86 } | |
| 87 | |
| 88 TokenHandlerUtil::TokenValidationDelegate::~TokenValidationDelegate() { | |
| 89 } | |
| 90 | |
| 91 void TokenHandlerUtil::TokenValidationDelegate::OnOAuthError() { | |
| 92 callback_.Run(user_id_, INVALID); | |
| 93 if (owner_) | |
| 94 owner_->OnValidationComplete(token_); | |
| 95 } | |
| 96 | |
| 97 void TokenHandlerUtil::TokenValidationDelegate::OnNetworkError( | |
| 98 int response_code) { | |
| 99 callback_.Run(user_id_, UNKNOWN); | |
| 100 if (owner_) | |
| 101 owner_->OnValidationComplete(token_); | |
| 102 } | |
| 103 | |
| 104 void TokenHandlerUtil::TokenValidationDelegate::OnGetTokenInfoResponse( | |
| 105 scoped_ptr<base::DictionaryValue> token_info) { | |
| 106 TokenHandleStatus outcome = UNKNOWN; | |
| 107 if (!token_info->HasKey("error")) { | |
| 108 int expires_in = 0; | |
| 109 if (token_info->GetInteger("expires_in", &expires_in)) | |
| 110 outcome = (expires_in < 0) ? INVALID : VALID; | |
| 111 } | |
| 112 callback_.Run(user_id_, outcome); | |
| 113 if (owner_) | |
| 114 owner_->OnValidationComplete(token_); | |
| 115 } | |
| OLD | NEW |