Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(161)

Side by Side Diff: components/precache/core/precache_session_tables.cc

Issue 1961153003: Add pause/resume functionality to precache (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 4 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright 2016 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "components/precache/core/precache_session_tables.h"
6
7 #include <string>
8
9 #include "base/logging.h"
10 #include "base/time/time.h"
11 #include "sql/connection.h"
12 #include "sql/statement.h"
13
14 using sql::Statement;
15
16 namespace {
17
18 // Returns the spec of the given URL.
19 std::string GetKey(const GURL& url) {
20 return url.spec();
21 }
22
23 } // namespace
24
25 namespace precache {
26
27 PrecacheSessionTables::PrecacheSessionTables() : db_(nullptr) {}
28
29 PrecacheSessionTables::~PrecacheSessionTables() {}
30
31 bool PrecacheSessionTables::Init(sql::Connection* db) {
32 DCHECK(!db_); // Init must only be called once.
33 DCHECK(db); // The database connection must be non-NULL.
34 db_ = db;
35 return CreateTablesIfNonExistent();
36 }
37
38 void PrecacheSessionTables::GetURLs(std::list<GURL>* manifests,
39 std::list<GURL>* resources) {
40 manifests->clear();
41 resources->clear();
42 Statement statement(db_->GetCachedStatement(
43 SQL_FROM_HERE, "SELECT url, type FROM precache_task_urls"));
44 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.
45 GURL url = GURL(statement.ColumnString(0));
46 URLType type = static_cast<URLType>(statement.ColumnInt64(1));
47 if (type == MANIFEST)
48 manifests->push_back(url);
49 else if (type == RESOURCE)
50 resources->push_back(url);
51 else
52 NOTREACHED();
53 }
54 }
55
56 void PrecacheSessionTables::GetStatistics(int64_t* total_response_bytes,
57 int64_t* network_response_bytes,
58 int64_t* num_manifest_urls_to_fetch,
59 base::TimeTicks* start_time) {
60 *total_response_bytes = 0;
61 *network_response_bytes = 0;
62 *num_manifest_urls_to_fetch = 0;
63 *start_time = base::TimeTicks();
64 Statement statement(db_->GetCachedStatement(
65 SQL_FROM_HERE, "SELECT type, value from precache_stats"));
66 while (statement.Step()) {
67 int type = statement.ColumnInt64(0);
68 int64_t value = statement.ColumnInt64(1);
69 switch (type) {
70 case TOTAL_RESPONSE_BYTES:
71 *total_response_bytes = value;
72 break;
73 case NETWORK_RESPONSE_BYTES:
74 *network_response_bytes = value;
75 break;
76 case NUM_MANIFEST_URLS_TO_FETCH:
77 *num_manifest_urls_to_fetch = value;
78 break;
79 case START_TIME:
80 *start_time = base::TimeTicks::FromInternalValue(value);
81 break;
82 default:
83 NOTREACHED();
84 }
85 }
86 }
87
88 void PrecacheSessionTables::StoreURLs(const std::list<GURL>& manifests,
89 const std::list<GURL>& resources) {
90 ClearURLs();
91 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.
92 StoreURL(manifest, MANIFEST);
93 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.
94 StoreURL(resource, RESOURCE);
95 }
96
97 void PrecacheSessionTables::StoreStatistics(
98 int64_t total_response_bytes,
99 int64_t network_response_bytes,
100 int64_t num_manifest_urls_to_fetch,
101 const base::TimeTicks& start_time) {
102 StoreStatistic(TOTAL_RESPONSE_BYTES, total_response_bytes);
103 StoreStatistic(NETWORK_RESPONSE_BYTES, network_response_bytes);
104 StoreStatistic(NUM_MANIFEST_URLS_TO_FETCH, num_manifest_urls_to_fetch);
105 StoreStatistic(START_TIME, start_time.ToInternalValue());
106 }
107
108 void PrecacheSessionTables::StoreURL(const GURL& url, URLType type) {
109 // TODO(bengr): Store start times and priorities per URL.
110 Statement statement(db_->GetCachedStatement(
111 SQL_FROM_HERE,
112 "INSERT OR REPLACE INTO precache_task_urls (url, time, priority, type) "
113 "VALUES(?,?,?,?)"));
114 statement.BindString(0, GetKey(url));
115 statement.BindInt64(1, 0);
116 statement.BindInt64(2, 0);
117 statement.BindInt(3, static_cast<int>(type));
118 statement.Run();
119 }
120
121 void PrecacheSessionTables::StoreStatistic(StatType type, int64_t value) {
122 Statement statement(db_->GetCachedStatement(
123 SQL_FROM_HERE,
124 "INSERT OR REPLACE INTO precache_stats (type, value) VALUES(?,?)"));
125 statement.BindInt(0, static_cast<int>(type));
126 statement.BindInt64(1, value);
127 statement.Run();
128 }
129
130 void PrecacheSessionTables::ClearURLs() {
131 Statement statement(
132 db_->GetCachedStatement(SQL_FROM_HERE, "DELETE FROM precache_task_urls"));
133 statement.Run();
134 }
135
136 void PrecacheSessionTables::ClearStatistics() {
137 Statement statement(
138 db_->GetCachedStatement(SQL_FROM_HERE, "DELETE FROM precache_stats"));
139 statement.Run();
140 }
141
142 bool PrecacheSessionTables::CreateTablesIfNonExistent() {
143 if (!db_->Execute(
144 "CREATE TABLE IF NOT EXISTS precache_task_urls ("
145 "url TEXT PRIMARY KEY, "
146 "time INTEGER, priority INTEGER, type INTEGER)")) {
147 return false;
148 }
149 return db_->Execute(
150 "CREATE TABLE IF NOT EXISTS precache_stats (type INTEGER PRIMARY_KEY, "
151 "value INTEGER)");
152 }
153
154 } // namespace precache
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698