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

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

Issue 2197573003: Provide API in RequestCoordinator to remove results by client ID. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Comment change (TODO) 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"
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
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) {
fgorski 2016/08/02 05:01:23 const& client_id
Pete Williamson 2016/08/02 23:27:17 Done.
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,
fgorski 2016/08/02 05:01:23 nit: alignment issues. Please run git cl format.
Pete Williamson 2016/08/02 23:27:17 Done.
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) {
fgorski 2016/08/02 05:01:23 const auto&
Pete Williamson 2016/08/02 23:27:17 Done.
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
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
fgorski 2016/08/02 05:01:23 nit: remove empty line.
Pete Williamson 2016/08/02 23:27:17 Done.
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))
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 26 matching lines...) Expand all
255 FROM_HERE, base::Bind(callback, UpdateStatus::FAILED)); 312 FROM_HERE, base::Bind(callback, UpdateStatus::FAILED));
256 return; 313 return;
257 } 314 }
258 315
259 background_task_runner_->PostTask( 316 background_task_runner_->PostTask(
260 FROM_HERE, 317 FROM_HERE,
261 base::Bind(&RequestQueueStoreSQL::AddOrUpdateRequestSync, db_.get(), 318 base::Bind(&RequestQueueStoreSQL::AddOrUpdateRequestSync, db_.get(),
262 base::ThreadTaskRunnerHandle::Get(), request, callback)); 319 base::ThreadTaskRunnerHandle::Get(), request, callback));
263 } 320 }
264 321
322 // TODO(petewil): This is unused, since request_coordinator doesn't keep
323 // request_ids, and neither do clients. Plan is to remove this API in a future
324 // changelist. If we do discover a need to keep it, name it
325 // RemoveRequestsByRequestId to be more parallell with RemoveRequestsByClientId.
fgorski 2016/08/02 05:01:22 cool. Only saw it now.
Pete Williamson 2016/08/02 23:27:17 Acknowledged.
265 void RequestQueueStoreSQL::RemoveRequests( 326 void RequestQueueStoreSQL::RemoveRequests(
266 const std::vector<int64_t>& request_ids, 327 const std::vector<int64_t>& request_ids,
267 const RemoveCallback& callback) { 328 const RemoveCallback& callback) {
268 DCHECK(db_.get()); 329 DCHECK(db_.get());
269 if (!db_.get()) { 330 if (!db_.get()) {
270 // Nothing to do, but post a callback instead of calling directly 331 // Nothing to do, but post a callback instead of calling directly
271 // to preserve the async style behavior to prevent bugs. 332 // to preserve the async style behavior to prevent bugs.
272 base::ThreadTaskRunnerHandle::Get()->PostTask( 333 base::ThreadTaskRunnerHandle::Get()->PostTask(
273 FROM_HERE, base::Bind(callback, false, 0)); 334 FROM_HERE, base::Bind(callback, false, 0));
274 return; 335 return;
275 } 336 }
276 337
277 background_task_runner_->PostTask( 338 background_task_runner_->PostTask(
278 FROM_HERE, 339 FROM_HERE,
279 base::Bind(&RequestQueueStoreSQL::RemoveRequestsSync, db_.get(), 340 base::Bind(&RequestQueueStoreSQL::RemoveRequestsSync, db_.get(),
280 base::ThreadTaskRunnerHandle::Get(), request_ids, callback)); 341 base::ThreadTaskRunnerHandle::Get(), request_ids, callback));
281 } 342 }
282 343
344 void RequestQueueStoreSQL::RemoveRequestsByClientId(
345 const std::vector<ClientId>& client_ids, const RemoveCallback& callback) {
346 DCHECK(db_.get());
347 if (!db_.get()) {
348 // Nothing to do, but post a callback instead of calling directly
349 // to preserve the async style behavior to prevent bugs.
350 base::ThreadTaskRunnerHandle::Get()->PostTask(
351 FROM_HERE, base::Bind(callback, false, 0));
352 return;
353 }
354
355 background_task_runner_->PostTask(
356 FROM_HERE,
357 base::Bind(&RequestQueueStoreSQL::RemoveRequestsByClientIdSync, db_.get(),
358 base::ThreadTaskRunnerHandle::Get(), client_ids, callback));
359 }
360
283 void RequestQueueStoreSQL::Reset(const ResetCallback& callback) { 361 void RequestQueueStoreSQL::Reset(const ResetCallback& callback) {
284 DCHECK(db_.get()); 362 DCHECK(db_.get());
285 if (!db_.get()) { 363 if (!db_.get()) {
286 // Nothing to do, but post a callback instead of calling directly 364 // Nothing to do, but post a callback instead of calling directly
287 // to preserve the async style behavior to prevent bugs. 365 // to preserve the async style behavior to prevent bugs.
288 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, 366 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE,
289 base::Bind(callback, false)); 367 base::Bind(callback, false));
290 return; 368 return;
291 } 369 }
292 370
(...skipping 25 matching lines...) Expand all
318 } 396 }
319 397
320 void RequestQueueStoreSQL::OnResetDone(const ResetCallback& callback, 398 void RequestQueueStoreSQL::OnResetDone(const ResetCallback& callback,
321 bool success) { 399 bool success) {
322 // Complete connection initialization post reset. 400 // Complete connection initialization post reset.
323 OnOpenConnectionDone(success); 401 OnOpenConnectionDone(success);
324 callback.Run(success); 402 callback.Run(success);
325 } 403 }
326 404
327 } // namespace offline_pages 405 } // namespace offline_pages
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698