| 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/prerendering_offliner.h" | 5 #include "chrome/browser/android/offline_pages/prerendering_offliner.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/metrics/histogram_macros.h" | 8 #include "base/metrics/histogram_macros.h" |
| 9 #include "base/sys_info.h" | 9 #include "base/sys_info.h" |
| 10 #include "chrome/browser/android/offline_pages/offline_page_mhtml_archiver.h" | 10 #include "chrome/browser/android/offline_pages/offline_page_mhtml_archiver.h" |
| (...skipping 18 matching lines...) Expand all Loading... |
| 29 offline_page_model_(offline_page_model), | 29 offline_page_model_(offline_page_model), |
| 30 pending_request_(nullptr), | 30 pending_request_(nullptr), |
| 31 is_low_end_device_(base::SysInfo::IsLowEndDevice()), | 31 is_low_end_device_(base::SysInfo::IsLowEndDevice()), |
| 32 app_listener_(nullptr), | 32 app_listener_(nullptr), |
| 33 weak_ptr_factory_(this) {} | 33 weak_ptr_factory_(this) {} |
| 34 | 34 |
| 35 PrerenderingOffliner::~PrerenderingOffliner() {} | 35 PrerenderingOffliner::~PrerenderingOffliner() {} |
| 36 | 36 |
| 37 void PrerenderingOffliner::OnNetworkProgress(const SavePageRequest& request, | 37 void PrerenderingOffliner::OnNetworkProgress(const SavePageRequest& request, |
| 38 int64_t bytes) { | 38 int64_t bytes) { |
| 39 if (!pending_request_) | 39 if (!pending_request_ || !progress_callback_) |
| 40 return; | |
| 41 DownloadUIAdapter* ui_adapter = | |
| 42 DownloadUIAdapter::FromOfflinePageModel(offline_page_model_); | |
| 43 if (!ui_adapter) | |
| 44 return; | 40 return; |
| 45 | 41 |
| 46 ui_adapter->UpdateProgress(request.request_id(), bytes); | 42 progress_callback_.Run(request, bytes); |
| 47 } | 43 } |
| 48 | 44 |
| 49 void PrerenderingOffliner::OnLoadPageDone( | 45 void PrerenderingOffliner::OnLoadPageDone( |
| 50 const SavePageRequest& request, | 46 const SavePageRequest& request, |
| 51 Offliner::RequestStatus load_status, | 47 Offliner::RequestStatus load_status, |
| 52 content::WebContents* web_contents) { | 48 content::WebContents* web_contents) { |
| 53 // Check if request is still pending receiving a callback. | 49 // Check if request is still pending receiving a callback. |
| 54 // Note: it is possible to get a loaded page, start the save operation, | 50 // Note: it is possible to get a loaded page, start the save operation, |
| 55 // and then get another callback from the Loader (eg, if its loaded | 51 // and then get another callback from the Loader (eg, if its loaded |
| 56 // WebContents is being destroyed for some resource reclamation). | 52 // WebContents is being destroyed for some resource reclamation). |
| (...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 124 if (save_result == SavePageResult::SUCCESS) { | 120 if (save_result == SavePageResult::SUCCESS) { |
| 125 save_status = RequestStatus::SAVED; | 121 save_status = RequestStatus::SAVED; |
| 126 } else { | 122 } else { |
| 127 // TODO(dougarnett): Consider reflecting some recommendation to retry the | 123 // TODO(dougarnett): Consider reflecting some recommendation to retry the |
| 128 // request based on specific save error cases. | 124 // request based on specific save error cases. |
| 129 save_status = RequestStatus::SAVE_FAILED; | 125 save_status = RequestStatus::SAVE_FAILED; |
| 130 } | 126 } |
| 131 completion_callback_.Run(request, save_status); | 127 completion_callback_.Run(request, save_status); |
| 132 } | 128 } |
| 133 | 129 |
| 134 bool PrerenderingOffliner::LoadAndSave(const SavePageRequest& request, | 130 bool PrerenderingOffliner::LoadAndSave( |
| 135 const CompletionCallback& callback) { | 131 const SavePageRequest& request, |
| 132 const CompletionCallback& completion_callback, |
| 133 const ProgressCallback& progress_callback) { |
| 136 DCHECK(!pending_request_.get()); | 134 DCHECK(!pending_request_.get()); |
| 137 | 135 |
| 138 if (pending_request_) { | 136 if (pending_request_) { |
| 139 DVLOG(1) << "Already have pending request"; | 137 DVLOG(1) << "Already have pending request"; |
| 140 return false; | 138 return false; |
| 141 } | 139 } |
| 142 | 140 |
| 143 // Do not allow loading for custom tabs clients if 3rd party cookies blocked. | 141 // Do not allow loading for custom tabs clients if 3rd party cookies blocked. |
| 144 // TODO(dewittj): Revise api to specify policy rather than hard code to | 142 // TODO(dewittj): Revise api to specify policy rather than hard code to |
| 145 // name_space. | 143 // name_space. |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 183 1); | 181 1); |
| 184 } | 182 } |
| 185 | 183 |
| 186 if (!OfflinePageModel::CanSaveURL(request.url())) { | 184 if (!OfflinePageModel::CanSaveURL(request.url())) { |
| 187 DVLOG(1) << "Not able to save page for requested url: " << request.url(); | 185 DVLOG(1) << "Not able to save page for requested url: " << request.url(); |
| 188 return false; | 186 return false; |
| 189 } | 187 } |
| 190 | 188 |
| 191 // Track copy of pending request for callback handling. | 189 // Track copy of pending request for callback handling. |
| 192 pending_request_.reset(new SavePageRequest(request)); | 190 pending_request_.reset(new SavePageRequest(request)); |
| 193 completion_callback_ = callback; | 191 completion_callback_ = completion_callback; |
| 192 progress_callback_ = progress_callback; |
| 194 | 193 |
| 195 // Kick off load page attempt. | 194 // Kick off load page attempt. |
| 196 bool accepted = GetOrCreateLoader()->LoadPage( | 195 bool accepted = GetOrCreateLoader()->LoadPage( |
| 197 request.url(), base::Bind(&PrerenderingOffliner::OnLoadPageDone, | 196 request.url(), base::Bind(&PrerenderingOffliner::OnLoadPageDone, |
| 198 weak_ptr_factory_.GetWeakPtr(), request), | 197 weak_ptr_factory_.GetWeakPtr(), request), |
| 199 base::Bind(&PrerenderingOffliner::OnNetworkProgress, | 198 base::Bind(&PrerenderingOffliner::OnNetworkProgress, |
| 200 weak_ptr_factory_.GetWeakPtr(), request)); | 199 weak_ptr_factory_.GetWeakPtr(), request)); |
| 201 if (!accepted) { | 200 if (!accepted) { |
| 202 pending_request_.reset(nullptr); | 201 pending_request_.reset(nullptr); |
| 203 } else { | 202 } else { |
| (...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 271 base::android::APPLICATION_STATE_HAS_RUNNING_ACTIVITIES) { | 270 base::android::APPLICATION_STATE_HAS_RUNNING_ACTIVITIES) { |
| 272 DVLOG(1) << "App became active, canceling current offlining request"; | 271 DVLOG(1) << "App became active, canceling current offlining request"; |
| 273 SavePageRequest* request = pending_request_.get(); | 272 SavePageRequest* request = pending_request_.get(); |
| 274 Cancel(); | 273 Cancel(); |
| 275 completion_callback_.Run(*request, | 274 completion_callback_.Run(*request, |
| 276 Offliner::RequestStatus::FOREGROUND_CANCELED); | 275 Offliner::RequestStatus::FOREGROUND_CANCELED); |
| 277 } | 276 } |
| 278 } | 277 } |
| 279 | 278 |
| 280 } // namespace offline_pages | 279 } // namespace offline_pages |
| OLD | NEW |