| 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/payment_method_manifest_table.h" | 5 #include "components/payments/android/payment_method_manifest_table.h" |
| 6 | 6 |
| 7 #include <time.h> |
| 8 |
| 9 #include "base/time/time.h" |
| 7 #include "sql/statement.h" | 10 #include "sql/statement.h" |
| 8 #include "sql/transaction.h" | 11 #include "sql/transaction.h" |
| 9 | 12 |
| 10 namespace payments { | 13 namespace payments { |
| 11 namespace { | 14 namespace { |
| 15 // Data valid duration in seconds. |
| 16 const time_t DATA_VALID_TIME_IN_SECONDS = 90 * 24 * 60 * 60; |
| 17 |
| 12 WebDatabaseTable::TypeKey GetKey() { | 18 WebDatabaseTable::TypeKey GetKey() { |
| 13 // We just need a unique constant. Use the address of a static that | 19 // We just need a unique constant. Use the address of a static that |
| 14 // COMDAT folding won't touch in an optimizing linker. | 20 // COMDAT folding won't touch in an optimizing linker. |
| 15 static int table_key = 0; | 21 static int table_key = 0; |
| 16 return reinterpret_cast<void*>(&table_key); | 22 return reinterpret_cast<void*>(&table_key); |
| 17 } | 23 } |
| 18 } | 24 } |
| 19 | 25 |
| 20 PaymentMethodManifestTable::PaymentMethodManifestTable() {} | 26 PaymentMethodManifestTable::PaymentMethodManifestTable() {} |
| 21 | 27 |
| 22 PaymentMethodManifestTable::~PaymentMethodManifestTable() {} | 28 PaymentMethodManifestTable::~PaymentMethodManifestTable() {} |
| 23 | 29 |
| 24 PaymentMethodManifestTable* PaymentMethodManifestTable::FromWebDatabase( | 30 PaymentMethodManifestTable* PaymentMethodManifestTable::FromWebDatabase( |
| 25 WebDatabase* db) { | 31 WebDatabase* db) { |
| 26 return static_cast<PaymentMethodManifestTable*>(db->GetTable(GetKey())); | 32 return static_cast<PaymentMethodManifestTable*>(db->GetTable(GetKey())); |
| 27 } | 33 } |
| 28 | 34 |
| 29 WebDatabaseTable::TypeKey PaymentMethodManifestTable::GetTypeKey() const { | 35 WebDatabaseTable::TypeKey PaymentMethodManifestTable::GetTypeKey() const { |
| 30 return GetKey(); | 36 return GetKey(); |
| 31 } | 37 } |
| 32 | 38 |
| 33 bool PaymentMethodManifestTable::CreateTablesIfNecessary() { | 39 bool PaymentMethodManifestTable::CreateTablesIfNecessary() { |
| 34 if (!db_->Execute("CREATE TABLE IF NOT EXISTS payment_method_manifest ( " | 40 if (!db_->Execute("CREATE TABLE IF NOT EXISTS payment_method_manifest ( " |
| 41 "expire_date INTEGER NOT NULL DEFAULT 0, " |
| 35 "method_name VARCHAR, " | 42 "method_name VARCHAR, " |
| 36 "web_app_id VARCHAR) ")) { | 43 "web_app_id VARCHAR) ")) { |
| 37 NOTREACHED(); | 44 NOTREACHED(); |
| 38 return false; | 45 return false; |
| 39 } | 46 } |
| 40 | 47 |
| 41 return true; | 48 return true; |
| 42 } | 49 } |
| 43 | 50 |
| 44 bool PaymentMethodManifestTable::IsSyncable() { | 51 bool PaymentMethodManifestTable::IsSyncable() { |
| 45 return false; | 52 return false; |
| 46 } | 53 } |
| 47 | 54 |
| 48 bool PaymentMethodManifestTable::MigrateToVersion( | 55 bool PaymentMethodManifestTable::MigrateToVersion( |
| 49 int version, | 56 int version, |
| 50 bool* update_compatible_version) { | 57 bool* update_compatible_version) { |
| 51 return true; | 58 return true; |
| 52 } | 59 } |
| 53 | 60 |
| 61 void PaymentMethodManifestTable::RemoveExpiredData() { |
| 62 const time_t now_date_in_seconds = base::Time::NowFromSystemTime().ToTimeT(); |
| 63 sql::Statement s(db_->GetUniqueStatement( |
| 64 "DELETE FROM payment_method_manifest WHERE expire_date < ? ")); |
| 65 s.BindInt64(0, now_date_in_seconds); |
| 66 s.Run(); |
| 67 } |
| 68 |
| 54 bool PaymentMethodManifestTable::AddManifest( | 69 bool PaymentMethodManifestTable::AddManifest( |
| 55 const std::string& payment_method, | 70 const std::string& payment_method, |
| 56 const std::vector<std::string>& web_app_ids) { | 71 const std::vector<std::string>& web_app_ids) { |
| 57 sql::Transaction transaction(db_); | 72 sql::Transaction transaction(db_); |
| 58 if (!transaction.Begin()) | 73 if (!transaction.Begin()) |
| 59 return false; | 74 return false; |
| 60 | 75 |
| 61 sql::Statement s1(db_->GetUniqueStatement( | 76 sql::Statement s1(db_->GetUniqueStatement( |
| 62 "DELETE FROM payment_method_manifest WHERE method_name=? ")); | 77 "DELETE FROM payment_method_manifest WHERE method_name=? ")); |
| 63 s1.BindString(0, payment_method); | 78 s1.BindString(0, payment_method); |
| 64 if (!s1.Run()) | 79 if (!s1.Run()) |
| 65 return false; | 80 return false; |
| 66 | 81 |
| 67 sql::Statement s2( | 82 sql::Statement s2( |
| 68 db_->GetUniqueStatement("INSERT INTO payment_method_manifest " | 83 db_->GetUniqueStatement("INSERT INTO payment_method_manifest " |
| 69 "(method_name, web_app_id) " | 84 "(expire_date, method_name, web_app_id) " |
| 70 "VALUES (?, ?) ")); | 85 "VALUES (?, ?, ?) ")); |
| 86 const time_t expire_date_in_seconds = |
| 87 base::Time::NowFromSystemTime().ToTimeT() + DATA_VALID_TIME_IN_SECONDS; |
| 71 for (const auto& id : web_app_ids) { | 88 for (const auto& id : web_app_ids) { |
| 72 int index = 0; | 89 int index = 0; |
| 90 s2.BindInt64(index++, expire_date_in_seconds); |
| 73 s2.BindString(index++, payment_method); | 91 s2.BindString(index++, payment_method); |
| 74 s2.BindString(index, id); | 92 s2.BindString(index, id); |
| 75 if (!s2.Run()) | 93 if (!s2.Run()) |
| 76 return false; | 94 return false; |
| 77 s2.Reset(true); | 95 s2.Reset(true); |
| 78 } | 96 } |
| 79 | 97 |
| 80 if (!transaction.Commit()) | 98 if (!transaction.Commit()) |
| 81 return false; | 99 return false; |
| 82 | 100 |
| (...skipping 10 matching lines...) Expand all Loading... |
| 93 s.BindString(0, payment_method); | 111 s.BindString(0, payment_method); |
| 94 | 112 |
| 95 while (s.Step()) { | 113 while (s.Step()) { |
| 96 web_app_ids.emplace_back(s.ColumnString(0)); | 114 web_app_ids.emplace_back(s.ColumnString(0)); |
| 97 } | 115 } |
| 98 | 116 |
| 99 return web_app_ids; | 117 return web_app_ids; |
| 100 } | 118 } |
| 101 | 119 |
| 102 } // namespace payments | 120 } // namespace payments |
| OLD | NEW |