browser_persister.h 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. // Copyright 2020 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 WEBLAYER_BROWSER_PERSISTENCE_BROWSER_PERSISTER_H_
  5. #define WEBLAYER_BROWSER_PERSISTENCE_BROWSER_PERSISTER_H_
  6. #include <stddef.h>
  7. #include <map>
  8. #include <memory>
  9. #include <string>
  10. #include <utility>
  11. #include <vector>
  12. #include "base/memory/raw_ptr.h"
  13. #include "base/memory/weak_ptr.h"
  14. #include "base/scoped_multi_source_observation.h"
  15. #include "components/sessions/content/session_tab_helper_delegate.h"
  16. #include "components/sessions/core/command_storage_manager_delegate.h"
  17. #include "components/sessions/core/session_service_commands.h"
  18. #include "weblayer/browser/tab_impl.h"
  19. #include "weblayer/public/browser_observer.h"
  20. class SessionID;
  21. namespace sessions {
  22. class SessionCommand;
  23. }
  24. namespace weblayer {
  25. class BrowserImpl;
  26. // BrowserPersister is responsible for maintaining the state of tabs in a
  27. // single Browser so that they can be restored at a later date. The state is
  28. // written to a file. To avoid having to write the complete state anytime
  29. // something changes interesting events (represented as SessionCommands) are
  30. // written to disk. To restore, the events are read back and the state
  31. // recreated. At certain times the file is truncated and rebuilt from the
  32. // current state.
  33. class BrowserPersister : public sessions::CommandStorageManagerDelegate,
  34. public sessions::SessionTabHelperDelegate,
  35. public BrowserObserver,
  36. public TabImpl::DataObserver {
  37. public:
  38. BrowserPersister(const base::FilePath& path,
  39. BrowserImpl* browser,
  40. const std::vector<uint8_t>& decryption_key);
  41. BrowserPersister(const BrowserPersister&) = delete;
  42. BrowserPersister& operator=(const BrowserPersister&) = delete;
  43. ~BrowserPersister() override;
  44. bool is_restore_in_progress() const { return is_restore_in_progress_; }
  45. void SaveIfNecessary();
  46. // Returns the key used to encrypt the file. Empty if not encrypted.
  47. // Encryption is done when saving and the profile is off the record.
  48. const std::vector<uint8_t>& GetCryptoKey() const;
  49. private:
  50. friend class BrowserPersisterTestHelper;
  51. using IdToRange = std::map<SessionID, std::pair<int, int>>;
  52. // CommandStorageManagerDelegate:
  53. bool ShouldUseDelayedSave() override;
  54. void OnWillSaveCommands() override;
  55. void OnGeneratedNewCryptoKey(const std::vector<uint8_t>& key) override;
  56. void OnErrorWritingSessionCommands() override;
  57. // BrowserObserver;
  58. void OnTabAdded(Tab* tab) override;
  59. void OnTabRemoved(Tab* tab, bool active_tab_changed) override;
  60. void OnActiveTabChanged(Tab* tab) override;
  61. // TabImpl::DataObserver:
  62. void OnDataChanged(TabImpl* tab,
  63. const std::map<std::string, std::string>& data) override;
  64. // sessions::SessionTabHelperDelegate:
  65. void SetTabUserAgentOverride(const SessionID& window_id,
  66. const SessionID& tab_id,
  67. const sessions::SerializedUserAgentOverride&
  68. user_agent_override) override;
  69. void SetSelectedNavigationIndex(const SessionID& window_id,
  70. const SessionID& tab_id,
  71. int index) override;
  72. void UpdateTabNavigation(
  73. const SessionID& window_id,
  74. const SessionID& tab_id,
  75. const sessions::SerializedNavigationEntry& navigation) override;
  76. void TabNavigationPathPruned(const SessionID& window_id,
  77. const SessionID& tab_id,
  78. int index,
  79. int count) override;
  80. void TabNavigationPathEntriesDeleted(const SessionID& window_id,
  81. const SessionID& tab_id) override;
  82. // Schedules recreating the file on the next save.
  83. void ScheduleRebuildOnNextSave();
  84. // Called with the contents of the previous session.
  85. void OnGotLastSessionCommands(
  86. std::vector<std::unique_ptr<sessions::SessionCommand>> commands,
  87. bool read_error);
  88. // Schedules commands to recreate the state of the specified tab.
  89. void BuildCommandsForTab(TabImpl* tab, int index_in_window);
  90. // Schedules commands to recreate the state of |browser_|.
  91. void BuildCommandsForBrowser();
  92. // Schedules the specified command.
  93. void ScheduleCommand(std::unique_ptr<sessions::SessionCommand> command);
  94. void ProcessRestoreCommands(
  95. const std::vector<std::unique_ptr<sessions::SessionWindow>>& windows);
  96. raw_ptr<BrowserImpl> browser_;
  97. // ID used for the browser. The sessions code requires each tab to be
  98. // associated with a browser.
  99. const SessionID browser_session_id_;
  100. std::unique_ptr<sessions::CommandStorageManager> command_storage_manager_;
  101. // Maps from session tab id to the range of navigation entries that has
  102. // been written to disk.
  103. IdToRange tab_to_available_range_;
  104. // Force session commands to be rebuild before next save event.
  105. bool rebuild_on_next_save_;
  106. std::vector<uint8_t> crypto_key_;
  107. base::ScopedMultiSourceObservation<TabImpl,
  108. TabImpl::DataObserver,
  109. &TabImpl::AddDataObserver,
  110. &TabImpl::RemoveDataObserver>
  111. data_observations_{this};
  112. // True while asynchronously reading the state to restore.
  113. bool is_restore_in_progress_ = true;
  114. base::WeakPtrFactory<BrowserPersister> weak_factory_{this};
  115. };
  116. } // namespace weblayer
  117. #endif // WEBLAYER_BROWSER_PERSISTENCE_BROWSER_PERSISTER_H_