| 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/background_loader_offliner.h" | 5 #include "chrome/browser/android/offline_pages/background_loader_offliner.h" |
| 6 | 6 |
| 7 #include "base/metrics/histogram_macros.h" | 7 #include "base/metrics/histogram_macros.h" |
| 8 #include "base/sys_info.h" | 8 #include "base/sys_info.h" |
| 9 #include "chrome/browser/android/offline_pages/offline_page_mhtml_archiver.h" | 9 #include "chrome/browser/android/offline_pages/offline_page_mhtml_archiver.h" |
| 10 #include "chrome/browser/android/offline_pages/offliner_helper.h" | 10 #include "chrome/browser/android/offline_pages/offliner_helper.h" |
| 11 #include "chrome/browser/profiles/profile.h" | 11 #include "chrome/browser/profiles/profile.h" |
| 12 #include "components/offline_pages/core/background/save_page_request.h" | 12 #include "components/offline_pages/core/background/save_page_request.h" |
| 13 #include "components/offline_pages/core/client_namespace_constants.h" | 13 #include "components/offline_pages/core/client_namespace_constants.h" |
| 14 #include "components/offline_pages/core/offline_page_model.h" | 14 #include "components/offline_pages/core/offline_page_model.h" |
| 15 #include "content/public/browser/browser_context.h" | 15 #include "content/public/browser/browser_context.h" |
| 16 #include "content/public/browser/navigation_handle.h" | 16 #include "content/public/browser/navigation_handle.h" |
| 17 #include "content/public/browser/render_frame_host.h" | 17 #include "content/public/browser/render_frame_host.h" |
| 18 #include "content/public/browser/web_contents.h" | 18 #include "content/public/browser/web_contents.h" |
| 19 #include "content/public/browser/web_contents_user_data.h" | 19 #include "content/public/browser/web_contents_user_data.h" |
| 20 | 20 |
| 21 namespace offline_pages { | 21 namespace offline_pages { |
| 22 | 22 |
| 23 namespace { | 23 namespace { |
| 24 const long kOfflinePageDelayMs = 2000; | |
| 25 const long kOfflineDomContentLoadedMs = 25000; | |
| 26 | 24 |
| 27 class OfflinerData : public content::WebContentsUserData<OfflinerData> { | 25 class OfflinerData : public content::WebContentsUserData<OfflinerData> { |
| 28 public: | 26 public: |
| 29 static void AddToWebContents(content::WebContents* webcontents, | 27 static void AddToWebContents(content::WebContents* webcontents, |
| 30 BackgroundLoaderOffliner* offliner) { | 28 BackgroundLoaderOffliner* offliner) { |
| 31 DCHECK(offliner); | 29 DCHECK(offliner); |
| 32 webcontents->SetUserData(UserDataKey(), std::unique_ptr<OfflinerData>( | 30 webcontents->SetUserData(UserDataKey(), std::unique_ptr<OfflinerData>( |
| 33 new OfflinerData(offliner))); | 31 new OfflinerData(offliner))); |
| 34 } | 32 } |
| 35 | 33 |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 67 | 65 |
| 68 BackgroundLoaderOffliner::BackgroundLoaderOffliner( | 66 BackgroundLoaderOffliner::BackgroundLoaderOffliner( |
| 69 content::BrowserContext* browser_context, | 67 content::BrowserContext* browser_context, |
| 70 const OfflinerPolicy* policy, | 68 const OfflinerPolicy* policy, |
| 71 OfflinePageModel* offline_page_model) | 69 OfflinePageModel* offline_page_model) |
| 72 : browser_context_(browser_context), | 70 : browser_context_(browser_context), |
| 73 offline_page_model_(offline_page_model), | 71 offline_page_model_(offline_page_model), |
| 74 is_low_end_device_(base::SysInfo::IsLowEndDevice()), | 72 is_low_end_device_(base::SysInfo::IsLowEndDevice()), |
| 75 save_state_(NONE), | 73 save_state_(NONE), |
| 76 page_load_state_(SUCCESS), | 74 page_load_state_(SUCCESS), |
| 77 page_delay_ms_(kOfflinePageDelayMs), | |
| 78 network_bytes_(0LL), | 75 network_bytes_(0LL), |
| 79 weak_ptr_factory_(this) { | 76 weak_ptr_factory_(this) { |
| 80 DCHECK(offline_page_model_); | 77 DCHECK(offline_page_model_); |
| 81 DCHECK(browser_context_); | 78 DCHECK(browser_context_); |
| 82 } | 79 } |
| 83 | 80 |
| 84 BackgroundLoaderOffliner::~BackgroundLoaderOffliner() {} | 81 BackgroundLoaderOffliner::~BackgroundLoaderOffliner() {} |
| 85 | 82 |
| 86 // static | 83 // static |
| 87 BackgroundLoaderOffliner* BackgroundLoaderOffliner::FromWebContents( | 84 BackgroundLoaderOffliner* BackgroundLoaderOffliner::FromWebContents( |
| (...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 161 progress_callback_ = progress_callback; | 158 progress_callback_ = progress_callback; |
| 162 | 159 |
| 163 // Listen for app foreground/background change. | 160 // Listen for app foreground/background change. |
| 164 app_listener_.reset(new base::android::ApplicationStatusListener( | 161 app_listener_.reset(new base::android::ApplicationStatusListener( |
| 165 base::Bind(&BackgroundLoaderOffliner::OnApplicationStateChange, | 162 base::Bind(&BackgroundLoaderOffliner::OnApplicationStateChange, |
| 166 weak_ptr_factory_.GetWeakPtr()))); | 163 weak_ptr_factory_.GetWeakPtr()))); |
| 167 | 164 |
| 168 // Load page attempt. | 165 // Load page attempt. |
| 169 loader_.get()->LoadPage(request.url()); | 166 loader_.get()->LoadPage(request.url()); |
| 170 | 167 |
| 171 snapshot_controller_.reset( | 168 snapshot_controller_ = SnapshotController::CreateForBackgroundOfflining( |
| 172 new SnapshotController(base::ThreadTaskRunnerHandle::Get(), this, | 169 base::ThreadTaskRunnerHandle::Get(), this); |
| 173 kOfflineDomContentLoadedMs, page_delay_ms_)); | |
| 174 | 170 |
| 175 return true; | 171 return true; |
| 176 } | 172 } |
| 177 | 173 |
| 178 void BackgroundLoaderOffliner::Cancel(const CancelCallback& callback) { | 174 void BackgroundLoaderOffliner::Cancel(const CancelCallback& callback) { |
| 179 // TODO(chili): We are not able to cancel a pending | 175 // TODO(chili): We are not able to cancel a pending |
| 180 // OfflinePageModel::SaveSnapshot() operation. We will notify caller that | 176 // OfflinePageModel::SaveSnapshot() operation. We will notify caller that |
| 181 // cancel completed once the SavePage operation returns. | 177 // cancel completed once the SavePage operation returns. |
| 182 if (!pending_request_) { | 178 if (!pending_request_) { |
| 183 callback.Run(0LL); | 179 callback.Run(0LL); |
| (...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 254 switch (navigation_handle->GetNetErrorCode()) { | 250 switch (navigation_handle->GetNetErrorCode()) { |
| 255 case net::ERR_INTERNET_DISCONNECTED: | 251 case net::ERR_INTERNET_DISCONNECTED: |
| 256 page_load_state_ = DELAY_RETRY; | 252 page_load_state_ = DELAY_RETRY; |
| 257 break; | 253 break; |
| 258 default: | 254 default: |
| 259 page_load_state_ = RETRIABLE; | 255 page_load_state_ = RETRIABLE; |
| 260 } | 256 } |
| 261 } | 257 } |
| 262 } | 258 } |
| 263 | 259 |
| 264 void BackgroundLoaderOffliner::SetPageDelayForTest(long delay_ms) { | 260 void BackgroundLoaderOffliner::SetSnapshotControllerForTest( |
| 265 page_delay_ms_ = delay_ms; | 261 std::unique_ptr<SnapshotController> controller) { |
| 262 snapshot_controller_ = std::move(controller); |
| 266 } | 263 } |
| 267 | 264 |
| 268 void BackgroundLoaderOffliner::OnNetworkBytesChanged(int64_t bytes) { | 265 void BackgroundLoaderOffliner::OnNetworkBytesChanged(int64_t bytes) { |
| 269 if (pending_request_ && save_state_ != SAVING) { | 266 if (pending_request_ && save_state_ != SAVING) { |
| 270 network_bytes_ += bytes; | 267 network_bytes_ += bytes; |
| 271 progress_callback_.Run(*pending_request_, network_bytes_); | 268 progress_callback_.Run(*pending_request_, network_bytes_); |
| 272 } | 269 } |
| 273 } | 270 } |
| 274 | 271 |
| 275 void BackgroundLoaderOffliner::StartSnapshot() { | 272 void BackgroundLoaderOffliner::StartSnapshot() { |
| (...skipping 114 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 390 int64_t offline_id) { | 387 int64_t offline_id) { |
| 391 // If for some reason the request was reset during while waiting for callback | 388 // If for some reason the request was reset during while waiting for callback |
| 392 // ignore the completion callback. | 389 // ignore the completion callback. |
| 393 if (pending_request_ && pending_request_->request_id() != offline_id) | 390 if (pending_request_ && pending_request_->request_id() != offline_id) |
| 394 return; | 391 return; |
| 395 completion_callback_.Run(request, RequestStatus::FOREGROUND_CANCELED); | 392 completion_callback_.Run(request, RequestStatus::FOREGROUND_CANCELED); |
| 396 } | 393 } |
| 397 } // namespace offline_pages | 394 } // namespace offline_pages |
| 398 | 395 |
| 399 DEFINE_WEB_CONTENTS_USER_DATA_KEY(offline_pages::OfflinerData); | 396 DEFINE_WEB_CONTENTS_USER_DATA_KEY(offline_pages::OfflinerData); |
| OLD | NEW |