Chromium Code Reviews| Index: components/precache/core/precache_session_tables.cc |
| diff --git a/components/precache/core/precache_session_tables.cc b/components/precache/core/precache_session_tables.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..7f2f5efb69c5de6eb464abbd2c81733137e95d8b |
| --- /dev/null |
| +++ b/components/precache/core/precache_session_tables.cc |
| @@ -0,0 +1,154 @@ |
| +// Copyright 2016 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "components/precache/core/precache_session_tables.h" |
| + |
| +#include <string> |
| + |
| +#include "base/logging.h" |
| +#include "base/time/time.h" |
| +#include "sql/connection.h" |
| +#include "sql/statement.h" |
| + |
| +using sql::Statement; |
| + |
| +namespace { |
| + |
| +// Returns the spec of the given URL. |
| +std::string GetKey(const GURL& url) { |
| + return url.spec(); |
| +} |
| + |
| +} // namespace |
| + |
| +namespace precache { |
| + |
| +PrecacheSessionTables::PrecacheSessionTables() : db_(nullptr) {} |
| + |
| +PrecacheSessionTables::~PrecacheSessionTables() {} |
| + |
| +bool PrecacheSessionTables::Init(sql::Connection* db) { |
| + DCHECK(!db_); // Init must only be called once. |
| + DCHECK(db); // The database connection must be non-NULL. |
| + db_ = db; |
| + return CreateTablesIfNonExistent(); |
| +} |
| + |
| +void PrecacheSessionTables::GetURLs(std::list<GURL>* manifests, |
| + std::list<GURL>* resources) { |
| + manifests->clear(); |
| + resources->clear(); |
| + Statement statement(db_->GetCachedStatement( |
| + SQL_FROM_HERE, "SELECT url, type FROM precache_task_urls")); |
| + while (statement.Step()) { |
|
sclittle
2016/05/10 00:01:27
How large can the list of URLs get? e.g. 50? 500?
bengr
2016/05/19 01:25:44
Covered in tests.
|
| + GURL url = GURL(statement.ColumnString(0)); |
| + URLType type = static_cast<URLType>(statement.ColumnInt64(1)); |
| + if (type == MANIFEST) |
| + manifests->push_back(url); |
| + else if (type == RESOURCE) |
| + resources->push_back(url); |
| + else |
| + NOTREACHED(); |
| + } |
| +} |
| + |
| +void PrecacheSessionTables::GetStatistics(int64_t* total_response_bytes, |
| + int64_t* network_response_bytes, |
| + int64_t* num_manifest_urls_to_fetch, |
| + base::TimeTicks* start_time) { |
| + *total_response_bytes = 0; |
| + *network_response_bytes = 0; |
| + *num_manifest_urls_to_fetch = 0; |
| + *start_time = base::TimeTicks(); |
| + Statement statement(db_->GetCachedStatement( |
| + SQL_FROM_HERE, "SELECT type, value from precache_stats")); |
| + while (statement.Step()) { |
| + int type = statement.ColumnInt64(0); |
| + int64_t value = statement.ColumnInt64(1); |
| + switch (type) { |
| + case TOTAL_RESPONSE_BYTES: |
| + *total_response_bytes = value; |
| + break; |
| + case NETWORK_RESPONSE_BYTES: |
| + *network_response_bytes = value; |
| + break; |
| + case NUM_MANIFEST_URLS_TO_FETCH: |
| + *num_manifest_urls_to_fetch = value; |
| + break; |
| + case START_TIME: |
| + *start_time = base::TimeTicks::FromInternalValue(value); |
| + break; |
| + default: |
| + NOTREACHED(); |
| + } |
| + } |
| +} |
| + |
| +void PrecacheSessionTables::StoreURLs(const std::list<GURL>& manifests, |
| + const std::list<GURL>& resources) { |
| + ClearURLs(); |
| + for (auto manifest : manifests) |
|
sclittle
2016/05/10 00:01:27
Use "const auto&" or "const GURL&" here to avoid c
bengr
2016/05/19 01:25:44
Done.
|
| + StoreURL(manifest, MANIFEST); |
| + for (auto resource : resources) |
|
sclittle
2016/05/10 00:01:27
Use "const auto&" or "const GURL&" here to avoid c
bengr
2016/05/19 01:25:44
Done.
|
| + StoreURL(resource, RESOURCE); |
| +} |
| + |
| +void PrecacheSessionTables::StoreStatistics( |
| + int64_t total_response_bytes, |
| + int64_t network_response_bytes, |
| + int64_t num_manifest_urls_to_fetch, |
| + const base::TimeTicks& start_time) { |
| + StoreStatistic(TOTAL_RESPONSE_BYTES, total_response_bytes); |
| + StoreStatistic(NETWORK_RESPONSE_BYTES, network_response_bytes); |
| + StoreStatistic(NUM_MANIFEST_URLS_TO_FETCH, num_manifest_urls_to_fetch); |
| + StoreStatistic(START_TIME, start_time.ToInternalValue()); |
| +} |
| + |
| +void PrecacheSessionTables::StoreURL(const GURL& url, URLType type) { |
| + // TODO(bengr): Store start times and priorities per URL. |
| + Statement statement(db_->GetCachedStatement( |
| + SQL_FROM_HERE, |
| + "INSERT OR REPLACE INTO precache_task_urls (url, time, priority, type) " |
| + "VALUES(?,?,?,?)")); |
| + statement.BindString(0, GetKey(url)); |
| + statement.BindInt64(1, 0); |
| + statement.BindInt64(2, 0); |
| + statement.BindInt(3, static_cast<int>(type)); |
| + statement.Run(); |
| +} |
| + |
| +void PrecacheSessionTables::StoreStatistic(StatType type, int64_t value) { |
| + Statement statement(db_->GetCachedStatement( |
| + SQL_FROM_HERE, |
| + "INSERT OR REPLACE INTO precache_stats (type, value) VALUES(?,?)")); |
| + statement.BindInt(0, static_cast<int>(type)); |
| + statement.BindInt64(1, value); |
| + statement.Run(); |
| +} |
| + |
| +void PrecacheSessionTables::ClearURLs() { |
| + Statement statement( |
| + db_->GetCachedStatement(SQL_FROM_HERE, "DELETE FROM precache_task_urls")); |
| + statement.Run(); |
| +} |
| + |
| +void PrecacheSessionTables::ClearStatistics() { |
| + Statement statement( |
| + db_->GetCachedStatement(SQL_FROM_HERE, "DELETE FROM precache_stats")); |
| + statement.Run(); |
| +} |
| + |
| +bool PrecacheSessionTables::CreateTablesIfNonExistent() { |
| + if (!db_->Execute( |
| + "CREATE TABLE IF NOT EXISTS precache_task_urls (" |
| + "url TEXT PRIMARY KEY, " |
| + "time INTEGER, priority INTEGER, type INTEGER)")) { |
| + return false; |
| + } |
| + return db_->Execute( |
| + "CREATE TABLE IF NOT EXISTS precache_stats (type INTEGER PRIMARY_KEY, " |
| + "value INTEGER)"); |
| +} |
| + |
| +} // namespace precache |