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/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/sys_info.h" | |
| 8 #include "chrome/browser/android/offline_pages/offline_page_mhtml_archiver.h" | 9 #include "chrome/browser/android/offline_pages/offline_page_mhtml_archiver.h" |
| 9 #include "components/offline_pages/background/save_page_request.h" | 10 #include "components/offline_pages/background/save_page_request.h" |
| 10 #include "components/offline_pages/offline_page_model.h" | 11 #include "components/offline_pages/offline_page_model.h" |
| 11 #include "content/public/browser/browser_context.h" | 12 #include "content/public/browser/browser_context.h" |
| 12 #include "content/public/browser/web_contents.h" | 13 #include "content/public/browser/web_contents.h" |
| 13 | 14 |
| 14 namespace offline_pages { | 15 namespace offline_pages { |
| 15 | 16 |
| 16 PrerenderingOffliner::PrerenderingOffliner( | 17 PrerenderingOffliner::PrerenderingOffliner( |
| 17 content::BrowserContext* browser_context, | 18 content::BrowserContext* browser_context, |
| 18 const OfflinerPolicy* policy, | 19 const OfflinerPolicy* policy, |
| 19 OfflinePageModel* offline_page_model) | 20 OfflinePageModel* offline_page_model) |
| 20 : browser_context_(browser_context), | 21 : browser_context_(browser_context), |
| 21 offline_page_model_(offline_page_model), | 22 offline_page_model_(offline_page_model), |
| 22 pending_request_(nullptr), | 23 pending_request_(nullptr), |
| 24 is_low_end_device_(base::SysInfo::IsLowEndDevice()), | |
| 25 app_listener_(nullptr), | |
| 23 weak_ptr_factory_(this) {} | 26 weak_ptr_factory_(this) {} |
| 24 | 27 |
| 25 PrerenderingOffliner::~PrerenderingOffliner() {} | 28 PrerenderingOffliner::~PrerenderingOffliner() {} |
| 26 | 29 |
| 27 void PrerenderingOffliner::OnLoadPageDone( | 30 void PrerenderingOffliner::OnLoadPageDone( |
| 28 const SavePageRequest& request, | 31 const SavePageRequest& request, |
| 29 const CompletionCallback& completion_callback, | |
| 30 Offliner::RequestStatus load_status, | 32 Offliner::RequestStatus load_status, |
| 31 content::WebContents* web_contents) { | 33 content::WebContents* web_contents) { |
| 32 // Check if request is still pending receiving a callback. | 34 // Check if request is still pending receiving a callback. |
| 33 // Note: it is possible to get a loaded page, start the save operation, | 35 // Note: it is possible to get a loaded page, start the save operation, |
| 34 // and then get another callback from the Loader (eg, if its loaded | 36 // and then get another callback from the Loader (eg, if its loaded |
| 35 // WebContents is being destroyed for some resource reclamation). | 37 // WebContents is being destroyed for some resource reclamation). |
| 36 if (!pending_request_) | 38 if (!pending_request_) |
| 37 return; | 39 return; |
| 38 | 40 |
| 39 // Since we are able to stop/cancel a previous load request, we should | 41 // Since we are able to stop/cancel a previous load request, we should |
| (...skipping 14 matching lines...) Expand all Loading... | |
| 54 std::unique_ptr<OfflinePageArchiver> archiver( | 56 std::unique_ptr<OfflinePageArchiver> archiver( |
| 55 new OfflinePageMHTMLArchiver(web_contents)); | 57 new OfflinePageMHTMLArchiver(web_contents)); |
| 56 // Pass in the URL from the WebContents in case it is redirected from | 58 // Pass in the URL from the WebContents in case it is redirected from |
| 57 // the requested URL. This is to work around a check in the | 59 // the requested URL. This is to work around a check in the |
| 58 // OfflinePageModel implementation that ensures URL passed in here is | 60 // OfflinePageModel implementation that ensures URL passed in here is |
| 59 // same as LastCommittedURL from the snapshot. | 61 // same as LastCommittedURL from the snapshot. |
| 60 // TODO(dougarnett): Raise issue of how to better deal with redirects. | 62 // TODO(dougarnett): Raise issue of how to better deal with redirects. |
| 61 SavePage(web_contents->GetLastCommittedURL(), request.client_id(), | 63 SavePage(web_contents->GetLastCommittedURL(), request.client_id(), |
| 62 std::move(archiver), | 64 std::move(archiver), |
| 63 base::Bind(&PrerenderingOffliner::OnSavePageDone, | 65 base::Bind(&PrerenderingOffliner::OnSavePageDone, |
| 64 weak_ptr_factory_.GetWeakPtr(), request, | 66 weak_ptr_factory_.GetWeakPtr(), request)); |
| 65 completion_callback)); | |
| 66 } else { | 67 } else { |
| 67 // Clear pending request and then run the completion callback. | 68 // Clear pending request and then run its completion callback. |
| 68 pending_request_.reset(nullptr); | 69 pending_request_.reset(nullptr); |
| 69 completion_callback.Run(request, load_status); | 70 app_listener_.reset(nullptr); |
|
fgorski
2016/07/19 16:09:44
nit: this does not match the comment and is in bet
dougarnett
2016/07/19 17:40:50
Revised comments to include.
| |
| 71 completion_callback_.Run(request, load_status); | |
| 70 } | 72 } |
| 71 } | 73 } |
| 72 | 74 |
| 73 void PrerenderingOffliner::OnSavePageDone( | 75 void PrerenderingOffliner::OnSavePageDone( |
| 74 const SavePageRequest& request, | 76 const SavePageRequest& request, |
| 75 const CompletionCallback& completion_callback, | |
| 76 SavePageResult save_result, | 77 SavePageResult save_result, |
| 77 int64_t offline_id) { | 78 int64_t offline_id) { |
| 78 // Check if request is still pending receiving a callback. | 79 // Check if request is still pending receiving a callback. |
| 79 if (!pending_request_) | 80 if (!pending_request_) |
| 80 return; | 81 return; |
| 81 | 82 |
| 82 // Also check that save callback is for same request as pending request | 83 // Also check that this completed request is same as the pending one |
| 83 // (since SavePage request is not cancel-able currently and could be old). | 84 // (since SavePage request is not cancel-able currently and could be old). |
| 84 if (request.request_id() != pending_request_->request_id()) { | 85 if (request.request_id() != pending_request_->request_id()) { |
| 85 DVLOG(1) << "Ignoring save callback for old request"; | 86 DVLOG(1) << "Ignoring save callback for old request"; |
| 86 return; | 87 return; |
| 87 } | 88 } |
| 88 | 89 |
| 89 // Clear pending request here and inform loader we are done with WebContents. | 90 // Clear pending request here and inform loader we are done with WebContents. |
| 90 pending_request_.reset(nullptr); | 91 pending_request_.reset(nullptr); |
| 92 app_listener_.reset(nullptr); | |
| 91 GetOrCreateLoader()->StopLoading(); | 93 GetOrCreateLoader()->StopLoading(); |
| 92 | 94 |
| 93 // Determine status and run the completion callback. | 95 // Determine status and run the completion callback. |
| 94 Offliner::RequestStatus save_status; | 96 Offliner::RequestStatus save_status; |
| 95 if (save_result == SavePageResult::SUCCESS) { | 97 if (save_result == SavePageResult::SUCCESS) { |
| 96 save_status = RequestStatus::SAVED; | 98 save_status = RequestStatus::SAVED; |
| 97 } else { | 99 } else { |
| 98 // TODO(dougarnett): Consider reflecting some recommendation to retry the | 100 // TODO(dougarnett): Consider reflecting some recommendation to retry the |
| 99 // request based on specific save error cases. | 101 // request based on specific save error cases. |
| 100 save_status = RequestStatus::SAVE_FAILED; | 102 save_status = RequestStatus::SAVE_FAILED; |
| 101 } | 103 } |
| 102 completion_callback.Run(request, save_status); | 104 completion_callback_.Run(request, save_status); |
| 103 } | 105 } |
| 104 | 106 |
| 105 bool PrerenderingOffliner::LoadAndSave(const SavePageRequest& request, | 107 bool PrerenderingOffliner::LoadAndSave(const SavePageRequest& request, |
| 106 const CompletionCallback& callback) { | 108 const CompletionCallback& callback) { |
| 107 if (pending_request_) { | 109 if (pending_request_) { |
| 108 DVLOG(1) << "Already have pending request"; | 110 DVLOG(1) << "Already have pending request"; |
| 109 return false; | 111 return false; |
| 110 } | 112 } |
| 111 | 113 |
| 112 if (!GetOrCreateLoader()->CanPrerender()) { | 114 if (!GetOrCreateLoader()->CanPrerender()) { |
| 113 DVLOG(1) << "Prerendering not allowed/configured"; | 115 DVLOG(1) << "Prerendering not allowed/configured"; |
| 114 return false; | 116 return false; |
| 115 } | 117 } |
| 116 | 118 |
| 117 if (!OfflinePageModel::CanSaveURL(request.url())) { | 119 if (!OfflinePageModel::CanSaveURL(request.url())) { |
| 118 DVLOG(1) << "Not able to save page for requested url: " << request.url(); | 120 DVLOG(1) << "Not able to save page for requested url: " << request.url(); |
| 119 return false; | 121 return false; |
| 120 } | 122 } |
| 121 | 123 |
| 122 // Track copy of pending request for callback handling. | 124 // Track copy of pending request for callback handling. |
| 123 pending_request_.reset(new SavePageRequest(request)); | 125 pending_request_.reset(new SavePageRequest(request)); |
| 126 completion_callback_ = callback; | |
| 124 | 127 |
| 125 // Kick off load page attempt. | 128 // Kick off load page attempt. |
| 126 bool accepted = GetOrCreateLoader()->LoadPage( | 129 bool accepted = GetOrCreateLoader()->LoadPage( |
| 127 request.url(), | 130 request.url(), base::Bind(&PrerenderingOffliner::OnLoadPageDone, |
| 128 base::Bind(&PrerenderingOffliner::OnLoadPageDone, | 131 weak_ptr_factory_.GetWeakPtr(), request)); |
| 129 weak_ptr_factory_.GetWeakPtr(), request, callback)); | 132 if (!accepted) { |
| 130 if (!accepted) | |
| 131 pending_request_.reset(nullptr); | 133 pending_request_.reset(nullptr); |
| 134 } else { | |
| 135 app_listener_.reset(new base::android::ApplicationStatusListener( | |
| 136 base::Bind(&PrerenderingOffliner::OnApplicationStateChange, | |
| 137 weak_ptr_factory_.GetWeakPtr()))); | |
| 138 } | |
| 132 | 139 |
| 133 return accepted; | 140 return accepted; |
| 134 } | 141 } |
| 135 | 142 |
| 136 void PrerenderingOffliner::Cancel() { | 143 void PrerenderingOffliner::Cancel() { |
| 137 if (pending_request_) { | 144 if (pending_request_) { |
| 138 pending_request_.reset(nullptr); | 145 pending_request_.reset(nullptr); |
| 146 app_listener_.reset(nullptr); | |
| 139 GetOrCreateLoader()->StopLoading(); | 147 GetOrCreateLoader()->StopLoading(); |
| 140 // TODO(dougarnett): Consider ability to cancel SavePage request. | 148 // TODO(dougarnett): Consider ability to cancel SavePage request. |
| 141 } | 149 } |
| 142 } | 150 } |
| 143 | 151 |
| 144 void PrerenderingOffliner::SetLoaderForTesting( | 152 void PrerenderingOffliner::SetLoaderForTesting( |
| 145 std::unique_ptr<PrerenderingLoader> loader) { | 153 std::unique_ptr<PrerenderingLoader> loader) { |
| 146 DCHECK(!loader_); | 154 DCHECK(!loader_); |
| 147 loader_ = std::move(loader); | 155 loader_ = std::move(loader); |
| 148 } | 156 } |
| 149 | 157 |
| 158 void PrerenderingOffliner::SetLowEndDeviceForTesting(bool is_low_end_device) { | |
| 159 is_low_end_device_ = is_low_end_device; | |
| 160 } | |
| 161 | |
| 162 void PrerenderingOffliner::SetApplicationStateForTesting( | |
| 163 base::android::ApplicationState application_state) { | |
| 164 OnApplicationStateChange(application_state); | |
| 165 } | |
| 166 | |
| 150 void PrerenderingOffliner::SavePage( | 167 void PrerenderingOffliner::SavePage( |
| 151 const GURL& url, | 168 const GURL& url, |
| 152 const ClientId& client_id, | 169 const ClientId& client_id, |
| 153 std::unique_ptr<OfflinePageArchiver> archiver, | 170 std::unique_ptr<OfflinePageArchiver> archiver, |
| 154 const SavePageCallback& callback) { | 171 const SavePageCallback& save_callback) { |
| 155 DCHECK(offline_page_model_); | 172 DCHECK(offline_page_model_); |
| 156 offline_page_model_->SavePage(url, client_id, std::move(archiver), callback); | 173 offline_page_model_->SavePage(url, client_id, std::move(archiver), |
| 174 save_callback); | |
| 157 } | 175 } |
| 158 | 176 |
| 159 PrerenderingLoader* PrerenderingOffliner::GetOrCreateLoader() { | 177 PrerenderingLoader* PrerenderingOffliner::GetOrCreateLoader() { |
| 160 if (!loader_) { | 178 if (!loader_) { |
| 161 loader_.reset(new PrerenderingLoader(browser_context_)); | 179 loader_.reset(new PrerenderingLoader(browser_context_)); |
| 162 } | 180 } |
| 163 return loader_.get(); | 181 return loader_.get(); |
| 164 } | 182 } |
| 165 | 183 |
| 184 void PrerenderingOffliner::OnApplicationStateChange( | |
| 185 base::android::ApplicationState application_state) { | |
| 186 if (pending_request_ && is_low_end_device_ && | |
| 187 application_state == | |
| 188 base::android::APPLICATION_STATE_HAS_RUNNING_ACTIVITIES) { | |
| 189 DVLOG(1) << "App became active, canceling current offlining request"; | |
| 190 SavePageRequest* request = pending_request_.get(); | |
| 191 Cancel(); | |
| 192 completion_callback_.Run(*request, | |
| 193 Offliner::RequestStatus::FOREGROUND_CANCELED); | |
| 194 } | |
| 195 } | |
| 196 | |
| 166 } // namespace offline_pages | 197 } // namespace offline_pages |
| OLD | NEW |