blob: a7a63b17d4a03d5f69301b6bfd27a4ab368b05ea [file] [log] [blame]
Mikel Astiz069039782018-03-02 11:04:431// 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 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,
Mikel Astizbeeed8b2018-06-07 06:55:4664 SyncedSessionTracker* session_tracker);
Peter Boström09c01822021-09-20 22:43:2765
66 LocalSessionEventHandlerImpl(const LocalSessionEventHandlerImpl&) = delete;
67 LocalSessionEventHandlerImpl& operator=(const LocalSessionEventHandlerImpl&) =
68 delete;
69
Mikel Astiz069039782018-03-02 11:04:4370 ~LocalSessionEventHandlerImpl() override;
71
Mikel Astiz069039782018-03-02 11:04:4372 // LocalSessionEventHandler implementation.
Mikel Astizbeeed8b2018-06-07 06:55:4673 void OnSessionRestoreComplete() override;
Mikel Astiz069039782018-03-02 11:04:4374 void OnLocalTabModified(SyncedTabDelegate* modified_tab) override;
Mikel Astiz069039782018-03-02 11:04:4375
Mikel Astizbef1f992018-04-10 10:42:5576 // Returns tab specifics from |tab_delegate|. Exposed publicly for testing.
77 sync_pb::SessionTab GetTabSpecificsFromDelegateForTest(
78 const SyncedTabDelegate& tab_delegate) const;
Mikel Astiz069039782018-03-02 11:04:4379
80 private:
81 enum ReloadTabsOption { RELOAD_TABS, DONT_RELOAD_TABS };
Mikel Astizbc775d6b2018-05-23 14:49:2182
Mikel Astiz3cb0ad32018-12-03 09:16:0683 void CleanupLocalTabs(WriteBatch* batch);
84
Victor Hugo Vianna Silva53785e52021-09-23 13:37:2185 void AssociateWindows(ReloadTabsOption option, WriteBatch* batch);
Mikel Astiz069039782018-03-02 11:04:4386
87 // Loads and reassociates the local tab referenced in |tab|.
Mikel Astiz556bf092018-03-06 13:16:3388 // |batch| must not be null. This function will append necessary
Mikel Astize8554752018-06-20 10:39:4489 // changes for processing later.
Victor Hugo Vianna Silva53785e52021-09-23 13:37:2190 void AssociateTab(SyncedTabDelegate* const tab, WriteBatch* batch);
Mikel Astiz069039782018-03-02 11:04:4391
Mikel Astizbef1f992018-04-10 10:42:5592 // Set |session_tab| from |tab_delegate|.
93 sync_pb::SessionTab GetTabSpecificsFromDelegate(
94 const SyncedTabDelegate& tab_delegate) const;
Mikel Astiz069039782018-03-02 11:04:4395
Mikel Astiz069039782018-03-02 11:04:4396 // Update |tab_specifics| with the corresponding task ids.
David Maunderf476abb2019-07-15 17:22:1597 static void WriteTasksIntoSpecifics(sync_pb::SessionTab* tab_specifics,
98 SyncedTabDelegate* tab_delegate);
Mikel Astiz069039782018-03-02 11:04:4399
Mikel Astiz069039782018-03-02 11:04:43100 // Injected dependencies (not owned).
Keishi Hattori0e45c022021-11-27 09:25:52101 const raw_ptr<Delegate> delegate_;
102 const raw_ptr<SyncSessionsClient> sessions_client_;
103 const raw_ptr<SyncedSessionTracker> session_tracker_;
Mikel Astiz069039782018-03-02 11:04:43104
Mikel Astiz069039782018-03-02 11:04:43105 std::string current_session_tag_;
Mikel Astiz069039782018-03-02 11:04:43106};
107
108} // namespace sync_sessions
109
110#endif // COMPONENTS_SYNC_SESSIONS_LOCAL_SESSION_EVENT_HANDLER_IMPL_H_