| 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 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 78 } | 78 } |
| 79 | 79 |
| 80 if (load_status == Offliner::RequestStatus::LOADED) { | 80 if (load_status == Offliner::RequestStatus::LOADED) { |
| 81 // The page has successfully loaded so now try to save the page. | 81 // The page has successfully loaded so now try to save the page. |
| 82 // After issuing the save request we will wait for either: the save | 82 // After issuing the save request we will wait for either: the save |
| 83 // callback or a CANCELED load callback (triggered because the loaded | 83 // callback or a CANCELED load callback (triggered because the loaded |
| 84 // WebContents are being destroyed) - whichever callback occurs first. | 84 // WebContents are being destroyed) - whichever callback occurs first. |
| 85 DCHECK(web_contents); | 85 DCHECK(web_contents); |
| 86 std::unique_ptr<OfflinePageArchiver> archiver( | 86 std::unique_ptr<OfflinePageArchiver> archiver( |
| 87 new OfflinePageMHTMLArchiver(web_contents)); | 87 new OfflinePageMHTMLArchiver(web_contents)); |
| 88 // Pass in the URL from the WebContents in case it is redirected from | 88 |
| 89 // the requested URL. This is to work around a check in the | 89 OfflinePageModel::SavePageParams save_page_params; |
| 90 // OfflinePageModel implementation that ensures URL passed in here is | 90 save_page_params.url = web_contents->GetLastCommittedURL(); |
| 91 // same as LastCommittedURL from the snapshot. | 91 save_page_params.client_id = request.client_id(); |
| 92 // TODO(dougarnett): Raise issue of how to better deal with redirects. | 92 save_page_params.proposed_offline_id = request.request_id(); |
| 93 SavePage(web_contents->GetLastCommittedURL(), request.client_id(), | 93 // Pass in the original URL if it is different from the last committed URL |
| 94 request.request_id(), std::move(archiver), | 94 // when redirects occur. |
| 95 if (save_page_params.url != request.url()) |
| 96 save_page_params.original_url = request.url(); |
| 97 |
| 98 SavePage(save_page_params, std::move(archiver), |
| 95 base::Bind(&PrerenderingOffliner::OnSavePageDone, | 99 base::Bind(&PrerenderingOffliner::OnSavePageDone, |
| 96 weak_ptr_factory_.GetWeakPtr(), request)); | 100 weak_ptr_factory_.GetWeakPtr(), request)); |
| 97 } else { | 101 } else { |
| 98 // Clear pending request and app listener then run completion callback. | 102 // Clear pending request and app listener then run completion callback. |
| 99 pending_request_.reset(nullptr); | 103 pending_request_.reset(nullptr); |
| 100 app_listener_.reset(nullptr); | 104 app_listener_.reset(nullptr); |
| 101 completion_callback_.Run(request, load_status); | 105 completion_callback_.Run(request, load_status); |
| 102 } | 106 } |
| 103 } | 107 } |
| 104 | 108 |
| (...skipping 130 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 235 void PrerenderingOffliner::SetLowEndDeviceForTesting(bool is_low_end_device) { | 239 void PrerenderingOffliner::SetLowEndDeviceForTesting(bool is_low_end_device) { |
| 236 is_low_end_device_ = is_low_end_device; | 240 is_low_end_device_ = is_low_end_device; |
| 237 } | 241 } |
| 238 | 242 |
| 239 void PrerenderingOffliner::SetApplicationStateForTesting( | 243 void PrerenderingOffliner::SetApplicationStateForTesting( |
| 240 base::android::ApplicationState application_state) { | 244 base::android::ApplicationState application_state) { |
| 241 OnApplicationStateChange(application_state); | 245 OnApplicationStateChange(application_state); |
| 242 } | 246 } |
| 243 | 247 |
| 244 void PrerenderingOffliner::SavePage( | 248 void PrerenderingOffliner::SavePage( |
| 245 const GURL& url, | 249 const OfflinePageModel::SavePageParams& save_page_params, |
| 246 const ClientId& client_id, | |
| 247 int64_t proposed_offline_id, | |
| 248 std::unique_ptr<OfflinePageArchiver> archiver, | 250 std::unique_ptr<OfflinePageArchiver> archiver, |
| 249 const SavePageCallback& save_callback) { | 251 const SavePageCallback& save_callback) { |
| 250 DCHECK(offline_page_model_); | 252 DCHECK(offline_page_model_); |
| 251 offline_page_model_->SavePage(url, client_id, proposed_offline_id, | 253 offline_page_model_->SavePage( |
| 252 std::move(archiver), save_callback); | 254 save_page_params, std::move(archiver), save_callback); |
| 253 } | 255 } |
| 254 | 256 |
| 255 PrerenderingLoader* PrerenderingOffliner::GetOrCreateLoader() { | 257 PrerenderingLoader* PrerenderingOffliner::GetOrCreateLoader() { |
| 256 if (!loader_) { | 258 if (!loader_) { |
| 257 loader_.reset(new PrerenderingLoader(browser_context_)); | 259 loader_.reset(new PrerenderingLoader(browser_context_)); |
| 258 } | 260 } |
| 259 return loader_.get(); | 261 return loader_.get(); |
| 260 } | 262 } |
| 261 | 263 |
| 262 void PrerenderingOffliner::OnApplicationStateChange( | 264 void PrerenderingOffliner::OnApplicationStateChange( |
| 263 base::android::ApplicationState application_state) { | 265 base::android::ApplicationState application_state) { |
| 264 if (pending_request_ && is_low_end_device_ && | 266 if (pending_request_ && is_low_end_device_ && |
| 265 application_state == | 267 application_state == |
| 266 base::android::APPLICATION_STATE_HAS_RUNNING_ACTIVITIES) { | 268 base::android::APPLICATION_STATE_HAS_RUNNING_ACTIVITIES) { |
| 267 DVLOG(1) << "App became active, canceling current offlining request"; | 269 DVLOG(1) << "App became active, canceling current offlining request"; |
| 268 SavePageRequest* request = pending_request_.get(); | 270 SavePageRequest* request = pending_request_.get(); |
| 269 Cancel(); | 271 Cancel(); |
| 270 completion_callback_.Run(*request, | 272 completion_callback_.Run(*request, |
| 271 Offliner::RequestStatus::FOREGROUND_CANCELED); | 273 Offliner::RequestStatus::FOREGROUND_CANCELED); |
| 272 } | 274 } |
| 273 } | 275 } |
| 274 | 276 |
| 275 } // namespace offline_pages | 277 } // namespace offline_pages |
| OLD | NEW |