blob: 1694107682f23e15827faebb6b9b8ad2539c85cf [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
12#include "base/macros.h"
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 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();
38 virtual ~WriteBatch();
39 virtual void Delete(int tab_node_id) = 0;
Mikel Astizba63cf52018-05-23 14:01:0540 virtual void Put(std::unique_ptr<sync_pb::SessionSpecifics> specifics) = 0;
Mikel Astiz069039782018-03-02 11:04:4341 virtual void Commit() = 0;
42
43 private:
44 DISALLOW_COPY_AND_ASSIGN(WriteBatch);
45 };
46
47 class Delegate {
48 public:
49 virtual ~Delegate();
50 virtual std::unique_ptr<WriteBatch> CreateLocalSessionWriteBatch() = 0;
Mikel Astiz3cb0ad32018-12-03 09:16:0651 virtual bool IsTabNodeUnsynced(int tab_node_id) = 0;
Mikel Astiz556bf092018-03-06 13:16:3352 // Analogous to SessionsGlobalIdMapper.
53 virtual void TrackLocalNavigationId(base::Time timestamp,
54 int unique_id) = 0;
Mikel Astiz069039782018-03-02 11:04:4355 // Analogous to the functions in FaviconCache.
56 virtual void OnPageFaviconUpdated(const GURL& page_url) = 0;
57 virtual void OnFaviconVisited(const GURL& page_url,
58 const GURL& favicon_url) = 0;
59 };
60
Mikel Astizbeeed8b2018-06-07 06:55:4661 // Raw pointers must not be null and all pointees must outlive this object.
62 // A side effect of this constructor could include (unless session restore is
63 // ongoing) the creation of a write batch (via |delegate| and committing
64 // changes).
Mikel Astiz069039782018-03-02 11:04:4365 LocalSessionEventHandlerImpl(Delegate* delegate,
66 SyncSessionsClient* sessions_client,
Mikel Astizbeeed8b2018-06-07 06:55:4667 SyncedSessionTracker* session_tracker);
Mikel Astiz069039782018-03-02 11:04:4368 ~LocalSessionEventHandlerImpl() override;
69
Mikel Astiz069039782018-03-02 11:04:4370 // LocalSessionEventHandler implementation.
Mikel Astizbeeed8b2018-06-07 06:55:4671 void OnSessionRestoreComplete() override;
Mikel Astiz069039782018-03-02 11:04:4372 void OnLocalTabModified(SyncedTabDelegate* modified_tab) override;
73 void OnFaviconsChanged(const std::set<GURL>& page_urls,
74 const GURL& icon_url) override;
75
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
Mikel Astiz069039782018-03-02 11:04:4385 void AssociateWindows(ReloadTabsOption option,
Mikel Astiz556bf092018-03-06 13:16:3386 WriteBatch* batch);
Mikel Astiz069039782018-03-02 11:04:4387
88 // Loads and reassociates the local tab referenced in |tab|.
Mikel Astiz556bf092018-03-06 13:16:3389 // |batch| must not be null. This function will append necessary
Mikel Astize8554752018-06-20 10:39:4490 // changes for processing later.
Mikel Astiz069039782018-03-02 11:04:4391 void AssociateTab(SyncedTabDelegate* const tab,
Mikel Astiz556bf092018-03-06 13:16:3392 WriteBatch* batch);
Mikel Astiz069039782018-03-02 11:04:4393
Mikel Astizbef1f992018-04-10 10:42:5594 // Set |session_tab| from |tab_delegate|.
95 sync_pb::SessionTab GetTabSpecificsFromDelegate(
96 const SyncedTabDelegate& tab_delegate) const;
Mikel Astiz069039782018-03-02 11:04:4397
Mikel Astiz069039782018-03-02 11:04:4398 // Update |tab_specifics| with the corresponding task ids.
David Maunderf476abb2019-07-15 17:22:1599 static void WriteTasksIntoSpecifics(sync_pb::SessionTab* tab_specifics,
100 SyncedTabDelegate* tab_delegate);
Mikel Astiz069039782018-03-02 11:04:43101
Mikel Astiz069039782018-03-02 11:04:43102 // Injected dependencies (not owned).
103 Delegate* const delegate_;
104 SyncSessionsClient* const sessions_client_;
105 SyncedSessionTracker* const session_tracker_;
106
Mikel Astiz069039782018-03-02 11:04:43107 std::string current_session_tag_;
108
109 DISALLOW_COPY_AND_ASSIGN(LocalSessionEventHandlerImpl);
110};
111
112} // namespace sync_sessions
113
114#endif // COMPONENTS_SYNC_SESSIONS_LOCAL_SESSION_EVENT_HANDLER_IMPL_H_