Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(173)

Side by Side Diff: components/payments/content/payment_manifest_section_table.cc

Issue 2801513002: [Payments] Add web app manifest section table in SQLite web database (Closed)
Patch Set: address comments Created 3 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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 "sql/statement.h"
13
14 namespace payments {
15 namespace {
16
17 // Note that the finger print is calculated with SHA-256.
18 const size_t kFingerPrintLength = 32;
19
20 WebDatabaseTable::TypeKey GetKey() {
21 // We just need a unique constant. Use the address of a static that
22 // COMDAT folding won't touch in an optimizing linker.
23 static int table_key = 0;
24 return reinterpret_cast<void*>(&table_key);
25 }
26
27 // Converts 2-dimensional vector |finger_prints| to 1-dimesional vector.
28 std::unique_ptr<std::vector<uint8_t>> SerializeFingerPrints(
29 const std::vector<std::vector<uint8_t>>& finger_prints) {
30 auto serialized_finger_prints = base::MakeUnique<std::vector<uint8_t>>();
31
32 for (const auto finger_print : finger_prints) {
please use gerrit instead 2017/04/06 14:35:29 Let's put a "&" after "auto" to make sure that no
gogerald1 2017/04/06 16:42:09 Done.
33 DCHECK(finger_print.size() == kFingerPrintLength);
please use gerrit instead 2017/04/06 14:35:28 DCHECK_EQ(kFingerPrintLength, finger_print.size())
gogerald1 2017/04/06 16:42:09 Done.
34 serialized_finger_prints->insert(serialized_finger_prints->end(),
35 finger_print.begin(), finger_print.end());
36 }
37
38 return serialized_finger_prints;
39 }
40
41 // Converts 1-dimensional vector created by SerializeFingerPrints back to
42 // 2-dimensional vector. Each vector of the second dimensional vector has exact
43 // kFingerPrintLength number of elements.
44 bool DeserializeFingerPrints(
45 const std::vector<uint8_t>& finger_prints,
46 std::vector<std::vector<uint8_t>>& deserialized_finger_prints) {
please use gerrit instead 2017/04/06 14:35:29 Output is usually a pointer instead of ref.
gogerald1 2017/04/06 16:42:09 Agreed, but ref looks slightly better here since d
47 if (finger_prints.size() % kFingerPrintLength != 0)
48 return false;
49
50 for (size_t i = 0; i < finger_prints.size();) {
please use gerrit instead 2017/04/06 14:35:29 You can move i+=kFingerprintlength into this line.
gogerald1 2017/04/06 16:42:09 Done.
51 deserialized_finger_prints.emplace_back(
52 finger_prints.begin() + i,
53 finger_prints.begin() + i + kFingerPrintLength);
54 i += kFingerPrintLength;
55 }
56 return true;
57 }
58
59 } // namespace
60
61 PaymentManifestSectionTable::PaymentManifestSectionTable() {}
62
63 PaymentManifestSectionTable::~PaymentManifestSectionTable() {}
64
65 PaymentManifestSectionTable* PaymentManifestSectionTable::FromWebDatabase(
66 WebDatabase* db) {
67 return static_cast<PaymentManifestSectionTable*>(db->GetTable(GetKey()));
68 }
69
70 WebDatabaseTable::TypeKey PaymentManifestSectionTable::GetTypeKey() const {
71 return GetKey();
72 }
73
74 bool PaymentManifestSectionTable::CreateTablesIfNecessary() {
75 if (!db_->DoesTableExist("payment_manifest_section")) {
76 if (!db_->Execute("CREATE TABLE payment_manifest_section ( "
77 "method_name VARCHAR, "
78 "package_name VARCHAR, "
79 "version INTEGER NOT NULL DEFAULT 0, "
80 "finger_prints BLOB) ")) {
81 NOTREACHED();
82 return false;
83 }
84 }
85
86 return true;
87 }
88
89 bool PaymentManifestSectionTable::IsSyncable() {
90 return false;
91 }
92
93 bool PaymentManifestSectionTable::MigrateToVersion(
94 int version,
95 bool* update_compatible_version) {
96 return true;
97 }
98
99 bool PaymentManifestSectionTable::AddPaymentManifestSections(
100 const std::string& method_name,
101 const std::vector<mojom::PaymentManifestSectionPtr>& manifest) {
102 if (!db_->BeginTransaction())
103 return false;
104
105 sql::Statement s1(db_->GetUniqueStatement(
106 "DELETE FROM payment_manifest_section WHERE method_name=?"));
107 s1.BindString(0, method_name);
108
109 if (!s1.Run()) {
110 db_->RollbackTransaction();
111 return false;
112 }
113
114 sql::Statement s2(db_->GetUniqueStatement(
115 "INSERT INTO payment_manifest_section "
116 "(method_name, package_name, version, finger_prints) "
117 "VALUES (?, ?, ?, ?)"));
118 for (size_t i = 0; i < manifest.size(); i++) {
119 int index = 0;
120 s2.BindString(index++, method_name);
121 s2.BindString(index++, manifest[i]->package_name);
122 s2.BindInt64(index++, manifest[i]->version);
123
124 std::unique_ptr<std::vector<uint8_t>> serialized_finger_prints =
125 SerializeFingerPrints(manifest[i]->sha256_cert_fingerprints);
126 s2.BindBlob(index, serialized_finger_prints->data(),
127 serialized_finger_prints->size());
128
129 if (!s2.Run()) {
130 db_->RollbackTransaction();
131 return false;
132 }
133 s2.Reset(true);
134 }
135
136 if (!db_->CommitTransaction()) {
137 db_->RollbackTransaction();
138 return false;
139 }
140
141 return true;
142 }
143
144 std::vector<mojom::PaymentManifestSectionPtr>
145 PaymentManifestSectionTable::GetPaymentManifestSections(
146 const std::string& method_name) {
147 std::vector<mojom::PaymentManifestSectionPtr> manifest;
148 sql::Statement s(
149 db_->GetUniqueStatement("SELECT package_name, version, finger_prints "
150 "FROM payment_manifest_section "
151 "WHERE method_name=?"));
152 s.BindString(0, method_name);
153
154 if (!s.is_valid()) {
155 return manifest;
156 }
157
158 while (s.Step()) {
159 mojom::PaymentManifestSectionPtr section =
160 mojom::PaymentManifestSection::New();
161
162 int index = 0;
163 section->package_name = s.ColumnString(index++);
164 section->version = s.ColumnInt64(index++);
165
166 std::vector<uint8_t> finger_prints;
167 if (!s.ColumnBlobAsVector(index, &finger_prints)) {
168 NOTREACHED();
169 continue;
please use gerrit instead 2017/04/06 14:35:29 Let's not read out only partial information from t
gogerald1 2017/04/06 16:42:09 Done.
170 }
171
172 if (!DeserializeFingerPrints(finger_prints,
173 section->sha256_cert_fingerprints)) {
174 NOTREACHED();
175 continue;
please use gerrit instead 2017/04/06 14:35:28 manifest.clear(); return manifest;
gogerald1 2017/04/06 16:42:09 Done.
176 }
177
178 manifest.push_back(std::move(section));
179 }
180
181 return manifest;
182 }
183
184 } // payments
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698