Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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/signin/core/browser/webdata/token_service_table.h" | 5 #include "components/signin/core/browser/webdata/token_service_table.h" |
| 6 | 6 |
| 7 #include <map> | 7 #include <map> |
| 8 #include <string> | 8 #include <string> |
| 9 | 9 |
| 10 #include "base/logging.h" | 10 #include "base/logging.h" |
| 11 #include "base/metrics/histogram_macros.h" | |
| 11 #include "components/os_crypt/os_crypt.h" | 12 #include "components/os_crypt/os_crypt.h" |
| 12 #include "components/webdata/common/web_database.h" | 13 #include "components/webdata/common/web_database.h" |
| 13 #include "sql/statement.h" | 14 #include "sql/statement.h" |
| 14 | 15 |
| 15 namespace { | 16 namespace { |
| 16 | 17 |
| 17 WebDatabaseTable::TypeKey GetKey() { | 18 WebDatabaseTable::TypeKey GetKey() { |
| 18 // We just need a unique constant. Use the address of a static that | 19 // We just need a unique constant. Use the address of a static that |
| 19 // COMDAT folding won't touch in an optimizing linker. | 20 // COMDAT folding won't touch in an optimizing linker. |
| 20 static int table_key = 0; | 21 static int table_key = 0; |
| 21 return reinterpret_cast<void*>(&table_key); | 22 return reinterpret_cast<void*>(&table_key); |
| 22 } | 23 } |
| 23 | 24 |
| 25 // Entries in the |Signin.TokenTable.ReadTokenFromDB| histogram. | |
| 26 enum ReadOneTokenEnum { | |
| 27 READ_ONE_TOKEN_SUCCESS, | |
| 28 READ_ONE_TOKEN_DB_SUCCESS_DECRYPT_FAILED, | |
| 29 READ_ONE_TOKEN_DB_FAILED_BAD_ENTRY, | |
| 30 READ_ONE_TOKEN_MAX_VALUE | |
| 31 }; | |
| 32 | |
| 24 } // namespace | 33 } // namespace |
| 25 | 34 |
| 26 TokenServiceTable* TokenServiceTable::FromWebDatabase(WebDatabase* db) { | 35 TokenServiceTable* TokenServiceTable::FromWebDatabase(WebDatabase* db) { |
| 27 return static_cast<TokenServiceTable*>(db->GetTable(GetKey())); | 36 return static_cast<TokenServiceTable*>(db->GetTable(GetKey())); |
| 28 | 37 |
| 29 } | 38 } |
| 30 | 39 |
| 31 WebDatabaseTable::TypeKey TokenServiceTable::GetTypeKey() const { | 40 WebDatabaseTable::TypeKey TokenServiceTable::GetTypeKey() const { |
| 32 return GetKey(); | 41 return GetKey(); |
| 33 } | 42 } |
| (...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 86 static_cast<int>(encrypted_token.length())); | 95 static_cast<int>(encrypted_token.length())); |
| 87 | 96 |
| 88 return s.Run(); | 97 return s.Run(); |
| 89 } | 98 } |
| 90 | 99 |
| 91 bool TokenServiceTable::GetAllTokens( | 100 bool TokenServiceTable::GetAllTokens( |
| 92 std::map<std::string, std::string>* tokens) { | 101 std::map<std::string, std::string>* tokens) { |
| 93 sql::Statement s(db_->GetUniqueStatement( | 102 sql::Statement s(db_->GetUniqueStatement( |
| 94 "SELECT service, encrypted_token FROM token_service")); | 103 "SELECT service, encrypted_token FROM token_service")); |
| 95 | 104 |
| 96 if (!s.is_valid()) | 105 UMA_HISTOGRAM_BOOLEAN("Signin.TokenTable.GetAllTokensSqlStatement", |
| 106 s.is_valid()); | |
| 107 | |
| 108 if (!s.is_valid()) { | |
| 109 LOG(ERROR) << "Failed to load tokens (invalid SQL statement)."; | |
| 97 return false; | 110 return false; |
| 111 } | |
| 98 | 112 |
| 99 while (s.Step()) { | 113 while (s.Step()) { |
| 100 std::string encrypted_token; | 114 std::string encrypted_token; |
| 101 std::string decrypted_token; | 115 std::string decrypted_token; |
| 102 std::string service; | 116 std::string service; |
| 103 service = s.ColumnString(0); | 117 service = s.ColumnString(0); |
| 104 bool entry_ok = !service.empty() && | 118 bool entry_ok = !service.empty() && |
| 105 s.ColumnBlobAsString(1, &encrypted_token); | 119 s.ColumnBlobAsString(1, &encrypted_token); |
| 106 if (entry_ok) { | 120 if (entry_ok) { |
| 107 OSCrypt::DecryptString(encrypted_token, &decrypted_token); | 121 if (OSCrypt::DecryptString(encrypted_token, &decrypted_token)) { |
| 108 (*tokens)[service] = decrypted_token; | 122 (*tokens)[service] = decrypted_token; |
| 123 UMA_HISTOGRAM_ENUMERATION("Signin.TokenTable.ReadTokenFromDB", | |
| 124 READ_ONE_TOKEN_SUCCESS, | |
| 125 READ_ONE_TOKEN_MAX_VALUE); | |
| 126 } else { | |
| 127 // Chrome relies on native APIs to encrypt and decrypt the tokens which | |
| 128 // may fail (see http://crbug.com/686485). | |
| 129 LOG(ERROR) << "Failed to decrypt token for service " << service; | |
| 130 UMA_HISTOGRAM_ENUMERATION("Signin.TokenTable.ReadTokenFromDB", | |
| 131 READ_ONE_TOKEN_DB_SUCCESS_DECRYPT_FAILED, | |
| 132 READ_ONE_TOKEN_MAX_VALUE); | |
| 133 } | |
| 109 } else { | 134 } else { |
| 110 NOTREACHED(); | 135 LOG(ERROR) << "Bad token entry for service " << service; |
| 136 UMA_HISTOGRAM_ENUMERATION("Signin.TokenTable.ReadTokenFromDB", | |
| 137 READ_ONE_TOKEN_DB_FAILED_BAD_ENTRY, | |
| 138 READ_ONE_TOKEN_MAX_VALUE); | |
|
Alexei Svitkine (slow)
2017/02/06 15:35:51
Please refactor the code so that this macro only a
msarda
2017/02/06 16:59:27
Done.
| |
| 111 return false; | 139 return false; |
| 112 } | 140 } |
| 113 } | 141 } |
| 114 return true; | 142 return true; |
| 115 } | 143 } |
| OLD | NEW |