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

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: bleh it works now 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_)
66 return;
67
68 if (save_state_ != NONE) {
69 save_state_ = DELETE_AFTER_SAVE;
70 return;
71 }
72
73 ResetState();
74 }
75
76 void BackgroundLoaderOffliner::DidStopLoading() {
77 if (!pending_request_.get()) {
78 DVLOG(1) << "DidStopLoading called even though no pending request.";
79 return;
80 }
81
82 save_state_ = SAVING;
83 SavePageRequest request(*pending_request_.get());
84 content::WebContents* web_contents(
85 content::WebContentsObserver::web_contents());
86
87 std::unique_ptr<OfflinePageArchiver> archiver(
88 new OfflinePageMHTMLArchiver(web_contents));
89
90 OfflinePageModel::SavePageParams params;
91 params.url = web_contents->GetLastCommittedURL();
92 params.client_id = request.client_id();
93 params.proposed_offline_id = request.request_id();
94 offline_page_model_->SavePage(
95 params, std::move(archiver),
96 base::Bind(&BackgroundLoaderOffliner::OnPageSaved,
97 weak_ptr_factory_.GetWeakPtr()));
98 }
99
100 void BackgroundLoaderOffliner::OnPageSaved(SavePageResult save_result,
101 int64_t offline_id) {
102 if (!pending_request_)
103 return;
104
105 SavePageRequest request(*pending_request_.get());
106 ResetState();
107
108 if (save_state_ == DELETE_AFTER_SAVE) {
109 save_state_ = NONE;
110 return;
111 }
112
113 save_state_ = NONE;
114
115 Offliner::RequestStatus save_status;
116 if (save_result == SavePageResult::SUCCESS)
117 save_status = RequestStatus::SAVED;
118 else
119 save_status = RequestStatus::SAVE_FAILED;
120
121 completion_callback_.Run(request, save_status);
122 }
123
124 void BackgroundLoaderOffliner::ResetState() {
125 pending_request_.reset();
126 // TODO(chili): Remove after RequestCoordinator can handle multiple offliners.
127 // We reset the loader and observer after completion so loaders
128 // will not be re-used across different requests/tries. This is a temporary
129 // solution while there exists assumptions about the number of offliners
130 // there are.
131 loader_.reset(
132 new background_loader::BackgroundLoaderContents(browser_context_));
133 content::WebContentsObserver::Observe(loader_.get()->web_contents());
134 }
135
136 void BackgroundLoaderOffliner::OnApplicationStateChange(
137 base::android::ApplicationState application_state) {
138 if (pending_request_ && is_low_end_device_ &&
139 application_state ==
140 base::android::APPLICATION_STATE_HAS_RUNNING_ACTIVITIES) {
141 DVLOG(1) << "App became active, canceling current offlining request";
142 SavePageRequest* request = pending_request_.get();
143 Cancel();
144 completion_callback_.Run(*request, RequestStatus::FOREGROUND_CANCELED);
145 }
146 }
147
148 } // namespace offline_pages
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698