Chromium Code Reviews| 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_path.h" | 10 #include "base/files/file_path.h" |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 51 void ReadingListDownloadService::Shutdown() { | 51 void ReadingListDownloadService::Shutdown() { |
| 52 reading_list_model_->RemoveObserver(this); | 52 reading_list_model_->RemoveObserver(this); |
| 53 } | 53 } |
| 54 | 54 |
| 55 void ReadingListDownloadService::ReadingListModelLoaded( | 55 void ReadingListDownloadService::ReadingListModelLoaded( |
| 56 const ReadingListModel* model) { | 56 const ReadingListModel* model) { |
| 57 DCHECK_EQ(reading_list_model_, model); | 57 DCHECK_EQ(reading_list_model_, model); |
| 58 DownloadAllEntries(); | 58 DownloadAllEntries(); |
| 59 } | 59 } |
| 60 | 60 |
| 61 void ReadingListDownloadService::ReadingListWillRemoveReadEntry( | 61 void ReadingListDownloadService::ReadingListWillRemoveEntry( |
| 62 const ReadingListModel* model, | 62 const ReadingListModel* model, |
| 63 size_t index) { | 63 const GURL& url) { |
| 64 DCHECK_EQ(reading_list_model_, model); | 64 DCHECK_EQ(reading_list_model_, model); |
| 65 RemoveDownloadedEntry(model->GetReadEntryAtIndex(index)); | 65 DCHECK(model->GetEntryByURL(url)); |
| 66 RemoveDownloadedEntry(url); | |
| 66 } | 67 } |
| 67 | 68 |
| 68 void ReadingListDownloadService::ReadingListWillRemoveUnreadEntry( | 69 void ReadingListDownloadService::ReadingListDidAddEntry( |
| 69 const ReadingListModel* model, | 70 const ReadingListModel* model, |
| 70 size_t index) { | 71 const GURL& url) { |
| 71 DCHECK_EQ(reading_list_model_, model); | 72 DCHECK_EQ(reading_list_model_, model); |
| 72 RemoveDownloadedEntry(model->GetUnreadEntryAtIndex(index)); | 73 ScheduleDownloadEntry(url); |
| 73 } | |
| 74 | |
| 75 void ReadingListDownloadService::ReadingListWillAddUnreadEntry( | |
| 76 const ReadingListModel* model, | |
| 77 const ReadingListEntry& entry) { | |
| 78 DCHECK_EQ(reading_list_model_, model); | |
| 79 ScheduleDownloadEntry(entry); | |
| 80 } | |
| 81 | |
| 82 void ReadingListDownloadService::ReadingListWillAddReadEntry( | |
| 83 const ReadingListModel* model, | |
| 84 const ReadingListEntry& entry) { | |
| 85 DCHECK_EQ(reading_list_model_, model); | |
| 86 ScheduleDownloadEntry(entry); | |
| 87 } | 74 } |
| 88 | 75 |
| 89 void ReadingListDownloadService::DownloadAllEntries() { | 76 void ReadingListDownloadService::DownloadAllEntries() { |
| 90 DCHECK(reading_list_model_->loaded()); | 77 DCHECK(reading_list_model_->loaded()); |
| 91 size_t size = reading_list_model_->unread_size(); | 78 for (const auto& iterator : *reading_list_model_) { |
| 92 for (size_t i = 0; i < size; i++) { | 79 this->ScheduleDownloadEntry(iterator.first); |
| 93 const ReadingListEntry& entry = | |
| 94 reading_list_model_->GetUnreadEntryAtIndex(i); | |
| 95 this->ScheduleDownloadEntry(entry); | |
| 96 } | |
| 97 size = reading_list_model_->read_size(); | |
| 98 for (size_t i = 0; i < size; i++) { | |
| 99 const ReadingListEntry& entry = reading_list_model_->GetReadEntryAtIndex(i); | |
| 100 this->ScheduleDownloadEntry(entry); | |
| 101 } | 80 } |
| 102 } | 81 } |
| 103 | 82 |
| 104 void ReadingListDownloadService::ScheduleDownloadEntry( | 83 void ReadingListDownloadService::ScheduleDownloadEntry(const GURL& url) { |
| 105 const ReadingListEntry& entry) { | |
| 106 DCHECK(reading_list_model_->loaded()); | 84 DCHECK(reading_list_model_->loaded()); |
| 107 if (entry.DistilledState() == ReadingListEntry::ERROR || | 85 const ReadingListEntry* entry = reading_list_model_->GetEntryByURL(url); |
| 108 entry.DistilledState() == ReadingListEntry::PROCESSED) | 86 if (!entry || entry->DistilledState() == ReadingListEntry::ERROR || |
| 87 entry->DistilledState() == ReadingListEntry::PROCESSED) | |
| 109 return; | 88 return; |
| 110 | 89 GURL local_url(url); |
|
jif-google
2016/11/28 16:28:21
I don't understand why we need to use a local GURL
Olivier
2016/11/28 17:58:25
Because it will be posted on another thread.
I wil
jif
2016/11/28 18:15:31
Spoiler:
https://cs.chromium.org/chromium/src/docs
Olivier
2016/11/30 09:15:22
Done.
| |
| 111 web::WebThread::PostDelayedTask( | 90 web::WebThread::PostDelayedTask( |
| 112 web::WebThread::UI, FROM_HERE, | 91 web::WebThread::UI, FROM_HERE, |
| 113 base::Bind(&ReadingListDownloadService::DownloadEntryFromURL, | 92 base::Bind(&ReadingListDownloadService::DownloadEntry, |
| 114 weak_ptr_factory_.GetWeakPtr(), entry.URL()), | 93 weak_ptr_factory_.GetWeakPtr(), local_url), |
| 115 entry.TimeUntilNextTry()); | 94 entry->TimeUntilNextTry()); |
| 116 } | 95 } |
| 117 | 96 |
| 118 void ReadingListDownloadService::ScheduleDownloadEntryFromURL(const GURL& url) { | 97 void ReadingListDownloadService::DownloadEntry(const GURL& url) { |
| 119 auto download_callback = | |
| 120 base::Bind(&ReadingListDownloadService::ScheduleDownloadEntry, | |
| 121 base::Unretained(this)); | |
| 122 reading_list_model_->CallbackEntryURL(url, download_callback); | |
| 123 } | |
| 124 | |
| 125 void ReadingListDownloadService::DownloadEntryFromURL(const GURL& url) { | |
| 126 auto download_callback = base::Bind( | |
| 127 &ReadingListDownloadService::DownloadEntry, base::Unretained(this)); | |
| 128 reading_list_model_->CallbackEntryURL(url, download_callback); | |
| 129 } | |
| 130 | |
| 131 void ReadingListDownloadService::DownloadEntry(const ReadingListEntry& entry) { | |
| 132 DCHECK(reading_list_model_->loaded()); | 98 DCHECK(reading_list_model_->loaded()); |
| 133 if (entry.DistilledState() == ReadingListEntry::ERROR || | 99 const ReadingListEntry* entry = reading_list_model_->GetEntryByURL(url); |
| 134 entry.DistilledState() == ReadingListEntry::PROCESSED) | 100 if (!entry || entry->DistilledState() == ReadingListEntry::ERROR || |
| 101 entry->DistilledState() == ReadingListEntry::PROCESSED) | |
| 135 return; | 102 return; |
| 136 | 103 |
| 137 if (net::NetworkChangeNotifier::IsOffline()) { | 104 if (net::NetworkChangeNotifier::IsOffline()) { |
| 138 // There is no connection, save it for download only if we did not exceed | 105 // There is no connection, save it for download only if we did not exceed |
| 139 // the maximaxum number of tries. | 106 // the maximaxum number of tries. |
| 140 if (entry.FailedDownloadCounter() < kNumberOfFailsBeforeWifiOnly) | 107 if (entry->FailedDownloadCounter() < kNumberOfFailsBeforeWifiOnly) |
| 141 url_to_download_cellular_.push_back(entry.URL()); | 108 url_to_download_cellular_.push_back(entry->URL()); |
| 142 if (entry.FailedDownloadCounter() < kNumberOfFailsBeforeStop) | 109 if (entry->FailedDownloadCounter() < kNumberOfFailsBeforeStop) |
| 143 url_to_download_wifi_.push_back(entry.URL()); | 110 url_to_download_wifi_.push_back(entry->URL()); |
| 144 return; | 111 return; |
| 145 } | 112 } |
| 146 | 113 |
| 147 // There is a connection. | 114 // There is a connection. |
| 148 if (entry.FailedDownloadCounter() < kNumberOfFailsBeforeWifiOnly) { | 115 if (entry->FailedDownloadCounter() < kNumberOfFailsBeforeWifiOnly) { |
| 149 // Try to download the page, whatever the connection. | 116 // Try to download the page, whatever the connection. |
| 150 reading_list_model_->SetEntryDistilledState(entry.URL(), | 117 reading_list_model_->SetEntryDistilledState(entry->URL(), |
| 151 ReadingListEntry::PROCESSING); | 118 ReadingListEntry::PROCESSING); |
| 152 url_downloader_->DownloadOfflineURL(entry.URL()); | 119 url_downloader_->DownloadOfflineURL(entry->URL()); |
| 153 | 120 |
| 154 } else if (entry.FailedDownloadCounter() < kNumberOfFailsBeforeStop) { | 121 } else if (entry->FailedDownloadCounter() < kNumberOfFailsBeforeStop) { |
| 155 // Try to download the page only if the connection is wifi. | 122 // Try to download the page only if the connection is wifi. |
| 156 if (net::NetworkChangeNotifier::GetConnectionType() == | 123 if (net::NetworkChangeNotifier::GetConnectionType() == |
| 157 net::NetworkChangeNotifier::CONNECTION_WIFI) { | 124 net::NetworkChangeNotifier::CONNECTION_WIFI) { |
| 158 // The connection is wifi, download the page. | 125 // The connection is wifi, download the page. |
| 159 reading_list_model_->SetEntryDistilledState(entry.URL(), | 126 reading_list_model_->SetEntryDistilledState(entry->URL(), |
| 160 ReadingListEntry::PROCESSING); | 127 ReadingListEntry::PROCESSING); |
| 161 url_downloader_->DownloadOfflineURL(entry.URL()); | 128 url_downloader_->DownloadOfflineURL(entry->URL()); |
| 162 | 129 |
| 163 } else { | 130 } else { |
| 164 // The connection is not wifi, save it for download when the connection | 131 // The connection is not wifi, save it for download when the connection |
| 165 // changes to wifi. | 132 // changes to wifi. |
| 166 url_to_download_wifi_.push_back(entry.URL()); | 133 url_to_download_wifi_.push_back(entry->URL()); |
| 167 } | 134 } |
| 168 } | 135 } |
| 169 } | 136 } |
| 170 | 137 |
| 171 void ReadingListDownloadService::RemoveDownloadedEntry( | 138 void ReadingListDownloadService::RemoveDownloadedEntry(const GURL& url) { |
| 172 const ReadingListEntry& entry) { | |
| 173 DCHECK(reading_list_model_->loaded()); | 139 DCHECK(reading_list_model_->loaded()); |
| 174 url_downloader_->RemoveOfflineURL(entry.URL()); | 140 url_downloader_->RemoveOfflineURL(url); |
| 175 } | 141 } |
| 176 | 142 |
| 177 void ReadingListDownloadService::OnDownloadEnd( | 143 void ReadingListDownloadService::OnDownloadEnd( |
| 178 const GURL& url, | 144 const GURL& url, |
| 179 URLDownloader::SuccessState success, | 145 URLDownloader::SuccessState success, |
| 180 const base::FilePath& distilled_path, | 146 const base::FilePath& distilled_path, |
| 181 const std::string& title) { | 147 const std::string& title) { |
| 182 DCHECK(reading_list_model_->loaded()); | 148 DCHECK(reading_list_model_->loaded()); |
| 183 if ((success == URLDownloader::DOWNLOAD_SUCCESS || | 149 if ((success == URLDownloader::DOWNLOAD_SUCCESS || |
| 184 success == URLDownloader::DOWNLOAD_EXISTS) && | 150 success == URLDownloader::DOWNLOAD_EXISTS) && |
| 185 !distilled_path.empty()) { | 151 !distilled_path.empty()) { |
| 186 reading_list_model_->SetEntryDistilledPath(url, distilled_path); | 152 reading_list_model_->SetEntryDistilledPath(url, distilled_path); |
| 187 | 153 |
| 188 } else if (success == URLDownloader::ERROR_RETRY) { | 154 } else if (success == URLDownloader::ERROR_RETRY) { |
| 189 reading_list_model_->SetEntryDistilledState(url, | 155 reading_list_model_->SetEntryDistilledState(url, |
| 190 ReadingListEntry::WILL_RETRY); | 156 ReadingListEntry::WILL_RETRY); |
| 191 ScheduleDownloadEntryFromURL(url); | 157 ScheduleDownloadEntry(url); |
| 192 | |
| 193 } else if (success == URLDownloader::ERROR_PERMANENT) { | 158 } else if (success == URLDownloader::ERROR_PERMANENT) { |
| 194 reading_list_model_->SetEntryDistilledState(url, ReadingListEntry::ERROR); | 159 reading_list_model_->SetEntryDistilledState(url, ReadingListEntry::ERROR); |
| 195 } | 160 } |
| 196 } | 161 } |
| 197 | 162 |
| 198 void ReadingListDownloadService::OnDeleteEnd(const GURL& url, bool success) { | 163 void ReadingListDownloadService::OnDeleteEnd(const GURL& url, bool success) { |
| 199 // Nothing to update as this is only called when deleting reading list entries | 164 // Nothing to update as this is only called when deleting reading list entries |
| 200 } | 165 } |
| 201 | 166 |
| 202 void ReadingListDownloadService::OnConnectionTypeChanged( | 167 void ReadingListDownloadService::OnConnectionTypeChanged( |
| 203 net::NetworkChangeNotifier::ConnectionType type) { | 168 net::NetworkChangeNotifier::ConnectionType type) { |
| 204 if (type == net::NetworkChangeNotifier::CONNECTION_NONE) { | 169 if (type == net::NetworkChangeNotifier::CONNECTION_NONE) { |
| 205 had_connection_ = false; | 170 had_connection_ = false; |
| 206 return; | 171 return; |
| 207 } | 172 } |
| 208 | 173 |
| 209 if (!had_connection_) { | 174 if (!had_connection_) { |
| 210 had_connection_ = true; | 175 had_connection_ = true; |
| 211 for (auto& url : url_to_download_cellular_) { | 176 for (auto& url : url_to_download_cellular_) { |
| 212 ScheduleDownloadEntryFromURL(url); | 177 ScheduleDownloadEntry(url); |
| 213 } | 178 } |
| 214 } | 179 } |
| 215 if (type == net::NetworkChangeNotifier::CONNECTION_WIFI) { | 180 if (type == net::NetworkChangeNotifier::CONNECTION_WIFI) { |
| 216 for (auto& url : url_to_download_wifi_) { | 181 for (auto& url : url_to_download_wifi_) { |
| 217 ScheduleDownloadEntryFromURL(url); | 182 ScheduleDownloadEntry(url); |
| 218 } | 183 } |
| 219 } | 184 } |
| 220 } | 185 } |
| OLD | NEW |