Commit 69a1f75e authored by gogerald's avatar gogerald Committed by Commit bot

Set cached manifest expiration date

The expiration days is set to 90 days.
Note that the expired cached manifest will be refreshed by downloading it online.

BUG=708508

Review-Url: https://codereview.chromium.org/2845753003
Cr-Commit-Position: refs/heads/master@{#467790}
parent 9a822f4b
...@@ -72,6 +72,7 @@ std::unique_ptr<WDTypedResult> ...@@ -72,6 +72,7 @@ std::unique_ptr<WDTypedResult>
PaymentManifestWebDataService::GetPaymentWebAppManifestImpl( PaymentManifestWebDataService::GetPaymentWebAppManifestImpl(
const std::string& web_app, const std::string& web_app,
WebDatabase* db) { WebDatabase* db) {
RemoveExpiredData(db);
return base::MakeUnique< return base::MakeUnique<
WDResult<std::vector<mojom::WebAppManifestSectionPtr>>>( WDResult<std::vector<mojom::WebAppManifestSectionPtr>>>(
PAYMENT_WEB_APP_MANIFEST, PAYMENT_WEB_APP_MANIFEST,
...@@ -94,10 +95,16 @@ std::unique_ptr<WDTypedResult> ...@@ -94,10 +95,16 @@ std::unique_ptr<WDTypedResult>
PaymentManifestWebDataService::GetPaymentMethodManifestImpl( PaymentManifestWebDataService::GetPaymentMethodManifestImpl(
const std::string& payment_method, const std::string& payment_method,
WebDatabase* db) { WebDatabase* db) {
RemoveExpiredData(db);
return base::MakeUnique<WDResult<std::vector<std::string>>>( return base::MakeUnique<WDResult<std::vector<std::string>>>(
PAYMENT_METHOD_MANIFEST, PAYMENT_METHOD_MANIFEST,
PaymentMethodManifestTable::FromWebDatabase(db)->GetManifest( PaymentMethodManifestTable::FromWebDatabase(db)->GetManifest(
payment_method)); payment_method));
} }
void PaymentManifestWebDataService::RemoveExpiredData(WebDatabase* db) {
PaymentMethodManifestTable::FromWebDatabase(db)->RemoveExpiredData();
WebAppManifestSectionTable::FromWebDatabase(db)->RemoveExpiredData();
}
} // namespace payments } // namespace payments
\ No newline at end of file
...@@ -45,6 +45,8 @@ class PaymentManifestWebDataService : public WebDataServiceBase { ...@@ -45,6 +45,8 @@ class PaymentManifestWebDataService : public WebDataServiceBase {
private: private:
~PaymentManifestWebDataService() override; ~PaymentManifestWebDataService() override;
void RemoveExpiredData(WebDatabase* db);
WebDatabase::State AddPaymentWebAppManifestImpl( WebDatabase::State AddPaymentWebAppManifestImpl(
const std::vector<mojom::WebAppManifestSectionPtr>& manifest, const std::vector<mojom::WebAppManifestSectionPtr>& manifest,
WebDatabase* db); WebDatabase* db);
......
...@@ -4,11 +4,17 @@ ...@@ -4,11 +4,17 @@
#include "components/payments/android/payment_method_manifest_table.h" #include "components/payments/android/payment_method_manifest_table.h"
#include <time.h>
#include "base/time/time.h"
#include "sql/statement.h" #include "sql/statement.h"
#include "sql/transaction.h" #include "sql/transaction.h"
namespace payments { namespace payments {
namespace { namespace {
// Data valid duration in seconds.
const time_t DATA_VALID_TIME_IN_SECONDS = 90 * 24 * 60 * 60;
WebDatabaseTable::TypeKey GetKey() { WebDatabaseTable::TypeKey GetKey() {
// We just need a unique constant. Use the address of a static that // We just need a unique constant. Use the address of a static that
// COMDAT folding won't touch in an optimizing linker. // COMDAT folding won't touch in an optimizing linker.
...@@ -32,6 +38,7 @@ WebDatabaseTable::TypeKey PaymentMethodManifestTable::GetTypeKey() const { ...@@ -32,6 +38,7 @@ WebDatabaseTable::TypeKey PaymentMethodManifestTable::GetTypeKey() const {
bool PaymentMethodManifestTable::CreateTablesIfNecessary() { bool PaymentMethodManifestTable::CreateTablesIfNecessary() {
if (!db_->Execute("CREATE TABLE IF NOT EXISTS payment_method_manifest ( " if (!db_->Execute("CREATE TABLE IF NOT EXISTS payment_method_manifest ( "
"expire_date INTEGER NOT NULL DEFAULT 0, "
"method_name VARCHAR, " "method_name VARCHAR, "
"web_app_id VARCHAR) ")) { "web_app_id VARCHAR) ")) {
NOTREACHED(); NOTREACHED();
...@@ -51,6 +58,14 @@ bool PaymentMethodManifestTable::MigrateToVersion( ...@@ -51,6 +58,14 @@ bool PaymentMethodManifestTable::MigrateToVersion(
return true; return true;
} }
void PaymentMethodManifestTable::RemoveExpiredData() {
const time_t now_date_in_seconds = base::Time::NowFromSystemTime().ToTimeT();
sql::Statement s(db_->GetUniqueStatement(
"DELETE FROM payment_method_manifest WHERE expire_date < ? "));
s.BindInt64(0, now_date_in_seconds);
s.Run();
}
bool PaymentMethodManifestTable::AddManifest( bool PaymentMethodManifestTable::AddManifest(
const std::string& payment_method, const std::string& payment_method,
const std::vector<std::string>& web_app_ids) { const std::vector<std::string>& web_app_ids) {
...@@ -66,10 +81,13 @@ bool PaymentMethodManifestTable::AddManifest( ...@@ -66,10 +81,13 @@ bool PaymentMethodManifestTable::AddManifest(
sql::Statement s2( sql::Statement s2(
db_->GetUniqueStatement("INSERT INTO payment_method_manifest " db_->GetUniqueStatement("INSERT INTO payment_method_manifest "
"(method_name, web_app_id) " "(expire_date, method_name, web_app_id) "
"VALUES (?, ?) ")); "VALUES (?, ?, ?) "));
const time_t expire_date_in_seconds =
base::Time::NowFromSystemTime().ToTimeT() + DATA_VALID_TIME_IN_SECONDS;
for (const auto& id : web_app_ids) { for (const auto& id : web_app_ids) {
int index = 0; int index = 0;
s2.BindInt64(index++, expire_date_in_seconds);
s2.BindString(index++, payment_method); s2.BindString(index++, payment_method);
s2.BindString(index, id); s2.BindString(index, id);
if (!s2.Run()) if (!s2.Run())
......
...@@ -20,6 +20,9 @@ namespace payments { ...@@ -20,6 +20,9 @@ namespace payments {
// supported web app in this payment method manifest. // supported web app in this payment method manifest.
// Note that a payment method manifest might contain // Note that a payment method manifest might contain
// multiple supported web apps ids. // multiple supported web apps ids.
//
// expire_date The expire date in seconds from 1601-01-01 00:00:00
// UTC.
// method_name The method name. // method_name The method name.
// web_app_id The supported web app id. // web_app_id The supported web app id.
// (WebAppManifestSection.id). // (WebAppManifestSection.id).
...@@ -38,6 +41,9 @@ class PaymentMethodManifestTable : public WebDatabaseTable { ...@@ -38,6 +41,9 @@ class PaymentMethodManifestTable : public WebDatabaseTable {
bool IsSyncable() override; bool IsSyncable() override;
bool MigrateToVersion(int version, bool* update_compatible_version) override; bool MigrateToVersion(int version, bool* update_compatible_version) override;
// Remove expired data.
void RemoveExpiredData();
// Adds |payment_method|'s manifest. |web_app_ids| contains supported web apps // Adds |payment_method|'s manifest. |web_app_ids| contains supported web apps
// ids. // ids.
bool AddManifest(const std::string& payment_method, bool AddManifest(const std::string& payment_method,
......
...@@ -5,14 +5,17 @@ ...@@ -5,14 +5,17 @@
#include "components/payments/android/web_app_manifest_section_table.h" #include "components/payments/android/web_app_manifest_section_table.h"
#include <stdint.h> #include <stdint.h>
#include <time.h>
#include <memory> #include <memory>
#include "base/time/time.h"
#include "sql/statement.h" #include "sql/statement.h"
#include "sql/transaction.h" #include "sql/transaction.h"
namespace payments { namespace payments {
namespace { namespace {
// Data valid duration in seconds.
const time_t DATA_VALID_TIME_IN_SECONDS = 90 * 24 * 60 * 60;
// Note that the fingerprint is calculated with SHA-256. // Note that the fingerprint is calculated with SHA-256.
const size_t kFingerPrintLength = 32; const size_t kFingerPrintLength = 32;
...@@ -72,6 +75,7 @@ WebDatabaseTable::TypeKey WebAppManifestSectionTable::GetTypeKey() const { ...@@ -72,6 +75,7 @@ WebDatabaseTable::TypeKey WebAppManifestSectionTable::GetTypeKey() const {
bool WebAppManifestSectionTable::CreateTablesIfNecessary() { bool WebAppManifestSectionTable::CreateTablesIfNecessary() {
if (!db_->Execute("CREATE TABLE IF NOT EXISTS web_app_manifest_section ( " if (!db_->Execute("CREATE TABLE IF NOT EXISTS web_app_manifest_section ( "
"expire_date INTEGER NOT NULL DEFAULT 0, "
"id VARCHAR, " "id VARCHAR, "
"min_version INTEGER NOT NULL DEFAULT 0, " "min_version INTEGER NOT NULL DEFAULT 0, "
"fingerprints BLOB) ")) { "fingerprints BLOB) ")) {
...@@ -92,6 +96,14 @@ bool WebAppManifestSectionTable::MigrateToVersion( ...@@ -92,6 +96,14 @@ bool WebAppManifestSectionTable::MigrateToVersion(
return true; return true;
} }
void WebAppManifestSectionTable::RemoveExpiredData() {
const time_t now_date_in_seconds = base::Time::NowFromSystemTime().ToTimeT();
sql::Statement s(db_->GetUniqueStatement(
"DELETE FROM web_app_manifest_section WHERE expire_date < ? "));
s.BindInt64(0, now_date_in_seconds);
s.Run();
}
bool WebAppManifestSectionTable::AddWebAppManifest( bool WebAppManifestSectionTable::AddWebAppManifest(
const std::vector<mojom::WebAppManifestSectionPtr>& manifest) { const std::vector<mojom::WebAppManifestSectionPtr>& manifest) {
DCHECK_LT(0U, manifest.size()); DCHECK_LT(0U, manifest.size());
...@@ -108,11 +120,14 @@ bool WebAppManifestSectionTable::AddWebAppManifest( ...@@ -108,11 +120,14 @@ bool WebAppManifestSectionTable::AddWebAppManifest(
sql::Statement s2( sql::Statement s2(
db_->GetUniqueStatement("INSERT INTO web_app_manifest_section " db_->GetUniqueStatement("INSERT INTO web_app_manifest_section "
"(id, min_version, fingerprints) " "(expire_date, id, min_version, fingerprints) "
"VALUES (?, ?, ?)")); "VALUES (?, ?, ?, ?)"));
const time_t expire_date_in_seconds =
base::Time::NowFromSystemTime().ToTimeT() + DATA_VALID_TIME_IN_SECONDS;
for (const auto& section : manifest) { for (const auto& section : manifest) {
DCHECK_EQ(manifest[0]->id, section->id); DCHECK_EQ(manifest[0]->id, section->id);
int index = 0; int index = 0;
s2.BindInt64(index++, expire_date_in_seconds);
s2.BindString(index++, section->id); s2.BindString(index++, section->id);
s2.BindInt64(index++, section->min_version); s2.BindInt64(index++, section->min_version);
std::unique_ptr<std::vector<uint8_t>> serialized_fingerprints = std::unique_ptr<std::vector<uint8_t>> serialized_fingerprints =
......
...@@ -21,6 +21,8 @@ namespace payments { ...@@ -21,6 +21,8 @@ namespace payments {
// web_app_manifest_section The table stores the contents in // web_app_manifest_section The table stores the contents in
// WebAppManifestSection. // WebAppManifestSection.
// //
// expire_date The data expire date in seconds from 1601-01-01
// 00:00:00 UTC.
// id The package name of the app. // id The package name of the app.
// min_version Minimum version number of the app. // min_version Minimum version number of the app.
// fingerprints The result of SHA256(signing certificate bytes) for // fingerprints The result of SHA256(signing certificate bytes) for
...@@ -40,6 +42,9 @@ class WebAppManifestSectionTable : public WebDatabaseTable { ...@@ -40,6 +42,9 @@ class WebAppManifestSectionTable : public WebDatabaseTable {
bool IsSyncable() override; bool IsSyncable() override;
bool MigrateToVersion(int version, bool* update_compatible_version) override; bool MigrateToVersion(int version, bool* update_compatible_version) override;
// Remove expired data.
void RemoveExpiredData();
// Adds the web app |manifest|. Note that the previous web app manifest will // Adds the web app |manifest|. Note that the previous web app manifest will
// be deleted. // be deleted.
bool AddWebAppManifest( bool AddWebAppManifest(
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment