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

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

Issue 2713993002: RecentTabHelper: adds DVLOG and reload unit test. (Closed)
Patch Set: Created 3 years, 10 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
« no previous file with comments | « no previous file | chrome/browser/android/offline_pages/recent_tab_helper_unittest.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 99 matching lines...) Expand 10 before | Expand all | Expand 10 after
110 const ClientId& client_id, int64_t request_id) { 110 const ClientId& client_id, int64_t request_id) {
111 // Note: as this implementation only supports one client namespace, enforce 111 // Note: as this implementation only supports one client namespace, enforce
112 // that the call is from Downloads. 112 // that the call is from Downloads.
113 DCHECK_EQ(kDownloadNamespace, client_id.name_space); 113 DCHECK_EQ(kDownloadNamespace, client_id.name_space);
114 auto new_downloads_snapshot_info = 114 auto new_downloads_snapshot_info =
115 base::MakeUnique<SnapshotProgressInfo>(client_id, request_id); 115 base::MakeUnique<SnapshotProgressInfo>(client_id, request_id);
116 116
117 // If this tab helper is not enabled, immediately give the job back to 117 // If this tab helper is not enabled, immediately give the job back to
118 // RequestCoordinator. 118 // RequestCoordinator.
119 if (!EnsureInitialized()) { 119 if (!EnsureInitialized()) {
120 DVLOG(1) << "Snapshots disabled; ignored download request for: "
121 << web_contents()->GetLastCommittedURL().spec();
120 ReportDownloadStatusToRequestCoordinator(new_downloads_snapshot_info.get(), 122 ReportDownloadStatusToRequestCoordinator(new_downloads_snapshot_info.get(),
121 false); 123 false);
122 return; 124 return;
123 } 125 }
124 126
125 // If there is an ongoing snapshot request, completely ignore this one and 127 // If there is an ongoing snapshot request, completely ignore this one and
126 // cancel the Background Offliner request. 128 // cancel the Background Offliner request.
127 // TODO(carlosk): it might be better to make the decision to schedule or not 129 // TODO(carlosk): it might be better to make the decision to schedule or not
128 // the background request here. See https://crbug.com/686165. 130 // the background request here. See https://crbug.com/686165.
129 // TODO(carlosk): there is an edge case that happens when the ongoing request 131 // TODO(carlosk): there is an edge case that happens when the ongoing request
130 // was automatically and transparently scheduled by a navigation event and 132 // was automatically and transparently scheduled by a navigation event and
131 // this call happens due to the user pressing the download button. The user's 133 // this call happens due to the user pressing the download button. The user's
132 // request to download the page will be immediately dismissed. See 134 // request to download the page will be immediately dismissed. See
133 // https://crbug.com/686283. 135 // https://crbug.com/686283.
134 if (downloads_ongoing_snapshot_info_) { 136 if (downloads_ongoing_snapshot_info_) {
137 DVLOG(1) << "Ongoing request exist; ignored download request for: "
138 << web_contents()->GetLastCommittedURL().spec();
135 ReportDownloadStatusToRequestCoordinator(new_downloads_snapshot_info.get(), 139 ReportDownloadStatusToRequestCoordinator(new_downloads_snapshot_info.get(),
136 true); 140 true);
137 return; 141 return;
138 } 142 }
139 143
140 // Stores the new snapshot info. 144 // Stores the new snapshot info.
141 downloads_ongoing_snapshot_info_ = std::move(new_downloads_snapshot_info); 145 downloads_ongoing_snapshot_info_ = std::move(new_downloads_snapshot_info);
142 146
143 // If the page is not yet ready for a snapshot return now as it will be 147 // If the page is not yet ready for a snapshot return now as it will be
144 // started later, once page loading advances. 148 // started later, once page loading advances.
145 if (PageQuality::POOR == snapshot_controller_->current_page_quality()) { 149 if (PageQuality::POOR == snapshot_controller_->current_page_quality()) {
150 DVLOG(1) << "Waiting for loading page to serve download request for: "
151 << web_contents()->GetLastCommittedURL().spec();
146 downloads_snapshot_on_hold_ = true; 152 downloads_snapshot_on_hold_ = true;
147 return; 153 return;
148 } 154 }
149 155
150 // Otherwise start saving the snapshot now. 156 // Otherwise start saving the snapshot now.
157 DVLOG(1) << "Starting download request for: "
158 << web_contents()->GetLastCommittedURL().spec();
151 SaveSnapshotForDownloads(false); 159 SaveSnapshotForDownloads(false);
152 } 160 }
153 161
154 // Initialize lazily. It needs TabAndroid for initialization, which is also a 162 // Initialize lazily. It needs TabAndroid for initialization, which is also a
155 // TabHelper - so can't initialize in constructor because of uncertain order 163 // TabHelper - so can't initialize in constructor because of uncertain order
156 // of creation of TabHelpers. 164 // of creation of TabHelpers.
157 bool RecentTabHelper::EnsureInitialized() { 165 bool RecentTabHelper::EnsureInitialized() {
158 if (snapshot_controller_) // Initialized already. 166 if (snapshot_controller_) // Initialized already.
159 return snapshots_enabled_; 167 return snapshots_enabled_;
160 168
(...skipping 17 matching lines...) Expand all
178 web_contents()->GetBrowserContext()); 186 web_contents()->GetBrowserContext());
179 } 187 }
180 188
181 return snapshots_enabled_; 189 return snapshots_enabled_;
182 } 190 }
183 191
184 void RecentTabHelper::DidFinishNavigation( 192 void RecentTabHelper::DidFinishNavigation(
185 content::NavigationHandle* navigation_handle) { 193 content::NavigationHandle* navigation_handle) {
186 if (!navigation_handle->IsInMainFrame() || 194 if (!navigation_handle->IsInMainFrame() ||
187 !navigation_handle->HasCommitted() || navigation_handle->IsSamePage()) { 195 !navigation_handle->HasCommitted() || navigation_handle->IsSamePage()) {
196 DVLOG_IF(1, navigation_handle->IsInMainFrame())
197 << "Main frame navigation ignored (reasons: "
198 << !navigation_handle->HasCommitted() << ", "
199 << navigation_handle->IsSamePage()
200 << ") to: " << web_contents()->GetLastCommittedURL().spec();
188 return; 201 return;
189 } 202 }
190 203
191 if (!EnsureInitialized()) 204 if (!EnsureInitialized())
192 return; 205 return;
206 DVLOG(1) << "Navigation acknowledged to: "
207 << web_contents()->GetLastCommittedURL().spec();
193 208
194 // If there is an ongoing downloads request, lets allow Background Offliner to 209 // If there is an ongoing downloads request, lets make Background Offliner
195 // continue downloading this page. 210 // take over downloading that page.
196 if (downloads_ongoing_snapshot_info_) { 211 if (downloads_ongoing_snapshot_info_) {
212 DVLOG(1) << " - Passing ongoing downloads request to Background Offliner";
197 ReportDownloadStatusToRequestCoordinator( 213 ReportDownloadStatusToRequestCoordinator(
198 downloads_ongoing_snapshot_info_.get(), false); 214 downloads_ongoing_snapshot_info_.get(), false);
199 } 215 }
200 216
201 // Cancel any and all in flight snapshot tasks from the previous page. 217 // Cancel any and all in flight snapshot tasks from the previous page.
202 CancelInFlightSnapshots(); 218 CancelInFlightSnapshots();
203 downloads_snapshot_on_hold_ = false; 219 downloads_snapshot_on_hold_ = false;
204 220
205 // Always reset so that posted tasks get canceled. 221 // Always reset so that posted tasks get canceled.
206 snapshot_controller_->Reset(); 222 snapshot_controller_->Reset();
207 223
208 // Check for conditions that would cause us not to snapshot. 224 // Check for conditions that would cause us not to snapshot.
209 bool can_save = 225 bool can_save =
210 !navigation_handle->IsErrorPage() && 226 !navigation_handle->IsErrorPage() &&
211 OfflinePageModel::CanSaveURL(web_contents()->GetLastCommittedURL()) && 227 OfflinePageModel::CanSaveURL(web_contents()->GetLastCommittedURL()) &&
212 OfflinePageUtils::GetOfflinePageFromWebContents(web_contents()) == 228 OfflinePageUtils::GetOfflinePageFromWebContents(web_contents()) ==
213 nullptr; 229 nullptr;
230 DVLOG_IF(1, !can_save) << " - Page can not be saved";
214 231
215 UMA_HISTOGRAM_BOOLEAN("OfflinePages.CanSaveRecentPage", can_save); 232 UMA_HISTOGRAM_BOOLEAN("OfflinePages.CanSaveRecentPage", can_save);
216 233
217 if (!can_save) 234 if (!can_save)
218 snapshot_controller_->Stop(); 235 snapshot_controller_->Stop();
219 last_n_listen_to_tab_hidden_ = can_save && !delegate_->IsLowEndDevice() && 236 last_n_listen_to_tab_hidden_ = can_save && !delegate_->IsLowEndDevice() &&
220 IsOffliningRecentPagesEnabled(); 237 IsOffliningRecentPagesEnabled();
238 DVLOG_IF(1, !last_n_listen_to_tab_hidden_) << " - Last_n is disabled";
221 } 239 }
222 240
223 void RecentTabHelper::DocumentAvailableInMainFrame() { 241 void RecentTabHelper::DocumentAvailableInMainFrame() {
224 EnsureInitialized(); 242 EnsureInitialized();
225 snapshot_controller_->DocumentAvailableInMainFrame(); 243 snapshot_controller_->DocumentAvailableInMainFrame();
226 } 244 }
227 245
228 void RecentTabHelper::DocumentOnLoadCompletedInMainFrame() { 246 void RecentTabHelper::DocumentOnLoadCompletedInMainFrame() {
229 EnsureInitialized(); 247 EnsureInitialized();
230 snapshot_controller_->DocumentOnLoadCompletedInMainFrame(); 248 snapshot_controller_->DocumentOnLoadCompletedInMainFrame();
231 } 249 }
232 250
233 void RecentTabHelper::WebContentsDestroyed() { 251 void RecentTabHelper::WebContentsDestroyed() {
234 // If there is an ongoing downloads request, lets allow Background Offliner to 252 // If there is an ongoing downloads request, lets allow Background Offliner to
235 // continue downloading this page. 253 // continue downloading this page.
236 if (downloads_ongoing_snapshot_info_) 254 if (downloads_ongoing_snapshot_info_) {
255 DVLOG(1) << "WebContents destroyed; passing ongoing downloads request to "
256 "Background Offliner";
237 ReportDownloadStatusToRequestCoordinator( 257 ReportDownloadStatusToRequestCoordinator(
238 downloads_ongoing_snapshot_info_.get(), false); 258 downloads_ongoing_snapshot_info_.get(), false);
259 }
239 // And cancel any ongoing snapshots. 260 // And cancel any ongoing snapshots.
240 CancelInFlightSnapshots(); 261 CancelInFlightSnapshots();
241 } 262 }
242 263
243 // TODO(carlosk): this method is also called when the tab is being closed, when 264 // TODO(carlosk): this method is also called when the tab is being closed, when
244 // saving a snapshot is probably useless (low probability of the user undoing 265 // saving a snapshot is probably useless (low probability of the user undoing
245 // the close). We should detect that and avoid the saving. 266 // the close). We should detect that and avoid the saving.
246 void RecentTabHelper::WasHidden() { 267 void RecentTabHelper::WasHidden() {
247 if (!IsOffliningRecentPagesEnabled()) 268 if (!IsOffliningRecentPagesEnabled())
248 return; 269 return;
249 270
250 // Return immediately if last_n is not listening to tab hidden events or if a 271 // Return immediately if last_n is not listening to tab hidden events, if a
251 // last_n snapshot is currently being saved. 272 // last_n snapshot is currently being saved or if the tab is closing.
252 if (!last_n_listen_to_tab_hidden_ || last_n_ongoing_snapshot_info_) 273 if (!last_n_listen_to_tab_hidden_ || last_n_ongoing_snapshot_info_) {
274 DVLOG(1) << "Will not snapshot for last_n (reasons: "
275 << !last_n_listen_to_tab_hidden_ << ", "
276 << !!last_n_ongoing_snapshot_info_
277 << ") for: " << web_contents()->GetLastCommittedURL().spec();
253 return; 278 return;
279 }
254 280
255 // Do not save if page quality is too low. 281 // Do not save if page quality is too low.
256 // Note: we assume page quality for a page can only increase. 282 // Note: we assume page quality for a page can only increase.
257 if (snapshot_controller_->current_page_quality() == PageQuality::POOR) 283 if (snapshot_controller_->current_page_quality() == PageQuality::POOR) {
284 DVLOG(1) << "Will not snapshot for last_n (page quality too low) for: "
285 << web_contents()->GetLastCommittedURL().spec();
258 return; 286 return;
287 }
259 288
289 DVLOG(1) << "Starting last_n snapshot for: "
290 << web_contents()->GetLastCommittedURL().spec();
260 last_n_ongoing_snapshot_info_ = 291 last_n_ongoing_snapshot_info_ =
261 base::MakeUnique<SnapshotProgressInfo>(GetRecentPagesClientId()); 292 base::MakeUnique<SnapshotProgressInfo>(GetRecentPagesClientId());
262 DCHECK(last_n_ongoing_snapshot_info_->IsForLastN()); 293 DCHECK(last_n_ongoing_snapshot_info_->IsForLastN());
263 DCHECK(snapshots_enabled_); 294 DCHECK(snapshots_enabled_);
264 // Remove previously captured pages for this tab. 295 // Remove previously captured pages for this tab.
265 page_model_->GetOfflineIdsForClientId( 296 page_model_->GetOfflineIdsForClientId(
266 GetRecentPagesClientId(), 297 GetRecentPagesClientId(),
267 base::Bind(&RecentTabHelper::ContinueSnapshotWithIdsToPurge, 298 base::Bind(&RecentTabHelper::ContinueSnapshotWithIdsToPurge,
268 weak_ptr_factory_.GetWeakPtr(), 299 weak_ptr_factory_.GetWeakPtr(),
269 last_n_ongoing_snapshot_info_.get())); 300 last_n_ongoing_snapshot_info_.get()));
270 } 301 }
271 302
272 // TODO(carlosk): rename this to RequestSnapshot and make it return a bool 303 // TODO(carlosk): rename this to RequestSnapshot and make it return a bool
273 // representing the acceptance of the snapshot request. 304 // representing the acceptance of the snapshot request.
274 void RecentTabHelper::StartSnapshot() { 305 void RecentTabHelper::StartSnapshot() {
275 DCHECK_NE(PageQuality::POOR, snapshot_controller_->current_page_quality()); 306 DCHECK_NE(PageQuality::POOR, snapshot_controller_->current_page_quality());
276 307
277 // As long as snapshots are enabled for this tab, there are two situations 308 // As long as snapshots are enabled for this tab, there are two situations
278 // that allow for a navigation event to start a snapshot: 309 // that allow for a navigation event to start a snapshot:
279 // 1) There is a request on hold waiting for the page to be minimally loaded. 310 // 1) There is a request on hold waiting for the page to be minimally loaded.
280 if (snapshots_enabled_ && downloads_snapshot_on_hold_) { 311 if (snapshots_enabled_ && downloads_snapshot_on_hold_) {
312 DVLOG(1) << "Resuming downloads snapshot request for: "
313 << web_contents()->GetLastCommittedURL().spec();
281 downloads_snapshot_on_hold_ = false; 314 downloads_snapshot_on_hold_ = false;
282 SaveSnapshotForDownloads(false); 315 SaveSnapshotForDownloads(false);
283 return; 316 return;
284 } 317 }
285 318
286 // 2) There's no ongoing snapshot and a previous one was saved with lower 319 // 2) There's no ongoing snapshot and a previous one was saved with lower
287 // expected quality than what would be possible now. 320 // expected quality than what would be possible now.
288 if (snapshots_enabled_ && 321 if (snapshots_enabled_ &&
289 (!downloads_ongoing_snapshot_info_ && 322 (!downloads_ongoing_snapshot_info_ &&
290 downloads_latest_saved_snapshot_info_ && 323 downloads_latest_saved_snapshot_info_ &&
291 downloads_latest_saved_snapshot_info_->expected_page_quality < 324 downloads_latest_saved_snapshot_info_->expected_page_quality <
292 snapshot_controller_->current_page_quality())) { 325 snapshot_controller_->current_page_quality())) {
326 DVLOG(1) << "Upgrading last downloads snapshot for: "
327 << web_contents()->GetLastCommittedURL().spec();
293 SaveSnapshotForDownloads(true); 328 SaveSnapshotForDownloads(true);
294 return; 329 return;
295 } 330 }
296 331
297 // Notify the controller that a snapshot was not started. 332 // Notify the controller that a snapshot was not started.
298 snapshot_controller_->PendingSnapshotCompleted(); 333 snapshot_controller_->PendingSnapshotCompleted();
299 } 334 }
300 335
301 void RecentTabHelper::SaveSnapshotForDownloads(bool replace_latest) { 336 void RecentTabHelper::SaveSnapshotForDownloads(bool replace_latest) {
302 DCHECK_NE(PageQuality::POOR, snapshot_controller_->current_page_quality()); 337 DCHECK_NE(PageQuality::POOR, snapshot_controller_->current_page_quality());
(...skipping 24 matching lines...) Expand all
327 // 4) Notify requesters about the final result of the operation. 362 // 4) Notify requesters about the final result of the operation.
328 // 363 //
329 // For last_n requests the sequence is always started in 1). For downloads it 364 // For last_n requests the sequence is always started in 1). For downloads it
330 // starts in either 2) or 3). Step 4) might be called anytime during the chain 365 // starts in either 2) or 3). Step 4) might be called anytime during the chain
331 // for early termination in case of errors. 366 // for early termination in case of errors.
332 void RecentTabHelper::ContinueSnapshotWithIdsToPurge( 367 void RecentTabHelper::ContinueSnapshotWithIdsToPurge(
333 SnapshotProgressInfo* snapshot_info, 368 SnapshotProgressInfo* snapshot_info,
334 const std::vector<int64_t>& page_ids) { 369 const std::vector<int64_t>& page_ids) {
335 DCHECK(snapshot_info); 370 DCHECK(snapshot_info);
336 371
372 DVLOG_IF(1, !page_ids.empty()) << "Deleting " << page_ids.size()
373 << " offline pages...";
337 page_model_->DeletePagesByOfflineId( 374 page_model_->DeletePagesByOfflineId(
338 page_ids, base::Bind(&RecentTabHelper::ContinueSnapshotAfterPurge, 375 page_ids, base::Bind(&RecentTabHelper::ContinueSnapshotAfterPurge,
339 weak_ptr_factory_.GetWeakPtr(), snapshot_info)); 376 weak_ptr_factory_.GetWeakPtr(), snapshot_info));
340 } 377 }
341 378
342 void RecentTabHelper::ContinueSnapshotAfterPurge( 379 void RecentTabHelper::ContinueSnapshotAfterPurge(
343 SnapshotProgressInfo* snapshot_info, 380 SnapshotProgressInfo* snapshot_info,
344 OfflinePageModel::DeletePageResult result) { 381 OfflinePageModel::DeletePageResult result) {
345 if (result != OfflinePageModel::DeletePageResult::SUCCESS) { 382 if (result != OfflinePageModel::DeletePageResult::SUCCESS) {
346 ReportSnapshotCompleted(snapshot_info, false); 383 ReportSnapshotCompleted(snapshot_info, false);
(...skipping 20 matching lines...) Expand all
367 DCHECK(snapshot_info->IsForLastN() || 404 DCHECK(snapshot_info->IsForLastN() ||
368 snapshot_info->request_id == offline_id); 405 snapshot_info->request_id == offline_id);
369 ReportSnapshotCompleted(snapshot_info, result == SavePageResult::SUCCESS); 406 ReportSnapshotCompleted(snapshot_info, result == SavePageResult::SUCCESS);
370 } 407 }
371 408
372 // Note: this is the final step in the chain of callbacks and it's where the 409 // Note: this is the final step in the chain of callbacks and it's where the
373 // behavior is different depending on this being a last_n or downloads snapshot. 410 // behavior is different depending on this being a last_n or downloads snapshot.
374 void RecentTabHelper::ReportSnapshotCompleted( 411 void RecentTabHelper::ReportSnapshotCompleted(
375 SnapshotProgressInfo* snapshot_info, 412 SnapshotProgressInfo* snapshot_info,
376 bool success) { 413 bool success) {
414 DVLOG(1) << (snapshot_info->IsForLastN() ? "Last_n" : "Downloads")
415 << " snapshot " << (success ? "succeeded" : "failed")
416 << " for: " << web_contents()->GetLastCommittedURL().spec();
377 if (snapshot_info->IsForLastN()) { 417 if (snapshot_info->IsForLastN()) {
378 DCHECK_EQ(snapshot_info, last_n_ongoing_snapshot_info_.get()); 418 DCHECK_EQ(snapshot_info, last_n_ongoing_snapshot_info_.get());
379 last_n_ongoing_snapshot_info_.reset(); 419 last_n_ongoing_snapshot_info_.reset();
380 return; 420 return;
381 } 421 }
382 422
383 DCHECK_EQ(snapshot_info, downloads_ongoing_snapshot_info_.get()); 423 DCHECK_EQ(snapshot_info, downloads_ongoing_snapshot_info_.get());
384 snapshot_controller_->PendingSnapshotCompleted(); 424 snapshot_controller_->PendingSnapshotCompleted();
385 // Tell RequestCoordinator how the request should be processed further. 425 // Tell RequestCoordinator how the request should be processed further.
386 ReportDownloadStatusToRequestCoordinator(snapshot_info, success); 426 ReportDownloadStatusToRequestCoordinator(snapshot_info, success);
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after
423 } 463 }
424 464
425 void RecentTabHelper::CancelInFlightSnapshots() { 465 void RecentTabHelper::CancelInFlightSnapshots() {
426 weak_ptr_factory_.InvalidateWeakPtrs(); 466 weak_ptr_factory_.InvalidateWeakPtrs();
427 downloads_ongoing_snapshot_info_.reset(); 467 downloads_ongoing_snapshot_info_.reset();
428 downloads_latest_saved_snapshot_info_.reset(); 468 downloads_latest_saved_snapshot_info_.reset();
429 last_n_ongoing_snapshot_info_.reset(); 469 last_n_ongoing_snapshot_info_.reset();
430 } 470 }
431 471
432 } // namespace offline_pages 472 } // namespace offline_pages
OLDNEW
« no previous file with comments | « no previous file | chrome/browser/android/offline_pages/recent_tab_helper_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698