| 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 "ios/chrome/browser/reading_list/reading_list_download_service.h" | 5 #include "ios/chrome/browser/reading_list/reading_list_download_service.h" |
| 6 | 6 |
| 7 #include <utility> | 7 #include <utility> |
| 8 | 8 |
| 9 #include "base/bind.h" | 9 #include "base/bind.h" |
| 10 #include "base/files/file_enumerator.h" |
| 10 #include "base/files/file_path.h" | 11 #include "base/files/file_path.h" |
| 12 #include "base/files/file_util.h" |
| 11 #include "base/memory/ptr_util.h" | 13 #include "base/memory/ptr_util.h" |
| 12 #include "base/metrics/histogram_macros.h" | 14 #include "base/metrics/histogram_macros.h" |
| 13 #include "components/reading_list/ios/offline_url_utils.h" | 15 #include "components/reading_list/ios/offline_url_utils.h" |
| 14 #include "components/reading_list/ios/reading_list_entry.h" | 16 #include "components/reading_list/ios/reading_list_entry.h" |
| 15 #include "components/reading_list/ios/reading_list_model.h" | 17 #include "components/reading_list/ios/reading_list_model.h" |
| 16 #include "ios/chrome/browser/reading_list/reading_list_distiller_page_factory.h" | 18 #include "ios/chrome/browser/reading_list/reading_list_distiller_page_factory.h" |
| 17 #include "ios/web/public/web_thread.h" | 19 #include "ios/web/public/web_thread.h" |
| 18 | 20 |
| 19 namespace { | 21 namespace { |
| 20 // Status of the download when it ends, for UMA report. | 22 // Status of the download when it ends, for UMA report. |
| (...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 74 return reading_list::OfflineRootDirectoryPath(chrome_profile_path_); | 76 return reading_list::OfflineRootDirectoryPath(chrome_profile_path_); |
| 75 } | 77 } |
| 76 | 78 |
| 77 void ReadingListDownloadService::Shutdown() { | 79 void ReadingListDownloadService::Shutdown() { |
| 78 reading_list_model_->RemoveObserver(this); | 80 reading_list_model_->RemoveObserver(this); |
| 79 } | 81 } |
| 80 | 82 |
| 81 void ReadingListDownloadService::ReadingListModelLoaded( | 83 void ReadingListDownloadService::ReadingListModelLoaded( |
| 82 const ReadingListModel* model) { | 84 const ReadingListModel* model) { |
| 83 DCHECK_EQ(reading_list_model_, model); | 85 DCHECK_EQ(reading_list_model_, model); |
| 84 DownloadAllEntries(); | 86 SyncWithModel(); |
| 85 } | 87 } |
| 86 | 88 |
| 87 void ReadingListDownloadService::ReadingListWillRemoveEntry( | 89 void ReadingListDownloadService::ReadingListWillRemoveEntry( |
| 88 const ReadingListModel* model, | 90 const ReadingListModel* model, |
| 89 const GURL& url) { | 91 const GURL& url) { |
| 90 DCHECK_EQ(reading_list_model_, model); | 92 DCHECK_EQ(reading_list_model_, model); |
| 91 DCHECK(model->GetEntryByURL(url)); | 93 DCHECK(model->GetEntryByURL(url)); |
| 92 RemoveDownloadedEntry(url); | 94 RemoveDownloadedEntry(url); |
| 93 } | 95 } |
| 94 | 96 |
| (...skipping 14 matching lines...) Expand all Loading... |
| 109 | 111 |
| 110 void ReadingListDownloadService::ProcessNewEntry(const GURL& url) { | 112 void ReadingListDownloadService::ProcessNewEntry(const GURL& url) { |
| 111 const ReadingListEntry* entry = reading_list_model_->GetEntryByURL(url); | 113 const ReadingListEntry* entry = reading_list_model_->GetEntryByURL(url); |
| 112 if (!entry || entry->IsRead()) { | 114 if (!entry || entry->IsRead()) { |
| 113 url_downloader_->CancelDownloadOfflineURL(url); | 115 url_downloader_->CancelDownloadOfflineURL(url); |
| 114 } else { | 116 } else { |
| 115 ScheduleDownloadEntry(url); | 117 ScheduleDownloadEntry(url); |
| 116 } | 118 } |
| 117 } | 119 } |
| 118 | 120 |
| 119 void ReadingListDownloadService::DownloadAllEntries() { | 121 void ReadingListDownloadService::SyncWithModel() { |
| 120 DCHECK(reading_list_model_->loaded()); | 122 DCHECK(reading_list_model_->loaded()); |
| 123 std::set<std::string> processed_directories; |
| 124 std::set<GURL> unprocessed_entries; |
| 121 for (const auto& url : reading_list_model_->Keys()) { | 125 for (const auto& url : reading_list_model_->Keys()) { |
| 126 const ReadingListEntry* entry = reading_list_model_->GetEntryByURL(url); |
| 127 switch (entry->DistilledState()) { |
| 128 case ReadingListEntry::PROCESSED: |
| 129 processed_directories.insert(reading_list::OfflineURLDirectoryID(url)); |
| 130 break; |
| 131 case ReadingListEntry::WAITING: |
| 132 case ReadingListEntry::PROCESSING: |
| 133 case ReadingListEntry::WILL_RETRY: |
| 134 unprocessed_entries.insert(url); |
| 135 break; |
| 136 case ReadingListEntry::ERROR: |
| 137 break; |
| 138 } |
| 139 } |
| 140 web::WebThread::PostTaskAndReply( |
| 141 web::WebThread::FILE, FROM_HERE, |
| 142 base::Bind(&ReadingListDownloadService::CleanUpFiles, |
| 143 base::Unretained(this), processed_directories), |
| 144 base::Bind(&ReadingListDownloadService::DownloadUnprocessedEntries, |
| 145 base::Unretained(this), unprocessed_entries)); |
| 146 } |
| 147 |
| 148 void ReadingListDownloadService::CleanUpFiles( |
| 149 const std::set<std::string>& processed_directories) { |
| 150 base::FileEnumerator file_enumerator(OfflineRoot(), false, |
| 151 base::FileEnumerator::DIRECTORIES); |
| 152 for (base::FilePath sub_directory = file_enumerator.Next(); |
| 153 !sub_directory.empty(); sub_directory = file_enumerator.Next()) { |
| 154 std::string directory_name = sub_directory.BaseName().value(); |
| 155 if (!processed_directories.count(directory_name)) { |
| 156 base::DeleteFile(sub_directory, true); |
| 157 } |
| 158 } |
| 159 } |
| 160 |
| 161 void ReadingListDownloadService::DownloadUnprocessedEntries( |
| 162 const std::set<GURL>& unprocessed_entries) { |
| 163 for (const GURL& url : unprocessed_entries) { |
| 122 this->ScheduleDownloadEntry(url); | 164 this->ScheduleDownloadEntry(url); |
| 123 } | 165 } |
| 124 } | 166 } |
| 125 | 167 |
| 126 void ReadingListDownloadService::ScheduleDownloadEntry(const GURL& url) { | 168 void ReadingListDownloadService::ScheduleDownloadEntry(const GURL& url) { |
| 127 DCHECK(reading_list_model_->loaded()); | 169 DCHECK(reading_list_model_->loaded()); |
| 128 const ReadingListEntry* entry = reading_list_model_->GetEntryByURL(url); | 170 const ReadingListEntry* entry = reading_list_model_->GetEntryByURL(url); |
| 129 if (!entry || entry->DistilledState() == ReadingListEntry::ERROR || | 171 if (!entry || entry->DistilledState() == ReadingListEntry::ERROR || |
| 130 entry->DistilledState() == ReadingListEntry::PROCESSED || entry->IsRead()) | 172 entry->DistilledState() == ReadingListEntry::PROCESSED || entry->IsRead()) |
| 131 return; | 173 return; |
| (...skipping 112 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 244 for (auto& url : url_to_download_cellular_) { | 286 for (auto& url : url_to_download_cellular_) { |
| 245 ScheduleDownloadEntry(url); | 287 ScheduleDownloadEntry(url); |
| 246 } | 288 } |
| 247 } | 289 } |
| 248 if (type == net::NetworkChangeNotifier::CONNECTION_WIFI) { | 290 if (type == net::NetworkChangeNotifier::CONNECTION_WIFI) { |
| 249 for (auto& url : url_to_download_wifi_) { | 291 for (auto& url : url_to_download_wifi_) { |
| 250 ScheduleDownloadEntry(url); | 292 ScheduleDownloadEntry(url); |
| 251 } | 293 } |
| 252 } | 294 } |
| 253 } | 295 } |
| OLD | NEW |