blob: 25519634681c638882486f27ff380dc3578e0680 [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;
40 virtual void Add(std::unique_ptr<sync_pb::SessionSpecifics> specifics) = 0;
41 virtual void Update(
42 std::unique_ptr<sync_pb::SessionSpecifics> specifics) = 0;
43 virtual void Commit() = 0;
44
45 private:
46 DISALLOW_COPY_AND_ASSIGN(WriteBatch);
47 };
48
49 class Delegate {
50 public:
51 virtual ~Delegate();
52 virtual std::unique_ptr<WriteBatch> CreateLocalSessionWriteBatch() = 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 // Analogous to the functions in FaviconCache.
57 virtual void OnPageFaviconUpdated(const GURL& page_url) = 0;
58 virtual void OnFaviconVisited(const GURL& page_url,
59 const GURL& favicon_url) = 0;
60 };
61
Mikel Astiz556bf092018-03-06 13:16:3362 // Raw pointers must not be null and all pointees except |*initial_batch| must
63 // outlive this object. |*initial_batch| may or may not be initially empty
64 // (depending on whether the caller wants to bundle together other writes).
65 // This constructor populates |*initial_batch| to resync local window and tab
66 // information, but does *not* Commit() the batch.
Mikel Astiz069039782018-03-02 11:04:4367 LocalSessionEventHandlerImpl(Delegate* delegate,
68 SyncSessionsClient* sessions_client,
Mikel Astiz556bf092018-03-06 13:16:3369 SyncedSessionTracker* session_tracker,
70 WriteBatch* initial_batch);
Mikel Astiz069039782018-03-02 11:04:4371 ~LocalSessionEventHandlerImpl() override;
72
Mikel Astiz069039782018-03-02 11:04:4373 // LocalSessionEventHandler implementation.
74 void OnLocalTabModified(SyncedTabDelegate* modified_tab) override;
75 void OnFaviconsChanged(const std::set<GURL>& page_urls,
76 const GURL& icon_url) override;
77
Mikel Astizbef1f992018-04-10 10:42:5578 // Returns tab specifics from |tab_delegate|. Exposed publicly for testing.
79 sync_pb::SessionTab GetTabSpecificsFromDelegateForTest(
80 const SyncedTabDelegate& tab_delegate) const;
Mikel Astiz069039782018-03-02 11:04:4381
82 private:
83 enum ReloadTabsOption { RELOAD_TABS, DONT_RELOAD_TABS };
84 void AssociateWindows(ReloadTabsOption option,
85 bool has_tabbed_window,
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 Astiz069039782018-03-02 11:04:4390 // changes for processing later. Will only assign a new sync id if there is
91 // a tabbed window, which results in failure for tabs without sync ids yet.
92 void AssociateTab(SyncedTabDelegate* const tab,
93 bool has_tabbed_window,
Mikel Astiz556bf092018-03-06 13:16:3394 WriteBatch* batch);
Mikel Astiz069039782018-03-02 11:04:4395
96 // It's possible that when we associate windows, tabs aren't all loaded
97 // into memory yet (e.g on android) and we don't have a WebContents. In this
98 // case we can't do a full association, but we still want to update tab IDs
99 // as they may have changed after a session was restored. This method
100 // compares new_tab_id and new_window_id against the previously persisted tab
101 // ID and window ID (from our TabNodePool) and updates them if either differs.
102 void AssociateRestoredPlaceholderTab(const SyncedTabDelegate& tab_delegate,
Mikel Astize023c572018-03-28 07:56:56103 SessionID new_tab_id,
104 SessionID new_window_id,
Mikel Astiz556bf092018-03-06 13:16:33105 WriteBatch* batch);
Mikel Astiz069039782018-03-02 11:04:43106
Mikel Astiz556bf092018-03-06 13:16:33107 // Appends an ACTION_UPDATE for a sync tab entity onto |batch| to
Mikel Astiz069039782018-03-02 11:04:43108 // reflect the contents of |tab|, given the tab node id |sync_id|.
109 void AppendChangeForExistingTab(int sync_id,
110 const sessions::SessionTab& tab,
Mikel Astiz556bf092018-03-06 13:16:33111 WriteBatch* batch) const;
Mikel Astiz069039782018-03-02 11:04:43112
Mikel Astizbef1f992018-04-10 10:42:55113 // Set |session_tab| from |tab_delegate|.
114 sync_pb::SessionTab GetTabSpecificsFromDelegate(
115 const SyncedTabDelegate& tab_delegate) const;
Mikel Astiz069039782018-03-02 11:04:43116
117 // Updates task tracker with the navigations of |tab_delegate|.
118 void UpdateTaskTracker(SyncedTabDelegate* const tab_delegate);
119
120 // Update |tab_specifics| with the corresponding task ids.
121 void WriteTasksIntoSpecifics(sync_pb::SessionTab* tab_specifics);
122
123 // On Android, it's possible to not have any tabbed windows when only custom
124 // tabs are currently open. This means that there is tab data that will be
125 // restored later, but we cannot access it. This method is an elaborate way to
126 // check if we're currently in that state or not.
127 bool ScanForTabbedWindow();
128
129 // Injected dependencies (not owned).
130 Delegate* const delegate_;
131 SyncSessionsClient* const sessions_client_;
132 SyncedSessionTracker* const session_tracker_;
133
Mikel Astiz069039782018-03-02 11:04:43134 // Tracks Chrome Tasks, which associates navigations, with tab and navigation
135 // changes of current session.
136 TaskTracker task_tracker_;
137
138 std::string current_session_tag_;
139
140 DISALLOW_COPY_AND_ASSIGN(LocalSessionEventHandlerImpl);
141};
142
143} // namespace sync_sessions
144
145#endif // COMPONENTS_SYNC_SESSIONS_LOCAL_SESSION_EVENT_HANDLER_IMPL_H_