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

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: Fixing comment for zea. 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 13 matching lines...) Expand all
24 24
25 namespace browser_sync { 25 namespace browser_sync {
26 26
27 // Class to manage synced sessions. The tracker will own all SyncedSession 27 // Class to manage synced sessions. The tracker will own all SyncedSession
28 // and SyncedSessionTab objects it creates, and deletes them appropriately on 28 // and SyncedSessionTab objects it creates, and deletes them appropriately on
29 // destruction. 29 // destruction.
30 // Note: SyncedSession objects are created for all synced sessions, including 30 // Note: SyncedSession objects are created for all synced sessions, including
31 // the local session (whose tag we maintain separately). 31 // the local session (whose tag we maintain separately).
32 class SyncedSessionTracker { 32 class SyncedSessionTracker {
33 public: 33 public:
34 // Different ways to lookup/filter tabs.
35 enum SessionLookup {
36 RAW, // Return all foreign sessions.
37 PRESENTABLE // Have one window with at least one tab with syncable content.
38 };
39
34 explicit SyncedSessionTracker( 40 explicit SyncedSessionTracker(
35 sync_sessions::SyncSessionsClient* sessions_client); 41 sync_sessions::SyncSessionsClient* sessions_client);
36 ~SyncedSessionTracker(); 42 ~SyncedSessionTracker();
37 43
38 // We track and distinguish the local session from foreign sessions. 44 // We track and distinguish the local session from foreign sessions.
39 void SetLocalSessionTag(const std::string& local_session_tag); 45 void SetLocalSessionTag(const std::string& local_session_tag);
40 46
41 // Fill a preallocated vector with all foreign sessions we're tracking (skips 47 // Fill a preallocated vector with all foreign sessions we're tracking (skips
42 // the local session object). SyncedSession ownership remains within the 48 // the local session object). SyncedSession ownership remains within the
43 // SyncedSessionTracker. 49 // SyncedSessionTracker. Lookup parameter is used to decide which foreign tabs
50 // should be include.
44 // Returns true if we had foreign sessions to fill it with, false otherwise. 51 // Returns true if we had foreign sessions to fill it with, false otherwise.
45 bool LookupAllForeignSessions( 52 bool LookupAllForeignSessions(
46 std::vector<const sync_driver::SyncedSession*>* sessions) const; 53 std::vector<const sync_driver::SyncedSession*>* sessions,
54 SessionLookup lookup) const;
47 55
48 // Attempts to look up the session windows associatd with the session given 56 // Attempts to look up the session windows associatd with the session given
49 // by |session_tag|. Ownership Of SessionWindows stays within the 57 // by |session_tag|. Ownership Of SessionWindows stays within the
50 // SyncedSessionTracker. 58 // SyncedSessionTracker.
51 // If lookup succeeds: 59 // If lookup succeeds:
52 // - Fills windows with the SessionWindow pointers, returns true. 60 // - Fills windows with the SessionWindow pointers, returns true.
53 // Else 61 // Else
54 // - Returns false. 62 // - Returns false.
55 bool LookupSessionWindows( 63 bool LookupSessionWindows(
56 const std::string& session_tag, 64 const std::string& session_tag,
57 std::vector<const sessions::SessionWindow*>* windows) const; 65 std::vector<const sessions::SessionWindow*>* windows) const;
58 66
59 // Attempts to look up the tab associated with the given tag and tab id. 67 // Attempts to look up the tab associated with the given tag and tab id.
60 // Ownership of the SessionTab remains within the SyncedSessionTracker. 68 // Ownership of the SessionTab remains within the SyncedSessionTracker.
61 // If lookup succeeds: 69 // If lookup succeeds:
62 // - Sets tab to point to the SessionTab, and returns true. 70 // - Sets tab to point to the SessionTab, and returns true.
63 // Else 71 // Else
64 // - Returns false, tab is set to NULL. 72 // - Returns false, tab is set to NULL.
65 bool LookupSessionTab(const std::string& session_tag, 73 bool LookupSessionTab(const std::string& session_tag,
66 SessionID::id_type tab_id, 74 SessionID::id_type tab_id,
67 const sessions::SessionTab** tab) const; 75 const sessions::SessionTab** tab) const;
68 76
69 // Allows retrieval of existing data for the local session. Unlike GetSession 77 // Allows retrieval of existing data for the local session. Unlike GetSession
70 // this won't create-if-not-present. 78 // this won't create-if-not-present.
71 bool LookupLocalSession(const sync_driver::SyncedSession** output) const; 79 bool LookupLocalSession(const sync_driver::SyncedSession** output) const;
72 80
73 // Returns a pointer to the SyncedSession object associated with 81 // Returns a pointer to the SyncedSession object associated with
74 // |session_tag|. If none exists, creates one. Ownership of the 82 // |session_tag|. If none exists, creates one. Ownership of the
75 // SyncedSession remains within the SyncedSessionTracker. 83 // SyncedSession remains within the SyncedSessionTracker.
76 sync_driver::SyncedSession* GetSession(const std::string& session_tag); 84 sync_driver::SyncedSession* GetSession(const std::string& session_tag);
77 85
78 // Deletes the session associated with |session_tag| if it exists. 86 // Deletes the session associated with |session_tag| if it exists.
79 // Returns true if the session existed and was deleted, false otherwise. 87 // Returns true if the session existed and was deleted, false otherwise.
80 bool DeleteSession(const std::string& session_tag); 88 bool DeleteSession(const std::string& session_tag);
81 89
82 // Resets the tracking information for the session specified by |session_tag|. 90 // Resets the tracking information for the session specified by |session_tag|.
83 // 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
84 // keeping pointers saved in the synced_window_map_ and synced_tab_map_. 92 // keeping pointers saved in the synced_window_map_ and synced_tab_map_.
85 // Once reset, all calls to PutWindowInSession and PutTabInWindow will denote 93 // Once reset, all calls to PutWindowInSession and PutTabInWindow will denote
86 // that the requested windows and tabs are owned (by setting the boolean 94 // that the requested windows and tabs are owned (by setting the boolean
87 // in their SessionWindowWrapper/SessionTabWrapper to true) and add them back 95 // in their SessionWindowWrapper/SessionTabWrapper to true) and add them back
88 // to their session. The next call to CleanupSession(...) will delete those 96 // to their session. The next call to CleanupSession(...) will delete those
89 // windows and tabs not owned. 97 // windows and tabs not owned.
90 void ResetSessionTracking(const std::string& session_tag); 98 void ResetSessionTracking(const std::string& session_tag);
91 99
100 // Tracks the deletion of a foreign tab by removing the given |tab_node_id|
101 // from the parent session. Doesn't actually remove any tab objects because
102 // the header may have or may not have already been updated to no longer
103 // parent this tab. Regardless, when the header is updated then cleanup will
104 // remove the actual tab data. However, this method always needs to be called
105 // upon foreign tab deletion, otherwise LookupTabNodeIds(...) may return
106 // already deleted tab node ids.
107 void DeleteForeignTab(const std::string& session_tag, int tab_node_id);
108
92 // Deletes those windows and tabs associated with |session_tag| that are no 109 // Deletes those windows and tabs associated with |session_tag| that are no
93 // longer owned. 110 // longer owned.
94 // See ResetSessionTracking(...). 111 // See ResetSessionTracking(...).
95 void CleanupSession(const std::string& session_tag); 112 void CleanupSession(const std::string& session_tag);
96 113
97 // Adds the window with id |window_id| to the session specified by 114 // 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 115 // |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, 116 // that session, creates one. Similarly, if the session did not exist yet,
100 // creates it. Ownership of the SessionWindow remains within the 117 // creates it. Ownership of the SessionWindow remains within the
101 // SyncedSessionTracker. 118 // SyncedSessionTracker.
(...skipping 13 matching lines...) Expand all
115 // Returns a pointer to the SessionTab object associated with |tab_id| for 132 // Returns a pointer to the SessionTab object associated with |tab_id| for
116 // the session specified with |session_tag|. If none exists, creates one. 133 // the session specified with |session_tag|. If none exists, creates one.
117 // Ownership of the SessionTab remains within the SyncedSessionTracker. 134 // Ownership of the SessionTab remains within the SyncedSessionTracker.
118 // |tab_node_id| must be a valid node id for the node backing this tab. 135 // |tab_node_id| must be a valid node id for the node backing this tab.
119 sessions::SessionTab* GetTab(const std::string& session_tag, 136 sessions::SessionTab* GetTab(const std::string& session_tag,
120 SessionID::id_type tab_id, 137 SessionID::id_type tab_id,
121 int tab_node_id); 138 int tab_node_id);
122 139
123 // Fills |tab_node_ids| with the tab node ids (see GetTab) for all the tabs* 140 // Fills |tab_node_ids| with the tab node ids (see GetTab) for all the tabs*
124 // associated with the session having tag |session_tag|. 141 // associated with the session having tag |session_tag|.
125 // Returns false if we don't have any record of the session. If no tabs were 142 void LookupTabNodeIds(const std::string& session_tag,
126 // found as part of the session, the return value will be true but
127 // |tab_node_ids| will be empty.
128 //
129 // * - note that this only returns the ids we're aware of; it's possible we
130 // don't have the latest tab state from a foreign session and it's also
131 // possible we just haven't updated the tab_node_id for a tab yet, so the
132 // result list should not be treated as authoritative.
133 bool LookupTabNodeIds(const std::string& session_tag,
134 std::set<int>* tab_node_ids); 143 std::set<int>* tab_node_ids);
135 144
136 // Free the memory for all dynamically allocated objects and clear the 145 // Free the memory for all dynamically allocated objects and clear the
137 // tracking structures. 146 // tracking structures.
138 void Clear(); 147 void Clear();
139 148
140 bool Empty() const { 149 bool Empty() const {
141 return synced_tab_map_.empty() && synced_session_map_.empty(); 150 return synced_tab_map_.empty() && synced_session_map_.empty();
142 } 151 }
143 152
(...skipping 25 matching lines...) Expand all
169 // orphaned. CleanupSession(...) can then delete any orphanted data objects 178 // orphaned. CleanupSession(...) can then delete any orphanted data objects
170 // and wrapper for a given session via session tag. 179 // and wrapper for a given session via session tag.
171 enum OwnedState { IS_OWNED, NOT_OWNED }; 180 enum OwnedState { IS_OWNED, NOT_OWNED };
172 181
173 // Datatypes for accessing session data. Neither of the *Wrappers actually 182 // Datatypes for accessing session data. Neither of the *Wrappers actually
174 // have ownership of the Windows/Tabs, they just provide id-based access to 183 // 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 184 // them. The ownership remains within its containing session (for windows and
176 // mapped tabs, unmapped tabs are owned by the unmapped_tabs_ container). 185 // 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 186 // Note, we pair pointers with bools so that we can track what is owned and
178 // what can be deleted (see ResetSessionTracking(..) and CleanupSession(..) 187 // what can be deleted (see ResetSessionTracking(..) and CleanupSession(..)
179 // above). 188 // above). IsOwned is used as a wrapper constructor parameter for readability.
180 // The wrappers also serve as a convenient place to augment state stored in
181 // SessionTab for sync purposes, such as |tab_node_id|.
182 // IsOwned is used as a wrapper constructor parameter for readability.
183 struct SessionTabWrapper { 189 struct SessionTabWrapper {
184 SessionTabWrapper() 190 SessionTabWrapper() : tab_ptr(NULL), owned(false) {}
185 : tab_ptr(NULL), 191
186 owned(false), 192 SessionTabWrapper(sessions::SessionTab* tab_ptr, OwnedState owned)
187 tab_node_id(TabNodePool::kInvalidTabNodeID) {} 193 : tab_ptr(tab_ptr), owned(owned == IS_OWNED) {}
188 SessionTabWrapper(sessions::SessionTab* tab_ptr, 194
189 OwnedState owned,
190 int tab_node_id)
191 : tab_ptr(tab_ptr),
192 owned(owned == IS_OWNED),
193 tab_node_id(tab_node_id) {}
194 sessions::SessionTab* tab_ptr; 195 sessions::SessionTab* tab_ptr;
195 196
196 // This is used as part of a mark-and-sweep approach to garbage 197 // 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". 198 // collection for closed tabs that are no longer "in use", or "owned".
198 // ResetSessionTracking will clear |owned| bits, and if it is not claimed 199 // 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. 200 // by a window by the time CleanupSession is called it will be deleted.
200 bool owned; 201 bool owned;
201
202 // 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
204 // "tab node id" is described in session_specifics.proto.
205 int tab_node_id;
206 }; 202 };
207 typedef std::map<SessionID::id_type, SessionTabWrapper> IDToSessionTabMap; 203 typedef std::map<SessionID::id_type, SessionTabWrapper> IDToSessionTabMap;
208 typedef std::map<std::string, IDToSessionTabMap> SyncedTabMap; 204 typedef std::map<std::string, IDToSessionTabMap> SyncedTabMap;
209 205
210 struct SessionWindowWrapper { 206 struct SessionWindowWrapper {
211 SessionWindowWrapper() : window_ptr(NULL), owned(false) {} 207 SessionWindowWrapper() : window_ptr(NULL), owned(false) {}
212 SessionWindowWrapper(sessions::SessionWindow* window_ptr, OwnedState owned) 208 SessionWindowWrapper(sessions::SessionWindow* window_ptr, OwnedState owned)
213 : window_ptr(window_ptr), owned(owned == IS_OWNED) {} 209 : window_ptr(window_ptr), owned(owned == IS_OWNED) {}
214 sessions::SessionWindow* window_ptr; 210 sessions::SessionWindow* window_ptr;
215 bool owned; 211 bool owned;
216 }; 212 };
217 typedef std::map<SessionID::id_type, SessionWindowWrapper> 213 typedef std::map<SessionID::id_type, SessionWindowWrapper>
218 IDToSessionWindowMap; 214 IDToSessionWindowMap;
219 typedef std::map<std::string, IDToSessionWindowMap> SyncedWindowMap; 215 typedef std::map<std::string, IDToSessionWindowMap> SyncedWindowMap;
220 216
221 typedef std::map<std::string, sync_driver::SyncedSession*> SyncedSessionMap; 217 typedef std::map<std::string, sync_driver::SyncedSession*> SyncedSessionMap;
222 218
223 // Helper methods for deleting SessionWindows and SessionTabs without owners. 219 // Helper methods for deleting SessionWindows and SessionTabs without owners.
224 bool DeleteOldSessionWindowIfNecessary(SessionWindowWrapper window_wrapper); 220 bool DeleteOldSessionWindowIfNecessary(
225 bool DeleteOldSessionTabIfNecessary(SessionTabWrapper tab_wrapper); 221 const SessionWindowWrapper& window_wrapper);
222 bool DeleteOldSessionTabIfNecessary(const SessionTabWrapper& tab_wrapper);
226 223
227 // Implementation for GetTab(...) above, permits invalid tab_node_id. 224 // Implementation for GetTab(...) above, permits invalid tab_node_id.
228 sessions::SessionTab* GetTabImpl(const std::string& session_tag, 225 sessions::SessionTab* GetTabImpl(const std::string& session_tag,
229 SessionID::id_type tab_id, 226 SessionID::id_type tab_id,
230 int tab_node_id); 227 int tab_node_id);
231 228
232 // The client of the sync sessions datatype. 229 // The client of the sync sessions datatype.
233 sync_sessions::SyncSessionsClient* const sessions_client_; 230 sync_sessions::SyncSessionsClient* const sessions_client_;
234 231
235 // Per client mapping of tab id's to their SessionTab objects. 232 // 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 253 // The tag for this machine's local session, so we can distinguish the foreign
257 // sessions. 254 // sessions.
258 std::string local_session_tag_; 255 std::string local_session_tag_;
259 256
260 DISALLOW_COPY_AND_ASSIGN(SyncedSessionTracker); 257 DISALLOW_COPY_AND_ASSIGN(SyncedSessionTracker);
261 }; 258 };
262 259
263 } // namespace browser_sync 260 } // namespace browser_sync
264 261
265 #endif // COMPONENTS_SYNC_SESSIONS_SYNCED_SESSION_TRACKER_H_ 262 #endif // COMPONENTS_SYNC_SESSIONS_SYNCED_SESSION_TRACKER_H_
OLDNEW
« no previous file with comments | « components/sync_sessions/synced_session.h ('k') | components/sync_sessions/synced_session_tracker.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698