blob: 071b593b26690ec173370f838223b79b95b4fec2 [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"
18#include "components/sync_sessions/task_tracker.h"
19
20namespace sync_pb {
21class SessionSpecifics;
22class SessionTab;
23} // namespace sync_pb
24
25namespace sync_sessions {
26
27class SyncedSessionTracker;
28class SyncedTabDelegate;
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).
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 Astiz556bf092018-03-06 13:16:3351 // Analogous to SessionsGlobalIdMapper.
52 virtual void TrackLocalNavigationId(base::Time timestamp,
53 int unique_id) = 0;
Mikel Astiz069039782018-03-02 11:04:4354 // Analogous to the functions in FaviconCache.
55 virtual void OnPageFaviconUpdated(const GURL& page_url) = 0;
56 virtual void OnFaviconVisited(const GURL& page_url,
57 const GURL& favicon_url) = 0;
58 };
59
Mikel Astiz556bf092018-03-06 13:16:3360 // Raw pointers must not be null and all pointees except |*initial_batch| must
61 // outlive this object. |*initial_batch| may or may not be initially empty
62 // (depending on whether the caller wants to bundle together other writes).
63 // This constructor populates |*initial_batch| to resync local window and tab
64 // information, but does *not* Commit() the batch.
Mikel Astiz069039782018-03-02 11:04:4365 LocalSessionEventHandlerImpl(Delegate* delegate,
66 SyncSessionsClient* sessions_client,
Mikel Astiz556bf092018-03-06 13:16:3367 SyncedSessionTracker* session_tracker,
68 WriteBatch* initial_batch);
Mikel Astiz069039782018-03-02 11:04:4369 ~LocalSessionEventHandlerImpl() override;
70
Mikel Astiz069039782018-03-02 11:04:4371 // LocalSessionEventHandler implementation.
72 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
83 // Updates |session_tracker_| with tab_id<->tab_node_id association that the
84 // delegate already knows about, while resolving conflicts if the delegate
85 // reports conflicting sync IDs. This makes sure duplicate tab_node_id-s are
86 // not assigned. On return, the following conditions are met:
87 // 1. Delegate contains no duplicate sync IDs (tab_node_id).
88 // 2. Delegate contains no sync-ID <-> tab_id association that the tracker
89 // doesn't know about (but not the opposite).
90 void AssociateExistingSyncIds();
91
Mikel Astiz069039782018-03-02 11:04:4392 void AssociateWindows(ReloadTabsOption option,
93 bool has_tabbed_window,
Mikel Astiz556bf092018-03-06 13:16:3394 WriteBatch* batch);
Mikel Astiz069039782018-03-02 11:04:4395
96 // Loads and reassociates the local tab referenced in |tab|.
Mikel Astiz556bf092018-03-06 13:16:3397 // |batch| must not be null. This function will append necessary
Mikel Astiz069039782018-03-02 11:04:4398 // changes for processing later. Will only assign a new sync id if there is
99 // a tabbed window, which results in failure for tabs without sync ids yet.
100 void AssociateTab(SyncedTabDelegate* const tab,
101 bool has_tabbed_window,
Mikel Astiz556bf092018-03-06 13:16:33102 WriteBatch* batch);
Mikel Astiz069039782018-03-02 11:04:43103
104 // It's possible that when we associate windows, tabs aren't all loaded
105 // into memory yet (e.g on android) and we don't have a WebContents. In this
106 // case we can't do a full association, but we still want to update tab IDs
107 // as they may have changed after a session was restored. This method
108 // compares new_tab_id and new_window_id against the previously persisted tab
109 // ID and window ID (from our TabNodePool) and updates them if either differs.
110 void AssociateRestoredPlaceholderTab(const SyncedTabDelegate& tab_delegate,
Mikel Astize023c572018-03-28 07:56:56111 SessionID new_tab_id,
112 SessionID new_window_id,
Mikel Astiz556bf092018-03-06 13:16:33113 WriteBatch* batch);
Mikel Astiz069039782018-03-02 11:04:43114
Mikel Astiz556bf092018-03-06 13:16:33115 // Appends an ACTION_UPDATE for a sync tab entity onto |batch| to
Mikel Astiz069039782018-03-02 11:04:43116 // reflect the contents of |tab|, given the tab node id |sync_id|.
117 void AppendChangeForExistingTab(int sync_id,
118 const sessions::SessionTab& tab,
Mikel Astiz556bf092018-03-06 13:16:33119 WriteBatch* batch) const;
Mikel Astiz069039782018-03-02 11:04:43120
Mikel Astizbef1f992018-04-10 10:42:55121 // Set |session_tab| from |tab_delegate|.
122 sync_pb::SessionTab GetTabSpecificsFromDelegate(
123 const SyncedTabDelegate& tab_delegate) const;
Mikel Astiz069039782018-03-02 11:04:43124
125 // Updates task tracker with the navigations of |tab_delegate|.
126 void UpdateTaskTracker(SyncedTabDelegate* const tab_delegate);
127
128 // Update |tab_specifics| with the corresponding task ids.
129 void WriteTasksIntoSpecifics(sync_pb::SessionTab* tab_specifics);
130
131 // On Android, it's possible to not have any tabbed windows when only custom
132 // tabs are currently open. This means that there is tab data that will be
133 // restored later, but we cannot access it. This method is an elaborate way to
134 // check if we're currently in that state or not.
135 bool ScanForTabbedWindow();
136
137 // Injected dependencies (not owned).
138 Delegate* const delegate_;
139 SyncSessionsClient* const sessions_client_;
140 SyncedSessionTracker* const session_tracker_;
141
Mikel Astiz069039782018-03-02 11:04:43142 // Tracks Chrome Tasks, which associates navigations, with tab and navigation
143 // changes of current session.
144 TaskTracker task_tracker_;
145
146 std::string current_session_tag_;
147
148 DISALLOW_COPY_AND_ASSIGN(LocalSessionEventHandlerImpl);
149};
150
151} // namespace sync_sessions
152
153#endif // COMPONENTS_SYNC_SESSIONS_LOCAL_SESSION_EVENT_HANDLER_IMPL_H_