Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(38)

Side by Side Diff: chrome/browser/android/offline_pages/background_loader_offliner.cc

Issue 2534673002: [Offline pages] Create offliner that uses background loader (Closed)
Patch Set: test update Created 4 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
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
3 // found in the LICENSE file.
4
5 #include "chrome/browser/android/offline_pages/background_loader_offliner.h"
6
7 #include "base/sys_info.h"
8 #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/offline_page_model.h"
11 #include "content/public/browser/browser_context.h"
12 #include "content/public/browser/web_contents.h"
13
14 namespace offline_pages {
15
16 BackgroundLoaderOffliner::BackgroundLoaderOffliner(
17 content::BrowserContext* browser_context,
18 const OfflinerPolicy* policy,
19 OfflinePageModel* offline_page_model)
20 : WebContentsObserver(),
21 browser_context_(browser_context),
22 offline_page_model_(offline_page_model),
23 pending_request_(nullptr),
24 app_listener_(nullptr),
25 is_low_end_device_(base::SysInfo::IsLowEndDevice()),
26 weak_ptr_factory_(this) {
27 ResetState();
28 }
29
30 BackgroundLoaderOffliner::~BackgroundLoaderOffliner() {}
31
32 bool BackgroundLoaderOffliner::LoadAndSave(const SavePageRequest& request,
33 const CompletionCallback& callback) {
34 DCHECK(!pending_request_.get());
35
36 if (pending_request_) {
37 DVLOG(1) << "Already have pending request";
38 return false;
39 }
40
41 if (!OfflinePageModel::CanSaveURL(request.url())) {
42 DVLOG(1) << "Not able to save page for requested url: " << request.url();
43 return false;
44 }
45
46 // Track copy of pending request.
47 pending_request_.reset(new SavePageRequest(request));
48 completion_callback_ = callback;
49
50 loader_.get()->LoadPage(request.url());
51
52 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.
53 base::Bind(&BackgroundLoaderOffliner::OnApplicationStateChange,
54 weak_ptr_factory_.GetWeakPtr())));
55
56 return true;
57 }
58
59 void BackgroundLoaderOffliner::Cancel() {
60 if (pending_request_) {
61 pending_request_.reset(nullptr);
62 ResetState();
63 }
64 }
65
66 void BackgroundLoaderOffliner::DidStopLoading() {
67 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.
68 return;
69
70 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
71 content::WebContents* web_contents(
72 content::WebContentsObserver::web_contents());
73
74 std::unique_ptr<OfflinePageArchiver> archiver(
75 new OfflinePageMHTMLArchiver(web_contents));
76
77 DCHECK(offline_page_model_);
78 OfflinePageModel::SavePageParams params;
79 params.url = web_contents->GetLastCommittedURL();
80 params.client_id = request.client_id();
81 params.proposed_offline_id = request.request_id();
82 offline_page_model_->SavePage(
83 params, std::move(archiver),
84 base::Bind(&BackgroundLoaderOffliner::OnPageSaved,
85 weak_ptr_factory_.GetWeakPtr()));
86 }
87
88 void BackgroundLoaderOffliner::OnPageSaved(SavePageResult save_result,
89 int64_t offline_id) {
90 if (!pending_request_)
91 return;
92
93 SavePageRequest request(*pending_request_.get());
94
95 pending_request_.reset(nullptr);
96 ResetState();
97
98 Offliner::RequestStatus save_status;
99 if (save_result == SavePageResult::SUCCESS) {
100 save_status = RequestStatus::SAVED;
101 } else {
102 save_status = RequestStatus::SAVE_FAILED;
103 }
104
105 completion_callback_.Run(request, save_status);
106 }
107
108 void BackgroundLoaderOffliner::ResetState() {
109 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.
110 new background_loader::BackgroundLoaderContents(browser_context_));
111 content::WebContentsObserver::Observe(loader_.get()->web_contents());
112 }
113
114 void BackgroundLoaderOffliner::OnApplicationStateChange(
115 base::android::ApplicationState application_state) {
116 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
117 application_state ==
118 base::android::APPLICATION_STATE_HAS_RUNNING_ACTIVITIES) {
119 DVLOG(1) << "App became active, canceling current offlining request";
120 SavePageRequest* request = pending_request_.get();
121 Cancel();
122 completion_callback_.Run(*request,
123 RequestStatus::FOREGROUND_CANCELED);
124 }
125 }
126
127 } // namespace offline_pages
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698