| 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" |
| 11 #include "base/logging.h" |
| 11 #include "base/sequenced_task_runner.h" | 12 #include "base/sequenced_task_runner.h" |
| 12 #include "base/threading/thread_task_runner_handle.h" | 13 #include "base/threading/thread_task_runner_handle.h" |
| 13 #include "components/offline_pages/background/save_page_request.h" | 14 #include "components/offline_pages/background/save_page_request.h" |
| 14 #include "sql/connection.h" | 15 #include "sql/connection.h" |
| 15 #include "sql/statement.h" | 16 #include "sql/statement.h" |
| 16 #include "sql/transaction.h" | 17 #include "sql/transaction.h" |
| 17 | 18 |
| 18 namespace offline_pages { | 19 namespace offline_pages { |
| 19 | 20 |
| 20 namespace { | 21 namespace { |
| (...skipping 27 matching lines...) Expand all Loading... |
| 48 } | 49 } |
| 49 | 50 |
| 50 bool DeleteRequestById(sql::Connection* db, int64_t request_id) { | 51 bool DeleteRequestById(sql::Connection* db, int64_t request_id) { |
| 51 const char kSql[] = | 52 const char kSql[] = |
| 52 "DELETE FROM " REQUEST_QUEUE_TABLE_NAME " WHERE request_id=?"; | 53 "DELETE FROM " REQUEST_QUEUE_TABLE_NAME " WHERE request_id=?"; |
| 53 sql::Statement statement(db->GetCachedStatement(SQL_FROM_HERE, kSql)); | 54 sql::Statement statement(db->GetCachedStatement(SQL_FROM_HERE, kSql)); |
| 54 statement.BindInt64(0, request_id); | 55 statement.BindInt64(0, request_id); |
| 55 return statement.Run(); | 56 return statement.Run(); |
| 56 } | 57 } |
| 57 | 58 |
| 59 bool DeleteRequestByClientId(sql::Connection* db, const ClientId& client_id) { |
| 60 const char kSql[] = "DELETE FROM " REQUEST_QUEUE_TABLE_NAME |
| 61 " WHERE client_namespace=? AND client_id=?"; |
| 62 sql::Statement statement(db->GetCachedStatement(SQL_FROM_HERE, kSql)); |
| 63 statement.BindString(0, client_id.name_space); |
| 64 statement.BindString(1, client_id.id); |
| 65 DVLOG(2) << kSql << " client_namespace " << client_id.name_space |
| 66 << " client_id " << client_id.id; |
| 67 return statement.Run(); |
| 68 } |
| 69 |
| 58 bool DeleteRequestsByIds(sql::Connection* db, | 70 bool DeleteRequestsByIds(sql::Connection* db, |
| 59 const std::vector<int64_t>& request_ids, | 71 const std::vector<int64_t>& request_ids, |
| 60 int* count) { | 72 int* count) { |
| 61 DCHECK(count); | 73 DCHECK(count); |
| 62 // If you create a transaction but don't Commit() it is automatically | 74 // If you create a transaction but don't Commit() it is automatically |
| 63 // rolled back by its destructor when it falls out of scope. | 75 // rolled back by its destructor when it falls out of scope. |
| 64 sql::Transaction transaction(db); | 76 sql::Transaction transaction(db); |
| 65 if (!transaction.Begin()) | 77 if (!transaction.Begin()) |
| 66 return false; | 78 return false; |
| 67 | 79 |
| 68 *count = 0; | 80 *count = 0; |
| 69 for (auto request_id : request_ids) { | 81 for (auto request_id : request_ids) { |
| 70 if (!DeleteRequestById(db, request_id)) | 82 if (!DeleteRequestById(db, request_id)) |
| 71 return false; | 83 return false; |
| 72 *count += db->GetLastChangeCount(); | 84 *count += db->GetLastChangeCount(); |
| 73 } | 85 } |
| 74 | 86 |
| 75 if (!transaction.Commit()) | 87 if (!transaction.Commit()) |
| 76 return false; | 88 return false; |
| 77 | 89 |
| 78 return true; | 90 return true; |
| 79 } | 91 } |
| 80 | 92 |
| 93 bool DeleteRequestsByClientIds(sql::Connection* db, |
| 94 const std::vector<ClientId>& client_ids, |
| 95 int* count) { |
| 96 DCHECK(count); |
| 97 // If you create a transaction but don't Commit() it is automatically |
| 98 // rolled back by its destructor when it falls out of scope. |
| 99 sql::Transaction transaction(db); |
| 100 if (!transaction.Begin()) |
| 101 return false; |
| 102 |
| 103 *count = 0; |
| 104 for (const auto& client_id : client_ids) { |
| 105 if (!DeleteRequestByClientId(db, client_id)) |
| 106 return false; |
| 107 *count += db->GetLastChangeCount(); |
| 108 } |
| 109 |
| 110 if (!transaction.Commit()) |
| 111 return false; |
| 112 |
| 113 return true; |
| 114 } |
| 115 |
| 81 // Create a save page request from a SQL result. Expects complete rows with | 116 // Create a save page request from a SQL result. Expects complete rows with |
| 82 // all columns present. Columns are in order they are defined in select query | 117 // all columns present. Columns are in order they are defined in select query |
| 83 // in |RequestQueueStore::RequestSync| method. | 118 // in |RequestQueueStore::RequestSync| method. |
| 84 SavePageRequest MakeSavePageRequest(const sql::Statement& statement) { | 119 SavePageRequest MakeSavePageRequest(const sql::Statement& statement) { |
| 85 const int64_t id = statement.ColumnInt64(0); | 120 const int64_t id = statement.ColumnInt64(0); |
| 86 const base::Time creation_time = | 121 const base::Time creation_time = |
| 87 base::Time::FromInternalValue(statement.ColumnInt64(1)); | 122 base::Time::FromInternalValue(statement.ColumnInt64(1)); |
| 88 const base::Time activation_time = | 123 const base::Time activation_time = |
| 89 base::Time::FromInternalValue(statement.ColumnInt64(2)); | 124 base::Time::FromInternalValue(statement.ColumnInt64(2)); |
| 90 const base::Time last_attempt_time = | 125 const base::Time last_attempt_time = |
| 91 base::Time::FromInternalValue(statement.ColumnInt64(3)); | 126 base::Time::FromInternalValue(statement.ColumnInt64(3)); |
| 92 const int64_t last_attempt_count = statement.ColumnInt64(4); | 127 const int64_t last_attempt_count = statement.ColumnInt64(4); |
| 93 const GURL url(statement.ColumnString(5)); | 128 const GURL url(statement.ColumnString(5)); |
| 94 const ClientId client_id(statement.ColumnString(6), | 129 const ClientId client_id(statement.ColumnString(6), |
| 95 statement.ColumnString(7)); | 130 statement.ColumnString(7)); |
| 96 | 131 |
| 132 DVLOG(2) << "making save page request - id " << id << " url " << url |
| 133 << " client_id " << client_id.name_space << "-" << client_id.id |
| 134 << " creation time " << creation_time << " user requested " |
| 135 << kUserRequested; |
| 136 |
| 97 SavePageRequest request( | 137 SavePageRequest request( |
| 98 id, url, client_id, creation_time, activation_time, kUserRequested); | 138 id, url, client_id, creation_time, activation_time, kUserRequested); |
| 99 request.set_last_attempt_time(last_attempt_time); | 139 request.set_last_attempt_time(last_attempt_time); |
| 100 request.set_attempt_count(last_attempt_count); | 140 request.set_attempt_count(last_attempt_count); |
| 101 return request; | 141 return request; |
| 102 } | 142 } |
| 103 | 143 |
| 104 RequestQueueStore::UpdateStatus InsertOrReplace( | 144 RequestQueueStore::UpdateStatus InsertOrReplace( |
| 105 sql::Connection* db, | 145 sql::Connection* db, |
| 106 const SavePageRequest& request) { | 146 const SavePageRequest& request) { |
| (...skipping 103 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 210 const RemoveCallback& callback) { | 250 const RemoveCallback& callback) { |
| 211 // TODO(fgorski): add UMA metrics here. | 251 // TODO(fgorski): add UMA metrics here. |
| 212 int count = 0; | 252 int count = 0; |
| 213 if (DeleteRequestsByIds(db, request_ids, &count)) | 253 if (DeleteRequestsByIds(db, request_ids, &count)) |
| 214 runner->PostTask(FROM_HERE, base::Bind(callback, true, count)); | 254 runner->PostTask(FROM_HERE, base::Bind(callback, true, count)); |
| 215 else | 255 else |
| 216 runner->PostTask(FROM_HERE, base::Bind(callback, false, 0)); | 256 runner->PostTask(FROM_HERE, base::Bind(callback, false, 0)); |
| 217 } | 257 } |
| 218 | 258 |
| 219 // static | 259 // static |
| 260 void RequestQueueStoreSQL::RemoveRequestsByClientIdSync( |
| 261 sql::Connection* db, |
| 262 scoped_refptr<base::SingleThreadTaskRunner> runner, |
| 263 const std::vector<ClientId>& client_ids, |
| 264 const RemoveCallback& callback) { |
| 265 // TODO(fgorski): add UMA metrics here. |
| 266 int count = 0; |
| 267 if (DeleteRequestsByClientIds(db, client_ids, &count)) |
| 268 runner->PostTask(FROM_HERE, base::Bind(callback, true, count)); |
| 269 else |
| 270 runner->PostTask(FROM_HERE, base::Bind(callback, false, 0)); |
| 271 } |
| 272 |
| 273 // static |
| 220 void RequestQueueStoreSQL::ResetSync( | 274 void RequestQueueStoreSQL::ResetSync( |
| 221 sql::Connection* db, | 275 sql::Connection* db, |
| 222 const base::FilePath& db_file_path, | 276 const base::FilePath& db_file_path, |
| 223 scoped_refptr<base::SingleThreadTaskRunner> runner, | 277 scoped_refptr<base::SingleThreadTaskRunner> runner, |
| 224 const ResetCallback& callback) { | 278 const ResetCallback& callback) { |
| 225 // This method deletes the content of the whole store and reinitializes it. | 279 // This method deletes the content of the whole store and reinitializes it. |
| 226 bool success = db->Raze(); | 280 bool success = db->Raze(); |
| 227 db->Close(); | 281 db->Close(); |
| 228 if (success) | 282 if (success) |
| 229 success = InitDatabase(db, db_file_path); | 283 success = InitDatabase(db, db_file_path); |
| (...skipping 25 matching lines...) Expand all Loading... |
| 255 FROM_HERE, base::Bind(callback, UpdateStatus::FAILED)); | 309 FROM_HERE, base::Bind(callback, UpdateStatus::FAILED)); |
| 256 return; | 310 return; |
| 257 } | 311 } |
| 258 | 312 |
| 259 background_task_runner_->PostTask( | 313 background_task_runner_->PostTask( |
| 260 FROM_HERE, | 314 FROM_HERE, |
| 261 base::Bind(&RequestQueueStoreSQL::AddOrUpdateRequestSync, db_.get(), | 315 base::Bind(&RequestQueueStoreSQL::AddOrUpdateRequestSync, db_.get(), |
| 262 base::ThreadTaskRunnerHandle::Get(), request, callback)); | 316 base::ThreadTaskRunnerHandle::Get(), request, callback)); |
| 263 } | 317 } |
| 264 | 318 |
| 319 // TODO(petewil): This is unused, since request_coordinator doesn't keep |
| 320 // request_ids, and neither do clients. Plan is to remove this API in a future |
| 321 // changelist. If we do discover a need to keep it, name it |
| 322 // RemoveRequestsByRequestId to be more parallell with RemoveRequestsByClientId. |
| 265 void RequestQueueStoreSQL::RemoveRequests( | 323 void RequestQueueStoreSQL::RemoveRequests( |
| 266 const std::vector<int64_t>& request_ids, | 324 const std::vector<int64_t>& request_ids, |
| 267 const RemoveCallback& callback) { | 325 const RemoveCallback& callback) { |
| 268 DCHECK(db_.get()); | 326 DCHECK(db_.get()); |
| 269 if (!db_.get()) { | 327 if (!db_.get()) { |
| 270 // Nothing to do, but post a callback instead of calling directly | 328 // Nothing to do, but post a callback instead of calling directly |
| 271 // to preserve the async style behavior to prevent bugs. | 329 // to preserve the async style behavior to prevent bugs. |
| 272 base::ThreadTaskRunnerHandle::Get()->PostTask( | 330 base::ThreadTaskRunnerHandle::Get()->PostTask( |
| 273 FROM_HERE, base::Bind(callback, false, 0)); | 331 FROM_HERE, base::Bind(callback, false, 0)); |
| 274 return; | 332 return; |
| 275 } | 333 } |
| 276 | 334 |
| 277 background_task_runner_->PostTask( | 335 background_task_runner_->PostTask( |
| 278 FROM_HERE, | 336 FROM_HERE, |
| 279 base::Bind(&RequestQueueStoreSQL::RemoveRequestsSync, db_.get(), | 337 base::Bind(&RequestQueueStoreSQL::RemoveRequestsSync, db_.get(), |
| 280 base::ThreadTaskRunnerHandle::Get(), request_ids, callback)); | 338 base::ThreadTaskRunnerHandle::Get(), request_ids, callback)); |
| 281 } | 339 } |
| 282 | 340 |
| 341 void RequestQueueStoreSQL::RemoveRequestsByClientId( |
| 342 const std::vector<ClientId>& client_ids, |
| 343 const RemoveCallback& callback) { |
| 344 DCHECK(db_.get()); |
| 345 if (!db_.get()) { |
| 346 // Nothing to do, but post a callback instead of calling directly |
| 347 // to preserve the async style behavior to prevent bugs. |
| 348 base::ThreadTaskRunnerHandle::Get()->PostTask( |
| 349 FROM_HERE, base::Bind(callback, false, 0)); |
| 350 return; |
| 351 } |
| 352 |
| 353 background_task_runner_->PostTask( |
| 354 FROM_HERE, |
| 355 base::Bind(&RequestQueueStoreSQL::RemoveRequestsByClientIdSync, db_.get(), |
| 356 base::ThreadTaskRunnerHandle::Get(), client_ids, callback)); |
| 357 } |
| 358 |
| 283 void RequestQueueStoreSQL::Reset(const ResetCallback& callback) { | 359 void RequestQueueStoreSQL::Reset(const ResetCallback& callback) { |
| 284 DCHECK(db_.get()); | 360 DCHECK(db_.get()); |
| 285 if (!db_.get()) { | 361 if (!db_.get()) { |
| 286 // Nothing to do, but post a callback instead of calling directly | 362 // Nothing to do, but post a callback instead of calling directly |
| 287 // to preserve the async style behavior to prevent bugs. | 363 // to preserve the async style behavior to prevent bugs. |
| 288 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, | 364 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, |
| 289 base::Bind(callback, false)); | 365 base::Bind(callback, false)); |
| 290 return; | 366 return; |
| 291 } | 367 } |
| 292 | 368 |
| (...skipping 25 matching lines...) Expand all Loading... |
| 318 } | 394 } |
| 319 | 395 |
| 320 void RequestQueueStoreSQL::OnResetDone(const ResetCallback& callback, | 396 void RequestQueueStoreSQL::OnResetDone(const ResetCallback& callback, |
| 321 bool success) { | 397 bool success) { |
| 322 // Complete connection initialization post reset. | 398 // Complete connection initialization post reset. |
| 323 OnOpenConnectionDone(success); | 399 OnOpenConnectionDone(success); |
| 324 callback.Run(success); | 400 callback.Run(success); |
| 325 } | 401 } |
| 326 | 402 |
| 327 } // namespace offline_pages | 403 } // namespace offline_pages |
| OLD | NEW |