Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2017 The Chromium Authors. All rights reserved. | |
| 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 "components/payments/content/payment_manifest_section_table.h" | |
| 6 | |
| 7 #include <stdint.h> | |
| 8 | |
| 9 #include <memory> | |
| 10 | |
| 11 #include "base/logging.h" | |
| 12 #include "content/public/browser/browser_thread.h" | |
| 13 #include "sql/statement.h" | |
| 14 | |
| 15 using content::BrowserThread; | |
|
please use gerrit instead
2017/04/06 16:47:19
This pollutes the global namespace of header files
gogerald1
2017/04/07 17:51:07
Acknowledged. Not applicable anymore.
| |
| 16 | |
| 17 namespace payments { | |
| 18 namespace { | |
| 19 | |
|
please use gerrit instead
2017/04/06 16:47:19
using ::content::BrowserThread;
gogerald1
2017/04/07 17:51:07
Acknowledged. Not applicable anymore.
| |
| 20 // Note that the finger print is calculated with SHA-256. | |
| 21 const size_t kFingerPrintLength = 32; | |
| 22 | |
| 23 WebDatabaseTable::TypeKey GetKey() { | |
| 24 // We just need a unique constant. Use the address of a static that | |
| 25 // COMDAT folding won't touch in an optimizing linker. | |
| 26 static int table_key = 0; | |
| 27 return reinterpret_cast<void*>(&table_key); | |
| 28 } | |
| 29 | |
| 30 // Converts 2-dimensional vector |finger_prints| to 1-dimesional vector. | |
| 31 std::unique_ptr<std::vector<uint8_t>> SerializeFingerPrints( | |
| 32 const std::vector<std::vector<uint8_t>>& finger_prints) { | |
| 33 auto serialized_finger_prints = base::MakeUnique<std::vector<uint8_t>>(); | |
| 34 | |
| 35 for (const auto& finger_print : finger_prints) { | |
| 36 DCHECK_EQ(finger_print.size(), kFingerPrintLength); | |
| 37 serialized_finger_prints->insert(serialized_finger_prints->end(), | |
| 38 finger_print.begin(), finger_print.end()); | |
| 39 } | |
| 40 | |
| 41 return serialized_finger_prints; | |
| 42 } | |
| 43 | |
| 44 // Converts 1-dimensional vector created by SerializeFingerPrints back to | |
| 45 // 2-dimensional vector. Each vector of the second dimensional vector has exact | |
| 46 // kFingerPrintLength number of elements. | |
| 47 bool DeserializeFingerPrints( | |
| 48 const std::vector<uint8_t>& finger_prints, | |
| 49 std::vector<std::vector<uint8_t>>& deserialized_finger_prints) { | |
| 50 if (finger_prints.size() % kFingerPrintLength != 0) | |
| 51 return false; | |
| 52 | |
| 53 for (size_t i = 0; i < finger_prints.size(); i += kFingerPrintLength) { | |
| 54 deserialized_finger_prints.emplace_back( | |
| 55 finger_prints.begin() + i, | |
| 56 finger_prints.begin() + i + kFingerPrintLength); | |
| 57 } | |
| 58 return true; | |
| 59 } | |
| 60 | |
| 61 } // namespace | |
| 62 | |
| 63 PaymentManifestSectionTable::PaymentManifestSectionTable() {} | |
| 64 | |
| 65 PaymentManifestSectionTable::~PaymentManifestSectionTable() {} | |
| 66 | |
| 67 PaymentManifestSectionTable* PaymentManifestSectionTable::FromWebDatabase( | |
| 68 WebDatabase* db) { | |
| 69 return static_cast<PaymentManifestSectionTable*>(db->GetTable(GetKey())); | |
| 70 } | |
| 71 | |
| 72 WebDatabaseTable::TypeKey PaymentManifestSectionTable::GetTypeKey() const { | |
| 73 return GetKey(); | |
| 74 } | |
| 75 | |
| 76 bool PaymentManifestSectionTable::CreateTablesIfNecessary() { | |
| 77 if (!db_->DoesTableExist("payment_manifest_section")) { | |
| 78 if (!db_->Execute("CREATE TABLE payment_manifest_section ( " | |
| 79 "method_name VARCHAR, " | |
| 80 "package_name VARCHAR, " | |
| 81 "version INTEGER NOT NULL DEFAULT 0, " | |
| 82 "finger_prints BLOB) ")) { | |
| 83 NOTREACHED(); | |
| 84 return false; | |
| 85 } | |
| 86 } | |
| 87 | |
| 88 return true; | |
| 89 } | |
| 90 | |
| 91 bool PaymentManifestSectionTable::IsSyncable() { | |
| 92 return false; | |
| 93 } | |
| 94 | |
| 95 bool PaymentManifestSectionTable::MigrateToVersion( | |
| 96 int version, | |
| 97 bool* update_compatible_version) { | |
| 98 return true; | |
| 99 } | |
| 100 | |
| 101 bool PaymentManifestSectionTable::AddPaymentManifestSections( | |
| 102 const std::string& method_name, | |
| 103 const std::vector<mojom::PaymentManifestSectionPtr>& manifest) { | |
| 104 if (BrowserThread::IsMessageLoopValid(BrowserThread::DB)) | |
| 105 DCHECK_CURRENTLY_ON(BrowserThread::DB); | |
|
please use gerrit instead
2017/04/06 16:47:19
DCHECK is pre-processed out of the code in release
gogerald1
2017/04/06 18:31:55
Yes, the reason of using IsMessageLoopValid is for
please use gerrit instead
2017/04/06 18:50:52
ok.
gogerald1
2017/04/07 17:51:07
Acknowledged. Not applicable anymore.
| |
| 106 | |
| 107 if (!db_->BeginTransaction()) | |
| 108 return false; | |
| 109 | |
| 110 sql::Statement s1(db_->GetUniqueStatement( | |
| 111 "DELETE FROM payment_manifest_section WHERE method_name=?")); | |
| 112 s1.BindString(0, method_name); | |
| 113 | |
| 114 if (!s1.Run()) { | |
| 115 db_->RollbackTransaction(); | |
| 116 return false; | |
| 117 } | |
| 118 | |
| 119 sql::Statement s2(db_->GetUniqueStatement( | |
| 120 "INSERT INTO payment_manifest_section " | |
| 121 "(method_name, package_name, version, finger_prints) " | |
| 122 "VALUES (?, ?, ?, ?)")); | |
| 123 for (size_t i = 0; i < manifest.size(); i++) { | |
| 124 int index = 0; | |
| 125 s2.BindString(index++, method_name); | |
| 126 s2.BindString(index++, manifest[i]->package_name); | |
| 127 s2.BindInt64(index++, manifest[i]->version); | |
| 128 | |
| 129 std::unique_ptr<std::vector<uint8_t>> serialized_finger_prints = | |
| 130 SerializeFingerPrints(manifest[i]->sha256_cert_fingerprints); | |
| 131 s2.BindBlob(index, serialized_finger_prints->data(), | |
| 132 serialized_finger_prints->size()); | |
| 133 | |
| 134 if (!s2.Run()) { | |
| 135 db_->RollbackTransaction(); | |
| 136 return false; | |
| 137 } | |
| 138 s2.Reset(true); | |
| 139 } | |
| 140 | |
| 141 if (!db_->CommitTransaction()) { | |
| 142 db_->RollbackTransaction(); | |
| 143 return false; | |
| 144 } | |
| 145 | |
| 146 return true; | |
| 147 } | |
| 148 | |
| 149 std::vector<mojom::PaymentManifestSectionPtr> | |
| 150 PaymentManifestSectionTable::GetPaymentManifestSections( | |
| 151 const std::string& method_name) { | |
| 152 if (BrowserThread::IsMessageLoopValid(BrowserThread::DB)) | |
|
please use gerrit instead
2017/04/06 16:47:19
Ditto.
gogerald1
2017/04/07 17:51:07
Acknowledged. Not applicable anymore.
| |
| 153 DCHECK_CURRENTLY_ON(BrowserThread::DB); | |
| 154 | |
| 155 std::vector<mojom::PaymentManifestSectionPtr> manifest; | |
| 156 sql::Statement s( | |
| 157 db_->GetUniqueStatement("SELECT package_name, version, finger_prints " | |
| 158 "FROM payment_manifest_section " | |
| 159 "WHERE method_name=?")); | |
| 160 s.BindString(0, method_name); | |
| 161 | |
| 162 if (!s.is_valid()) | |
| 163 return manifest; | |
| 164 | |
| 165 while (s.Step()) { | |
| 166 mojom::PaymentManifestSectionPtr section = | |
| 167 mojom::PaymentManifestSection::New(); | |
| 168 | |
| 169 int index = 0; | |
| 170 section->package_name = s.ColumnString(index++); | |
| 171 section->version = s.ColumnInt64(index++); | |
| 172 | |
| 173 std::vector<uint8_t> finger_prints; | |
| 174 if (!s.ColumnBlobAsVector(index, &finger_prints)) { | |
| 175 NOTREACHED(); | |
|
please use gerrit instead
2017/04/06 16:47:19
Remove NOTREACHED(). This is easily reachable thro
gogerald1
2017/04/07 17:51:07
Done.
| |
| 176 manifest.clear(); | |
| 177 return manifest; | |
| 178 } | |
| 179 | |
| 180 if (!DeserializeFingerPrints(finger_prints, | |
| 181 section->sha256_cert_fingerprints)) { | |
| 182 NOTREACHED(); | |
|
please use gerrit instead
2017/04/06 16:47:19
Ditto.
gogerald1
2017/04/07 17:51:07
Done.
| |
| 183 manifest.clear(); | |
| 184 return manifest; | |
| 185 } | |
| 186 | |
| 187 manifest.push_back(std::move(section)); | |
| 188 } | |
| 189 | |
| 190 return manifest; | |
| 191 } | |
| 192 | |
| 193 } // payments | |
| OLD | NEW |