| OLD | NEW |
| 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 252 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 263 | 263 |
| 264 // 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 |
| 265 // 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 |
| 266 // the close). We should detect that and avoid the saving. | 266 // the close). We should detect that and avoid the saving. |
| 267 void RecentTabHelper::WasHidden() { | 267 void RecentTabHelper::WasHidden() { |
| 268 if (!IsOffliningRecentPagesEnabled()) | 268 if (!IsOffliningRecentPagesEnabled()) |
| 269 return; | 269 return; |
| 270 | 270 |
| 271 // Return immediately if last_n is not listening to tab hidden events, if a | 271 // Return immediately if last_n is not listening to tab hidden events, if a |
| 272 // last_n snapshot is currently being saved or if the tab is closing. | 272 // last_n snapshot is currently being saved or if the tab is closing. |
| 273 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 tab_is_closing_) { |
| 274 DVLOG(1) << "Will not snapshot for last_n (reasons: " | 275 DVLOG(1) << "Will not snapshot for last_n (reasons: " |
| 275 << !last_n_listen_to_tab_hidden_ << ", " | 276 << !last_n_listen_to_tab_hidden_ << ", " |
| 276 << !!last_n_ongoing_snapshot_info_ | 277 << !!last_n_ongoing_snapshot_info_ << ", " << tab_is_closing_ |
| 277 << ") for: " << web_contents()->GetLastCommittedURL().spec(); | 278 << ") for: " << web_contents()->GetLastCommittedURL().spec(); |
| 278 return; | 279 return; |
| 279 } | 280 } |
| 280 | 281 |
| 281 // Do not save if page quality is too low. | 282 // Do not save if page quality is too low. |
| 282 // Note: we assume page quality for a page can only increase. | 283 // Note: we assume page quality for a page can only increase. |
| 283 if (snapshot_controller_->current_page_quality() == PageQuality::POOR) { | 284 if (snapshot_controller_->current_page_quality() == PageQuality::POOR) { |
| 284 DVLOG(1) << "Will not snapshot for last_n (page quality too low) for: " | 285 DVLOG(1) << "Will not snapshot for last_n (page quality too low) for: " |
| 285 << web_contents()->GetLastCommittedURL().spec(); | 286 << web_contents()->GetLastCommittedURL().spec(); |
| 286 return; | 287 return; |
| 287 } | 288 } |
| 288 | 289 |
| 289 DVLOG(1) << "Starting last_n snapshot for: " | 290 DVLOG(1) << "Starting last_n snapshot for: " |
| 290 << web_contents()->GetLastCommittedURL().spec(); | 291 << web_contents()->GetLastCommittedURL().spec(); |
| 291 last_n_ongoing_snapshot_info_ = | 292 last_n_ongoing_snapshot_info_ = |
| 292 base::MakeUnique<SnapshotProgressInfo>(GetRecentPagesClientId()); | 293 base::MakeUnique<SnapshotProgressInfo>(GetRecentPagesClientId()); |
| 293 DCHECK(last_n_ongoing_snapshot_info_->IsForLastN()); | 294 DCHECK(last_n_ongoing_snapshot_info_->IsForLastN()); |
| 294 DCHECK(snapshots_enabled_); | 295 DCHECK(snapshots_enabled_); |
| 295 // Remove previously captured pages for this tab. | 296 // Remove previously captured pages for this tab. |
| 296 page_model_->GetOfflineIdsForClientId( | 297 page_model_->GetOfflineIdsForClientId( |
| 297 GetRecentPagesClientId(), | 298 GetRecentPagesClientId(), |
| 298 base::Bind(&RecentTabHelper::ContinueSnapshotWithIdsToPurge, | 299 base::Bind(&RecentTabHelper::ContinueSnapshotWithIdsToPurge, |
| 299 weak_ptr_factory_.GetWeakPtr(), | 300 weak_ptr_factory_.GetWeakPtr(), |
| 300 last_n_ongoing_snapshot_info_.get())); | 301 last_n_ongoing_snapshot_info_.get())); |
| 301 } | 302 } |
| 302 | 303 |
| 304 void RecentTabHelper::WasShown() { |
| 305 // If the tab was closing and is now being shown, the closure was reverted. |
| 306 DVLOG_IF(0, tab_is_closing_) << "Tab is not closing anymore: " |
| 307 << web_contents()->GetLastCommittedURL().spec(); |
| 308 tab_is_closing_ = false; |
| 309 } |
| 310 |
| 311 void RecentTabHelper::WillCloseTab() { |
| 312 DVLOG(1) << "Tab is now closing: " |
| 313 << web_contents()->GetLastCommittedURL().spec(); |
| 314 tab_is_closing_ = true; |
| 315 } |
| 316 |
| 303 // TODO(carlosk): rename this to RequestSnapshot and make it return a bool | 317 // TODO(carlosk): rename this to RequestSnapshot and make it return a bool |
| 304 // representing the acceptance of the snapshot request. | 318 // representing the acceptance of the snapshot request. |
| 305 void RecentTabHelper::StartSnapshot() { | 319 void RecentTabHelper::StartSnapshot() { |
| 306 DCHECK_NE(PageQuality::POOR, snapshot_controller_->current_page_quality()); | 320 DCHECK_NE(PageQuality::POOR, snapshot_controller_->current_page_quality()); |
| 307 | 321 |
| 308 // As long as snapshots are enabled for this tab, there are two situations | 322 // As long as snapshots are enabled for this tab, there are two situations |
| 309 // that allow for a navigation event to start a snapshot: | 323 // that allow for a navigation event to start a snapshot: |
| 310 // 1) There is a request on hold waiting for the page to be minimally loaded. | 324 // 1) There is a request on hold waiting for the page to be minimally loaded. |
| 311 if (snapshots_enabled_ && downloads_snapshot_on_hold_) { | 325 if (snapshots_enabled_ && downloads_snapshot_on_hold_) { |
| 312 DVLOG(1) << "Resuming downloads snapshot request for: " | 326 DVLOG(1) << "Resuming downloads snapshot request for: " |
| (...skipping 152 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 465 } | 479 } |
| 466 | 480 |
| 467 void RecentTabHelper::CancelInFlightSnapshots() { | 481 void RecentTabHelper::CancelInFlightSnapshots() { |
| 468 weak_ptr_factory_.InvalidateWeakPtrs(); | 482 weak_ptr_factory_.InvalidateWeakPtrs(); |
| 469 downloads_ongoing_snapshot_info_.reset(); | 483 downloads_ongoing_snapshot_info_.reset(); |
| 470 downloads_latest_saved_snapshot_info_.reset(); | 484 downloads_latest_saved_snapshot_info_.reset(); |
| 471 last_n_ongoing_snapshot_info_.reset(); | 485 last_n_ongoing_snapshot_info_.reset(); |
| 472 } | 486 } |
| 473 | 487 |
| 474 } // namespace offline_pages | 488 } // namespace offline_pages |
| OLD | NEW |