Mikel Astiz | 06903978 | 2018-03-02 11:04:43 | [diff] [blame] | 1 | // 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 Astiz | 06903978 | 2018-03-02 11:04:43 | [diff] [blame] | 17 | #include "components/sync_sessions/synced_session.h" |
| 18 | #include "components/sync_sessions/task_tracker.h" |
| 19 | |
| 20 | namespace sync_pb { |
| 21 | class SessionSpecifics; |
| 22 | class SessionTab; |
| 23 | } // namespace sync_pb |
| 24 | |
| 25 | namespace sync_sessions { |
| 26 | |
| 27 | class SyncedSessionTracker; |
| 28 | class 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). |
| 33 | class 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 Astiz | ba63cf5 | 2018-05-23 14:01:05 | [diff] [blame] | 40 | virtual void Put(std::unique_ptr<sync_pb::SessionSpecifics> specifics) = 0; |
Mikel Astiz | 06903978 | 2018-03-02 11:04:43 | [diff] [blame] | 41 | 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 Astiz | 556bf09 | 2018-03-06 13:16:33 | [diff] [blame] | 51 | // Analogous to SessionsGlobalIdMapper. |
| 52 | virtual void TrackLocalNavigationId(base::Time timestamp, |
| 53 | int unique_id) = 0; |
Mikel Astiz | 06903978 | 2018-03-02 11:04:43 | [diff] [blame] | 54 | // 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 Astiz | 556bf09 | 2018-03-06 13:16:33 | [diff] [blame] | 60 | // 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 Astiz | 06903978 | 2018-03-02 11:04:43 | [diff] [blame] | 65 | LocalSessionEventHandlerImpl(Delegate* delegate, |
| 66 | SyncSessionsClient* sessions_client, |
Mikel Astiz | 556bf09 | 2018-03-06 13:16:33 | [diff] [blame] | 67 | SyncedSessionTracker* session_tracker, |
| 68 | WriteBatch* initial_batch); |
Mikel Astiz | 06903978 | 2018-03-02 11:04:43 | [diff] [blame] | 69 | ~LocalSessionEventHandlerImpl() override; |
| 70 | |
Mikel Astiz | 06903978 | 2018-03-02 11:04:43 | [diff] [blame] | 71 | // 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 Astiz | bef1f99 | 2018-04-10 10:42:55 | [diff] [blame] | 76 | // Returns tab specifics from |tab_delegate|. Exposed publicly for testing. |
| 77 | sync_pb::SessionTab GetTabSpecificsFromDelegateForTest( |
| 78 | const SyncedTabDelegate& tab_delegate) const; |
Mikel Astiz | 06903978 | 2018-03-02 11:04:43 | [diff] [blame] | 79 | |
| 80 | private: |
| 81 | enum ReloadTabsOption { RELOAD_TABS, DONT_RELOAD_TABS }; |
Mikel Astiz | bc775d6b | 2018-05-23 14:49:21 | [diff] [blame^] | 82 | |
| 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 Astiz | 06903978 | 2018-03-02 11:04:43 | [diff] [blame] | 92 | void AssociateWindows(ReloadTabsOption option, |
| 93 | bool has_tabbed_window, |
Mikel Astiz | 556bf09 | 2018-03-06 13:16:33 | [diff] [blame] | 94 | WriteBatch* batch); |
Mikel Astiz | 06903978 | 2018-03-02 11:04:43 | [diff] [blame] | 95 | |
| 96 | // Loads and reassociates the local tab referenced in |tab|. |
Mikel Astiz | 556bf09 | 2018-03-06 13:16:33 | [diff] [blame] | 97 | // |batch| must not be null. This function will append necessary |
Mikel Astiz | 06903978 | 2018-03-02 11:04:43 | [diff] [blame] | 98 | // 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 Astiz | 556bf09 | 2018-03-06 13:16:33 | [diff] [blame] | 102 | WriteBatch* batch); |
Mikel Astiz | 06903978 | 2018-03-02 11:04:43 | [diff] [blame] | 103 | |
| 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 Astiz | e023c57 | 2018-03-28 07:56:56 | [diff] [blame] | 111 | SessionID new_tab_id, |
| 112 | SessionID new_window_id, |
Mikel Astiz | 556bf09 | 2018-03-06 13:16:33 | [diff] [blame] | 113 | WriteBatch* batch); |
Mikel Astiz | 06903978 | 2018-03-02 11:04:43 | [diff] [blame] | 114 | |
Mikel Astiz | 556bf09 | 2018-03-06 13:16:33 | [diff] [blame] | 115 | // Appends an ACTION_UPDATE for a sync tab entity onto |batch| to |
Mikel Astiz | 06903978 | 2018-03-02 11:04:43 | [diff] [blame] | 116 | // reflect the contents of |tab|, given the tab node id |sync_id|. |
| 117 | void AppendChangeForExistingTab(int sync_id, |
| 118 | const sessions::SessionTab& tab, |
Mikel Astiz | 556bf09 | 2018-03-06 13:16:33 | [diff] [blame] | 119 | WriteBatch* batch) const; |
Mikel Astiz | 06903978 | 2018-03-02 11:04:43 | [diff] [blame] | 120 | |
Mikel Astiz | bef1f99 | 2018-04-10 10:42:55 | [diff] [blame] | 121 | // Set |session_tab| from |tab_delegate|. |
| 122 | sync_pb::SessionTab GetTabSpecificsFromDelegate( |
| 123 | const SyncedTabDelegate& tab_delegate) const; |
Mikel Astiz | 06903978 | 2018-03-02 11:04:43 | [diff] [blame] | 124 | |
| 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 Astiz | 06903978 | 2018-03-02 11:04:43 | [diff] [blame] | 142 | // 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_ |