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

Side by Side Diff: components/offline_pages/offline_page_metadata_store_sql.cc

Issue 1993953002: [Offline pages] Adding expiration capability to OfflinePageModel (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Addressing feedback Created 4 years, 7 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/offline_pages/offline_page_metadata_store_sql.h" 5 #include "components/offline_pages/offline_page_metadata_store_sql.h"
6 6
7 #include "base/bind.h" 7 #include "base/bind.h"
8 #include "base/files/file_path.h" 8 #include "base/files/file_path.h"
9 #include "base/files/file_util.h" 9 #include "base/files/file_util.h"
10 #include "base/location.h" 10 #include "base/location.h"
(...skipping 24 matching lines...) Expand all
35 " access_count INTEGER NOT NULL," 35 " access_count INTEGER NOT NULL,"
36 " status INTEGER NOT NULL DEFAULT 0," 36 " status INTEGER NOT NULL DEFAULT 0,"
37 // A note on this field: It will be NULL for now and is reserved for 37 // A note on this field: It will be NULL for now and is reserved for
38 // later use. We will treat NULL as "Unknown" in any subsequent queries 38 // later use. We will treat NULL as "Unknown" in any subsequent queries
39 // for user_initiated values. 39 // for user_initiated values.
40 " user_initiated INTEGER," // this is actually a boolean 40 " user_initiated INTEGER," // this is actually a boolean
41 " client_namespace VARCHAR NOT NULL," 41 " client_namespace VARCHAR NOT NULL,"
42 " client_id VARCHAR NOT NULL," 42 " client_id VARCHAR NOT NULL,"
43 " online_url VARCHAR NOT NULL," 43 " online_url VARCHAR NOT NULL,"
44 " offline_url VARCHAR NOT NULL DEFAULT ''," 44 " offline_url VARCHAR NOT NULL DEFAULT '',"
45 " file_path VARCHAR NOT NULL" 45 " file_path VARCHAR NOT NULL,"
46 " expiration_time INTEGER NOT NULL"
bburns 2016/05/20 18:24:53 One of the SQL feedbacks was to pack the integers
fgorski 2016/05/20 20:54:54 Done.
46 ")"; 47 ")";
47 48
48 // This is cloned from //content/browser/appcache/appcache_database.cc 49 // This is cloned from //content/browser/appcache/appcache_database.cc
49 struct TableInfo { 50 struct TableInfo {
50 const char* table_name; 51 const char* table_name;
51 const char* columns; 52 const char* columns;
52 }; 53 };
53 54
54 const TableInfo kOfflinePagesTable{OFFLINE_PAGES_TABLE_NAME, 55 const TableInfo kOfflinePagesTable{OFFLINE_PAGES_TABLE_NAME,
55 kOfflinePagesColumns}; 56 kOfflinePagesColumns};
56 57
57 // This enum is used to define the indices for the columns in each row 58 // This enum is used to define the indices for the columns in each row
58 // that hold the different pieces of offline page. 59 // that hold the different pieces of offline page.
59 enum : int { 60 enum : int {
60 OP_OFFLINE_ID = 0, 61 OP_OFFLINE_ID = 0,
61 OP_CREATION_TIME, 62 OP_CREATION_TIME,
62 OP_FILE_SIZE, 63 OP_FILE_SIZE,
63 OP_VERSION, 64 OP_VERSION,
64 OP_LAST_ACCESS_TIME, 65 OP_LAST_ACCESS_TIME,
65 OP_ACCESS_COUNT, 66 OP_ACCESS_COUNT,
66 OP_STATUS, 67 OP_STATUS,
67 OP_USER_INITIATED, 68 OP_USER_INITIATED,
68 OP_CLIENT_NAMESPACE, 69 OP_CLIENT_NAMESPACE,
69 OP_CLIENT_ID, 70 OP_CLIENT_ID,
70 OP_ONLINE_URL, 71 OP_ONLINE_URL,
71 OP_OFFLINE_URL, 72 OP_OFFLINE_URL,
72 OP_FILE_PATH 73 OP_FILE_PATH,
74 OP_EXPIRATION_TIME,
73 }; 75 };
74 76
75 bool CreateTable(sql::Connection* db, const TableInfo& table_info) { 77 bool CreateTable(sql::Connection* db, const TableInfo& table_info) {
76 std::string sql("CREATE TABLE "); 78 std::string sql("CREATE TABLE ");
77 sql += table_info.table_name; 79 sql += table_info.table_name;
78 sql += table_info.columns; 80 sql += table_info.columns;
79 return db->Execute(sql.c_str()); 81 return db->Execute(sql.c_str());
80 } 82 }
81 83
82 bool CreateSchema(sql::Connection* db) { 84 bool CreateSchema(sql::Connection* db) {
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after
119 #endif 121 #endif
120 int64_t file_size = statement->ColumnInt64(OP_FILE_SIZE); 122 int64_t file_size = statement->ColumnInt64(OP_FILE_SIZE);
121 base::Time creation_time = 123 base::Time creation_time =
122 base::Time::FromInternalValue(statement->ColumnInt64(OP_CREATION_TIME)); 124 base::Time::FromInternalValue(statement->ColumnInt64(OP_CREATION_TIME));
123 125
124 OfflinePageItem item(url, id, client_id, path, file_size, creation_time); 126 OfflinePageItem item(url, id, client_id, path, file_size, creation_time);
125 item.last_access_time = base::Time::FromInternalValue( 127 item.last_access_time = base::Time::FromInternalValue(
126 statement->ColumnInt64(OP_LAST_ACCESS_TIME)); 128 statement->ColumnInt64(OP_LAST_ACCESS_TIME));
127 item.version = statement->ColumnInt(OP_VERSION); 129 item.version = statement->ColumnInt(OP_VERSION);
128 item.access_count = statement->ColumnInt(OP_ACCESS_COUNT); 130 item.access_count = statement->ColumnInt(OP_ACCESS_COUNT);
131 item.expiration_time =
132 base::Time::FromInternalValue(statement->ColumnInt64(OP_EXPIRATION_TIME));
129 return item; 133 return item;
130 } 134 }
131 135
132 bool InsertOrReplace(sql::Connection* db, const OfflinePageItem& item) { 136 bool InsertOrReplace(sql::Connection* db, const OfflinePageItem& item) {
133 const char kSql[] = 137 const char kSql[] =
134 "INSERT OR REPLACE INTO " OFFLINE_PAGES_TABLE_NAME 138 "INSERT OR REPLACE INTO " OFFLINE_PAGES_TABLE_NAME
135 " (offline_id, online_url, client_namespace, client_id, file_path, " 139 " (offline_id, online_url, client_namespace, client_id, file_path, "
136 "file_size, creation_time, last_access_time, version, access_count)" 140 "file_size, creation_time, last_access_time, version, access_count, "
141 "expiration_time)"
137 " VALUES " 142 " VALUES "
138 " (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"; 143 " (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
139 144
140 sql::Statement statement(db->GetCachedStatement(SQL_FROM_HERE, kSql)); 145 sql::Statement statement(db->GetCachedStatement(SQL_FROM_HERE, kSql));
141 statement.BindInt64(0, item.offline_id); 146 statement.BindInt64(0, item.offline_id);
142 statement.BindString(1, item.url.spec()); 147 statement.BindString(1, item.url.spec());
143 statement.BindString(2, item.client_id.name_space); 148 statement.BindString(2, item.client_id.name_space);
144 statement.BindString(3, item.client_id.id); 149 statement.BindString(3, item.client_id.id);
145 #if defined(OS_POSIX) 150 #if defined(OS_POSIX)
146 std::string path_string = item.file_path.value(); 151 std::string path_string = item.file_path.value();
147 #elif defined(OS_WIN) 152 #elif defined(OS_WIN)
148 std::string path_string = base::WideToUTF8(item.file_path.value()); 153 std::string path_string = base::WideToUTF8(item.file_path.value());
149 #else 154 #else
150 #error Unknown OS 155 #error Unknown OS
151 #endif 156 #endif
152 statement.BindString(4, path_string); 157 statement.BindString(4, path_string);
153 statement.BindInt64(5, item.file_size); 158 statement.BindInt64(5, item.file_size);
154 statement.BindInt64(6, item.creation_time.ToInternalValue()); 159 statement.BindInt64(6, item.creation_time.ToInternalValue());
155 statement.BindInt64(7, item.last_access_time.ToInternalValue()); 160 statement.BindInt64(7, item.last_access_time.ToInternalValue());
156 statement.BindInt(8, item.version); 161 statement.BindInt(8, item.version);
157 statement.BindInt(9, item.access_count); 162 statement.BindInt(9, item.access_count);
163 statement.BindInt64(10, item.expiration_time.ToInternalValue());
158 return statement.Run(); 164 return statement.Run();
159 } 165 }
160 166
161 bool InitDatabase(sql::Connection* db, base::FilePath path) { 167 bool InitDatabase(sql::Connection* db, base::FilePath path) {
162 db->set_page_size(4096); 168 db->set_page_size(4096);
163 db->set_cache_size(500); 169 db->set_cache_size(500);
164 db->set_histogram_tag("OfflinePageMetadata"); 170 db->set_histogram_tag("OfflinePageMetadata");
165 db->set_exclusive_locking(); 171 db->set_exclusive_locking();
166 172
167 base::File::Error err; 173 base::File::Error err;
(...skipping 154 matching lines...) Expand 10 before | Expand all | Expand 10 after
322 } 328 }
323 329
324 void OfflinePageMetadataStoreSQL::Reset(const ResetCallback& callback) { 330 void OfflinePageMetadataStoreSQL::Reset(const ResetCallback& callback) {
325 background_task_runner_->PostTask( 331 background_task_runner_->PostTask(
326 FROM_HERE, 332 FROM_HERE,
327 base::Bind(&OfflinePageMetadataStoreSQL::ResetSync, base::Passed(&db_), 333 base::Bind(&OfflinePageMetadataStoreSQL::ResetSync, base::Passed(&db_),
328 base::ThreadTaskRunnerHandle::Get(), callback)); 334 base::ThreadTaskRunnerHandle::Get(), callback));
329 } 335 }
330 336
331 } // namespace offline_pages 337 } // namespace offline_pages
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698