session_store.h 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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_SYNC_SESSIONS_SESSION_STORE_H_
  5. #define COMPONENTS_SYNC_SESSIONS_SESSION_STORE_H_
  6. #include <map>
  7. #include <memory>
  8. #include <string>
  9. #include <vector>
  10. #include "base/callback_forward.h"
  11. #include "base/memory/raw_ptr.h"
  12. #include "base/memory/weak_ptr.h"
  13. #include "base/time/time.h"
  14. #include "components/sync/model/data_batch.h"
  15. #include "components/sync/model/metadata_batch.h"
  16. #include "components/sync/model/model_error.h"
  17. #include "components/sync/model/model_type_store.h"
  18. #include "components/sync_sessions/synced_session_tracker.h"
  19. #include "third_party/abseil-cpp/absl/types/optional.h"
  20. namespace sync_sessions {
  21. // Class responsible for maintaining an in-memory representation of sync
  22. // sessions (by owning a SyncedSessionTracker) with the capability to persist
  23. // state to disk and restore (data and metadata). The API enforces a valid and
  24. // consistent state of the model, e.g. by making sure there is at most one sync
  25. // entity per client tag.
  26. class SessionStore {
  27. public:
  28. struct SessionInfo {
  29. std::string session_tag;
  30. std::string client_name;
  31. sync_pb::SyncEnums::DeviceType device_type = sync_pb::SyncEnums::TYPE_UNSET;
  32. };
  33. using OpenCallback = base::OnceCallback<void(
  34. const absl::optional<syncer::ModelError>& error,
  35. std::unique_ptr<SessionStore> store,
  36. std::unique_ptr<syncer::MetadataBatch> metadata_batch)>;
  37. // Opens a SessionStore instance, which involves IO to load previous state
  38. // from disk. |sessions_client| must not be null and must outlive the
  39. // SessionStore instance returned via |callback|, or until the callback is
  40. // cancelled.
  41. static void Open(const std::string& cache_guid,
  42. SyncSessionsClient* sessions_client,
  43. OpenCallback callback);
  44. // Verifies whether a proto is malformed (e.g. required fields are missing).
  45. static bool AreValidSpecifics(const sync_pb::SessionSpecifics& specifics);
  46. // |specifics| must be valid, see AreValidSpecifics().
  47. static std::string GetClientTag(const sync_pb::SessionSpecifics& specifics);
  48. // |specifics| must be valid, see AreValidSpecifics().
  49. static std::string GetStorageKey(const sync_pb::SessionSpecifics& specifics);
  50. static std::string GetHeaderStorageKey(const std::string& session_tag);
  51. static std::string GetTabStorageKey(const std::string& session_tag,
  52. int tab_node_id);
  53. // Verifies if |storage_key| corresponds to an entity in the local session,
  54. // identified by the session tag.
  55. bool StorageKeyMatchesLocalSession(const std::string& storage_key) const;
  56. // Various equivalents for testing.
  57. static std::string GetTabClientTagForTest(const std::string& session_tag,
  58. int tab_node_id);
  59. // Similar to ModelTypeStore::WriteBatch but enforces a consistent state. In
  60. // the current implementation, some functions do *NOT* update the tracker, so
  61. // callers are responsible for doing so.
  62. // TODO(crbug.com/681921): Enforce consistency between in-memory and persisted
  63. // data by always updating the tracker.
  64. class WriteBatch {
  65. public:
  66. // Callback that mimics the signature of ModelTypeStore::CommitWriteBatch().
  67. using CommitCallback = base::OnceCallback<void(
  68. std::unique_ptr<syncer::ModelTypeStore::WriteBatch>,
  69. syncer::ModelTypeStore::CallbackWithResult)>;
  70. // Raw pointers must not be nullptr and must outlive this object.
  71. WriteBatch(std::unique_ptr<syncer::ModelTypeStore::WriteBatch> batch,
  72. CommitCallback commit_cb,
  73. syncer::OnceModelErrorHandler error_handler,
  74. SyncedSessionTracker* session_tracker);
  75. WriteBatch(const WriteBatch&) = delete;
  76. WriteBatch& operator=(const WriteBatch&) = delete;
  77. ~WriteBatch();
  78. // Most mutations below return storage keys.
  79. std::string PutAndUpdateTracker(const sync_pb::SessionSpecifics& specifics,
  80. base::Time modification_time);
  81. // Returns all deleted storage keys, which may be more than one if
  82. // |storage_key| refers to a header entity.
  83. std::vector<std::string> DeleteForeignEntityAndUpdateTracker(
  84. const std::string& storage_key);
  85. // The functions below do not update SyncedSessionTracker and hence it is
  86. // the caller's responsibility to do so *before* calling these functions.
  87. std::string PutWithoutUpdatingTracker(
  88. const sync_pb::SessionSpecifics& specifics);
  89. std::string DeleteLocalTabWithoutUpdatingTracker(int tab_node_id);
  90. syncer::MetadataChangeList* GetMetadataChangeList();
  91. static void Commit(std::unique_ptr<WriteBatch> batch);
  92. private:
  93. std::unique_ptr<syncer::ModelTypeStore::WriteBatch> batch_;
  94. CommitCallback commit_cb_;
  95. syncer::OnceModelErrorHandler error_handler_;
  96. const raw_ptr<SyncedSessionTracker> session_tracker_;
  97. };
  98. SessionStore(const SessionStore&) = delete;
  99. SessionStore& operator=(const SessionStore&) = delete;
  100. ~SessionStore();
  101. const SessionInfo& local_session_info() const { return local_session_info_; }
  102. // Converts the in-memory model (SyncedSessionTracker) of sessions to sync
  103. // protos.
  104. std::unique_ptr<syncer::DataBatch> GetSessionDataForKeys(
  105. const std::vector<std::string>& storage_keys) const;
  106. // Returns all known session entities, local and foreign, generated from the
  107. // in-memory model (SyncedSessionTracker).
  108. std::unique_ptr<syncer::DataBatch> GetAllSessionData() const;
  109. // Write API. WriteBatch instances must not outlive this store and must be
  110. // committed prior to destruction. Besides, more than one uncommitted
  111. // instance must not exist at any time.
  112. std::unique_ptr<WriteBatch> CreateWriteBatch(
  113. syncer::OnceModelErrorHandler error_handler);
  114. void DeleteAllDataAndMetadata();
  115. // TODO(crbug.com/681921): Avoid exposing a mutable tracker, because that
  116. // bypasses the consistency-enforcing API.
  117. SyncedSessionTracker* mutable_tracker() { return &session_tracker_; }
  118. const SyncedSessionTracker* tracker() const { return &session_tracker_; }
  119. private:
  120. // Helper class used to collect all parameters needed by the constructor.
  121. struct Builder;
  122. static void OnStoreCreated(
  123. std::unique_ptr<Builder> builder,
  124. const absl::optional<syncer::ModelError>& error,
  125. std::unique_ptr<syncer::ModelTypeStore> underlying_store);
  126. static void OnReadAllMetadata(
  127. std::unique_ptr<Builder> builder,
  128. const absl::optional<syncer::ModelError>& error,
  129. std::unique_ptr<syncer::MetadataBatch> metadata_batch);
  130. static void OnReadAllData(std::unique_ptr<Builder> builder,
  131. const absl::optional<syncer::ModelError>& error);
  132. // |sessions_client| must not be null and must outlive this object.
  133. SessionStore(const SessionInfo& local_session_info,
  134. std::unique_ptr<syncer::ModelTypeStore> underlying_store,
  135. std::map<std::string, sync_pb::SessionSpecifics> initial_data,
  136. const syncer::EntityMetadataMap& initial_metadata,
  137. SyncSessionsClient* sessions_client);
  138. const SessionInfo local_session_info_;
  139. // In charge of actually persisting changes to disk.
  140. const std::unique_ptr<syncer::ModelTypeStore> store_;
  141. const raw_ptr<SyncSessionsClient> sessions_client_;
  142. SyncedSessionTracker session_tracker_;
  143. base::WeakPtrFactory<SessionStore> weak_ptr_factory_{this};
  144. };
  145. } // namespace sync_sessions
  146. #endif // COMPONENTS_SYNC_SESSIONS_SESSION_STORE_H_