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

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

Issue 2415473003: Query API: Introduces an OfflinePageModelQuery object. (Closed)
Patch Set: Address more comments. Created 4 years, 1 month 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_model_impl.h" 5 #include "components/offline_pages/offline_page_model_impl.h"
6 6
7 #include <algorithm> 7 #include <algorithm>
8 #include <limits> 8 #include <limits>
9 9
10 #include "base/bind.h" 10 #include "base/bind.h"
11 #include "base/files/file_util.h" 11 #include "base/files/file_util.h"
12 #include "base/location.h" 12 #include "base/location.h"
13 #include "base/logging.h" 13 #include "base/logging.h"
14 #include "base/metrics/histogram_macros.h" 14 #include "base/metrics/histogram_macros.h"
15 #include "base/rand_util.h" 15 #include "base/rand_util.h"
16 #include "base/sequenced_task_runner.h" 16 #include "base/sequenced_task_runner.h"
17 #include "base/strings/string16.h" 17 #include "base/strings/string16.h"
18 #include "base/strings/string_number_conversions.h" 18 #include "base/strings/string_number_conversions.h"
19 #include "base/threading/thread_task_runner_handle.h" 19 #include "base/threading/thread_task_runner_handle.h"
20 #include "base/time/clock.h" 20 #include "base/time/clock.h"
21 #include "base/time/time.h" 21 #include "base/time/time.h"
22 #include "components/offline_pages/archive_manager.h" 22 #include "components/offline_pages/archive_manager.h"
23 #include "components/offline_pages/client_namespace_constants.h" 23 #include "components/offline_pages/client_namespace_constants.h"
24 #include "components/offline_pages/client_policy_controller.h" 24 #include "components/offline_pages/client_policy_controller.h"
25 #include "components/offline_pages/offline_page_item.h" 25 #include "components/offline_pages/offline_page_item.h"
26 #include "components/offline_pages/offline_page_model_query.h"
26 #include "components/offline_pages/offline_page_storage_manager.h" 27 #include "components/offline_pages/offline_page_storage_manager.h"
27 #include "url/gurl.h" 28 #include "url/gurl.h"
28 29
29 using ArchiverResult = offline_pages::OfflinePageArchiver::ArchiverResult; 30 using ArchiverResult = offline_pages::OfflinePageArchiver::ArchiverResult;
30 using ClearStorageCallback = 31 using ClearStorageCallback =
31 offline_pages::OfflinePageStorageManager::ClearStorageCallback; 32 offline_pages::OfflinePageStorageManager::ClearStorageCallback;
32 using ClearStorageResult = 33 using ClearStorageResult =
33 offline_pages::OfflinePageStorageManager::ClearStorageResult; 34 offline_pages::OfflinePageStorageManager::ClearStorageResult;
34 35
35 namespace offline_pages { 36 namespace offline_pages {
(...skipping 388 matching lines...) Expand 10 before | Expand all | Expand 10 after
424 425
425 archive_manager_->DeleteMultipleArchives( 426 archive_manager_->DeleteMultipleArchives(
426 paths_to_delete, 427 paths_to_delete,
427 base::Bind(&OfflinePageModelImpl::OnDeleteArchiveFilesDone, 428 base::Bind(&OfflinePageModelImpl::OnDeleteArchiveFilesDone,
428 weak_ptr_factory_.GetWeakPtr(), offline_ids, callback)); 429 weak_ptr_factory_.GetWeakPtr(), offline_ids, callback));
429 } 430 }
430 431
431 void OfflinePageModelImpl::DeletePagesByClientIds( 432 void OfflinePageModelImpl::DeletePagesByClientIds(
432 const std::vector<ClientId>& client_ids, 433 const std::vector<ClientId>& client_ids,
433 const DeletePageCallback& callback) { 434 const DeletePageCallback& callback) {
434 RunWhenLoaded(base::Bind(&OfflinePageModelImpl::DoDeletePagesByClientIds, 435 OfflinePageModelQueryBuilder builder;
435 weak_ptr_factory_.GetWeakPtr(), client_ids, 436 builder
436 callback)); 437 .SetClientIds(OfflinePageModelQuery::Requirement::INCLUDE_MATCHING,
438 client_ids)
439 .AllowExpiredPages(true);
440 auto delete_pages = base::Bind(&OfflinePageModelImpl::DeletePages,
441 weak_ptr_factory_.GetWeakPtr(), callback);
442 RunWhenLoaded(base::Bind(
443 &OfflinePageModelImpl::ExecuteQuery, weak_ptr_factory_.GetWeakPtr(),
444 base::Passed(builder.Build(GetPolicyController())), delete_pages));
437 } 445 }
438 446
439 void OfflinePageModelImpl::DoDeletePagesByClientIds( 447 void OfflinePageModelImpl::DeletePages(
440 const std::vector<ClientId>& client_ids, 448 const DeletePageCallback& callback,
441 const DeletePageCallback& callback) { 449 const MultipleOfflinePageItemResult& pages) {
442 std::set<ClientId> client_id_set(client_ids.begin(), client_ids.end()); 450 DCHECK(is_loaded_);
443 451
444 std::vector<int64_t> offline_ids; 452 std::vector<int64_t> offline_ids;
445 for (const auto& page_pair : offline_pages_) { 453 for (auto& page : pages)
446 if (client_id_set.count(page_pair.second.client_id) > 0) 454 offline_ids.emplace_back(page.offline_id);
447 offline_ids.emplace_back(page_pair.first);
448 }
449 455
450 DoDeletePagesByOfflineId(offline_ids, callback); 456 DoDeletePagesByOfflineId(offline_ids, callback);
451 } 457 }
452 458
453 void OfflinePageModelImpl::GetPagesByClientIds( 459 void OfflinePageModelImpl::GetPagesByClientIds(
454 const std::vector<ClientId>& client_ids, 460 const std::vector<ClientId>& client_ids,
455 const MultipleOfflinePageItemCallback& callback) { 461 const MultipleOfflinePageItemCallback& callback) {
456 RunWhenLoaded(base::Bind(&OfflinePageModelImpl::DoGetPagesByClientIds, 462 OfflinePageModelQueryBuilder builder;
457 weak_ptr_factory_.GetWeakPtr(), client_ids, 463 builder.SetClientIds(OfflinePageModelQuery::Requirement::INCLUDE_MATCHING,
458 callback)); 464 client_ids);
459 } 465 RunWhenLoaded(base::Bind(
460 466 &OfflinePageModelImpl::ExecuteQuery, weak_ptr_factory_.GetWeakPtr(),
461 void OfflinePageModelImpl::DoGetPagesByClientIds( 467 base::Passed(builder.Build(GetPolicyController())), callback));
462 const std::vector<ClientId>& client_ids,
463 const MultipleOfflinePageItemCallback& callback) {
464 std::set<ClientId> client_id_set(client_ids.begin(), client_ids.end());
465
466 std::vector<OfflinePageItem> result;
467 for (const auto& page_pair : offline_pages_) {
468 if (!page_pair.second.IsExpired() &&
469 client_id_set.count(page_pair.second.client_id) > 0) {
470 result.emplace_back(page_pair.second);
471 }
472 }
473 callback.Run(result);
474 } 468 }
475 469
476 void OfflinePageModelImpl::DeleteCachedPagesByURLPredicate( 470 void OfflinePageModelImpl::DeleteCachedPagesByURLPredicate(
477 const UrlPredicate& predicate, 471 const UrlPredicate& predicate,
478 const DeletePageCallback& callback) { 472 const DeletePageCallback& callback) {
479 RunWhenLoaded( 473 RunWhenLoaded(
480 base::Bind(&OfflinePageModelImpl::DoDeleteCachedPagesByURLPredicate, 474 base::Bind(&OfflinePageModelImpl::DoDeleteCachedPagesByURLPredicate,
481 weak_ptr_factory_.GetWeakPtr(), predicate, callback)); 475 weak_ptr_factory_.GetWeakPtr(), predicate, callback));
482 } 476 }
483 477
484 void OfflinePageModelImpl::DoDeleteCachedPagesByURLPredicate( 478 void OfflinePageModelImpl::DoDeleteCachedPagesByURLPredicate(
485 const UrlPredicate& predicate, 479 const UrlPredicate& predicate,
486 const DeletePageCallback& callback) { 480 const DeletePageCallback& callback) {
487 DCHECK(is_loaded_); 481 DCHECK(is_loaded_);
488 482
489 std::vector<int64_t> offline_ids; 483 std::vector<int64_t> offline_ids;
490 for (const auto& id_page_pair : offline_pages_) { 484 for (const auto& id_page_pair : offline_pages_) {
491 if (IsRemovedOnCacheReset(id_page_pair.second) && 485 if (IsRemovedOnCacheReset(id_page_pair.second) &&
492 predicate.Run(id_page_pair.second.url)) { 486 predicate.Run(id_page_pair.second.url)) {
493 offline_ids.push_back(id_page_pair.first); 487 offline_ids.push_back(id_page_pair.first);
494 } 488 }
495 } 489 }
496 DoDeletePagesByOfflineId(offline_ids, callback); 490 DoDeletePagesByOfflineId(offline_ids, callback);
497 } 491 }
498 492
499 void OfflinePageModelImpl::CheckPagesExistOffline( 493 void OfflinePageModelImpl::CheckPagesExistOffline(
500 const std::set<GURL>& urls, 494 const std::set<GURL>& urls,
501 const CheckPagesExistOfflineCallback& callback) { 495 const CheckPagesExistOfflineCallback& callback) {
502 RunWhenLoaded( 496 OfflinePageModelQueryBuilder builder;
503 base::Bind(&OfflinePageModelImpl::CheckPagesExistOfflineAfterLoadDone, 497 builder
504 weak_ptr_factory_.GetWeakPtr(), urls, callback)); 498 .SetUrls(OfflinePageModelQuery::Requirement::INCLUDE_MATCHING,
499 std::vector<GURL>(urls.begin(), urls.end()))
500 .RequireRestrictedToOriginalTab(
501 OfflinePageModelQueryBuilder::Requirement::EXCLUDE_MATCHING);
502 auto pages_to_urls = base::Bind(
503 [](const CheckPagesExistOfflineCallback& callback,
504 const MultipleOfflinePageItemResult& pages) {
505 CheckPagesExistOfflineResult result;
506 for (auto& page : pages)
507 result.insert(page.url);
508 callback.Run(result);
509 },
510 callback);
511 RunWhenLoaded(base::Bind(
512 &OfflinePageModelImpl::ExecuteQuery, weak_ptr_factory_.GetWeakPtr(),
513 base::Passed(builder.Build(GetPolicyController())), pages_to_urls));
505 } 514 }
506 515
507 void OfflinePageModelImpl::CheckPagesExistOfflineAfterLoadDone( 516 void OfflinePageModelImpl::ExecuteQuery(
508 const std::set<GURL>& urls, 517 std::unique_ptr<OfflinePageModelQuery> query,
509 const CheckPagesExistOfflineCallback& callback) { 518 const MultipleOfflinePageItemCallback& callback) {
510 DCHECK(is_loaded_); 519 DCHECK(query);
511 CheckPagesExistOfflineResult result; 520
521 MultipleOfflinePageItemResult offline_pages_result;
522
512 for (const auto& id_page_pair : offline_pages_) { 523 for (const auto& id_page_pair : offline_pages_) {
513 // TODO(dewittj): Remove the "Last N" restriction in favor of a better query 524 if (query->Matches(id_page_pair.second))
514 // interface. See https://crbug.com/622763 for information. 525 offline_pages_result.emplace_back(id_page_pair.second);
515 if (id_page_pair.second.IsExpired() ||
516 id_page_pair.second.client_id.name_space == kLastNNamespace)
517 continue;
518 auto iter = urls.find(id_page_pair.second.url);
519 if (iter != urls.end())
520 result.insert(*iter);
521 } 526 }
522 callback.Run(result); 527
528 callback.Run(offline_pages_result);
523 } 529 }
524 530
525 void OfflinePageModelImpl::GetAllPages( 531 void OfflinePageModelImpl::GetAllPages(
526 const MultipleOfflinePageItemCallback& callback) { 532 const MultipleOfflinePageItemCallback& callback) {
527 RunWhenLoaded(base::Bind(&OfflinePageModelImpl::GetAllPagesAfterLoadDone, 533 OfflinePageModelQueryBuilder builder;
528 weak_ptr_factory_.GetWeakPtr(), GetAllPageMode::ALL, 534 RunWhenLoaded(base::Bind(
529 callback)); 535 &OfflinePageModelImpl::ExecuteQuery, weak_ptr_factory_.GetWeakPtr(),
536 base::Passed(builder.Build(GetPolicyController())), callback));
530 } 537 }
531 538
532 void OfflinePageModelImpl::GetAllPagesWithExpired( 539 void OfflinePageModelImpl::GetAllPagesWithExpired(
533 const MultipleOfflinePageItemCallback& callback) { 540 const MultipleOfflinePageItemCallback& callback) {
534 RunWhenLoaded(base::Bind(&OfflinePageModelImpl::GetAllPagesAfterLoadDone, 541 OfflinePageModelQueryBuilder builder;
535 weak_ptr_factory_.GetWeakPtr(), 542 builder.AllowExpiredPages(true);
536 GetAllPageMode::ALL_WITH_EXPIRED, callback));
537 }
538 543
539 void OfflinePageModelImpl::GetAllPagesAfterLoadDone( 544 RunWhenLoaded(base::Bind(
540 GetAllPageMode mode, 545 &OfflinePageModelImpl::ExecuteQuery, weak_ptr_factory_.GetWeakPtr(),
541 const MultipleOfflinePageItemCallback& callback) const { 546 base::Passed(builder.Build(GetPolicyController())), callback));
542 DCHECK(is_loaded_);
543
544 MultipleOfflinePageItemResult offline_pages;
545 for (const auto& id_page_pair : offline_pages_) {
546 if (mode == GetAllPageMode::ALL_WITH_EXPIRED ||
547 !id_page_pair.second.IsExpired())
548 offline_pages.push_back(id_page_pair.second);
549 }
550
551 callback.Run(offline_pages);
552 } 547 }
553 548
554 void OfflinePageModelImpl::GetOfflineIdsForClientId( 549 void OfflinePageModelImpl::GetOfflineIdsForClientId(
555 const ClientId& client_id, 550 const ClientId& client_id,
556 const MultipleOfflineIdCallback& callback) { 551 const MultipleOfflineIdCallback& callback) {
557 RunWhenLoaded( 552 RunWhenLoaded(
558 base::Bind(&OfflinePageModelImpl::GetOfflineIdsForClientIdWhenLoadDone, 553 base::Bind(&OfflinePageModelImpl::GetOfflineIdsForClientIdWhenLoadDone,
559 weak_ptr_factory_.GetWeakPtr(), client_id, callback)); 554 weak_ptr_factory_.GetWeakPtr(), client_id, callback));
560 } 555 }
561 556
(...skipping 15 matching lines...) Expand all
577 !id_page_pair.second.IsExpired()) { 572 !id_page_pair.second.IsExpired()) {
578 results.push_back(id_page_pair.second.offline_id); 573 results.push_back(id_page_pair.second.offline_id);
579 } 574 }
580 } 575 }
581 return results; 576 return results;
582 } 577 }
583 578
584 void OfflinePageModelImpl::GetPageByOfflineId( 579 void OfflinePageModelImpl::GetPageByOfflineId(
585 int64_t offline_id, 580 int64_t offline_id,
586 const SingleOfflinePageItemCallback& callback) { 581 const SingleOfflinePageItemCallback& callback) {
587 RunWhenLoaded( 582 std::vector<int64_t> query_ids;
588 base::Bind(&OfflinePageModelImpl::GetPageByOfflineIdWhenLoadDone, 583 query_ids.emplace_back(offline_id);
589 weak_ptr_factory_.GetWeakPtr(), offline_id, callback));
590 }
591 584
592 void OfflinePageModelImpl::GetPageByOfflineIdWhenLoadDone( 585 OfflinePageModelQueryBuilder builder;
593 int64_t offline_id, 586 builder.SetOfflinePageIds(
594 const SingleOfflinePageItemCallback& callback) const { 587 OfflinePageModelQuery::Requirement::INCLUDE_MATCHING, query_ids);
595 callback.Run(MaybeGetPageByOfflineId(offline_id));
596 }
597 588
598 const OfflinePageItem* OfflinePageModelImpl::MaybeGetPageByOfflineId( 589 auto multiple_callback = base::Bind(
599 int64_t offline_id) const { 590 [](const SingleOfflinePageItemCallback& callback,
600 const auto iter = offline_pages_.find(offline_id); 591 const MultipleOfflinePageItemResult& result) {
601 return iter != offline_pages_.end() && !iter->second.IsExpired() 592 DCHECK_LE(result.size(), 1U);
602 ? &(iter->second) 593 if (result.empty()) {
603 : nullptr; 594 callback.Run(nullptr);
595 } else {
596 callback.Run(&result[0]);
597 }
598 },
599 callback);
600
601 RunWhenLoaded(base::Bind(
602 &OfflinePageModelImpl::ExecuteQuery, weak_ptr_factory_.GetWeakPtr(),
603 base::Passed(builder.Build(GetPolicyController())), multiple_callback));
604 } 604 }
605 605
606 void OfflinePageModelImpl::GetPagesByOnlineURL( 606 void OfflinePageModelImpl::GetPagesByOnlineURL(
607 const GURL& online_url, 607 const GURL& online_url,
608 const MultipleOfflinePageItemCallback& callback) { 608 const MultipleOfflinePageItemCallback& callback) {
609 RunWhenLoaded( 609 RunWhenLoaded(
610 base::Bind(&OfflinePageModelImpl::GetPagesByOnlineURLWhenLoadDone, 610 base::Bind(&OfflinePageModelImpl::GetPagesByOnlineURLWhenLoadDone,
611 weak_ptr_factory_.GetWeakPtr(), online_url, callback)); 611 weak_ptr_factory_.GetWeakPtr(), online_url, callback));
612 } 612 }
613 613
(...skipping 468 matching lines...) Expand 10 before | Expand all | Expand 10 after
1082 } 1082 }
1083 1083
1084 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, task); 1084 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, task);
1085 } 1085 }
1086 1086
1087 base::Time OfflinePageModelImpl::GetCurrentTime() const { 1087 base::Time OfflinePageModelImpl::GetCurrentTime() const {
1088 return testing_clock_ ? testing_clock_->Now() : base::Time::Now(); 1088 return testing_clock_ ? testing_clock_->Now() : base::Time::Now();
1089 } 1089 }
1090 1090
1091 } // namespace offline_pages 1091 } // namespace offline_pages
OLDNEW
« no previous file with comments | « components/offline_pages/offline_page_model_impl.h ('k') | components/offline_pages/offline_page_model_impl_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698