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