| 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 166 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 177 page_model_ = OfflinePageModelFactory::GetForBrowserContext( | 177 page_model_ = OfflinePageModelFactory::GetForBrowserContext( |
| 178 web_contents()->GetBrowserContext()); | 178 web_contents()->GetBrowserContext()); |
| 179 } | 179 } |
| 180 | 180 |
| 181 return snapshots_enabled_; | 181 return snapshots_enabled_; |
| 182 } | 182 } |
| 183 | 183 |
| 184 void RecentTabHelper::DidFinishNavigation( | 184 void RecentTabHelper::DidFinishNavigation( |
| 185 content::NavigationHandle* navigation_handle) { | 185 content::NavigationHandle* navigation_handle) { |
| 186 if (!navigation_handle->IsInMainFrame() || | 186 if (!navigation_handle->IsInMainFrame() || |
| 187 !navigation_handle->HasCommitted()) { | 187 !navigation_handle->HasCommitted() || navigation_handle->IsSamePage()) { |
| 188 return; | 188 return; |
| 189 } | 189 } |
| 190 | 190 |
| 191 if (!EnsureInitialized()) | 191 if (!EnsureInitialized()) |
| 192 return; | 192 return; |
| 193 | 193 |
| 194 // If there is an ongoing downloads request, lets allow Background Offliner to | 194 // If there is an ongoing downloads request, lets allow Background Offliner to |
| 195 // continue downloading this page. | 195 // continue downloading this page. |
| 196 if (downloads_ongoing_snapshot_info_) { | 196 if (downloads_ongoing_snapshot_info_) { |
| 197 ReportDownloadStatusToRequestCoordinator( | 197 ReportDownloadStatusToRequestCoordinator( |
| 198 downloads_ongoing_snapshot_info_.get(), false); | 198 downloads_ongoing_snapshot_info_.get(), false); |
| 199 } | 199 } |
| 200 | 200 |
| 201 // Cancel any and all in flight snapshot tasks from the previous page. | 201 // Cancel any and all in flight snapshot tasks from the previous page. |
| 202 CancelInFlightSnapshots(); | 202 CancelInFlightSnapshots(); |
| 203 downloads_snapshot_on_hold_ = false; | 203 downloads_snapshot_on_hold_ = false; |
| 204 | 204 |
| 205 // New navigation, new snapshot session. | |
| 206 snapshot_url_ = web_contents()->GetLastCommittedURL(); | |
| 207 | |
| 208 // Always reset so that posted tasks get canceled. | 205 // Always reset so that posted tasks get canceled. |
| 209 snapshot_controller_->Reset(); | 206 snapshot_controller_->Reset(); |
| 210 | 207 |
| 211 // Check for conditions that would cause us not to snapshot. | 208 // Check for conditions that would cause us not to snapshot. |
| 212 bool can_save = !navigation_handle->IsErrorPage() && | 209 bool can_save = |
| 213 OfflinePageModel::CanSaveURL(snapshot_url_) && | 210 !navigation_handle->IsErrorPage() && |
| 214 OfflinePageUtils::GetOfflinePageFromWebContents( | 211 OfflinePageModel::CanSaveURL(web_contents()->GetLastCommittedURL()) && |
| 215 web_contents()) == nullptr; | 212 OfflinePageUtils::GetOfflinePageFromWebContents(web_contents()) == |
| 213 nullptr; |
| 216 | 214 |
| 217 UMA_HISTOGRAM_BOOLEAN("OfflinePages.CanSaveRecentPage", can_save); | 215 UMA_HISTOGRAM_BOOLEAN("OfflinePages.CanSaveRecentPage", can_save); |
| 218 | 216 |
| 219 if (!can_save) | 217 if (!can_save) |
| 220 snapshot_controller_->Stop(); | 218 snapshot_controller_->Stop(); |
| 221 last_n_listen_to_tab_hidden_ = can_save && !delegate_->IsLowEndDevice() && | 219 last_n_listen_to_tab_hidden_ = can_save && !delegate_->IsLowEndDevice() && |
| 222 IsOffliningRecentPagesEnabled(); | 220 IsOffliningRecentPagesEnabled(); |
| 223 last_n_latest_saved_quality_ = PageQuality::POOR; | |
| 224 } | 221 } |
| 225 | 222 |
| 226 void RecentTabHelper::DocumentAvailableInMainFrame() { | 223 void RecentTabHelper::DocumentAvailableInMainFrame() { |
| 227 EnsureInitialized(); | 224 EnsureInitialized(); |
| 228 snapshot_controller_->DocumentAvailableInMainFrame(); | 225 snapshot_controller_->DocumentAvailableInMainFrame(); |
| 229 } | 226 } |
| 230 | 227 |
| 231 void RecentTabHelper::DocumentOnLoadCompletedInMainFrame() { | 228 void RecentTabHelper::DocumentOnLoadCompletedInMainFrame() { |
| 232 EnsureInitialized(); | 229 EnsureInitialized(); |
| 233 snapshot_controller_->DocumentOnLoadCompletedInMainFrame(); | 230 snapshot_controller_->DocumentOnLoadCompletedInMainFrame(); |
| (...skipping 14 matching lines...) Expand all Loading... |
| 248 // the close). We should detect that and avoid the saving. | 245 // the close). We should detect that and avoid the saving. |
| 249 void RecentTabHelper::WasHidden() { | 246 void RecentTabHelper::WasHidden() { |
| 250 if (!IsOffliningRecentPagesEnabled()) | 247 if (!IsOffliningRecentPagesEnabled()) |
| 251 return; | 248 return; |
| 252 | 249 |
| 253 // Return immediately if last_n is not listening to tab hidden events or if a | 250 // Return immediately if last_n is not listening to tab hidden events or if a |
| 254 // last_n snapshot is currently being saved. | 251 // last_n snapshot is currently being saved. |
| 255 if (!last_n_listen_to_tab_hidden_ || last_n_ongoing_snapshot_info_) | 252 if (!last_n_listen_to_tab_hidden_ || last_n_ongoing_snapshot_info_) |
| 256 return; | 253 return; |
| 257 | 254 |
| 258 // Do not save if page quality is too low or if we already have a snapshot | 255 // Do not save if page quality is too low. |
| 259 // with the current quality level. | |
| 260 // Note: we assume page quality for a page can only increase. | 256 // Note: we assume page quality for a page can only increase. |
| 261 PageQuality current_quality = snapshot_controller_->current_page_quality(); | 257 if (snapshot_controller_->current_page_quality() == PageQuality::POOR) |
| 262 if (current_quality == PageQuality::POOR || | |
| 263 current_quality == last_n_latest_saved_quality_) { | |
| 264 return; | 258 return; |
| 265 } | |
| 266 | 259 |
| 267 last_n_ongoing_snapshot_info_ = | 260 last_n_ongoing_snapshot_info_ = |
| 268 base::MakeUnique<SnapshotProgressInfo>(GetRecentPagesClientId()); | 261 base::MakeUnique<SnapshotProgressInfo>(GetRecentPagesClientId()); |
| 269 DCHECK(last_n_ongoing_snapshot_info_->IsForLastN()); | 262 DCHECK(last_n_ongoing_snapshot_info_->IsForLastN()); |
| 270 DCHECK(snapshots_enabled_); | 263 DCHECK(snapshots_enabled_); |
| 271 // Remove previously captured pages for this tab. | 264 // Remove previously captured pages for this tab. |
| 272 page_model_->GetOfflineIdsForClientId( | 265 page_model_->GetOfflineIdsForClientId( |
| 273 GetRecentPagesClientId(), | 266 GetRecentPagesClientId(), |
| 274 base::Bind(&RecentTabHelper::ContinueSnapshotWithIdsToPurge, | 267 base::Bind(&RecentTabHelper::ContinueSnapshotWithIdsToPurge, |
| 275 weak_ptr_factory_.GetWeakPtr(), | 268 weak_ptr_factory_.GetWeakPtr(), |
| (...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 342 DCHECK(snapshot_info); | 335 DCHECK(snapshot_info); |
| 343 | 336 |
| 344 page_model_->DeletePagesByOfflineId( | 337 page_model_->DeletePagesByOfflineId( |
| 345 page_ids, base::Bind(&RecentTabHelper::ContinueSnapshotAfterPurge, | 338 page_ids, base::Bind(&RecentTabHelper::ContinueSnapshotAfterPurge, |
| 346 weak_ptr_factory_.GetWeakPtr(), snapshot_info)); | 339 weak_ptr_factory_.GetWeakPtr(), snapshot_info)); |
| 347 } | 340 } |
| 348 | 341 |
| 349 void RecentTabHelper::ContinueSnapshotAfterPurge( | 342 void RecentTabHelper::ContinueSnapshotAfterPurge( |
| 350 SnapshotProgressInfo* snapshot_info, | 343 SnapshotProgressInfo* snapshot_info, |
| 351 OfflinePageModel::DeletePageResult result) { | 344 OfflinePageModel::DeletePageResult result) { |
| 352 DCHECK_EQ(snapshot_url_, web_contents()->GetLastCommittedURL()); | |
| 353 if (result != OfflinePageModel::DeletePageResult::SUCCESS) { | 345 if (result != OfflinePageModel::DeletePageResult::SUCCESS) { |
| 354 ReportSnapshotCompleted(snapshot_info, false); | 346 ReportSnapshotCompleted(snapshot_info, false); |
| 355 return; | 347 return; |
| 356 } | 348 } |
| 357 | 349 |
| 350 DCHECK(OfflinePageModel::CanSaveURL(web_contents()->GetLastCommittedURL())); |
| 358 snapshot_info->expected_page_quality = | 351 snapshot_info->expected_page_quality = |
| 359 snapshot_controller_->current_page_quality(); | 352 snapshot_controller_->current_page_quality(); |
| 360 OfflinePageModel::SavePageParams save_page_params; | 353 OfflinePageModel::SavePageParams save_page_params; |
| 361 save_page_params.url = snapshot_url_; | 354 save_page_params.url = web_contents()->GetLastCommittedURL(); |
| 362 save_page_params.client_id = snapshot_info->client_id; | 355 save_page_params.client_id = snapshot_info->client_id; |
| 363 save_page_params.proposed_offline_id = snapshot_info->request_id; | 356 save_page_params.proposed_offline_id = snapshot_info->request_id; |
| 364 save_page_params.is_background = false; | 357 save_page_params.is_background = false; |
| 365 page_model_->SavePage( | 358 page_model_->SavePage( |
| 366 save_page_params, delegate_->CreatePageArchiver(web_contents()), | 359 save_page_params, delegate_->CreatePageArchiver(web_contents()), |
| 367 base::Bind(&RecentTabHelper::SavePageCallback, | 360 base::Bind(&RecentTabHelper::SavePageCallback, |
| 368 weak_ptr_factory_.GetWeakPtr(), snapshot_info)); | 361 weak_ptr_factory_.GetWeakPtr(), snapshot_info)); |
| 369 } | 362 } |
| 370 | 363 |
| 371 void RecentTabHelper::SavePageCallback(SnapshotProgressInfo* snapshot_info, | 364 void RecentTabHelper::SavePageCallback(SnapshotProgressInfo* snapshot_info, |
| 372 OfflinePageModel::SavePageResult result, | 365 OfflinePageModel::SavePageResult result, |
| 373 int64_t offline_id) { | 366 int64_t offline_id) { |
| 374 DCHECK(snapshot_info->IsForLastN() || | 367 DCHECK(snapshot_info->IsForLastN() || |
| 375 snapshot_info->request_id == offline_id); | 368 snapshot_info->request_id == offline_id); |
| 376 ReportSnapshotCompleted(snapshot_info, result == SavePageResult::SUCCESS); | 369 ReportSnapshotCompleted(snapshot_info, result == SavePageResult::SUCCESS); |
| 377 } | 370 } |
| 378 | 371 |
| 379 // Note: this is the final step in the chain of callbacks and it's where the | 372 // Note: this is the final step in the chain of callbacks and it's where the |
| 380 // behavior is different depending on this being a last_n or downloads snapshot. | 373 // behavior is different depending on this being a last_n or downloads snapshot. |
| 381 void RecentTabHelper::ReportSnapshotCompleted( | 374 void RecentTabHelper::ReportSnapshotCompleted( |
| 382 SnapshotProgressInfo* snapshot_info, | 375 SnapshotProgressInfo* snapshot_info, |
| 383 bool success) { | 376 bool success) { |
| 384 if (snapshot_info->IsForLastN()) { | 377 if (snapshot_info->IsForLastN()) { |
| 385 DCHECK_EQ(snapshot_info, last_n_ongoing_snapshot_info_.get()); | 378 DCHECK_EQ(snapshot_info, last_n_ongoing_snapshot_info_.get()); |
| 386 if (success) | |
| 387 last_n_latest_saved_quality_ = snapshot_info->expected_page_quality; | |
| 388 last_n_ongoing_snapshot_info_.reset(); | 379 last_n_ongoing_snapshot_info_.reset(); |
| 389 return; | 380 return; |
| 390 } | 381 } |
| 391 | 382 |
| 392 DCHECK_EQ(snapshot_info, downloads_ongoing_snapshot_info_.get()); | 383 DCHECK_EQ(snapshot_info, downloads_ongoing_snapshot_info_.get()); |
| 393 snapshot_controller_->PendingSnapshotCompleted(); | 384 snapshot_controller_->PendingSnapshotCompleted(); |
| 394 // Tell RequestCoordinator how the request should be processed further. | 385 // Tell RequestCoordinator how the request should be processed further. |
| 395 ReportDownloadStatusToRequestCoordinator(snapshot_info, success); | 386 ReportDownloadStatusToRequestCoordinator(snapshot_info, success); |
| 396 if (success) { | 387 if (success) { |
| 397 downloads_latest_saved_snapshot_info_ = | 388 downloads_latest_saved_snapshot_info_ = |
| (...skipping 14 matching lines...) Expand all Loading... |
| 412 | 403 |
| 413 RequestCoordinator* request_coordinator = | 404 RequestCoordinator* request_coordinator = |
| 414 RequestCoordinatorFactory::GetForBrowserContext( | 405 RequestCoordinatorFactory::GetForBrowserContext( |
| 415 web_contents()->GetBrowserContext()); | 406 web_contents()->GetBrowserContext()); |
| 416 if (!request_coordinator) | 407 if (!request_coordinator) |
| 417 return; | 408 return; |
| 418 | 409 |
| 419 // It is OK to call these methods more then once, depending on | 410 // It is OK to call these methods more then once, depending on |
| 420 // number of snapshots attempted in this tab helper. If the request_id is not | 411 // number of snapshots attempted in this tab helper. If the request_id is not |
| 421 // in the list of RequestCoordinator, these calls have no effect. | 412 // in the list of RequestCoordinator, these calls have no effect. |
| 422 if (cancel_background_request) | 413 if (cancel_background_request) { |
| 423 request_coordinator->MarkRequestCompleted(snapshot_info->request_id); | 414 request_coordinator->MarkRequestCompleted(snapshot_info->request_id); |
| 424 else | 415 } else { |
| 425 request_coordinator->EnableForOffliner(snapshot_info->request_id, | 416 request_coordinator->EnableForOffliner(snapshot_info->request_id, |
| 426 snapshot_info->client_id); | 417 snapshot_info->client_id); |
| 418 } |
| 427 } | 419 } |
| 428 | 420 |
| 429 ClientId RecentTabHelper::GetRecentPagesClientId() const { | 421 ClientId RecentTabHelper::GetRecentPagesClientId() const { |
| 430 return ClientId(kLastNNamespace, tab_id_); | 422 return ClientId(kLastNNamespace, tab_id_); |
| 431 } | 423 } |
| 432 | 424 |
| 433 void RecentTabHelper::CancelInFlightSnapshots() { | 425 void RecentTabHelper::CancelInFlightSnapshots() { |
| 434 weak_ptr_factory_.InvalidateWeakPtrs(); | 426 weak_ptr_factory_.InvalidateWeakPtrs(); |
| 435 downloads_ongoing_snapshot_info_.reset(); | 427 downloads_ongoing_snapshot_info_.reset(); |
| 436 downloads_latest_saved_snapshot_info_.reset(); | 428 downloads_latest_saved_snapshot_info_.reset(); |
| 437 last_n_ongoing_snapshot_info_.reset(); | 429 last_n_ongoing_snapshot_info_.reset(); |
| 438 } | 430 } |
| 439 | 431 |
| 440 } // namespace offline_pages | 432 } // namespace offline_pages |
| OLD | NEW |