| 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 #include <time.h> |
| 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 duration in seconds. |
| 18 const time_t DATA_VALID_TIME_IN_SECONDS = 90 * 24 * 60 * 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 |
| 85 bool WebAppManifestSectionTable::IsSyncable() { | 89 bool WebAppManifestSectionTable::IsSyncable() { |
| 86 return false; | 90 return false; |
| 87 } | 91 } |
| 88 | 92 |
| 89 bool WebAppManifestSectionTable::MigrateToVersion( | 93 bool WebAppManifestSectionTable::MigrateToVersion( |
| 90 int version, | 94 int version, |
| 91 bool* update_compatible_version) { | 95 bool* update_compatible_version) { |
| 92 return true; | 96 return true; |
| 93 } | 97 } |
| 94 | 98 |
| 99 void WebAppManifestSectionTable::RemoveExpiredData() { |
| 100 const time_t now_date_in_seconds = base::Time::NowFromSystemTime().ToTimeT(); |
| 101 sql::Statement s(db_->GetUniqueStatement( |
| 102 "DELETE FROM web_app_manifest_section WHERE expire_date < ? ")); |
| 103 s.BindInt64(0, now_date_in_seconds); |
| 104 s.Run(); |
| 105 } |
| 106 |
| 95 bool WebAppManifestSectionTable::AddWebAppManifest( | 107 bool WebAppManifestSectionTable::AddWebAppManifest( |
| 96 const std::vector<mojom::WebAppManifestSectionPtr>& manifest) { | 108 const std::vector<mojom::WebAppManifestSectionPtr>& manifest) { |
| 97 DCHECK_LT(0U, manifest.size()); | 109 DCHECK_LT(0U, manifest.size()); |
| 98 | 110 |
| 99 sql::Transaction transaction(db_); | 111 sql::Transaction transaction(db_); |
| 100 if (!transaction.Begin()) | 112 if (!transaction.Begin()) |
| 101 return false; | 113 return false; |
| 102 | 114 |
| 103 sql::Statement s1(db_->GetUniqueStatement( | 115 sql::Statement s1(db_->GetUniqueStatement( |
| 104 "DELETE FROM web_app_manifest_section WHERE id=? ")); | 116 "DELETE FROM web_app_manifest_section WHERE id=? ")); |
| 105 s1.BindString(0, manifest[0]->id); | 117 s1.BindString(0, manifest[0]->id); |
| 106 if (!s1.Run()) | 118 if (!s1.Run()) |
| 107 return false; | 119 return false; |
| 108 | 120 |
| 109 sql::Statement s2( | 121 sql::Statement s2( |
| 110 db_->GetUniqueStatement("INSERT INTO web_app_manifest_section " | 122 db_->GetUniqueStatement("INSERT INTO web_app_manifest_section " |
| 111 "(id, min_version, fingerprints) " | 123 "(expire_date, id, min_version, fingerprints) " |
| 112 "VALUES (?, ?, ?)")); | 124 "VALUES (?, ?, ?, ?)")); |
| 125 const time_t expire_date_in_seconds = |
| 126 base::Time::NowFromSystemTime().ToTimeT() + DATA_VALID_TIME_IN_SECONDS; |
| 113 for (const auto& section : manifest) { | 127 for (const auto& section : manifest) { |
| 114 DCHECK_EQ(manifest[0]->id, section->id); | 128 DCHECK_EQ(manifest[0]->id, section->id); |
| 115 int index = 0; | 129 int index = 0; |
| 130 s2.BindInt64(index++, expire_date_in_seconds); |
| 116 s2.BindString(index++, section->id); | 131 s2.BindString(index++, section->id); |
| 117 s2.BindInt64(index++, section->min_version); | 132 s2.BindInt64(index++, section->min_version); |
| 118 std::unique_ptr<std::vector<uint8_t>> serialized_fingerprints = | 133 std::unique_ptr<std::vector<uint8_t>> serialized_fingerprints = |
| 119 SerializeFingerPrints(section->fingerprints); | 134 SerializeFingerPrints(section->fingerprints); |
| 120 s2.BindBlob(index, serialized_fingerprints->data(), | 135 s2.BindBlob(index, serialized_fingerprints->data(), |
| 121 serialized_fingerprints->size()); | 136 serialized_fingerprints->size()); |
| 122 if (!s2.Run()) | 137 if (!s2.Run()) |
| 123 return false; | 138 return false; |
| 124 s2.Reset(true); | 139 s2.Reset(true); |
| 125 } | 140 } |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 158 break; | 173 break; |
| 159 } | 174 } |
| 160 | 175 |
| 161 manifest.emplace_back(std::move(section)); | 176 manifest.emplace_back(std::move(section)); |
| 162 } | 177 } |
| 163 | 178 |
| 164 return manifest; | 179 return manifest; |
| 165 } | 180 } |
| 166 | 181 |
| 167 } // namespace payments | 182 } // namespace payments |
| OLD | NEW |