background_snapshot_controller.cc 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. // Copyright 2018 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #include "components/offline_pages/core/background_snapshot_controller.h"
  5. #include "base/bind.h"
  6. #include "base/location.h"
  7. #include "base/time/time.h"
  8. #include "components/offline_pages/core/offline_page_feature.h"
  9. namespace {
  10. // Default delay, in milliseconds, between the main document OnLoad event and
  11. // snapshot.
  12. const int64_t kDelayAfterDocumentOnLoadCompletedMsBackground = 2000;
  13. // Default delay, in milliseconds, between renovations finishing and
  14. // taking a snapshot. Allows for page to update in response to the
  15. // renovations.
  16. const int64_t kDelayAfterRenovationsCompletedMs = 2000;
  17. // Delay for testing to keep polling times reasonable.
  18. const int64_t kDelayForTests = 0;
  19. } // namespace
  20. namespace offline_pages {
  21. BackgroundSnapshotController::BackgroundSnapshotController(
  22. const scoped_refptr<base::SingleThreadTaskRunner>& task_runner,
  23. BackgroundSnapshotController::Client* client,
  24. bool renovations_enabled)
  25. : task_runner_(task_runner),
  26. client_(client),
  27. state_(State::READY),
  28. delay_after_document_on_load_completed_ms_(
  29. kDelayAfterDocumentOnLoadCompletedMsBackground),
  30. delay_after_renovations_completed_ms_(kDelayAfterRenovationsCompletedMs) {
  31. DCHECK(!renovations_enabled);
  32. if (offline_pages::ShouldUseTestingSnapshotDelay()) {
  33. delay_after_document_on_load_completed_ms_ = kDelayForTests;
  34. delay_after_renovations_completed_ms_ = kDelayForTests;
  35. }
  36. }
  37. BackgroundSnapshotController::~BackgroundSnapshotController() {}
  38. void BackgroundSnapshotController::Reset() {
  39. // Cancel potentially delayed tasks that relate to the previous 'session'.
  40. weak_ptr_factory_.InvalidateWeakPtrs();
  41. state_ = State::READY;
  42. }
  43. void BackgroundSnapshotController::Stop() {
  44. state_ = State::STOPPED;
  45. }
  46. void BackgroundSnapshotController::RenovationsCompleted() {
  47. }
  48. void BackgroundSnapshotController::DocumentOnLoadCompletedInPrimaryMainFrame() {
  49. // Post a delayed task to snapshot and then stop this controller.
  50. task_runner_->PostDelayedTask(
  51. FROM_HERE,
  52. base::BindOnce(&BackgroundSnapshotController::MaybeStartSnapshotThenStop,
  53. weak_ptr_factory_.GetWeakPtr()),
  54. base::Milliseconds(delay_after_document_on_load_completed_ms_));
  55. }
  56. void BackgroundSnapshotController::MaybeStartSnapshot() {
  57. if (state_ != State::READY)
  58. return;
  59. state_ = State::SNAPSHOT_PENDING;
  60. client_->StartSnapshot();
  61. }
  62. void BackgroundSnapshotController::MaybeStartSnapshotThenStop() {
  63. MaybeStartSnapshot();
  64. Stop();
  65. }
  66. int64_t
  67. BackgroundSnapshotController::GetDelayAfterDocumentOnLoadCompletedForTest() {
  68. return delay_after_document_on_load_completed_ms_;
  69. }
  70. int64_t
  71. BackgroundSnapshotController::GetDelayAfterRenovationsCompletedForTest() {
  72. return delay_after_renovations_completed_ms_;
  73. }
  74. } // namespace offline_pages