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

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

Issue 1959393002: Choose the best offline page when given an online URL. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Address nits. Created 4 years, 7 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 2015 The Chromium Authors. All rights reserved. 1 // Copyright 2015 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.h" 5 #include "components/offline_pages/offline_page_model.h"
6 6
7 #include <algorithm> 7 #include <algorithm>
8 #include <utility> 8 #include <utility>
9 9
10 #include "base/bind.h" 10 #include "base/bind.h"
(...skipping 411 matching lines...) Expand 10 before | Expand all | Expand 10 after
422 422
423 const OfflinePageItem* OfflinePageModel::MaybeGetPageByOfflineURL( 423 const OfflinePageItem* OfflinePageModel::MaybeGetPageByOfflineURL(
424 const GURL& offline_url) const { 424 const GURL& offline_url) const {
425 for (const auto& id_page_pair : offline_pages_) { 425 for (const auto& id_page_pair : offline_pages_) {
426 if (id_page_pair.second.GetOfflineURL() == offline_url) 426 if (id_page_pair.second.GetOfflineURL() == offline_url)
427 return &(id_page_pair.second); 427 return &(id_page_pair.second);
428 } 428 }
429 return nullptr; 429 return nullptr;
430 } 430 }
431 431
432 void OfflinePageModel::GetBestPageForOnlineURL(
433 const GURL& online_url,
434 const SingleOfflinePageItemCallback callback) {
435 RunWhenLoaded(
436 base::Bind(&OfflinePageModel::GetBestPageForOnlineURLWhenLoadDone,
437 weak_ptr_factory_.GetWeakPtr(), online_url, callback));
438 }
439
440 void OfflinePageModel::GetBestPageForOnlineURLWhenLoadDone(
441 const GURL& online_url,
442 const SingleOfflinePageItemCallback& callback) const {
443 SingleOfflinePageItemResult result;
444
445 const OfflinePageItem* best_page = MaybeGetBestPageForOnlineURL(online_url);
446 if (best_page != nullptr)
447 result = *best_page;
448
449 callback.Run(result);
450 }
451
432 void OfflinePageModel::GetPagesByOnlineURL( 452 void OfflinePageModel::GetPagesByOnlineURL(
433 const GURL& online_url, 453 const GURL& online_url,
434 const MultipleOfflinePageItemCallback& callback) { 454 const MultipleOfflinePageItemCallback& callback) {
435 RunWhenLoaded(base::Bind(&OfflinePageModel::GetPagesByOnlineURLWhenLoadDone, 455 RunWhenLoaded(base::Bind(&OfflinePageModel::GetPagesByOnlineURLWhenLoadDone,
436 weak_ptr_factory_.GetWeakPtr(), online_url, 456 weak_ptr_factory_.GetWeakPtr(), online_url,
437 callback)); 457 callback));
438 } 458 }
439 459
440 void OfflinePageModel::GetPagesByOnlineURLWhenLoadDone( 460 void OfflinePageModel::GetPagesByOnlineURLWhenLoadDone(
441 const GURL& online_url, 461 const GURL& online_url,
442 const MultipleOfflinePageItemCallback& callback) const { 462 const MultipleOfflinePageItemCallback& callback) const {
443 std::vector<OfflinePageItem> result; 463 std::vector<OfflinePageItem> result;
444 464
445 for (const auto& id_page_pair : offline_pages_) { 465 for (const auto& id_page_pair : offline_pages_) {
446 if (id_page_pair.second.url == online_url) 466 if (id_page_pair.second.url == online_url)
447 result.push_back(id_page_pair.second); 467 result.push_back(id_page_pair.second);
448 } 468 }
449 469
450 callback.Run(result); 470 callback.Run(result);
451 } 471 }
452 472
453 const OfflinePageItem* OfflinePageModel::MaybeGetPageByOnlineURL( 473 const OfflinePageItem* OfflinePageModel::MaybeGetBestPageForOnlineURL(
454 const GURL& online_url) const { 474 const GURL& online_url) const {
475 const OfflinePageItem* result = nullptr;
455 for (const auto& id_page_pair : offline_pages_) { 476 for (const auto& id_page_pair : offline_pages_) {
456 if (id_page_pair.second.url == online_url) 477 if (id_page_pair.second.url == online_url) {
457 return &(id_page_pair.second); 478 if (!result || id_page_pair.second.creation_time > result->creation_time)
479 result = &(id_page_pair.second);
480 }
458 } 481 }
459 return nullptr; 482 return result;
460 } 483 }
461 484
462 void OfflinePageModel::CheckForExternalFileDeletion() { 485 void OfflinePageModel::CheckForExternalFileDeletion() {
463 DCHECK(is_loaded_); 486 DCHECK(is_loaded_);
464 487
465 std::vector<std::pair<int64_t, base::FilePath>> id_path_pairs; 488 std::vector<std::pair<int64_t, base::FilePath>> id_path_pairs;
466 for (const auto& id_page_pair : offline_pages_) { 489 for (const auto& id_page_pair : offline_pages_) {
467 id_path_pairs.push_back( 490 id_path_pairs.push_back(
468 std::make_pair(id_page_pair.first, id_page_pair.second.file_path)); 491 std::make_pair(id_page_pair.first, id_page_pair.second.file_path));
469 } 492 }
(...skipping 339 matching lines...) Expand 10 before | Expand all | Expand 10 after
809 void OfflinePageModel::RunWhenLoaded(const base::Closure& task) { 832 void OfflinePageModel::RunWhenLoaded(const base::Closure& task) {
810 if (!is_loaded_) { 833 if (!is_loaded_) {
811 delayed_tasks_.push_back(task); 834 delayed_tasks_.push_back(task);
812 return; 835 return;
813 } 836 }
814 837
815 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, task); 838 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, task);
816 } 839 }
817 840
818 } // namespace offline_pages 841 } // namespace offline_pages
OLDNEW
« no previous file with comments | « components/offline_pages/offline_page_model.h ('k') | components/offline_pages/offline_page_model_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698