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

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

Issue 2635023005: Minor improvements to RecentTabHelper. (Closed)
Patch Set: Removed unneeded DCHECK_CURRENTLY_ON. Created 3 years, 11 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 (c) 2016 The Chromium Authors. All rights reserved. 1 // Copyright (c) 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/recent_tab_helper.h" 5 #include "chrome/browser/android/offline_pages/recent_tab_helper.h"
6 6
7 #include <queue> 7 #include <queue>
8 #include <vector> 8 #include <vector>
9 9
10 #include "base/bind.h" 10 #include "base/bind.h"
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after
47 return base::ThreadTaskRunnerHandle::Get(); 47 return base::ThreadTaskRunnerHandle::Get();
48 } 48 }
49 bool GetTabId(content::WebContents* web_contents, int* tab_id) override { 49 bool GetTabId(content::WebContents* web_contents, int* tab_id) override {
50 return offline_pages::OfflinePageUtils::GetTabId(web_contents, tab_id); 50 return offline_pages::OfflinePageUtils::GetTabId(web_contents, tab_id);
51 } 51 }
52 }; 52 };
53 } // namespace 53 } // namespace
54 54
55 namespace offline_pages { 55 namespace offline_pages {
56 56
57 bool RecentTabHelper::SnapshotProgressInfo::IsForLastN() {
58 // A last_n snapshot always has an invalid request id.
59 return request_id == OfflinePageModel::kInvalidOfflineId;
60 }
61
57 RecentTabHelper::RecentTabHelper(content::WebContents* web_contents) 62 RecentTabHelper::RecentTabHelper(content::WebContents* web_contents)
58 : content::WebContentsObserver(web_contents), 63 : content::WebContentsObserver(web_contents),
59 page_model_(nullptr),
60 snapshots_enabled_(false),
61 is_page_ready_for_snapshot_(false),
62 delegate_(new DefaultDelegate()), 64 delegate_(new DefaultDelegate()),
63 weak_ptr_factory_(this) { 65 weak_ptr_factory_(this) {
64 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); 66 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
65 } 67 }
66 68
67 RecentTabHelper::~RecentTabHelper() { 69 RecentTabHelper::~RecentTabHelper() {
68 } 70 }
69 71
70 void RecentTabHelper::SetDelegate( 72 void RecentTabHelper::SetDelegate(
71 std::unique_ptr<RecentTabHelper::Delegate> delegate) { 73 std::unique_ptr<RecentTabHelper::Delegate> delegate) {
72 DCHECK(delegate); 74 DCHECK(delegate);
73 delegate_ = std::move(delegate); 75 delegate_ = std::move(delegate);
74 } 76 }
75 77
76 void RecentTabHelper::ObserveAndDownloadCurrentPage( 78 void RecentTabHelper::ObserveAndDownloadCurrentPage(
77 const ClientId& client_id, int64_t request_id) { 79 const ClientId& client_id, int64_t request_id) {
78 EnsureInitialized(); 80 EnsureInitialized();
79 download_info_ = base::MakeUnique<DownloadPageInfo>(client_id, request_id); 81 snapshot_info_ =
82 base::MakeUnique<SnapshotProgressInfo>(client_id, request_id);
80 83
81 // If this tab helper is not enabled, immediately give the job back to 84 // If this tab helper is not enabled, immediately give the job back to
82 // RequestCoordinator. 85 // RequestCoordinator.
83 if (!snapshots_enabled_ || !page_model_) { 86 if (!snapshots_enabled_ || !page_model_) {
84 ReportDownloadStatusToRequestCoordinator(); 87 ReportDownloadStatusToRequestCoordinator();
85 download_info_.reset(); 88 weak_ptr_factory_.InvalidateWeakPtrs();
89 snapshot_info_.reset();
86 return; 90 return;
87 } 91 }
88 92
89 // No snapshots yet happened on the current page - return and wait for some. 93 // No snapshots yet happened on the current page - return and wait for some.
90 if (!is_page_ready_for_snapshot_) 94 if (!is_page_ready_for_snapshot_)
91 return; 95 return;
92 96
93 // If snapshot already happened and we missed it, go ahead and snapshot now. 97 // If snapshot already happened and we missed it, go ahead and snapshot now.
94 OfflinePageModel::SavePageParams save_page_params; 98 OfflinePageModel::SavePageParams save_page_params;
95 save_page_params.url = web_contents()->GetLastCommittedURL(); 99 save_page_params.url = web_contents()->GetLastCommittedURL();
(...skipping 30 matching lines...) Expand all
126 130
127 if (!snapshots_enabled_) 131 if (!snapshots_enabled_)
128 return; 132 return;
129 133
130 page_model_ = OfflinePageModelFactory::GetForBrowserContext( 134 page_model_ = OfflinePageModelFactory::GetForBrowserContext(
131 web_contents()->GetBrowserContext()); 135 web_contents()->GetBrowserContext());
132 } 136 }
133 137
134 void RecentTabHelper::DidFinishNavigation( 138 void RecentTabHelper::DidFinishNavigation(
135 content::NavigationHandle* navigation_handle) { 139 content::NavigationHandle* navigation_handle) {
136 if (!navigation_handle->IsInMainFrame() || 140 if (!navigation_handle->IsInMainFrame() || navigation_handle->IsSamePage() ||
137 !navigation_handle->HasCommitted()) { 141 !navigation_handle->HasCommitted()) {
138 return; 142 return;
139 } 143 }
140 144
141 // Cancel tasks in flight that relate to the previous page.
142 weak_ptr_factory_.InvalidateWeakPtrs();
143
144 EnsureInitialized(); 145 EnsureInitialized();
145 if (!snapshots_enabled_) 146 if (!snapshots_enabled_)
146 return; 147 return;
147 148
149 // Cancel tasks in flight that relate to the previous page.
150 weak_ptr_factory_.InvalidateWeakPtrs();
151
148 // We navigated to a different page, lets report progress to Background 152 // We navigated to a different page, lets report progress to Background
149 // Offliner. 153 // Offliner.
150 if (download_info_ && !navigation_handle->IsSamePage()) { 154 ReportDownloadStatusToRequestCoordinator();
151 ReportDownloadStatusToRequestCoordinator();
152 }
153 155
154 if (offline_pages::IsOffliningRecentPagesEnabled()) { 156 if (offline_pages::IsOffliningRecentPagesEnabled()) {
155 download_info_ = base::MakeUnique<DownloadPageInfo>( 157 // Note: at any point in time |snapshot_info_| might be replaced with one
156 GetRecentPagesClientId(), OfflinePageModel::kInvalidOfflineId); 158 // created by ObserveAndDownloadCurrentPage for a download snapshot request.
159 snapshot_info_ =
160 base::MakeUnique<SnapshotProgressInfo>(GetRecentPagesClientId());
157 } else { 161 } else {
158 download_info_.reset(); 162 snapshot_info_.reset();
159 } 163 }
160 164
161 is_page_ready_for_snapshot_ = false; 165 is_page_ready_for_snapshot_ = false;
162 166
163 // New navigation, new snapshot session. 167 // New navigation, new snapshot session.
164 snapshot_url_ = web_contents()->GetLastCommittedURL(); 168 snapshot_url_ = web_contents()->GetLastCommittedURL();
165 169
166 // Check for conditions that would cause us not to snapshot. 170 // Check for conditions that would cause us not to snapshot.
167 bool can_save = !navigation_handle->IsErrorPage() && 171 bool can_save = !navigation_handle->IsErrorPage() &&
168 OfflinePageModel::CanSaveURL(snapshot_url_) && 172 OfflinePageModel::CanSaveURL(snapshot_url_) &&
(...skipping 14 matching lines...) Expand all
183 snapshot_controller_->DocumentAvailableInMainFrame(); 187 snapshot_controller_->DocumentAvailableInMainFrame();
184 } 188 }
185 189
186 void RecentTabHelper::DocumentOnLoadCompletedInMainFrame() { 190 void RecentTabHelper::DocumentOnLoadCompletedInMainFrame() {
187 EnsureInitialized(); 191 EnsureInitialized();
188 snapshot_controller_->DocumentOnLoadCompletedInMainFrame(); 192 snapshot_controller_->DocumentOnLoadCompletedInMainFrame();
189 } 193 }
190 194
191 void RecentTabHelper::WebContentsDestroyed() { 195 void RecentTabHelper::WebContentsDestroyed() {
192 // WebContents (and maybe Tab) is destroyed, report status to Offliner. 196 // WebContents (and maybe Tab) is destroyed, report status to Offliner.
193 if (!download_info_)
194 return;
195 ReportDownloadStatusToRequestCoordinator(); 197 ReportDownloadStatusToRequestCoordinator();
196 } 198 }
197 199
198 200
199 // This starts a sequence of async operations chained through callbacks: 201 // This starts a sequence of async operations chained through callbacks:
200 // - compute the set of old 'last_n' pages that have to be purged 202 // - compute the set of old 'last_n' pages that have to be purged
201 // - delete the pages found in the previous step 203 // - delete the pages found in the previous step
202 // - snapshot the current web contents 204 // - snapshot the current web contents
203 // Along the chain, the original URL is passed and compared, to detect 205 // Along the chain, the original URL is passed and compared, to detect
204 // possible navigation and cancel snapshot in that case. 206 // possible navigation and cancel snapshot in that case.
205 void RecentTabHelper::StartSnapshot() { 207 void RecentTabHelper::StartSnapshot() {
206 is_page_ready_for_snapshot_ = true; 208 is_page_ready_for_snapshot_ = true;
207 209
208 if (!snapshots_enabled_ || 210 if (!snapshots_enabled_ || !page_model_ || !snapshot_info_) {
209 !page_model_ ||
210 !download_info_) {
211 ReportSnapshotCompleted(); 211 ReportSnapshotCompleted();
212 return; 212 return;
213 } 213 }
214 214
215 // Remove previously captured pages for this tab. 215 // Remove previously captured pages for this tab.
216 page_model_->GetOfflineIdsForClientId( 216 page_model_->GetOfflineIdsForClientId(
217 GetRecentPagesClientId(), 217 GetRecentPagesClientId(),
218 base::Bind(&RecentTabHelper::ContinueSnapshotWithIdsToPurge, 218 base::Bind(&RecentTabHelper::ContinueSnapshotWithIdsToPurge,
219 weak_ptr_factory_.GetWeakPtr())); 219 weak_ptr_factory_.GetWeakPtr()));
220 } 220 }
221 221
222 void RecentTabHelper::ContinueSnapshotWithIdsToPurge( 222 void RecentTabHelper::ContinueSnapshotWithIdsToPurge(
223 const std::vector<int64_t>& page_ids) { 223 const std::vector<int64_t>& page_ids) {
224 if (!download_info_) 224 DCHECK(snapshot_info_);
225 return;
226 225
227 // Also remove the download page if this is not a first snapshot. 226 // Also remove the download page if this is not a first snapshot.
228 std::vector<int64_t> ids(page_ids); 227 std::vector<int64_t> ids(page_ids);
229 ids.push_back(download_info_->request_id_); 228 ids.push_back(snapshot_info_->request_id);
230 229
231 page_model_->DeletePagesByOfflineId( 230 page_model_->DeletePagesByOfflineId(
232 ids, base::Bind(&RecentTabHelper::ContinueSnapshotAfterPurge, 231 ids, base::Bind(&RecentTabHelper::ContinueSnapshotAfterPurge,
233 weak_ptr_factory_.GetWeakPtr())); 232 weak_ptr_factory_.GetWeakPtr()));
234 } 233 }
235 234
236 void RecentTabHelper::ContinueSnapshotAfterPurge( 235 void RecentTabHelper::ContinueSnapshotAfterPurge(
237 OfflinePageModel::DeletePageResult result) { 236 OfflinePageModel::DeletePageResult result) {
238 if (!download_info_ || 237 DCHECK(snapshot_info_);
239 result != OfflinePageModel::DeletePageResult::SUCCESS || 238 DCHECK_EQ(snapshot_url_, web_contents()->GetLastCommittedURL());
240 !IsSamePage()) { 239 if (result != OfflinePageModel::DeletePageResult::SUCCESS) {
241 ReportSnapshotCompleted(); 240 ReportSnapshotCompleted();
242 return; 241 return;
243 } 242 }
244 243
245 OfflinePageModel::SavePageParams save_page_params; 244 OfflinePageModel::SavePageParams save_page_params;
246 save_page_params.url = snapshot_url_; 245 save_page_params.url = snapshot_url_;
247 save_page_params.client_id = download_info_->client_id_; 246 save_page_params.client_id = snapshot_info_->client_id;
248 save_page_params.proposed_offline_id = download_info_->request_id_; 247 save_page_params.proposed_offline_id = snapshot_info_->request_id;
249 page_model_->SavePage(save_page_params, 248 page_model_->SavePage(save_page_params,
250 delegate_->CreatePageArchiver(web_contents()), 249 delegate_->CreatePageArchiver(web_contents()),
251 base::Bind(&RecentTabHelper::SavePageCallback, 250 base::Bind(&RecentTabHelper::SavePageCallback,
252 weak_ptr_factory_.GetWeakPtr())); 251 weak_ptr_factory_.GetWeakPtr()));
253 } 252 }
254 253
255 void RecentTabHelper::SavePageCallback(OfflinePageModel::SavePageResult result, 254 void RecentTabHelper::SavePageCallback(OfflinePageModel::SavePageResult result,
256 int64_t offline_id) { 255 int64_t offline_id) {
257 if (!download_info_) 256 DCHECK(snapshot_info_);
258 return; 257 snapshot_info_->page_snapshot_completed = (result == SavePageResult::SUCCESS);
259 download_info_->page_snapshot_completed_ =
260 (result == SavePageResult::SUCCESS);
261 ReportSnapshotCompleted(); 258 ReportSnapshotCompleted();
262 } 259 }
263 260
264 void RecentTabHelper::ReportSnapshotCompleted() { 261 void RecentTabHelper::ReportSnapshotCompleted() {
265 snapshot_controller_->PendingSnapshotCompleted(); 262 snapshot_controller_->PendingSnapshotCompleted();
266 // Tell RequestCoordinator how the request should be processed further. 263 // Tell RequestCoordinator how the request should be processed further.
267 ReportDownloadStatusToRequestCoordinator(); 264 ReportDownloadStatusToRequestCoordinator();
268 } 265 }
269 266
270 void RecentTabHelper::ReportDownloadStatusToRequestCoordinator() { 267 void RecentTabHelper::ReportDownloadStatusToRequestCoordinator() {
271 if (!download_info_) 268 if (!snapshot_info_ || snapshot_info_->IsForLastN())
272 return;
273
274 if (download_info_->request_id_ == OfflinePageModel::kInvalidOfflineId)
275 return; 269 return;
276 270
277 RequestCoordinator* request_coordinator = 271 RequestCoordinator* request_coordinator =
278 RequestCoordinatorFactory::GetForBrowserContext( 272 RequestCoordinatorFactory::GetForBrowserContext(
279 web_contents()->GetBrowserContext()); 273 web_contents()->GetBrowserContext());
280 if (!request_coordinator) 274 if (!request_coordinator)
281 return; 275 return;
282 276
283 // It is OK to call these methods more then once, depending on 277 // It is OK to call these methods more then once, depending on
284 // number of snapshots attempted in this tab helper. If the request_id is not 278 // number of snapshots attempted in this tab helper. If the request_id is not
285 // in the list of RequestCoordinator, these calls have no effect. 279 // in the list of RequestCoordinator, these calls have no effect.
286 if (download_info_->page_snapshot_completed_) 280 if (snapshot_info_->page_snapshot_completed)
287 request_coordinator->MarkRequestCompleted(download_info_->request_id_); 281 request_coordinator->MarkRequestCompleted(snapshot_info_->request_id);
288 else 282 else
289 request_coordinator->EnableForOffliner(download_info_->request_id_, 283 request_coordinator->EnableForOffliner(snapshot_info_->request_id,
290 download_info_->client_id_); 284 snapshot_info_->client_id);
291 }
292
293 bool RecentTabHelper::IsSamePage() const {
294 return web_contents() &&
295 (web_contents()->GetLastCommittedURL() == snapshot_url_);
296 } 285 }
297 286
298 ClientId RecentTabHelper::GetRecentPagesClientId() const { 287 ClientId RecentTabHelper::GetRecentPagesClientId() const {
299 return ClientId(kLastNNamespace, tab_id_); 288 return ClientId(kLastNNamespace, tab_id_);
300 } 289 }
301 290
302 } // namespace offline_pages 291 } // namespace offline_pages
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698