OLD | NEW |
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 <stdint.h> |
7 #include <string> | 8 #include <string> |
8 | 9 |
9 #include "base/logging.h" | 10 #include "base/logging.h" |
10 #include "base/time/time.h" | 11 #include "base/time/time.h" |
| 12 #include "components/precache/core/proto/timestamp.pb.h" |
11 #include "components/precache/core/proto/unfinished_work.pb.h" | 13 #include "components/precache/core/proto/unfinished_work.pb.h" |
12 #include "sql/connection.h" | 14 #include "sql/connection.h" |
13 #include "sql/statement.h" | 15 #include "sql/statement.h" |
14 | 16 |
15 using sql::Statement; | 17 using sql::Statement; |
16 | 18 |
17 namespace precache { | 19 namespace precache { |
18 | 20 |
19 PrecacheSessionTable::PrecacheSessionTable() : db_(nullptr) {} | 21 PrecacheSessionTable::PrecacheSessionTable() : db_(nullptr) {} |
20 | 22 |
21 PrecacheSessionTable::~PrecacheSessionTable() {} | 23 PrecacheSessionTable::~PrecacheSessionTable() {} |
22 | 24 |
23 bool PrecacheSessionTable::Init(sql::Connection* db) { | 25 bool PrecacheSessionTable::Init(sql::Connection* db) { |
24 DCHECK(!db_); // Init must only be called once. | 26 DCHECK(!db_); // Init must only be called once. |
25 DCHECK(db); // The database connection must be non-NULL. | 27 DCHECK(db); // The database connection must be non-NULL. |
26 db_ = db; | 28 db_ = db; |
27 return CreateTableIfNonExistent(); | 29 return CreateTableIfNonExistent(); |
28 } | 30 } |
29 | 31 |
| 32 void PrecacheSessionTable::SetLastPrecacheTimestamp(const base::Time& time) { |
| 33 DCHECK(!time.is_null()); |
| 34 Timestamp timestamp; |
| 35 timestamp.set_seconds((time - base::Time::UnixEpoch()).InSeconds()); |
| 36 Statement statement(db_->GetCachedStatement( |
| 37 SQL_FROM_HERE, |
| 38 "INSERT OR REPLACE INTO precache_session (type, value) VALUES(?,?)")); |
| 39 statement.BindInt(0, static_cast<int>(LAST_PRECACHE_TIMESTAMP)); |
| 40 statement.BindString(1, timestamp.SerializeAsString()); |
| 41 statement.Run(); |
| 42 } |
| 43 |
| 44 base::Time PrecacheSessionTable::GetLastPrecacheTimestamp() { |
| 45 Statement statement(db_->GetCachedStatement( |
| 46 SQL_FROM_HERE, "SELECT value from precache_session where type=?")); |
| 47 statement.BindInt(0, static_cast<int>(LAST_PRECACHE_TIMESTAMP)); |
| 48 Timestamp timestamp; |
| 49 if (statement.Step()) |
| 50 timestamp.ParseFromString(statement.ColumnString(0)); |
| 51 return timestamp.has_seconds() |
| 52 ? base::Time::UnixEpoch() + |
| 53 base::TimeDelta::FromSeconds(timestamp.seconds()) |
| 54 : base::Time(); |
| 55 } |
| 56 |
| 57 void PrecacheSessionTable::DeleteLastPrecacheTimestamp() { |
| 58 Statement statement(db_->GetCachedStatement( |
| 59 SQL_FROM_HERE, "DELETE FROM precache_session where type=?")); |
| 60 statement.BindInt(0, static_cast<int>(LAST_PRECACHE_TIMESTAMP)); |
| 61 statement.Run(); |
| 62 } |
| 63 |
30 // Store unfinished work. | 64 // Store unfinished work. |
31 void PrecacheSessionTable::SaveUnfinishedWork( | 65 void PrecacheSessionTable::SaveUnfinishedWork( |
32 std::unique_ptr<PrecacheUnfinishedWork> unfinished_work) { | 66 std::unique_ptr<PrecacheUnfinishedWork> unfinished_work) { |
33 Statement statement(db_->GetCachedStatement( | 67 Statement statement(db_->GetCachedStatement( |
34 SQL_FROM_HERE, | 68 SQL_FROM_HERE, |
35 "INSERT OR REPLACE INTO precache_session (type, value) VALUES(?,?)")); | 69 "INSERT OR REPLACE INTO precache_session (type, value) VALUES(?,?)")); |
36 statement.BindInt(0, static_cast<int>(UNFINISHED_WORK)); | 70 statement.BindInt(0, static_cast<int>(UNFINISHED_WORK)); |
37 statement.BindString(1, unfinished_work->SerializeAsString()); | 71 statement.BindString(1, unfinished_work->SerializeAsString()); |
38 statement.Run(); | 72 statement.Run(); |
39 } | 73 } |
(...skipping 21 matching lines...) Expand all Loading... |
61 statement.Run(); | 95 statement.Run(); |
62 } | 96 } |
63 | 97 |
64 bool PrecacheSessionTable::CreateTableIfNonExistent() { | 98 bool PrecacheSessionTable::CreateTableIfNonExistent() { |
65 return db_->Execute( | 99 return db_->Execute( |
66 "CREATE TABLE IF NOT EXISTS precache_session (type INTEGER PRIMARY KEY, " | 100 "CREATE TABLE IF NOT EXISTS precache_session (type INTEGER PRIMARY KEY, " |
67 "value STRING)"); | 101 "value STRING)"); |
68 } | 102 } |
69 | 103 |
70 } // namespace precache | 104 } // namespace precache |
OLD | NEW |