Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(143)

Side by Side Diff: components/sync_sessions/synced_session_tracker.h

Issue 1877083002: [Sync] Moved tab_node_id tracking to session object and improved foreign session garbage collection. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 4 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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
33 public: 33 public:
34 explicit SyncedSessionTracker( 34 explicit SyncedSessionTracker(
35 sync_sessions::SyncSessionsClient* sessions_client); 35 sync_sessions::SyncSessionsClient* sessions_client);
36 ~SyncedSessionTracker(); 36 ~SyncedSessionTracker();
37 37
38 // We track and distinguish the local session from foreign sessions. 38 // We track and distinguish the local session from foreign sessions.
39 void SetLocalSessionTag(const std::string& local_session_tag); 39 void SetLocalSessionTag(const std::string& local_session_tag);
40 40
41 // Fill a preallocated vector with all foreign sessions we're tracking (skips 41 // Fill a preallocated vector with all foreign sessions we're tracking (skips
42 // the local session object). SyncedSession ownership remains within the 42 // the local session object). SyncedSession ownership remains within the
43 // SyncedSessionTracker. 43 // SyncedSessionTracker. Can specify if you want only presentable sessions,
44 // which means they have at least one window with at least one tab that has
45 // syncable content.
44 // Returns true if we had foreign sessions to fill it with, false otherwise. 46 // Returns true if we had foreign sessions to fill it with, false otherwise.
45 bool LookupAllForeignSessions( 47 bool LookupAllForeignSessions(
46 std::vector<const sync_driver::SyncedSession*>* sessions) const; 48 std::vector<const sync_driver::SyncedSession*>* sessions,
49 bool presentable) const;
Nicolas Zea 2016/04/12 20:38:03 readability nit: prefer defining an enum and using
skym 2016/04/12 22:12:14 Done.
47 50
48 // Attempts to look up the session windows associatd with the session given 51 // Attempts to look up the session windows associatd with the session given
49 // by |session_tag|. Ownership Of SessionWindows stays within the 52 // by |session_tag|. Ownership Of SessionWindows stays within the
50 // SyncedSessionTracker. 53 // SyncedSessionTracker.
51 // If lookup succeeds: 54 // If lookup succeeds:
52 // - Fills windows with the SessionWindow pointers, returns true. 55 // - Fills windows with the SessionWindow pointers, returns true.
53 // Else 56 // Else
54 // - Returns false. 57 // - Returns false.
55 bool LookupSessionWindows( 58 bool LookupSessionWindows(
56 const std::string& session_tag, 59 const std::string& session_tag,
(...skipping 25 matching lines...) Expand all
82 // Resets the tracking information for the session specified by |session_tag|. 85 // Resets the tracking information for the session specified by |session_tag|.
83 // This involves clearing all the windows and tabs from the session, while 86 // This involves clearing all the windows and tabs from the session, while
84 // keeping pointers saved in the synced_window_map_ and synced_tab_map_. 87 // keeping pointers saved in the synced_window_map_ and synced_tab_map_.
85 // Once reset, all calls to PutWindowInSession and PutTabInWindow will denote 88 // Once reset, all calls to PutWindowInSession and PutTabInWindow will denote
86 // that the requested windows and tabs are owned (by setting the boolean 89 // that the requested windows and tabs are owned (by setting the boolean
87 // in their SessionWindowWrapper/SessionTabWrapper to true) and add them back 90 // in their SessionWindowWrapper/SessionTabWrapper to true) and add them back
88 // to their session. The next call to CleanupSession(...) will delete those 91 // to their session. The next call to CleanupSession(...) will delete those
89 // windows and tabs not owned. 92 // windows and tabs not owned.
90 void ResetSessionTracking(const std::string& session_tag); 93 void ResetSessionTracking(const std::string& session_tag);
91 94
95 // Deletes the given tab, primarily by |tab_node_id|. |tab_id| is needed to
96 // find the given tab, but isn't the primary identifier in the case, since
Nicolas Zea 2016/04/12 20:38:03 "in the case" -> "in this case"
skym 2016/04/12 22:12:14 Comment is different now.
97 // there may be multiple tabs shadowing eachother for the given |tab_id|. In
Nicolas Zea 2016/04/12 20:38:03 eachother -> each other Also, it's not clear ot m
skym 2016/04/12 22:12:14 Comment is different now. I've mostly stopped usi
98 // several scenarios we cannot actually delete the tab object, and calling
99 // this method will simply remove the given |tab_node_id|. If the tab is
Nicolas Zea 2016/04/12 20:38:03 will remove |tab_node_id| from where?
skym 2016/04/12 22:12:14 In this patch set it was from the SessionTabWrappe
100 // completely unowned and shadowing no other tab_node_ids, then this will
101 // result in an actual deletion.
102 void TryDeleteTab(const std::string& session_tag,
103 SessionID::id_type tab_id,
104 int tab_node_id);
105
92 // Deletes those windows and tabs associated with |session_tag| that are no 106 // Deletes those windows and tabs associated with |session_tag| that are no
93 // longer owned. 107 // longer owned. Should typically only be used for local data, since the pool
108 // will still be tracking identifiers. If this is ever run on a foreign
109 // session we lose the ability to correctly garbage collect.
94 // See ResetSessionTracking(...). 110 // See ResetSessionTracking(...).
95 void CleanupSession(const std::string& session_tag); 111 void CleanupSession(const std::string& session_tag);
96 112
97 // Adds the window with id |window_id| to the session specified by 113 // Adds the window with id |window_id| to the session specified by
98 // |session_tag|, and markes the window as being owned. If none existed for 114 // |session_tag|, and markes the window as being owned. If none existed for
99 // that session, creates one. Similarly, if the session did not exist yet, 115 // that session, creates one. Similarly, if the session did not exist yet,
100 // creates it. Ownership of the SessionWindow remains within the 116 // creates it. Ownership of the SessionWindow remains within the
101 // SyncedSessionTracker. 117 // SyncedSessionTracker.
102 void PutWindowInSession(const std::string& session_tag, 118 void PutWindowInSession(const std::string& session_tag,
103 SessionID::id_type window_id); 119 SessionID::id_type window_id);
(...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after
174 // have ownership of the Windows/Tabs, they just provide id-based access to 190 // have ownership of the Windows/Tabs, they just provide id-based access to
175 // them. The ownership remains within its containing session (for windows and 191 // them. The ownership remains within its containing session (for windows and
176 // mapped tabs, unmapped tabs are owned by the unmapped_tabs_ container). 192 // mapped tabs, unmapped tabs are owned by the unmapped_tabs_ container).
177 // Note, we pair pointers with bools so that we can track what is owned and 193 // Note, we pair pointers with bools so that we can track what is owned and
178 // what can be deleted (see ResetSessionTracking(..) and CleanupSession(..) 194 // what can be deleted (see ResetSessionTracking(..) and CleanupSession(..)
179 // above). 195 // above).
180 // The wrappers also serve as a convenient place to augment state stored in 196 // The wrappers also serve as a convenient place to augment state stored in
181 // SessionTab for sync purposes, such as |tab_node_id|. 197 // SessionTab for sync purposes, such as |tab_node_id|.
182 // IsOwned is used as a wrapper constructor parameter for readability. 198 // IsOwned is used as a wrapper constructor parameter for readability.
183 struct SessionTabWrapper { 199 struct SessionTabWrapper {
184 SessionTabWrapper() 200 SessionTabWrapper();
185 : tab_ptr(NULL), 201
186 owned(false),
187 tab_node_id(TabNodePool::kInvalidTabNodeID) {}
188 SessionTabWrapper(sessions::SessionTab* tab_ptr, 202 SessionTabWrapper(sessions::SessionTab* tab_ptr,
189 OwnedState owned, 203 OwnedState owned,
190 int tab_node_id) 204 int tab_node_id);
191 : tab_ptr(tab_ptr), 205
192 owned(owned == IS_OWNED), 206 ~SessionTabWrapper();
193 tab_node_id(tab_node_id) {} 207
194 sessions::SessionTab* tab_ptr; 208 sessions::SessionTab* tab_ptr;
195 209
196 // This is used as part of a mark-and-sweep approach to garbage 210 // This is used as part of a mark-and-sweep approach to garbage
197 // collection for closed tabs that are no longer "in use", or "owned". 211 // collection for closed tabs that are no longer "in use", or "owned".
198 // ResetSessionTracking will clear |owned| bits, and if it is not claimed 212 // ResetSessionTracking will clear |owned| bits, and if it is not claimed
199 // by a window by the time CleanupSession is called it will be deleted. 213 // by a window by the time CleanupSession is called it will be deleted.
200 bool owned; 214 bool owned;
201 215
202 // This lets us identify the sync node that is "backing" this tab in the 216 // This lets us identify the sync node that is "backing" this tab in the
203 // sync model, whether it is part of a local or foreign session. The 217 // sync model, whether it is part of a local or foreign session. The
204 // "tab node id" is described in session_specifics.proto. 218 // "tab node id" is described in session_specifics.proto. Sometimes there
205 int tab_node_id; 219 // may be multiple pieces of sync data that claim to have the same tab_id.
220 // In this case we remember each tab_node_id in this set in case we need
221 // to modify them. It's possible for multiple different tab_ids to use the
222 // same tab_node_id when the remote client is restarted during the local
223 // clients lifetime.
Nicolas Zea 2016/04/12 20:38:03 I find this paragraph pretty tough to parse. Could
skym 2016/04/12 22:12:14 Moved this to SyncedSession. Added a comment point
224 std::set<int> tab_node_ids;
206 }; 225 };
207 typedef std::map<SessionID::id_type, SessionTabWrapper> IDToSessionTabMap; 226 typedef std::map<SessionID::id_type, SessionTabWrapper> IDToSessionTabMap;
208 typedef std::map<std::string, IDToSessionTabMap> SyncedTabMap; 227 typedef std::map<std::string, IDToSessionTabMap> SyncedTabMap;
209 228
210 struct SessionWindowWrapper { 229 struct SessionWindowWrapper {
211 SessionWindowWrapper() : window_ptr(NULL), owned(false) {} 230 SessionWindowWrapper() : window_ptr(NULL), owned(false) {}
212 SessionWindowWrapper(sessions::SessionWindow* window_ptr, OwnedState owned) 231 SessionWindowWrapper(sessions::SessionWindow* window_ptr, OwnedState owned)
213 : window_ptr(window_ptr), owned(owned == IS_OWNED) {} 232 : window_ptr(window_ptr), owned(owned == IS_OWNED) {}
214 sessions::SessionWindow* window_ptr; 233 sessions::SessionWindow* window_ptr;
215 bool owned; 234 bool owned;
216 }; 235 };
217 typedef std::map<SessionID::id_type, SessionWindowWrapper> 236 typedef std::map<SessionID::id_type, SessionWindowWrapper>
218 IDToSessionWindowMap; 237 IDToSessionWindowMap;
219 typedef std::map<std::string, IDToSessionWindowMap> SyncedWindowMap; 238 typedef std::map<std::string, IDToSessionWindowMap> SyncedWindowMap;
220 239
221 typedef std::map<std::string, sync_driver::SyncedSession*> SyncedSessionMap; 240 typedef std::map<std::string, sync_driver::SyncedSession*> SyncedSessionMap;
222 241
223 // Helper methods for deleting SessionWindows and SessionTabs without owners. 242 // Helper methods for deleting SessionWindows and SessionTabs without owners.
224 bool DeleteOldSessionWindowIfNecessary(SessionWindowWrapper window_wrapper); 243 bool DeleteOldSessionWindowIfNecessary(
225 bool DeleteOldSessionTabIfNecessary(SessionTabWrapper tab_wrapper); 244 const SessionWindowWrapper& window_wrapper);
245 bool DeleteOldSessionTabIfNecessary(const SessionTabWrapper& tab_wrapper);
226 246
227 // Implementation for GetTab(...) above, permits invalid tab_node_id. 247 // Implementation for GetTab(...) above, permits invalid tab_node_id.
228 sessions::SessionTab* GetTabImpl(const std::string& session_tag, 248 sessions::SessionTab* GetTabImpl(const std::string& session_tag,
229 SessionID::id_type tab_id, 249 SessionID::id_type tab_id,
230 int tab_node_id); 250 int tab_node_id);
231 251
232 // The client of the sync sessions datatype. 252 // The client of the sync sessions datatype.
233 sync_sessions::SyncSessionsClient* const sessions_client_; 253 sync_sessions::SyncSessionsClient* const sessions_client_;
234 254
235 // Per client mapping of tab id's to their SessionTab objects. 255 // Per client mapping of tab id's to their SessionTab objects.
(...skipping 20 matching lines...) Expand all
256 // The tag for this machine's local session, so we can distinguish the foreign 276 // The tag for this machine's local session, so we can distinguish the foreign
257 // sessions. 277 // sessions.
258 std::string local_session_tag_; 278 std::string local_session_tag_;
259 279
260 DISALLOW_COPY_AND_ASSIGN(SyncedSessionTracker); 280 DISALLOW_COPY_AND_ASSIGN(SyncedSessionTracker);
261 }; 281 };
262 282
263 } // namespace browser_sync 283 } // namespace browser_sync
264 284
265 #endif // COMPONENTS_SYNC_SESSIONS_SYNCED_SESSION_TRACKER_H_ 285 #endif // COMPONENTS_SYNC_SESSIONS_SYNCED_SESSION_TRACKER_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698