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_enumerator.h" |
11 #include "base/files/file_path.h" | 11 #include "base/files/file_path.h" |
12 #include "base/files/file_util.h" | 12 #include "base/files/file_util.h" |
13 #include "base/memory/ptr_util.h" | 13 #include "base/memory/ptr_util.h" |
14 #include "base/metrics/histogram_macros.h" | 14 #include "base/metrics/histogram_macros.h" |
15 #include "base/strings/string_util.h" | 15 #include "base/strings/string_util.h" |
16 #include "components/reading_list/ios/offline_url_utils.h" | 16 #include "components/reading_list/core/offline_url_utils.h" |
17 #include "components/reading_list/ios/reading_list_entry.h" | 17 #include "components/reading_list/core/reading_list_entry.h" |
18 #include "components/reading_list/ios/reading_list_model.h" | 18 #include "components/reading_list/core/reading_list_model.h" |
19 #include "ios/chrome/browser/reading_list/reading_list_distiller_page_factory.h" | 19 #include "ios/chrome/browser/reading_list/reading_list_distiller_page_factory.h" |
20 #include "ios/web/public/web_thread.h" | 20 #include "ios/web/public/web_thread.h" |
21 | 21 |
22 namespace { | 22 namespace { |
23 // Status of the download when it ends, for UMA report. | 23 // Status of the download when it ends, for UMA report. |
24 // These match tools/metrics/histograms/histograms.xml. | 24 // These match tools/metrics/histograms/histograms.xml. |
25 enum UMADownloadStatus { | 25 enum UMADownloadStatus { |
26 // The download was successful. | 26 // The download was successful. |
27 SUCCESS = 0, | 27 SUCCESS = 0, |
28 // The download failed and it won't be retried. | 28 // The download failed and it won't be retried. |
(...skipping 104 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
133 const ReadingListEntry* entry = reading_list_model_->GetEntryByURL(url); | 133 const ReadingListEntry* entry = reading_list_model_->GetEntryByURL(url); |
134 switch (entry->DistilledState()) { | 134 switch (entry->DistilledState()) { |
135 case ReadingListEntry::PROCESSED: | 135 case ReadingListEntry::PROCESSED: |
136 processed_directories.insert(reading_list::OfflineURLDirectoryID(url)); | 136 processed_directories.insert(reading_list::OfflineURLDirectoryID(url)); |
137 break; | 137 break; |
138 case ReadingListEntry::WAITING: | 138 case ReadingListEntry::WAITING: |
139 case ReadingListEntry::PROCESSING: | 139 case ReadingListEntry::PROCESSING: |
140 case ReadingListEntry::WILL_RETRY: | 140 case ReadingListEntry::WILL_RETRY: |
141 unprocessed_entries.insert(url); | 141 unprocessed_entries.insert(url); |
142 break; | 142 break; |
143 case ReadingListEntry::ERROR: | 143 case ReadingListEntry::DISTILLATION_ERROR: |
144 break; | 144 break; |
145 } | 145 } |
146 } | 146 } |
147 web::WebThread::PostTaskAndReply( | 147 web::WebThread::PostTaskAndReply( |
148 web::WebThread::FILE, FROM_HERE, | 148 web::WebThread::FILE, FROM_HERE, |
149 base::Bind(&ReadingListDownloadService::CleanUpFiles, | 149 base::Bind(&ReadingListDownloadService::CleanUpFiles, |
150 base::Unretained(this), processed_directories), | 150 base::Unretained(this), processed_directories), |
151 base::Bind(&ReadingListDownloadService::DownloadUnprocessedEntries, | 151 base::Bind(&ReadingListDownloadService::DownloadUnprocessedEntries, |
152 base::Unretained(this), unprocessed_entries)); | 152 base::Unretained(this), unprocessed_entries)); |
153 } | 153 } |
(...skipping 14 matching lines...) Expand all Loading... |
168 void ReadingListDownloadService::DownloadUnprocessedEntries( | 168 void ReadingListDownloadService::DownloadUnprocessedEntries( |
169 const std::set<GURL>& unprocessed_entries) { | 169 const std::set<GURL>& unprocessed_entries) { |
170 for (const GURL& url : unprocessed_entries) { | 170 for (const GURL& url : unprocessed_entries) { |
171 this->ScheduleDownloadEntry(url); | 171 this->ScheduleDownloadEntry(url); |
172 } | 172 } |
173 } | 173 } |
174 | 174 |
175 void ReadingListDownloadService::ScheduleDownloadEntry(const GURL& url) { | 175 void ReadingListDownloadService::ScheduleDownloadEntry(const GURL& url) { |
176 DCHECK(reading_list_model_->loaded()); | 176 DCHECK(reading_list_model_->loaded()); |
177 const ReadingListEntry* entry = reading_list_model_->GetEntryByURL(url); | 177 const ReadingListEntry* entry = reading_list_model_->GetEntryByURL(url); |
178 if (!entry || entry->DistilledState() == ReadingListEntry::ERROR || | 178 if (!entry || |
| 179 entry->DistilledState() == ReadingListEntry::DISTILLATION_ERROR || |
179 entry->DistilledState() == ReadingListEntry::PROCESSED || entry->IsRead()) | 180 entry->DistilledState() == ReadingListEntry::PROCESSED || entry->IsRead()) |
180 return; | 181 return; |
181 GURL local_url(url); | 182 GURL local_url(url); |
182 web::WebThread::PostDelayedTask( | 183 web::WebThread::PostDelayedTask( |
183 web::WebThread::UI, FROM_HERE, | 184 web::WebThread::UI, FROM_HERE, |
184 base::Bind(&ReadingListDownloadService::DownloadEntry, | 185 base::Bind(&ReadingListDownloadService::DownloadEntry, |
185 weak_ptr_factory_.GetWeakPtr(), local_url), | 186 weak_ptr_factory_.GetWeakPtr(), local_url), |
186 entry->TimeUntilNextTry()); | 187 entry->TimeUntilNextTry()); |
187 } | 188 } |
188 | 189 |
189 void ReadingListDownloadService::DownloadEntry(const GURL& url) { | 190 void ReadingListDownloadService::DownloadEntry(const GURL& url) { |
190 DCHECK(reading_list_model_->loaded()); | 191 DCHECK(reading_list_model_->loaded()); |
191 const ReadingListEntry* entry = reading_list_model_->GetEntryByURL(url); | 192 const ReadingListEntry* entry = reading_list_model_->GetEntryByURL(url); |
192 if (!entry || entry->DistilledState() == ReadingListEntry::ERROR || | 193 if (!entry || |
| 194 entry->DistilledState() == ReadingListEntry::DISTILLATION_ERROR || |
193 entry->DistilledState() == ReadingListEntry::PROCESSED || entry->IsRead()) | 195 entry->DistilledState() == ReadingListEntry::PROCESSED || entry->IsRead()) |
194 return; | 196 return; |
195 | 197 |
196 if (net::NetworkChangeNotifier::IsOffline()) { | 198 if (net::NetworkChangeNotifier::IsOffline()) { |
197 // There is no connection, save it for download only if we did not exceed | 199 // There is no connection, save it for download only if we did not exceed |
198 // the maximaxum number of tries. | 200 // the maximaxum number of tries. |
199 if (entry->FailedDownloadCounter() < kNumberOfFailsBeforeWifiOnly) | 201 if (entry->FailedDownloadCounter() < kNumberOfFailsBeforeWifiOnly) |
200 url_to_download_cellular_.push_back(entry->URL()); | 202 url_to_download_cellular_.push_back(entry->URL()); |
201 if (entry->FailedDownloadCounter() < kNumberOfFailsBeforeStop) | 203 if (entry->FailedDownloadCounter() < kNumberOfFailsBeforeStop) |
202 url_to_download_wifi_.push_back(entry->URL()); | 204 url_to_download_wifi_.push_back(entry->URL()); |
(...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
268 if (entry && | 270 if (entry && |
269 entry->FailedDownloadCounter() + 1 < kNumberOfFailsBeforeStop) { | 271 entry->FailedDownloadCounter() + 1 < kNumberOfFailsBeforeStop) { |
270 reading_list_model_->SetEntryDistilledState( | 272 reading_list_model_->SetEntryDistilledState( |
271 url, ReadingListEntry::WILL_RETRY); | 273 url, ReadingListEntry::WILL_RETRY); |
272 ScheduleDownloadEntry(url); | 274 ScheduleDownloadEntry(url); |
273 UMA_HISTOGRAM_ENUMERATION("ReadingList.Download.Status", RETRY, | 275 UMA_HISTOGRAM_ENUMERATION("ReadingList.Download.Status", RETRY, |
274 STATUS_MAX); | 276 STATUS_MAX); |
275 } else { | 277 } else { |
276 UMA_HISTOGRAM_ENUMERATION("ReadingList.Download.Status", FAILURE, | 278 UMA_HISTOGRAM_ENUMERATION("ReadingList.Download.Status", FAILURE, |
277 STATUS_MAX); | 279 STATUS_MAX); |
278 reading_list_model_->SetEntryDistilledState(url, | 280 reading_list_model_->SetEntryDistilledState( |
279 ReadingListEntry::ERROR); | 281 url, ReadingListEntry::DISTILLATION_ERROR); |
280 } | 282 } |
281 break; | 283 break; |
282 } | 284 } |
283 } | 285 } |
284 } | 286 } |
285 | 287 |
286 void ReadingListDownloadService::OnDeleteEnd(const GURL& url, bool success) { | 288 void ReadingListDownloadService::OnDeleteEnd(const GURL& url, bool success) { |
287 // Nothing to update as this is only called when deleting reading list entries | 289 // Nothing to update as this is only called when deleting reading list entries |
288 } | 290 } |
289 | 291 |
290 void ReadingListDownloadService::OnConnectionTypeChanged( | 292 void ReadingListDownloadService::OnConnectionTypeChanged( |
291 net::NetworkChangeNotifier::ConnectionType type) { | 293 net::NetworkChangeNotifier::ConnectionType type) { |
292 if (type == net::NetworkChangeNotifier::CONNECTION_NONE) { | 294 if (type == net::NetworkChangeNotifier::CONNECTION_NONE) { |
293 had_connection_ = false; | 295 had_connection_ = false; |
294 return; | 296 return; |
295 } | 297 } |
296 | 298 |
297 if (!had_connection_) { | 299 if (!had_connection_) { |
298 had_connection_ = true; | 300 had_connection_ = true; |
299 for (auto& url : url_to_download_cellular_) { | 301 for (auto& url : url_to_download_cellular_) { |
300 ScheduleDownloadEntry(url); | 302 ScheduleDownloadEntry(url); |
301 } | 303 } |
302 } | 304 } |
303 if (type == net::NetworkChangeNotifier::CONNECTION_WIFI) { | 305 if (type == net::NetworkChangeNotifier::CONNECTION_WIFI) { |
304 for (auto& url : url_to_download_wifi_) { | 306 for (auto& url : url_to_download_wifi_) { |
305 ScheduleDownloadEntry(url); | 307 ScheduleDownloadEntry(url); |
306 } | 308 } |
307 } | 309 } |
308 } | 310 } |
OLD | NEW |