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

Side by Side Diff: chrome/browser/android/offline_pages/background_loader_offliner.cc

Issue 2656763002: [Offline pages] Add navigation error handling to background loader. (Closed)
Patch Set: code review update Created 3 years, 10 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 "chrome/browser/android/offline_pages/background_loader_offliner.h" 5 #include "chrome/browser/android/offline_pages/background_loader_offliner.h"
6 6
7 #include "base/metrics/histogram_macros.h" 7 #include "base/metrics/histogram_macros.h"
8 #include "base/sys_info.h" 8 #include "base/sys_info.h"
9 #include "chrome/browser/android/offline_pages/offline_page_mhtml_archiver.h" 9 #include "chrome/browser/android/offline_pages/offline_page_mhtml_archiver.h"
10 #include "chrome/browser/android/offline_pages/offliner_helper.h" 10 #include "chrome/browser/android/offline_pages/offliner_helper.h"
11 #include "chrome/browser/profiles/profile.h" 11 #include "chrome/browser/profiles/profile.h"
12 #include "components/offline_pages/core/background/save_page_request.h" 12 #include "components/offline_pages/core/background/save_page_request.h"
13 #include "components/offline_pages/core/client_namespace_constants.h" 13 #include "components/offline_pages/core/client_namespace_constants.h"
14 #include "components/offline_pages/core/offline_page_model.h" 14 #include "components/offline_pages/core/offline_page_model.h"
15 #include "content/public/browser/browser_context.h" 15 #include "content/public/browser/browser_context.h"
16 #include "content/public/browser/navigation_handle.h"
16 #include "content/public/browser/web_contents.h" 17 #include "content/public/browser/web_contents.h"
17 18
18 namespace offline_pages { 19 namespace offline_pages {
19 20
20 BackgroundLoaderOffliner::BackgroundLoaderOffliner( 21 BackgroundLoaderOffliner::BackgroundLoaderOffliner(
21 content::BrowserContext* browser_context, 22 content::BrowserContext* browser_context,
22 const OfflinerPolicy* policy, 23 const OfflinerPolicy* policy,
23 OfflinePageModel* offline_page_model) 24 OfflinePageModel* offline_page_model)
24 : browser_context_(browser_context), 25 : browser_context_(browser_context),
25 offline_page_model_(offline_page_model), 26 offline_page_model_(offline_page_model),
26 is_low_end_device_(base::SysInfo::IsLowEndDevice()), 27 is_low_end_device_(base::SysInfo::IsLowEndDevice()),
27 save_state_(NONE), 28 save_state_(NONE),
29 page_load_state_(SUCCESS),
28 weak_ptr_factory_(this) { 30 weak_ptr_factory_(this) {
29 DCHECK(offline_page_model_); 31 DCHECK(offline_page_model_);
30 DCHECK(browser_context_); 32 DCHECK(browser_context_);
31 } 33 }
32 34
33 BackgroundLoaderOffliner::~BackgroundLoaderOffliner() {} 35 BackgroundLoaderOffliner::~BackgroundLoaderOffliner() {}
34 36
35 bool BackgroundLoaderOffliner::LoadAndSave(const SavePageRequest& request, 37 bool BackgroundLoaderOffliner::LoadAndSave(const SavePageRequest& request,
36 const CompletionCallback& callback) { 38 const CompletionCallback& callback) {
37 DCHECK(callback); 39 DCHECK(callback);
(...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after
120 122
121 ResetState(); 123 ResetState();
122 } 124 }
123 125
124 void BackgroundLoaderOffliner::DidStopLoading() { 126 void BackgroundLoaderOffliner::DidStopLoading() {
125 if (!pending_request_.get()) { 127 if (!pending_request_.get()) {
126 DVLOG(1) << "DidStopLoading called even though no pending request."; 128 DVLOG(1) << "DidStopLoading called even though no pending request.";
127 return; 129 return;
128 } 130 }
129 131
132 SavePageRequest request(*pending_request_.get());
133 // If there was an error navigating to page, return loading failed.
134 if (page_load_state_ != SUCCESS) {
135 Offliner::RequestStatus status =
136 Offliner::RequestStatus::LOADING_FAILED_NO_RETRY;
137 if (page_load_state_ == RETRIABLE)
138 status = Offliner::RequestStatus::LOADING_FAILED;
139 completion_callback_.Run(
140 request,
141 status);
142 ResetState();
143 return;
144 }
145
130 save_state_ = SAVING; 146 save_state_ = SAVING;
131 SavePageRequest request(*pending_request_.get());
132 content::WebContents* web_contents( 147 content::WebContents* web_contents(
133 content::WebContentsObserver::web_contents()); 148 content::WebContentsObserver::web_contents());
134 149
135 std::unique_ptr<OfflinePageArchiver> archiver( 150 std::unique_ptr<OfflinePageArchiver> archiver(
136 new OfflinePageMHTMLArchiver(web_contents)); 151 new OfflinePageMHTMLArchiver(web_contents));
137 152
138 OfflinePageModel::SavePageParams params; 153 OfflinePageModel::SavePageParams params;
139 params.url = web_contents->GetLastCommittedURL(); 154 params.url = web_contents->GetLastCommittedURL();
140 params.client_id = request.client_id(); 155 params.client_id = request.client_id();
141 params.proposed_offline_id = request.request_id(); 156 params.proposed_offline_id = request.request_id();
(...skipping 20 matching lines...) Expand all
162 completion_callback_.Run(request, 177 completion_callback_.Run(request,
163 Offliner::RequestStatus::LOADING_FAILED); 178 Offliner::RequestStatus::LOADING_FAILED);
164 } 179 }
165 ResetState(); 180 ResetState();
166 } 181 }
167 } 182 }
168 183
169 void BackgroundLoaderOffliner::WebContentsDestroyed() { 184 void BackgroundLoaderOffliner::WebContentsDestroyed() {
170 if (pending_request_) { 185 if (pending_request_) {
171 SavePageRequest request(*pending_request_.get()); 186 SavePageRequest request(*pending_request_.get());
172 completion_callback_.Run(*pending_request_.get(), 187 completion_callback_.Run(request,
173 Offliner::RequestStatus::LOADING_FAILED); 188 Offliner::RequestStatus::LOADING_FAILED);
174 ResetState(); 189 ResetState();
175 } 190 }
176 } 191 }
177 192
193 void BackgroundLoaderOffliner::DidFinishNavigation(
194 content::NavigationHandle* navigation_handle) {
195 // If there was an error of any kind (certificate, client, DNS, etc),
196 // Mark as error page. Resetting here causes RecordNavigationMetrics to crash.
197 if (navigation_handle->IsErrorPage()) {
198 // TODO(chili): we need to UMA this.
199 switch (navigation_handle->GetNetErrorCode()) {
200 case ACCESS_DENIED:
201 case ADDRESS_INVALID:
202 case ADDRESS_UNREACHABLE:
203 case CERT_COMMON_NAME_INVALID:
204 case CERT_AUTHORITY_INVALID:
205 case CERT_CONTAINS_ERRORS:
206 case CERT_INVALID:
207 case CONNECTION_FAILED:
208 case DISALLOWED_URL_SCHEME:
209 case DNS_SERVER_FAILED:
210 case FILE_NOT_FOUND:
211 case FILE_PATH_TOO_LONG:
212 case FILE_TOO_LARGE:
213 case FILE_VIRUS_INFECTED:
214 case INVALID_HANDLE:
215 case INVALID_RESPONSE:
216 case INVALID_URL:
217 case NAME_NOT_RESOLVED:
218 case NAME_RESOLUTION_FAILED:
219 case MSG_TOO_BIG:
220 case SSL_CERT_BAD_FORMAT:
221 case SSL_CLIENT_AUTH_SIGNATURE_FAILED:
222 case SSL_PROTOCOL_ERROR:
223 case UNKNOWN_URL_SCHEME:
224 page_load_state_ = LOADING_FAILED_NO_RETRY;
225 break;
226 default:
227 page_load_state_ = LOADING_FAILED;
Pete Williamson 2017/01/31 23:19:49 RETRIABLE?
chili 2017/02/01 00:34:59 Yes... The one time i didn't wait for compile to f
228 }
229 }
230 }
231
178 void BackgroundLoaderOffliner::OnPageSaved(SavePageResult save_result, 232 void BackgroundLoaderOffliner::OnPageSaved(SavePageResult save_result,
179 int64_t offline_id) { 233 int64_t offline_id) {
180 if (!pending_request_) 234 if (!pending_request_)
181 return; 235 return;
182 236
183 SavePageRequest request(*pending_request_.get()); 237 SavePageRequest request(*pending_request_.get());
184 ResetState(); 238 ResetState();
185 239
186 if (save_state_ == DELETE_AFTER_SAVE) { 240 if (save_state_ == DELETE_AFTER_SAVE) {
187 save_state_ = NONE; 241 save_state_ = NONE;
188 return; 242 return;
189 } 243 }
190 244
191 save_state_ = NONE; 245 save_state_ = NONE;
192 246
193 Offliner::RequestStatus save_status; 247 Offliner::RequestStatus save_status;
194 if (save_result == SavePageResult::SUCCESS) 248 if (save_result == SavePageResult::SUCCESS)
195 save_status = RequestStatus::SAVED; 249 save_status = RequestStatus::SAVED;
196 else 250 else
197 save_status = RequestStatus::SAVE_FAILED; 251 save_status = RequestStatus::SAVE_FAILED;
198 252
199 completion_callback_.Run(request, save_status); 253 completion_callback_.Run(request, save_status);
200 } 254 }
201 255
202 void BackgroundLoaderOffliner::ResetState() { 256 void BackgroundLoaderOffliner::ResetState() {
203 pending_request_.reset(); 257 pending_request_.reset();
258 page_load_state_ = SUCCESS;
204 // TODO(chili): Remove after RequestCoordinator can handle multiple offliners. 259 // TODO(chili): Remove after RequestCoordinator can handle multiple offliners.
205 // We reset the loader and observer after completion so loaders 260 // We reset the loader and observer after completion so loaders
206 // will not be re-used across different requests/tries. This is a temporary 261 // will not be re-used across different requests/tries. This is a temporary
207 // solution while there exists assumptions about the number of offliners 262 // solution while there exists assumptions about the number of offliners
208 // there are. 263 // there are.
209 loader_.reset( 264 loader_.reset(
210 new background_loader::BackgroundLoaderContents(browser_context_)); 265 new background_loader::BackgroundLoaderContents(browser_context_));
211 content::WebContentsObserver::Observe(loader_.get()->web_contents()); 266 content::WebContentsObserver::Observe(loader_.get()->web_contents());
212 } 267 }
213 268
214 void BackgroundLoaderOffliner::OnApplicationStateChange( 269 void BackgroundLoaderOffliner::OnApplicationStateChange(
215 base::android::ApplicationState application_state) { 270 base::android::ApplicationState application_state) {
216 if (pending_request_ && is_low_end_device_ && 271 if (pending_request_ && is_low_end_device_ &&
217 application_state == 272 application_state ==
218 base::android::APPLICATION_STATE_HAS_RUNNING_ACTIVITIES) { 273 base::android::APPLICATION_STATE_HAS_RUNNING_ACTIVITIES) {
219 DVLOG(1) << "App became active, canceling current offlining request"; 274 DVLOG(1) << "App became active, canceling current offlining request";
220 SavePageRequest* request = pending_request_.get(); 275 SavePageRequest* request = pending_request_.get();
221 Cancel(); 276 Cancel();
222 completion_callback_.Run(*request, RequestStatus::FOREGROUND_CANCELED); 277 completion_callback_.Run(*request, RequestStatus::FOREGROUND_CANCELED);
223 } 278 }
224 } 279 }
225 280
226 } // namespace offline_pages 281 } // namespace offline_pages
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698