| 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" |
| 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 Loading... |
| 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.SetClientIds(OfflinePageModelQuery::Requirement::MATCH, client_ids) |
| 436 callback)); | 437 .AllowExpiredPages(true); |
| 438 auto delete_pages = base::Bind(&OfflinePageModelImpl::DeletePages, |
| 439 weak_ptr_factory_.GetWeakPtr(), callback); |
| 440 RunWhenLoaded(base::Bind( |
| 441 &OfflinePageModelImpl::ExecuteQuery, weak_ptr_factory_.GetWeakPtr(), |
| 442 base::Passed(builder.Build(GetPolicyController())), delete_pages)); |
| 437 } | 443 } |
| 438 | 444 |
| 439 void OfflinePageModelImpl::DoDeletePagesByClientIds( | 445 void OfflinePageModelImpl::DeletePages( |
| 440 const std::vector<ClientId>& client_ids, | 446 const DeletePageCallback& callback, |
| 441 const DeletePageCallback& callback) { | 447 const MultipleOfflinePageItemResult& pages) { |
| 442 std::set<ClientId> client_id_set(client_ids.begin(), client_ids.end()); | 448 DCHECK(is_loaded_); |
| 443 | 449 |
| 444 std::vector<int64_t> offline_ids; | 450 std::vector<int64_t> offline_ids; |
| 445 for (const auto& page_pair : offline_pages_) { | 451 for (auto& page : pages) |
| 446 if (client_id_set.count(page_pair.second.client_id) > 0) | 452 offline_ids.emplace_back(page.offline_id); |
| 447 offline_ids.emplace_back(page_pair.first); | |
| 448 } | |
| 449 | 453 |
| 450 DoDeletePagesByOfflineId(offline_ids, callback); | 454 DoDeletePagesByOfflineId(offline_ids, callback); |
| 451 } | 455 } |
| 452 | 456 |
| 453 void OfflinePageModelImpl::GetPagesByClientIds( | 457 void OfflinePageModelImpl::GetPagesByClientIds( |
| 454 const std::vector<ClientId>& client_ids, | 458 const std::vector<ClientId>& client_ids, |
| 455 const MultipleOfflinePageItemCallback& callback) { | 459 const MultipleOfflinePageItemCallback& callback) { |
| 456 RunWhenLoaded(base::Bind(&OfflinePageModelImpl::DoGetPagesByClientIds, | 460 OfflinePageModelQueryBuilder builder; |
| 457 weak_ptr_factory_.GetWeakPtr(), client_ids, | 461 builder.SetClientIds(OfflinePageModelQuery::Requirement::MATCH, client_ids); |
| 458 callback)); | 462 RunWhenLoaded(base::Bind( |
| 459 } | 463 &OfflinePageModelImpl::ExecuteQuery, weak_ptr_factory_.GetWeakPtr(), |
| 460 | 464 base::Passed(builder.Build(GetPolicyController())), callback)); |
| 461 void OfflinePageModelImpl::DoGetPagesByClientIds( | |
| 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 } | 465 } |
| 475 | 466 |
| 476 void OfflinePageModelImpl::DeleteCachedPagesByURLPredicate( | 467 void OfflinePageModelImpl::DeleteCachedPagesByURLPredicate( |
| 477 const UrlPredicate& predicate, | 468 const UrlPredicate& predicate, |
| 478 const DeletePageCallback& callback) { | 469 const DeletePageCallback& callback) { |
| 479 RunWhenLoaded( | 470 RunWhenLoaded( |
| 480 base::Bind(&OfflinePageModelImpl::DoDeleteCachedPagesByURLPredicate, | 471 base::Bind(&OfflinePageModelImpl::DoDeleteCachedPagesByURLPredicate, |
| 481 weak_ptr_factory_.GetWeakPtr(), predicate, callback)); | 472 weak_ptr_factory_.GetWeakPtr(), predicate, callback)); |
| 482 } | 473 } |
| 483 | 474 |
| 484 void OfflinePageModelImpl::DoDeleteCachedPagesByURLPredicate( | 475 void OfflinePageModelImpl::DoDeleteCachedPagesByURLPredicate( |
| 485 const UrlPredicate& predicate, | 476 const UrlPredicate& predicate, |
| 486 const DeletePageCallback& callback) { | 477 const DeletePageCallback& callback) { |
| 487 DCHECK(is_loaded_); | 478 DCHECK(is_loaded_); |
| 488 | 479 |
| 489 std::vector<int64_t> offline_ids; | 480 std::vector<int64_t> offline_ids; |
| 490 for (const auto& id_page_pair : offline_pages_) { | 481 for (const auto& id_page_pair : offline_pages_) { |
| 491 if (IsRemovedOnCacheReset(id_page_pair.second) && | 482 if (IsRemovedOnCacheReset(id_page_pair.second) && |
| 492 predicate.Run(id_page_pair.second.url)) { | 483 predicate.Run(id_page_pair.second.url)) { |
| 493 offline_ids.push_back(id_page_pair.first); | 484 offline_ids.push_back(id_page_pair.first); |
| 494 } | 485 } |
| 495 } | 486 } |
| 496 DoDeletePagesByOfflineId(offline_ids, callback); | 487 DoDeletePagesByOfflineId(offline_ids, callback); |
| 497 } | 488 } |
| 498 | 489 |
| 499 void OfflinePageModelImpl::CheckPagesExistOffline( | 490 void OfflinePageModelImpl::CheckPagesExistOffline( |
| 500 const std::set<GURL>& urls, | 491 const std::set<GURL>& urls, |
| 501 const CheckPagesExistOfflineCallback& callback) { | 492 const CheckPagesExistOfflineCallback& callback) { |
| 502 RunWhenLoaded( | 493 OfflinePageModelQueryBuilder builder; |
| 503 base::Bind(&OfflinePageModelImpl::CheckPagesExistOfflineAfterLoadDone, | 494 builder |
| 504 weak_ptr_factory_.GetWeakPtr(), urls, callback)); | 495 .SetUrls(OfflinePageModelQuery::Requirement::MATCH, |
| 496 std::vector<GURL>(urls.begin(), urls.end())) |
| 497 .RequireRestrictedToOriginalTab( |
| 498 OfflinePageModelQueryBuilder::Requirement::EXCLUDE); |
| 499 auto pages_to_urls = base::Bind( |
| 500 [](const CheckPagesExistOfflineCallback& callback, |
| 501 const MultipleOfflinePageItemResult& pages) { |
| 502 CheckPagesExistOfflineResult result; |
| 503 for (auto& page : pages) |
| 504 result.insert(page.url); |
| 505 callback.Run(result); |
| 506 }, |
| 507 callback); |
| 508 RunWhenLoaded(base::Bind( |
| 509 &OfflinePageModelImpl::ExecuteQuery, weak_ptr_factory_.GetWeakPtr(), |
| 510 base::Passed(builder.Build(GetPolicyController())), pages_to_urls)); |
| 505 } | 511 } |
| 506 | 512 |
| 507 void OfflinePageModelImpl::CheckPagesExistOfflineAfterLoadDone( | 513 void OfflinePageModelImpl::ExecuteQuery( |
| 508 const std::set<GURL>& urls, | 514 std::unique_ptr<OfflinePageModelQuery> query, |
| 509 const CheckPagesExistOfflineCallback& callback) { | 515 const MultipleOfflinePageItemCallback& callback) { |
| 510 DCHECK(is_loaded_); | 516 // TODO(dewittj): some sort of signalling here? |
| 511 CheckPagesExistOfflineResult result; | 517 if (!query) |
| 512 for (const auto& id_page_pair : offline_pages_) { | 518 return; |
| 513 // TODO(dewittj): Remove the "Last N" restriction in favor of a better query | 519 |
| 514 // interface. See https://crbug.com/622763 for information. | 520 MultipleOfflinePageItemResult offline_pages_result; |
| 515 if (id_page_pair.second.IsExpired() || | 521 |
| 516 id_page_pair.second.client_id.name_space == kLastNNamespace) | 522 std::set<int64_t> offline_ids; |
| 517 continue; | 523 // Optimization: since we store as a map of offline IDs, we will look them up |
| 518 auto iter = urls.find(id_page_pair.second.url); | 524 // directly if we have an explicit ID list. |
| 519 if (iter != urls.end()) | 525 if (query->GetRestrictedToOfflineIds(&offline_ids) == |
| 520 result.insert(*iter); | 526 OfflinePageModelQuery::Requirement::MATCH) { |
| 527 for (int64_t id : offline_ids) { |
| 528 if (offline_pages_.count(id) > 0 && query->Matches(offline_pages_[id])) |
| 529 offline_pages_result.emplace_back(offline_pages_[id]); |
| 530 } |
| 531 } else { |
| 532 for (const auto& id_page_pair : offline_pages_) { |
| 533 if (query->Matches(id_page_pair.second)) |
| 534 offline_pages_result.emplace_back(id_page_pair.second); |
| 535 } |
| 521 } | 536 } |
| 522 callback.Run(result); | 537 |
| 538 callback.Run(offline_pages_result); |
| 523 } | 539 } |
| 524 | 540 |
| 525 void OfflinePageModelImpl::GetAllPages( | 541 void OfflinePageModelImpl::GetAllPages( |
| 526 const MultipleOfflinePageItemCallback& callback) { | 542 const MultipleOfflinePageItemCallback& callback) { |
| 527 RunWhenLoaded(base::Bind(&OfflinePageModelImpl::GetAllPagesAfterLoadDone, | 543 OfflinePageModelQueryBuilder builder; |
| 528 weak_ptr_factory_.GetWeakPtr(), GetAllPageMode::ALL, | 544 RunWhenLoaded(base::Bind( |
| 529 callback)); | 545 &OfflinePageModelImpl::ExecuteQuery, weak_ptr_factory_.GetWeakPtr(), |
| 546 base::Passed(builder.Build(GetPolicyController())), callback)); |
| 530 } | 547 } |
| 531 | 548 |
| 532 void OfflinePageModelImpl::GetAllPagesWithExpired( | 549 void OfflinePageModelImpl::GetAllPagesWithExpired( |
| 533 const MultipleOfflinePageItemCallback& callback) { | 550 const MultipleOfflinePageItemCallback& callback) { |
| 534 RunWhenLoaded(base::Bind(&OfflinePageModelImpl::GetAllPagesAfterLoadDone, | 551 OfflinePageModelQueryBuilder builder; |
| 535 weak_ptr_factory_.GetWeakPtr(), | 552 builder.AllowExpiredPages(true); |
| 536 GetAllPageMode::ALL_WITH_EXPIRED, callback)); | |
| 537 } | |
| 538 | 553 |
| 539 void OfflinePageModelImpl::GetAllPagesAfterLoadDone( | 554 RunWhenLoaded(base::Bind( |
| 540 GetAllPageMode mode, | 555 &OfflinePageModelImpl::ExecuteQuery, weak_ptr_factory_.GetWeakPtr(), |
| 541 const MultipleOfflinePageItemCallback& callback) const { | 556 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 } | 557 } |
| 553 | 558 |
| 554 void OfflinePageModelImpl::GetOfflineIdsForClientId( | 559 void OfflinePageModelImpl::GetOfflineIdsForClientId( |
| 555 const ClientId& client_id, | 560 const ClientId& client_id, |
| 556 const MultipleOfflineIdCallback& callback) { | 561 const MultipleOfflineIdCallback& callback) { |
| 557 RunWhenLoaded( | 562 RunWhenLoaded( |
| 558 base::Bind(&OfflinePageModelImpl::GetOfflineIdsForClientIdWhenLoadDone, | 563 base::Bind(&OfflinePageModelImpl::GetOfflineIdsForClientIdWhenLoadDone, |
| 559 weak_ptr_factory_.GetWeakPtr(), client_id, callback)); | 564 weak_ptr_factory_.GetWeakPtr(), client_id, callback)); |
| 560 } | 565 } |
| 561 | 566 |
| (...skipping 15 matching lines...) Expand all Loading... |
| 577 !id_page_pair.second.IsExpired()) { | 582 !id_page_pair.second.IsExpired()) { |
| 578 results.push_back(id_page_pair.second.offline_id); | 583 results.push_back(id_page_pair.second.offline_id); |
| 579 } | 584 } |
| 580 } | 585 } |
| 581 return results; | 586 return results; |
| 582 } | 587 } |
| 583 | 588 |
| 584 void OfflinePageModelImpl::GetPageByOfflineId( | 589 void OfflinePageModelImpl::GetPageByOfflineId( |
| 585 int64_t offline_id, | 590 int64_t offline_id, |
| 586 const SingleOfflinePageItemCallback& callback) { | 591 const SingleOfflinePageItemCallback& callback) { |
| 587 RunWhenLoaded( | 592 std::vector<int64_t> query_ids; |
| 588 base::Bind(&OfflinePageModelImpl::GetPageByOfflineIdWhenLoadDone, | 593 query_ids.emplace_back(offline_id); |
| 589 weak_ptr_factory_.GetWeakPtr(), offline_id, callback)); | |
| 590 } | |
| 591 | 594 |
| 592 void OfflinePageModelImpl::GetPageByOfflineIdWhenLoadDone( | 595 OfflinePageModelQueryBuilder builder; |
| 593 int64_t offline_id, | 596 builder.SetOfflinePageIds(OfflinePageModelQuery::Requirement::MATCH, |
| 594 const SingleOfflinePageItemCallback& callback) const { | 597 query_ids); |
| 595 callback.Run(MaybeGetPageByOfflineId(offline_id)); | |
| 596 } | |
| 597 | 598 |
| 598 const OfflinePageItem* OfflinePageModelImpl::MaybeGetPageByOfflineId( | 599 auto multiple_callback = base::Bind( |
| 599 int64_t offline_id) const { | 600 [](const SingleOfflinePageItemCallback& callback, |
| 600 const auto iter = offline_pages_.find(offline_id); | 601 const MultipleOfflinePageItemResult& result) { |
| 601 return iter != offline_pages_.end() && !iter->second.IsExpired() | 602 DCHECK_LE(result.size(), 1U); |
| 602 ? &(iter->second) | 603 if (result.empty()) { |
| 603 : nullptr; | 604 callback.Run(nullptr); |
| 605 } else { |
| 606 callback.Run(&result[0]); |
| 607 } |
| 608 }, |
| 609 callback); |
| 610 |
| 611 RunWhenLoaded(base::Bind( |
| 612 &OfflinePageModelImpl::ExecuteQuery, weak_ptr_factory_.GetWeakPtr(), |
| 613 base::Passed(builder.Build(GetPolicyController())), multiple_callback)); |
| 604 } | 614 } |
| 605 | 615 |
| 606 void OfflinePageModelImpl::GetPagesByOnlineURL( | 616 void OfflinePageModelImpl::GetPagesByOnlineURL( |
| 607 const GURL& online_url, | 617 const GURL& online_url, |
| 608 const MultipleOfflinePageItemCallback& callback) { | 618 const MultipleOfflinePageItemCallback& callback) { |
| 609 RunWhenLoaded( | 619 RunWhenLoaded( |
| 610 base::Bind(&OfflinePageModelImpl::GetPagesByOnlineURLWhenLoadDone, | 620 base::Bind(&OfflinePageModelImpl::GetPagesByOnlineURLWhenLoadDone, |
| 611 weak_ptr_factory_.GetWeakPtr(), online_url, callback)); | 621 weak_ptr_factory_.GetWeakPtr(), online_url, callback)); |
| 612 } | 622 } |
| 613 | 623 |
| (...skipping 466 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1080 } | 1090 } |
| 1081 | 1091 |
| 1082 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, task); | 1092 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, task); |
| 1083 } | 1093 } |
| 1084 | 1094 |
| 1085 base::Time OfflinePageModelImpl::GetCurrentTime() const { | 1095 base::Time OfflinePageModelImpl::GetCurrentTime() const { |
| 1086 return testing_clock_ ? testing_clock_->Now() : base::Time::Now(); | 1096 return testing_clock_ ? testing_clock_->Now() : base::Time::Now(); |
| 1087 } | 1097 } |
| 1088 | 1098 |
| 1089 } // namespace offline_pages | 1099 } // namespace offline_pages |
| OLD | NEW |