Chromium Code Reviews| 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, " |
|
please use gerrit instead
2017/04/24 18:22:32
What happened to the primary key?
gogerald1
2017/04/26 13:46:33
id is not unique since an app may have multiple We
| |
| 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(manifest.size() > 0); |
|
please use gerrit instead
2017/04/24 18:22:33
DCHECK_LT(0U, manifest.size());
gogerald1
2017/04/26 13:46:33
Done.
| |
| 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) { |
|
please use gerrit instead
2017/04/24 18:22:32
DCHECK_EQ(manifest[0]->id, section->id);
gogerald1
2017/04/26 13:46:33
Done.
| |
| 104 s.BindString(index++, manifest->id); | 114 int index = 0; |
| 105 s.BindInt64(index++, manifest->min_version); | 115 s2.BindString(index++, section->id); |
| 106 std::unique_ptr<std::vector<uint8_t>> serialized_fingerprints = | 116 s2.BindInt64(index++, section->min_version); |
| 107 SerializeFingerPrints(manifest->fingerprints); | 117 std::unique_ptr<std::vector<uint8_t>> serialized_fingerprints = |
| 108 s.BindBlob(index, serialized_fingerprints->data(), | 118 SerializeFingerPrints(section->fingerprints); |
| 109 serialized_fingerprints->size()); | 119 s2.BindBlob(index, serialized_fingerprints->data(), |
| 110 if (!s.Run()) | 120 serialized_fingerprints->size()); |
| 121 if (!s2.Run()) | |
| 122 return false; | |
| 123 s2.Reset(true); | |
| 124 } | |
| 125 | |
| 126 if (!transaction.Commit()) | |
| 111 return false; | 127 return false; |
| 112 | 128 |
| 113 return true; | 129 return true; |
| 114 } | 130 } |
| 115 | 131 |
| 116 mojom::WebAppManifestSectionPtr WebAppManifestSectionTable::GetWebAppManifest( | 132 std::vector<mojom::WebAppManifestSectionPtr> |
| 117 const std::string& web_app) { | 133 WebAppManifestSectionTable::GetWebAppManifest(const std::string& web_app) { |
| 118 sql::Statement s( | 134 sql::Statement s( |
| 119 db_->GetUniqueStatement("SELECT id, min_version, fingerprints " | 135 db_->GetUniqueStatement("SELECT id, min_version, fingerprints " |
| 120 "FROM web_app_manifest_section " | 136 "FROM web_app_manifest_section " |
| 121 "WHERE id=?")); | 137 "WHERE id=?")); |
| 122 s.BindString(0, web_app); | 138 s.BindString(0, web_app); |
| 123 | 139 |
| 124 if (!s.Step()) | 140 std::vector<mojom::WebAppManifestSectionPtr> manifest; |
| 125 return nullptr; | 141 while (s.Step()) { |
| 142 mojom::WebAppManifestSectionPtr section = | |
| 143 mojom::WebAppManifestSection::New(); | |
| 126 | 144 |
| 127 mojom::WebAppManifestSectionPtr manifest = | 145 int index = 0; |
| 128 mojom::WebAppManifestSection::New(); | 146 section->id = s.ColumnString(index++); |
| 147 section->min_version = s.ColumnInt64(index++); | |
| 129 | 148 |
| 130 int index = 0; | 149 std::vector<uint8_t> fingerprints; |
| 131 manifest->id = s.ColumnString(index++); | 150 if (!s.ColumnBlobAsVector(index, &fingerprints)) |
|
please use gerrit instead
2017/04/24 18:22:32
This is an error condition, so you should signal a
gogerald1
2017/04/26 13:46:33
Done.
| |
| 132 manifest->min_version = s.ColumnInt64(index++); | 151 break; |
| 133 | 152 |
| 134 std::vector<uint8_t> fingerprints; | 153 if (!DeserializeFingerPrints(fingerprints, section->fingerprints)) |
| 135 if (!s.ColumnBlobAsVector(index, &fingerprints)) | 154 break; |
| 136 return nullptr; | |
| 137 | 155 |
| 138 if (!DeserializeFingerPrints(fingerprints, manifest->fingerprints)) | 156 manifest.emplace_back(std::move(section)); |
| 139 return nullptr; | 157 } |
| 140 | 158 |
| 141 return manifest; | 159 return manifest; |
| 142 } | 160 } |
| 143 | 161 |
| 144 } // namespace payments | 162 } // namespace payments |
| OLD | NEW |