| 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 "base/time/time.h" |
| 11 #include "sql/statement.h" | 12 #include "sql/statement.h" |
| 12 #include "sql/transaction.h" | 13 #include "sql/transaction.h" |
| 13 | 14 |
| 14 namespace payments { | 15 namespace payments { |
| 15 namespace { | 16 namespace { |
| 17 // Data valid time in seconds. |
| 18 const time_t DATA_VALID_TIME_IN_SECONDS = 90 * 24 * 60; |
| 16 | 19 |
| 17 // Note that the fingerprint is calculated with SHA-256. | 20 // Note that the fingerprint is calculated with SHA-256. |
| 18 const size_t kFingerPrintLength = 32; | 21 const size_t kFingerPrintLength = 32; |
| 19 | 22 |
| 20 WebDatabaseTable::TypeKey GetKey() { | 23 WebDatabaseTable::TypeKey GetKey() { |
| 21 // We just need a unique constant. Use the address of a static that | 24 // We just need a unique constant. Use the address of a static that |
| 22 // COMDAT folding won't touch in an optimizing linker. | 25 // COMDAT folding won't touch in an optimizing linker. |
| 23 static int table_key = 0; | 26 static int table_key = 0; |
| 24 return reinterpret_cast<void*>(&table_key); | 27 return reinterpret_cast<void*>(&table_key); |
| 25 } | 28 } |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 65 WebDatabase* db) { | 68 WebDatabase* db) { |
| 66 return static_cast<WebAppManifestSectionTable*>(db->GetTable(GetKey())); | 69 return static_cast<WebAppManifestSectionTable*>(db->GetTable(GetKey())); |
| 67 } | 70 } |
| 68 | 71 |
| 69 WebDatabaseTable::TypeKey WebAppManifestSectionTable::GetTypeKey() const { | 72 WebDatabaseTable::TypeKey WebAppManifestSectionTable::GetTypeKey() const { |
| 70 return GetKey(); | 73 return GetKey(); |
| 71 } | 74 } |
| 72 | 75 |
| 73 bool WebAppManifestSectionTable::CreateTablesIfNecessary() { | 76 bool WebAppManifestSectionTable::CreateTablesIfNecessary() { |
| 74 if (!db_->Execute("CREATE TABLE IF NOT EXISTS web_app_manifest_section ( " | 77 if (!db_->Execute("CREATE TABLE IF NOT EXISTS web_app_manifest_section ( " |
| 78 "expire_date INTEGER NOT NULL DEFAULT 0, " |
| 75 "id VARCHAR, " | 79 "id VARCHAR, " |
| 76 "min_version INTEGER NOT NULL DEFAULT 0, " | 80 "min_version INTEGER NOT NULL DEFAULT 0, " |
| 77 "fingerprints BLOB) ")) { | 81 "fingerprints BLOB) ")) { |
| 78 NOTREACHED(); | 82 NOTREACHED(); |
| 79 return false; | 83 return false; |
| 80 } | 84 } |
| 81 | 85 |
| 82 return true; | 86 return true; |
| 83 } | 87 } |
| 84 | 88 |
| (...skipping 16 matching lines...) Expand all Loading... |
| 101 return false; | 105 return false; |
| 102 | 106 |
| 103 sql::Statement s1(db_->GetUniqueStatement( | 107 sql::Statement s1(db_->GetUniqueStatement( |
| 104 "DELETE FROM web_app_manifest_section WHERE id=? ")); | 108 "DELETE FROM web_app_manifest_section WHERE id=? ")); |
| 105 s1.BindString(0, manifest[0]->id); | 109 s1.BindString(0, manifest[0]->id); |
| 106 if (!s1.Run()) | 110 if (!s1.Run()) |
| 107 return false; | 111 return false; |
| 108 | 112 |
| 109 sql::Statement s2( | 113 sql::Statement s2( |
| 110 db_->GetUniqueStatement("INSERT INTO web_app_manifest_section " | 114 db_->GetUniqueStatement("INSERT INTO web_app_manifest_section " |
| 111 "(id, min_version, fingerprints) " | 115 "(expire_date, id, min_version, fingerprints) " |
| 112 "VALUES (?, ?, ?)")); | 116 "VALUES (?, ?, ?, ?)")); |
| 117 const time_t expire_date_in_seconds = |
| 118 base::Time::Now().ToTimeT() + DATA_VALID_TIME_IN_SECONDS; |
| 113 for (const auto& section : manifest) { | 119 for (const auto& section : manifest) { |
| 114 DCHECK_EQ(manifest[0]->id, section->id); | 120 DCHECK_EQ(manifest[0]->id, section->id); |
| 115 int index = 0; | 121 int index = 0; |
| 122 s2.BindInt64(index++, expire_date_in_seconds); |
| 116 s2.BindString(index++, section->id); | 123 s2.BindString(index++, section->id); |
| 117 s2.BindInt64(index++, section->min_version); | 124 s2.BindInt64(index++, section->min_version); |
| 118 std::unique_ptr<std::vector<uint8_t>> serialized_fingerprints = | 125 std::unique_ptr<std::vector<uint8_t>> serialized_fingerprints = |
| 119 SerializeFingerPrints(section->fingerprints); | 126 SerializeFingerPrints(section->fingerprints); |
| 120 s2.BindBlob(index, serialized_fingerprints->data(), | 127 s2.BindBlob(index, serialized_fingerprints->data(), |
| 121 serialized_fingerprints->size()); | 128 serialized_fingerprints->size()); |
| 122 if (!s2.Run()) | 129 if (!s2.Run()) |
| 123 return false; | 130 return false; |
| 124 s2.Reset(true); | 131 s2.Reset(true); |
| 125 } | 132 } |
| 126 | 133 |
| 127 if (!transaction.Commit()) | 134 if (!transaction.Commit()) |
| 128 return false; | 135 return false; |
| 129 | 136 |
| 130 return true; | 137 return true; |
| 131 } | 138 } |
| 132 | 139 |
| 133 std::vector<mojom::WebAppManifestSectionPtr> | 140 std::vector<mojom::WebAppManifestSectionPtr> |
| 134 WebAppManifestSectionTable::GetWebAppManifest(const std::string& web_app) { | 141 WebAppManifestSectionTable::GetWebAppManifest(const std::string& web_app) { |
| 135 sql::Statement s( | 142 sql::Statement s(db_->GetUniqueStatement( |
| 136 db_->GetUniqueStatement("SELECT id, min_version, fingerprints " | 143 "SELECT expire_date, id, min_version, fingerprints " |
| 137 "FROM web_app_manifest_section " | 144 "FROM web_app_manifest_section " |
| 138 "WHERE id=?")); | 145 "WHERE id=?")); |
| 139 s.BindString(0, web_app); | 146 s.BindString(0, web_app); |
| 140 | 147 |
| 148 const time_t now_time_in_seconds = base::Time::Now().ToTimeT(); |
| 141 std::vector<mojom::WebAppManifestSectionPtr> manifest; | 149 std::vector<mojom::WebAppManifestSectionPtr> manifest; |
| 142 while (s.Step()) { | 150 while (s.Step()) { |
| 151 int index = 0; |
| 152 if (now_time_in_seconds > s.ColumnInt64(index++)) { |
| 153 manifest.clear(); |
| 154 break; |
| 155 } |
| 156 |
| 143 mojom::WebAppManifestSectionPtr section = | 157 mojom::WebAppManifestSectionPtr section = |
| 144 mojom::WebAppManifestSection::New(); | 158 mojom::WebAppManifestSection::New(); |
| 145 | 159 |
| 146 int index = 0; | |
| 147 section->id = s.ColumnString(index++); | 160 section->id = s.ColumnString(index++); |
| 148 section->min_version = s.ColumnInt64(index++); | 161 section->min_version = s.ColumnInt64(index++); |
| 149 | 162 |
| 150 std::vector<uint8_t> fingerprints; | 163 std::vector<uint8_t> fingerprints; |
| 151 if (!s.ColumnBlobAsVector(index, &fingerprints)) { | 164 if (!s.ColumnBlobAsVector(index, &fingerprints)) { |
| 152 manifest.clear(); | 165 manifest.clear(); |
| 153 break; | 166 break; |
| 154 } | 167 } |
| 155 | 168 |
| 156 if (!DeserializeFingerPrints(fingerprints, section->fingerprints)) { | 169 if (!DeserializeFingerPrints(fingerprints, section->fingerprints)) { |
| 157 manifest.clear(); | 170 manifest.clear(); |
| 158 break; | 171 break; |
| 159 } | 172 } |
| 160 | 173 |
| 161 manifest.emplace_back(std::move(section)); | 174 manifest.emplace_back(std::move(section)); |
| 162 } | 175 } |
| 163 | 176 |
| 164 return manifest; | 177 return manifest; |
| 165 } | 178 } |
| 166 | 179 |
| 167 } // namespace payments | 180 } // namespace payments |
| OLD | NEW |