OLD | NEW |
1 // Copyright 2017 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "components/payments/android/web_app_manifest_section_table.h" | 5 #include "components/payments/android/web_app_manifest_section_table.h" |
6 | 6 |
7 #include <stdint.h> | 7 #include <stdint.h> |
8 | 8 |
9 #include <memory> | 9 #include <memory> |
10 | 10 |
11 #include "sql/statement.h" | 11 #include "sql/statement.h" |
| 12 #include "sql/transaction.h" |
12 | 13 |
13 namespace payments { | 14 namespace payments { |
14 namespace { | 15 namespace { |
15 | 16 |
16 // Note that the fingerprint is calculated with SHA-256. | 17 // Note that the fingerprint is calculated with SHA-256. |
17 const size_t kFingerPrintLength = 32; | 18 const size_t kFingerPrintLength = 32; |
18 | 19 |
19 WebDatabaseTable::TypeKey GetKey() { | 20 WebDatabaseTable::TypeKey GetKey() { |
20 // We just need a unique constant. Use the address of a static that | 21 // We just need a unique constant. Use the address of a static that |
21 // COMDAT folding won't touch in an optimizing linker. | 22 // COMDAT folding won't touch in an optimizing linker. |
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
64 WebDatabase* db) { | 65 WebDatabase* db) { |
65 return static_cast<WebAppManifestSectionTable*>(db->GetTable(GetKey())); | 66 return static_cast<WebAppManifestSectionTable*>(db->GetTable(GetKey())); |
66 } | 67 } |
67 | 68 |
68 WebDatabaseTable::TypeKey WebAppManifestSectionTable::GetTypeKey() const { | 69 WebDatabaseTable::TypeKey WebAppManifestSectionTable::GetTypeKey() const { |
69 return GetKey(); | 70 return GetKey(); |
70 } | 71 } |
71 | 72 |
72 bool WebAppManifestSectionTable::CreateTablesIfNecessary() { | 73 bool WebAppManifestSectionTable::CreateTablesIfNecessary() { |
73 if (!db_->Execute("CREATE TABLE IF NOT EXISTS web_app_manifest_section ( " | 74 if (!db_->Execute("CREATE TABLE IF NOT EXISTS web_app_manifest_section ( " |
74 "id VARCHAR PRIMARY KEY, " | 75 "id VARCHAR, " |
75 "min_version INTEGER NOT NULL DEFAULT 0, " | 76 "min_version INTEGER NOT NULL DEFAULT 0, " |
76 "fingerprints BLOB) ")) { | 77 "fingerprints BLOB) ")) { |
77 NOTREACHED(); | 78 NOTREACHED(); |
78 return false; | 79 return false; |
79 } | 80 } |
80 | 81 |
81 return true; | 82 return true; |
82 } | 83 } |
83 | 84 |
84 bool WebAppManifestSectionTable::IsSyncable() { | 85 bool WebAppManifestSectionTable::IsSyncable() { |
85 return false; | 86 return false; |
86 } | 87 } |
87 | 88 |
88 bool WebAppManifestSectionTable::MigrateToVersion( | 89 bool WebAppManifestSectionTable::MigrateToVersion( |
89 int version, | 90 int version, |
90 bool* update_compatible_version) { | 91 bool* update_compatible_version) { |
91 return true; | 92 return true; |
92 } | 93 } |
93 | 94 |
94 bool WebAppManifestSectionTable::AddWebAppManifest( | 95 bool WebAppManifestSectionTable::AddWebAppManifest( |
95 mojom::WebAppManifestSection* manifest) { | 96 const std::vector<mojom::WebAppManifestSectionPtr>& manifest) { |
96 DCHECK(manifest); | 97 DCHECK_LT(0U, manifest.size()); |
97 DCHECK(!manifest->id.empty()); | |
98 | 98 |
99 sql::Statement s( | 99 sql::Transaction transaction(db_); |
100 db_->GetUniqueStatement("INSERT OR REPLACE INTO web_app_manifest_section " | 100 if (!transaction.Begin()) |
| 101 return false; |
| 102 |
| 103 sql::Statement s1(db_->GetUniqueStatement( |
| 104 "DELETE FROM web_app_manifest_section WHERE id=? ")); |
| 105 s1.BindString(0, manifest[0]->id); |
| 106 if (!s1.Run()) |
| 107 return false; |
| 108 |
| 109 sql::Statement s2( |
| 110 db_->GetUniqueStatement("INSERT INTO web_app_manifest_section " |
101 "(id, min_version, fingerprints) " | 111 "(id, min_version, fingerprints) " |
102 "VALUES (?, ?, ?)")); | 112 "VALUES (?, ?, ?)")); |
103 int index = 0; | 113 for (const auto& section : manifest) { |
104 s.BindString(index++, manifest->id); | 114 DCHECK_EQ(manifest[0]->id, section->id); |
105 s.BindInt64(index++, manifest->min_version); | 115 int index = 0; |
106 std::unique_ptr<std::vector<uint8_t>> serialized_fingerprints = | 116 s2.BindString(index++, section->id); |
107 SerializeFingerPrints(manifest->fingerprints); | 117 s2.BindInt64(index++, section->min_version); |
108 s.BindBlob(index, serialized_fingerprints->data(), | 118 std::unique_ptr<std::vector<uint8_t>> serialized_fingerprints = |
109 serialized_fingerprints->size()); | 119 SerializeFingerPrints(section->fingerprints); |
110 if (!s.Run()) | 120 s2.BindBlob(index, serialized_fingerprints->data(), |
| 121 serialized_fingerprints->size()); |
| 122 if (!s2.Run()) |
| 123 return false; |
| 124 s2.Reset(true); |
| 125 } |
| 126 |
| 127 if (!transaction.Commit()) |
111 return false; | 128 return false; |
112 | 129 |
113 return true; | 130 return true; |
114 } | 131 } |
115 | 132 |
116 mojom::WebAppManifestSectionPtr WebAppManifestSectionTable::GetWebAppManifest( | 133 std::vector<mojom::WebAppManifestSectionPtr> |
117 const std::string& web_app) { | 134 WebAppManifestSectionTable::GetWebAppManifest(const std::string& web_app) { |
118 sql::Statement s( | 135 sql::Statement s( |
119 db_->GetUniqueStatement("SELECT id, min_version, fingerprints " | 136 db_->GetUniqueStatement("SELECT id, min_version, fingerprints " |
120 "FROM web_app_manifest_section " | 137 "FROM web_app_manifest_section " |
121 "WHERE id=?")); | 138 "WHERE id=?")); |
122 s.BindString(0, web_app); | 139 s.BindString(0, web_app); |
123 | 140 |
124 if (!s.Step()) | 141 std::vector<mojom::WebAppManifestSectionPtr> manifest; |
125 return nullptr; | 142 while (s.Step()) { |
| 143 mojom::WebAppManifestSectionPtr section = |
| 144 mojom::WebAppManifestSection::New(); |
126 | 145 |
127 mojom::WebAppManifestSectionPtr manifest = | 146 int index = 0; |
128 mojom::WebAppManifestSection::New(); | 147 section->id = s.ColumnString(index++); |
| 148 section->min_version = s.ColumnInt64(index++); |
129 | 149 |
130 int index = 0; | 150 std::vector<uint8_t> fingerprints; |
131 manifest->id = s.ColumnString(index++); | 151 if (!s.ColumnBlobAsVector(index, &fingerprints)) { |
132 manifest->min_version = s.ColumnInt64(index++); | 152 manifest.clear(); |
| 153 break; |
| 154 } |
133 | 155 |
134 std::vector<uint8_t> fingerprints; | 156 if (!DeserializeFingerPrints(fingerprints, section->fingerprints)) { |
135 if (!s.ColumnBlobAsVector(index, &fingerprints)) | 157 manifest.clear(); |
136 return nullptr; | 158 break; |
| 159 } |
137 | 160 |
138 if (!DeserializeFingerPrints(fingerprints, manifest->fingerprints)) | 161 manifest.emplace_back(std::move(section)); |
139 return nullptr; | 162 } |
140 | 163 |
141 return manifest; | 164 return manifest; |
142 } | 165 } |
143 | 166 |
144 } // namespace payments | 167 } // namespace payments |
OLD | NEW |