Avi Drissman | 8ba1bad | 2022-09-13 19:22:36 | [diff] [blame] | 1 | // Copyright 2018 The Chromium Authors |
Mikel Astiz | aadda591 | 2018-04-13 11:14:37 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #ifndef COMPONENTS_SYNC_SESSIONS_SESSION_STORE_H_ |
| 6 | #define COMPONENTS_SYNC_SESSIONS_SESSION_STORE_H_ |
| 7 | |
| 8 | #include <map> |
| 9 | #include <memory> |
Arthur Sonzogni | c571efb | 2024-01-26 20:26:18 | [diff] [blame] | 10 | #include <optional> |
Mikel Astiz | aadda591 | 2018-04-13 11:14:37 | [diff] [blame] | 11 | #include <string> |
| 12 | #include <vector> |
| 13 | |
Avi Drissman | 12be031 | 2023-01-11 09:16:09 | [diff] [blame] | 14 | #include "base/functional/callback_forward.h" |
Keishi Hattori | 0e45c02 | 2021-11-27 09:25:52 | [diff] [blame] | 15 | #include "base/memory/raw_ptr.h" |
Mikel Astiz | aadda591 | 2018-04-13 11:14:37 | [diff] [blame] | 16 | #include "base/memory/weak_ptr.h" |
| 17 | #include "base/time/time.h" |
Mikel Astiz | aadda591 | 2018-04-13 11:14:37 | [diff] [blame] | 18 | #include "components/sync/model/data_batch.h" |
Mikel Astiz | 9048b12 | 2024-08-05 12:55:41 | [diff] [blame] | 19 | #include "components/sync/model/data_type_store.h" |
Victor Hugo Vianna Silva | b06c5097 | 2022-02-22 21:35:10 | [diff] [blame] | 20 | #include "components/sync/model/metadata_batch.h" |
Mikel Astiz | aadda591 | 2018-04-13 11:14:37 | [diff] [blame] | 21 | #include "components/sync/model/model_error.h" |
Jood Hajeer | 438e516 | 2022-11-21 12:02:19 | [diff] [blame] | 22 | #include "components/sync_device_info/device_info.h" |
Mikel Astiz | aadda591 | 2018-04-13 11:14:37 | [diff] [blame] | 23 | #include "components/sync_sessions/synced_session_tracker.h" |
| 24 | |
Mikel Astiz | aadda591 | 2018-04-13 11:14:37 | [diff] [blame] | 25 | namespace sync_sessions { |
| 26 | |
| 27 | // Class responsible for maintaining an in-memory representation of sync |
| 28 | // sessions (by owning a SyncedSessionTracker) with the capability to persist |
| 29 | // state to disk and restore (data and metadata). The API enforces a valid and |
| 30 | // consistent state of the model, e.g. by making sure there is at most one sync |
| 31 | // entity per client tag. |
| 32 | class SessionStore { |
| 33 | public: |
| 34 | struct SessionInfo { |
| 35 | std::string session_tag; |
| 36 | std::string client_name; |
| 37 | sync_pb::SyncEnums::DeviceType device_type = sync_pb::SyncEnums::TYPE_UNSET; |
Jood Hajeer | 438e516 | 2022-11-21 12:02:19 | [diff] [blame] | 38 | syncer::DeviceInfo::FormFactor device_form_factor = |
| 39 | syncer::DeviceInfo::FormFactor::kUnknown; |
Mikel Astiz | aadda591 | 2018-04-13 11:14:37 | [diff] [blame] | 40 | }; |
| 41 | |
Mikel Astiz | 6265a18 | 2019-02-11 15:26:01 | [diff] [blame] | 42 | using OpenCallback = base::OnceCallback<void( |
Arthur Sonzogni | c571efb | 2024-01-26 20:26:18 | [diff] [blame] | 43 | const std::optional<syncer::ModelError>& error, |
Mikel Astiz | aadda591 | 2018-04-13 11:14:37 | [diff] [blame] | 44 | std::unique_ptr<SessionStore> store, |
| 45 | std::unique_ptr<syncer::MetadataBatch> metadata_batch)>; |
Mikel Astiz | aadda591 | 2018-04-13 11:14:37 | [diff] [blame] | 46 | |
Mikel Astiz | 6265a18 | 2019-02-11 15:26:01 | [diff] [blame] | 47 | // Opens a SessionStore instance, which involves IO to load previous state |
| 48 | // from disk. |sessions_client| must not be null and must outlive the |
| 49 | // SessionStore instance returned via |callback|, or until the callback is |
| 50 | // cancelled. |
Victor Hugo Vianna Silva | 53785e5 | 2021-09-23 13:37:21 | [diff] [blame] | 51 | static void Open(const std::string& cache_guid, |
| 52 | SyncSessionsClient* sessions_client, |
| 53 | OpenCallback callback); |
Mikel Astiz | aadda591 | 2018-04-13 11:14:37 | [diff] [blame] | 54 | |
| 55 | // Verifies whether a proto is malformed (e.g. required fields are missing). |
| 56 | static bool AreValidSpecifics(const sync_pb::SessionSpecifics& specifics); |
| 57 | // |specifics| must be valid, see AreValidSpecifics(). |
| 58 | static std::string GetClientTag(const sync_pb::SessionSpecifics& specifics); |
| 59 | // |specifics| must be valid, see AreValidSpecifics(). |
| 60 | static std::string GetStorageKey(const sync_pb::SessionSpecifics& specifics); |
Mikel Astiz | c6a0352f | 2018-04-20 13:29:14 | [diff] [blame] | 61 | static std::string GetHeaderStorageKey(const std::string& session_tag); |
| 62 | static std::string GetTabStorageKey(const std::string& session_tag, |
| 63 | int tab_node_id); |
Mikel Astiz | 527ec91 | 2018-04-18 15:25:28 | [diff] [blame] | 64 | // Verifies if |storage_key| corresponds to an entity in the local session, |
| 65 | // identified by the session tag. |
| 66 | bool StorageKeyMatchesLocalSession(const std::string& storage_key) const; |
Mikel Astiz | aadda591 | 2018-04-13 11:14:37 | [diff] [blame] | 67 | |
Mikel Astiz | 527ec91 | 2018-04-18 15:25:28 | [diff] [blame] | 68 | // Various equivalents for testing. |
Mikel Astiz | 527ec91 | 2018-04-18 15:25:28 | [diff] [blame] | 69 | static std::string GetTabClientTagForTest(const std::string& session_tag, |
| 70 | int tab_node_id); |
Mikel Astiz | aadda591 | 2018-04-13 11:14:37 | [diff] [blame] | 71 | |
Mikel Astiz | 31a57c5e | 2024-08-05 15:45:52 | [diff] [blame] | 72 | // Similar to DataTypeStore::WriteBatch but enforces a consistent state. In |
Mikel Astiz | aadda591 | 2018-04-13 11:14:37 | [diff] [blame] | 73 | // the current implementation, some functions do *NOT* update the tracker, so |
| 74 | // callers are responsible for doing so. |
Alison Gale | 45c4058f | 2024-04-26 19:30:02 | [diff] [blame] | 75 | // TODO(crbug.com/41295474): Enforce consistency between in-memory and |
| 76 | // persisted data by always updating the tracker. |
Mikel Astiz | aadda591 | 2018-04-13 11:14:37 | [diff] [blame] | 77 | class WriteBatch { |
| 78 | public: |
Mikel Astiz | 31a57c5e | 2024-08-05 15:45:52 | [diff] [blame] | 79 | // Callback that mimics the signature of DataTypeStore::CommitWriteBatch(). |
Mikel Astiz | aadda591 | 2018-04-13 11:14:37 | [diff] [blame] | 80 | using CommitCallback = base::OnceCallback<void( |
Mikel Astiz | 31a57c5e | 2024-08-05 15:45:52 | [diff] [blame] | 81 | std::unique_ptr<syncer::DataTypeStore::WriteBatch>, |
| 82 | syncer::DataTypeStore::CallbackWithResult)>; |
Mikel Astiz | aadda591 | 2018-04-13 11:14:37 | [diff] [blame] | 83 | |
| 84 | // Raw pointers must not be nullptr and must outlive this object. |
Mikel Astiz | 31a57c5e | 2024-08-05 15:45:52 | [diff] [blame] | 85 | WriteBatch(std::unique_ptr<syncer::DataTypeStore::WriteBatch> batch, |
Mikel Astiz | aadda591 | 2018-04-13 11:14:37 | [diff] [blame] | 86 | CommitCallback commit_cb, |
| 87 | syncer::OnceModelErrorHandler error_handler, |
| 88 | SyncedSessionTracker* session_tracker); |
Peter Boström | 09c0182 | 2021-09-20 22:43:27 | [diff] [blame] | 89 | |
| 90 | WriteBatch(const WriteBatch&) = delete; |
| 91 | WriteBatch& operator=(const WriteBatch&) = delete; |
| 92 | |
Mikel Astiz | aadda591 | 2018-04-13 11:14:37 | [diff] [blame] | 93 | ~WriteBatch(); |
| 94 | |
Mikel Astiz | a1074c5 | 2018-12-04 09:22:49 | [diff] [blame] | 95 | // Most mutations below return storage keys. |
Mikel Astiz | aadda591 | 2018-04-13 11:14:37 | [diff] [blame] | 96 | std::string PutAndUpdateTracker(const sync_pb::SessionSpecifics& specifics, |
| 97 | base::Time modification_time); |
Mikel Astiz | a1074c5 | 2018-12-04 09:22:49 | [diff] [blame] | 98 | // Returns all deleted storage keys, which may be more than one if |
| 99 | // |storage_key| refers to a header entity. |
| 100 | std::vector<std::string> DeleteForeignEntityAndUpdateTracker( |
| 101 | const std::string& storage_key); |
Mikel Astiz | aadda591 | 2018-04-13 11:14:37 | [diff] [blame] | 102 | // The functions below do not update SyncedSessionTracker and hence it is |
| 103 | // the caller's responsibility to do so *before* calling these functions. |
| 104 | std::string PutWithoutUpdatingTracker( |
| 105 | const sync_pb::SessionSpecifics& specifics); |
| 106 | std::string DeleteLocalTabWithoutUpdatingTracker(int tab_node_id); |
| 107 | |
| 108 | syncer::MetadataChangeList* GetMetadataChangeList(); |
| 109 | |
| 110 | static void Commit(std::unique_ptr<WriteBatch> batch); |
| 111 | |
| 112 | private: |
Mikel Astiz | 31a57c5e | 2024-08-05 15:45:52 | [diff] [blame] | 113 | std::unique_ptr<syncer::DataTypeStore::WriteBatch> batch_; |
Mikel Astiz | aadda591 | 2018-04-13 11:14:37 | [diff] [blame] | 114 | CommitCallback commit_cb_; |
| 115 | syncer::OnceModelErrorHandler error_handler_; |
Keishi Hattori | 0e45c02 | 2021-11-27 09:25:52 | [diff] [blame] | 116 | const raw_ptr<SyncedSessionTracker> session_tracker_; |
Mikel Astiz | aadda591 | 2018-04-13 11:14:37 | [diff] [blame] | 117 | }; |
| 118 | |
Peter Boström | 9f667c38 | 2021-10-01 20:09:31 | [diff] [blame] | 119 | SessionStore(const SessionStore&) = delete; |
| 120 | SessionStore& operator=(const SessionStore&) = delete; |
| 121 | |
Mikel Astiz | aadda591 | 2018-04-13 11:14:37 | [diff] [blame] | 122 | ~SessionStore(); |
| 123 | |
| 124 | const SessionInfo& local_session_info() const { return local_session_info_; } |
| 125 | |
Mikel Astiz | db744b64 | 2018-04-20 10:17:14 | [diff] [blame] | 126 | // Converts the in-memory model (SyncedSessionTracker) of sessions to sync |
| 127 | // protos. |
| 128 | std::unique_ptr<syncer::DataBatch> GetSessionDataForKeys( |
Mikel Astiz | aadda591 | 2018-04-13 11:14:37 | [diff] [blame] | 129 | const std::vector<std::string>& storage_keys) const; |
| 130 | |
Mikel Astiz | db744b64 | 2018-04-20 10:17:14 | [diff] [blame] | 131 | // Returns all known session entities, local and foreign, generated from the |
| 132 | // in-memory model (SyncedSessionTracker). |
| 133 | std::unique_ptr<syncer::DataBatch> GetAllSessionData() const; |
Mikel Astiz | aadda591 | 2018-04-13 11:14:37 | [diff] [blame] | 134 | |
| 135 | // Write API. WriteBatch instances must not outlive this store and must be |
| 136 | // committed prior to destruction. Besides, more than one uncommitted |
| 137 | // instance must not exist at any time. |
| 138 | std::unique_ptr<WriteBatch> CreateWriteBatch( |
| 139 | syncer::OnceModelErrorHandler error_handler); |
Marc Treib | 71e67bc7 | 2024-09-26 10:33:12 | [diff] [blame] | 140 | |
| 141 | using RecreateEmptyStoreCallback = |
| 142 | base::OnceCallback<std::unique_ptr<SessionStore>( |
| 143 | const std::string& cache_guid, |
| 144 | SyncSessionsClient* sessions_client)>; |
| 145 | |
| 146 | // Deletes all data and metadata from the `session_store` and destroys it. |
| 147 | // Returns a callback that allows synchronously re-creating an empty |
| 148 | // SessionStore, by reusing the underlying DataTypeStore. |
| 149 | static RecreateEmptyStoreCallback DeleteAllDataAndMetadata( |
| 150 | std::unique_ptr<SessionStore> session_store); |
Mikel Astiz | aadda591 | 2018-04-13 11:14:37 | [diff] [blame] | 151 | |
Alison Gale | 45c4058f | 2024-04-26 19:30:02 | [diff] [blame] | 152 | // TODO(crbug.com/41295474): Avoid exposing a mutable tracker, because that |
Mikel Astiz | aadda591 | 2018-04-13 11:14:37 | [diff] [blame] | 153 | // bypasses the consistency-enforcing API. |
| 154 | SyncedSessionTracker* mutable_tracker() { return &session_tracker_; } |
| 155 | const SyncedSessionTracker* tracker() const { return &session_tracker_; } |
| 156 | |
| 157 | private: |
Mikel Astiz | 8680bb3 | 2019-04-10 19:55:13 | [diff] [blame] | 158 | // Helper class used to collect all parameters needed by the constructor. |
| 159 | struct Builder; |
| 160 | |
Mikel Astiz | 6265a18 | 2019-02-11 15:26:01 | [diff] [blame] | 161 | static void OnStoreCreated( |
Mikel Astiz | 8680bb3 | 2019-04-10 19:55:13 | [diff] [blame] | 162 | std::unique_ptr<Builder> builder, |
Arthur Sonzogni | c571efb | 2024-01-26 20:26:18 | [diff] [blame] | 163 | const std::optional<syncer::ModelError>& error, |
Mikel Astiz | 31a57c5e | 2024-08-05 15:45:52 | [diff] [blame] | 164 | std::unique_ptr<syncer::DataTypeStore> underlying_store); |
Mikel Astiz | 6265a18 | 2019-02-11 15:26:01 | [diff] [blame] | 165 | static void OnReadAllMetadata( |
Mikel Astiz | 8680bb3 | 2019-04-10 19:55:13 | [diff] [blame] | 166 | std::unique_ptr<Builder> builder, |
Arthur Sonzogni | c571efb | 2024-01-26 20:26:18 | [diff] [blame] | 167 | const std::optional<syncer::ModelError>& error, |
Mikel Astiz | 6265a18 | 2019-02-11 15:26:01 | [diff] [blame] | 168 | std::unique_ptr<syncer::MetadataBatch> metadata_batch); |
Mikel Astiz | 8680bb3 | 2019-04-10 19:55:13 | [diff] [blame] | 169 | static void OnReadAllData(std::unique_ptr<Builder> builder, |
Arthur Sonzogni | c571efb | 2024-01-26 20:26:18 | [diff] [blame] | 170 | const std::optional<syncer::ModelError>& error); |
Mikel Astiz | 6265a18 | 2019-02-11 15:26:01 | [diff] [blame] | 171 | |
Marc Treib | 71e67bc7 | 2024-09-26 10:33:12 | [diff] [blame] | 172 | static std::unique_ptr<SessionStore> RecreateEmptyStore( |
| 173 | SessionInfo local_session_info_without_session_tag, |
| 174 | std::unique_ptr<syncer::DataTypeStore> underlying_store, |
| 175 | const std::string& cache_guid, |
| 176 | SyncSessionsClient* sessions_client); |
| 177 | |
Mikel Astiz | 8680bb3 | 2019-04-10 19:55:13 | [diff] [blame] | 178 | // |sessions_client| must not be null and must outlive this object. |
Mikel Astiz | 6265a18 | 2019-02-11 15:26:01 | [diff] [blame] | 179 | SessionStore(const SessionInfo& local_session_info, |
Mikel Astiz | 31a57c5e | 2024-08-05 15:45:52 | [diff] [blame] | 180 | std::unique_ptr<syncer::DataTypeStore> underlying_store, |
Mikel Astiz | 8680bb3 | 2019-04-10 19:55:13 | [diff] [blame] | 181 | std::map<std::string, sync_pb::SessionSpecifics> initial_data, |
| 182 | const syncer::EntityMetadataMap& initial_metadata, |
Mikel Astiz | 6265a18 | 2019-02-11 15:26:01 | [diff] [blame] | 183 | SyncSessionsClient* sessions_client); |
| 184 | |
Mikel Astiz | 6265a18 | 2019-02-11 15:26:01 | [diff] [blame] | 185 | const SessionInfo local_session_info_; |
Mikel Astiz | 6265a18 | 2019-02-11 15:26:01 | [diff] [blame] | 186 | |
Mikel Astiz | aadda591 | 2018-04-13 11:14:37 | [diff] [blame] | 187 | // In charge of actually persisting changes to disk. |
Marc Treib | 71e67bc7 | 2024-09-26 10:33:12 | [diff] [blame] | 188 | std::unique_ptr<syncer::DataTypeStore> store_; |
Mikel Astiz | aadda591 | 2018-04-13 11:14:37 | [diff] [blame] | 189 | |
Keishi Hattori | 0e45c02 | 2021-11-27 09:25:52 | [diff] [blame] | 190 | const raw_ptr<SyncSessionsClient> sessions_client_; |
Mikel Astiz | 9a36638e | 2020-12-17 17:52:48 | [diff] [blame] | 191 | |
Mikel Astiz | aadda591 | 2018-04-13 11:14:37 | [diff] [blame] | 192 | SyncedSessionTracker session_tracker_; |
| 193 | |
Jeremy Roman | 5c341f6d | 2019-07-15 15:56:10 | [diff] [blame] | 194 | base::WeakPtrFactory<SessionStore> weak_ptr_factory_{this}; |
Mikel Astiz | aadda591 | 2018-04-13 11:14:37 | [diff] [blame] | 195 | }; |
| 196 | |
| 197 | } // namespace sync_sessions |
| 198 | |
| 199 | #endif // COMPONENTS_SYNC_SESSIONS_SESSION_STORE_H_ |