Chromium Code Reviews| Index: chrome/browser/android/offline_pages/background_loader_offliner.cc |
| diff --git a/chrome/browser/android/offline_pages/background_loader_offliner.cc b/chrome/browser/android/offline_pages/background_loader_offliner.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..6997cac6caacc6f0f267389e5f3d7b06dcff2bdf |
| --- /dev/null |
| +++ b/chrome/browser/android/offline_pages/background_loader_offliner.cc |
| @@ -0,0 +1,127 @@ |
| +// Copyright 2016 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "chrome/browser/android/offline_pages/background_loader_offliner.h" |
| + |
| +#include "base/sys_info.h" |
| +#include "chrome/browser/android/offline_pages/offline_page_mhtml_archiver.h" |
| +#include "components/offline_pages/background/save_page_request.h" |
| +#include "components/offline_pages/offline_page_model.h" |
| +#include "content/public/browser/browser_context.h" |
| +#include "content/public/browser/web_contents.h" |
| + |
| +namespace offline_pages { |
| + |
| +BackgroundLoaderOffliner::BackgroundLoaderOffliner( |
| + content::BrowserContext* browser_context, |
| + const OfflinerPolicy* policy, |
| + OfflinePageModel* offline_page_model) |
| + : WebContentsObserver(), |
| + browser_context_(browser_context), |
| + offline_page_model_(offline_page_model), |
| + pending_request_(nullptr), |
| + app_listener_(nullptr), |
| + is_low_end_device_(base::SysInfo::IsLowEndDevice()), |
| + weak_ptr_factory_(this) { |
| + ResetState(); |
| +} |
| + |
| +BackgroundLoaderOffliner::~BackgroundLoaderOffliner() {} |
| + |
| +bool BackgroundLoaderOffliner::LoadAndSave(const SavePageRequest& request, |
| + const CompletionCallback& callback) { |
| + DCHECK(!pending_request_.get()); |
| + |
| + if (pending_request_) { |
| + DVLOG(1) << "Already have pending request"; |
| + return false; |
| + } |
| + |
| + if (!OfflinePageModel::CanSaveURL(request.url())) { |
| + DVLOG(1) << "Not able to save page for requested url: " << request.url(); |
| + return false; |
| + } |
| + |
| + // Track copy of pending request. |
| + pending_request_.reset(new SavePageRequest(request)); |
| + completion_callback_ = callback; |
| + |
| + loader_.get()->LoadPage(request.url()); |
| + |
| + app_listener_.reset(new base::android::ApplicationStatusListener( |
|
Pete Williamson
2016/12/01 02:03:38
Does the listener need to be set before we start l
chili
2016/12/09 22:45:57
Done.
|
| + base::Bind(&BackgroundLoaderOffliner::OnApplicationStateChange, |
| + weak_ptr_factory_.GetWeakPtr()))); |
| + |
| + return true; |
| +} |
| + |
| +void BackgroundLoaderOffliner::Cancel() { |
| + if (pending_request_) { |
| + pending_request_.reset(nullptr); |
| + ResetState(); |
| + } |
| +} |
| + |
| +void BackgroundLoaderOffliner::DidStopLoading() { |
| + if (!pending_request_.get()) |
|
Pete Williamson
2016/12/01 02:03:38
Maybe we should log this, it indicates a problem.
chili
2016/12/09 22:45:57
done.
|
| + return; |
| + |
| + SavePageRequest request(*pending_request_.get()); |
|
Pete Williamson
2016/12/01 02:03:38
Can DidStopLoading() also get called for a page th
chili
2016/12/09 22:45:57
Yes. I've updated the original doc with what to d
|
| + content::WebContents* web_contents( |
| + content::WebContentsObserver::web_contents()); |
| + |
| + std::unique_ptr<OfflinePageArchiver> archiver( |
| + new OfflinePageMHTMLArchiver(web_contents)); |
| + |
| + DCHECK(offline_page_model_); |
| + OfflinePageModel::SavePageParams params; |
| + params.url = web_contents->GetLastCommittedURL(); |
| + params.client_id = request.client_id(); |
| + params.proposed_offline_id = request.request_id(); |
| + offline_page_model_->SavePage( |
| + params, std::move(archiver), |
| + base::Bind(&BackgroundLoaderOffliner::OnPageSaved, |
| + weak_ptr_factory_.GetWeakPtr())); |
| +} |
| + |
| +void BackgroundLoaderOffliner::OnPageSaved(SavePageResult save_result, |
| + int64_t offline_id) { |
| + if (!pending_request_) |
| + return; |
| + |
| + SavePageRequest request(*pending_request_.get()); |
| + |
| + pending_request_.reset(nullptr); |
| + ResetState(); |
| + |
| + Offliner::RequestStatus save_status; |
| + if (save_result == SavePageResult::SUCCESS) { |
| + save_status = RequestStatus::SAVED; |
| + } else { |
| + save_status = RequestStatus::SAVE_FAILED; |
| + } |
| + |
| + completion_callback_.Run(request, save_status); |
| +} |
| + |
| +void BackgroundLoaderOffliner::ResetState() { |
| + loader_.reset( |
|
dougarnett
2016/12/02 19:12:31
this is a bit interesting here so might be good to
chili
2016/12/09 22:45:57
Done.
|
| + new background_loader::BackgroundLoaderContents(browser_context_)); |
| + content::WebContentsObserver::Observe(loader_.get()->web_contents()); |
| +} |
| + |
| +void BackgroundLoaderOffliner::OnApplicationStateChange( |
| + base::android::ApplicationState application_state) { |
| + if (pending_request_ && is_low_end_device_ && |
|
dougarnett
2016/12/01 00:10:10
Btw, I hope we can drop this for new loader (but m
chili
2016/12/01 02:01:26
I was doing comparison between what I had and what
|
| + application_state == |
| + base::android::APPLICATION_STATE_HAS_RUNNING_ACTIVITIES) { |
| + DVLOG(1) << "App became active, canceling current offlining request"; |
| + SavePageRequest* request = pending_request_.get(); |
| + Cancel(); |
| + completion_callback_.Run(*request, |
| + RequestStatus::FOREGROUND_CANCELED); |
| + } |
| +} |
| + |
| +} // namespace offline_pages |