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 <stdint.h> |
8 #include <string> | 8 #include <string> |
9 | 9 |
10 #include "base/logging.h" | 10 #include "base/logging.h" |
11 #include "base/time/time.h" | 11 #include "base/time/time.h" |
| 12 #include "components/precache/core/proto/quota.pb.h" |
12 #include "components/precache/core/proto/timestamp.pb.h" | 13 #include "components/precache/core/proto/timestamp.pb.h" |
13 #include "components/precache/core/proto/unfinished_work.pb.h" | 14 #include "components/precache/core/proto/unfinished_work.pb.h" |
14 #include "sql/connection.h" | 15 #include "sql/connection.h" |
15 #include "sql/statement.h" | 16 #include "sql/statement.h" |
16 | 17 |
17 using sql::Statement; | 18 using sql::Statement; |
18 | 19 |
19 namespace precache { | 20 namespace precache { |
20 | 21 |
21 PrecacheSessionTable::PrecacheSessionTable() : db_(nullptr) {} | 22 PrecacheSessionTable::PrecacheSessionTable() : db_(nullptr) {} |
22 | 23 |
23 PrecacheSessionTable::~PrecacheSessionTable() {} | 24 PrecacheSessionTable::~PrecacheSessionTable() {} |
24 | 25 |
25 bool PrecacheSessionTable::Init(sql::Connection* db) { | 26 bool PrecacheSessionTable::Init(sql::Connection* db) { |
26 DCHECK(!db_); // Init must only be called once. | 27 DCHECK(!db_); // Init must only be called once. |
27 DCHECK(db); // The database connection must be non-NULL. | 28 DCHECK(db); // The database connection must be non-NULL. |
28 db_ = db; | 29 db_ = db; |
29 return CreateTableIfNonExistent(); | 30 return CreateTableIfNonExistent(); |
30 } | 31 } |
31 | 32 |
| 33 void PrecacheSessionTable::SetSessionDataType(SessionDataType id, |
| 34 const std::string& data) { |
| 35 Statement statement(db_->GetCachedStatement( |
| 36 SQL_FROM_HERE, |
| 37 "INSERT OR REPLACE INTO precache_session (type, value) VALUES(?,?)")); |
| 38 statement.BindInt(0, static_cast<int>(id)); |
| 39 statement.BindString(1, data); |
| 40 statement.Run(); |
| 41 } |
| 42 |
| 43 std::string PrecacheSessionTable::GetSessionDataType(SessionDataType id) { |
| 44 Statement statement(db_->GetCachedStatement( |
| 45 SQL_FROM_HERE, "SELECT value from precache_session where type=?")); |
| 46 statement.BindInt(0, static_cast<int>(id)); |
| 47 return statement.Step() ? statement.ColumnString(0) : std::string(); |
| 48 } |
| 49 |
32 void PrecacheSessionTable::SetLastPrecacheTimestamp(const base::Time& time) { | 50 void PrecacheSessionTable::SetLastPrecacheTimestamp(const base::Time& time) { |
33 DCHECK(!time.is_null()); | 51 DCHECK(!time.is_null()); |
34 Timestamp timestamp; | 52 Timestamp timestamp; |
35 timestamp.set_seconds((time - base::Time::UnixEpoch()).InSeconds()); | 53 timestamp.set_seconds((time - base::Time::UnixEpoch()).InSeconds()); |
36 Statement statement(db_->GetCachedStatement( | 54 SetSessionDataType(LAST_PRECACHE_TIMESTAMP, timestamp.SerializeAsString()); |
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 } | 55 } |
43 | 56 |
44 base::Time PrecacheSessionTable::GetLastPrecacheTimestamp() { | 57 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; | 58 Timestamp timestamp; |
49 if (statement.Step()) | 59 const std::string data = GetSessionDataType(LAST_PRECACHE_TIMESTAMP); |
50 timestamp.ParseFromString(statement.ColumnString(0)); | 60 if (!data.empty()) |
| 61 timestamp.ParseFromString(data); |
51 return timestamp.has_seconds() | 62 return timestamp.has_seconds() |
52 ? base::Time::UnixEpoch() + | 63 ? base::Time::UnixEpoch() + |
53 base::TimeDelta::FromSeconds(timestamp.seconds()) | 64 base::TimeDelta::FromSeconds(timestamp.seconds()) |
54 : base::Time(); | 65 : base::Time(); |
55 } | 66 } |
56 | 67 |
57 void PrecacheSessionTable::DeleteLastPrecacheTimestamp() { | 68 void PrecacheSessionTable::DeleteLastPrecacheTimestamp() { |
58 Statement statement(db_->GetCachedStatement( | 69 Statement statement(db_->GetCachedStatement( |
59 SQL_FROM_HERE, "DELETE FROM precache_session where type=?")); | 70 SQL_FROM_HERE, "DELETE FROM precache_session where type=?")); |
60 statement.BindInt(0, static_cast<int>(LAST_PRECACHE_TIMESTAMP)); | 71 statement.BindInt(0, static_cast<int>(LAST_PRECACHE_TIMESTAMP)); |
61 statement.Run(); | 72 statement.Run(); |
62 } | 73 } |
63 | 74 |
64 // Store unfinished work. | 75 // Store unfinished work. |
65 void PrecacheSessionTable::SaveUnfinishedWork( | 76 void PrecacheSessionTable::SaveUnfinishedWork( |
66 std::unique_ptr<PrecacheUnfinishedWork> unfinished_work) { | 77 std::unique_ptr<PrecacheUnfinishedWork> unfinished_work) { |
67 Statement statement(db_->GetCachedStatement( | 78 SetSessionDataType(UNFINISHED_WORK, unfinished_work->SerializeAsString()); |
68 SQL_FROM_HERE, | |
69 "INSERT OR REPLACE INTO precache_session (type, value) VALUES(?,?)")); | |
70 statement.BindInt(0, static_cast<int>(UNFINISHED_WORK)); | |
71 statement.BindString(1, unfinished_work->SerializeAsString()); | |
72 statement.Run(); | |
73 } | 79 } |
74 | 80 |
75 // Retrieve unfinished work. | 81 // Retrieve unfinished work. |
76 std::unique_ptr<PrecacheUnfinishedWork> | 82 std::unique_ptr<PrecacheUnfinishedWork> |
77 PrecacheSessionTable::GetUnfinishedWork() { | 83 PrecacheSessionTable::GetUnfinishedWork() { |
78 Statement statement(db_->GetCachedStatement( | 84 const std::string data = GetSessionDataType(UNFINISHED_WORK); |
79 SQL_FROM_HERE, "SELECT value from precache_session where type=?")); | |
80 statement.BindInt(0, static_cast<int>(UNFINISHED_WORK)); | |
81 std::unique_ptr<PrecacheUnfinishedWork> unfinished_work( | 85 std::unique_ptr<PrecacheUnfinishedWork> unfinished_work( |
82 new PrecacheUnfinishedWork()); | 86 new PrecacheUnfinishedWork()); |
83 if (statement.Step()) | 87 if (!data.empty()) |
84 unfinished_work->ParseFromString(statement.ColumnString(0)); | 88 unfinished_work->ParseFromString(data); |
85 return unfinished_work; | 89 return unfinished_work; |
86 } | 90 } |
87 | 91 |
88 | |
89 | |
90 void PrecacheSessionTable::DeleteUnfinishedWork() { | 92 void PrecacheSessionTable::DeleteUnfinishedWork() { |
91 Statement statement( | 93 Statement statement( |
92 db_->GetCachedStatement( | 94 db_->GetCachedStatement( |
93 SQL_FROM_HERE, "DELETE FROM precache_session where type=?")); | 95 SQL_FROM_HERE, "DELETE FROM precache_session where type=?")); |
94 statement.BindInt(0, static_cast<int>(UNFINISHED_WORK)); | 96 statement.BindInt(0, static_cast<int>(UNFINISHED_WORK)); |
95 statement.Run(); | 97 statement.Run(); |
96 } | 98 } |
97 | 99 |
| 100 void PrecacheSessionTable::SaveQuota(const PrecacheQuota& quota) { |
| 101 SetSessionDataType(QUOTA, quota.SerializeAsString()); |
| 102 } |
| 103 |
| 104 PrecacheQuota PrecacheSessionTable::GetQuota() { |
| 105 PrecacheQuota quota; |
| 106 const std::string data = GetSessionDataType(QUOTA); |
| 107 if (!data.empty()) |
| 108 quota.ParseFromString(data); |
| 109 return quota; |
| 110 } |
| 111 |
98 bool PrecacheSessionTable::CreateTableIfNonExistent() { | 112 bool PrecacheSessionTable::CreateTableIfNonExistent() { |
99 return db_->Execute( | 113 return db_->Execute( |
100 "CREATE TABLE IF NOT EXISTS precache_session (type INTEGER PRIMARY KEY, " | 114 "CREATE TABLE IF NOT EXISTS precache_session (type INTEGER PRIMARY KEY, " |
101 "value STRING)"); | 115 "value STRING)"); |
102 } | 116 } |
103 | 117 |
104 } // namespace precache | 118 } // namespace precache |
OLD | NEW |