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 "components/gcm_driver/crypto/gcm_key_store.h" | 5 #include "components/gcm_driver/crypto/gcm_key_store.h" |
6 | 6 |
7 #include <stddef.h> | 7 #include <stddef.h> |
8 | 8 |
9 #include <utility> | 9 #include <utility> |
10 | 10 |
11 #include "base/logging.h" | 11 #include "base/logging.h" |
| 12 #include "base/metrics/histogram_macros.h" |
12 #include "components/gcm_driver/crypto/p256_key_util.h" | 13 #include "components/gcm_driver/crypto/p256_key_util.h" |
13 #include "components/leveldb_proto/proto_database_impl.h" | 14 #include "components/leveldb_proto/proto_database_impl.h" |
14 #include "crypto/random.h" | 15 #include "crypto/random.h" |
15 | 16 |
16 namespace gcm { | 17 namespace gcm { |
17 | 18 |
18 // Statistics are logged to UMA with this string as part of histogram name. They | 19 // Statistics are logged to UMA with this string as part of histogram name. They |
19 // can all be found under LevelDB.*.GCMKeyStore. Changing this needs to | 20 // can all be found under LevelDB.*.GCMKeyStore. Changing this needs to |
20 // synchronize with histograms.xml, AND will also become incompatible with older | 21 // synchronize with histograms.xml, AND will also become incompatible with older |
21 // browsers still reporting the previous values. | 22 // browsers still reporting the previous values. |
(...skipping 25 matching lines...) Expand all Loading... |
47 void GCMKeyStore::GetKeys(const std::string& app_id, | 48 void GCMKeyStore::GetKeys(const std::string& app_id, |
48 const KeysCallback& callback) { | 49 const KeysCallback& callback) { |
49 LazyInitialize(base::Bind(&GCMKeyStore::GetKeysAfterInitialize, | 50 LazyInitialize(base::Bind(&GCMKeyStore::GetKeysAfterInitialize, |
50 weak_factory_.GetWeakPtr(), app_id, callback)); | 51 weak_factory_.GetWeakPtr(), app_id, callback)); |
51 } | 52 } |
52 | 53 |
53 void GCMKeyStore::GetKeysAfterInitialize(const std::string& app_id, | 54 void GCMKeyStore::GetKeysAfterInitialize(const std::string& app_id, |
54 const KeysCallback& callback) { | 55 const KeysCallback& callback) { |
55 DCHECK(state_ == State::INITIALIZED || state_ == State::FAILED); | 56 DCHECK(state_ == State::INITIALIZED || state_ == State::FAILED); |
56 const auto& iter = key_pairs_.find(app_id); | 57 const auto& iter = key_pairs_.find(app_id); |
57 if (iter == key_pairs_.end() || state_ != State::INITIALIZED) { | 58 |
| 59 const bool success = state_ == State::INITIALIZED && iter != key_pairs_.end(); |
| 60 UMA_HISTOGRAM_BOOLEAN("GCM.Crypto.GetKeySuccessRate", success); |
| 61 |
| 62 if (!success) { |
58 callback.Run(KeyPair(), std::string() /* auth_secret */); | 63 callback.Run(KeyPair(), std::string() /* auth_secret */); |
59 return; | 64 return; |
60 } | 65 } |
61 | 66 |
62 const auto& auth_secret_iter = auth_secrets_.find(app_id); | 67 const auto& auth_secret_iter = auth_secrets_.find(app_id); |
63 DCHECK(auth_secret_iter != auth_secrets_.end()); | 68 DCHECK(auth_secret_iter != auth_secrets_.end()); |
64 | 69 |
65 callback.Run(iter->second, auth_secret_iter->second); | 70 callback.Run(iter->second, auth_secret_iter->second); |
66 } | 71 } |
67 | 72 |
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
121 std::move(entries_to_save), std::move(keys_to_remove), | 126 std::move(entries_to_save), std::move(keys_to_remove), |
122 base::Bind(&GCMKeyStore::DidStoreKeys, weak_factory_.GetWeakPtr(), app_id, | 127 base::Bind(&GCMKeyStore::DidStoreKeys, weak_factory_.GetWeakPtr(), app_id, |
123 *pair, auth_secret, callback)); | 128 *pair, auth_secret, callback)); |
124 } | 129 } |
125 | 130 |
126 void GCMKeyStore::DidStoreKeys(const std::string& app_id, | 131 void GCMKeyStore::DidStoreKeys(const std::string& app_id, |
127 const KeyPair& pair, | 132 const KeyPair& pair, |
128 const std::string& auth_secret, | 133 const std::string& auth_secret, |
129 const KeysCallback& callback, | 134 const KeysCallback& callback, |
130 bool success) { | 135 bool success) { |
| 136 UMA_HISTOGRAM_BOOLEAN("GCM.Crypto.CreateKeySuccessRate", success); |
131 DCHECK_EQ(0u, key_pairs_.count(app_id)); | 137 DCHECK_EQ(0u, key_pairs_.count(app_id)); |
132 | 138 |
133 if (!success) { | 139 if (!success) { |
134 DVLOG(1) << "Unable to store the created key in the GCM Key Store."; | 140 DVLOG(1) << "Unable to store the created key in the GCM Key Store."; |
135 callback.Run(KeyPair(), std::string() /* auth_secret */); | 141 callback.Run(KeyPair(), std::string() /* auth_secret */); |
136 return; | 142 return; |
137 } | 143 } |
138 | 144 |
139 key_pairs_[app_id] = pair; | 145 key_pairs_[app_id] = pair; |
140 auth_secrets_[app_id] = auth_secret; | 146 auth_secrets_[app_id] = auth_secret; |
(...skipping 25 matching lines...) Expand all Loading... |
166 | 172 |
167 database_->UpdateEntries( | 173 database_->UpdateEntries( |
168 std::move(entries_to_save), std::move(keys_to_remove), | 174 std::move(entries_to_save), std::move(keys_to_remove), |
169 base::Bind(&GCMKeyStore::DidRemoveKeys, weak_factory_.GetWeakPtr(), | 175 base::Bind(&GCMKeyStore::DidRemoveKeys, weak_factory_.GetWeakPtr(), |
170 app_id, callback)); | 176 app_id, callback)); |
171 } | 177 } |
172 | 178 |
173 void GCMKeyStore::DidRemoveKeys(const std::string& app_id, | 179 void GCMKeyStore::DidRemoveKeys(const std::string& app_id, |
174 const base::Closure& callback, | 180 const base::Closure& callback, |
175 bool success) { | 181 bool success) { |
176 // TODO(peter): Add a histogram for tracking |success|. | 182 UMA_HISTOGRAM_BOOLEAN("GCM.Crypto.RemoveKeySuccessRate", success); |
177 | 183 |
178 if (success) { | 184 if (success) { |
179 key_pairs_.erase(app_id); | 185 key_pairs_.erase(app_id); |
180 auth_secrets_.erase(app_id); | 186 auth_secrets_.erase(app_id); |
181 } else { | 187 } else { |
182 DVLOG(1) << "Unable to delete a key from the GCM Key Store."; | 188 DVLOG(1) << "Unable to delete a key from the GCM Key Store."; |
183 } | 189 } |
184 | 190 |
185 callback.Run(); | 191 callback.Run(); |
186 } | 192 } |
(...skipping 12 matching lines...) Expand all Loading... |
199 | 205 |
200 database_.reset(new leveldb_proto::ProtoDatabaseImpl<EncryptionData>( | 206 database_.reset(new leveldb_proto::ProtoDatabaseImpl<EncryptionData>( |
201 blocking_task_runner_)); | 207 blocking_task_runner_)); |
202 | 208 |
203 database_->Init( | 209 database_->Init( |
204 kDatabaseUMAClientName, key_store_path_, | 210 kDatabaseUMAClientName, key_store_path_, |
205 base::Bind(&GCMKeyStore::DidInitialize, weak_factory_.GetWeakPtr())); | 211 base::Bind(&GCMKeyStore::DidInitialize, weak_factory_.GetWeakPtr())); |
206 } | 212 } |
207 | 213 |
208 void GCMKeyStore::DidInitialize(bool success) { | 214 void GCMKeyStore::DidInitialize(bool success) { |
| 215 UMA_HISTOGRAM_BOOLEAN("GCM.Crypto.InitKeyStoreSuccessRate", success); |
209 if (!success) { | 216 if (!success) { |
210 DVLOG(1) << "Unable to initialize the GCM Key Store."; | 217 DVLOG(1) << "Unable to initialize the GCM Key Store."; |
211 state_ = State::FAILED; | 218 state_ = State::FAILED; |
212 | 219 |
213 delayed_task_controller_.SetReady(); | 220 delayed_task_controller_.SetReady(); |
214 return; | 221 return; |
215 } | 222 } |
216 | 223 |
217 database_->LoadEntries( | 224 database_->LoadEntries( |
218 base::Bind(&GCMKeyStore::DidLoadKeys, weak_factory_.GetWeakPtr())); | 225 base::Bind(&GCMKeyStore::DidLoadKeys, weak_factory_.GetWeakPtr())); |
219 } | 226 } |
220 | 227 |
221 void GCMKeyStore::DidLoadKeys(bool success, | 228 void GCMKeyStore::DidLoadKeys(bool success, |
222 scoped_ptr<std::vector<EncryptionData>> entries) { | 229 scoped_ptr<std::vector<EncryptionData>> entries) { |
| 230 UMA_HISTOGRAM_BOOLEAN("GCM.Crypto.LoadKeyStoreSuccessRate", success); |
223 if (!success) { | 231 if (!success) { |
224 DVLOG(1) << "Unable to load entries into the GCM Key Store."; | 232 DVLOG(1) << "Unable to load entries into the GCM Key Store."; |
225 state_ = State::FAILED; | 233 state_ = State::FAILED; |
226 | 234 |
227 delayed_task_controller_.SetReady(); | 235 delayed_task_controller_.SetReady(); |
228 return; | 236 return; |
229 } | 237 } |
230 | 238 |
231 for (const EncryptionData& entry : *entries) { | 239 for (const EncryptionData& entry : *entries) { |
232 DCHECK_EQ(1, entry.keys_size()); | 240 DCHECK_EQ(1, entry.keys_size()); |
233 | 241 |
234 key_pairs_[entry.app_id()] = entry.keys(0); | 242 key_pairs_[entry.app_id()] = entry.keys(0); |
235 auth_secrets_[entry.app_id()] = entry.auth_secret(); | 243 auth_secrets_[entry.app_id()] = entry.auth_secret(); |
236 } | 244 } |
237 | 245 |
238 state_ = State::INITIALIZED; | 246 state_ = State::INITIALIZED; |
239 | 247 |
240 delayed_task_controller_.SetReady(); | 248 delayed_task_controller_.SetReady(); |
241 } | 249 } |
242 | 250 |
243 } // namespace gcm | 251 } // namespace gcm |
OLD | NEW |