Chromium Code Reviews| OLD | NEW |
|---|---|
| 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" |
| (...skipping 423 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 434 void OfflinePageModelImpl::DeletePagesByClientIds( | 434 void OfflinePageModelImpl::DeletePagesByClientIds( |
| 435 const std::vector<ClientId>& client_ids, | 435 const std::vector<ClientId>& client_ids, |
| 436 const DeletePageCallback& callback) { | 436 const DeletePageCallback& callback) { |
| 437 OfflinePageModelQueryBuilder builder; | 437 OfflinePageModelQueryBuilder builder; |
| 438 builder | 438 builder |
| 439 .SetClientIds(OfflinePageModelQuery::Requirement::INCLUDE_MATCHING, | 439 .SetClientIds(OfflinePageModelQuery::Requirement::INCLUDE_MATCHING, |
| 440 client_ids) | 440 client_ids) |
| 441 .AllowExpiredPages(true); | 441 .AllowExpiredPages(true); |
| 442 auto delete_pages = base::Bind(&OfflinePageModelImpl::DeletePages, | 442 auto delete_pages = base::Bind(&OfflinePageModelImpl::DeletePages, |
| 443 weak_ptr_factory_.GetWeakPtr(), callback); | 443 weak_ptr_factory_.GetWeakPtr(), callback); |
| 444 RunWhenLoaded(base::Bind(&OfflinePageModelImpl::GetPagesMatchingQuery, | 444 RunWhenLoaded(base::Bind( |
| 445 weak_ptr_factory_.GetWeakPtr(), | 445 &OfflinePageModelImpl::GetPagesMatchingQueryWhenLoadDone, |
| 446 base::Passed(builder.Build(GetPolicyController())), | 446 weak_ptr_factory_.GetWeakPtr(), |
| 447 delete_pages)); | 447 base::Passed(builder.Build(GetPolicyController())), delete_pages)); |
| 448 } | 448 } |
| 449 | 449 |
| 450 void OfflinePageModelImpl::DeletePages( | 450 void OfflinePageModelImpl::DeletePages( |
| 451 const DeletePageCallback& callback, | 451 const DeletePageCallback& callback, |
| 452 const MultipleOfflinePageItemResult& pages) { | 452 const MultipleOfflinePageItemResult& pages) { |
| 453 DCHECK(is_loaded_); | 453 DCHECK(is_loaded_); |
| 454 | 454 |
| 455 std::vector<int64_t> offline_ids; | 455 std::vector<int64_t> offline_ids; |
| 456 for (auto& page : pages) | 456 for (auto& page : pages) |
| 457 offline_ids.emplace_back(page.offline_id); | 457 offline_ids.emplace_back(page.offline_id); |
| 458 | 458 |
| 459 DoDeletePagesByOfflineId(offline_ids, callback); | 459 DoDeletePagesByOfflineId(offline_ids, callback); |
| 460 } | 460 } |
| 461 | 461 |
| 462 void OfflinePageModelImpl::GetPagesMatchingQuery( | |
| 463 std::unique_ptr<OfflinePageModelQuery> query, | |
| 464 const MultipleOfflinePageItemCallback& callback) { | |
| 465 RunWhenLoaded(base::Bind( | |
| 466 &OfflinePageModelImpl::GetPagesMatchingQueryWhenLoadDone, | |
| 467 weak_ptr_factory_.GetWeakPtr(), base::Passed(&query), callback)); | |
| 468 } | |
| 469 | |
| 470 void OfflinePageModelImpl::GetPagesMatchingQueryWhenLoadDone( | |
| 471 std::unique_ptr<OfflinePageModelQuery> query, | |
| 472 const MultipleOfflinePageItemCallback& callback) { | |
| 473 DCHECK(query); | |
| 474 DCHECK(is_loaded_); | |
| 475 | |
| 476 MultipleOfflinePageItemResult offline_pages_result; | |
| 477 | |
| 478 for (const auto& id_page_pair : offline_pages_) { | |
| 479 if (query->Matches(id_page_pair.second)) | |
| 480 offline_pages_result.emplace_back(id_page_pair.second); | |
| 481 } | |
| 482 | |
| 483 callback.Run(offline_pages_result); | |
| 484 } | |
| 485 | |
| 462 void OfflinePageModelImpl::GetPagesByClientIds( | 486 void OfflinePageModelImpl::GetPagesByClientIds( |
| 463 const std::vector<ClientId>& client_ids, | 487 const std::vector<ClientId>& client_ids, |
| 464 const MultipleOfflinePageItemCallback& callback) { | 488 const MultipleOfflinePageItemCallback& callback) { |
| 465 OfflinePageModelQueryBuilder builder; | 489 OfflinePageModelQueryBuilder builder; |
| 466 builder.SetClientIds(OfflinePageModelQuery::Requirement::INCLUDE_MATCHING, | 490 builder.SetClientIds(OfflinePageModelQuery::Requirement::INCLUDE_MATCHING, |
| 467 client_ids); | 491 client_ids); |
| 468 RunWhenLoaded(base::Bind(&OfflinePageModelImpl::GetPagesMatchingQuery, | 492 RunWhenLoaded( |
|
dewittj
2016/11/28 17:52:09
This could simply be rewritten as GetPagesMatching
fgorski
2016/11/28 18:30:58
I know, but it sticks to the pattern applied in th
| |
| 469 weak_ptr_factory_.GetWeakPtr(), | 493 base::Bind(&OfflinePageModelImpl::GetPagesMatchingQueryWhenLoadDone, |
| 470 base::Passed(builder.Build(GetPolicyController())), | 494 weak_ptr_factory_.GetWeakPtr(), |
| 471 callback)); | 495 base::Passed(builder.Build(GetPolicyController())), callback)); |
| 472 } | 496 } |
| 473 | 497 |
| 474 void OfflinePageModelImpl::DeleteCachedPagesByURLPredicate( | 498 void OfflinePageModelImpl::DeleteCachedPagesByURLPredicate( |
| 475 const UrlPredicate& predicate, | 499 const UrlPredicate& predicate, |
| 476 const DeletePageCallback& callback) { | 500 const DeletePageCallback& callback) { |
| 477 RunWhenLoaded( | 501 RunWhenLoaded( |
| 478 base::Bind(&OfflinePageModelImpl::DoDeleteCachedPagesByURLPredicate, | 502 base::Bind(&OfflinePageModelImpl::DoDeleteCachedPagesByURLPredicate, |
| 479 weak_ptr_factory_.GetWeakPtr(), predicate, callback)); | 503 weak_ptr_factory_.GetWeakPtr(), predicate, callback)); |
| 480 } | 504 } |
| 481 | 505 |
| (...skipping 23 matching lines...) Expand all Loading... | |
| 505 OfflinePageModelQueryBuilder::Requirement::EXCLUDE_MATCHING); | 529 OfflinePageModelQueryBuilder::Requirement::EXCLUDE_MATCHING); |
| 506 auto pages_to_urls = base::Bind( | 530 auto pages_to_urls = base::Bind( |
| 507 [](const CheckPagesExistOfflineCallback& callback, | 531 [](const CheckPagesExistOfflineCallback& callback, |
| 508 const MultipleOfflinePageItemResult& pages) { | 532 const MultipleOfflinePageItemResult& pages) { |
| 509 CheckPagesExistOfflineResult result; | 533 CheckPagesExistOfflineResult result; |
| 510 for (auto& page : pages) | 534 for (auto& page : pages) |
| 511 result.insert(page.url); | 535 result.insert(page.url); |
| 512 callback.Run(result); | 536 callback.Run(result); |
| 513 }, | 537 }, |
| 514 callback); | 538 callback); |
| 515 RunWhenLoaded(base::Bind(&OfflinePageModelImpl::GetPagesMatchingQuery, | 539 RunWhenLoaded(base::Bind( |
| 516 weak_ptr_factory_.GetWeakPtr(), | 540 &OfflinePageModelImpl::GetPagesMatchingQueryWhenLoadDone, |
| 517 base::Passed(builder.Build(GetPolicyController())), | 541 weak_ptr_factory_.GetWeakPtr(), |
| 518 pages_to_urls)); | 542 base::Passed(builder.Build(GetPolicyController())), pages_to_urls)); |
| 519 } | |
| 520 | |
| 521 void OfflinePageModelImpl::GetPagesMatchingQuery( | |
| 522 std::unique_ptr<OfflinePageModelQuery> query, | |
| 523 const MultipleOfflinePageItemCallback& callback) { | |
| 524 DCHECK(query); | |
| 525 | |
| 526 MultipleOfflinePageItemResult offline_pages_result; | |
| 527 | |
| 528 for (const auto& id_page_pair : offline_pages_) { | |
| 529 if (query->Matches(id_page_pair.second)) | |
| 530 offline_pages_result.emplace_back(id_page_pair.second); | |
| 531 } | |
| 532 | |
| 533 callback.Run(offline_pages_result); | |
| 534 } | 543 } |
| 535 | 544 |
| 536 void OfflinePageModelImpl::GetAllPages( | 545 void OfflinePageModelImpl::GetAllPages( |
| 537 const MultipleOfflinePageItemCallback& callback) { | 546 const MultipleOfflinePageItemCallback& callback) { |
| 538 OfflinePageModelQueryBuilder builder; | 547 OfflinePageModelQueryBuilder builder; |
| 539 RunWhenLoaded(base::Bind(&OfflinePageModelImpl::GetPagesMatchingQuery, | 548 RunWhenLoaded( |
| 540 weak_ptr_factory_.GetWeakPtr(), | 549 base::Bind(&OfflinePageModelImpl::GetPagesMatchingQueryWhenLoadDone, |
| 541 base::Passed(builder.Build(GetPolicyController())), | 550 weak_ptr_factory_.GetWeakPtr(), |
| 542 callback)); | 551 base::Passed(builder.Build(GetPolicyController())), callback)); |
| 543 } | 552 } |
| 544 | 553 |
| 545 void OfflinePageModelImpl::GetAllPagesWithExpired( | 554 void OfflinePageModelImpl::GetAllPagesWithExpired( |
| 546 const MultipleOfflinePageItemCallback& callback) { | 555 const MultipleOfflinePageItemCallback& callback) { |
| 547 OfflinePageModelQueryBuilder builder; | 556 OfflinePageModelQueryBuilder builder; |
| 548 builder.AllowExpiredPages(true); | 557 builder.AllowExpiredPages(true); |
| 549 | 558 |
| 550 RunWhenLoaded(base::Bind(&OfflinePageModelImpl::GetPagesMatchingQuery, | 559 RunWhenLoaded( |
| 551 weak_ptr_factory_.GetWeakPtr(), | 560 base::Bind(&OfflinePageModelImpl::GetPagesMatchingQueryWhenLoadDone, |
| 552 base::Passed(builder.Build(GetPolicyController())), | 561 weak_ptr_factory_.GetWeakPtr(), |
| 553 callback)); | 562 base::Passed(builder.Build(GetPolicyController())), callback)); |
| 554 } | 563 } |
| 555 | 564 |
| 556 void OfflinePageModelImpl::GetOfflineIdsForClientId( | 565 void OfflinePageModelImpl::GetOfflineIdsForClientId( |
| 557 const ClientId& client_id, | 566 const ClientId& client_id, |
| 558 const MultipleOfflineIdCallback& callback) { | 567 const MultipleOfflineIdCallback& callback) { |
| 559 RunWhenLoaded( | 568 RunWhenLoaded( |
| 560 base::Bind(&OfflinePageModelImpl::GetOfflineIdsForClientIdWhenLoadDone, | 569 base::Bind(&OfflinePageModelImpl::GetOfflineIdsForClientIdWhenLoadDone, |
| 561 weak_ptr_factory_.GetWeakPtr(), client_id, callback)); | 570 weak_ptr_factory_.GetWeakPtr(), client_id, callback)); |
| 562 } | 571 } |
| 563 | 572 |
| 564 void OfflinePageModelImpl::GetOfflineIdsForClientIdWhenLoadDone( | 573 void OfflinePageModelImpl::GetOfflineIdsForClientIdWhenLoadDone( |
| 565 const ClientId& client_id, | 574 const ClientId& client_id, |
| 566 const MultipleOfflineIdCallback& callback) const { | 575 const MultipleOfflineIdCallback& callback) const { |
| 576 DCHECK(is_loaded_); | |
| 567 callback.Run(MaybeGetOfflineIdsForClientId(client_id)); | 577 callback.Run(MaybeGetOfflineIdsForClientId(client_id)); |
| 568 } | 578 } |
| 569 | 579 |
| 570 const std::vector<int64_t> OfflinePageModelImpl::MaybeGetOfflineIdsForClientId( | 580 const std::vector<int64_t> OfflinePageModelImpl::MaybeGetOfflineIdsForClientId( |
| 571 const ClientId& client_id) const { | 581 const ClientId& client_id) const { |
| 572 DCHECK(is_loaded_); | 582 DCHECK(is_loaded_); |
| 573 std::vector<int64_t> results; | 583 std::vector<int64_t> results; |
| 574 | 584 |
| 575 // We want only all pages, including those marked for deletion. | 585 // We want only all pages, including those marked for deletion. |
| 576 // TODO(fgorski): actually use an index rather than linear scan. | 586 // TODO(fgorski): actually use an index rather than linear scan. |
| (...skipping 21 matching lines...) Expand all Loading... | |
| 598 const MultipleOfflinePageItemResult& result) { | 608 const MultipleOfflinePageItemResult& result) { |
| 599 DCHECK_LE(result.size(), 1U); | 609 DCHECK_LE(result.size(), 1U); |
| 600 if (result.empty()) { | 610 if (result.empty()) { |
| 601 callback.Run(nullptr); | 611 callback.Run(nullptr); |
| 602 } else { | 612 } else { |
| 603 callback.Run(&result[0]); | 613 callback.Run(&result[0]); |
| 604 } | 614 } |
| 605 }, | 615 }, |
| 606 callback); | 616 callback); |
| 607 | 617 |
| 608 RunWhenLoaded(base::Bind(&OfflinePageModelImpl::GetPagesMatchingQuery, | 618 RunWhenLoaded(base::Bind( |
| 609 weak_ptr_factory_.GetWeakPtr(), | 619 &OfflinePageModelImpl::GetPagesMatchingQueryWhenLoadDone, |
| 610 base::Passed(builder.Build(GetPolicyController())), | 620 weak_ptr_factory_.GetWeakPtr(), |
| 611 multiple_callback)); | 621 base::Passed(builder.Build(GetPolicyController())), multiple_callback)); |
| 612 } | 622 } |
| 613 | 623 |
| 614 void OfflinePageModelImpl::GetPagesByURL( | 624 void OfflinePageModelImpl::GetPagesByURL( |
| 615 const GURL& url, | 625 const GURL& url, |
| 616 URLSearchMode url_search_mode, | 626 URLSearchMode url_search_mode, |
| 617 const MultipleOfflinePageItemCallback& callback) { | 627 const MultipleOfflinePageItemCallback& callback) { |
| 618 RunWhenLoaded( | 628 RunWhenLoaded( |
| 619 base::Bind(&OfflinePageModelImpl::GetPagesByURLWhenLoadDone, | 629 base::Bind(&OfflinePageModelImpl::GetPagesByURLWhenLoadDone, |
| 620 weak_ptr_factory_.GetWeakPtr(), url, | 630 weak_ptr_factory_.GetWeakPtr(), url, |
| 621 url_search_mode, callback)); | 631 url_search_mode, callback)); |
| 622 } | 632 } |
| 623 | 633 |
| 624 void OfflinePageModelImpl::GetPagesByURLWhenLoadDone( | 634 void OfflinePageModelImpl::GetPagesByURLWhenLoadDone( |
| 625 const GURL& url, | 635 const GURL& url, |
| 626 URLSearchMode url_search_mode, | 636 URLSearchMode url_search_mode, |
| 627 const MultipleOfflinePageItemCallback& callback) const { | 637 const MultipleOfflinePageItemCallback& callback) const { |
| 638 DCHECK(is_loaded_); | |
| 628 std::vector<OfflinePageItem> result; | 639 std::vector<OfflinePageItem> result; |
| 629 | 640 |
| 630 GURL::Replacements remove_params; | 641 GURL::Replacements remove_params; |
| 631 remove_params.ClearRef(); | 642 remove_params.ClearRef(); |
| 632 | 643 |
| 633 GURL url_without_fragment = | 644 GURL url_without_fragment = |
| 634 url.ReplaceComponents(remove_params); | 645 url.ReplaceComponents(remove_params); |
| 635 | 646 |
| 636 for (const auto& id_page_pair : offline_pages_) { | 647 for (const auto& id_page_pair : offline_pages_) { |
| 637 if (id_page_pair.second.IsExpired()) | 648 if (id_page_pair.second.IsExpired()) |
| (...skipping 506 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1144 } | 1155 } |
| 1145 | 1156 |
| 1146 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, task); | 1157 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, task); |
| 1147 } | 1158 } |
| 1148 | 1159 |
| 1149 base::Time OfflinePageModelImpl::GetCurrentTime() const { | 1160 base::Time OfflinePageModelImpl::GetCurrentTime() const { |
| 1150 return testing_clock_ ? testing_clock_->Now() : base::Time::Now(); | 1161 return testing_clock_ ? testing_clock_->Now() : base::Time::Now(); |
| 1151 } | 1162 } |
| 1152 | 1163 |
| 1153 } // namespace offline_pages | 1164 } // namespace offline_pages |
| OLD | NEW |