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

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

Issue 2149773002: PrerenderingOffliner will abort background load if it sees chrome transitioning to foreground on lo… (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Revise some comments wrt app listener setting/clearing Created 4 years, 5 months 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
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/prerendering_offliner.h" 5 #include "chrome/browser/android/offline_pages/prerendering_offliner.h"
6 6
7 #include "base/bind.h" 7 #include "base/bind.h"
8 #include "base/sys_info.h"
8 #include "chrome/browser/android/offline_pages/offline_page_mhtml_archiver.h" 9 #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/background/save_page_request.h"
10 #include "components/offline_pages/offline_page_model.h" 11 #include "components/offline_pages/offline_page_model.h"
11 #include "content/public/browser/browser_context.h" 12 #include "content/public/browser/browser_context.h"
12 #include "content/public/browser/web_contents.h" 13 #include "content/public/browser/web_contents.h"
13 14
14 namespace offline_pages { 15 namespace offline_pages {
15 16
16 PrerenderingOffliner::PrerenderingOffliner( 17 PrerenderingOffliner::PrerenderingOffliner(
17 content::BrowserContext* browser_context, 18 content::BrowserContext* browser_context,
18 const OfflinerPolicy* policy, 19 const OfflinerPolicy* policy,
19 OfflinePageModel* offline_page_model) 20 OfflinePageModel* offline_page_model)
20 : browser_context_(browser_context), 21 : browser_context_(browser_context),
21 offline_page_model_(offline_page_model), 22 offline_page_model_(offline_page_model),
22 pending_request_(nullptr), 23 pending_request_(nullptr),
24 is_low_end_device_(base::SysInfo::IsLowEndDevice()),
25 app_listener_(nullptr),
23 weak_ptr_factory_(this) {} 26 weak_ptr_factory_(this) {}
24 27
25 PrerenderingOffliner::~PrerenderingOffliner() {} 28 PrerenderingOffliner::~PrerenderingOffliner() {}
26 29
27 void PrerenderingOffliner::OnLoadPageDone( 30 void PrerenderingOffliner::OnLoadPageDone(
28 const SavePageRequest& request, 31 const SavePageRequest& request,
29 const CompletionCallback& completion_callback,
30 Offliner::RequestStatus load_status, 32 Offliner::RequestStatus load_status,
31 content::WebContents* web_contents) { 33 content::WebContents* web_contents) {
32 // Check if request is still pending receiving a callback. 34 // Check if request is still pending receiving a callback.
33 // Note: it is possible to get a loaded page, start the save operation, 35 // Note: it is possible to get a loaded page, start the save operation,
34 // and then get another callback from the Loader (eg, if its loaded 36 // and then get another callback from the Loader (eg, if its loaded
35 // WebContents is being destroyed for some resource reclamation). 37 // WebContents is being destroyed for some resource reclamation).
36 if (!pending_request_) 38 if (!pending_request_)
37 return; 39 return;
38 40
39 // Since we are able to stop/cancel a previous load request, we should 41 // Since we are able to stop/cancel a previous load request, we should
(...skipping 14 matching lines...) Expand all
54 std::unique_ptr<OfflinePageArchiver> archiver( 56 std::unique_ptr<OfflinePageArchiver> archiver(
55 new OfflinePageMHTMLArchiver(web_contents)); 57 new OfflinePageMHTMLArchiver(web_contents));
56 // Pass in the URL from the WebContents in case it is redirected from 58 // Pass in the URL from the WebContents in case it is redirected from
57 // the requested URL. This is to work around a check in the 59 // the requested URL. This is to work around a check in the
58 // OfflinePageModel implementation that ensures URL passed in here is 60 // OfflinePageModel implementation that ensures URL passed in here is
59 // same as LastCommittedURL from the snapshot. 61 // same as LastCommittedURL from the snapshot.
60 // TODO(dougarnett): Raise issue of how to better deal with redirects. 62 // TODO(dougarnett): Raise issue of how to better deal with redirects.
61 SavePage(web_contents->GetLastCommittedURL(), request.client_id(), 63 SavePage(web_contents->GetLastCommittedURL(), request.client_id(),
62 std::move(archiver), 64 std::move(archiver),
63 base::Bind(&PrerenderingOffliner::OnSavePageDone, 65 base::Bind(&PrerenderingOffliner::OnSavePageDone,
64 weak_ptr_factory_.GetWeakPtr(), request, 66 weak_ptr_factory_.GetWeakPtr(), request));
65 completion_callback));
66 } else { 67 } else {
67 // Clear pending request and then run the completion callback. 68 // Clear pending request and app listener then run completion callback.
68 pending_request_.reset(nullptr); 69 pending_request_.reset(nullptr);
69 completion_callback.Run(request, load_status); 70 app_listener_.reset(nullptr);
71 completion_callback_.Run(request, load_status);
70 } 72 }
71 } 73 }
72 74
73 void PrerenderingOffliner::OnSavePageDone( 75 void PrerenderingOffliner::OnSavePageDone(
74 const SavePageRequest& request, 76 const SavePageRequest& request,
75 const CompletionCallback& completion_callback,
76 SavePageResult save_result, 77 SavePageResult save_result,
77 int64_t offline_id) { 78 int64_t offline_id) {
78 // Check if request is still pending receiving a callback. 79 // Check if request is still pending receiving a callback.
79 if (!pending_request_) 80 if (!pending_request_)
80 return; 81 return;
81 82
82 // Also check that save callback is for same request as pending request 83 // Also check that this completed request is same as the pending one
83 // (since SavePage request is not cancel-able currently and could be old). 84 // (since SavePage request is not cancel-able currently and could be old).
84 if (request.request_id() != pending_request_->request_id()) { 85 if (request.request_id() != pending_request_->request_id()) {
85 DVLOG(1) << "Ignoring save callback for old request"; 86 DVLOG(1) << "Ignoring save callback for old request";
86 return; 87 return;
87 } 88 }
88 89
89 // Clear pending request here and inform loader we are done with WebContents. 90 // Clear pending request and app listener here and then inform loader we
91 // are done with WebContents.
90 pending_request_.reset(nullptr); 92 pending_request_.reset(nullptr);
93 app_listener_.reset(nullptr);
91 GetOrCreateLoader()->StopLoading(); 94 GetOrCreateLoader()->StopLoading();
92 95
93 // Determine status and run the completion callback. 96 // Determine status and run the completion callback.
94 Offliner::RequestStatus save_status; 97 Offliner::RequestStatus save_status;
95 if (save_result == SavePageResult::SUCCESS) { 98 if (save_result == SavePageResult::SUCCESS) {
96 save_status = RequestStatus::SAVED; 99 save_status = RequestStatus::SAVED;
97 } else { 100 } else {
98 // TODO(dougarnett): Consider reflecting some recommendation to retry the 101 // TODO(dougarnett): Consider reflecting some recommendation to retry the
99 // request based on specific save error cases. 102 // request based on specific save error cases.
100 save_status = RequestStatus::SAVE_FAILED; 103 save_status = RequestStatus::SAVE_FAILED;
101 } 104 }
102 completion_callback.Run(request, save_status); 105 completion_callback_.Run(request, save_status);
103 } 106 }
104 107
105 bool PrerenderingOffliner::LoadAndSave(const SavePageRequest& request, 108 bool PrerenderingOffliner::LoadAndSave(const SavePageRequest& request,
106 const CompletionCallback& callback) { 109 const CompletionCallback& callback) {
107 if (pending_request_) { 110 if (pending_request_) {
108 DVLOG(1) << "Already have pending request"; 111 DVLOG(1) << "Already have pending request";
109 return false; 112 return false;
110 } 113 }
111 114
112 if (!GetOrCreateLoader()->CanPrerender()) { 115 if (!GetOrCreateLoader()->CanPrerender()) {
113 DVLOG(1) << "Prerendering not allowed/configured"; 116 DVLOG(1) << "Prerendering not allowed/configured";
114 return false; 117 return false;
115 } 118 }
116 119
117 if (!OfflinePageModel::CanSaveURL(request.url())) { 120 if (!OfflinePageModel::CanSaveURL(request.url())) {
118 DVLOG(1) << "Not able to save page for requested url: " << request.url(); 121 DVLOG(1) << "Not able to save page for requested url: " << request.url();
119 return false; 122 return false;
120 } 123 }
121 124
122 // Track copy of pending request for callback handling. 125 // Track copy of pending request for callback handling.
123 pending_request_.reset(new SavePageRequest(request)); 126 pending_request_.reset(new SavePageRequest(request));
127 completion_callback_ = callback;
124 128
125 // Kick off load page attempt. 129 // Kick off load page attempt.
126 bool accepted = GetOrCreateLoader()->LoadPage( 130 bool accepted = GetOrCreateLoader()->LoadPage(
127 request.url(), 131 request.url(), base::Bind(&PrerenderingOffliner::OnLoadPageDone,
128 base::Bind(&PrerenderingOffliner::OnLoadPageDone, 132 weak_ptr_factory_.GetWeakPtr(), request));
129 weak_ptr_factory_.GetWeakPtr(), request, callback)); 133 if (!accepted) {
130 if (!accepted)
131 pending_request_.reset(nullptr); 134 pending_request_.reset(nullptr);
135 } else {
136 // Create app listener for the pending request.
137 app_listener_.reset(new base::android::ApplicationStatusListener(
138 base::Bind(&PrerenderingOffliner::OnApplicationStateChange,
139 weak_ptr_factory_.GetWeakPtr())));
140 }
132 141
133 return accepted; 142 return accepted;
134 } 143 }
135 144
136 void PrerenderingOffliner::Cancel() { 145 void PrerenderingOffliner::Cancel() {
137 if (pending_request_) { 146 if (pending_request_) {
138 pending_request_.reset(nullptr); 147 pending_request_.reset(nullptr);
148 app_listener_.reset(nullptr);
139 GetOrCreateLoader()->StopLoading(); 149 GetOrCreateLoader()->StopLoading();
140 // TODO(dougarnett): Consider ability to cancel SavePage request. 150 // TODO(dougarnett): Consider ability to cancel SavePage request.
141 } 151 }
142 } 152 }
143 153
144 void PrerenderingOffliner::SetLoaderForTesting( 154 void PrerenderingOffliner::SetLoaderForTesting(
145 std::unique_ptr<PrerenderingLoader> loader) { 155 std::unique_ptr<PrerenderingLoader> loader) {
146 DCHECK(!loader_); 156 DCHECK(!loader_);
147 loader_ = std::move(loader); 157 loader_ = std::move(loader);
148 } 158 }
149 159
160 void PrerenderingOffliner::SetLowEndDeviceForTesting(bool is_low_end_device) {
161 is_low_end_device_ = is_low_end_device;
162 }
163
164 void PrerenderingOffliner::SetApplicationStateForTesting(
165 base::android::ApplicationState application_state) {
166 OnApplicationStateChange(application_state);
167 }
168
150 void PrerenderingOffliner::SavePage( 169 void PrerenderingOffliner::SavePage(
151 const GURL& url, 170 const GURL& url,
152 const ClientId& client_id, 171 const ClientId& client_id,
153 std::unique_ptr<OfflinePageArchiver> archiver, 172 std::unique_ptr<OfflinePageArchiver> archiver,
154 const SavePageCallback& callback) { 173 const SavePageCallback& save_callback) {
155 DCHECK(offline_page_model_); 174 DCHECK(offline_page_model_);
156 offline_page_model_->SavePage(url, client_id, std::move(archiver), callback); 175 offline_page_model_->SavePage(url, client_id, std::move(archiver),
176 save_callback);
157 } 177 }
158 178
159 PrerenderingLoader* PrerenderingOffliner::GetOrCreateLoader() { 179 PrerenderingLoader* PrerenderingOffliner::GetOrCreateLoader() {
160 if (!loader_) { 180 if (!loader_) {
161 loader_.reset(new PrerenderingLoader(browser_context_)); 181 loader_.reset(new PrerenderingLoader(browser_context_));
162 } 182 }
163 return loader_.get(); 183 return loader_.get();
164 } 184 }
165 185
186 void PrerenderingOffliner::OnApplicationStateChange(
187 base::android::ApplicationState application_state) {
188 if (pending_request_ && is_low_end_device_ &&
189 application_state ==
190 base::android::APPLICATION_STATE_HAS_RUNNING_ACTIVITIES) {
191 DVLOG(1) << "App became active, canceling current offlining request";
192 SavePageRequest* request = pending_request_.get();
193 Cancel();
194 completion_callback_.Run(*request,
195 Offliner::RequestStatus::FOREGROUND_CANCELED);
196 }
197 }
198
166 } // namespace offline_pages 199 } // namespace offline_pages
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698