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