OLD | NEW |
1 // Copyright 2012 The Chromium Authors. All rights reserved. | 1 // Copyright 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef COMPONENTS_SYNC_SESSIONS_SYNCED_SESSION_TRACKER_H_ | 5 #ifndef COMPONENTS_SYNC_SESSIONS_SYNCED_SESSION_TRACKER_H_ |
6 #define COMPONENTS_SYNC_SESSIONS_SYNCED_SESSION_TRACKER_H_ | 6 #define COMPONENTS_SYNC_SESSIONS_SYNCED_SESSION_TRACKER_H_ |
7 | 7 |
8 #include <stddef.h> | 8 #include <stddef.h> |
9 | 9 |
10 #include <map> | 10 #include <map> |
(...skipping 22 matching lines...) Expand all Loading... |
33 public: | 33 public: |
34 // Different ways to lookup/filter tabs. | 34 // Different ways to lookup/filter tabs. |
35 enum SessionLookup { | 35 enum SessionLookup { |
36 RAW, // Return all foreign sessions. | 36 RAW, // Return all foreign sessions. |
37 PRESENTABLE // Have one window with at least one tab with syncable content. | 37 PRESENTABLE // Have one window with at least one tab with syncable content. |
38 }; | 38 }; |
39 | 39 |
40 explicit SyncedSessionTracker(SyncSessionsClient* sessions_client); | 40 explicit SyncedSessionTracker(SyncSessionsClient* sessions_client); |
41 ~SyncedSessionTracker(); | 41 ~SyncedSessionTracker(); |
42 | 42 |
43 // **** Synced session/tab query methods. **** | 43 // We track and distinguish the local session from foreign sessions. |
| 44 void SetLocalSessionTag(const std::string& local_session_tag); |
44 | 45 |
45 // Fill a preallocated vector with all foreign sessions we're tracking (skips | 46 // Fill a preallocated vector with all foreign sessions we're tracking (skips |
46 // the local session object). SyncedSession ownership remains within the | 47 // the local session object). SyncedSession ownership remains within the |
47 // SyncedSessionTracker. Lookup parameter is used to decide which foreign tabs | 48 // SyncedSessionTracker. Lookup parameter is used to decide which foreign tabs |
48 // should be include. | 49 // should be include. |
49 // Returns true if we had foreign sessions to fill it with, false otherwise. | 50 // Returns true if we had foreign sessions to fill it with, false otherwise. |
50 bool LookupAllForeignSessions(std::vector<const SyncedSession*>* sessions, | 51 bool LookupAllForeignSessions(std::vector<const SyncedSession*>* sessions, |
51 SessionLookup lookup) const; | 52 SessionLookup lookup) const; |
52 | 53 |
53 // Fills |tab_node_ids| with the tab node ids (see GetTab) for all the tabs* | |
54 // associated with the session having tag |session_tag|. | |
55 void LookupForeignTabNodeIds(const std::string& session_tag, | |
56 std::set<int>* tab_node_ids) const; | |
57 | |
58 // Attempts to look up the session windows associatd with the session given | 54 // Attempts to look up the session windows associatd with the session given |
59 // by |session_tag|. Ownership of SessionWindows stays within the | 55 // by |session_tag|. Ownership of SessionWindows stays within the |
60 // SyncedSessionTracker. | 56 // SyncedSessionTracker. |
61 // If lookup succeeds: | 57 // If lookup succeeds: |
62 // - Fills windows with the SessionWindow pointers, returns true. | 58 // - Fills windows with the SessionWindow pointers, returns true. |
63 // Else | 59 // Else |
64 // - Returns false. | 60 // - Returns false. |
65 bool LookupSessionWindows( | 61 bool LookupSessionWindows( |
66 const std::string& session_tag, | 62 const std::string& session_tag, |
67 std::vector<const sessions::SessionWindow*>* windows) const; | 63 std::vector<const sessions::SessionWindow*>* windows) const; |
68 | 64 |
69 // Attempts to look up the tab associated with the given tag and tab id. | 65 // Attempts to look up the tab associated with the given tag and tab id. |
70 // Ownership of the SessionTab remains within the SyncedSessionTracker. | 66 // Ownership of the SessionTab remains within the SyncedSessionTracker. |
71 // If lookup succeeds: | 67 // If lookup succeeds: |
72 // - Sets tab to point to the SessionTab, and returns true. | 68 // - Sets tab to point to the SessionTab, and returns true. |
73 // Else | 69 // Else |
74 // - Returns false, tab is set to null. | 70 // - Returns false, tab is set to null. |
75 bool LookupSessionTab(const std::string& session_tag, | 71 bool LookupSessionTab(const std::string& session_tag, |
76 SessionID::id_type tab_id, | 72 SessionID::id_type tab_id, |
77 const sessions::SessionTab** tab) const; | 73 const sessions::SessionTab** tab) const; |
78 | 74 |
79 // Allows retrieval of existing data for the local session. Unlike GetSession | 75 // Allows retrieval of existing data for the local session. Unlike GetSession |
80 // this won't create-if-not-present. | 76 // this won't create-if-not-present. |
81 bool LookupLocalSession(const SyncedSession** output) const; | 77 bool LookupLocalSession(const SyncedSession** output) const; |
82 | 78 |
83 // **** Methods for manipulating synced sessions and tabs. **** | |
84 | |
85 // Returns a pointer to the SyncedSession object associated with | 79 // Returns a pointer to the SyncedSession object associated with |
86 // |session_tag|. If none exists, creates one. Ownership of the | 80 // |session_tag|. If none exists, creates one. Ownership of the |
87 // SyncedSession remains within the SyncedSessionTracker. | 81 // SyncedSession remains within the SyncedSessionTracker. |
88 SyncedSession* GetSession(const std::string& session_tag); | 82 SyncedSession* GetSession(const std::string& session_tag); |
89 | 83 |
| 84 // Deletes the session associated with |session_tag| if it exists. |
| 85 // Returns true if the session existed and was deleted, false otherwise. |
| 86 bool DeleteSession(const std::string& session_tag); |
| 87 |
90 // Resets the tracking information for the session specified by |session_tag|. | 88 // Resets the tracking information for the session specified by |session_tag|. |
91 // This involves clearing all the windows and tabs from the session, while | 89 // This involves clearing all the windows and tabs from the session, while |
92 // keeping pointers saved in the synced_window_map_ and synced_tab_map_. Once | 90 // keeping pointers saved in the synced_window_map_ and synced_tab_map_. Once |
93 // reset, all calls to PutWindowInSession and PutTabInWindow will denote that | 91 // reset, all calls to PutWindowInSession and PutTabInWindow will denote that |
94 // the requested windows and tabs are owned and add them back to their | 92 // the requested windows and tabs are owned and add them back to their |
95 // session. The next call to CleanupSession(...) will delete those windows and | 93 // session. The next call to CleanupSession(...) will delete those windows and |
96 // tabs not owned. | 94 // tabs not owned. |
97 void ResetSessionTracking(const std::string& session_tag); | 95 void ResetSessionTracking(const std::string& session_tag); |
98 | 96 |
99 // Adds the window with id |window_id| to the session specified by | |
100 // |session_tag|. If none existed for that session, creates one. Similarly, if | |
101 // the session did not exist yet, creates it. Ownership of the SessionWindow | |
102 // remains within the SyncedSessionTracker. | |
103 void PutWindowInSession(const std::string& session_tag, | |
104 SessionID::id_type window_id); | |
105 | |
106 // Adds the tab with id |tab_id| to the window |window_id|. If none existed | |
107 // for that session, creates one. Ownership of the SessionTab remains within | |
108 // the SyncedSessionTracker. | |
109 // | |
110 // Note: GetSession(..) must have already been called with |session_tag| to | |
111 // ensure we having mapping information for this session. | |
112 void PutTabInWindow(const std::string& session_tag, | |
113 SessionID::id_type window_id, | |
114 SessionID::id_type tab_id); | |
115 | |
116 // Adds |tab_node_id| to the session specified by |session_tag|, creating that | |
117 // session if necessary. This is necessary to ensure that each session has an | |
118 // up to date list of tab nodes linked to it for session deletion purposes. | |
119 // Note that this won't update the local tab pool, even if the local session | |
120 // tag is passed. The tab pool is only updated with new tab nodes when they're | |
121 // associated with a tab id (see ReassociateLocalTabNode or | |
122 // GetTabNodeFromLocalTabId). | |
123 void OnTabNodeSeen(const std::string& session_tag, int tab_node_id); | |
124 | |
125 // Returns a pointer to the SessionTab object associated with | |
126 // |tab_id| for the session specified with |session_tag|. | |
127 // Note: Ownership of the SessionTab remains within the SyncedSessionTracker. | |
128 // TODO(zea): Replace SessionTab with a Sync specific wrapper. | |
129 // crbug.com/662597 | |
130 sessions::SessionTab* GetTab(const std::string& session_tag, | |
131 SessionID::id_type tab_id); | |
132 | |
133 // **** Methods specific to foreign sessions. **** | |
134 | |
135 // Tracks the deletion of a foreign tab by removing the given |tab_node_id| | 97 // Tracks the deletion of a foreign tab by removing the given |tab_node_id| |
136 // from the parent session. Doesn't actually remove any tab objects because | 98 // from the parent session. Doesn't actually remove any tab objects because |
137 // the header may have or may not have already been updated to no longer | 99 // the header may have or may not have already been updated to no longer |
138 // parent this tab. Regardless, when the header is updated then cleanup will | 100 // parent this tab. Regardless, when the header is updated then cleanup will |
139 // remove the actual tab data. However, this method always needs to be called | 101 // remove the actual tab data. However, this method always needs to be called |
140 // upon foreign tab deletion, otherwise LookupTabNodeIds(...) may return | 102 // upon foreign tab deletion, otherwise LookupTabNodeIds(...) may return |
141 // already deleted tab node ids. | 103 // already deleted tab node ids. |
142 void DeleteForeignTab(const std::string& session_tag, int tab_node_id); | 104 void DeleteForeignTab(const std::string& session_tag, int tab_node_id); |
143 | 105 |
144 // Deletes the session associated with |session_tag| if it exists. | 106 // Deletes those windows and tabs associated with |session_tag| that are no |
145 // Returns true if the session existed and was deleted, false otherwise. | 107 // longer owned. See ResetSessionTracking(...). |
146 bool DeleteForeignSession(const std::string& session_tag); | 108 void CleanupSession(const std::string& session_tag); |
147 | 109 |
148 // Deletes those windows and tabs associated with |session_tag| that are no | 110 // Adds the window with id |window_id| to the session specified by |
149 // longer owned. See ResetSessionTracking(...).. | 111 // |session_tag|. If none existed for that session, creates one. Similarly, if |
150 void CleanupForeignSession(const std::string& session_tag); | 112 // the session did not exist yet, creates it. Ownership of the SessionWindow |
| 113 // remains within the SyncedSessionTracker. |
| 114 void PutWindowInSession(const std::string& session_tag, |
| 115 SessionID::id_type window_id); |
151 | 116 |
152 // **** Methods specific to the local session. **** | 117 // Adds the tab with id |tab_id| to the window |window_id|. If none existed |
| 118 // for that session, creates one. Ownership of the SessionTab remains within |
| 119 // the SyncedSessionTracker. |
| 120 // |
| 121 // Note: GetSession(..) must have already been called with |session_tag| to |
| 122 // ensure we having mapping information for this session. |
| 123 void PutTabInWindow(const std::string& session_tag, |
| 124 SessionID::id_type window_id, |
| 125 SessionID::id_type tab_id, |
| 126 size_t tab_index); |
153 | 127 |
154 // Set the local session tag. Must be called before any other local session | 128 // Returns a pointer to the SessionTab object associated with |tab_id| for |
155 // methods are invoked. | 129 // the session specified with |session_tag|. If none exists, creates one. |
156 void SetLocalSessionTag(const std::string& local_session_tag); | 130 // Ownership of the SessionTab remains within the SyncedSessionTracker. |
| 131 // |tab_node_id| must be a valid node id for the node backing this tab. |
| 132 sessions::SessionTab* GetTab(const std::string& session_tag, |
| 133 SessionID::id_type tab_id, |
| 134 int tab_node_id); |
157 | 135 |
158 // Similar to CleanupForeignSession, but also marks any unmapped tabs as free | 136 // Fills |tab_node_ids| with the tab node ids (see GetTab) for all the tabs* |
159 // in the tab node pool and fills |deleted_node_ids| with the set of locally | 137 // associated with the session having tag |session_tag|. |
160 // free tab nodes to be deleted. | 138 void LookupTabNodeIds(const std::string& session_tag, |
161 void CleanupLocalTabs(std::set<int>* deleted_node_ids); | 139 std::set<int>* tab_node_ids); |
162 | |
163 // Fills |tab_node_id| with a tab node for |tab_id|. Returns true if an | |
164 // existing tab node was found, false if there was none and one had to be | |
165 // created. | |
166 bool GetTabNodeFromLocalTabId(SessionID::id_type tab_id, int* tab_node_id); | |
167 | |
168 // Returns whether |tab_node_id| refers to a valid tab node that is associated | |
169 // with a tab. | |
170 bool IsLocalTabNodeAssociated(int tab_node_id); | |
171 | |
172 // Reassociates the tab denoted by |tab_node_id| with a new tab id, preserving | |
173 // any previous SessionTab object the node was associated with. This is useful | |
174 // on restart when sync needs to reassociate tabs from a previous session with | |
175 // newly restored tabs (and can be used in conjunction with PutTabInWindow). | |
176 void ReassociateLocalTab(int tab_node_id, SessionID::id_type new_tab_id); | |
177 | |
178 // **** Methods for querying/manipulating overall state ****. | |
179 | 140 |
180 // Free the memory for all dynamically allocated objects and clear the | 141 // Free the memory for all dynamically allocated objects and clear the |
181 // tracking structures. | 142 // tracking structures. |
182 void Clear(); | 143 void Clear(); |
183 | 144 |
184 bool Empty() const { | 145 bool Empty() const { |
185 return synced_tab_map_.empty() && synced_session_map_.empty(); | 146 return synced_tab_map_.empty() && synced_session_map_.empty(); |
186 } | 147 } |
187 | 148 |
188 // Includes both foreign sessions and the local session. | 149 // Includes both foreign sessions and the local session. |
189 size_t num_synced_sessions() const { return synced_session_map_.size(); } | 150 size_t num_synced_sessions() const { return synced_session_map_.size(); } |
190 | 151 |
191 // Returns the number of tabs associated with the specified session tag. | 152 // Returns the number of tabs associated with the specified session tag. |
192 size_t num_synced_tabs(const std::string& session_tag) const { | 153 size_t num_synced_tabs(const std::string& session_tag) const { |
193 auto iter = synced_tab_map_.find(session_tag); | 154 auto iter = synced_tab_map_.find(session_tag); |
194 if (iter != synced_tab_map_.end()) { | 155 if (iter != synced_tab_map_.end()) { |
195 return iter->second.size(); | 156 return iter->second.size(); |
196 } else { | 157 } else { |
197 return 0; | 158 return 0; |
198 } | 159 } |
199 } | 160 } |
200 | 161 |
201 // Returns whether a tab is unmapped or not. | |
202 bool IsTabUnmappedForTesting(SessionID::id_type tab_id); | |
203 | |
204 private: | 162 private: |
205 friend class SessionsSyncManagerTest; | 163 // Implementation for GetTab(...) above, permits invalid tab_node_id. |
206 friend class SyncedSessionTrackerTest; | 164 sessions::SessionTab* GetTabImpl(const std::string& session_tag, |
207 | 165 SessionID::id_type tab_id, |
208 // Implementation of CleanupForeignSession/CleanupLocalTabs. | 166 int tab_node_id); |
209 void CleanupSessionImpl(const std::string& session_tag); | |
210 | 167 |
211 // The client of the sync sessions datatype. | 168 // The client of the sync sessions datatype. |
212 SyncSessionsClient* const sessions_client_; | 169 SyncSessionsClient* const sessions_client_; |
213 | 170 |
214 // The mapping of tab/window to their SessionTab/SessionWindow objects. | 171 // The mapping of tab/window ids to their SessionTab/SessionWindow objects. |
215 // The SessionTab/SessionWindow objects referred to may be owned either by the | 172 // The SessionTab/SessionWindow objects referred to may be owned either by the |
216 // session in the |synced_session_map_| or be temporarily unmapped and live in | 173 // session in the |synced_session_map_| or be temporarily unmapped and live in |
217 // the |unmapped_tabs_|/|unmapped_windows_| collections. | 174 // the |unmapped_tabs_|/|unmapped_windows_| collections. |
218 // | 175 // |
219 // Map: session tag -> (tab/window -> SessionTab*/SessionWindow*) | 176 // Map: session tag -> (tab/window id -> SessionTab*/SessionWindow*) |
220 std::map<std::string, std::map<SessionID::id_type, sessions::SessionTab*>> | 177 std::map<std::string, std::map<SessionID::id_type, sessions::SessionTab*>> |
221 synced_tab_map_; | 178 synced_tab_map_; |
222 std::map<std::string, std::map<SessionID::id_type, sessions::SessionWindow*>> | 179 std::map<std::string, std::map<SessionID::id_type, sessions::SessionWindow*>> |
223 synced_window_map_; | 180 synced_window_map_; |
224 | 181 |
225 // The collection that owns the SyncedSessions, and transitively, all of the | 182 // The collection that owns the SyncedSessions, and transitively, all of the |
226 // windows and tabs they contain. | 183 // windows and tabs they contain. |
227 // | 184 // |
228 // Map: session tag -> owned SyncedSession | 185 // Map: session tag -> owned SyncedSession |
229 std::map<std::string, std::unique_ptr<SyncedSession>> synced_session_map_; | 186 std::map<std::string, std::unique_ptr<SyncedSession>> synced_session_map_; |
230 | 187 |
231 // The collection of tabs/windows not owned by a SyncedSession. This is the | 188 // The collection of tabs/windows not owned by a SyncedSession. This is the |
232 // case either because 1. (in the case of tabs) they were newly created by | 189 // case either because 1. (in the case of tabs) they were newly created by |
233 // GetTab() and not yet added to a session, or 2. they were removed from their | 190 // GetTab() and not yet added to a session, or 2. they were removed from their |
234 // owning session by a call to ResetSessionTracking() and not yet added back. | 191 // owning session by a call to ResetSessionTracking() and not yet added back. |
235 // | 192 // |
236 // Map: session tag -> (tab/window id -> owned SessionTab/SessionWindow) | 193 // Map: session tag -> (tab/window id -> owned SessionTab/SessionWindow) |
237 std::map<std::string, | 194 std::map<std::string, |
238 std::map<SessionID::id_type, std::unique_ptr<sessions::SessionTab>>> | 195 std::map<SessionID::id_type, std::unique_ptr<sessions::SessionTab>>> |
239 unmapped_tabs_; | 196 unmapped_tabs_; |
240 std::map< | 197 std::map< |
241 std::string, | 198 std::string, |
242 std::map<SessionID::id_type, std::unique_ptr<sessions::SessionWindow>>> | 199 std::map<SessionID::id_type, std::unique_ptr<sessions::SessionWindow>>> |
243 unmapped_windows_; | 200 unmapped_windows_; |
244 | 201 |
245 // The tag for this machine's local session, so we can distinguish the foreign | 202 // The tag for this machine's local session, so we can distinguish the foreign |
246 // sessions. | 203 // sessions. |
247 std::string local_session_tag_; | 204 std::string local_session_tag_; |
248 | 205 |
249 // Pool of used/available sync nodes associated with local tabs. | |
250 TabNodePool local_tab_pool_; | |
251 | |
252 DISALLOW_COPY_AND_ASSIGN(SyncedSessionTracker); | 206 DISALLOW_COPY_AND_ASSIGN(SyncedSessionTracker); |
253 }; | 207 }; |
254 | 208 |
255 } // namespace sync_sessions | 209 } // namespace sync_sessions |
256 | 210 |
257 #endif // COMPONENTS_SYNC_SESSIONS_SYNCED_SESSION_TRACKER_H_ | 211 #endif // COMPONENTS_SYNC_SESSIONS_SYNCED_SESSION_TRACKER_H_ |
OLD | NEW |