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

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

Issue 2683263002: Reland v4 of Session refactor (Closed)
Patch Set: Better handle possible corruption in sync node and add some testing Created 3 years, 10 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 // 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
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
132 sessions::SessionTab* GetTab(const std::string& session_tag, 130 sessions::SessionTab* GetTab(const std::string& session_tag,
133 SessionID::id_type tab_id, 131 SessionID::id_type tab_id);
134 int tab_node_id);
135 132
136 // Fills |tab_node_ids| with the tab node ids (see GetTab) for all the tabs* 133 // **** Methods specific to foreign sessions. ****
137 // associated with the session having tag |session_tag|. 134
138 void LookupTabNodeIds(const std::string& session_tag, 135 // Tracks the deletion of a foreign tab by removing the given |tab_node_id|
139 std::set<int>* tab_node_ids); 136 // 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
138 // 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
140 // upon foreign tab deletion, otherwise LookupTabNodeIds(...) may return
141 // already deleted tab node ids.
142 void DeleteForeignTab(const std::string& session_tag, int tab_node_id);
143
144 // Deletes the session associated with |session_tag| if it exists.
145 // Returns true if the session existed and was deleted, false otherwise.
146 bool DeleteForeignSession(const std::string& session_tag);
147
148 // Deletes those windows and tabs associated with |session_tag| that are no
149 // longer owned. See ResetSessionTracking(...)..
150 void CleanupForeignSession(const std::string& session_tag);
151
152 // **** Methods specific to the local session. ****
153
154 // Set the local session tag. Must be called before any other local session
155 // methods are invoked.
156 void SetLocalSessionTag(const std::string& local_session_tag);
157
158 // Similar to CleanupForeignSession, but also marks any unmapped tabs as free
159 // in the tab node pool and fills |deleted_node_ids| with the set of locally
160 // free tab nodes to be deleted.
161 void CleanupLocalTabs(std::set<int>* deleted_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 ****.
140 179
141 // Free the memory for all dynamically allocated objects and clear the 180 // Free the memory for all dynamically allocated objects and clear the
142 // tracking structures. 181 // tracking structures.
143 void Clear(); 182 void Clear();
144 183
145 bool Empty() const { 184 bool Empty() const {
146 return synced_tab_map_.empty() && synced_session_map_.empty(); 185 return synced_tab_map_.empty() && synced_session_map_.empty();
147 } 186 }
148 187
149 // Includes both foreign sessions and the local session. 188 // Includes both foreign sessions and the local session.
150 size_t num_synced_sessions() const { return synced_session_map_.size(); } 189 size_t num_synced_sessions() const { return synced_session_map_.size(); }
151 190
152 // Returns the number of tabs associated with the specified session tag. 191 // Returns the number of tabs associated with the specified session tag.
153 size_t num_synced_tabs(const std::string& session_tag) const { 192 size_t num_synced_tabs(const std::string& session_tag) const {
154 auto iter = synced_tab_map_.find(session_tag); 193 auto iter = synced_tab_map_.find(session_tag);
155 if (iter != synced_tab_map_.end()) { 194 if (iter != synced_tab_map_.end()) {
156 return iter->second.size(); 195 return iter->second.size();
157 } else { 196 } else {
158 return 0; 197 return 0;
159 } 198 }
160 } 199 }
161 200
201 // Returns whether a tab is unmapped or not.
202 bool IsTabUnmappedForTesting(SessionID::id_type tab_id);
203
162 private: 204 private:
163 // Implementation for GetTab(...) above, permits invalid tab_node_id. 205 friend class SessionsSyncManagerTest;
164 sessions::SessionTab* GetTabImpl(const std::string& session_tag, 206 friend class SyncedSessionTrackerTest;
165 SessionID::id_type tab_id, 207
166 int tab_node_id); 208 // Implementation of CleanupForeignSession/CleanupLocalTabs.
209 void CleanupSessionImpl(const std::string& session_tag);
167 210
168 // The client of the sync sessions datatype. 211 // The client of the sync sessions datatype.
169 SyncSessionsClient* const sessions_client_; 212 SyncSessionsClient* const sessions_client_;
170 213
171 // The mapping of tab/window ids to their SessionTab/SessionWindow objects. 214 // The mapping of tab/window to their SessionTab/SessionWindow objects.
172 // The SessionTab/SessionWindow objects referred to may be owned either by the 215 // 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 216 // session in the |synced_session_map_| or be temporarily unmapped and live in
174 // the |unmapped_tabs_|/|unmapped_windows_| collections. 217 // the |unmapped_tabs_|/|unmapped_windows_| collections.
175 // 218 //
176 // Map: session tag -> (tab/window id -> SessionTab*/SessionWindow*) 219 // Map: session tag -> (tab/window -> SessionTab*/SessionWindow*)
177 std::map<std::string, std::map<SessionID::id_type, sessions::SessionTab*>> 220 std::map<std::string, std::map<SessionID::id_type, sessions::SessionTab*>>
178 synced_tab_map_; 221 synced_tab_map_;
179 std::map<std::string, std::map<SessionID::id_type, sessions::SessionWindow*>> 222 std::map<std::string, std::map<SessionID::id_type, sessions::SessionWindow*>>
180 synced_window_map_; 223 synced_window_map_;
181 224
182 // The collection that owns the SyncedSessions, and transitively, all of the 225 // The collection that owns the SyncedSessions, and transitively, all of the
183 // windows and tabs they contain. 226 // windows and tabs they contain.
184 // 227 //
185 // Map: session tag -> owned SyncedSession 228 // Map: session tag -> owned SyncedSession
186 std::map<std::string, std::unique_ptr<SyncedSession>> synced_session_map_; 229 std::map<std::string, std::unique_ptr<SyncedSession>> synced_session_map_;
187 230
188 // The collection of tabs/windows not owned by a SyncedSession. This is the 231 // 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 232 // 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 233 // 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. 234 // owning session by a call to ResetSessionTracking() and not yet added back.
192 // 235 //
193 // Map: session tag -> (tab/window id -> owned SessionTab/SessionWindow) 236 // Map: session tag -> (tab/window id -> owned SessionTab/SessionWindow)
194 std::map<std::string, 237 std::map<std::string,
195 std::map<SessionID::id_type, std::unique_ptr<sessions::SessionTab>>> 238 std::map<SessionID::id_type, std::unique_ptr<sessions::SessionTab>>>
196 unmapped_tabs_; 239 unmapped_tabs_;
197 std::map< 240 std::map<
198 std::string, 241 std::string,
199 std::map<SessionID::id_type, std::unique_ptr<sessions::SessionWindow>>> 242 std::map<SessionID::id_type, std::unique_ptr<sessions::SessionWindow>>>
200 unmapped_windows_; 243 unmapped_windows_;
201 244
202 // The tag for this machine's local session, so we can distinguish the foreign 245 // The tag for this machine's local session, so we can distinguish the foreign
203 // sessions. 246 // sessions.
204 std::string local_session_tag_; 247 std::string local_session_tag_;
205 248
249 // Pool of used/available sync nodes associated with local tabs.
250 TabNodePool local_tab_pool_;
251
206 DISALLOW_COPY_AND_ASSIGN(SyncedSessionTracker); 252 DISALLOW_COPY_AND_ASSIGN(SyncedSessionTracker);
207 }; 253 };
208 254
209 } // namespace sync_sessions 255 } // namespace sync_sessions
210 256
211 #endif // COMPONENTS_SYNC_SESSIONS_SYNCED_SESSION_TRACKER_H_ 257 #endif // COMPONENTS_SYNC_SESSIONS_SYNCED_SESSION_TRACKER_H_
OLDNEW
« no previous file with comments | « components/sync_sessions/sessions_sync_manager.cc ('k') | components/sync_sessions/synced_session_tracker.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698