OLD | NEW |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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/policy/dm_token_storage.h" | 5 #include "chrome/browser/chromeos/policy/dm_token_storage.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/task_scheduler/post_task.h" | 8 #include "base/task_scheduler/post_task.h" |
9 #include "chrome/browser/chromeos/settings/token_encryptor.h" | 9 #include "chrome/browser/chromeos/settings/token_encryptor.h" |
10 #include "chrome/common/pref_names.h" | 10 #include "chrome/common/pref_names.h" |
(...skipping 110 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
121 EncryptAndStoreToken(); | 121 EncryptAndStoreToken(); |
122 else if (!retrieve_callbacks_.empty()) | 122 else if (!retrieve_callbacks_.empty()) |
123 LoadAndDecryptToken(); | 123 LoadAndDecryptToken(); |
124 } | 124 } |
125 | 125 |
126 void DMTokenStorage::EncryptAndStoreToken() { | 126 void DMTokenStorage::EncryptAndStoreToken() { |
127 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 127 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
128 DCHECK(!system_salt_.empty()); | 128 DCHECK(!system_salt_.empty()); |
129 DCHECK(!dm_token_.empty()); | 129 DCHECK(!dm_token_.empty()); |
130 base::PostTaskWithTraitsAndReplyWithResult( | 130 base::PostTaskWithTraitsAndReplyWithResult( |
131 FROM_HERE, | 131 FROM_HERE, {base::MayBlock(), base::TaskPriority::BACKGROUND}, |
132 base::TaskTraits().MayBlock().WithPriority( | |
133 base::TaskPriority::BACKGROUND), | |
134 base::Bind(&EncryptToken, system_salt_, dm_token_), | 132 base::Bind(&EncryptToken, system_salt_, dm_token_), |
135 base::Bind(&DMTokenStorage::OnTokenEncrypted, | 133 base::Bind(&DMTokenStorage::OnTokenEncrypted, |
136 weak_ptr_factory_.GetWeakPtr())); | 134 weak_ptr_factory_.GetWeakPtr())); |
137 } | 135 } |
138 | 136 |
139 void DMTokenStorage::OnTokenEncrypted(const std::string& encrypted_dm_token) { | 137 void DMTokenStorage::OnTokenEncrypted(const std::string& encrypted_dm_token) { |
140 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 138 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
141 if (encrypted_dm_token.empty()) { | 139 if (encrypted_dm_token.empty()) { |
142 DLOG(ERROR) << "Failed to encrypt DM token."; | 140 DLOG(ERROR) << "Failed to encrypt DM token."; |
143 } else { | 141 } else { |
144 local_state_->SetString(prefs::kDeviceDMToken, encrypted_dm_token); | 142 local_state_->SetString(prefs::kDeviceDMToken, encrypted_dm_token); |
145 } | 143 } |
146 FlushStoreTokenCallback(!encrypted_dm_token.empty()); | 144 FlushStoreTokenCallback(!encrypted_dm_token.empty()); |
147 } | 145 } |
148 | 146 |
149 void DMTokenStorage::LoadAndDecryptToken() { | 147 void DMTokenStorage::LoadAndDecryptToken() { |
150 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 148 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
151 DCHECK_EQ(SaltState::LOADED, state_); | 149 DCHECK_EQ(SaltState::LOADED, state_); |
152 std::string encrypted_dm_token = | 150 std::string encrypted_dm_token = |
153 local_state_->GetString(prefs::kDeviceDMToken); | 151 local_state_->GetString(prefs::kDeviceDMToken); |
154 if (!encrypted_dm_token.empty()) { | 152 if (!encrypted_dm_token.empty()) { |
155 base::PostTaskWithTraitsAndReplyWithResult( | 153 base::PostTaskWithTraitsAndReplyWithResult( |
156 FROM_HERE, | 154 FROM_HERE, {base::MayBlock(), base::TaskPriority::BACKGROUND}, |
157 base::TaskTraits().MayBlock().WithPriority( | |
158 base::TaskPriority::BACKGROUND), | |
159 base::Bind(&DecryptToken, system_salt_, encrypted_dm_token), | 155 base::Bind(&DecryptToken, system_salt_, encrypted_dm_token), |
160 base::Bind(&DMTokenStorage::FlushRetrieveTokenCallback, | 156 base::Bind(&DMTokenStorage::FlushRetrieveTokenCallback, |
161 weak_ptr_factory_.GetWeakPtr())); | 157 weak_ptr_factory_.GetWeakPtr())); |
162 } else { | 158 } else { |
163 DLOG(ERROR) << "No DM token in the local state."; | 159 DLOG(ERROR) << "No DM token in the local state."; |
164 FlushRetrieveTokenCallback(std::string()); | 160 FlushRetrieveTokenCallback(std::string()); |
165 } | 161 } |
166 } | 162 } |
167 | 163 |
168 void DMTokenStorage::FlushStoreTokenCallback(bool status) { | 164 void DMTokenStorage::FlushStoreTokenCallback(bool status) { |
169 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 165 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
170 if (!store_callback_.is_null()) { | 166 if (!store_callback_.is_null()) { |
171 std::move(store_callback_).Run(status); | 167 std::move(store_callback_).Run(status); |
172 } | 168 } |
173 } | 169 } |
174 | 170 |
175 void DMTokenStorage::FlushRetrieveTokenCallback(const std::string& dm_token) { | 171 void DMTokenStorage::FlushRetrieveTokenCallback(const std::string& dm_token) { |
176 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 172 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
177 if (retrieve_callbacks_.empty()) | 173 if (retrieve_callbacks_.empty()) |
178 return; | 174 return; |
179 if (dm_token.empty()) | 175 if (dm_token.empty()) |
180 DLOG(ERROR) << "Failed to retrieve DM token."; | 176 DLOG(ERROR) << "Failed to retrieve DM token."; |
181 std::vector<RetrieveCallback> callbacks; | 177 std::vector<RetrieveCallback> callbacks; |
182 callbacks.swap(retrieve_callbacks_); | 178 callbacks.swap(retrieve_callbacks_); |
183 for (RetrieveCallback& callback : callbacks) | 179 for (RetrieveCallback& callback : callbacks) |
184 std::move(callback).Run(dm_token); | 180 std::move(callback).Run(dm_token); |
185 } | 181 } |
186 | 182 |
187 } // namespace policy | 183 } // namespace policy |
OLD | NEW |