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