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

Side by Side Diff: ios/chrome/browser/reading_list/reading_list_download_service.cc

Issue 2351113003: Reading list downloader retries on recoverable error (Closed)
Patch Set: Reviewable Created 4 years, 3 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 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"
11 #include "base/memory/ptr_util.h" 11 #include "base/memory/ptr_util.h"
12 #include "ios/chrome/browser/reading_list/reading_list_entry.h" 12 #include "ios/chrome/browser/reading_list/reading_list_entry.h"
13 #include "ios/chrome/browser/reading_list/reading_list_model.h" 13 #include "ios/chrome/browser/reading_list/reading_list_model.h"
14 #include "ios/web/public/web_thread.h"
15
16 namespace {
17 // Number of time the download must fail before the download occurs only in
18 // wifi.
19 const int kNumberOfFailsBeforeWifiOnly = 5;
20 // Number of time the download must fail before we give up trying to download
21 // it.
22 const int kNumberOfFailsBeforeStop = 7;
23 } // namespace
14 24
15 ReadingListDownloadService::ReadingListDownloadService( 25 ReadingListDownloadService::ReadingListDownloadService(
16 ReadingListModel* reading_list_model, 26 ReadingListModel* reading_list_model,
17 dom_distiller::DomDistillerService* distiller_service, 27 dom_distiller::DomDistillerService* distiller_service,
18 PrefService* prefs, 28 PrefService* prefs,
19 base::FilePath chrome_profile_path) 29 base::FilePath chrome_profile_path)
20 : reading_list_model_(reading_list_model) { 30 : reading_list_model_(reading_list_model),
31 had_connection_(!net::NetworkChangeNotifier::IsOffline()) {
21 DCHECK(reading_list_model); 32 DCHECK(reading_list_model);
22 url_downloader_ = base::MakeUnique<URLDownloader>( 33 url_downloader_ = base::MakeUnique<URLDownloader>(
23 distiller_service, prefs, chrome_profile_path, 34 distiller_service, prefs, chrome_profile_path,
24 base::Bind(&ReadingListDownloadService::OnDownloadEnd, 35 base::Bind(&ReadingListDownloadService::OnDownloadEnd,
25 base::Unretained(this)), 36 base::Unretained(this)),
26 base::Bind(&ReadingListDownloadService::OnDeleteEnd, 37 base::Bind(&ReadingListDownloadService::OnDeleteEnd,
27 base::Unretained(this))); 38 base::Unretained(this)));
39 net::NetworkChangeNotifier::AddConnectionTypeObserver(this);
28 } 40 }
29 41
30 ReadingListDownloadService::~ReadingListDownloadService() {} 42 ReadingListDownloadService::~ReadingListDownloadService() {
43 net::NetworkChangeNotifier::RemoveConnectionTypeObserver(this);
44 }
31 45
32 void ReadingListDownloadService::Initialize() { 46 void ReadingListDownloadService::Initialize() {
33 reading_list_model_->AddObserver(this); 47 reading_list_model_->AddObserver(this);
34 } 48 }
35 49
36 void ReadingListDownloadService::Shutdown() { 50 void ReadingListDownloadService::Shutdown() {
37 reading_list_model_->RemoveObserver(this); 51 reading_list_model_->RemoveObserver(this);
38 } 52 }
39 53
40 void ReadingListDownloadService::ReadingListModelLoaded( 54 void ReadingListDownloadService::ReadingListModelLoaded(
(...skipping 13 matching lines...) Expand all
54 const ReadingListModel* model, 68 const ReadingListModel* model,
55 size_t index) { 69 size_t index) {
56 DCHECK_EQ(reading_list_model_, model); 70 DCHECK_EQ(reading_list_model_, model);
57 RemoveDownloadedEntry(model->GetUnreadEntryAtIndex(index)); 71 RemoveDownloadedEntry(model->GetUnreadEntryAtIndex(index));
58 } 72 }
59 73
60 void ReadingListDownloadService::ReadingListWillAddUnreadEntry( 74 void ReadingListDownloadService::ReadingListWillAddUnreadEntry(
61 const ReadingListModel* model, 75 const ReadingListModel* model,
62 const ReadingListEntry& entry) { 76 const ReadingListEntry& entry) {
63 DCHECK_EQ(reading_list_model_, model); 77 DCHECK_EQ(reading_list_model_, model);
64 DownloadEntry(entry); 78 ScheduleDownloadEntry(entry);
65 } 79 }
66 80
67 void ReadingListDownloadService::ReadingListWillAddReadEntry( 81 void ReadingListDownloadService::ReadingListWillAddReadEntry(
68 const ReadingListModel* model, 82 const ReadingListModel* model,
69 const ReadingListEntry& entry) { 83 const ReadingListEntry& entry) {
70 DCHECK_EQ(reading_list_model_, model); 84 DCHECK_EQ(reading_list_model_, model);
71 DownloadEntry(entry); 85 ScheduleDownloadEntry(entry);
72 } 86 }
73 87
74 void ReadingListDownloadService::DownloadAllEntries() { 88 void ReadingListDownloadService::DownloadAllEntries() {
75 DCHECK(reading_list_model_->loaded()); 89 DCHECK(reading_list_model_->loaded());
76 size_t size = reading_list_model_->unread_size(); 90 size_t size = reading_list_model_->unread_size();
77 for (size_t i = 0; i < size; i++) { 91 for (size_t i = 0; i < size; i++) {
78 const ReadingListEntry& entry = 92 const ReadingListEntry& entry =
79 reading_list_model_->GetUnreadEntryAtIndex(i); 93 reading_list_model_->GetUnreadEntryAtIndex(i);
80 this->DownloadEntry(entry); 94 this->ScheduleDownloadEntry(entry);
81 } 95 }
82 size = reading_list_model_->read_size(); 96 size = reading_list_model_->read_size();
83 for (size_t i = 0; i < size; i++) { 97 for (size_t i = 0; i < size; i++) {
84 const ReadingListEntry& entry = reading_list_model_->GetReadEntryAtIndex(i); 98 const ReadingListEntry& entry = reading_list_model_->GetReadEntryAtIndex(i);
85 this->DownloadEntry(entry); 99 this->ScheduleDownloadEntry(entry);
86 } 100 }
87 } 101 }
88 102
103 void ReadingListDownloadService::ScheduleDownloadEntry(
104 const ReadingListEntry& entry) {
105 DCHECK(reading_list_model_->loaded());
106 if (entry.DistilledState() == ReadingListEntry::ERROR ||
107 entry.DistilledState() == ReadingListEntry::PROCESSED)
108 return;
109
110 web::WebThread::PostDelayedTask(
111 web::WebThread::UI, FROM_HERE,
112 base::Bind(&ReadingListDownloadService::DownloadEntryFromURL,
113 base::Unretained(this), entry.URL()),
gambard 2016/09/20 15:55:05 I am not really sure about this, in particular of
noyau (Ping after 24h) 2016/09/21 11:08:06 You're right not to be sure :) See the use of bas
gambard 2016/09/22 13:52:46 Using GetWeakPtr() should be OK according to https
114 entry.TimeUntilNextTry());
115 }
116
117 void ReadingListDownloadService::DownloadEntryFromURL(const GURL& url) {
118 auto download_callback =
119 base::Bind(&ReadingListDownloadService::ScheduleDownloadEntry,
120 base::Unretained(this));
121 reading_list_model_->CallbackEntryURL(url, download_callback);
122 }
123
89 void ReadingListDownloadService::DownloadEntry(const ReadingListEntry& entry) { 124 void ReadingListDownloadService::DownloadEntry(const ReadingListEntry& entry) {
90 DCHECK(reading_list_model_->loaded()); 125 DCHECK(reading_list_model_->loaded());
91 if (entry.DistilledState() != ReadingListEntry::ERROR) { 126 if (entry.DistilledState() == ReadingListEntry::ERROR ||
127 entry.DistilledState() == ReadingListEntry::PROCESSED)
128 return;
129
130 if (net::NetworkChangeNotifier::IsOffline()) {
131 // There is no connection, save it for download only if we did not exceed
132 // the maximaxum number of tries.
133 if (entry.FailedDownloadCounter() < kNumberOfFailsBeforeWifiOnly)
134 url_to_download_cellular_.push_back(entry.URL());
135 if (entry.FailedDownloadCounter() < kNumberOfFailsBeforeStop)
136 url_to_download_wifi_.push_back(entry.URL());
137 return;
138 }
139
140 // There is a connection.
141 if (entry.FailedDownloadCounter() < kNumberOfFailsBeforeWifiOnly) {
142 // Try to download the page, whatever the connection.
92 reading_list_model_->SetEntryDistilledState(entry.URL(), 143 reading_list_model_->SetEntryDistilledState(entry.URL(),
93 ReadingListEntry::PROCESSING); 144 ReadingListEntry::PROCESSING);
94 url_downloader_->DownloadOfflineURL(entry.URL()); 145 url_downloader_->DownloadOfflineURL(entry.URL());
146
147 } else if (entry.FailedDownloadCounter() < kNumberOfFailsBeforeStop) {
148 // Try to download the page only if the connection is wifi.
149 if (net::NetworkChangeNotifier::GetConnectionType() ==
150 net::NetworkChangeNotifier::CONNECTION_WIFI) {
151 // The connection is wifi, download the page.
152 reading_list_model_->SetEntryDistilledState(entry.URL(),
153 ReadingListEntry::PROCESSING);
154 url_downloader_->DownloadOfflineURL(entry.URL());
155
156 } else {
157 // The connection is not wifi, save it for download when the connection
158 // changes to wifi.
159 url_to_download_wifi_.push_back(entry.URL());
160 }
95 } 161 }
96 } 162 }
97 163
98 void ReadingListDownloadService::RemoveDownloadedEntry( 164 void ReadingListDownloadService::RemoveDownloadedEntry(
99 const ReadingListEntry& entry) { 165 const ReadingListEntry& entry) {
100 DCHECK(reading_list_model_->loaded()); 166 DCHECK(reading_list_model_->loaded());
101 url_downloader_->RemoveOfflineURL(entry.URL()); 167 url_downloader_->RemoveOfflineURL(entry.URL());
102 } 168 }
103 169
104 void ReadingListDownloadService::OnDownloadEnd( 170 void ReadingListDownloadService::OnDownloadEnd(
105 const GURL& url, 171 const GURL& url,
106 URLDownloader::SuccessState success, 172 URLDownloader::SuccessState success,
107 const GURL& distilled_url, 173 const GURL& distilled_url,
108 const std::string& title) { 174 const std::string& title) {
109 DCHECK(reading_list_model_->loaded()); 175 DCHECK(reading_list_model_->loaded());
110 if ((success == URLDownloader::DOWNLOAD_SUCCESS || 176 if ((success == URLDownloader::DOWNLOAD_SUCCESS ||
111 success == URLDownloader::DOWNLOAD_EXISTS) && 177 success == URLDownloader::DOWNLOAD_EXISTS) &&
112 distilled_url.is_valid()) { 178 distilled_url.is_valid()) {
113 reading_list_model_->SetEntryDistilledURL(url, distilled_url); 179 reading_list_model_->SetEntryDistilledURL(url, distilled_url);
180
114 } else if (success == URLDownloader::ERROR_RETRY) { 181 } else if (success == URLDownloader::ERROR_RETRY) {
115 reading_list_model_->SetEntryDistilledState(url, 182 reading_list_model_->SetEntryDistilledState(url,
116 ReadingListEntry::WILL_RETRY); 183 ReadingListEntry::WILL_RETRY);
184 DownloadEntryFromURL(url);
185
117 } else if (success == URLDownloader::ERROR_PERMANENT) { 186 } else if (success == URLDownloader::ERROR_PERMANENT) {
118 reading_list_model_->SetEntryDistilledState(url, ReadingListEntry::ERROR); 187 reading_list_model_->SetEntryDistilledState(url, ReadingListEntry::ERROR);
119 } 188 }
120 } 189 }
121 190
122 void ReadingListDownloadService::OnDeleteEnd(const GURL& url, bool success) { 191 void ReadingListDownloadService::OnDeleteEnd(const GURL& url, bool success) {
123 // Nothing to update as this is only called when deleting reading list entries 192 // Nothing to update as this is only called when deleting reading list entries
124 } 193 }
194
195 void ReadingListDownloadService::OnConnectionTypeChanged(
196 net::NetworkChangeNotifier::ConnectionType type) {
197 if (type == net::NetworkChangeNotifier::CONNECTION_NONE) {
198 had_connection_ = false;
199 return;
200 }
201
202 if (!had_connection_) {
203 had_connection_ = true;
204 for (auto& url : url_to_download_cellular_) {
205 DownloadEntryFromURL(url);
206 }
207 }
208 if (type == net::NetworkChangeNotifier::CONNECTION_WIFI) {
209 for (auto& url : url_to_download_wifi_) {
210 DownloadEntryFromURL(url);
211 }
212 }
213 }
OLDNEW
« no previous file with comments | « ios/chrome/browser/reading_list/reading_list_download_service.h ('k') | ios/chrome/browser/reading_list/reading_list_entry.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698