background_snapshot_controller.h 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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. #ifndef COMPONENTS_OFFLINE_PAGES_CORE_BACKGROUND_SNAPSHOT_CONTROLLER_H_
  5. #define COMPONENTS_OFFLINE_PAGES_CORE_BACKGROUND_SNAPSHOT_CONTROLLER_H_
  6. #include "base/memory/raw_ptr.h"
  7. #include "base/memory/ref_counted.h"
  8. #include "base/memory/weak_ptr.h"
  9. #include "base/task/single_thread_task_runner.h"
  10. namespace offline_pages {
  11. // Takes various signals and produces StartSnapshot calls following a specific
  12. // policy.
  13. // Main invariants:
  14. // - Some signals prevent more snapshots to be taken.
  15. // OnLoad is currently such signal.
  16. // - Once Reset() is called on the BackgroundSnapshotController, the delayed
  17. // tasks are reset so no StartSnapshot calls is made 'cross-session'.
  18. class BackgroundSnapshotController {
  19. public:
  20. enum class State {
  21. READY, // Listening to input, will start snapshot when needed.
  22. SNAPSHOT_PENDING, // Snapshot is in progress, don't start another.
  23. STOPPED, // Terminal state, no snapshots until reset.
  24. };
  25. // The expected quality of a page based on its current loading progress.
  26. enum class PageQuality {
  27. // Not loaded enough to reach a minimum level of quality. Snapshots taken at
  28. // this point are expected to be useless.
  29. POOR = 0,
  30. // A minimal level of quality has been attained but the page is still
  31. // loading so its quality is continuously increasing. One or more snapshots
  32. // could be taken at this stage as later ones are expected to have higher
  33. // quality.
  34. FAIR_AND_IMPROVING,
  35. // The page is loaded enough and has attained its peak expected quality.
  36. // Snapshots taken at this point are not expected to increase in quality
  37. // after the first one.
  38. HIGH,
  39. };
  40. // Client of the BackgroundSnapshotController.
  41. class Client {
  42. public:
  43. // Invoked at a good moment to start a snapshot.
  44. virtual void StartSnapshot() = 0;
  45. protected:
  46. virtual ~Client() {}
  47. };
  48. BackgroundSnapshotController(
  49. const scoped_refptr<base::SingleThreadTaskRunner>& task_runner,
  50. BackgroundSnapshotController::Client* client,
  51. bool renovations_enabled);
  52. BackgroundSnapshotController(const BackgroundSnapshotController&) = delete;
  53. BackgroundSnapshotController& operator=(const BackgroundSnapshotController&) =
  54. delete;
  55. virtual ~BackgroundSnapshotController();
  56. // Resets the 'session', returning controller to initial state.
  57. void Reset();
  58. // Stops current session, no more Client::StartSnapshot calls will be
  59. // invoked from the BackgroundSnapshotController until current session is
  60. // Reset(). Called by Client, for example when it encounters an error loading
  61. // the page.
  62. void Stop();
  63. // The Client calls this when renovations have completed.
  64. void RenovationsCompleted();
  65. // Invoked from WebContentObserver::DocumentOnLoadCompletedInPrimaryMainFrame
  66. void DocumentOnLoadCompletedInPrimaryMainFrame();
  67. int64_t GetDelayAfterDocumentOnLoadCompletedForTest();
  68. int64_t GetDelayAfterRenovationsCompletedForTest();
  69. private:
  70. void MaybeStartSnapshot();
  71. void MaybeStartSnapshotThenStop();
  72. scoped_refptr<base::SingleThreadTaskRunner> task_runner_;
  73. // Client owns this class.
  74. raw_ptr<BackgroundSnapshotController::Client> client_;
  75. BackgroundSnapshotController::State state_;
  76. int64_t delay_after_document_on_load_completed_ms_;
  77. int64_t delay_after_renovations_completed_ms_;
  78. base::WeakPtrFactory<BackgroundSnapshotController> weak_ptr_factory_{this};
  79. };
  80. } // namespace offline_pages
  81. #endif // COMPONENTS_OFFLINE_PAGES_CORE_BACKGROUND_SNAPSHOT_CONTROLLER_H_