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

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

Issue 2228813003: Changes to fit better with the needs of the download manager. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: CR fixes for 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 48 matching lines...) Expand 10 before | Expand all | Expand 10 after
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) { 69 bool DeleteRequestsByIds(
70 const char kSql[] = "DELETE FROM " REQUEST_QUEUE_TABLE_NAME 70 sql::Connection* db,
71 " WHERE client_namespace=? AND client_id=?"; 71 const std::vector<int64_t>& request_ids,
72 sql::Statement statement(db->GetCachedStatement(SQL_FROM_HERE, kSql)); 72 std::vector<RequestQueue::UpdateRequestResult>& results) {
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,
81 const std::vector<int64_t>& request_ids,
82 int* count) {
83 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()) {
77 for (size_t i = 0; i < request_ids.size(); ++i)
78 results.push_back(RequestQueue::UpdateRequestResult::STORE_FAILURE);
88 return false; 79 return false;
89
90 *count = 0;
91 for (auto request_id : request_ids) {
92 if (!DeleteRequestById(db, request_id))
93 return false;
94 *count += db->GetLastChangeCount();
95 } 80 }
96 81
97 if (!transaction.Commit()) 82 for (auto request_id : request_ids) {
83 if (DeleteRequestById(db, request_id))
84 results.push_back(RequestQueue::UpdateRequestResult::SUCCESS);
85 else
86 results.push_back(
87 RequestQueue::UpdateRequestResult::REQUEST_DOES_NOT_EXIST);
88 }
89
90 if (!transaction.Commit()) {
91 results.clear();
92 for (size_t i = 0; i < request_ids.size(); ++i)
93 results.push_back(RequestQueue::UpdateRequestResult::STORE_FAILURE);
98 return false; 94 return false;
95 }
99 96
100 return true; 97 return true;
101 } 98 }
102 99
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 100 // 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 101 // all columns present. Columns are in order they are defined in select query
128 // in |RequestQueueStore::RequestSync| method. 102 // in |RequestQueueStore::RequestSync| method.
129 SavePageRequest MakeSavePageRequest(const sql::Statement& statement) { 103 SavePageRequest MakeSavePageRequest(const sql::Statement& statement) {
130 const int64_t id = statement.ColumnInt64(0); 104 const int64_t id = statement.ColumnInt64(0);
131 const base::Time creation_time = 105 const base::Time creation_time =
132 base::Time::FromInternalValue(statement.ColumnInt64(1)); 106 base::Time::FromInternalValue(statement.ColumnInt64(1));
133 const base::Time activation_time = 107 const base::Time activation_time =
134 base::Time::FromInternalValue(statement.ColumnInt64(2)); 108 base::Time::FromInternalValue(statement.ColumnInt64(2));
135 const base::Time last_attempt_time = 109 const base::Time last_attempt_time =
(...skipping 124 matching lines...) Expand 10 before | Expand all | Expand 10 after
260 RequestQueueStore::UpdateStatus status = InsertOrReplace(db, request); 234 RequestQueueStore::UpdateStatus status = InsertOrReplace(db, request);
261 runner->PostTask(FROM_HERE, base::Bind(callback, status)); 235 runner->PostTask(FROM_HERE, base::Bind(callback, status));
262 } 236 }
263 237
264 // static 238 // static
265 void RequestQueueStoreSQL::RemoveRequestsSync( 239 void RequestQueueStoreSQL::RemoveRequestsSync(
266 sql::Connection* db, 240 sql::Connection* db,
267 scoped_refptr<base::SingleThreadTaskRunner> runner, 241 scoped_refptr<base::SingleThreadTaskRunner> runner,
268 const std::vector<int64_t>& request_ids, 242 const std::vector<int64_t>& request_ids,
269 const RemoveCallback& callback) { 243 const RemoveCallback& callback) {
244 std::vector<RequestQueue::UpdateRequestResult> results;
270 // TODO(fgorski): add UMA metrics here. 245 // TODO(fgorski): add UMA metrics here.
271 int count = 0; 246 DeleteRequestsByIds(db, request_ids, results);
272 if (DeleteRequestsByIds(db, request_ids, &count)) 247 runner->PostTask(FROM_HERE, base::Bind(callback, request_ids, results));
273 runner->PostTask(FROM_HERE, base::Bind(callback, true, count));
274 else
275 runner->PostTask(FROM_HERE, base::Bind(callback, false, 0));
276 } 248 }
277 249
278 // static 250 // 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( 251 void RequestQueueStoreSQL::ResetSync(
294 sql::Connection* db, 252 sql::Connection* db,
295 const base::FilePath& db_file_path, 253 const base::FilePath& db_file_path,
296 scoped_refptr<base::SingleThreadTaskRunner> runner, 254 scoped_refptr<base::SingleThreadTaskRunner> runner,
297 const ResetCallback& callback) { 255 const ResetCallback& callback) {
298 // This method deletes the content of the whole store and reinitializes it. 256 // This method deletes the content of the whole store and reinitializes it.
299 bool success = db->Raze(); 257 bool success = db->Raze();
300 db->Close(); 258 db->Close();
301 if (success) 259 if (success)
302 success = InitDatabase(db, db_file_path); 260 success = InitDatabase(db, db_file_path);
(...skipping 25 matching lines...) Expand all
328 FROM_HERE, base::Bind(callback, UpdateStatus::FAILED)); 286 FROM_HERE, base::Bind(callback, UpdateStatus::FAILED));
329 return; 287 return;
330 } 288 }
331 289
332 background_task_runner_->PostTask( 290 background_task_runner_->PostTask(
333 FROM_HERE, 291 FROM_HERE,
334 base::Bind(&RequestQueueStoreSQL::AddOrUpdateRequestSync, db_.get(), 292 base::Bind(&RequestQueueStoreSQL::AddOrUpdateRequestSync, db_.get(),
335 base::ThreadTaskRunnerHandle::Get(), request, callback)); 293 base::ThreadTaskRunnerHandle::Get(), request, callback));
336 } 294 }
337 295
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. 296 // RemoveRequestsByRequestId to be more parallell with RemoveRequestsByClientId.
342 void RequestQueueStoreSQL::RemoveRequests( 297 void RequestQueueStoreSQL::RemoveRequests(
343 const std::vector<int64_t>& request_ids, 298 const std::vector<int64_t>& request_ids,
344 const RemoveCallback& callback) { 299 const RemoveCallback& callback) {
345 DCHECK(db_.get()); 300 DCHECK(db_.get());
346 if (!db_.get()) { 301 if (!db_.get()) {
302 std::vector<RequestQueue::UpdateRequestResult> statuses;
347 // Nothing to do, but post a callback instead of calling directly 303 // Nothing to do, but post a callback instead of calling directly
348 // to preserve the async style behavior to prevent bugs. 304 // to preserve the async style behavior to prevent bugs.
349 base::ThreadTaskRunnerHandle::Get()->PostTask( 305 base::ThreadTaskRunnerHandle::Get()->PostTask(
350 FROM_HERE, base::Bind(callback, false, 0)); 306 FROM_HERE, base::Bind(callback, request_ids, statuses));
351 return; 307 return;
352 } 308 }
353 309
354 background_task_runner_->PostTask( 310 background_task_runner_->PostTask(
355 FROM_HERE, 311 FROM_HERE,
356 base::Bind(&RequestQueueStoreSQL::RemoveRequestsSync, db_.get(), 312 base::Bind(&RequestQueueStoreSQL::RemoveRequestsSync, db_.get(),
357 base::ThreadTaskRunnerHandle::Get(), request_ids, callback)); 313 base::ThreadTaskRunnerHandle::Get(), request_ids, callback));
358 } 314 }
359 315
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) { 316 void RequestQueueStoreSQL::Reset(const ResetCallback& callback) {
379 DCHECK(db_.get()); 317 DCHECK(db_.get());
380 if (!db_.get()) { 318 if (!db_.get()) {
381 // Nothing to do, but post a callback instead of calling directly 319 // Nothing to do, but post a callback instead of calling directly
382 // to preserve the async style behavior to prevent bugs. 320 // to preserve the async style behavior to prevent bugs.
383 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, 321 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE,
384 base::Bind(callback, false)); 322 base::Bind(callback, false));
385 return; 323 return;
386 } 324 }
387 325
(...skipping 25 matching lines...) Expand all
413 } 351 }
414 352
415 void RequestQueueStoreSQL::OnResetDone(const ResetCallback& callback, 353 void RequestQueueStoreSQL::OnResetDone(const ResetCallback& callback,
416 bool success) { 354 bool success) {
417 // Complete connection initialization post reset. 355 // Complete connection initialization post reset.
418 OnOpenConnectionDone(success); 356 OnOpenConnectionDone(success);
419 callback.Run(success); 357 callback.Run(success);
420 } 358 }
421 359
422 } // namespace offline_pages 360 } // namespace offline_pages
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698