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 "base/time/time.h" | |
7 #include "sql/statement.h" | 8 #include "sql/statement.h" |
8 #include "sql/transaction.h" | 9 #include "sql/transaction.h" |
9 | 10 |
10 namespace payments { | 11 namespace payments { |
11 namespace { | 12 namespace { |
13 // Data valid time in seconds. | |
14 const time_t DATA_VALID_TIME_IN_SECONDS = 90 * 24 * 60; | |
please use gerrit instead
2017/04/27 15:45:17
That's 1.5 days. You're missing an extra "* 60".
please use gerrit instead
2017/04/27 15:45:18
#include <time.h>
gogerald1
2017/04/27 18:08:39
Done.
gogerald1
2017/04/27 18:08:39
Done. Good catch
| |
15 | |
12 WebDatabaseTable::TypeKey GetKey() { | 16 WebDatabaseTable::TypeKey GetKey() { |
13 // We just need a unique constant. Use the address of a static that | 17 // We just need a unique constant. Use the address of a static that |
14 // COMDAT folding won't touch in an optimizing linker. | 18 // COMDAT folding won't touch in an optimizing linker. |
15 static int table_key = 0; | 19 static int table_key = 0; |
16 return reinterpret_cast<void*>(&table_key); | 20 return reinterpret_cast<void*>(&table_key); |
17 } | 21 } |
18 } | 22 } |
19 | 23 |
20 PaymentMethodManifestTable::PaymentMethodManifestTable() {} | 24 PaymentMethodManifestTable::PaymentMethodManifestTable() {} |
21 | 25 |
22 PaymentMethodManifestTable::~PaymentMethodManifestTable() {} | 26 PaymentMethodManifestTable::~PaymentMethodManifestTable() {} |
23 | 27 |
24 PaymentMethodManifestTable* PaymentMethodManifestTable::FromWebDatabase( | 28 PaymentMethodManifestTable* PaymentMethodManifestTable::FromWebDatabase( |
25 WebDatabase* db) { | 29 WebDatabase* db) { |
26 return static_cast<PaymentMethodManifestTable*>(db->GetTable(GetKey())); | 30 return static_cast<PaymentMethodManifestTable*>(db->GetTable(GetKey())); |
27 } | 31 } |
28 | 32 |
29 WebDatabaseTable::TypeKey PaymentMethodManifestTable::GetTypeKey() const { | 33 WebDatabaseTable::TypeKey PaymentMethodManifestTable::GetTypeKey() const { |
30 return GetKey(); | 34 return GetKey(); |
31 } | 35 } |
32 | 36 |
33 bool PaymentMethodManifestTable::CreateTablesIfNecessary() { | 37 bool PaymentMethodManifestTable::CreateTablesIfNecessary() { |
34 if (!db_->Execute("CREATE TABLE IF NOT EXISTS payment_method_manifest ( " | 38 if (!db_->Execute("CREATE TABLE IF NOT EXISTS payment_method_manifest ( " |
39 "expire_date INTEGER NOT NULL DEFAULT 0, " | |
35 "method_name VARCHAR, " | 40 "method_name VARCHAR, " |
36 "web_app_id VARCHAR) ")) { | 41 "web_app_id VARCHAR) ")) { |
37 NOTREACHED(); | 42 NOTREACHED(); |
38 return false; | 43 return false; |
39 } | 44 } |
40 | 45 |
41 return true; | 46 return true; |
42 } | 47 } |
43 | 48 |
44 bool PaymentMethodManifestTable::IsSyncable() { | 49 bool PaymentMethodManifestTable::IsSyncable() { |
(...skipping 14 matching lines...) Expand all Loading... | |
59 return false; | 64 return false; |
60 | 65 |
61 sql::Statement s1(db_->GetUniqueStatement( | 66 sql::Statement s1(db_->GetUniqueStatement( |
62 "DELETE FROM payment_method_manifest WHERE method_name=? ")); | 67 "DELETE FROM payment_method_manifest WHERE method_name=? ")); |
63 s1.BindString(0, payment_method); | 68 s1.BindString(0, payment_method); |
64 if (!s1.Run()) | 69 if (!s1.Run()) |
65 return false; | 70 return false; |
66 | 71 |
67 sql::Statement s2( | 72 sql::Statement s2( |
68 db_->GetUniqueStatement("INSERT INTO payment_method_manifest " | 73 db_->GetUniqueStatement("INSERT INTO payment_method_manifest " |
69 "(method_name, web_app_id) " | 74 "(expire_date, method_name, web_app_id) " |
70 "VALUES (?, ?) ")); | 75 "VALUES (?, ?, ?) ")); |
76 const time_t expire_date_in_seconds = | |
77 base::Time::Now().ToTimeT() + DATA_VALID_TIME_IN_SECONDS; | |
71 for (const auto& id : web_app_ids) { | 78 for (const auto& id : web_app_ids) { |
72 int index = 0; | 79 int index = 0; |
80 s2.BindInt64(index++, expire_date_in_seconds); | |
73 s2.BindString(index++, payment_method); | 81 s2.BindString(index++, payment_method); |
74 s2.BindString(index, id); | 82 s2.BindString(index, id); |
75 if (!s2.Run()) | 83 if (!s2.Run()) |
76 return false; | 84 return false; |
77 s2.Reset(true); | 85 s2.Reset(true); |
78 } | 86 } |
79 | 87 |
80 if (!transaction.Commit()) | 88 if (!transaction.Commit()) |
81 return false; | 89 return false; |
82 | 90 |
83 return true; | 91 return true; |
84 } | 92 } |
85 | 93 |
86 std::vector<std::string> PaymentMethodManifestTable::GetManifest( | 94 std::vector<std::string> PaymentMethodManifestTable::GetManifest( |
87 const std::string& payment_method) { | 95 const std::string& payment_method) { |
88 std::vector<std::string> web_app_ids; | 96 std::vector<std::string> web_app_ids; |
89 sql::Statement s( | 97 sql::Statement s( |
90 db_->GetUniqueStatement("SELECT web_app_id " | 98 db_->GetUniqueStatement("SELECT expire_date, web_app_id " |
91 "FROM payment_method_manifest " | 99 "FROM payment_method_manifest " |
92 "WHERE method_name=?")); | 100 "WHERE method_name=?")); |
93 s.BindString(0, payment_method); | 101 s.BindString(0, payment_method); |
94 | 102 |
103 const time_t now_date_in_seconds = base::Time::Now().ToTimeT(); | |
95 while (s.Step()) { | 104 while (s.Step()) { |
96 web_app_ids.emplace_back(s.ColumnString(0)); | 105 int index = 0; |
106 if (now_date_in_seconds > s.ColumnInt64(index++)) { | |
107 web_app_ids.clear(); | |
please use gerrit instead
2017/04/27 15:45:18
You're leaving the stale data in the table, which
gogerald1
2017/04/27 18:08:39
Thought about this, but we have two tables, we mig
| |
108 break; | |
109 } | |
110 web_app_ids.emplace_back(s.ColumnString(index)); | |
97 } | 111 } |
98 | 112 |
99 return web_app_ids; | 113 return web_app_ids; |
100 } | 114 } |
101 | 115 |
102 } // namespace payments | 116 } // namespace payments |
OLD | NEW |