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