| 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/json/json_writer.h" | 8 #include "base/json/json_writer.h" |
| 9 #include "base/metrics/histogram_macros.h" | 9 #include "base/metrics/histogram_macros.h" |
| 10 #include "base/sys_info.h" | 10 #include "base/sys_info.h" |
| 11 #include "base/threading/thread_task_runner_handle.h" |
| 11 #include "chrome/browser/android/offline_pages/offline_page_mhtml_archiver.h" | 12 #include "chrome/browser/android/offline_pages/offline_page_mhtml_archiver.h" |
| 12 #include "chrome/browser/android/offline_pages/offliner_helper.h" | 13 #include "chrome/browser/android/offline_pages/offliner_helper.h" |
| 13 #include "chrome/browser/profiles/profile.h" | 14 #include "chrome/browser/profiles/profile.h" |
| 14 #include "components/offline_pages/core/background/offliner_policy.h" | 15 #include "components/offline_pages/core/background/offliner_policy.h" |
| 15 #include "components/offline_pages/core/background/save_page_request.h" | 16 #include "components/offline_pages/core/background/save_page_request.h" |
| 16 #include "components/offline_pages/core/client_namespace_constants.h" | 17 #include "components/offline_pages/core/client_namespace_constants.h" |
| 17 #include "components/offline_pages/core/downloads/download_ui_adapter.h" | 18 #include "components/offline_pages/core/downloads/download_ui_adapter.h" |
| 18 #include "components/offline_pages/core/offline_page_feature.h" | 19 #include "components/offline_pages/core/offline_page_feature.h" |
| 19 #include "components/offline_pages/core/offline_page_model.h" | 20 #include "components/offline_pages/core/offline_page_model.h" |
| 20 #include "content/public/browser/browser_context.h" | 21 #include "content/public/browser/browser_context.h" |
| 21 #include "content/public/browser/mhtml_extra_parts.h" | 22 #include "content/public/browser/mhtml_extra_parts.h" |
| 22 #include "content/public/browser/web_contents.h" | 23 #include "content/public/browser/web_contents.h" |
| 23 | 24 |
| 25 namespace offline_pages { |
| 26 |
| 24 namespace { | 27 namespace { |
| 25 const char kContentType[] = "text/plain"; | 28 const char kContentType[] = "text/plain"; |
| 26 const char kContentTransferEncodingBinary[] = | 29 const char kContentTransferEncodingBinary[] = |
| 27 "Content-Transfer-Encoding: binary"; | 30 "Content-Transfer-Encoding: binary"; |
| 28 const char kXHeaderForSignals[] = "X-Chrome-Loading-Metrics-Data: 1"; | 31 const char kXHeaderForSignals[] = "X-Chrome-Loading-Metrics-Data: 1"; |
| 32 |
| 33 void HandleApplicationStateChangeCancel( |
| 34 const Offliner::CompletionCallback& completion_callback, |
| 35 const SavePageRequest& canceled_request) { |
| 36 completion_callback.Run(canceled_request, |
| 37 Offliner::RequestStatus::FOREGROUND_CANCELED); |
| 38 } |
| 29 } // namespace | 39 } // namespace |
| 30 | 40 |
| 31 namespace offline_pages { | |
| 32 | |
| 33 PrerenderingOffliner::PrerenderingOffliner( | 41 PrerenderingOffliner::PrerenderingOffliner( |
| 34 content::BrowserContext* browser_context, | 42 content::BrowserContext* browser_context, |
| 35 const OfflinerPolicy* policy, | 43 const OfflinerPolicy* policy, |
| 36 OfflinePageModel* offline_page_model) | 44 OfflinePageModel* offline_page_model) |
| 37 : browser_context_(browser_context), | 45 : browser_context_(browser_context), |
| 38 policy_(policy), | 46 policy_(policy), |
| 39 offline_page_model_(offline_page_model), | 47 offline_page_model_(offline_page_model), |
| 40 pending_request_(nullptr), | 48 pending_request_(nullptr), |
| 41 is_low_end_device_(base::SysInfo::IsLowEndDevice()), | 49 is_low_end_device_(base::SysInfo::IsLowEndDevice()), |
| 42 saved_on_last_retry_(false), | 50 saved_on_last_retry_(false), |
| (...skipping 204 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 247 } else { | 255 } else { |
| 248 // Create app listener for the pending request. | 256 // Create app listener for the pending request. |
| 249 app_listener_.reset(new base::android::ApplicationStatusListener( | 257 app_listener_.reset(new base::android::ApplicationStatusListener( |
| 250 base::Bind(&PrerenderingOffliner::OnApplicationStateChange, | 258 base::Bind(&PrerenderingOffliner::OnApplicationStateChange, |
| 251 weak_ptr_factory_.GetWeakPtr()))); | 259 weak_ptr_factory_.GetWeakPtr()))); |
| 252 } | 260 } |
| 253 | 261 |
| 254 return accepted; | 262 return accepted; |
| 255 } | 263 } |
| 256 | 264 |
| 257 void PrerenderingOffliner::Cancel(const CancelCallback& callback) { | 265 bool PrerenderingOffliner::Cancel(const CancelCallback& callback) { |
| 258 int64_t request_id = 0LL; | 266 if (!pending_request_) |
| 259 if (pending_request_) { | 267 return false; |
| 260 request_id = pending_request_->request_id(); | 268 |
| 261 pending_request_.reset(nullptr); | 269 // Post the cancel callback right after this call concludes. |
| 262 app_listener_.reset(nullptr); | 270 base::ThreadTaskRunnerHandle::Get()->PostTask( |
| 263 GetOrCreateLoader()->StopLoading(); | 271 FROM_HERE, base::Bind(callback, *pending_request_.get())); |
| 264 // TODO(dougarnett): Consider ability to cancel SavePage request. | 272 GetOrCreateLoader()->StopLoading(); |
| 265 } | 273 pending_request_.reset(nullptr); |
| 266 callback.Run(request_id); | 274 app_listener_.reset(nullptr); |
| 275 return true; |
| 267 } | 276 } |
| 268 | 277 |
| 269 bool PrerenderingOffliner::HandleTimeout(const SavePageRequest& request) { | 278 bool PrerenderingOffliner::HandleTimeout(int64_t request_id) { |
| 270 if (pending_request_) { | 279 if (pending_request_) { |
| 271 DCHECK(request.request_id() == pending_request_->request_id()); | 280 DCHECK(request_id == pending_request_->request_id()); |
| 272 if (GetOrCreateLoader()->IsLowbarMet() && | 281 if (GetOrCreateLoader()->IsLowbarMet() && |
| 273 (request.started_attempt_count() + 1 >= policy_->GetMaxStartedTries() || | 282 (pending_request_->started_attempt_count() + 1 >= |
| 274 request.completed_attempt_count() + 1 >= | 283 policy_->GetMaxStartedTries() || |
| 284 pending_request_->completed_attempt_count() + 1 >= |
| 275 policy_->GetMaxCompletedTries())) { | 285 policy_->GetMaxCompletedTries())) { |
| 276 saved_on_last_retry_ = true; | 286 saved_on_last_retry_ = true; |
| 277 GetOrCreateLoader()->StartSnapshot(); | 287 GetOrCreateLoader()->StartSnapshot(); |
| 278 return true; | 288 return true; |
| 279 } | 289 } |
| 280 } | 290 } |
| 281 return false; | 291 return false; |
| 282 } | 292 } |
| 283 | 293 |
| 284 void PrerenderingOffliner::SetLoaderForTesting( | 294 void PrerenderingOffliner::SetLoaderForTesting( |
| (...skipping 26 matching lines...) Expand all Loading... |
| 311 } | 321 } |
| 312 return loader_.get(); | 322 return loader_.get(); |
| 313 } | 323 } |
| 314 | 324 |
| 315 void PrerenderingOffliner::OnApplicationStateChange( | 325 void PrerenderingOffliner::OnApplicationStateChange( |
| 316 base::android::ApplicationState application_state) { | 326 base::android::ApplicationState application_state) { |
| 317 if (pending_request_ && is_low_end_device_ && | 327 if (pending_request_ && is_low_end_device_ && |
| 318 application_state == | 328 application_state == |
| 319 base::android::APPLICATION_STATE_HAS_RUNNING_ACTIVITIES) { | 329 base::android::APPLICATION_STATE_HAS_RUNNING_ACTIVITIES) { |
| 320 DVLOG(1) << "App became active, canceling current offlining request"; | 330 DVLOG(1) << "App became active, canceling current offlining request"; |
| 321 SavePageRequest* request = pending_request_.get(); | 331 // No need to check the return value or complete early, as false would |
| 322 // This works because Bind will make a copy of request, and we | 332 // indicate that there was no request, in which case the state change is |
| 323 // should not have to worry about reset being called before cancel callback. | 333 // ignored. |
| 324 Cancel(base::Bind(&PrerenderingOffliner::HandleApplicationStateChangeCancel, | 334 Cancel( |
| 325 weak_ptr_factory_.GetWeakPtr(), *request)); | 335 base::Bind(HandleApplicationStateChangeCancel, completion_callback_)); |
| 326 } | 336 } |
| 327 } | 337 } |
| 328 | |
| 329 void PrerenderingOffliner::HandleApplicationStateChangeCancel( | |
| 330 const SavePageRequest& request, | |
| 331 int64_t offline_id) { | |
| 332 // This shouldn't be immediate, but account for case where request was reset | |
| 333 // while waiting for callback. | |
| 334 if (pending_request_ && pending_request_->request_id() != offline_id) | |
| 335 return; | |
| 336 completion_callback_.Run(request, RequestStatus::FOREGROUND_CANCELED); | |
| 337 } | |
| 338 } // namespace offline_pages | 338 } // namespace offline_pages |
| OLD | NEW |