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" |
11 #include "chrome/browser/android/offline_pages/offliner_helper.h" | 11 #include "chrome/browser/android/offline_pages/offliner_helper.h" |
12 #include "chrome/browser/profiles/profile.h" | 12 #include "chrome/browser/profiles/profile.h" |
13 #include "components/offline_pages/core/background/save_page_request.h" | 13 #include "components/offline_pages/core/background/save_page_request.h" |
14 #include "components/offline_pages/core/client_namespace_constants.h" | 14 #include "components/offline_pages/core/client_namespace_constants.h" |
| 15 #include "components/offline_pages/core/downloads/download_ui_adapter.h" |
15 #include "components/offline_pages/core/offline_page_model.h" | 16 #include "components/offline_pages/core/offline_page_model.h" |
16 #include "content/public/browser/browser_context.h" | 17 #include "content/public/browser/browser_context.h" |
17 #include "content/public/browser/web_contents.h" | 18 #include "content/public/browser/web_contents.h" |
18 | 19 |
19 namespace offline_pages { | 20 namespace offline_pages { |
| 21 namespace { |
| 22 const char kDownloadUIAdapterKey[] = "download-ui-adapter"; |
| 23 } |
20 | 24 |
21 PrerenderingOffliner::PrerenderingOffliner( | 25 PrerenderingOffliner::PrerenderingOffliner( |
22 content::BrowserContext* browser_context, | 26 content::BrowserContext* browser_context, |
23 const OfflinerPolicy* policy, | 27 const OfflinerPolicy* policy, |
24 OfflinePageModel* offline_page_model) | 28 OfflinePageModel* offline_page_model) |
25 : browser_context_(browser_context), | 29 : browser_context_(browser_context), |
26 offline_page_model_(offline_page_model), | 30 offline_page_model_(offline_page_model), |
27 pending_request_(nullptr), | 31 pending_request_(nullptr), |
28 is_low_end_device_(base::SysInfo::IsLowEndDevice()), | 32 is_low_end_device_(base::SysInfo::IsLowEndDevice()), |
29 app_listener_(nullptr), | 33 app_listener_(nullptr), |
30 weak_ptr_factory_(this) {} | 34 weak_ptr_factory_(this) {} |
31 | 35 |
32 PrerenderingOffliner::~PrerenderingOffliner() {} | 36 PrerenderingOffliner::~PrerenderingOffliner() {} |
33 | 37 |
| 38 void PrerenderingOffliner::OnNetworkProgress(const SavePageRequest& request, |
| 39 int64_t bytes) { |
| 40 if (!pending_request_) |
| 41 return; |
| 42 |
| 43 DownloadUIAdapter* ui_adapter = static_cast<DownloadUIAdapter*>( |
| 44 offline_page_model_->GetUserData(kDownloadUIAdapterKey)); |
| 45 |
| 46 if (!ui_adapter) |
| 47 return; |
| 48 |
| 49 ui_adapter->UpdateProgress(request.request_id(), bytes); |
| 50 } |
| 51 |
34 void PrerenderingOffliner::OnLoadPageDone( | 52 void PrerenderingOffliner::OnLoadPageDone( |
35 const SavePageRequest& request, | 53 const SavePageRequest& request, |
36 Offliner::RequestStatus load_status, | 54 Offliner::RequestStatus load_status, |
37 content::WebContents* web_contents) { | 55 content::WebContents* web_contents) { |
38 // Check if request is still pending receiving a callback. | 56 // Check if request is still pending receiving a callback. |
39 // Note: it is possible to get a loaded page, start the save operation, | 57 // Note: it is possible to get a loaded page, start the save operation, |
40 // and then get another callback from the Loader (eg, if its loaded | 58 // and then get another callback from the Loader (eg, if its loaded |
41 // WebContents is being destroyed for some resource reclamation). | 59 // WebContents is being destroyed for some resource reclamation). |
42 if (!pending_request_) | 60 if (!pending_request_) |
43 return; | 61 return; |
(...skipping 127 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
171 return false; | 189 return false; |
172 } | 190 } |
173 | 191 |
174 // Track copy of pending request for callback handling. | 192 // Track copy of pending request for callback handling. |
175 pending_request_.reset(new SavePageRequest(request)); | 193 pending_request_.reset(new SavePageRequest(request)); |
176 completion_callback_ = callback; | 194 completion_callback_ = callback; |
177 | 195 |
178 // Kick off load page attempt. | 196 // Kick off load page attempt. |
179 bool accepted = GetOrCreateLoader()->LoadPage( | 197 bool accepted = GetOrCreateLoader()->LoadPage( |
180 request.url(), base::Bind(&PrerenderingOffliner::OnLoadPageDone, | 198 request.url(), base::Bind(&PrerenderingOffliner::OnLoadPageDone, |
181 weak_ptr_factory_.GetWeakPtr(), request)); | 199 weak_ptr_factory_.GetWeakPtr(), request), |
| 200 base::Bind(&PrerenderingOffliner::OnNetworkProgress, |
| 201 weak_ptr_factory_.GetWeakPtr(), request)); |
182 if (!accepted) { | 202 if (!accepted) { |
183 pending_request_.reset(nullptr); | 203 pending_request_.reset(nullptr); |
184 } else { | 204 } else { |
185 // Create app listener for the pending request. | 205 // Create app listener for the pending request. |
186 app_listener_.reset(new base::android::ApplicationStatusListener( | 206 app_listener_.reset(new base::android::ApplicationStatusListener( |
187 base::Bind(&PrerenderingOffliner::OnApplicationStateChange, | 207 base::Bind(&PrerenderingOffliner::OnApplicationStateChange, |
188 weak_ptr_factory_.GetWeakPtr()))); | 208 weak_ptr_factory_.GetWeakPtr()))); |
189 } | 209 } |
190 | 210 |
191 return accepted; | 211 return accepted; |
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
238 base::android::APPLICATION_STATE_HAS_RUNNING_ACTIVITIES) { | 258 base::android::APPLICATION_STATE_HAS_RUNNING_ACTIVITIES) { |
239 DVLOG(1) << "App became active, canceling current offlining request"; | 259 DVLOG(1) << "App became active, canceling current offlining request"; |
240 SavePageRequest* request = pending_request_.get(); | 260 SavePageRequest* request = pending_request_.get(); |
241 Cancel(); | 261 Cancel(); |
242 completion_callback_.Run(*request, | 262 completion_callback_.Run(*request, |
243 Offliner::RequestStatus::FOREGROUND_CANCELED); | 263 Offliner::RequestStatus::FOREGROUND_CANCELED); |
244 } | 264 } |
245 } | 265 } |
246 | 266 |
247 } // namespace offline_pages | 267 } // namespace offline_pages |
OLD | NEW |