| OLD | NEW |
| (Empty) |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "components/offline_pages/offline_page_metadata_store_sql.h" | |
| 6 | |
| 7 #include "base/bind.h" | |
| 8 #include "base/files/file_path.h" | |
| 9 #include "base/files/file_util.h" | |
| 10 #include "base/location.h" | |
| 11 #include "base/logging.h" | |
| 12 #include "base/metrics/histogram_macros.h" | |
| 13 #include "base/sequenced_task_runner.h" | |
| 14 #include "base/strings/utf_string_conversions.h" | |
| 15 #include "base/threading/thread_task_runner_handle.h" | |
| 16 #include "components/offline_pages/offline_page_item.h" | |
| 17 #include "sql/connection.h" | |
| 18 #include "sql/statement.h" | |
| 19 #include "sql/transaction.h" | |
| 20 | |
| 21 namespace offline_pages { | |
| 22 | |
| 23 namespace { | |
| 24 | |
| 25 // This is a macro instead of a const so that | |
| 26 // it can be used inline in other SQL statements below. | |
| 27 #define OFFLINE_PAGES_TABLE_NAME "offlinepages_v1" | |
| 28 | |
| 29 bool CreateOfflinePagesTable(sql::Connection* db) { | |
| 30 const char kSql[] = "CREATE TABLE IF NOT EXISTS " OFFLINE_PAGES_TABLE_NAME | |
| 31 "(offline_id INTEGER PRIMARY KEY NOT NULL," | |
| 32 " creation_time INTEGER NOT NULL," | |
| 33 " file_size INTEGER NOT NULL," | |
| 34 " last_access_time INTEGER NOT NULL," | |
| 35 " access_count INTEGER NOT NULL," | |
| 36 " client_namespace VARCHAR NOT NULL," | |
| 37 " client_id VARCHAR NOT NULL," | |
| 38 " online_url VARCHAR NOT NULL," | |
| 39 " file_path VARCHAR NOT NULL," | |
| 40 " title VARCHAR NOT NULL DEFAULT ''," | |
| 41 " original_url VARCHAR NOT NULL DEFAULT ''" | |
| 42 ")"; | |
| 43 return db->Execute(kSql); | |
| 44 } | |
| 45 | |
| 46 bool UpgradeWithQuery(sql::Connection* db, const char* upgrade_sql) { | |
| 47 if (!db->Execute("ALTER TABLE " OFFLINE_PAGES_TABLE_NAME | |
| 48 " RENAME TO temp_" OFFLINE_PAGES_TABLE_NAME)) { | |
| 49 return false; | |
| 50 } | |
| 51 if (!CreateOfflinePagesTable(db)) | |
| 52 return false; | |
| 53 if (!db->Execute(upgrade_sql)) | |
| 54 return false; | |
| 55 if (!db->Execute("DROP TABLE IF EXISTS temp_" OFFLINE_PAGES_TABLE_NAME)) | |
| 56 return false; | |
| 57 return true; | |
| 58 } | |
| 59 | |
| 60 bool UpgradeFrom52(sql::Connection* db) { | |
| 61 const char kSql[] = | |
| 62 "INSERT INTO " OFFLINE_PAGES_TABLE_NAME | |
| 63 " (offline_id, creation_time, file_size, last_access_time, " | |
| 64 "access_count, client_namespace, client_id, " | |
| 65 "online_url, file_path) " | |
| 66 "SELECT " | |
| 67 "offline_id, creation_time, file_size, last_access_time, " | |
| 68 "access_count, client_namespace, client_id, " | |
| 69 "online_url, file_path " | |
| 70 "FROM temp_" OFFLINE_PAGES_TABLE_NAME; | |
| 71 return UpgradeWithQuery(db, kSql); | |
| 72 } | |
| 73 | |
| 74 bool UpgradeFrom53(sql::Connection* db) { | |
| 75 const char kSql[] = | |
| 76 "INSERT INTO " OFFLINE_PAGES_TABLE_NAME | |
| 77 " (offline_id, creation_time, file_size, last_access_time, " | |
| 78 "access_count, client_namespace, client_id, online_url, " | |
| 79 "file_path) " | |
| 80 "SELECT " | |
| 81 "offline_id, creation_time, file_size, last_access_time, " | |
| 82 "access_count, client_namespace, client_id, online_url, " | |
| 83 "file_path " | |
| 84 "FROM temp_" OFFLINE_PAGES_TABLE_NAME; | |
| 85 return UpgradeWithQuery(db, kSql); | |
| 86 } | |
| 87 | |
| 88 bool UpgradeFrom54(sql::Connection* db) { | |
| 89 const char kSql[] = | |
| 90 "INSERT INTO " OFFLINE_PAGES_TABLE_NAME | |
| 91 " (offline_id, creation_time, file_size, last_access_time, " | |
| 92 "access_count, client_namespace, client_id, online_url, " | |
| 93 "file_path, title) " | |
| 94 "SELECT " | |
| 95 "offline_id, creation_time, file_size, last_access_time, " | |
| 96 "access_count, client_namespace, client_id, online_url, " | |
| 97 "file_path, title " | |
| 98 "FROM temp_" OFFLINE_PAGES_TABLE_NAME; | |
| 99 return UpgradeWithQuery(db, kSql); | |
| 100 } | |
| 101 | |
| 102 bool UpgradeFrom55(sql::Connection* db) { | |
| 103 const char kSql[] = | |
| 104 "INSERT INTO " OFFLINE_PAGES_TABLE_NAME | |
| 105 " (offline_id, creation_time, file_size, last_access_time, " | |
| 106 "access_count, client_namespace, client_id, online_url, " | |
| 107 "file_path, title) " | |
| 108 "SELECT " | |
| 109 "offline_id, creation_time, file_size, last_access_time, " | |
| 110 "access_count, client_namespace, client_id, online_url, " | |
| 111 "file_path, title " | |
| 112 "FROM temp_" OFFLINE_PAGES_TABLE_NAME; | |
| 113 return UpgradeWithQuery(db, kSql); | |
| 114 } | |
| 115 | |
| 116 bool UpgradeFrom56(sql::Connection* db) { | |
| 117 const char kSql[] = | |
| 118 "INSERT INTO " OFFLINE_PAGES_TABLE_NAME | |
| 119 " (offline_id, creation_time, file_size, last_access_time, " | |
| 120 "access_count, client_namespace, client_id, online_url, " | |
| 121 "file_path, title, original_url) " | |
| 122 "SELECT " | |
| 123 "offline_id, creation_time, file_size, last_access_time, " | |
| 124 "access_count, client_namespace, client_id, online_url, " | |
| 125 "file_path, title, original_url " | |
| 126 "FROM temp_" OFFLINE_PAGES_TABLE_NAME; | |
| 127 return UpgradeWithQuery(db, kSql); | |
| 128 } | |
| 129 | |
| 130 bool CreateSchema(sql::Connection* db) { | |
| 131 // If you create a transaction but don't Commit() it is automatically | |
| 132 // rolled back by its destructor when it falls out of scope. | |
| 133 sql::Transaction transaction(db); | |
| 134 if (!transaction.Begin()) | |
| 135 return false; | |
| 136 | |
| 137 if (!db->DoesTableExist(OFFLINE_PAGES_TABLE_NAME)) { | |
| 138 if (!CreateOfflinePagesTable(db)) | |
| 139 return false; | |
| 140 } | |
| 141 | |
| 142 // Upgrade section. Details are described in the header file. | |
| 143 if (!db->DoesColumnExist(OFFLINE_PAGES_TABLE_NAME, "expiration_time") && | |
| 144 !db->DoesColumnExist(OFFLINE_PAGES_TABLE_NAME, "title")) { | |
| 145 if (!UpgradeFrom52(db)) | |
| 146 return false; | |
| 147 } else if (!db->DoesColumnExist(OFFLINE_PAGES_TABLE_NAME, "title")) { | |
| 148 if (!UpgradeFrom53(db)) | |
| 149 return false; | |
| 150 } else if (db->DoesColumnExist(OFFLINE_PAGES_TABLE_NAME, "offline_url")) { | |
| 151 if (!UpgradeFrom54(db)) | |
| 152 return false; | |
| 153 } else if (!db->DoesColumnExist(OFFLINE_PAGES_TABLE_NAME, "original_url")) { | |
| 154 if (!UpgradeFrom55(db)) | |
| 155 return false; | |
| 156 } else if (db->DoesColumnExist(OFFLINE_PAGES_TABLE_NAME, "expiration_time")) { | |
| 157 if (!UpgradeFrom56(db)) | |
| 158 return false; | |
| 159 } | |
| 160 | |
| 161 // TODO(fgorski): Add indices here. | |
| 162 return transaction.Commit(); | |
| 163 } | |
| 164 | |
| 165 bool DeleteByOfflineId(sql::Connection* db, int64_t offline_id) { | |
| 166 static const char kSql[] = | |
| 167 "DELETE FROM " OFFLINE_PAGES_TABLE_NAME " WHERE offline_id=?"; | |
| 168 sql::Statement statement(db->GetCachedStatement(SQL_FROM_HERE, kSql)); | |
| 169 statement.BindInt64(0, offline_id); | |
| 170 return statement.Run(); | |
| 171 } | |
| 172 | |
| 173 base::FilePath GetPathFromUTF8String(const std::string& path_string) { | |
| 174 #if defined(OS_POSIX) | |
| 175 return base::FilePath(path_string); | |
| 176 #elif defined(OS_WIN) | |
| 177 return base::FilePath(base::UTF8ToWide(path_string)); | |
| 178 #else | |
| 179 #error Unknown OS | |
| 180 #endif | |
| 181 } | |
| 182 | |
| 183 std::string GetUTF8StringFromPath(const base::FilePath& path) { | |
| 184 #if defined(OS_POSIX) | |
| 185 return path.value(); | |
| 186 #elif defined(OS_WIN) | |
| 187 return base::WideToUTF8(path.value()); | |
| 188 #else | |
| 189 #error Unknown OS | |
| 190 #endif | |
| 191 } | |
| 192 | |
| 193 // Create an offline page item from a SQL result. Expects complete rows with | |
| 194 // all columns present. | |
| 195 OfflinePageItem MakeOfflinePageItem(sql::Statement* statement) { | |
| 196 int64_t id = statement->ColumnInt64(0); | |
| 197 base::Time creation_time = | |
| 198 base::Time::FromInternalValue(statement->ColumnInt64(1)); | |
| 199 int64_t file_size = statement->ColumnInt64(2); | |
| 200 base::Time last_access_time = | |
| 201 base::Time::FromInternalValue(statement->ColumnInt64(3)); | |
| 202 int access_count = statement->ColumnInt(4); | |
| 203 ClientId client_id(statement->ColumnString(5), statement->ColumnString(6)); | |
| 204 GURL url(statement->ColumnString(7)); | |
| 205 base::FilePath path(GetPathFromUTF8String(statement->ColumnString(8))); | |
| 206 base::string16 title = statement->ColumnString16(9); | |
| 207 GURL original_url(statement->ColumnString(10)); | |
| 208 | |
| 209 OfflinePageItem item(url, id, client_id, path, file_size, creation_time); | |
| 210 item.last_access_time = last_access_time; | |
| 211 item.access_count = access_count; | |
| 212 item.title = title; | |
| 213 item.original_url = original_url; | |
| 214 return item; | |
| 215 } | |
| 216 | |
| 217 ItemActionStatus Insert(sql::Connection* db, const OfflinePageItem& item) { | |
| 218 // Using 'INSERT OR FAIL' or 'INSERT OR ABORT' in the query below causes debug | |
| 219 // builds to DLOG. | |
| 220 const char kSql[] = | |
| 221 "INSERT OR IGNORE INTO " OFFLINE_PAGES_TABLE_NAME | |
| 222 " (offline_id, online_url, client_namespace, client_id, file_path, " | |
| 223 "file_size, creation_time, last_access_time, access_count, " | |
| 224 "title, original_url)" | |
| 225 " VALUES " | |
| 226 " (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"; | |
| 227 | |
| 228 sql::Statement statement(db->GetCachedStatement(SQL_FROM_HERE, kSql)); | |
| 229 statement.BindInt64(0, item.offline_id); | |
| 230 statement.BindString(1, item.url.spec()); | |
| 231 statement.BindString(2, item.client_id.name_space); | |
| 232 statement.BindString(3, item.client_id.id); | |
| 233 statement.BindString(4, GetUTF8StringFromPath(item.file_path)); | |
| 234 statement.BindInt64(5, item.file_size); | |
| 235 statement.BindInt64(6, item.creation_time.ToInternalValue()); | |
| 236 statement.BindInt64(7, item.last_access_time.ToInternalValue()); | |
| 237 statement.BindInt(8, item.access_count); | |
| 238 statement.BindString16(9, item.title); | |
| 239 statement.BindString(10, item.original_url.spec()); | |
| 240 if (!statement.Run()) | |
| 241 return ItemActionStatus::STORE_ERROR; | |
| 242 if (db->GetLastChangeCount() == 0) | |
| 243 return ItemActionStatus::ALREADY_EXISTS; | |
| 244 return ItemActionStatus::SUCCESS; | |
| 245 } | |
| 246 | |
| 247 bool Update(sql::Connection* db, const OfflinePageItem& item) { | |
| 248 const char kSql[] = | |
| 249 "UPDATE OR IGNORE " OFFLINE_PAGES_TABLE_NAME | |
| 250 " SET online_url = ?, client_namespace = ?, client_id = ?, file_path = ?," | |
| 251 " file_size = ?, creation_time = ?, last_access_time = ?," | |
| 252 " access_count = ?, title = ?, original_url = ?" | |
| 253 " WHERE offline_id = ?"; | |
| 254 | |
| 255 sql::Statement statement(db->GetCachedStatement(SQL_FROM_HERE, kSql)); | |
| 256 statement.BindString(0, item.url.spec()); | |
| 257 statement.BindString(1, item.client_id.name_space); | |
| 258 statement.BindString(2, item.client_id.id); | |
| 259 statement.BindString(3, GetUTF8StringFromPath(item.file_path)); | |
| 260 statement.BindInt64(4, item.file_size); | |
| 261 statement.BindInt64(5, item.creation_time.ToInternalValue()); | |
| 262 statement.BindInt64(6, item.last_access_time.ToInternalValue()); | |
| 263 statement.BindInt(7, item.access_count); | |
| 264 statement.BindString16(8, item.title); | |
| 265 statement.BindString(9, item.original_url.spec()); | |
| 266 statement.BindInt64(10, item.offline_id); | |
| 267 return statement.Run() && db->GetLastChangeCount() > 0; | |
| 268 } | |
| 269 | |
| 270 bool InitDatabase(sql::Connection* db, base::FilePath path) { | |
| 271 db->set_page_size(4096); | |
| 272 db->set_cache_size(500); | |
| 273 db->set_histogram_tag("OfflinePageMetadata"); | |
| 274 db->set_exclusive_locking(); | |
| 275 | |
| 276 base::File::Error err; | |
| 277 if (!base::CreateDirectoryAndGetError(path.DirName(), &err)) { | |
| 278 LOG(ERROR) << "Failed to create offline pages db directory: " | |
| 279 << base::File::ErrorToString(err); | |
| 280 return false; | |
| 281 } | |
| 282 if (!db->Open(path)) { | |
| 283 LOG(ERROR) << "Failed to open database"; | |
| 284 return false; | |
| 285 } | |
| 286 db->Preload(); | |
| 287 | |
| 288 return CreateSchema(db); | |
| 289 } | |
| 290 | |
| 291 void NotifyLoadResult(scoped_refptr<base::SingleThreadTaskRunner> runner, | |
| 292 const OfflinePageMetadataStore::LoadCallback& callback, | |
| 293 OfflinePageMetadataStore::LoadStatus status, | |
| 294 const std::vector<OfflinePageItem>& result) { | |
| 295 // TODO(fgorski): Switch to SQL specific UMA metrics. | |
| 296 UMA_HISTOGRAM_ENUMERATION("OfflinePages.LoadStatus", status, | |
| 297 OfflinePageMetadataStore::LOAD_STATUS_COUNT); | |
| 298 if (status == OfflinePageMetadataStore::LOAD_SUCCEEDED) { | |
| 299 UMA_HISTOGRAM_COUNTS("OfflinePages.SavedPageCount", | |
| 300 static_cast<int32_t>(result.size())); | |
| 301 } else { | |
| 302 DVLOG(1) << "Offline pages database loading failed: " << status; | |
| 303 } | |
| 304 runner->PostTask(FROM_HERE, base::Bind(callback, result)); | |
| 305 } | |
| 306 | |
| 307 void OpenConnectionSync(sql::Connection* db, | |
| 308 scoped_refptr<base::SingleThreadTaskRunner> runner, | |
| 309 const base::FilePath& path, | |
| 310 const base::Callback<void(bool)>& callback) { | |
| 311 bool success = InitDatabase(db, path); | |
| 312 runner->PostTask(FROM_HERE, base::Bind(callback, success)); | |
| 313 } | |
| 314 | |
| 315 bool GetPageByOfflineIdSync(sql::Connection* db, | |
| 316 int64_t offline_id, | |
| 317 OfflinePageItem* item) { | |
| 318 const char kSql[] = | |
| 319 "SELECT * FROM " OFFLINE_PAGES_TABLE_NAME " WHERE offline_id = ?"; | |
| 320 sql::Statement statement(db->GetCachedStatement(SQL_FROM_HERE, kSql)); | |
| 321 statement.BindInt64(0, offline_id); | |
| 322 | |
| 323 if (statement.Step()) { | |
| 324 *item = MakeOfflinePageItem(&statement); | |
| 325 return true; | |
| 326 } | |
| 327 | |
| 328 return false; | |
| 329 } | |
| 330 | |
| 331 void GetOfflinePagesSync( | |
| 332 sql::Connection* db, | |
| 333 scoped_refptr<base::SingleThreadTaskRunner> runner, | |
| 334 const OfflinePageMetadataStore::LoadCallback& callback) { | |
| 335 const char kSql[] = "SELECT * FROM " OFFLINE_PAGES_TABLE_NAME; | |
| 336 | |
| 337 sql::Statement statement(db->GetCachedStatement(SQL_FROM_HERE, kSql)); | |
| 338 | |
| 339 std::vector<OfflinePageItem> result; | |
| 340 while (statement.Step()) | |
| 341 result.push_back(MakeOfflinePageItem(&statement)); | |
| 342 | |
| 343 if (statement.Succeeded()) { | |
| 344 NotifyLoadResult(runner, callback, OfflinePageMetadataStore::LOAD_SUCCEEDED, | |
| 345 result); | |
| 346 } else { | |
| 347 result.clear(); | |
| 348 NotifyLoadResult(runner, callback, | |
| 349 OfflinePageMetadataStore::STORE_LOAD_FAILED, result); | |
| 350 } | |
| 351 } | |
| 352 | |
| 353 void AddOfflinePageSync(sql::Connection* db, | |
| 354 scoped_refptr<base::SingleThreadTaskRunner> runner, | |
| 355 const OfflinePageItem& offline_page, | |
| 356 const OfflinePageMetadataStore::AddCallback& callback) { | |
| 357 ItemActionStatus status = Insert(db, offline_page); | |
| 358 runner->PostTask(FROM_HERE, base::Bind(callback, status)); | |
| 359 } | |
| 360 | |
| 361 void PostStoreUpdateResultForIds( | |
| 362 scoped_refptr<base::SingleThreadTaskRunner> runner, | |
| 363 StoreState store_state, | |
| 364 const std::vector<int64_t>& offline_ids, | |
| 365 ItemActionStatus action_status, | |
| 366 const OfflinePageMetadataStore::UpdateCallback& callback) { | |
| 367 std::unique_ptr<OfflinePagesUpdateResult> result( | |
| 368 new OfflinePagesUpdateResult(store_state)); | |
| 369 for (const auto& offline_id : offline_ids) | |
| 370 result->item_statuses.push_back(std::make_pair(offline_id, action_status)); | |
| 371 runner->PostTask(FROM_HERE, base::Bind(callback, base::Passed(&result))); | |
| 372 } | |
| 373 | |
| 374 void PostStoreErrorForAllPages( | |
| 375 scoped_refptr<base::SingleThreadTaskRunner> runner, | |
| 376 const std::vector<OfflinePageItem>& pages, | |
| 377 const OfflinePageMetadataStore::UpdateCallback& callback) { | |
| 378 std::vector<int64_t> offline_ids; | |
| 379 for (const auto& page : pages) | |
| 380 offline_ids.push_back(page.offline_id); | |
| 381 PostStoreUpdateResultForIds(runner, StoreState::LOADED, offline_ids, | |
| 382 ItemActionStatus::STORE_ERROR, callback); | |
| 383 } | |
| 384 | |
| 385 void PostStoreErrorForAllIds( | |
| 386 scoped_refptr<base::SingleThreadTaskRunner> runner, | |
| 387 const std::vector<int64_t>& offline_ids, | |
| 388 const OfflinePageMetadataStore::UpdateCallback& callback) { | |
| 389 PostStoreUpdateResultForIds(runner, StoreState::LOADED, offline_ids, | |
| 390 ItemActionStatus::STORE_ERROR, callback); | |
| 391 } | |
| 392 | |
| 393 void UpdateOfflinePagesSync( | |
| 394 sql::Connection* db, | |
| 395 scoped_refptr<base::SingleThreadTaskRunner> runner, | |
| 396 const std::vector<OfflinePageItem>& pages, | |
| 397 const OfflinePageMetadataStore::UpdateCallback& callback) { | |
| 398 std::unique_ptr<OfflinePagesUpdateResult> result( | |
| 399 new OfflinePagesUpdateResult(StoreState::LOADED)); | |
| 400 | |
| 401 sql::Transaction transaction(db); | |
| 402 if (!transaction.Begin()) { | |
| 403 PostStoreErrorForAllPages(runner, pages, callback); | |
| 404 return; | |
| 405 } | |
| 406 | |
| 407 for (const auto& page : pages) { | |
| 408 if (Update(db, page)) { | |
| 409 result->updated_items.push_back(page); | |
| 410 result->item_statuses.push_back( | |
| 411 std::make_pair(page.offline_id, ItemActionStatus::SUCCESS)); | |
| 412 } else { | |
| 413 result->item_statuses.push_back( | |
| 414 std::make_pair(page.offline_id, ItemActionStatus::NOT_FOUND)); | |
| 415 } | |
| 416 } | |
| 417 | |
| 418 if (!transaction.Commit()) { | |
| 419 PostStoreErrorForAllPages(runner, pages, callback); | |
| 420 return; | |
| 421 } | |
| 422 runner->PostTask(FROM_HERE, base::Bind(callback, base::Passed(&result))); | |
| 423 } | |
| 424 | |
| 425 void RemoveOfflinePagesSync( | |
| 426 const std::vector<int64_t>& offline_ids, | |
| 427 sql::Connection* db, | |
| 428 scoped_refptr<base::SingleThreadTaskRunner> runner, | |
| 429 const OfflinePageMetadataStore::UpdateCallback& callback) { | |
| 430 // TODO(fgorski): Perhaps add metrics here. | |
| 431 std::unique_ptr<OfflinePagesUpdateResult> result( | |
| 432 new OfflinePagesUpdateResult(StoreState::LOADED)); | |
| 433 | |
| 434 // If you create a transaction but don't Commit() it is automatically | |
| 435 // rolled back by its destructor when it falls out of scope. | |
| 436 sql::Transaction transaction(db); | |
| 437 if (!transaction.Begin()) { | |
| 438 PostStoreErrorForAllIds(runner, offline_ids, callback); | |
| 439 return; | |
| 440 } | |
| 441 | |
| 442 for (int64_t offline_id : offline_ids) { | |
| 443 OfflinePageItem page; | |
| 444 ItemActionStatus status; | |
| 445 if (!GetPageByOfflineIdSync(db, offline_id, &page)) { | |
| 446 status = ItemActionStatus::NOT_FOUND; | |
| 447 } else if (!DeleteByOfflineId(db, offline_id)) { | |
| 448 status = ItemActionStatus::STORE_ERROR; | |
| 449 } else { | |
| 450 status = ItemActionStatus::SUCCESS; | |
| 451 result->updated_items.push_back(page); | |
| 452 } | |
| 453 | |
| 454 result->item_statuses.push_back(std::make_pair(offline_id, status)); | |
| 455 } | |
| 456 | |
| 457 if (!transaction.Commit()) { | |
| 458 PostStoreErrorForAllIds(runner, offline_ids, callback); | |
| 459 return; | |
| 460 } | |
| 461 | |
| 462 runner->PostTask(FROM_HERE, base::Bind(callback, base::Passed(&result))); | |
| 463 } | |
| 464 | |
| 465 void ResetSync(sql::Connection* db, | |
| 466 const base::FilePath& db_file_path, | |
| 467 scoped_refptr<base::SingleThreadTaskRunner> runner, | |
| 468 const base::Callback<void(bool)>& callback) { | |
| 469 // This method deletes the content of the whole store and reinitializes it. | |
| 470 bool success = true; | |
| 471 if (db) { | |
| 472 success = db->Raze(); | |
| 473 db->Close(); | |
| 474 } | |
| 475 success = base::DeleteFile(db_file_path, true /*recursive*/) && success; | |
| 476 runner->PostTask(FROM_HERE, base::Bind(callback, success)); | |
| 477 } | |
| 478 | |
| 479 } // anonymous namespace | |
| 480 | |
| 481 OfflinePageMetadataStoreSQL::OfflinePageMetadataStoreSQL( | |
| 482 scoped_refptr<base::SequencedTaskRunner> background_task_runner, | |
| 483 const base::FilePath& path) | |
| 484 : background_task_runner_(std::move(background_task_runner)), | |
| 485 db_file_path_(path.AppendASCII("OfflinePages.db")), | |
| 486 state_(StoreState::NOT_LOADED), | |
| 487 weak_ptr_factory_(this) { | |
| 488 } | |
| 489 | |
| 490 OfflinePageMetadataStoreSQL::~OfflinePageMetadataStoreSQL() { | |
| 491 if (db_.get() && | |
| 492 !background_task_runner_->DeleteSoon(FROM_HERE, db_.release())) { | |
| 493 DLOG(WARNING) << "SQL database will not be deleted."; | |
| 494 } | |
| 495 } | |
| 496 | |
| 497 void OfflinePageMetadataStoreSQL::Initialize( | |
| 498 const InitializeCallback& callback) { | |
| 499 DCHECK(!db_); | |
| 500 db_.reset(new sql::Connection()); | |
| 501 background_task_runner_->PostTask( | |
| 502 FROM_HERE, | |
| 503 base::Bind(&OpenConnectionSync, db_.get(), | |
| 504 base::ThreadTaskRunnerHandle::Get(), db_file_path_, | |
| 505 base::Bind(&OfflinePageMetadataStoreSQL::OnOpenConnectionDone, | |
| 506 weak_ptr_factory_.GetWeakPtr(), callback))); | |
| 507 } | |
| 508 | |
| 509 void OfflinePageMetadataStoreSQL::GetOfflinePages( | |
| 510 const LoadCallback& callback) { | |
| 511 if (!CheckDb()) { | |
| 512 base::ThreadTaskRunnerHandle::Get()->PostTask( | |
| 513 FROM_HERE, base::Bind(callback, std::vector<OfflinePageItem>())); | |
| 514 return; | |
| 515 } | |
| 516 | |
| 517 background_task_runner_->PostTask( | |
| 518 FROM_HERE, base::Bind(&GetOfflinePagesSync, db_.get(), | |
| 519 base::ThreadTaskRunnerHandle::Get(), callback)); | |
| 520 } | |
| 521 | |
| 522 void OfflinePageMetadataStoreSQL::AddOfflinePage( | |
| 523 const OfflinePageItem& offline_page, | |
| 524 const AddCallback& callback) { | |
| 525 if (!CheckDb()) { | |
| 526 base::ThreadTaskRunnerHandle::Get()->PostTask( | |
| 527 FROM_HERE, base::Bind(callback, ItemActionStatus::STORE_ERROR)); | |
| 528 return; | |
| 529 } | |
| 530 | |
| 531 background_task_runner_->PostTask( | |
| 532 FROM_HERE, | |
| 533 base::Bind(&AddOfflinePageSync, db_.get(), | |
| 534 base::ThreadTaskRunnerHandle::Get(), offline_page, callback)); | |
| 535 } | |
| 536 | |
| 537 void OfflinePageMetadataStoreSQL::UpdateOfflinePages( | |
| 538 const std::vector<OfflinePageItem>& pages, | |
| 539 const UpdateCallback& callback) { | |
| 540 if (!CheckDb()) { | |
| 541 PostStoreErrorForAllPages(base::ThreadTaskRunnerHandle::Get(), pages, | |
| 542 callback); | |
| 543 return; | |
| 544 } | |
| 545 | |
| 546 background_task_runner_->PostTask( | |
| 547 FROM_HERE, | |
| 548 base::Bind(&UpdateOfflinePagesSync, db_.get(), | |
| 549 base::ThreadTaskRunnerHandle::Get(), pages, callback)); | |
| 550 } | |
| 551 | |
| 552 void OfflinePageMetadataStoreSQL::RemoveOfflinePages( | |
| 553 const std::vector<int64_t>& offline_ids, | |
| 554 const UpdateCallback& callback) { | |
| 555 if (!CheckDb()) { | |
| 556 PostStoreErrorForAllIds(base::ThreadTaskRunnerHandle::Get(), offline_ids, | |
| 557 callback); | |
| 558 return; | |
| 559 } | |
| 560 | |
| 561 if (offline_ids.empty()) { | |
| 562 // Nothing to do, but post a callback instead of calling directly | |
| 563 // to preserve the async style behavior to prevent bugs. | |
| 564 PostStoreUpdateResultForIds( | |
| 565 base::ThreadTaskRunnerHandle::Get(), state(), offline_ids, | |
| 566 ItemActionStatus::NOT_FOUND /* will be ignored */, callback); | |
| 567 return; | |
| 568 } | |
| 569 | |
| 570 background_task_runner_->PostTask( | |
| 571 FROM_HERE, base::Bind(&RemoveOfflinePagesSync, offline_ids, db_.get(), | |
| 572 base::ThreadTaskRunnerHandle::Get(), callback)); | |
| 573 } | |
| 574 | |
| 575 void OfflinePageMetadataStoreSQL::Reset(const ResetCallback& callback) { | |
| 576 background_task_runner_->PostTask( | |
| 577 FROM_HERE, | |
| 578 base::Bind(&ResetSync, db_.get(), db_file_path_, | |
| 579 base::ThreadTaskRunnerHandle::Get(), | |
| 580 base::Bind(&OfflinePageMetadataStoreSQL::OnResetDone, | |
| 581 weak_ptr_factory_.GetWeakPtr(), callback))); | |
| 582 } | |
| 583 | |
| 584 StoreState OfflinePageMetadataStoreSQL::state() const { | |
| 585 return state_; | |
| 586 } | |
| 587 | |
| 588 void OfflinePageMetadataStoreSQL::SetStateForTesting(StoreState state, | |
| 589 bool reset_db) { | |
| 590 state_ = state; | |
| 591 if (reset_db) | |
| 592 db_.reset(nullptr); | |
| 593 } | |
| 594 | |
| 595 void OfflinePageMetadataStoreSQL::OnOpenConnectionDone( | |
| 596 const InitializeCallback& callback, | |
| 597 bool success) { | |
| 598 DCHECK(db_.get()); | |
| 599 state_ = success ? StoreState::LOADED : StoreState::FAILED_LOADING; | |
| 600 callback.Run(success); | |
| 601 } | |
| 602 | |
| 603 void OfflinePageMetadataStoreSQL::OnResetDone(const ResetCallback& callback, | |
| 604 bool success) { | |
| 605 state_ = success ? StoreState::NOT_LOADED : StoreState::FAILED_RESET; | |
| 606 db_.reset(); | |
| 607 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, | |
| 608 base::Bind(callback, success)); | |
| 609 } | |
| 610 | |
| 611 bool OfflinePageMetadataStoreSQL::CheckDb() const { | |
| 612 return db_ && state_ == StoreState::LOADED; | |
| 613 } | |
| 614 | |
| 615 } // namespace offline_pages | |
| OLD | NEW |