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 "base/logging.h" | |
| 8 #include "sql/statement.h" | |
| 9 | |
| 10 namespace payments { | |
| 11 namespace { | |
|
please use gerrit instead
2017/04/05 20:33:30
Newline after "namespace {".
gogerald1
2017/04/05 23:38:48
Done.
| |
| 12 // Note that the finger print is calculated with SHA-256. | |
| 13 const uint32_t kFingerPrintLength = 32; | |
|
please use gerrit instead
2017/04/05 20:33:30
size_t
gogerald1
2017/04/05 23:38:48
Done.
| |
| 14 | |
| 15 WebDatabaseTable::TypeKey GetKey() { | |
| 16 // We just need a unique constant. Use the address of a static that | |
| 17 // COMDAT folding won't touch in an optimizing linker. | |
| 18 static int table_key = 0; | |
| 19 return reinterpret_cast<void*>(&table_key); | |
|
please use gerrit instead
2017/04/05 20:33:31
Shouldn't you cast to WebDatabaseTable::TypeKey?
gogerald1
2017/04/05 23:38:48
WebDatabaseTable::TypeKey is a void pointer in ess
| |
| 20 } | |
| 21 } | |
|
please use gerrit instead
2017/04/05 20:33:31
Newline before "}" and add a comment " // namespa
gogerald1
2017/04/05 23:38:48
Done.
| |
| 22 | |
| 23 PaymentManifestSectionTable::PaymentManifestSectionTable() {} | |
| 24 | |
| 25 PaymentManifestSectionTable::~PaymentManifestSectionTable() {} | |
| 26 | |
| 27 PaymentManifestSectionTable* PaymentManifestSectionTable::FromWebDatabase( | |
| 28 WebDatabase* db) { | |
| 29 return static_cast<PaymentManifestSectionTable*>(db->GetTable(GetKey())); | |
| 30 } | |
| 31 | |
| 32 WebDatabaseTable::TypeKey PaymentManifestSectionTable::GetTypeKey() const { | |
| 33 return GetKey(); | |
| 34 } | |
| 35 | |
| 36 bool PaymentManifestSectionTable::CreateTablesIfNecessary() { | |
| 37 if (!db_->DoesTableExist("payment_manifest_section")) { | |
| 38 if (!db_->Execute("CREATE TABLE payment_manifest_section ( " | |
| 39 "method_name VARCHAR, " | |
| 40 "package_name VARCHAR, " | |
| 41 "version INTEGER NOT NULL DEFAULT 0, " | |
| 42 "finger_prints BLOB) ")) { | |
| 43 NOTREACHED(); | |
| 44 return false; | |
| 45 } | |
| 46 } | |
| 47 | |
| 48 return true; | |
| 49 } | |
| 50 | |
| 51 bool PaymentManifestSectionTable::IsSyncable() { | |
| 52 return false; | |
| 53 } | |
| 54 | |
| 55 bool PaymentManifestSectionTable::MigrateToVersion( | |
| 56 int version, | |
| 57 bool* update_compatible_version) { | |
| 58 return true; | |
| 59 } | |
| 60 | |
| 61 bool PaymentManifestSectionTable::AddPaymentManifestSections( | |
| 62 std::string& method_name, | |
| 63 std::vector<mojom::PaymentManifestSectionPtr>& manifest) { | |
| 64 sql::Statement s1(db_->GetUniqueStatement( | |
|
please use gerrit instead
2017/04/05 20:33:30
Use a transaction to group these statements togeth
gogerald1
2017/04/05 23:38:48
Done.
| |
| 65 "DELETE FROM payment_manifest_section WHERE method_name=?")); | |
| 66 s1.BindString(0, method_name); | |
| 67 | |
| 68 if (!s1.Run()) { | |
|
please use gerrit instead
2017/04/05 20:33:31
No need for {} on a single-line body of an if-stat
gogerald1
2017/04/05 23:38:48
It's this true? long time ago (> 1 year) when I am
| |
| 69 return false; | |
| 70 } | |
| 71 | |
| 72 sql::Statement s2(db_->GetUniqueStatement( | |
| 73 "INSERT INTO payment_manifest_section " | |
| 74 "(method_name, package_name, version, finger_prints) " | |
| 75 "VALUES (?, ?, ?, ?)")); | |
| 76 for (size_t i = 0; i < manifest.size(); i++) { | |
| 77 int index = 0; | |
| 78 s2.BindString(index++, method_name); | |
| 79 s2.BindString(index++, manifest[i]->package_name); | |
| 80 s2.BindInt64(index++, manifest[i]->version); | |
| 81 | |
| 82 std::unique_ptr<std::vector<uint8_t>> serialized_finger_prints = | |
| 83 SerializeFingerPrints(manifest[i]->sha256_cert_fingerprints); | |
| 84 s2.BindBlob(index, serialized_finger_prints->data(), | |
| 85 serialized_finger_prints->size()); | |
| 86 | |
| 87 s2.Run(); | |
|
please use gerrit instead
2017/04/05 20:33:30
Check for error.
gogerald1
2017/04/05 23:38:48
Done.
| |
| 88 s2.Reset(true); | |
| 89 } | |
| 90 | |
| 91 return true; | |
| 92 } | |
| 93 | |
| 94 std::vector<mojom::PaymentManifestSectionPtr> | |
| 95 PaymentManifestSectionTable::GetPaymentManifestSections( | |
| 96 std::string& method_name) { | |
| 97 std::vector<mojom::PaymentManifestSectionPtr> manifest; | |
| 98 sql::Statement s( | |
| 99 db_->GetUniqueStatement("SELECT package_name, version, finger_prints " | |
| 100 "FROM payment_manifest_section " | |
| 101 "WHERE method_name=?")); | |
| 102 s.BindString(0, method_name); | |
| 103 | |
| 104 if (!s.is_valid()) { | |
|
please use gerrit instead
2017/04/05 20:33:30
No {}.
gogerald1
2017/04/05 23:38:48
Done.
please use gerrit instead
2017/04/06 14:35:28
Not yet.
gogerald1
2017/04/06 16:42:09
Done.
| |
| 105 return manifest; | |
| 106 } | |
| 107 | |
| 108 while (s.Step()) { | |
| 109 mojom::PaymentManifestSectionPtr section = | |
| 110 mojom::PaymentManifestSection::New(); | |
| 111 | |
| 112 int index = 0; | |
| 113 section->package_name = s.ColumnString(index++); | |
| 114 section->version = s.ColumnInt64(index++); | |
| 115 | |
| 116 std::vector<uint8_t> finger_prints; | |
| 117 if (!s.ColumnBlobAsVector(index, &finger_prints)) { | |
| 118 NOTREACHED(); | |
| 119 continue; | |
| 120 } | |
| 121 DeserializeFingerPrints(finger_prints, section->sha256_cert_fingerprints); | |
| 122 | |
| 123 manifest.push_back(std::move(section)); | |
| 124 } | |
| 125 | |
| 126 return manifest; | |
| 127 } | |
| 128 | |
| 129 std::unique_ptr<std::vector<uint8_t>> | |
| 130 PaymentManifestSectionTable::SerializeFingerPrints( | |
| 131 const std::vector<std::vector<uint8_t>>& finger_prints) { | |
| 132 std::unique_ptr<std::vector<uint8_t>> serialized_finger_prints( | |
| 133 new std::vector<uint8_t>()); | |
|
please use gerrit instead
2017/04/05 20:33:30
base::MakeUnique.
gogerald1
2017/04/05 23:38:49
Done.
| |
| 134 | |
| 135 if (finger_prints.empty()) { | |
| 136 return serialized_finger_prints; | |
| 137 } | |
|
please use gerrit instead
2017/04/05 20:33:30
This if statement is not useful, because the for l
gogerald1
2017/04/05 23:38:48
Done.
| |
| 138 | |
| 139 for (auto finger_print : finger_prints) { | |
|
please use gerrit instead
2017/04/05 20:33:30
const auto&
gogerald1
2017/04/05 23:38:49
Done.
| |
| 140 DCHECK(finger_print.size() == kFingerPrintLength); | |
| 141 for (auto value : finger_print) { | |
| 142 serialized_finger_prints->push_back(value); | |
| 143 } | |
|
please use gerrit instead
2017/04/05 20:33:30
Replace this for loop with:
serialized_finger_pri
gogerald1
2017/04/05 23:38:48
Done.
| |
| 144 } | |
| 145 | |
| 146 return serialized_finger_prints; | |
| 147 } | |
| 148 | |
| 149 void PaymentManifestSectionTable::DeserializeFingerPrints( | |
| 150 const std::vector<uint8_t>& finger_prints, | |
| 151 std::vector<std::vector<uint8_t>>& deserialized_finger_prints) { | |
| 152 DCHECK(finger_prints.size() % kFingerPrintLength == 0); | |
|
please use gerrit instead
2017/04/05 20:33:30
Don't trust anything that comes from disk. If this
gogerald1
2017/04/05 23:38:48
Done.
| |
| 153 for (size_t i = 0; i < finger_prints.size();) { | |
| 154 std::vector<uint8_t> finger_print; | |
| 155 for (uint32_t j = 0; j < kFingerPrintLength; j++) { | |
| 156 finger_print.push_back(finger_prints[i + j]); | |
| 157 } | |
| 158 deserialized_finger_prints.push_back(finger_print); | |
| 159 i += kFingerPrintLength; | |
| 160 } | |
|
please use gerrit instead
2017/04/05 20:33:31
You can do this loop easier:
for (size_t i = 0; i
gogerald1
2017/04/05 23:38:48
Done.
| |
| 161 } | |
|
please use gerrit instead
2017/04/05 20:33:30
Newline below.
gogerald1
2017/04/05 23:38:49
Done.
| |
| 162 } // payments | |
| OLD | NEW |