Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(414)

Side by Side Diff: components/offline_pages/background/request_queue_store_sql.cc

Issue 2221323003: Add an API to Pause and Resume background offlining requests. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: CR feedback per Dimich Created 4 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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
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 ChangeRequestState(sql::Connection* db,
81 const int64_t request_id,
82 const SavePageRequest::RequestState new_state) {
83 const char kSql[] = "UPDATE " REQUEST_QUEUE_TABLE_NAME
84 " SET state=?"
85 " WHERE request_id=?";
86 sql::Statement statement(db->GetCachedStatement(SQL_FROM_HERE, kSql));
87 statement.BindInt64(0, static_cast<long>(new_state));
fgorski 2016/08/10 16:00:09 please update the cast to type to int64_t. This wi
Pete Williamson 2016/08/10 21:38:01 Done.
88 statement.BindInt64(1, request_id);
89 return statement.Run();
90 }
91
80 bool DeleteRequestsByIds(sql::Connection* db, 92 bool DeleteRequestsByIds(sql::Connection* db,
81 const std::vector<int64_t>& request_ids, 93 const std::vector<int64_t>& request_ids,
82 int* count) { 94 int* count) {
83 DCHECK(count); 95 DCHECK(count);
84 // If you create a transaction but don't Commit() it is automatically 96 // If you create a transaction but don't Commit() it is automatically
85 // rolled back by its destructor when it falls out of scope. 97 // rolled back by its destructor when it falls out of scope.
86 sql::Transaction transaction(db); 98 sql::Transaction transaction(db);
87 if (!transaction.Begin()) 99 if (!transaction.Begin())
88 return false; 100 return false;
89 101
(...skipping 26 matching lines...) Expand all
116 return false; 128 return false;
117 *count += db->GetLastChangeCount(); 129 *count += db->GetLastChangeCount();
118 } 130 }
119 131
120 if (!transaction.Commit()) 132 if (!transaction.Commit())
121 return false; 133 return false;
122 134
123 return true; 135 return true;
124 } 136 }
125 137
138 RequestQueueStore::UpdateStatus ChangeRequestsState(
139 sql::Connection* db,
140 const std::vector<int64_t>& request_ids,
141 SavePageRequest::RequestState new_state) {
142 // If you create a transaction but don't Commit() it is automatically
143 // rolled back by its destructor when it falls out of scope.
144 sql::Transaction transaction(db);
145 if (!transaction.Begin())
146 return RequestQueueStore::UpdateStatus::FAILED;
147 for (const auto& request_id : request_ids) {
148 if (!ChangeRequestState(db, request_id, new_state))
149 return RequestQueueStore::UpdateStatus::FAILED;
150 }
151
152 if (!transaction.Commit())
153 return RequestQueueStore::UpdateStatus::FAILED;
154
155 return RequestQueueStore::UpdateStatus::UPDATED;
156 }
157
126 // Create a save page request from a SQL result. Expects complete rows with 158 // 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 159 // all columns present. Columns are in order they are defined in select query
128 // in |RequestQueueStore::RequestSync| method. 160 // in |RequestQueueStore::RequestSync| method.
129 SavePageRequest MakeSavePageRequest(const sql::Statement& statement) { 161 SavePageRequest MakeSavePageRequest(const sql::Statement& statement) {
130 const int64_t id = statement.ColumnInt64(0); 162 const int64_t id = statement.ColumnInt64(0);
131 const base::Time creation_time = 163 const base::Time creation_time =
132 base::Time::FromInternalValue(statement.ColumnInt64(1)); 164 base::Time::FromInternalValue(statement.ColumnInt64(1));
133 const base::Time activation_time = 165 const base::Time activation_time =
134 base::Time::FromInternalValue(statement.ColumnInt64(2)); 166 base::Time::FromInternalValue(statement.ColumnInt64(2));
135 const base::Time last_attempt_time = 167 const base::Time last_attempt_time =
(...skipping 147 matching lines...) Expand 10 before | Expand all | Expand 10 after
283 const RemoveCallback& callback) { 315 const RemoveCallback& callback) {
284 // TODO(fgorski): add UMA metrics here. 316 // TODO(fgorski): add UMA metrics here.
285 int count = 0; 317 int count = 0;
286 if (DeleteRequestsByClientIds(db, client_ids, &count)) 318 if (DeleteRequestsByClientIds(db, client_ids, &count))
287 runner->PostTask(FROM_HERE, base::Bind(callback, true, count)); 319 runner->PostTask(FROM_HERE, base::Bind(callback, true, count));
288 else 320 else
289 runner->PostTask(FROM_HERE, base::Bind(callback, false, 0)); 321 runner->PostTask(FROM_HERE, base::Bind(callback, false, 0));
290 } 322 }
291 323
292 // static 324 // static
325 void RequestQueueStoreSQL::ChangeRequestsStateSync(
326 sql::Connection* db,
327 scoped_refptr<base::SingleThreadTaskRunner> runner,
328 const std::vector<int64_t>& request_ids,
329 const SavePageRequest::RequestState new_state,
330 const UpdateCallback& callback) {
331 // TODO(fgorski): add UMA metrics here.
332 RequestQueueStore::UpdateStatus status =
333 offline_pages::ChangeRequestsState(db, request_ids, new_state);
334 runner->PostTask(FROM_HERE, base::Bind(callback, status));
335 }
336
337 // static
293 void RequestQueueStoreSQL::ResetSync( 338 void RequestQueueStoreSQL::ResetSync(
294 sql::Connection* db, 339 sql::Connection* db,
295 const base::FilePath& db_file_path, 340 const base::FilePath& db_file_path,
296 scoped_refptr<base::SingleThreadTaskRunner> runner, 341 scoped_refptr<base::SingleThreadTaskRunner> runner,
297 const ResetCallback& callback) { 342 const ResetCallback& callback) {
298 // This method deletes the content of the whole store and reinitializes it. 343 // This method deletes the content of the whole store and reinitializes it.
299 bool success = db->Raze(); 344 bool success = db->Raze();
300 db->Close(); 345 db->Close();
301 if (success) 346 if (success)
302 success = InitDatabase(db, db_file_path); 347 success = InitDatabase(db, db_file_path);
303 runner->PostTask(FROM_HERE, base::Bind(callback, success)); 348 runner->PostTask(FROM_HERE, base::Bind(callback, success));
304 } 349 }
305 350
351 bool RequestQueueStoreSQL::CheckDb(const base::Closure& callback) {
352 DCHECK(db_.get());
353 if (!db_.get()) {
354 // Nothing to do, but post a callback instead of calling directly
355 // to preserve the async style behavior to prevent bugs.
356 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE,
357 base::Bind(callback));
358 return false;
359 }
360 return true;
361 }
362
306 void RequestQueueStoreSQL::GetRequests(const GetRequestsCallback& callback) { 363 void RequestQueueStoreSQL::GetRequests(const GetRequestsCallback& callback) {
307 DCHECK(db_.get()); 364 DCHECK(db_.get());
308 if (!db_.get()) { 365 if (!CheckDb(base::Bind(callback, false, std::vector<SavePageRequest>())))
309 // Nothing to do, but post a callback instead of calling directly
310 // to preserve the async style behavior to prevent bugs.
311 base::ThreadTaskRunnerHandle::Get()->PostTask(
312 FROM_HERE, base::Bind(callback, false, std::vector<SavePageRequest>()));
313 return; 366 return;
314 }
315 367
316 background_task_runner_->PostTask( 368 background_task_runner_->PostTask(
317 FROM_HERE, base::Bind(&RequestQueueStoreSQL::GetRequestsSync, db_.get(), 369 FROM_HERE, base::Bind(&RequestQueueStoreSQL::GetRequestsSync, db_.get(),
318 base::ThreadTaskRunnerHandle::Get(), callback)); 370 base::ThreadTaskRunnerHandle::Get(), callback));
319 } 371 }
320 372
321 void RequestQueueStoreSQL::AddOrUpdateRequest(const SavePageRequest& request, 373 void RequestQueueStoreSQL::AddOrUpdateRequest(const SavePageRequest& request,
322 const UpdateCallback& callback) { 374 const UpdateCallback& callback) {
323 DCHECK(db_.get()); 375 DCHECK(db_.get());
324 if (!db_.get()) { 376 if (!CheckDb(base::Bind(callback, UpdateStatus::FAILED)))
325 // Nothing to do, but post a callback instead of calling directly
326 // to preserve the async style behavior to prevent bugs.
327 base::ThreadTaskRunnerHandle::Get()->PostTask(
328 FROM_HERE, base::Bind(callback, UpdateStatus::FAILED));
329 return; 377 return;
330 }
331 378
332 background_task_runner_->PostTask( 379 background_task_runner_->PostTask(
333 FROM_HERE, 380 FROM_HERE,
334 base::Bind(&RequestQueueStoreSQL::AddOrUpdateRequestSync, db_.get(), 381 base::Bind(&RequestQueueStoreSQL::AddOrUpdateRequestSync, db_.get(),
335 base::ThreadTaskRunnerHandle::Get(), request, callback)); 382 base::ThreadTaskRunnerHandle::Get(), request, callback));
336 } 383 }
337 384
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( 385 void RequestQueueStoreSQL::RemoveRequests(
343 const std::vector<int64_t>& request_ids, 386 const std::vector<int64_t>& request_ids,
344 const RemoveCallback& callback) { 387 const RemoveCallback& callback) {
345 DCHECK(db_.get()); 388 if (!CheckDb(base::Bind(callback, false, 0)))
346 if (!db_.get()) {
347 // Nothing to do, but post a callback instead of calling directly
348 // to preserve the async style behavior to prevent bugs.
349 base::ThreadTaskRunnerHandle::Get()->PostTask(
350 FROM_HERE, base::Bind(callback, false, 0));
351 return; 389 return;
352 }
353 390
354 background_task_runner_->PostTask( 391 background_task_runner_->PostTask(
355 FROM_HERE, 392 FROM_HERE,
356 base::Bind(&RequestQueueStoreSQL::RemoveRequestsSync, db_.get(), 393 base::Bind(&RequestQueueStoreSQL::RemoveRequestsSync, db_.get(),
357 base::ThreadTaskRunnerHandle::Get(), request_ids, callback)); 394 base::ThreadTaskRunnerHandle::Get(), request_ids, callback));
358 } 395 }
359 396
360 void RequestQueueStoreSQL::RemoveRequestsByClientId( 397 void RequestQueueStoreSQL::RemoveRequestsByClientId(
361 const std::vector<ClientId>& client_ids, 398 const std::vector<ClientId>& client_ids,
362 const RemoveCallback& callback) { 399 const RemoveCallback& callback) {
363 DCHECK(db_.get()); 400 DCHECK(db_.get());
364 if (!db_.get()) { 401 if (!db_.get()) {
fgorski 2016/08/10 16:00:09 CheckDB please.
Pete Williamson 2016/08/10 21:38:00 This whole function will be removed in a changelis
fgorski 2016/08/10 21:49:16 Acknowledged.
365 // Nothing to do, but post a callback instead of calling directly 402 // Nothing to do, but post a callback instead of calling directly
366 // to preserve the async style behavior to prevent bugs. 403 // to preserve the async style behavior to prevent bugs.
367 base::ThreadTaskRunnerHandle::Get()->PostTask( 404 base::ThreadTaskRunnerHandle::Get()->PostTask(
368 FROM_HERE, base::Bind(callback, false, 0)); 405 FROM_HERE, base::Bind(callback, false, 0));
369 return; 406 return;
370 } 407 }
371 408
372 background_task_runner_->PostTask( 409 background_task_runner_->PostTask(
373 FROM_HERE, 410 FROM_HERE,
374 base::Bind(&RequestQueueStoreSQL::RemoveRequestsByClientIdSync, db_.get(), 411 base::Bind(&RequestQueueStoreSQL::RemoveRequestsByClientIdSync, db_.get(),
375 base::ThreadTaskRunnerHandle::Get(), client_ids, callback)); 412 base::ThreadTaskRunnerHandle::Get(), client_ids, callback));
376 } 413 }
377 414
415 void RequestQueueStoreSQL::ChangeRequestsState(
416 const std::vector<int64_t>& request_ids,
417 const SavePageRequest::RequestState new_state,
418 const UpdateCallback& callback) {
419 if (!CheckDb(base::Bind(callback, UpdateStatus::FAILED)))
420 return;
421
422 background_task_runner_->PostTask(
423 FROM_HERE, base::Bind(&RequestQueueStoreSQL::ChangeRequestsStateSync,
424 db_.get(), base::ThreadTaskRunnerHandle::Get(),
425 request_ids, new_state, callback));
426 }
427
378 void RequestQueueStoreSQL::Reset(const ResetCallback& callback) { 428 void RequestQueueStoreSQL::Reset(const ResetCallback& callback) {
379 DCHECK(db_.get()); 429 DCHECK(db_.get());
380 if (!db_.get()) { 430 if (!CheckDb(base::Bind(callback, false)))
381 // Nothing to do, but post a callback instead of calling directly
382 // to preserve the async style behavior to prevent bugs.
383 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE,
384 base::Bind(callback, false));
385 return; 431 return;
386 }
387 432
388 background_task_runner_->PostTask( 433 background_task_runner_->PostTask(
389 FROM_HERE, 434 FROM_HERE,
390 base::Bind(&RequestQueueStoreSQL::ResetSync, db_.get(), db_file_path_, 435 base::Bind(&RequestQueueStoreSQL::ResetSync, db_.get(), db_file_path_,
391 base::ThreadTaskRunnerHandle::Get(), 436 base::ThreadTaskRunnerHandle::Get(),
392 base::Bind(&RequestQueueStoreSQL::OnResetDone, 437 base::Bind(&RequestQueueStoreSQL::OnResetDone,
393 weak_ptr_factory_.GetWeakPtr(), callback))); 438 weak_ptr_factory_.GetWeakPtr(), callback)));
394 } 439 }
395 440
396 void RequestQueueStoreSQL::OpenConnection() { 441 void RequestQueueStoreSQL::OpenConnection() {
(...skipping 16 matching lines...) Expand all
413 } 458 }
414 459
415 void RequestQueueStoreSQL::OnResetDone(const ResetCallback& callback, 460 void RequestQueueStoreSQL::OnResetDone(const ResetCallback& callback,
416 bool success) { 461 bool success) {
417 // Complete connection initialization post reset. 462 // Complete connection initialization post reset.
418 OnOpenConnectionDone(success); 463 OnOpenConnectionDone(success);
419 callback.Run(success); 464 callback.Run(success);
420 } 465 }
421 466
422 } // namespace offline_pages 467 } // namespace offline_pages
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698