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" |
11 #include "base/numerics/safe_conversions.h" | |
10 #include "base/time/time.h" | 12 #include "base/time/time.h" |
13 #include "components/precache/core/proto/timestamp.pb.h" | |
11 #include "components/precache/core/proto/unfinished_work.pb.h" | 14 #include "components/precache/core/proto/unfinished_work.pb.h" |
12 #include "sql/connection.h" | 15 #include "sql/connection.h" |
13 #include "sql/statement.h" | 16 #include "sql/statement.h" |
14 | 17 |
15 using sql::Statement; | 18 using sql::Statement; |
16 | 19 |
17 namespace precache { | 20 namespace precache { |
18 | 21 |
19 PrecacheSessionTable::PrecacheSessionTable() : db_(nullptr) {} | 22 PrecacheSessionTable::PrecacheSessionTable() : db_(nullptr) {} |
20 | 23 |
21 PrecacheSessionTable::~PrecacheSessionTable() {} | 24 PrecacheSessionTable::~PrecacheSessionTable() {} |
22 | 25 |
23 bool PrecacheSessionTable::Init(sql::Connection* db) { | 26 bool PrecacheSessionTable::Init(sql::Connection* db) { |
24 DCHECK(!db_); // Init must only be called once. | 27 DCHECK(!db_); // Init must only be called once. |
25 DCHECK(db); // The database connection must be non-NULL. | 28 DCHECK(db); // The database connection must be non-NULL. |
26 db_ = db; | 29 db_ = db; |
27 return CreateTableIfNonExistent(); | 30 return CreateTableIfNonExistent(); |
28 } | 31 } |
29 | 32 |
33 void PrecacheSessionTable::SetLastPrecacheTimestamp(const base::Time& time) { | |
34 Timestamp timestamp; | |
35 timestamp.set_seconds(base::saturated_cast<int64_t>(time.ToDoubleT())); | |
twifkak
2016/07/08 23:47:53
Nit: I think `(time - base::Time::UnixEpoch()).InS
jamartin
2016/07/12 14:18:44
Done.
| |
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 base::Time::FromDoubleT(timestamp.seconds()); | |
twifkak
2016/07/08 23:47:53
Likewise, this could become `base::Time::UnixEpoch
jamartin
2016/07/12 14:18:44
Done.
| |
52 } | |
53 | |
54 void PrecacheSessionTable::DeleteLastPrecacheTimestamp() { | |
55 Statement statement(db_->GetCachedStatement( | |
56 SQL_FROM_HERE, "DELETE FROM precache_session where type=?")); | |
57 statement.BindInt(0, static_cast<int>(LAST_PRECACHE_TIMESTAMP)); | |
58 statement.Run(); | |
59 } | |
60 | |
30 // Store unfinished work. | 61 // Store unfinished work. |
31 void PrecacheSessionTable::SaveUnfinishedWork( | 62 void PrecacheSessionTable::SaveUnfinishedWork( |
32 std::unique_ptr<PrecacheUnfinishedWork> unfinished_work) { | 63 std::unique_ptr<PrecacheUnfinishedWork> unfinished_work) { |
33 Statement statement(db_->GetCachedStatement( | 64 Statement statement(db_->GetCachedStatement( |
34 SQL_FROM_HERE, | 65 SQL_FROM_HERE, |
35 "INSERT OR REPLACE INTO precache_session (type, value) VALUES(?,?)")); | 66 "INSERT OR REPLACE INTO precache_session (type, value) VALUES(?,?)")); |
36 statement.BindInt(0, static_cast<int>(UNFINISHED_WORK)); | 67 statement.BindInt(0, static_cast<int>(UNFINISHED_WORK)); |
37 statement.BindString(1, unfinished_work->SerializeAsString()); | 68 statement.BindString(1, unfinished_work->SerializeAsString()); |
38 statement.Run(); | 69 statement.Run(); |
39 } | 70 } |
(...skipping 21 matching lines...) Expand all Loading... | |
61 statement.Run(); | 92 statement.Run(); |
62 } | 93 } |
63 | 94 |
64 bool PrecacheSessionTable::CreateTableIfNonExistent() { | 95 bool PrecacheSessionTable::CreateTableIfNonExistent() { |
65 return db_->Execute( | 96 return db_->Execute( |
66 "CREATE TABLE IF NOT EXISTS precache_session (type INTEGER PRIMARY KEY, " | 97 "CREATE TABLE IF NOT EXISTS precache_session (type INTEGER PRIMARY KEY, " |
67 "value STRING)"); | 98 "value STRING)"); |
68 } | 99 } |
69 | 100 |
70 } // namespace precache | 101 } // namespace precache |
OLD | NEW |