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/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, " | |
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 != nullptr); | |
99 if (manifest->id.empty()) | |
100 return false; | |
101 | |
102 sql::Transaction transaction(db_); | |
103 if (!transaction.Begin()) | |
104 return false; | |
105 | |
106 sql::Statement s1(db_->GetUniqueStatement( | |
107 "DELETE FROM web_app_manifest_section WHERE id=?")); | |
108 s1.BindString(0, manifest->id); | |
109 if (!s1.Run()) | |
110 return false; | |
111 | |
112 sql::Statement s2( | |
113 db_->GetUniqueStatement("INSERT INTO web_app_manifest_section " | |
114 "(id, min_version, fingerprints) " | |
115 "VALUES (?, ?, ?)")); | |
116 int index = 0; | |
117 s2.BindString(index++, manifest->id); | |
118 s2.BindInt64(index++, manifest->min_version); | |
119 std::unique_ptr<std::vector<uint8_t>> serialized_fingerprints = | |
120 SerializeFingerPrints(manifest->fingerprints); | |
121 s2.BindBlob(index, serialized_fingerprints->data(), | |
122 serialized_fingerprints->size()); | |
123 if (!s2.Run()) | |
124 return false; | |
125 | |
126 if (!transaction.Commit()) | |
127 return false; | |
128 | |
129 return true; | |
130 } | |
131 | |
132 mojom::WebAppManifestSectionPtr WebAppManifestSectionTable::GetWebAppManifest( | |
133 std::string web_app) { | |
134 sql::Statement s( | |
135 db_->GetUniqueStatement("SELECT id, min_version, fingerprints " | |
136 "FROM web_app_manifest_section " | |
137 "WHERE id=?" | |
138 "LIMIT 1")); | |
Scott Hess - ex-Googler
2017/04/11 19:31:46
Why are you doing the LIMIT 1, here?
Oooooh. Sho
gogerald1
2017/04/11 21:53:30
Done.
| |
139 s.BindString(0, web_app); | |
140 | |
141 if (!s.is_valid()) | |
142 return nullptr; | |
Scott Hess - ex-Googler
2017/04/11 19:31:46
This is implicit in s.Step(). It will return |fal
gogerald1
2017/04/11 21:53:30
Done.
| |
143 | |
144 if (s.Step()) { | |
145 mojom::WebAppManifestSectionPtr manifest = | |
146 mojom::WebAppManifestSection::New(); | |
147 | |
148 int index = 0; | |
149 manifest->id = s.ColumnString(index++); | |
150 manifest->min_version = s.ColumnInt64(index++); | |
151 | |
152 std::vector<uint8_t> fingerprints; | |
153 if (!s.ColumnBlobAsVector(index, &fingerprints)) | |
154 return nullptr; | |
155 | |
156 if (!DeserializeFingerPrints(fingerprints, manifest->fingerprints)) | |
157 return nullptr; | |
158 | |
159 return manifest; | |
160 } | |
161 | |
162 return nullptr; | |
163 } | |
164 | |
165 } // payments | |
OLD | NEW |