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" |
Mikel Astiz | 06903978 | 2018-03-02 11:04:43 | [diff] [blame] | 18 | |
| 19 | namespace sync_pb { |
| 20 | class SessionSpecifics; |
| 21 | class SessionTab; |
| 22 | } // namespace sync_pb |
| 23 | |
| 24 | namespace sync_sessions { |
| 25 | |
| 26 | class SyncedSessionTracker; |
| 27 | class SyncedTabDelegate; |
David Maunder | f476abb | 2019-07-15 17:22:15 | [diff] [blame^] | 28 | class SyncSessionsClient; |
Mikel Astiz | 06903978 | 2018-03-02 11:04:43 | [diff] [blame] | 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 | 3cb0ad3 | 2018-12-03 09:16:06 | [diff] [blame] | 51 | virtual bool IsTabNodeUnsynced(int tab_node_id) = 0; |
Mikel Astiz | 556bf09 | 2018-03-06 13:16:33 | [diff] [blame] | 52 | // Analogous to SessionsGlobalIdMapper. |
| 53 | virtual void TrackLocalNavigationId(base::Time timestamp, |
| 54 | int unique_id) = 0; |
Mikel Astiz | 06903978 | 2018-03-02 11:04:43 | [diff] [blame] | 55 | // Analogous to the functions in FaviconCache. |
| 56 | virtual void OnPageFaviconUpdated(const GURL& page_url) = 0; |
| 57 | virtual void OnFaviconVisited(const GURL& page_url, |
| 58 | const GURL& favicon_url) = 0; |
| 59 | }; |
| 60 | |
Mikel Astiz | beeed8b | 2018-06-07 06:55:46 | [diff] [blame] | 61 | // Raw pointers must not be null and all pointees must outlive this object. |
| 62 | // A side effect of this constructor could include (unless session restore is |
| 63 | // ongoing) the creation of a write batch (via |delegate| and committing |
| 64 | // changes). |
Mikel Astiz | 06903978 | 2018-03-02 11:04:43 | [diff] [blame] | 65 | LocalSessionEventHandlerImpl(Delegate* delegate, |
| 66 | SyncSessionsClient* sessions_client, |
Mikel Astiz | beeed8b | 2018-06-07 06:55:46 | [diff] [blame] | 67 | SyncedSessionTracker* session_tracker); |
Mikel Astiz | 06903978 | 2018-03-02 11:04:43 | [diff] [blame] | 68 | ~LocalSessionEventHandlerImpl() override; |
| 69 | |
Mikel Astiz | 06903978 | 2018-03-02 11:04:43 | [diff] [blame] | 70 | // LocalSessionEventHandler implementation. |
Mikel Astiz | beeed8b | 2018-06-07 06:55:46 | [diff] [blame] | 71 | void OnSessionRestoreComplete() override; |
Mikel Astiz | 06903978 | 2018-03-02 11:04:43 | [diff] [blame] | 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 | |
Mikel Astiz | 3cb0ad3 | 2018-12-03 09:16:06 | [diff] [blame] | 83 | void CleanupLocalTabs(WriteBatch* batch); |
| 84 | |
Mikel Astiz | 06903978 | 2018-03-02 11:04:43 | [diff] [blame] | 85 | void AssociateWindows(ReloadTabsOption option, |
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 | e855475 | 2018-06-20 10:39:44 | [diff] [blame] | 90 | // changes for processing later. |
Mikel Astiz | 06903978 | 2018-03-02 11:04:43 | [diff] [blame] | 91 | void AssociateTab(SyncedTabDelegate* const tab, |
Mikel Astiz | 556bf09 | 2018-03-06 13:16:33 | [diff] [blame] | 92 | WriteBatch* batch); |
Mikel Astiz | 06903978 | 2018-03-02 11:04:43 | [diff] [blame] | 93 | |
Mikel Astiz | bef1f99 | 2018-04-10 10:42:55 | [diff] [blame] | 94 | // Set |session_tab| from |tab_delegate|. |
| 95 | sync_pb::SessionTab GetTabSpecificsFromDelegate( |
| 96 | const SyncedTabDelegate& tab_delegate) const; |
Mikel Astiz | 06903978 | 2018-03-02 11:04:43 | [diff] [blame] | 97 | |
Mikel Astiz | 06903978 | 2018-03-02 11:04:43 | [diff] [blame] | 98 | // Update |tab_specifics| with the corresponding task ids. |
David Maunder | f476abb | 2019-07-15 17:22:15 | [diff] [blame^] | 99 | static void WriteTasksIntoSpecifics(sync_pb::SessionTab* tab_specifics, |
| 100 | SyncedTabDelegate* tab_delegate); |
Mikel Astiz | 06903978 | 2018-03-02 11:04:43 | [diff] [blame] | 101 | |
Mikel Astiz | 06903978 | 2018-03-02 11:04:43 | [diff] [blame] | 102 | // Injected dependencies (not owned). |
| 103 | Delegate* const delegate_; |
| 104 | SyncSessionsClient* const sessions_client_; |
| 105 | SyncedSessionTracker* const session_tracker_; |
| 106 | |
Mikel Astiz | 06903978 | 2018-03-02 11:04:43 | [diff] [blame] | 107 | std::string current_session_tag_; |
| 108 | |
| 109 | DISALLOW_COPY_AND_ASSIGN(LocalSessionEventHandlerImpl); |
| 110 | }; |
| 111 | |
| 112 | } // namespace sync_sessions |
| 113 | |
| 114 | #endif // COMPONENTS_SYNC_SESSIONS_LOCAL_SESSION_EVENT_HANDLER_IMPL_H_ |