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

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: Code review responses 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/core/background/save_page_request.h"
10 #include "components/offline_pages/core/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 : browser_context_(browser_context),
21 offline_page_model_(offline_page_model),
22 is_low_end_device_(base::SysInfo::IsLowEndDevice()),
23 save_state_(NONE),
24 weak_ptr_factory_(this) {
25 DCHECK(offline_page_model_);
26 DCHECK(browser_context_);
27 }
28
29 BackgroundLoaderOffliner::~BackgroundLoaderOffliner() {}
30
31 bool BackgroundLoaderOffliner::LoadAndSave(const SavePageRequest& request,
32 const CompletionCallback& callback) {
33 DCHECK(callback);
34
35 if (pending_request_) {
36 DVLOG(1) << "Already have pending request";
37 return false;
38 }
39
40 if (!OfflinePageModel::CanSaveURL(request.url())) {
41 DVLOG(1) << "Not able to save page for requested url: " << request.url();
42 return false;
43 }
44
45 if (!loader_)
46 ResetState();
47
48 // Track copy of pending request.
49 pending_request_.reset(new SavePageRequest(request));
50 completion_callback_ = callback;
51
52 // Listen for app foreground/background change.
53 app_listener_.reset(new base::android::ApplicationStatusListener(
54 base::Bind(&BackgroundLoaderOffliner::OnApplicationStateChange,
55 weak_ptr_factory_.GetWeakPtr())));
56
57 loader_.get()->LoadPage(request.url());
58
59 return true;
60 }
61
62 void BackgroundLoaderOffliner::Cancel() {
63 // TODO(chili): We are not able to cancel a pending
64 // OfflinePageModel::SavePage() operation. We just ignore the callback.
65 if (pending_request_) {
fgorski 2016/12/15 22:01:13 how about? if (!pending_request_) return; if (
chili 2016/12/15 23:50:36 Done.
66 if (save_state_ != NONE) {
fgorski 2016/12/15 21:59:07 I don't understand the logic here. Can we chat abo
chili 2016/12/15 23:50:36 Done.
67 save_state_ = DELETE_AFTER_SAVE;
68 } else {
69 ResetState();
70 }
71 }
72 }
73
74 void BackgroundLoaderOffliner::DidStopLoading() {
75 if (!pending_request_.get()) {
76 DVLOG(1) << "DidStopLoading called even though no pending request.";
77 return;
78 }
79
80 save_state_ = SAVING;
81 SavePageRequest request(*pending_request_.get());
82 content::WebContents* web_contents(
83 content::WebContentsObserver::web_contents());
84
85 std::unique_ptr<OfflinePageArchiver> archiver(
86 new OfflinePageMHTMLArchiver(web_contents));
87
88 OfflinePageModel::SavePageParams params;
89 params.url = web_contents->GetLastCommittedURL();
90 params.client_id = request.client_id();
91 params.proposed_offline_id = request.request_id();
92 offline_page_model_->SavePage(
93 params, std::move(archiver),
94 base::Bind(&BackgroundLoaderOffliner::OnPageSaved,
95 weak_ptr_factory_.GetWeakPtr()));
96 }
97
98 void BackgroundLoaderOffliner::OnPageSaved(SavePageResult save_result,
99 int64_t offline_id) {
100 if (!pending_request_)
101 return;
102
103 SavePageRequest request(*pending_request_.get());
104 ResetState();
105
106 if (save_state_ == DELETE_AFTER_SAVE) {
107 save_state_ = NONE;
108 return;
109 }
110
111 save_state_ = NONE;
112
113 Offliner::RequestStatus save_status;
114 if (save_result == SavePageResult::SUCCESS)
115 save_status = RequestStatus::SAVED;
116 else
117 save_status = RequestStatus::SAVE_FAILED;
118
119 completion_callback_.Run(request, save_status);
120 }
121
122 void BackgroundLoaderOffliner::ResetState() {
123 pending_request_.reset();
124 // TODO(chili): Remove after RequestCoordinator can handle multiple offliners.
125 // We reset the loader and observer after completion so loaders
126 // will not be re-used across different requests/tries. This is a temporary
127 // solution while there exists assumptions about the number of offliners
128 // there are.
129 loader_.reset(
130 new background_loader::BackgroundLoaderContents(browser_context_));
131 content::WebContentsObserver::Observe(loader_.get()->web_contents());
132 }
133
134 void BackgroundLoaderOffliner::OnApplicationStateChange(
135 base::android::ApplicationState application_state) {
136 if (pending_request_ && is_low_end_device_ &&
137 application_state ==
138 base::android::APPLICATION_STATE_HAS_RUNNING_ACTIVITIES) {
139 DVLOG(1) << "App became active, canceling current offlining request";
140 SavePageRequest* request = pending_request_.get();
141 Cancel();
142 completion_callback_.Run(*request, RequestStatus::FOREGROUND_CANCELED);
143 }
144 }
145
146 } // namespace offline_pages
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698