Chromium Code Reviews| 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/background/request_queue_store_sql.h" | 5 #include "components/offline_pages/background/request_queue_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 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 70 const char kSql[] = "DELETE FROM " REQUEST_QUEUE_TABLE_NAME | 70 const char kSql[] = "DELETE FROM " REQUEST_QUEUE_TABLE_NAME |
| 71 " WHERE client_namespace=? AND client_id=?"; | 71 " WHERE client_namespace=? AND client_id=?"; |
| 72 sql::Statement statement(db->GetCachedStatement(SQL_FROM_HERE, kSql)); | 72 sql::Statement statement(db->GetCachedStatement(SQL_FROM_HERE, kSql)); |
| 73 statement.BindString(0, client_id.name_space); | 73 statement.BindString(0, client_id.name_space); |
| 74 statement.BindString(1, client_id.id); | 74 statement.BindString(1, client_id.id); |
| 75 DVLOG(2) << kSql << " client_namespace " << client_id.name_space | 75 DVLOG(2) << kSql << " client_namespace " << client_id.name_space |
| 76 << " client_id " << client_id.id; | 76 << " client_id " << client_id.id; |
| 77 return statement.Run(); | 77 return statement.Run(); |
| 78 } | 78 } |
| 79 | 79 |
| 80 bool PauseRequest(sql::Connection* db, const int64_t request_id) { | |
| 81 const char kSql[] = "UPDATE " REQUEST_QUEUE_TABLE_NAME | |
| 82 " SET state=?" | |
| 83 " WHERE request_id=?"; | |
| 84 sql::Statement statement(db->GetCachedStatement(SQL_FROM_HERE, kSql)); | |
| 85 statement.BindInt64( | |
| 86 0, static_cast<long>(SavePageRequest::RequestState::PAUSED)); | |
| 87 statement.BindInt64(1, request_id); | |
| 88 return statement.Run(); | |
| 89 } | |
| 90 | |
| 91 bool ResumeRequest(sql::Connection* db, const int64_t request_id) { | |
| 92 const char kSql[] = "UPDATE " REQUEST_QUEUE_TABLE_NAME | |
| 93 " SET state=?" | |
| 94 " WHERE request_id=?"; | |
| 95 sql::Statement statement(db->GetCachedStatement(SQL_FROM_HERE, kSql)); | |
| 96 statement.BindInt64( | |
| 97 0, static_cast<long>(SavePageRequest::RequestState::AVAILABLE)); | |
| 98 statement.BindInt64(1, request_id); | |
| 99 return statement.Run(); | |
| 100 } | |
| 101 | |
| 80 bool DeleteRequestsByIds(sql::Connection* db, | 102 bool DeleteRequestsByIds(sql::Connection* db, |
| 81 const std::vector<int64_t>& request_ids, | 103 const std::vector<int64_t>& request_ids, |
| 82 int* count) { | 104 int* count) { |
| 83 DCHECK(count); | 105 DCHECK(count); |
| 84 // If you create a transaction but don't Commit() it is automatically | 106 // If you create a transaction but don't Commit() it is automatically |
| 85 // rolled back by its destructor when it falls out of scope. | 107 // rolled back by its destructor when it falls out of scope. |
| 86 sql::Transaction transaction(db); | 108 sql::Transaction transaction(db); |
| 87 if (!transaction.Begin()) | 109 if (!transaction.Begin()) |
| 88 return false; | 110 return false; |
| 89 | 111 |
| (...skipping 26 matching lines...) Expand all Loading... | |
| 116 return false; | 138 return false; |
| 117 *count += db->GetLastChangeCount(); | 139 *count += db->GetLastChangeCount(); |
| 118 } | 140 } |
| 119 | 141 |
| 120 if (!transaction.Commit()) | 142 if (!transaction.Commit()) |
| 121 return false; | 143 return false; |
| 122 | 144 |
| 123 return true; | 145 return true; |
| 124 } | 146 } |
| 125 | 147 |
| 148 RequestQueueStore::UpdateStatus PauseRequests( | |
| 149 sql::Connection* db, const std::vector<int64_t>& request_ids) { | |
| 150 // If you create a transaction but don't Commit() it is automatically | |
| 151 // rolled back by its destructor when it falls out of scope. | |
| 152 sql::Transaction transaction(db); | |
| 153 if (!transaction.Begin()) | |
| 154 return RequestQueueStore::UpdateStatus::FAILED; | |
| 155 for (const auto& request_id : request_ids) { | |
| 156 if (!PauseRequest(db, request_id)) | |
| 157 return RequestQueueStore::UpdateStatus::FAILED; | |
| 158 } | |
| 159 | |
| 160 if (!transaction.Commit()) | |
| 161 return RequestQueueStore::UpdateStatus::FAILED; | |
| 162 | |
| 163 return RequestQueueStore::UpdateStatus::UPDATED; | |
| 164 } | |
| 165 | |
| 166 RequestQueueStore::UpdateStatus ResumeRequests( | |
| 167 sql::Connection* db, const std::vector<int64_t>& request_ids) { | |
| 168 // If you create a transaction but don't Commit() it is automatically | |
| 169 // rolled back by its destructor when it falls out of scope. | |
| 170 sql::Transaction transaction(db); | |
| 171 if (!transaction.Begin()) | |
| 172 return RequestQueueStore::UpdateStatus::FAILED; | |
| 173 | |
| 174 for (const auto& request_id : request_ids) { | |
| 175 if (!ResumeRequest(db, request_id)) | |
| 176 return RequestQueueStore::UpdateStatus::FAILED; | |
| 177 } | |
| 178 | |
| 179 if (!transaction.Commit()) | |
| 180 return RequestQueueStore::UpdateStatus::FAILED; | |
| 181 | |
| 182 return RequestQueueStore::UpdateStatus::UPDATED; | |
| 183 } | |
| 184 | |
| 126 // Create a save page request from a SQL result. Expects complete rows with | 185 // Create a save page request from a SQL result. Expects complete rows with |
| 127 // all columns present. Columns are in order they are defined in select query | 186 // all columns present. Columns are in order they are defined in select query |
| 128 // in |RequestQueueStore::RequestSync| method. | 187 // in |RequestQueueStore::RequestSync| method. |
| 129 SavePageRequest MakeSavePageRequest(const sql::Statement& statement) { | 188 SavePageRequest MakeSavePageRequest(const sql::Statement& statement) { |
| 130 const int64_t id = statement.ColumnInt64(0); | 189 const int64_t id = statement.ColumnInt64(0); |
| 131 const base::Time creation_time = | 190 const base::Time creation_time = |
| 132 base::Time::FromInternalValue(statement.ColumnInt64(1)); | 191 base::Time::FromInternalValue(statement.ColumnInt64(1)); |
| 133 const base::Time activation_time = | 192 const base::Time activation_time = |
| 134 base::Time::FromInternalValue(statement.ColumnInt64(2)); | 193 base::Time::FromInternalValue(statement.ColumnInt64(2)); |
| 135 const base::Time last_attempt_time = | 194 const base::Time last_attempt_time = |
| (...skipping 147 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 283 const RemoveCallback& callback) { | 342 const RemoveCallback& callback) { |
| 284 // TODO(fgorski): add UMA metrics here. | 343 // TODO(fgorski): add UMA metrics here. |
| 285 int count = 0; | 344 int count = 0; |
| 286 if (DeleteRequestsByClientIds(db, client_ids, &count)) | 345 if (DeleteRequestsByClientIds(db, client_ids, &count)) |
| 287 runner->PostTask(FROM_HERE, base::Bind(callback, true, count)); | 346 runner->PostTask(FROM_HERE, base::Bind(callback, true, count)); |
| 288 else | 347 else |
| 289 runner->PostTask(FROM_HERE, base::Bind(callback, false, 0)); | 348 runner->PostTask(FROM_HERE, base::Bind(callback, false, 0)); |
| 290 } | 349 } |
| 291 | 350 |
| 292 // static | 351 // static |
| 352 void RequestQueueStoreSQL::PauseRequestsSync( | |
| 353 sql::Connection* db, | |
| 354 scoped_refptr<base::SingleThreadTaskRunner> runner, | |
| 355 const std::vector<int64_t>& request_ids, | |
| 356 const UpdateCallback& callback) { | |
| 357 // TODO(fgorski): add UMA metrics here. | |
| 358 RequestQueueStore::UpdateStatus status = | |
| 359 offline_pages::PauseRequests(db, request_ids); | |
| 360 runner->PostTask(FROM_HERE, base::Bind(callback, status)); | |
| 361 } | |
| 362 | |
| 363 // static | |
| 364 void RequestQueueStoreSQL::ResumeRequestsSync( | |
| 365 sql::Connection* db, | |
| 366 scoped_refptr<base::SingleThreadTaskRunner> runner, | |
| 367 const std::vector<int64_t>& request_ids, | |
| 368 const UpdateCallback& callback) { | |
| 369 // TODO(fgorski): add UMA metrics here. | |
| 370 RequestQueueStore::UpdateStatus status = | |
| 371 offline_pages::ResumeRequests(db, request_ids); | |
| 372 runner->PostTask(FROM_HERE, base::Bind(callback, status)); | |
| 373 } | |
| 374 | |
| 375 // static | |
| 293 void RequestQueueStoreSQL::ResetSync( | 376 void RequestQueueStoreSQL::ResetSync( |
| 294 sql::Connection* db, | 377 sql::Connection* db, |
| 295 const base::FilePath& db_file_path, | 378 const base::FilePath& db_file_path, |
| 296 scoped_refptr<base::SingleThreadTaskRunner> runner, | 379 scoped_refptr<base::SingleThreadTaskRunner> runner, |
| 297 const ResetCallback& callback) { | 380 const ResetCallback& callback) { |
| 298 // This method deletes the content of the whole store and reinitializes it. | 381 // This method deletes the content of the whole store and reinitializes it. |
| 299 bool success = db->Raze(); | 382 bool success = db->Raze(); |
| 300 db->Close(); | 383 db->Close(); |
| 301 if (success) | 384 if (success) |
| 302 success = InitDatabase(db, db_file_path); | 385 success = InitDatabase(db, db_file_path); |
| (...skipping 25 matching lines...) Expand all Loading... | |
| 328 FROM_HERE, base::Bind(callback, UpdateStatus::FAILED)); | 411 FROM_HERE, base::Bind(callback, UpdateStatus::FAILED)); |
| 329 return; | 412 return; |
| 330 } | 413 } |
| 331 | 414 |
| 332 background_task_runner_->PostTask( | 415 background_task_runner_->PostTask( |
| 333 FROM_HERE, | 416 FROM_HERE, |
| 334 base::Bind(&RequestQueueStoreSQL::AddOrUpdateRequestSync, db_.get(), | 417 base::Bind(&RequestQueueStoreSQL::AddOrUpdateRequestSync, db_.get(), |
| 335 base::ThreadTaskRunnerHandle::Get(), request, callback)); | 418 base::ThreadTaskRunnerHandle::Get(), request, callback)); |
| 336 } | 419 } |
| 337 | 420 |
| 338 // TODO(petewil): This is unused, since request_coordinator doesn't keep | |
| 339 // request_ids, and neither do clients. Plan is to remove this API in a future | |
| 340 // changelist. If we do discover a need to keep it, name it | |
| 341 // RemoveRequestsByRequestId to be more parallell with RemoveRequestsByClientId. | |
| 342 void RequestQueueStoreSQL::RemoveRequests( | 421 void RequestQueueStoreSQL::RemoveRequests( |
| 343 const std::vector<int64_t>& request_ids, | 422 const std::vector<int64_t>& request_ids, |
| 344 const RemoveCallback& callback) { | 423 const RemoveCallback& callback) { |
| 345 DCHECK(db_.get()); | 424 DCHECK(db_.get()); |
| 346 if (!db_.get()) { | 425 if (!db_.get()) { |
| 347 // Nothing to do, but post a callback instead of calling directly | 426 // Nothing to do, but post a callback instead of calling directly |
| 348 // to preserve the async style behavior to prevent bugs. | 427 // to preserve the async style behavior to prevent bugs. |
| 349 base::ThreadTaskRunnerHandle::Get()->PostTask( | 428 base::ThreadTaskRunnerHandle::Get()->PostTask( |
| 350 FROM_HERE, base::Bind(callback, false, 0)); | 429 FROM_HERE, base::Bind(callback, false, 0)); |
| 351 return; | 430 return; |
| (...skipping 16 matching lines...) Expand all Loading... | |
| 368 FROM_HERE, base::Bind(callback, false, 0)); | 447 FROM_HERE, base::Bind(callback, false, 0)); |
| 369 return; | 448 return; |
| 370 } | 449 } |
| 371 | 450 |
| 372 background_task_runner_->PostTask( | 451 background_task_runner_->PostTask( |
| 373 FROM_HERE, | 452 FROM_HERE, |
| 374 base::Bind(&RequestQueueStoreSQL::RemoveRequestsByClientIdSync, db_.get(), | 453 base::Bind(&RequestQueueStoreSQL::RemoveRequestsByClientIdSync, db_.get(), |
| 375 base::ThreadTaskRunnerHandle::Get(), client_ids, callback)); | 454 base::ThreadTaskRunnerHandle::Get(), client_ids, callback)); |
| 376 } | 455 } |
| 377 | 456 |
| 457 | |
| 458 void RequestQueueStoreSQL::PauseRequests( | |
| 459 const std::vector<int64_t>& request_ids, | |
| 460 const UpdateCallback& callback) { | |
| 461 DCHECK(db_.get()); | |
| 462 if (!db_.get()) { | |
|
Dmitry Titov
2016/08/09 21:01:43
Could you pull all these identical blocks into som
Pete Williamson
2016/08/09 22:53:24
Done. Unfortunately, the callbacks have different
| |
| 463 // Nothing to do, but post a callback instead of calling directly | |
| 464 // to preserve the async style behavior to prevent bugs. | |
| 465 base::ThreadTaskRunnerHandle::Get()->PostTask( | |
| 466 FROM_HERE, base::Bind(callback, UpdateStatus::FAILED)); | |
| 467 return; | |
| 468 } | |
| 469 | |
| 470 background_task_runner_->PostTask( | |
|
Dmitry Titov
2016/08/09 21:01:43
Is there a reason we don't use PostTaskAndReply in
Pete Williamson
2016/08/09 22:53:24
I'm following the model in the rest of the file.
| |
| 471 FROM_HERE, | |
| 472 base::Bind(&RequestQueueStoreSQL::PauseRequestsSync, db_.get(), | |
| 473 base::ThreadTaskRunnerHandle::Get(), request_ids, callback)); | |
| 474 } | |
| 475 | |
| 476 void RequestQueueStoreSQL::ResumeRequests( | |
| 477 const std::vector<int64_t>& request_ids, | |
| 478 const UpdateCallback& callback) { | |
| 479 DCHECK(db_.get()); | |
| 480 if (!db_.get()) { | |
| 481 // Nothing to do, but post a callback instead of calling directly | |
| 482 // to preserve the async style behavior to prevent bugs. | |
| 483 base::ThreadTaskRunnerHandle::Get()->PostTask( | |
| 484 FROM_HERE, base::Bind(callback, UpdateStatus::FAILED)); | |
| 485 return; | |
| 486 } | |
| 487 | |
| 488 background_task_runner_->PostTask( | |
| 489 FROM_HERE, | |
| 490 base::Bind(&RequestQueueStoreSQL::ResumeRequestsSync, db_.get(), | |
| 491 base::ThreadTaskRunnerHandle::Get(), request_ids, callback)); | |
| 492 } | |
| 493 | |
| 494 | |
| 378 void RequestQueueStoreSQL::Reset(const ResetCallback& callback) { | 495 void RequestQueueStoreSQL::Reset(const ResetCallback& callback) { |
| 379 DCHECK(db_.get()); | 496 DCHECK(db_.get()); |
| 380 if (!db_.get()) { | 497 if (!db_.get()) { |
| 381 // Nothing to do, but post a callback instead of calling directly | 498 // Nothing to do, but post a callback instead of calling directly |
| 382 // to preserve the async style behavior to prevent bugs. | 499 // to preserve the async style behavior to prevent bugs. |
| 383 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, | 500 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, |
| 384 base::Bind(callback, false)); | 501 base::Bind(callback, false)); |
| 385 return; | 502 return; |
| 386 } | 503 } |
| 387 | 504 |
| (...skipping 25 matching lines...) Expand all Loading... | |
| 413 } | 530 } |
| 414 | 531 |
| 415 void RequestQueueStoreSQL::OnResetDone(const ResetCallback& callback, | 532 void RequestQueueStoreSQL::OnResetDone(const ResetCallback& callback, |
| 416 bool success) { | 533 bool success) { |
| 417 // Complete connection initialization post reset. | 534 // Complete connection initialization post reset. |
| 418 OnOpenConnectionDone(success); | 535 OnOpenConnectionDone(success); |
| 419 callback.Run(success); | 536 callback.Run(success); |
| 420 } | 537 } |
| 421 | 538 |
| 422 } // namespace offline_pages | 539 } // namespace offline_pages |
| OLD | NEW |