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