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

Side by Side Diff: components/offline_pages/offline_page_metadata_store_sql.cc

Issue 2339693002: [Offline pages] Splitting Add and Update methods in OPMStore (Closed)
Patch Set: Addressing feedback Created 4 years, 3 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/offline_page_metadata_store_sql.h" 5 #include "components/offline_pages/offline_page_metadata_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 257 matching lines...) Expand 10 before | Expand all | Expand 10 after
268 if (statement.Succeeded()) { 268 if (statement.Succeeded()) {
269 NotifyLoadResult(runner, callback, OfflinePageMetadataStore::LOAD_SUCCEEDED, 269 NotifyLoadResult(runner, callback, OfflinePageMetadataStore::LOAD_SUCCEEDED,
270 result); 270 result);
271 } else { 271 } else {
272 result.clear(); 272 result.clear();
273 NotifyLoadResult(runner, callback, 273 NotifyLoadResult(runner, callback,
274 OfflinePageMetadataStore::STORE_LOAD_FAILED, result); 274 OfflinePageMetadataStore::STORE_LOAD_FAILED, result);
275 } 275 }
276 } 276 }
277 277
278 void AddOrUpdateOfflinePageSync( 278 void AddOfflinePageSync(sql::Connection* db,
279 const OfflinePageItem& offline_page, 279 scoped_refptr<base::SingleThreadTaskRunner> runner,
280 const OfflinePageItem& offline_page,
281 const OfflinePageMetadataStore::AddCallback& callback) {
282 // TODO(fgorski): Only insert should happen here.
283 InsertOrReplace(db, offline_page);
284 runner->PostTask(FROM_HERE,
285 base::Bind(callback, OfflinePageMetadataStore::SUCCESS));
286 }
287
288 void UpdateOfflinePagesSync(
280 sql::Connection* db, 289 sql::Connection* db,
281 scoped_refptr<base::SingleThreadTaskRunner> runner, 290 scoped_refptr<base::SingleThreadTaskRunner> runner,
291 const std::vector<OfflinePageItem>& pages,
282 const OfflinePageMetadataStore::UpdateCallback& callback) { 292 const OfflinePageMetadataStore::UpdateCallback& callback) {
283 // TODO(bburns): add UMA metrics here (and for levelDB). 293 // TODO(fgorski): Update the callback to provide information about all items
284 bool ok = InsertOrReplace(db, offline_page); 294 // and not just a high level boolean.
285 runner->PostTask(FROM_HERE, base::Bind(callback, ok)); 295 sql::Transaction transaction(db);
296 if (!transaction.Begin()) {
297 runner->PostTask(FROM_HERE, base::Bind(callback, false));
298 return;
299 }
300
301 // TODO(fgorski): Switch to only accept updates and not create new items.
302 for (auto& page : pages)
303 InsertOrReplace(db, page);
304
305 bool result = transaction.Commit();
306 runner->PostTask(FROM_HERE, base::Bind(callback, result));
286 } 307 }
287 308
288 void RemoveOfflinePagesSync( 309 void RemoveOfflinePagesSync(
289 const std::vector<int64_t>& offline_ids, 310 const std::vector<int64_t>& offline_ids,
290 sql::Connection* db, 311 sql::Connection* db,
291 scoped_refptr<base::SingleThreadTaskRunner> runner, 312 scoped_refptr<base::SingleThreadTaskRunner> runner,
292 const OfflinePageMetadataStore::UpdateCallback& callback) { 313 const OfflinePageMetadataStore::UpdateCallback& callback) {
293 // TODO(bburns): add UMA metrics here (and for levelDB). 314 // TODO(bburns): add UMA metrics here (and for levelDB).
294 315
295 // If you create a transaction but don't Commit() it is automatically 316 // If you create a transaction but don't Commit() it is automatically
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after
348 } 369 }
349 } 370 }
350 371
351 void OfflinePageMetadataStoreSQL::GetOfflinePages( 372 void OfflinePageMetadataStoreSQL::GetOfflinePages(
352 const LoadCallback& callback) { 373 const LoadCallback& callback) {
353 background_task_runner_->PostTask( 374 background_task_runner_->PostTask(
354 FROM_HERE, base::Bind(&GetOfflinePagesSync, db_.get(), 375 FROM_HERE, base::Bind(&GetOfflinePagesSync, db_.get(),
355 base::ThreadTaskRunnerHandle::Get(), callback)); 376 base::ThreadTaskRunnerHandle::Get(), callback));
356 } 377 }
357 378
358 void OfflinePageMetadataStoreSQL::AddOrUpdateOfflinePage( 379 void OfflinePageMetadataStoreSQL::AddOfflinePage(
359 const OfflinePageItem& offline_page, 380 const OfflinePageItem& offline_page,
360 const UpdateCallback& callback) { 381 const AddCallback& callback) {
361 DCHECK(db_.get()); 382 if (!CheckDb(base::Bind(callback, STORE_ERROR)))
383 return;
384
362 background_task_runner_->PostTask( 385 background_task_runner_->PostTask(
363 FROM_HERE, 386 FROM_HERE,
364 base::Bind(&AddOrUpdateOfflinePageSync, offline_page, db_.get(), 387 base::Bind(&AddOfflinePageSync, db_.get(),
365 base::ThreadTaskRunnerHandle::Get(), callback)); 388 base::ThreadTaskRunnerHandle::Get(), offline_page, callback));
389 }
390
391 void OfflinePageMetadataStoreSQL::UpdateOfflinePages(
392 const std::vector<OfflinePageItem>& pages,
393 const UpdateCallback& callback) {
394 if (!CheckDb(base::Bind(callback, false)))
395 return;
396
397 background_task_runner_->PostTask(
398 FROM_HERE,
399 base::Bind(&UpdateOfflinePagesSync, db_.get(),
400 base::ThreadTaskRunnerHandle::Get(), pages, callback));
366 } 401 }
367 402
368 void OfflinePageMetadataStoreSQL::RemoveOfflinePages( 403 void OfflinePageMetadataStoreSQL::RemoveOfflinePages(
369 const std::vector<int64_t>& offline_ids, 404 const std::vector<int64_t>& offline_ids,
370 const UpdateCallback& callback) { 405 const UpdateCallback& callback) {
371 DCHECK(db_.get()); 406 DCHECK(db_.get());
372 407
373 if (offline_ids.empty()) { 408 if (offline_ids.empty()) {
374 // Nothing to do, but post a callback instead of calling directly 409 // Nothing to do, but post a callback instead of calling directly
375 // to preserve the async style behavior to prevent bugs. 410 // to preserve the async style behavior to prevent bugs.
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after
433 DCHECK(db_.get()); 468 DCHECK(db_.get());
434 if (!db_.get()) { 469 if (!db_.get()) {
435 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, 470 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE,
436 base::Bind(callback)); 471 base::Bind(callback));
437 return false; 472 return false;
438 } 473 }
439 return true; 474 return true;
440 } 475 }
441 476
442 } // namespace offline_pages 477 } // namespace offline_pages
OLDNEW
« no previous file with comments | « components/offline_pages/offline_page_metadata_store_sql.h ('k') | components/offline_pages/offline_page_model_impl.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698