Mikel Astiz | 06903978 | 2018-03-02 11:04:43 | [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_LOCAL_SESSION_EVENT_HANDLER_IMPL_H_ |
| 6 | #define COMPONENTS_SYNC_SESSIONS_LOCAL_SESSION_EVENT_HANDLER_IMPL_H_ |
| 7 | |
| 8 | #include <memory> |
| 9 | #include <set> |
| 10 | #include <string> |
| 11 | |
Keishi Hattori | 0e45c02 | 2021-11-27 09:25:52 | [diff] [blame^] | 12 | #include "base/memory/raw_ptr.h" |
Mikel Astiz | 06903978 | 2018-03-02 11:04:43 | [diff] [blame] | 13 | #include "base/time/time.h" |
| 14 | #include "components/sessions/core/session_id.h" |
| 15 | #include "components/sessions/core/session_types.h" |
| 16 | #include "components/sync_sessions/local_session_event_router.h" |
Mikel Astiz | 06903978 | 2018-03-02 11:04:43 | [diff] [blame] | 17 | #include "components/sync_sessions/synced_session.h" |
Mikel Astiz | 06903978 | 2018-03-02 11:04:43 | [diff] [blame] | 18 | |
| 19 | namespace sync_pb { |
| 20 | class SessionSpecifics; |
| 21 | class SessionTab; |
| 22 | } // namespace sync_pb |
| 23 | |
| 24 | namespace sync_sessions { |
| 25 | |
| 26 | class SyncedSessionTracker; |
| 27 | class SyncedTabDelegate; |
David Maunder | f476abb | 2019-07-15 17:22:15 | [diff] [blame] | 28 | class SyncSessionsClient; |
Mikel Astiz | 06903978 | 2018-03-02 11:04:43 | [diff] [blame] | 29 | |
| 30 | // Class responsible for propagating local session changes to the sessions |
| 31 | // model including SyncedSessionTracker (in-memory representation) as well as |
| 32 | // the persistency and sync layers (via delegate). |
| 33 | class LocalSessionEventHandlerImpl : public LocalSessionEventHandler { |
| 34 | public: |
| 35 | class WriteBatch { |
| 36 | public: |
| 37 | WriteBatch(); |
Peter Boström | 09c0182 | 2021-09-20 22:43:27 | [diff] [blame] | 38 | |
| 39 | WriteBatch(const WriteBatch&) = delete; |
| 40 | WriteBatch& operator=(const WriteBatch&) = delete; |
| 41 | |
Mikel Astiz | 06903978 | 2018-03-02 11:04:43 | [diff] [blame] | 42 | virtual ~WriteBatch(); |
| 43 | virtual void Delete(int tab_node_id) = 0; |
Mikel Astiz | ba63cf5 | 2018-05-23 14:01:05 | [diff] [blame] | 44 | virtual void Put(std::unique_ptr<sync_pb::SessionSpecifics> specifics) = 0; |
Mikel Astiz | 06903978 | 2018-03-02 11:04:43 | [diff] [blame] | 45 | virtual void Commit() = 0; |
Mikel Astiz | 06903978 | 2018-03-02 11:04:43 | [diff] [blame] | 46 | }; |
| 47 | |
| 48 | class Delegate { |
| 49 | public: |
| 50 | virtual ~Delegate(); |
| 51 | virtual std::unique_ptr<WriteBatch> CreateLocalSessionWriteBatch() = 0; |
Mikel Astiz | 3cb0ad3 | 2018-12-03 09:16:06 | [diff] [blame] | 52 | virtual bool IsTabNodeUnsynced(int tab_node_id) = 0; |
Mikel Astiz | 556bf09 | 2018-03-06 13:16:33 | [diff] [blame] | 53 | // Analogous to SessionsGlobalIdMapper. |
| 54 | virtual void TrackLocalNavigationId(base::Time timestamp, |
| 55 | int unique_id) = 0; |
Mikel Astiz | 06903978 | 2018-03-02 11:04:43 | [diff] [blame] | 56 | }; |
| 57 | |
Mikel Astiz | beeed8b | 2018-06-07 06:55:46 | [diff] [blame] | 58 | // Raw pointers must not be null and all pointees must outlive this object. |
| 59 | // A side effect of this constructor could include (unless session restore is |
| 60 | // ongoing) the creation of a write batch (via |delegate| and committing |
| 61 | // changes). |
Mikel Astiz | 06903978 | 2018-03-02 11:04:43 | [diff] [blame] | 62 | LocalSessionEventHandlerImpl(Delegate* delegate, |
| 63 | SyncSessionsClient* sessions_client, |
Mikel Astiz | beeed8b | 2018-06-07 06:55:46 | [diff] [blame] | 64 | SyncedSessionTracker* session_tracker); |
Peter Boström | 09c0182 | 2021-09-20 22:43:27 | [diff] [blame] | 65 | |
| 66 | LocalSessionEventHandlerImpl(const LocalSessionEventHandlerImpl&) = delete; |
| 67 | LocalSessionEventHandlerImpl& operator=(const LocalSessionEventHandlerImpl&) = |
| 68 | delete; |
| 69 | |
Mikel Astiz | 06903978 | 2018-03-02 11:04:43 | [diff] [blame] | 70 | ~LocalSessionEventHandlerImpl() override; |
| 71 | |
Mikel Astiz | 06903978 | 2018-03-02 11:04:43 | [diff] [blame] | 72 | // LocalSessionEventHandler implementation. |
Mikel Astiz | beeed8b | 2018-06-07 06:55:46 | [diff] [blame] | 73 | void OnSessionRestoreComplete() override; |
Mikel Astiz | 06903978 | 2018-03-02 11:04:43 | [diff] [blame] | 74 | void OnLocalTabModified(SyncedTabDelegate* modified_tab) override; |
Mikel Astiz | 06903978 | 2018-03-02 11:04:43 | [diff] [blame] | 75 | |
Mikel Astiz | bef1f99 | 2018-04-10 10:42:55 | [diff] [blame] | 76 | // Returns tab specifics from |tab_delegate|. Exposed publicly for testing. |
| 77 | sync_pb::SessionTab GetTabSpecificsFromDelegateForTest( |
| 78 | const SyncedTabDelegate& tab_delegate) const; |
Mikel Astiz | 06903978 | 2018-03-02 11:04:43 | [diff] [blame] | 79 | |
| 80 | private: |
| 81 | enum ReloadTabsOption { RELOAD_TABS, DONT_RELOAD_TABS }; |
Mikel Astiz | bc775d6b | 2018-05-23 14:49:21 | [diff] [blame] | 82 | |
Mikel Astiz | 3cb0ad3 | 2018-12-03 09:16:06 | [diff] [blame] | 83 | void CleanupLocalTabs(WriteBatch* batch); |
| 84 | |
Victor Hugo Vianna Silva | 53785e5 | 2021-09-23 13:37:21 | [diff] [blame] | 85 | void AssociateWindows(ReloadTabsOption option, WriteBatch* batch); |
Mikel Astiz | 06903978 | 2018-03-02 11:04:43 | [diff] [blame] | 86 | |
| 87 | // Loads and reassociates the local tab referenced in |tab|. |
Mikel Astiz | 556bf09 | 2018-03-06 13:16:33 | [diff] [blame] | 88 | // |batch| must not be null. This function will append necessary |
Mikel Astiz | e855475 | 2018-06-20 10:39:44 | [diff] [blame] | 89 | // changes for processing later. |
Victor Hugo Vianna Silva | 53785e5 | 2021-09-23 13:37:21 | [diff] [blame] | 90 | void AssociateTab(SyncedTabDelegate* const tab, WriteBatch* batch); |
Mikel Astiz | 06903978 | 2018-03-02 11:04:43 | [diff] [blame] | 91 | |
Mikel Astiz | bef1f99 | 2018-04-10 10:42:55 | [diff] [blame] | 92 | // Set |session_tab| from |tab_delegate|. |
| 93 | sync_pb::SessionTab GetTabSpecificsFromDelegate( |
| 94 | const SyncedTabDelegate& tab_delegate) const; |
Mikel Astiz | 06903978 | 2018-03-02 11:04:43 | [diff] [blame] | 95 | |
Mikel Astiz | 06903978 | 2018-03-02 11:04:43 | [diff] [blame] | 96 | // Update |tab_specifics| with the corresponding task ids. |
David Maunder | f476abb | 2019-07-15 17:22:15 | [diff] [blame] | 97 | static void WriteTasksIntoSpecifics(sync_pb::SessionTab* tab_specifics, |
| 98 | SyncedTabDelegate* tab_delegate); |
Mikel Astiz | 06903978 | 2018-03-02 11:04:43 | [diff] [blame] | 99 | |
Mikel Astiz | 06903978 | 2018-03-02 11:04:43 | [diff] [blame] | 100 | // Injected dependencies (not owned). |
Keishi Hattori | 0e45c02 | 2021-11-27 09:25:52 | [diff] [blame^] | 101 | const raw_ptr<Delegate> delegate_; |
| 102 | const raw_ptr<SyncSessionsClient> sessions_client_; |
| 103 | const raw_ptr<SyncedSessionTracker> session_tracker_; |
Mikel Astiz | 06903978 | 2018-03-02 11:04:43 | [diff] [blame] | 104 | |
Mikel Astiz | 06903978 | 2018-03-02 11:04:43 | [diff] [blame] | 105 | std::string current_session_tag_; |
Mikel Astiz | 06903978 | 2018-03-02 11:04:43 | [diff] [blame] | 106 | }; |
| 107 | |
| 108 | } // namespace sync_sessions |
| 109 | |
| 110 | #endif // COMPONENTS_SYNC_SESSIONS_LOCAL_SESSION_EVENT_HANDLER_IMPL_H_ |