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_id.h" |
12 #include "components/user_manager/user_manager.h" | 13 #include "components/user_manager/user_manager.h" |
13 #include "google_apis/gaia/gaia_oauth_client.h" | 14 #include "google_apis/gaia/gaia_oauth_client.h" |
14 | 15 |
15 namespace { | 16 namespace { |
16 | 17 |
17 const char kTokenHandlePref[] = "PasswordTokenHandle"; | 18 const char kTokenHandlePref[] = "PasswordTokenHandle"; |
18 const char kTokenHandleStatusPref[] = "TokenHandleStatus"; | 19 const char kTokenHandleStatusPref[] = "TokenHandleStatus"; |
19 | 20 |
20 const char kHandleStatusValid[] = "valid"; | 21 const char kHandleStatusValid[] = "valid"; |
21 const char kHandleStatusInvalid[] = "invalid"; | 22 const char kHandleStatusInvalid[] = "invalid"; |
22 const char* kDefaultHandleStatus = kHandleStatusValid; | 23 const char* kDefaultHandleStatus = kHandleStatusValid; |
23 | 24 |
24 static const int kMaxRetries = 3; | 25 static const int kMaxRetries = 3; |
25 | 26 |
26 } // namespace | 27 } // namespace |
27 | 28 |
28 TokenHandleUtil::TokenHandleUtil(user_manager::UserManager* user_manager) | 29 TokenHandleUtil::TokenHandleUtil(user_manager::UserManager* user_manager) |
29 : user_manager_(user_manager), weak_factory_(this) { | 30 : user_manager_(user_manager), weak_factory_(this) { |
30 } | 31 } |
31 | 32 |
32 TokenHandleUtil::~TokenHandleUtil() { | 33 TokenHandleUtil::~TokenHandleUtil() { |
33 weak_factory_.InvalidateWeakPtrs(); | 34 weak_factory_.InvalidateWeakPtrs(); |
34 gaia_client_.reset(); | 35 gaia_client_.reset(); |
35 } | 36 } |
36 | 37 |
37 bool TokenHandleUtil::HasToken(const AccountId& account_id) { | 38 bool TokenHandleUtil::HasToken(const user_manager::UserID& user_id) { |
38 const base::DictionaryValue* dict = nullptr; | 39 const base::DictionaryValue* dict = nullptr; |
39 std::string token; | 40 std::string token; |
40 if (!user_manager_->FindKnownUserPrefs(account_id, &dict)) | 41 if (!user_manager_->FindKnownUserPrefs(user_id, &dict)) |
41 return false; | 42 return false; |
42 if (!dict->GetString(kTokenHandlePref, &token)) | 43 if (!dict->GetString(kTokenHandlePref, &token)) |
43 return false; | 44 return false; |
44 return !token.empty(); | 45 return !token.empty(); |
45 } | 46 } |
46 | 47 |
47 bool TokenHandleUtil::ShouldObtainHandle(const AccountId& account_id) { | 48 bool TokenHandleUtil::ShouldObtainHandle(const user_manager::UserID& user_id) { |
48 const base::DictionaryValue* dict = nullptr; | 49 const base::DictionaryValue* dict = nullptr; |
49 std::string token; | 50 std::string token; |
50 if (!user_manager_->FindKnownUserPrefs(account_id, &dict)) | 51 if (!user_manager_->FindKnownUserPrefs(user_id, &dict)) |
51 return true; | 52 return true; |
52 if (!dict->GetString(kTokenHandlePref, &token)) | 53 if (!dict->GetString(kTokenHandlePref, &token)) |
53 return true; | 54 return true; |
54 if (token.empty()) | 55 if (token.empty()) |
55 return true; | 56 return true; |
56 std::string status(kDefaultHandleStatus); | 57 std::string status(kDefaultHandleStatus); |
57 dict->GetString(kTokenHandleStatusPref, &status); | 58 dict->GetString(kTokenHandleStatusPref, &status); |
58 return kHandleStatusInvalid == status; | 59 return kHandleStatusInvalid == status; |
59 } | 60 } |
60 | 61 |
61 void TokenHandleUtil::DeleteHandle(const AccountId& account_id) { | 62 void TokenHandleUtil::DeleteHandle(const user_manager::UserID& user_id) { |
62 const base::DictionaryValue* dict = nullptr; | 63 const base::DictionaryValue* dict = nullptr; |
63 if (!user_manager_->FindKnownUserPrefs(account_id, &dict)) | 64 if (!user_manager_->FindKnownUserPrefs(user_id, &dict)) |
64 return; | 65 return; |
65 scoped_ptr<base::DictionaryValue> dict_copy(dict->DeepCopy()); | 66 scoped_ptr<base::DictionaryValue> dict_copy(dict->DeepCopy()); |
66 dict_copy->Remove(kTokenHandlePref, nullptr); | 67 dict_copy->Remove(kTokenHandlePref, nullptr); |
67 dict_copy->Remove(kTokenHandleStatusPref, nullptr); | 68 dict_copy->Remove(kTokenHandleStatusPref, nullptr); |
68 user_manager_->UpdateKnownUserPrefs(account_id, *dict_copy.get(), | 69 user_manager_->UpdateKnownUserPrefs(user_id, *dict_copy.get(), |
69 /* replace values */ true); | 70 /* replace values */ true); |
70 } | 71 } |
71 | 72 |
72 void TokenHandleUtil::MarkHandleInvalid(const AccountId& account_id) { | 73 void TokenHandleUtil::MarkHandleInvalid(const user_manager::UserID& user_id) { |
73 user_manager_->SetKnownUserStringPref(account_id, kTokenHandleStatusPref, | 74 user_manager_->SetKnownUserStringPref(user_id, kTokenHandleStatusPref, |
74 kHandleStatusInvalid); | 75 kHandleStatusInvalid); |
75 } | 76 } |
76 | 77 |
77 void TokenHandleUtil::CheckToken(const AccountId& account_id, | 78 void TokenHandleUtil::CheckToken(const user_manager::UserID& user_id, |
78 const TokenValidationCallback& callback) { | 79 const TokenValidationCallback& callback) { |
79 const base::DictionaryValue* dict = nullptr; | 80 const base::DictionaryValue* dict = nullptr; |
80 std::string token; | 81 std::string token; |
81 if (!user_manager_->FindKnownUserPrefs(account_id, &dict)) { | 82 if (!user_manager_->FindKnownUserPrefs(user_id, &dict)) { |
82 callback.Run(account_id, UNKNOWN); | 83 callback.Run(user_id, UNKNOWN); |
83 return; | 84 return; |
84 } | 85 } |
85 if (!dict->GetString(kTokenHandlePref, &token)) { | 86 if (!dict->GetString(kTokenHandlePref, &token)) { |
86 callback.Run(account_id, UNKNOWN); | 87 callback.Run(user_id, UNKNOWN); |
87 return; | 88 return; |
88 } | 89 } |
89 | 90 |
90 if (!gaia_client_.get()) { | 91 if (!gaia_client_.get()) { |
91 auto request_context = | 92 auto request_context = |
92 chromeos::ProfileHelper::Get()->GetSigninProfile()->GetRequestContext(); | 93 chromeos::ProfileHelper::Get()->GetSigninProfile()->GetRequestContext(); |
93 gaia_client_.reset(new gaia::GaiaOAuthClient(request_context)); | 94 gaia_client_.reset(new gaia::GaiaOAuthClient(request_context)); |
94 } | 95 } |
95 | 96 |
96 validation_delegates_.set( | 97 validation_delegates_.set( |
97 token, scoped_ptr<TokenDelegate>(new TokenDelegate( | 98 token, scoped_ptr<TokenDelegate>(new TokenDelegate( |
98 weak_factory_.GetWeakPtr(), account_id, token, callback))); | 99 weak_factory_.GetWeakPtr(), user_id, token, callback))); |
99 gaia_client_->GetTokenHandleInfo(token, kMaxRetries, | 100 gaia_client_->GetTokenHandleInfo(token, kMaxRetries, |
100 validation_delegates_.get(token)); | 101 validation_delegates_.get(token)); |
101 } | 102 } |
102 | 103 |
103 void TokenHandleUtil::StoreTokenHandle(const AccountId& account_id, | 104 void TokenHandleUtil::StoreTokenHandle(const user_manager::UserID& user_id, |
104 const std::string& handle) { | 105 const std::string& handle) { |
105 user_manager_->SetKnownUserStringPref(account_id, kTokenHandlePref, handle); | 106 user_manager_->SetKnownUserStringPref(user_id, kTokenHandlePref, handle); |
106 user_manager_->SetKnownUserStringPref(account_id, kTokenHandleStatusPref, | 107 user_manager_->SetKnownUserStringPref(user_id, kTokenHandleStatusPref, |
107 kHandleStatusValid); | 108 kHandleStatusValid); |
108 } | 109 } |
109 | 110 |
110 void TokenHandleUtil::OnValidationComplete(const std::string& token) { | 111 void TokenHandleUtil::OnValidationComplete(const std::string& token) { |
111 validation_delegates_.erase(token); | 112 validation_delegates_.erase(token); |
112 } | 113 } |
113 | 114 |
114 void TokenHandleUtil::OnObtainTokenComplete(const AccountId& account_id) { | 115 void TokenHandleUtil::OnObtainTokenComplete( |
115 obtain_delegates_.erase(account_id); | 116 const user_manager::UserID& user_id) { |
| 117 obtain_delegates_.erase(user_id); |
116 } | 118 } |
117 | 119 |
118 TokenHandleUtil::TokenDelegate::TokenDelegate( | 120 TokenHandleUtil::TokenDelegate::TokenDelegate( |
119 const base::WeakPtr<TokenHandleUtil>& owner, | 121 const base::WeakPtr<TokenHandleUtil>& owner, |
120 const AccountId& account_id, | 122 const user_manager::UserID& user_id, |
121 const std::string& token, | 123 const std::string& token, |
122 const TokenValidationCallback& callback) | 124 const TokenValidationCallback& callback) |
123 : owner_(owner), | 125 : owner_(owner), |
124 account_id_(account_id), | 126 user_id_(user_id), |
125 token_(token), | 127 token_(token), |
126 tokeninfo_response_start_time_(base::TimeTicks::Now()), | 128 tokeninfo_response_start_time_(base::TimeTicks::Now()), |
127 callback_(callback) {} | 129 callback_(callback) { |
| 130 } |
128 | 131 |
129 TokenHandleUtil::TokenDelegate::~TokenDelegate() { | 132 TokenHandleUtil::TokenDelegate::~TokenDelegate() { |
130 } | 133 } |
131 | 134 |
132 void TokenHandleUtil::TokenDelegate::OnOAuthError() { | 135 void TokenHandleUtil::TokenDelegate::OnOAuthError() { |
133 callback_.Run(account_id_, INVALID); | 136 callback_.Run(user_id_, INVALID); |
134 NotifyDone(); | 137 NotifyDone(); |
135 } | 138 } |
136 | 139 |
137 // Warning: NotifyDone() deletes |this| | 140 // Warning: NotifyDone() deletes |this| |
138 void TokenHandleUtil::TokenDelegate::NotifyDone() { | 141 void TokenHandleUtil::TokenDelegate::NotifyDone() { |
139 if (owner_) | 142 if (owner_) |
140 owner_->OnValidationComplete(token_); | 143 owner_->OnValidationComplete(token_); |
141 } | 144 } |
142 | 145 |
143 void TokenHandleUtil::TokenDelegate::OnNetworkError(int response_code) { | 146 void TokenHandleUtil::TokenDelegate::OnNetworkError(int response_code) { |
144 callback_.Run(account_id_, UNKNOWN); | 147 callback_.Run(user_id_, UNKNOWN); |
145 NotifyDone(); | 148 NotifyDone(); |
146 } | 149 } |
147 | 150 |
148 void TokenHandleUtil::TokenDelegate::OnGetTokenInfoResponse( | 151 void TokenHandleUtil::TokenDelegate::OnGetTokenInfoResponse( |
149 scoped_ptr<base::DictionaryValue> token_info) { | 152 scoped_ptr<base::DictionaryValue> token_info) { |
150 TokenHandleStatus outcome = UNKNOWN; | 153 TokenHandleStatus outcome = UNKNOWN; |
151 if (!token_info->HasKey("error")) { | 154 if (!token_info->HasKey("error")) { |
152 int expires_in = 0; | 155 int expires_in = 0; |
153 if (token_info->GetInteger("expires_in", &expires_in)) | 156 if (token_info->GetInteger("expires_in", &expires_in)) |
154 outcome = (expires_in < 0) ? INVALID : VALID; | 157 outcome = (expires_in < 0) ? INVALID : VALID; |
155 } | 158 } |
156 | 159 |
157 const base::TimeDelta duration = | 160 const base::TimeDelta duration = |
158 base::TimeTicks::Now() - tokeninfo_response_start_time_; | 161 base::TimeTicks::Now() - tokeninfo_response_start_time_; |
159 UMA_HISTOGRAM_TIMES("Login.TokenCheckResponseTime", duration); | 162 UMA_HISTOGRAM_TIMES("Login.TokenCheckResponseTime", duration); |
160 callback_.Run(account_id_, outcome); | 163 callback_.Run(user_id_, outcome); |
161 NotifyDone(); | 164 NotifyDone(); |
162 } | 165 } |
OLD | NEW |