| 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 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 59 } | 59 } |
| 60 | 60 |
| 61 bool DeleteRequestById(sql::Connection* db, int64_t request_id) { | 61 bool DeleteRequestById(sql::Connection* db, int64_t request_id) { |
| 62 const char kSql[] = | 62 const char kSql[] = |
| 63 "DELETE FROM " REQUEST_QUEUE_TABLE_NAME " WHERE request_id=?"; | 63 "DELETE FROM " REQUEST_QUEUE_TABLE_NAME " WHERE request_id=?"; |
| 64 sql::Statement statement(db->GetCachedStatement(SQL_FROM_HERE, kSql)); | 64 sql::Statement statement(db->GetCachedStatement(SQL_FROM_HERE, kSql)); |
| 65 statement.BindInt64(0, request_id); | 65 statement.BindInt64(0, request_id); |
| 66 return statement.Run(); | 66 return statement.Run(); |
| 67 } | 67 } |
| 68 | 68 |
| 69 bool DeleteRequestByClientId(sql::Connection* db, const ClientId& client_id) { | |
| 70 const char kSql[] = "DELETE FROM " REQUEST_QUEUE_TABLE_NAME | |
| 71 " WHERE client_namespace=? AND client_id=?"; | |
| 72 sql::Statement statement(db->GetCachedStatement(SQL_FROM_HERE, kSql)); | |
| 73 statement.BindString(0, client_id.name_space); | |
| 74 statement.BindString(1, client_id.id); | |
| 75 DVLOG(2) << kSql << " client_namespace " << client_id.name_space | |
| 76 << " client_id " << client_id.id; | |
| 77 return statement.Run(); | |
| 78 } | |
| 79 | |
| 80 bool DeleteRequestsByIds(sql::Connection* db, | 69 bool DeleteRequestsByIds(sql::Connection* db, |
| 81 const std::vector<int64_t>& request_ids, | 70 const std::vector<int64_t>& request_ids, |
| 82 int* count) { | 71 int* count) { |
| 83 DCHECK(count); | 72 DCHECK(count); |
| 84 // If you create a transaction but don't Commit() it is automatically | 73 // If you create a transaction but don't Commit() it is automatically |
| 85 // rolled back by its destructor when it falls out of scope. | 74 // rolled back by its destructor when it falls out of scope. |
| 86 sql::Transaction transaction(db); | 75 sql::Transaction transaction(db); |
| 87 if (!transaction.Begin()) | 76 if (!transaction.Begin()) |
| 88 return false; | 77 return false; |
| 89 | 78 |
| 90 *count = 0; | 79 *count = 0; |
| 91 for (auto request_id : request_ids) { | 80 for (auto request_id : request_ids) { |
| 92 if (!DeleteRequestById(db, request_id)) | 81 if (!DeleteRequestById(db, request_id)) |
| 93 return false; | 82 return false; |
| 94 *count += db->GetLastChangeCount(); | 83 *count += db->GetLastChangeCount(); |
| 95 } | 84 } |
| 96 | 85 |
| 97 if (!transaction.Commit()) | 86 if (!transaction.Commit()) |
| 98 return false; | 87 return false; |
| 99 | 88 |
| 100 return true; | 89 return true; |
| 101 } | 90 } |
| 102 | 91 |
| 103 bool DeleteRequestsByClientIds(sql::Connection* db, | |
| 104 const std::vector<ClientId>& client_ids, | |
| 105 int* count) { | |
| 106 DCHECK(count); | |
| 107 // If you create a transaction but don't Commit() it is automatically | |
| 108 // rolled back by its destructor when it falls out of scope. | |
| 109 sql::Transaction transaction(db); | |
| 110 if (!transaction.Begin()) | |
| 111 return false; | |
| 112 | |
| 113 *count = 0; | |
| 114 for (const auto& client_id : client_ids) { | |
| 115 if (!DeleteRequestByClientId(db, client_id)) | |
| 116 return false; | |
| 117 *count += db->GetLastChangeCount(); | |
| 118 } | |
| 119 | |
| 120 if (!transaction.Commit()) | |
| 121 return false; | |
| 122 | |
| 123 return true; | |
| 124 } | |
| 125 | |
| 126 // Create a save page request from a SQL result. Expects complete rows with | 92 // 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 | 93 // all columns present. Columns are in order they are defined in select query |
| 128 // in |RequestQueueStore::RequestSync| method. | 94 // in |RequestQueueStore::RequestSync| method. |
| 129 SavePageRequest MakeSavePageRequest(const sql::Statement& statement) { | 95 SavePageRequest MakeSavePageRequest(const sql::Statement& statement) { |
| 130 const int64_t id = statement.ColumnInt64(0); | 96 const int64_t id = statement.ColumnInt64(0); |
| 131 const base::Time creation_time = | 97 const base::Time creation_time = |
| 132 base::Time::FromInternalValue(statement.ColumnInt64(1)); | 98 base::Time::FromInternalValue(statement.ColumnInt64(1)); |
| 133 const base::Time activation_time = | 99 const base::Time activation_time = |
| 134 base::Time::FromInternalValue(statement.ColumnInt64(2)); | 100 base::Time::FromInternalValue(statement.ColumnInt64(2)); |
| 135 const base::Time last_attempt_time = | 101 const base::Time last_attempt_time = |
| (...skipping 133 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 269 const RemoveCallback& callback) { | 235 const RemoveCallback& callback) { |
| 270 // TODO(fgorski): add UMA metrics here. | 236 // TODO(fgorski): add UMA metrics here. |
| 271 int count = 0; | 237 int count = 0; |
| 272 if (DeleteRequestsByIds(db, request_ids, &count)) | 238 if (DeleteRequestsByIds(db, request_ids, &count)) |
| 273 runner->PostTask(FROM_HERE, base::Bind(callback, true, count)); | 239 runner->PostTask(FROM_HERE, base::Bind(callback, true, count)); |
| 274 else | 240 else |
| 275 runner->PostTask(FROM_HERE, base::Bind(callback, false, 0)); | 241 runner->PostTask(FROM_HERE, base::Bind(callback, false, 0)); |
| 276 } | 242 } |
| 277 | 243 |
| 278 // static | 244 // static |
| 279 void RequestQueueStoreSQL::RemoveRequestsByClientIdSync( | |
| 280 sql::Connection* db, | |
| 281 scoped_refptr<base::SingleThreadTaskRunner> runner, | |
| 282 const std::vector<ClientId>& client_ids, | |
| 283 const RemoveCallback& callback) { | |
| 284 // TODO(fgorski): add UMA metrics here. | |
| 285 int count = 0; | |
| 286 if (DeleteRequestsByClientIds(db, client_ids, &count)) | |
| 287 runner->PostTask(FROM_HERE, base::Bind(callback, true, count)); | |
| 288 else | |
| 289 runner->PostTask(FROM_HERE, base::Bind(callback, false, 0)); | |
| 290 } | |
| 291 | |
| 292 // static | |
| 293 void RequestQueueStoreSQL::ResetSync( | 245 void RequestQueueStoreSQL::ResetSync( |
| 294 sql::Connection* db, | 246 sql::Connection* db, |
| 295 const base::FilePath& db_file_path, | 247 const base::FilePath& db_file_path, |
| 296 scoped_refptr<base::SingleThreadTaskRunner> runner, | 248 scoped_refptr<base::SingleThreadTaskRunner> runner, |
| 297 const ResetCallback& callback) { | 249 const ResetCallback& callback) { |
| 298 // This method deletes the content of the whole store and reinitializes it. | 250 // This method deletes the content of the whole store and reinitializes it. |
| 299 bool success = db->Raze(); | 251 bool success = db->Raze(); |
| 300 db->Close(); | 252 db->Close(); |
| 301 if (success) | 253 if (success) |
| 302 success = InitDatabase(db, db_file_path); | 254 success = InitDatabase(db, db_file_path); |
| (...skipping 25 matching lines...) Expand all Loading... |
| 328 FROM_HERE, base::Bind(callback, UpdateStatus::FAILED)); | 280 FROM_HERE, base::Bind(callback, UpdateStatus::FAILED)); |
| 329 return; | 281 return; |
| 330 } | 282 } |
| 331 | 283 |
| 332 background_task_runner_->PostTask( | 284 background_task_runner_->PostTask( |
| 333 FROM_HERE, | 285 FROM_HERE, |
| 334 base::Bind(&RequestQueueStoreSQL::AddOrUpdateRequestSync, db_.get(), | 286 base::Bind(&RequestQueueStoreSQL::AddOrUpdateRequestSync, db_.get(), |
| 335 base::ThreadTaskRunnerHandle::Get(), request, callback)); | 287 base::ThreadTaskRunnerHandle::Get(), request, callback)); |
| 336 } | 288 } |
| 337 | 289 |
| 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. | 290 // RemoveRequestsByRequestId to be more parallell with RemoveRequestsByClientId. |
| 342 void RequestQueueStoreSQL::RemoveRequests( | 291 void RequestQueueStoreSQL::RemoveRequests( |
| 343 const std::vector<int64_t>& request_ids, | 292 const std::vector<int64_t>& request_ids, |
| 344 const RemoveCallback& callback) { | 293 const RemoveCallback& callback) { |
| 345 DCHECK(db_.get()); | 294 DCHECK(db_.get()); |
| 346 if (!db_.get()) { | 295 if (!db_.get()) { |
| 347 // Nothing to do, but post a callback instead of calling directly | 296 // Nothing to do, but post a callback instead of calling directly |
| 348 // to preserve the async style behavior to prevent bugs. | 297 // to preserve the async style behavior to prevent bugs. |
| 349 base::ThreadTaskRunnerHandle::Get()->PostTask( | 298 base::ThreadTaskRunnerHandle::Get()->PostTask( |
| 350 FROM_HERE, base::Bind(callback, false, 0)); | 299 FROM_HERE, base::Bind(callback, false, 0)); |
| 351 return; | 300 return; |
| 352 } | 301 } |
| 353 | 302 |
| 354 background_task_runner_->PostTask( | 303 background_task_runner_->PostTask( |
| 355 FROM_HERE, | 304 FROM_HERE, |
| 356 base::Bind(&RequestQueueStoreSQL::RemoveRequestsSync, db_.get(), | 305 base::Bind(&RequestQueueStoreSQL::RemoveRequestsSync, db_.get(), |
| 357 base::ThreadTaskRunnerHandle::Get(), request_ids, callback)); | 306 base::ThreadTaskRunnerHandle::Get(), request_ids, callback)); |
| 358 } | 307 } |
| 359 | 308 |
| 360 void RequestQueueStoreSQL::RemoveRequestsByClientId( | |
| 361 const std::vector<ClientId>& client_ids, | |
| 362 const RemoveCallback& callback) { | |
| 363 DCHECK(db_.get()); | |
| 364 if (!db_.get()) { | |
| 365 // Nothing to do, but post a callback instead of calling directly | |
| 366 // to preserve the async style behavior to prevent bugs. | |
| 367 base::ThreadTaskRunnerHandle::Get()->PostTask( | |
| 368 FROM_HERE, base::Bind(callback, false, 0)); | |
| 369 return; | |
| 370 } | |
| 371 | |
| 372 background_task_runner_->PostTask( | |
| 373 FROM_HERE, | |
| 374 base::Bind(&RequestQueueStoreSQL::RemoveRequestsByClientIdSync, db_.get(), | |
| 375 base::ThreadTaskRunnerHandle::Get(), client_ids, callback)); | |
| 376 } | |
| 377 | |
| 378 void RequestQueueStoreSQL::Reset(const ResetCallback& callback) { | 309 void RequestQueueStoreSQL::Reset(const ResetCallback& callback) { |
| 379 DCHECK(db_.get()); | 310 DCHECK(db_.get()); |
| 380 if (!db_.get()) { | 311 if (!db_.get()) { |
| 381 // Nothing to do, but post a callback instead of calling directly | 312 // Nothing to do, but post a callback instead of calling directly |
| 382 // to preserve the async style behavior to prevent bugs. | 313 // to preserve the async style behavior to prevent bugs. |
| 383 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, | 314 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, |
| 384 base::Bind(callback, false)); | 315 base::Bind(callback, false)); |
| 385 return; | 316 return; |
| 386 } | 317 } |
| 387 | 318 |
| (...skipping 25 matching lines...) Expand all Loading... |
| 413 } | 344 } |
| 414 | 345 |
| 415 void RequestQueueStoreSQL::OnResetDone(const ResetCallback& callback, | 346 void RequestQueueStoreSQL::OnResetDone(const ResetCallback& callback, |
| 416 bool success) { | 347 bool success) { |
| 417 // Complete connection initialization post reset. | 348 // Complete connection initialization post reset. |
| 418 OnOpenConnectionDone(success); | 349 OnOpenConnectionDone(success); |
| 419 callback.Run(success); | 350 callback.Run(success); |
| 420 } | 351 } |
| 421 | 352 |
| 422 } // namespace offline_pages | 353 } // namespace offline_pages |
| OLD | NEW |