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