blob: 9272381da0bf6d09dc495a86d9f8bbd77862a49a [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 Astizbeeed8b2018-06-07 06:55:4660 // Raw pointers must not be null and all pointees must outlive this object.
61 // A side effect of this constructor could include (unless session restore is
62 // ongoing) the creation of a write batch (via |delegate| and committing
63 // changes).
Mikel Astiz069039782018-03-02 11:04:4364 LocalSessionEventHandlerImpl(Delegate* delegate,
65 SyncSessionsClient* sessions_client,
Mikel Astizbeeed8b2018-06-07 06:55:4666 SyncedSessionTracker* session_tracker);
Mikel Astiz069039782018-03-02 11:04:4367 ~LocalSessionEventHandlerImpl() override;
68
Mikel Astiz069039782018-03-02 11:04:4369 // LocalSessionEventHandler implementation.
Mikel Astizbeeed8b2018-06-07 06:55:4670 void OnSessionRestoreComplete() override;
Mikel Astiz069039782018-03-02 11:04:4371 void OnLocalTabModified(SyncedTabDelegate* modified_tab) override;
72 void OnFaviconsChanged(const std::set<GURL>& page_urls,
73 const GURL& icon_url) override;
74
Mikel Astizbef1f992018-04-10 10:42:5575 // Returns tab specifics from |tab_delegate|. Exposed publicly for testing.
76 sync_pb::SessionTab GetTabSpecificsFromDelegateForTest(
77 const SyncedTabDelegate& tab_delegate) const;
Mikel Astiz069039782018-03-02 11:04:4378
79 private:
80 enum ReloadTabsOption { RELOAD_TABS, DONT_RELOAD_TABS };
Mikel Astizbc775d6b2018-05-23 14:49:2181
Mikel Astiz069039782018-03-02 11:04:4382 void AssociateWindows(ReloadTabsOption option,
Mikel Astiz556bf092018-03-06 13:16:3383 WriteBatch* batch);
Mikel Astiz069039782018-03-02 11:04:4384
85 // Loads and reassociates the local tab referenced in |tab|.
Mikel Astiz556bf092018-03-06 13:16:3386 // |batch| must not be null. This function will append necessary
Mikel Astize8554752018-06-20 10:39:4487 // changes for processing later.
Mikel Astiz069039782018-03-02 11:04:4388 void AssociateTab(SyncedTabDelegate* const tab,
Mikel Astiz556bf092018-03-06 13:16:3389 WriteBatch* batch);
Mikel Astiz069039782018-03-02 11:04:4390
91 // It's possible that when we associate windows, tabs aren't all loaded
92 // into memory yet (e.g on android) and we don't have a WebContents. In this
93 // case we can't do a full association, but we still want to update tab IDs
94 // as they may have changed after a session was restored. This method
Mikel Astize8554752018-06-20 10:39:4495 // new_window_id against the previously persisted window ID (from our
96 // TabNodePool) and updates it.
Mikel Astiz069039782018-03-02 11:04:4397 void AssociateRestoredPlaceholderTab(const SyncedTabDelegate& tab_delegate,
Mikel Astiz3308e2e32018-06-19 19:08:4298 SessionID tab_id,
Mikel Astize023c572018-03-28 07:56:5699 SessionID new_window_id,
Mikel Astiz556bf092018-03-06 13:16:33100 WriteBatch* batch);
Mikel Astiz069039782018-03-02 11:04:43101
Mikel Astizbef1f992018-04-10 10:42:55102 // Set |session_tab| from |tab_delegate|.
103 sync_pb::SessionTab GetTabSpecificsFromDelegate(
104 const SyncedTabDelegate& tab_delegate) const;
Mikel Astiz069039782018-03-02 11:04:43105
106 // Updates task tracker with the navigations of |tab_delegate|.
107 void UpdateTaskTracker(SyncedTabDelegate* const tab_delegate);
108
109 // Update |tab_specifics| with the corresponding task ids.
110 void WriteTasksIntoSpecifics(sync_pb::SessionTab* tab_specifics);
111
Mikel Astiz069039782018-03-02 11:04:43112 // Injected dependencies (not owned).
113 Delegate* const delegate_;
114 SyncSessionsClient* const sessions_client_;
115 SyncedSessionTracker* const session_tracker_;
116
Mikel Astiz069039782018-03-02 11:04:43117 // Tracks Chrome Tasks, which associates navigations, with tab and navigation
118 // changes of current session.
119 TaskTracker task_tracker_;
120
121 std::string current_session_tag_;
122
123 DISALLOW_COPY_AND_ASSIGN(LocalSessionEventHandlerImpl);
124};
125
126} // namespace sync_sessions
127
128#endif // COMPONENTS_SYNC_SESSIONS_LOCAL_SESSION_EVENT_HANDLER_IMPL_H_