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

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

Issue 2123813002: Report Precache.TimeSinceLastPrecache (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: MaybePostFlush Created 4 years, 5 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
1 // Copyright 2016 The Chromium Authors. All rights reserved. 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 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/precache/core/precache_session_table.h" 5 #include "components/precache/core/precache_session_table.h"
6 6
7 #include <string> 7 #include <string>
8 8
9 #include "base/logging.h" 9 #include "base/logging.h"
10 #include "base/time/time.h" 10 #include "base/time/time.h"
11 #include "components/precache/core/proto/timestamp.pb.h"
11 #include "components/precache/core/proto/unfinished_work.pb.h" 12 #include "components/precache/core/proto/unfinished_work.pb.h"
12 #include "sql/connection.h" 13 #include "sql/connection.h"
13 #include "sql/statement.h" 14 #include "sql/statement.h"
14 15
15 using sql::Statement; 16 using sql::Statement;
16 17
17 namespace precache { 18 namespace precache {
18 19
19 PrecacheSessionTable::PrecacheSessionTable() : db_(nullptr) {} 20 PrecacheSessionTable::PrecacheSessionTable() : db_(nullptr) {}
20 21
21 PrecacheSessionTable::~PrecacheSessionTable() {} 22 PrecacheSessionTable::~PrecacheSessionTable() {}
22 23
23 bool PrecacheSessionTable::Init(sql::Connection* db) { 24 bool PrecacheSessionTable::Init(sql::Connection* db) {
24 DCHECK(!db_); // Init must only be called once. 25 DCHECK(!db_); // Init must only be called once.
25 DCHECK(db); // The database connection must be non-NULL. 26 DCHECK(db); // The database connection must be non-NULL.
26 db_ = db; 27 db_ = db;
27 return CreateTableIfNonExistent(); 28 return CreateTableIfNonExistent();
28 } 29 }
29 30
31 void PrecacheSessionTable::SetLastPrecacheTimestamp(const base::Time& time) {
Raj 2016/07/05 19:46:28 The previous precache task start time is stored in
jamartin 2016/07/06 14:14:14 What would be the benefit? If it is not clear, I'd
32 Timestamp timestamp;
33 timestamp.set_seconds(time.ToDoubleT());
Raj 2016/07/05 19:46:28 There seems to be a conversion here ? static_cast<
jamartin 2016/07/06 14:14:14 Good catch. I got lucky and it was working OK bef
34 Statement statement(db_->GetCachedStatement(
35 SQL_FROM_HERE,
36 "INSERT OR REPLACE INTO precache_session (type, value) VALUES(?,?)"));
37 statement.BindInt(0, static_cast<int>(LAST_PRECACHE_TIMESTAMP));
38 statement.BindString(1, timestamp.SerializeAsString());
Raj 2016/07/05 19:46:28 nit: Just wondering what is the need for timestamp
jamartin 2016/07/06 14:14:14 I had at least the following alternatives: a) Cre
39 statement.Run();
40 }
41
42 base::Time PrecacheSessionTable::GetLastPrecacheTimestamp() {
43 Statement statement(db_->GetCachedStatement(
44 SQL_FROM_HERE, "SELECT value from precache_session where type=?"));
45 statement.BindInt(0, static_cast<int>(LAST_PRECACHE_TIMESTAMP));
46 Timestamp timestamp;
47 if (statement.Step())
48 timestamp.ParseFromString(statement.ColumnString(0));
49 return base::Time::FromDoubleT(timestamp.seconds());
50 }
51
52 void PrecacheSessionTable::DeleteLastPrecacheTimestamp() {
53 Statement statement(db_->GetCachedStatement(
54 SQL_FROM_HERE, "DELETE FROM precache_session where type=?"));
55 statement.BindInt(0, static_cast<int>(LAST_PRECACHE_TIMESTAMP));
56 statement.Run();
57 }
58
30 // Store unfinished work. 59 // Store unfinished work.
31 void PrecacheSessionTable::SaveUnfinishedWork( 60 void PrecacheSessionTable::SaveUnfinishedWork(
32 std::unique_ptr<PrecacheUnfinishedWork> unfinished_work) { 61 std::unique_ptr<PrecacheUnfinishedWork> unfinished_work) {
33 Statement statement(db_->GetCachedStatement( 62 Statement statement(db_->GetCachedStatement(
34 SQL_FROM_HERE, 63 SQL_FROM_HERE,
35 "INSERT OR REPLACE INTO precache_session (type, value) VALUES(?,?)")); 64 "INSERT OR REPLACE INTO precache_session (type, value) VALUES(?,?)"));
36 statement.BindInt(0, static_cast<int>(UNFINISHED_WORK)); 65 statement.BindInt(0, static_cast<int>(UNFINISHED_WORK));
37 statement.BindString(1, unfinished_work->SerializeAsString()); 66 statement.BindString(1, unfinished_work->SerializeAsString());
38 statement.Run(); 67 statement.Run();
39 } 68 }
(...skipping 21 matching lines...) Expand all
61 statement.Run(); 90 statement.Run();
62 } 91 }
63 92
64 bool PrecacheSessionTable::CreateTableIfNonExistent() { 93 bool PrecacheSessionTable::CreateTableIfNonExistent() {
65 return db_->Execute( 94 return db_->Execute(
66 "CREATE TABLE IF NOT EXISTS precache_session (type INTEGER PRIMARY KEY, " 95 "CREATE TABLE IF NOT EXISTS precache_session (type INTEGER PRIMARY KEY, "
67 "value STRING)"); 96 "value STRING)");
68 } 97 }
69 98
70 } // namespace precache 99 } // namespace precache
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698