blob: abbb00d9cdcf6f46ade487a3e884c63afd7cda41 [file] [log] [blame]
Avi Drissman8ba1bad2022-09-13 19:22:361// Copyright 2018 The Chromium Authors
Mikel Astiz069039782018-03-02 11:04:432// 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 Hattori0e45c022021-11-27 09:25:5212#include "base/memory/raw_ptr.h"
Mikel Astiz069039782018-03-02 11:04:4313#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 Astiz069039782018-03-02 11:04:4317#include "components/sync_sessions/synced_session.h"
Mikel Astiz069039782018-03-02 11:04:4318
19namespace sync_pb {
20class SessionSpecifics;
21class SessionTab;
22} // namespace sync_pb
23
24namespace sync_sessions {
25
26class SyncedSessionTracker;
27class SyncedTabDelegate;
David Maunderf476abb2019-07-15 17:22:1528class SyncSessionsClient;
Mikel Astiz069039782018-03-02 11:04:4329
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).
33class LocalSessionEventHandlerImpl : public LocalSessionEventHandler {
34 public:
35 class WriteBatch {
36 public:
37 WriteBatch();
Peter Boström09c01822021-09-20 22:43:2738
39 WriteBatch(const WriteBatch&) = delete;
40 WriteBatch& operator=(const WriteBatch&) = delete;
41
Mikel Astiz069039782018-03-02 11:04:4342 virtual ~WriteBatch();
43 virtual void Delete(int tab_node_id) = 0;
Mikel Astizba63cf52018-05-23 14:01:0544 virtual void Put(std::unique_ptr<sync_pb::SessionSpecifics> specifics) = 0;
Mikel Astiz069039782018-03-02 11:04:4345 virtual void Commit() = 0;
Mikel Astiz069039782018-03-02 11:04:4346 };
47
48 class Delegate {
49 public:
50 virtual ~Delegate();
51 virtual std::unique_ptr<WriteBatch> CreateLocalSessionWriteBatch() = 0;
Mikel Astiz3cb0ad32018-12-03 09:16:0652 virtual bool IsTabNodeUnsynced(int tab_node_id) = 0;
Mikel Astiz556bf092018-03-06 13:16:3353 // Analogous to SessionsGlobalIdMapper.
54 virtual void TrackLocalNavigationId(base::Time timestamp,
55 int unique_id) = 0;
Mikel Astiz069039782018-03-02 11:04:4356 };
57
Mikel Astizbeeed8b2018-06-07 06:55:4658 // 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 Astiz069039782018-03-02 11:04:4362 LocalSessionEventHandlerImpl(Delegate* delegate,
63 SyncSessionsClient* sessions_client,
Marc Treib7d07e2082024-09-05 09:06:3864 SyncedSessionTracker* session_tracker,
65 bool is_new_session);
Peter Boström09c01822021-09-20 22:43:2766
67 LocalSessionEventHandlerImpl(const LocalSessionEventHandlerImpl&) = delete;
68 LocalSessionEventHandlerImpl& operator=(const LocalSessionEventHandlerImpl&) =
69 delete;
70
Mikel Astiz069039782018-03-02 11:04:4371 ~LocalSessionEventHandlerImpl() override;
72
Mikel Astiz069039782018-03-02 11:04:4373 // LocalSessionEventHandler implementation.
Mikel Astizbeeed8b2018-06-07 06:55:4674 void OnSessionRestoreComplete() override;
Mikel Astiz069039782018-03-02 11:04:4375 void OnLocalTabModified(SyncedTabDelegate* modified_tab) override;
Erik Chen4b855152024-11-06 23:02:1976 void OnLocalTabClosed() override;
Mikel Astiz069039782018-03-02 11:04:4377
Mikel Astizbef1f992018-04-10 10:42:5578 // Returns tab specifics from |tab_delegate|. Exposed publicly for testing.
79 sync_pb::SessionTab GetTabSpecificsFromDelegateForTest(
Gauthier Ambardb352412f2023-12-22 10:59:3880 SyncedTabDelegate& tab_delegate) const;
Mikel Astiz069039782018-03-02 11:04:4381
82 private:
83 enum ReloadTabsOption { RELOAD_TABS, DONT_RELOAD_TABS };
Mikel Astizbc775d6b2018-05-23 14:49:2184
Mikel Astiz3cb0ad32018-12-03 09:16:0685 void CleanupLocalTabs(WriteBatch* batch);
86
Brandon Fong40f20c5d2023-08-31 17:41:5087 void AssociateWindows(ReloadTabsOption option,
88 WriteBatch* batch,
89 bool is_session_restore);
Mikel Astiz069039782018-03-02 11:04:4390
91 // Loads and reassociates the local tab referenced in |tab|.
Mikel Astiz556bf092018-03-06 13:16:3392 // |batch| must not be null. This function will append necessary
Mikel Astize8554752018-06-20 10:39:4493 // changes for processing later.
Victor Hugo Vianna Silva53785e52021-09-23 13:37:2194 void AssociateTab(SyncedTabDelegate* const tab, WriteBatch* batch);
Mikel Astiz069039782018-03-02 11:04:4395
Mikel Astizbef1f992018-04-10 10:42:5596 // Set |session_tab| from |tab_delegate|.
97 sync_pb::SessionTab GetTabSpecificsFromDelegate(
Gauthier Ambardb352412f2023-12-22 10:59:3898 SyncedTabDelegate& tab_delegate) const;
Mikel Astiz069039782018-03-02 11:04:4399
Brandon Fong40f20c5d2023-08-31 17:41:50100 bool AssociatePlaceholderTab(std::unique_ptr<SyncedTabDelegate> snapshot,
101 WriteBatch* batch);
102
Mikel Astiz069039782018-03-02 11:04:43103 // Injected dependencies (not owned).
Keishi Hattori0e45c022021-11-27 09:25:52104 const raw_ptr<Delegate> delegate_;
105 const raw_ptr<SyncSessionsClient> sessions_client_;
106 const raw_ptr<SyncedSessionTracker> session_tracker_;
Mikel Astiz069039782018-03-02 11:04:43107
Mikel Astiz069039782018-03-02 11:04:43108 std::string current_session_tag_;
Mikel Astiz069039782018-03-02 11:04:43109};
110
111} // namespace sync_sessions
112
113#endif // COMPONENTS_SYNC_SESSIONS_LOCAL_SESSION_EVENT_HANDLER_IMPL_H_