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

Side by Side Diff: components/payments/android/web_app_manifest_section_table.cc

Issue 2801513002: [Payments] Add web app manifest section table in SQLite web database (Closed)
Patch Set: rebase and rename for the new manifest format 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/android/web_app_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 fingerprint 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 |fingerprints| to 1-dimesional vector.
28 std::unique_ptr<std::vector<uint8_t>> SerializeFingerPrints(
29 const std::vector<std::vector<uint8_t>>& fingerprints) {
30 auto serialized_fingerprints = base::MakeUnique<std::vector<uint8_t>>();
31
32 for (const auto& fingerprint : fingerprints) {
33 DCHECK_EQ(fingerprint.size(), kFingerPrintLength);
34 serialized_fingerprints->insert(serialized_fingerprints->end(),
35 fingerprint.begin(), fingerprint.end());
36 }
37
38 return serialized_fingerprints;
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>& fingerprints,
46 std::vector<std::vector<uint8_t>>& deserialized_fingerprints) {
47 if (fingerprints.size() % kFingerPrintLength != 0)
48 return false;
49
50 for (size_t i = 0; i < fingerprints.size(); i += kFingerPrintLength) {
51 deserialized_fingerprints.emplace_back(
52 fingerprints.begin() + i,
53 fingerprints.begin() + i + kFingerPrintLength);
54 }
55 return true;
56 }
57
58 } // namespace
59
60 WebAppManifestSectionTable::WebAppManifestSectionTable() {}
61
62 WebAppManifestSectionTable::~WebAppManifestSectionTable() {}
63
64 WebAppManifestSectionTable* WebAppManifestSectionTable::FromWebDatabase(
65 WebDatabase* db) {
66 return static_cast<WebAppManifestSectionTable*>(db->GetTable(GetKey()));
67 }
68
69 WebDatabaseTable::TypeKey WebAppManifestSectionTable::GetTypeKey() const {
70 return GetKey();
71 }
72
73 bool WebAppManifestSectionTable::CreateTablesIfNecessary() {
74 if (!db_->DoesTableExist("web_app_manifest_section")) {
75 if (!db_->Execute("CREATE TABLE web_app_manifest_section ( "
Scott Hess - ex-Googler 2017/04/11 17:24:06 How about "CREATE TABLE IF NOT EXISTS web_app_mani
gogerald1 2017/04/11 19:01:04 Done.
76 "id VARCHAR, "
77 "min_version INTEGER NOT NULL DEFAULT 0, "
78 "fingerprints BLOB) ")) {
79 NOTREACHED();
80 return false;
81 }
82 }
83
84 return true;
85 }
86
87 bool WebAppManifestSectionTable::IsSyncable() {
88 return false;
89 }
90
91 bool WebAppManifestSectionTable::MigrateToVersion(
92 int version,
93 bool* update_compatible_version) {
94 return true;
95 }
96
97 bool WebAppManifestSectionTable::AddWebAppManifest(
98 mojom::WebAppManifestSection* manifest) {
99 if (manifest == nullptr || manifest->id.empty())
Scott Hess - ex-Googler 2017/04/11 17:24:06 Is this something which should never happen becaus
gogerald1 2017/04/11 19:01:04 Done. manifest should never be nullptr, DCHECKed i
Scott Hess - ex-Googler 2017/04/11 19:31:45 I'm wondering about the empty() check, now. All o
gogerald1 2017/04/11 21:53:30 Done.
100 return false;
101
102 if (!db_->BeginTransaction())
Scott Hess - ex-Googler 2017/04/11 17:24:06 Manual transaction handling is brittle, use sql::T
gogerald1 2017/04/11 19:01:04 Done.
103 return false;
104
105 sql::Statement s1(db_->GetUniqueStatement(
106 "DELETE FROM web_app_manifest_section WHERE id=?"));
107 s1.BindString(0, manifest->id);
108 if (!s1.Run()) {
109 db_->RollbackTransaction();
110 return false;
111 }
112
113 sql::Statement s2(
114 db_->GetUniqueStatement("INSERT INTO web_app_manifest_section "
115 "(id, min_version, fingerprints) "
116 "VALUES (?, ?, ?)"));
117 int index = 0;
118 s2.BindString(index++, manifest->id);
119 s2.BindInt64(index++, manifest->min_version);
120 std::unique_ptr<std::vector<uint8_t>> serialized_fingerprints =
121 SerializeFingerPrints(manifest->fingerprints);
122 s2.BindBlob(index, serialized_fingerprints->data(),
123 serialized_fingerprints->size());
124 if (!s2.Run()) {
125 db_->RollbackTransaction();
126 return false;
127 }
128
129 if (!db_->CommitTransaction()) {
130 db_->RollbackTransaction();
131 return false;
132 }
133
134 return true;
135 }
136
137 mojom::WebAppManifestSectionPtr WebAppManifestSectionTable::GetWebAppManifest(
138 std::string web_app) {
139 mojom::WebAppManifestSectionPtr manifest;
140 sql::Statement s(
141 db_->GetUniqueStatement("SELECT id, min_version, fingerprints "
142 "FROM web_app_manifest_section "
143 "WHERE id=?"
144 "LIMIT 1"));
145 s.BindString(0, web_app);
146
147 if (!s.is_valid())
148 return manifest;
Scott Hess - ex-Googler 2017/04/11 17:24:06 |manifest| is not apparently initialized at this p
gogerald1 2017/04/11 19:01:04 Done.
149
150 if (s.Step()) {
151 manifest = mojom::WebAppManifestSection::New();
152
153 int index = 0;
154 manifest->id = s.ColumnString(index++);
155 manifest->min_version = s.ColumnInt64(index++);
156
157 std::vector<uint8_t> fingerprints;
158 if (!s.ColumnBlobAsVector(index, &fingerprints)) {
159 manifest = nullptr;
160 return manifest;
Scott Hess - ex-Googler 2017/04/11 17:24:06 Just return nullptr. I assume |manifest| is scope
gogerald1 2017/04/11 19:01:04 Done.
161 }
162
163 if (!DeserializeFingerPrints(fingerprints, manifest->fingerprints)) {
164 manifest = nullptr;
165 return manifest;
Scott Hess - ex-Googler 2017/04/11 17:24:06 Also here.
gogerald1 2017/04/11 19:01:04 Done.
166 }
167 }
168
169 return manifest;
170 }
171
172 } // payments
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698