Chromium Code Reviews| Index: components/offline_pages/background/request_queue_store_sql.cc |
| diff --git a/components/offline_pages/background/request_queue_store_sql.cc b/components/offline_pages/background/request_queue_store_sql.cc |
| index 264b2acb42734ee9db93c9ceefd8bde8a97f26b6..bdc91a7ba411667b5b3cf43792364f1710cd7b93 100644 |
| --- a/components/offline_pages/background/request_queue_store_sql.cc |
| +++ b/components/offline_pages/background/request_queue_store_sql.cc |
| @@ -77,6 +77,28 @@ bool DeleteRequestByClientId(sql::Connection* db, const ClientId& client_id) { |
| return statement.Run(); |
| } |
| +bool PauseRequest(sql::Connection* db, const int64_t request_id) { |
| + const char kSql[] = "UPDATE " REQUEST_QUEUE_TABLE_NAME |
| + " SET state=?" |
| + " WHERE request_id=?"; |
| + sql::Statement statement(db->GetCachedStatement(SQL_FROM_HERE, kSql)); |
| + statement.BindInt64( |
| + 0, static_cast<long>(SavePageRequest::RequestState::PAUSED)); |
| + statement.BindInt64(1, request_id); |
| + return statement.Run(); |
| +} |
| + |
| +bool ResumeRequest(sql::Connection* db, const int64_t request_id) { |
| + const char kSql[] = "UPDATE " REQUEST_QUEUE_TABLE_NAME |
| + " SET state=?" |
| + " WHERE request_id=?"; |
| + sql::Statement statement(db->GetCachedStatement(SQL_FROM_HERE, kSql)); |
| + statement.BindInt64( |
| + 0, static_cast<long>(SavePageRequest::RequestState::AVAILABLE)); |
| + statement.BindInt64(1, request_id); |
| + return statement.Run(); |
| +} |
| + |
| bool DeleteRequestsByIds(sql::Connection* db, |
| const std::vector<int64_t>& request_ids, |
| int* count) { |
| @@ -123,6 +145,43 @@ bool DeleteRequestsByClientIds(sql::Connection* db, |
| return true; |
| } |
| +RequestQueueStore::UpdateStatus PauseRequests( |
| + sql::Connection* db, const std::vector<int64_t>& request_ids) { |
| + // If you create a transaction but don't Commit() it is automatically |
| + // rolled back by its destructor when it falls out of scope. |
| + sql::Transaction transaction(db); |
| + if (!transaction.Begin()) |
| + return RequestQueueStore::UpdateStatus::FAILED; |
| + for (const auto& request_id : request_ids) { |
| + if (!PauseRequest(db, request_id)) |
| + return RequestQueueStore::UpdateStatus::FAILED; |
| + } |
| + |
| + if (!transaction.Commit()) |
| + return RequestQueueStore::UpdateStatus::FAILED; |
| + |
| + return RequestQueueStore::UpdateStatus::UPDATED; |
| +} |
| + |
| +RequestQueueStore::UpdateStatus ResumeRequests( |
| + sql::Connection* db, const std::vector<int64_t>& request_ids) { |
| + // If you create a transaction but don't Commit() it is automatically |
| + // rolled back by its destructor when it falls out of scope. |
| + sql::Transaction transaction(db); |
| + if (!transaction.Begin()) |
| + return RequestQueueStore::UpdateStatus::FAILED; |
| + |
| + for (const auto& request_id : request_ids) { |
| + if (!ResumeRequest(db, request_id)) |
| + return RequestQueueStore::UpdateStatus::FAILED; |
| + } |
| + |
| + if (!transaction.Commit()) |
| + return RequestQueueStore::UpdateStatus::FAILED; |
| + |
| + return RequestQueueStore::UpdateStatus::UPDATED; |
| +} |
| + |
| // Create a save page request from a SQL result. Expects complete rows with |
| // all columns present. Columns are in order they are defined in select query |
| // in |RequestQueueStore::RequestSync| method. |
| @@ -290,6 +349,30 @@ void RequestQueueStoreSQL::RemoveRequestsByClientIdSync( |
| } |
| // static |
| +void RequestQueueStoreSQL::PauseRequestsSync( |
| + sql::Connection* db, |
| + scoped_refptr<base::SingleThreadTaskRunner> runner, |
| + const std::vector<int64_t>& request_ids, |
| + const UpdateCallback& callback) { |
| + // TODO(fgorski): add UMA metrics here. |
| + RequestQueueStore::UpdateStatus status = |
| + offline_pages::PauseRequests(db, request_ids); |
| + runner->PostTask(FROM_HERE, base::Bind(callback, status)); |
| +} |
| + |
| +// static |
| +void RequestQueueStoreSQL::ResumeRequestsSync( |
| + sql::Connection* db, |
| + scoped_refptr<base::SingleThreadTaskRunner> runner, |
| + const std::vector<int64_t>& request_ids, |
| + const UpdateCallback& callback) { |
| + // TODO(fgorski): add UMA metrics here. |
| + RequestQueueStore::UpdateStatus status = |
| + offline_pages::ResumeRequests(db, request_ids); |
| + runner->PostTask(FROM_HERE, base::Bind(callback, status)); |
| +} |
| + |
| +// static |
| void RequestQueueStoreSQL::ResetSync( |
| sql::Connection* db, |
| const base::FilePath& db_file_path, |
| @@ -335,10 +418,6 @@ void RequestQueueStoreSQL::AddOrUpdateRequest(const SavePageRequest& request, |
| base::ThreadTaskRunnerHandle::Get(), request, callback)); |
| } |
| -// TODO(petewil): This is unused, since request_coordinator doesn't keep |
| -// request_ids, and neither do clients. Plan is to remove this API in a future |
| -// changelist. If we do discover a need to keep it, name it |
| -// RemoveRequestsByRequestId to be more parallell with RemoveRequestsByClientId. |
| void RequestQueueStoreSQL::RemoveRequests( |
| const std::vector<int64_t>& request_ids, |
| const RemoveCallback& callback) { |
| @@ -375,6 +454,44 @@ void RequestQueueStoreSQL::RemoveRequestsByClientId( |
| base::ThreadTaskRunnerHandle::Get(), client_ids, callback)); |
| } |
| + |
| +void RequestQueueStoreSQL::PauseRequests( |
| + const std::vector<int64_t>& request_ids, |
| + const UpdateCallback& callback) { |
| + DCHECK(db_.get()); |
| + 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
|
| + // Nothing to do, but post a callback instead of calling directly |
| + // to preserve the async style behavior to prevent bugs. |
| + base::ThreadTaskRunnerHandle::Get()->PostTask( |
| + FROM_HERE, base::Bind(callback, UpdateStatus::FAILED)); |
| + return; |
| + } |
| + |
| + 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.
|
| + FROM_HERE, |
| + base::Bind(&RequestQueueStoreSQL::PauseRequestsSync, db_.get(), |
| + base::ThreadTaskRunnerHandle::Get(), request_ids, callback)); |
| +} |
| + |
| +void RequestQueueStoreSQL::ResumeRequests( |
| + const std::vector<int64_t>& request_ids, |
| + const UpdateCallback& callback) { |
| + DCHECK(db_.get()); |
| + if (!db_.get()) { |
| + // Nothing to do, but post a callback instead of calling directly |
| + // to preserve the async style behavior to prevent bugs. |
| + base::ThreadTaskRunnerHandle::Get()->PostTask( |
| + FROM_HERE, base::Bind(callback, UpdateStatus::FAILED)); |
| + return; |
| + } |
| + |
| + background_task_runner_->PostTask( |
| + FROM_HERE, |
| + base::Bind(&RequestQueueStoreSQL::ResumeRequestsSync, db_.get(), |
| + base::ThreadTaskRunnerHandle::Get(), request_ids, callback)); |
| +} |
| + |
| + |
| void RequestQueueStoreSQL::Reset(const ResetCallback& callback) { |
| DCHECK(db_.get()); |
| if (!db_.get()) { |