| 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/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 20 matching lines...) Expand all Loading... |
| 31 " creation_time INTEGER NOT NULL," | 31 " creation_time INTEGER NOT NULL," |
| 32 " file_size INTEGER NOT NULL," | 32 " file_size INTEGER NOT NULL," |
| 33 " version INTEGER NOT NULL," | 33 " version INTEGER NOT NULL," |
| 34 " last_access_time INTEGER NOT NULL," | 34 " last_access_time INTEGER NOT NULL," |
| 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 " expiration_time INTEGER NOT NULL," |
| 41 " client_namespace VARCHAR NOT NULL," | 42 " client_namespace VARCHAR NOT NULL," |
| 42 " client_id VARCHAR NOT NULL," | 43 " client_id VARCHAR NOT NULL," |
| 43 " online_url VARCHAR NOT NULL," | 44 " online_url VARCHAR NOT NULL," |
| 44 " offline_url VARCHAR NOT NULL DEFAULT ''," | 45 " offline_url VARCHAR NOT NULL DEFAULT ''," |
| 45 " file_path VARCHAR NOT NULL" | 46 " file_path VARCHAR NOT NULL" |
| 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, |
| 69 OP_EXPIRATION_TIME, |
| 68 OP_CLIENT_NAMESPACE, | 70 OP_CLIENT_NAMESPACE, |
| 69 OP_CLIENT_ID, | 71 OP_CLIENT_ID, |
| 70 OP_ONLINE_URL, | 72 OP_ONLINE_URL, |
| 71 OP_OFFLINE_URL, | 73 OP_OFFLINE_URL, |
| 72 OP_FILE_PATH | 74 OP_FILE_PATH, |
| 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 Loading... |
| 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 155 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 323 } | 329 } |
| 324 | 330 |
| 325 void OfflinePageMetadataStoreSQL::Reset(const ResetCallback& callback) { | 331 void OfflinePageMetadataStoreSQL::Reset(const ResetCallback& callback) { |
| 326 background_task_runner_->PostTask( | 332 background_task_runner_->PostTask( |
| 327 FROM_HERE, | 333 FROM_HERE, |
| 328 base::Bind(&OfflinePageMetadataStoreSQL::ResetSync, base::Passed(&db_), | 334 base::Bind(&OfflinePageMetadataStoreSQL::ResetSync, base::Passed(&db_), |
| 329 base::ThreadTaskRunnerHandle::Get(), callback)); | 335 base::ThreadTaskRunnerHandle::Get(), callback)); |
| 330 } | 336 } |
| 331 | 337 |
| 332 } // namespace offline_pages | 338 } // namespace offline_pages |
| OLD | NEW |