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

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: 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 #include "sql/transaction.h"
14
15 namespace payments {
16 namespace {
17
18 // Note that the fingerprint is calculated with SHA-256.
19 const size_t kFingerPrintLength = 32;
20
21 WebDatabaseTable::TypeKey GetKey() {
22 // We just need a unique constant. Use the address of a static that
23 // COMDAT folding won't touch in an optimizing linker.
24 static int table_key = 0;
25 return reinterpret_cast<void*>(&table_key);
26 }
27
28 // Converts 2-dimensional vector |fingerprints| to 1-dimesional vector.
29 std::unique_ptr<std::vector<uint8_t>> SerializeFingerPrints(
30 const std::vector<std::vector<uint8_t>>& fingerprints) {
31 auto serialized_fingerprints = base::MakeUnique<std::vector<uint8_t>>();
32
33 for (const auto& fingerprint : fingerprints) {
34 DCHECK_EQ(fingerprint.size(), kFingerPrintLength);
35 serialized_fingerprints->insert(serialized_fingerprints->end(),
36 fingerprint.begin(), fingerprint.end());
37 }
38
39 return serialized_fingerprints;
40 }
41
42 // Converts 1-dimensional vector created by SerializeFingerPrints back to
43 // 2-dimensional vector. Each vector of the second dimensional vector has exact
44 // kFingerPrintLength number of elements.
45 bool DeserializeFingerPrints(
46 const std::vector<uint8_t>& fingerprints,
47 std::vector<std::vector<uint8_t>>& deserialized_fingerprints) {
48 if (fingerprints.size() % kFingerPrintLength != 0)
49 return false;
50
51 for (size_t i = 0; i < fingerprints.size(); i += kFingerPrintLength) {
52 deserialized_fingerprints.emplace_back(
53 fingerprints.begin() + i,
54 fingerprints.begin() + i + kFingerPrintLength);
55 }
56 return true;
57 }
58
59 } // namespace
60
61 WebAppManifestSectionTable::WebAppManifestSectionTable() {}
62
63 WebAppManifestSectionTable::~WebAppManifestSectionTable() {}
64
65 WebAppManifestSectionTable* WebAppManifestSectionTable::FromWebDatabase(
66 WebDatabase* db) {
67 return static_cast<WebAppManifestSectionTable*>(db->GetTable(GetKey()));
68 }
69
70 WebDatabaseTable::TypeKey WebAppManifestSectionTable::GetTypeKey() const {
71 return GetKey();
72 }
73
74 bool WebAppManifestSectionTable::CreateTablesIfNecessary() {
75 if (!db_->Execute("CREATE TABLE IF NOT EXISTS web_app_manifest_section ( "
76 "id VARCHAR PRIMARY KEY, "
77 "min_version INTEGER NOT NULL DEFAULT 0, "
78 "fingerprints BLOB) ")) {
79 NOTREACHED();
80 return false;
81 }
82
83 return true;
84 }
85
86 bool WebAppManifestSectionTable::IsSyncable() {
87 return false;
88 }
89
90 bool WebAppManifestSectionTable::MigrateToVersion(
91 int version,
92 bool* update_compatible_version) {
93 return true;
94 }
95
96 bool WebAppManifestSectionTable::AddWebAppManifest(
97 mojom::WebAppManifestSection* manifest) {
98 DCHECK(manifest);
99 DCHECK(!manifest->id.empty());
100
101 sql::Statement s(
102 db_->GetUniqueStatement("INSERT OR REPLACE INTO web_app_manifest_section "
103 "(id, min_version, fingerprints) "
104 "VALUES (?, ?, ?)"));
105 int index = 0;
106 s.BindString(index++, manifest->id);
107 s.BindInt64(index++, manifest->min_version);
108 std::unique_ptr<std::vector<uint8_t>> serialized_fingerprints =
109 SerializeFingerPrints(manifest->fingerprints);
110 s.BindBlob(index, serialized_fingerprints->data(),
111 serialized_fingerprints->size());
112 if (!s.Run())
113 return false;
114
115 return true;
116 }
117
118 mojom::WebAppManifestSectionPtr WebAppManifestSectionTable::GetWebAppManifest(
119 const std::string& web_app) {
120 sql::Statement s(
121 db_->GetUniqueStatement("SELECT id, min_version, fingerprints "
122 "FROM web_app_manifest_section "
123 "WHERE id=?"));
124 s.BindString(0, web_app);
125
126 if (!s.Step())
127 return nullptr;
128
129 mojom::WebAppManifestSectionPtr manifest =
130 mojom::WebAppManifestSection::New();
131
132 int index = 0;
133 manifest->id = s.ColumnString(index++);
134 manifest->min_version = s.ColumnInt64(index++);
135
136 std::vector<uint8_t> fingerprints;
137 if (!s.ColumnBlobAsVector(index, &fingerprints))
138 return nullptr;
139
140 if (!DeserializeFingerPrints(fingerprints, manifest->fingerprints))
141 return nullptr;
142
143 return manifest;
144 }
145
146 } // payments
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698