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; |
| 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 Astiz | 556bf09 | 2018-03-06 13:16:33 | [diff] [blame] | 53 | // Analogous to SessionsGlobalIdMapper. |
| 54 | virtual void TrackLocalNavigationId(base::Time timestamp, |
| 55 | int unique_id) = 0; |
Mikel Astiz | 06903978 | 2018-03-02 11:04:43 | [diff] [blame] | 56 | // 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 Astiz | 556bf09 | 2018-03-06 13:16:33 | [diff] [blame] | 62 | // 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 Astiz | 06903978 | 2018-03-02 11:04:43 | [diff] [blame] | 67 | LocalSessionEventHandlerImpl(Delegate* delegate, |
| 68 | SyncSessionsClient* sessions_client, |
Mikel Astiz | 556bf09 | 2018-03-06 13:16:33 | [diff] [blame] | 69 | SyncedSessionTracker* session_tracker, |
| 70 | WriteBatch* initial_batch); |
Mikel Astiz | 06903978 | 2018-03-02 11:04:43 | [diff] [blame] | 71 | ~LocalSessionEventHandlerImpl() override; |
| 72 | |
Mikel Astiz | 06903978 | 2018-03-02 11:04:43 | [diff] [blame] | 73 | // 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 Astiz | bef1f99 | 2018-04-10 10:42:55 | [diff] [blame^] | 78 | // Returns tab specifics from |tab_delegate|. Exposed publicly for testing. |
| 79 | sync_pb::SessionTab GetTabSpecificsFromDelegateForTest( |
| 80 | const SyncedTabDelegate& tab_delegate) const; |
Mikel Astiz | 06903978 | 2018-03-02 11:04:43 | [diff] [blame] | 81 | |
| 82 | private: |
| 83 | enum ReloadTabsOption { RELOAD_TABS, DONT_RELOAD_TABS }; |
| 84 | void AssociateWindows(ReloadTabsOption option, |
| 85 | bool has_tabbed_window, |
Mikel Astiz | 556bf09 | 2018-03-06 13:16:33 | [diff] [blame] | 86 | WriteBatch* batch); |
Mikel Astiz | 06903978 | 2018-03-02 11:04:43 | [diff] [blame] | 87 | |
| 88 | // Loads and reassociates the local tab referenced in |tab|. |
Mikel Astiz | 556bf09 | 2018-03-06 13:16:33 | [diff] [blame] | 89 | // |batch| must not be null. This function will append necessary |
Mikel Astiz | 06903978 | 2018-03-02 11:04:43 | [diff] [blame] | 90 | // 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 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 | // 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 Astiz | e023c57 | 2018-03-28 07:56:56 | [diff] [blame] | 103 | SessionID new_tab_id, |
| 104 | SessionID new_window_id, |
Mikel Astiz | 556bf09 | 2018-03-06 13:16:33 | [diff] [blame] | 105 | WriteBatch* batch); |
Mikel Astiz | 06903978 | 2018-03-02 11:04:43 | [diff] [blame] | 106 | |
Mikel Astiz | 556bf09 | 2018-03-06 13:16:33 | [diff] [blame] | 107 | // Appends an ACTION_UPDATE for a sync tab entity onto |batch| to |
Mikel Astiz | 06903978 | 2018-03-02 11:04:43 | [diff] [blame] | 108 | // reflect the contents of |tab|, given the tab node id |sync_id|. |
| 109 | void AppendChangeForExistingTab(int sync_id, |
| 110 | const sessions::SessionTab& tab, |
Mikel Astiz | 556bf09 | 2018-03-06 13:16:33 | [diff] [blame] | 111 | WriteBatch* batch) const; |
Mikel Astiz | 06903978 | 2018-03-02 11:04:43 | [diff] [blame] | 112 | |
Mikel Astiz | bef1f99 | 2018-04-10 10:42:55 | [diff] [blame^] | 113 | // Set |session_tab| from |tab_delegate|. |
| 114 | sync_pb::SessionTab GetTabSpecificsFromDelegate( |
| 115 | const SyncedTabDelegate& tab_delegate) const; |
Mikel Astiz | 06903978 | 2018-03-02 11:04:43 | [diff] [blame] | 116 | |
| 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 Astiz | 06903978 | 2018-03-02 11:04:43 | [diff] [blame] | 134 | // 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_ |