| 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 317 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 328 | 328 |
| 329 void OfflinePageModelImpl::AddObserver(Observer* observer) { | 329 void OfflinePageModelImpl::AddObserver(Observer* observer) { |
| 330 observers_.AddObserver(observer); | 330 observers_.AddObserver(observer); |
| 331 } | 331 } |
| 332 | 332 |
| 333 void OfflinePageModelImpl::RemoveObserver(Observer* observer) { | 333 void OfflinePageModelImpl::RemoveObserver(Observer* observer) { |
| 334 observers_.RemoveObserver(observer); | 334 observers_.RemoveObserver(observer); |
| 335 } | 335 } |
| 336 | 336 |
| 337 void OfflinePageModelImpl::SavePage( | 337 void OfflinePageModelImpl::SavePage( |
| 338 const GURL& url, | 338 const SavePageParams& save_page_params, |
| 339 const ClientId& client_id, | |
| 340 int64_t proposed_offline_id, | |
| 341 std::unique_ptr<OfflinePageArchiver> archiver, | 339 std::unique_ptr<OfflinePageArchiver> archiver, |
| 342 const SavePageCallback& callback) { | 340 const SavePageCallback& callback) { |
| 343 DCHECK(is_loaded_); | 341 DCHECK(is_loaded_); |
| 344 | 342 |
| 345 // Skip saving the page that is not intended to be saved, like local file | 343 // Skip saving the page that is not intended to be saved, like local file |
| 346 // page. | 344 // page. |
| 347 if (!OfflinePageModel::CanSaveURL(url)) { | 345 if (!OfflinePageModel::CanSaveURL(save_page_params.url)) { |
| 348 InformSavePageDone(callback, SavePageResult::SKIPPED, client_id, | 346 InformSavePageDone(callback, SavePageResult::SKIPPED, |
| 349 kInvalidOfflineId); | 347 save_page_params.client_id, kInvalidOfflineId); |
| 350 return; | 348 return; |
| 351 } | 349 } |
| 352 | 350 |
| 353 // The web contents is not available if archiver is not created and passed. | 351 // The web contents is not available if archiver is not created and passed. |
| 354 if (!archiver.get()) { | 352 if (!archiver.get()) { |
| 355 InformSavePageDone(callback, SavePageResult::CONTENT_UNAVAILABLE, client_id, | 353 InformSavePageDone(callback, SavePageResult::CONTENT_UNAVAILABLE, |
| 356 kInvalidOfflineId); | 354 save_page_params.client_id, kInvalidOfflineId); |
| 357 return; | 355 return; |
| 358 } | 356 } |
| 359 | 357 |
| 360 // If we already have an offline id, use it. If not, generate one. | 358 // If we already have an offline id, use it. If not, generate one. |
| 361 if (proposed_offline_id == kInvalidOfflineId) | 359 int64_t offline_id = save_page_params.proposed_offline_id; |
| 362 proposed_offline_id = GenerateOfflineId(); | 360 if (offline_id == kInvalidOfflineId) |
| 361 offline_id = GenerateOfflineId(); |
| 363 | 362 |
| 364 archiver->CreateArchive( | 363 archiver->CreateArchive( |
| 365 archives_dir_, proposed_offline_id, | 364 archives_dir_, offline_id, |
| 366 base::Bind(&OfflinePageModelImpl::OnCreateArchiveDone, | 365 base::Bind(&OfflinePageModelImpl::OnCreateArchiveDone, |
| 367 weak_ptr_factory_.GetWeakPtr(), url, proposed_offline_id, | 366 weak_ptr_factory_.GetWeakPtr(), save_page_params, offline_id, |
| 368 client_id, GetCurrentTime(), callback)); | 367 GetCurrentTime(), callback)); |
| 369 pending_archivers_.push_back(std::move(archiver)); | 368 pending_archivers_.push_back(std::move(archiver)); |
| 370 } | 369 } |
| 371 | 370 |
| 372 void OfflinePageModelImpl::MarkPageAccessed(int64_t offline_id) { | 371 void OfflinePageModelImpl::MarkPageAccessed(int64_t offline_id) { |
| 373 RunWhenLoaded(base::Bind(&OfflinePageModelImpl::MarkPageAccessedWhenLoadDone, | 372 RunWhenLoaded(base::Bind(&OfflinePageModelImpl::MarkPageAccessedWhenLoadDone, |
| 374 weak_ptr_factory_.GetWeakPtr(), offline_id)); | 373 weak_ptr_factory_.GetWeakPtr(), offline_id)); |
| 375 } | 374 } |
| 376 | 375 |
| 377 void OfflinePageModelImpl::MarkPageAccessedWhenLoadDone(int64_t offline_id) { | 376 void OfflinePageModelImpl::MarkPageAccessedWhenLoadDone(int64_t offline_id) { |
| 378 DCHECK(is_loaded_); | 377 DCHECK(is_loaded_); |
| (...skipping 340 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 719 } | 718 } |
| 720 | 719 |
| 721 bool OfflinePageModelImpl::is_loaded() const { | 720 bool OfflinePageModelImpl::is_loaded() const { |
| 722 return is_loaded_; | 721 return is_loaded_; |
| 723 } | 722 } |
| 724 | 723 |
| 725 OfflineEventLogger* OfflinePageModelImpl::GetLogger() { | 724 OfflineEventLogger* OfflinePageModelImpl::GetLogger() { |
| 726 return &offline_event_logger_; | 725 return &offline_event_logger_; |
| 727 } | 726 } |
| 728 | 727 |
| 729 void OfflinePageModelImpl::OnCreateArchiveDone(const GURL& requested_url, | 728 void OfflinePageModelImpl::OnCreateArchiveDone( |
| 730 int64_t offline_id, | 729 const SavePageParams& save_page_params, |
| 731 const ClientId& client_id, | 730 int64_t offline_id, |
| 732 const base::Time& start_time, | 731 const base::Time& start_time, |
| 733 const SavePageCallback& callback, | 732 const SavePageCallback& callback, |
| 734 OfflinePageArchiver* archiver, | 733 OfflinePageArchiver* archiver, |
| 735 ArchiverResult archiver_result, | 734 ArchiverResult archiver_result, |
| 736 const GURL& url, | 735 const GURL& url, |
| 737 const base::FilePath& file_path, | 736 const base::FilePath& file_path, |
| 738 const base::string16& title, | 737 const base::string16& title, |
| 739 int64_t file_size) { | 738 int64_t file_size) { |
| 740 if (requested_url != url) { | 739 if (save_page_params.url != url) { |
| 741 DVLOG(1) << "Saved URL does not match requested URL."; | 740 DVLOG(1) << "Saved URL does not match requested URL."; |
| 742 // TODO(fgorski): We have created an archive for a wrong URL. It should be | 741 // TODO(fgorski): We have created an archive for a wrong URL. It should be |
| 743 // deleted from here, once archiver has the right functionality. | 742 // deleted from here, once archiver has the right functionality. |
| 744 InformSavePageDone(callback, SavePageResult::ARCHIVE_CREATION_FAILED, | 743 InformSavePageDone(callback, SavePageResult::ARCHIVE_CREATION_FAILED, |
| 745 client_id, offline_id); | 744 save_page_params.client_id, offline_id); |
| 746 DeletePendingArchiver(archiver); | 745 DeletePendingArchiver(archiver); |
| 747 return; | 746 return; |
| 748 } | 747 } |
| 749 | 748 |
| 750 if (archiver_result != ArchiverResult::SUCCESSFULLY_CREATED) { | 749 if (archiver_result != ArchiverResult::SUCCESSFULLY_CREATED) { |
| 751 SavePageResult result = ToSavePageResult(archiver_result); | 750 SavePageResult result = ToSavePageResult(archiver_result); |
| 752 InformSavePageDone(callback, result, client_id, offline_id); | 751 InformSavePageDone( |
| 752 callback, result, save_page_params.client_id, offline_id); |
| 753 DeletePendingArchiver(archiver); | 753 DeletePendingArchiver(archiver); |
| 754 return; | 754 return; |
| 755 } | 755 } |
| 756 OfflinePageItem offline_page_item(url, offline_id, client_id, file_path, | 756 OfflinePageItem offline_page_item(url, offline_id, save_page_params.client_id, |
| 757 file_size, start_time); | 757 file_path, file_size, start_time); |
| 758 offline_page_item.title = title; | 758 offline_page_item.title = title; |
| 759 offline_page_item.original_url = save_page_params.original_url; |
| 759 store_->AddOfflinePage(offline_page_item, | 760 store_->AddOfflinePage(offline_page_item, |
| 760 base::Bind(&OfflinePageModelImpl::OnAddOfflinePageDone, | 761 base::Bind(&OfflinePageModelImpl::OnAddOfflinePageDone, |
| 761 weak_ptr_factory_.GetWeakPtr(), archiver, | 762 weak_ptr_factory_.GetWeakPtr(), archiver, |
| 762 callback, offline_page_item)); | 763 callback, offline_page_item)); |
| 763 } | 764 } |
| 764 | 765 |
| 765 void OfflinePageModelImpl::OnAddOfflinePageDone( | 766 void OfflinePageModelImpl::OnAddOfflinePageDone( |
| 766 OfflinePageArchiver* archiver, | 767 OfflinePageArchiver* archiver, |
| 767 const SavePageCallback& callback, | 768 const SavePageCallback& callback, |
| 768 const OfflinePageItem& offline_page, | 769 const OfflinePageItem& offline_page, |
| (...skipping 313 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1082 } | 1083 } |
| 1083 | 1084 |
| 1084 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, task); | 1085 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, task); |
| 1085 } | 1086 } |
| 1086 | 1087 |
| 1087 base::Time OfflinePageModelImpl::GetCurrentTime() const { | 1088 base::Time OfflinePageModelImpl::GetCurrentTime() const { |
| 1088 return testing_clock_ ? testing_clock_->Now() : base::Time::Now(); | 1089 return testing_clock_ ? testing_clock_->Now() : base::Time::Now(); |
| 1089 } | 1090 } |
| 1090 | 1091 |
| 1091 } // namespace offline_pages | 1092 } // namespace offline_pages |
| OLD | NEW |