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 "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 is_error_(false), | |
| 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 Loading... | |
| 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 (is_error_) { | |
| 135 completion_callback_.Run( | |
| 136 request, | |
| 137 Offliner::RequestStatus::LOADING_FAILED_NO_RETRY); | |
|
Pete Williamson
2017/01/25 18:34:26
Why NO_RETRY? It might be a malformed URL, in whi
chili
2017/01/31 20:07:37
This is a good point. While my gut feeling says t
| |
| 138 ResetState(); | |
| 139 return; | |
| 140 } | |
| 141 | |
| 130 save_state_ = SAVING; | 142 save_state_ = SAVING; |
| 131 SavePageRequest request(*pending_request_.get()); | |
| 132 content::WebContents* web_contents( | 143 content::WebContents* web_contents( |
| 133 content::WebContentsObserver::web_contents()); | 144 content::WebContentsObserver::web_contents()); |
| 134 | 145 |
| 135 std::unique_ptr<OfflinePageArchiver> archiver( | 146 std::unique_ptr<OfflinePageArchiver> archiver( |
| 136 new OfflinePageMHTMLArchiver(web_contents)); | 147 new OfflinePageMHTMLArchiver(web_contents)); |
| 137 | 148 |
| 138 OfflinePageModel::SavePageParams params; | 149 OfflinePageModel::SavePageParams params; |
| 139 params.url = web_contents->GetLastCommittedURL(); | 150 params.url = web_contents->GetLastCommittedURL(); |
| 140 params.client_id = request.client_id(); | 151 params.client_id = request.client_id(); |
| 141 params.proposed_offline_id = request.request_id(); | 152 params.proposed_offline_id = request.request_id(); |
| (...skipping 19 matching lines...) Expand all Loading... | |
| 161 completion_callback_.Run(request, | 172 completion_callback_.Run(request, |
| 162 Offliner::RequestStatus::LOADING_FAILED); | 173 Offliner::RequestStatus::LOADING_FAILED); |
| 163 } | 174 } |
| 164 ResetState(); | 175 ResetState(); |
| 165 } | 176 } |
| 166 } | 177 } |
| 167 | 178 |
| 168 void BackgroundLoaderOffliner::WebContentsDestroyed() { | 179 void BackgroundLoaderOffliner::WebContentsDestroyed() { |
| 169 if (pending_request_) { | 180 if (pending_request_) { |
| 170 SavePageRequest request(*pending_request_.get()); | 181 SavePageRequest request(*pending_request_.get()); |
| 171 completion_callback_.Run(*pending_request_.get(), | 182 completion_callback_.Run(request, |
| 172 Offliner::RequestStatus::LOADING_FAILED); | 183 Offliner::RequestStatus::LOADING_FAILED); |
| 173 ResetState(); | 184 ResetState(); |
| 174 } | 185 } |
| 175 } | 186 } |
| 176 | 187 |
| 188 void BackgroundLoaderOffliner::DidFinishNavigation( | |
| 189 content::NavigationHandle* navigation_handle) { | |
| 190 // If there was an error of any kind (certificate, client, DNS, etc), | |
| 191 // Mark as error page. Resetting here causes RecordNavigationMetrics to crash. | |
| 192 if (navigation_handle->IsErrorPage()) { | |
|
fgorski
2017/01/25 17:05:14
nit: {} not needed
chili
2017/01/31 20:07:37
Added a switch case. Keeping the {} :)
| |
| 193 is_error_ = true; | |
| 194 } | |
| 195 } | |
| 196 | |
| 177 void BackgroundLoaderOffliner::OnPageSaved(SavePageResult save_result, | 197 void BackgroundLoaderOffliner::OnPageSaved(SavePageResult save_result, |
| 178 int64_t offline_id) { | 198 int64_t offline_id) { |
| 179 if (!pending_request_) | 199 if (!pending_request_) |
| 180 return; | 200 return; |
| 181 | 201 |
| 182 SavePageRequest request(*pending_request_.get()); | 202 SavePageRequest request(*pending_request_.get()); |
| 183 ResetState(); | 203 ResetState(); |
| 184 | 204 |
| 185 if (save_state_ == DELETE_AFTER_SAVE) { | 205 if (save_state_ == DELETE_AFTER_SAVE) { |
| 186 save_state_ = NONE; | 206 save_state_ = NONE; |
| 187 return; | 207 return; |
| 188 } | 208 } |
| 189 | 209 |
| 190 save_state_ = NONE; | 210 save_state_ = NONE; |
| 191 | 211 |
| 192 Offliner::RequestStatus save_status; | 212 Offliner::RequestStatus save_status; |
| 193 if (save_result == SavePageResult::SUCCESS) | 213 if (save_result == SavePageResult::SUCCESS) |
| 194 save_status = RequestStatus::SAVED; | 214 save_status = RequestStatus::SAVED; |
| 195 else | 215 else |
| 196 save_status = RequestStatus::SAVE_FAILED; | 216 save_status = RequestStatus::SAVE_FAILED; |
| 197 | 217 |
| 198 completion_callback_.Run(request, save_status); | 218 completion_callback_.Run(request, save_status); |
| 199 } | 219 } |
| 200 | 220 |
| 201 void BackgroundLoaderOffliner::ResetState() { | 221 void BackgroundLoaderOffliner::ResetState() { |
| 202 pending_request_.reset(); | 222 pending_request_.reset(); |
| 223 is_error_ = false; | |
| 203 // TODO(chili): Remove after RequestCoordinator can handle multiple offliners. | 224 // TODO(chili): Remove after RequestCoordinator can handle multiple offliners. |
| 204 // We reset the loader and observer after completion so loaders | 225 // We reset the loader and observer after completion so loaders |
| 205 // will not be re-used across different requests/tries. This is a temporary | 226 // will not be re-used across different requests/tries. This is a temporary |
| 206 // solution while there exists assumptions about the number of offliners | 227 // solution while there exists assumptions about the number of offliners |
| 207 // there are. | 228 // there are. |
| 208 loader_.reset( | 229 loader_.reset( |
| 209 new background_loader::BackgroundLoaderContents(browser_context_)); | 230 new background_loader::BackgroundLoaderContents(browser_context_)); |
| 210 content::WebContentsObserver::Observe(loader_.get()->web_contents()); | 231 content::WebContentsObserver::Observe(loader_.get()->web_contents()); |
| 211 } | 232 } |
| 212 | 233 |
| 213 void BackgroundLoaderOffliner::OnApplicationStateChange( | 234 void BackgroundLoaderOffliner::OnApplicationStateChange( |
| 214 base::android::ApplicationState application_state) { | 235 base::android::ApplicationState application_state) { |
| 215 if (pending_request_ && is_low_end_device_ && | 236 if (pending_request_ && is_low_end_device_ && |
| 216 application_state == | 237 application_state == |
| 217 base::android::APPLICATION_STATE_HAS_RUNNING_ACTIVITIES) { | 238 base::android::APPLICATION_STATE_HAS_RUNNING_ACTIVITIES) { |
| 218 DVLOG(1) << "App became active, canceling current offlining request"; | 239 DVLOG(1) << "App became active, canceling current offlining request"; |
| 219 SavePageRequest* request = pending_request_.get(); | 240 SavePageRequest* request = pending_request_.get(); |
| 220 Cancel(); | 241 Cancel(); |
| 221 completion_callback_.Run(*request, RequestStatus::FOREGROUND_CANCELED); | 242 completion_callback_.Run(*request, RequestStatus::FOREGROUND_CANCELED); |
| 222 } | 243 } |
| 223 } | 244 } |
| 224 | 245 |
| 225 } // namespace offline_pages | 246 } // namespace offline_pages |
| OLD | NEW |