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

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

Issue 2575773003: Revert "Reland of [Sync] Put session tracker in charge of maintaining local state." (Closed)
Patch Set: Created 4 years 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 // **** 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
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
99 // Adds the window with id |window_id| to the session specified by 110 // 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 111 // |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 112 // the session did not exist yet, creates it. Ownership of the SessionWindow
102 // remains within the SyncedSessionTracker. 113 // remains within the SyncedSessionTracker.
103 void PutWindowInSession(const std::string& session_tag, 114 void PutWindowInSession(const std::string& session_tag,
104 SessionID::id_type window_id); 115 SessionID::id_type window_id);
105 116
106 // Adds the tab with id |tab_id| to the window |window_id|. If none existed 117 // 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 118 // for that session, creates one. Ownership of the SessionTab remains within
108 // the SyncedSessionTracker. 119 // the SyncedSessionTracker.
109 // 120 //
110 // Note: GetSession(..) must have already been called with |session_tag| to 121 // Note: GetSession(..) must have already been called with |session_tag| to
111 // ensure we having mapping information for this session. 122 // ensure we having mapping information for this session.
112 void PutTabInWindow(const std::string& session_tag, 123 void PutTabInWindow(const std::string& session_tag,
113 SessionID::id_type window_id, 124 SessionID::id_type window_id,
114 SessionID::id_type tab_id, 125 SessionID::id_type tab_id,
115 size_t tab_index); 126 size_t tab_index);
116 127
117 // Adds |tab_node_id| to the session specified by |session_tag|, creating that 128 // Returns a pointer to the SessionTab object associated with |tab_id| for
118 // session if necessary. This is necessary to ensure that each session has an 129 // the session specified with |session_tag|. If none exists, creates one.
119 // up to date list of tab nodes linked to it for session deletion purposes. 130 // Ownership of the SessionTab remains within the SyncedSessionTracker.
120 // Note that this won't update the local tab pool, even if the local session 131 // |tab_node_id| must be a valid node id for the node backing this tab.
121 // tag is passed. The tab pool is only updated with new tab nodes when they're 132 sessions::SessionTab* GetTab(const std::string& session_tag,
122 // associated with a tab id (see ReassociateLocalTabNode or GetTabNodeForTab). 133 SessionID::id_type tab_id,
123 void OnTabNodeSeen(const std::string& session_tag, int tab_node_id); 134 int tab_node_id);
124 135
125 // Returns a pointer to the SessionTab object associated with 136 // Fills |tab_node_ids| with the tab node ids (see GetTab) for all the tabs*
126 // |tab_id| for the session specified with |session_tag|. 137 // associated with the session having tag |session_tag|.
127 // Note: Ownership of the SessionTab remains within the SyncedSessionTracker. 138 void LookupTabNodeIds(const std::string& session_tag,
128 // TODO(zea): Replace SessionTab with a Sync specific wrapper. 139 std::set<int>* tab_node_ids);
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|
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 GetTabNodeForLocalTab(int tab_id, int* tab_node_id);
167
168 // Reassociates the tab denoted by |tab_node_id| with a new tab id, preserving
169 // any previous SessionTab object the node was associated with. This is useful
170 // on restart when sync needs to reassociate tabs from a previous session with
171 // newly restored tabs (and can be used in conjunction with PutTabInWindow).
172 void ReassociateLocalTab(int tab_node_id, SessionID::id_type new_tab_id);
173
174 // **** Methods for querying/manipulating overall state ****.
175 140
176 // Free the memory for all dynamically allocated objects and clear the 141 // Free the memory for all dynamically allocated objects and clear the
177 // tracking structures. 142 // tracking structures.
178 void Clear(); 143 void Clear();
179 144
180 bool Empty() const { 145 bool Empty() const {
181 return synced_tab_map_.empty() && synced_session_map_.empty(); 146 return synced_tab_map_.empty() && synced_session_map_.empty();
182 } 147 }
183 148
184 // Includes both foreign sessions and the local session. 149 // Includes both foreign sessions and the local session.
185 size_t num_synced_sessions() const { return synced_session_map_.size(); } 150 size_t num_synced_sessions() const { return synced_session_map_.size(); }
186 151
187 // Returns the number of tabs associated with the specified session tag. 152 // Returns the number of tabs associated with the specified session tag.
188 size_t num_synced_tabs(const std::string& session_tag) const { 153 size_t num_synced_tabs(const std::string& session_tag) const {
189 auto iter = synced_tab_map_.find(session_tag); 154 auto iter = synced_tab_map_.find(session_tag);
190 if (iter != synced_tab_map_.end()) { 155 if (iter != synced_tab_map_.end()) {
191 return iter->second.size(); 156 return iter->second.size();
192 } else { 157 } else {
193 return 0; 158 return 0;
194 } 159 }
195 } 160 }
196 161
197 private: 162 private:
198 friend class SessionsSyncManagerTest; 163 // Implementation for GetTab(...) above, permits invalid tab_node_id.
199 friend class SyncedSessionTrackerTest; 164 sessions::SessionTab* GetTabImpl(const std::string& session_tag,
200 165 SessionID::id_type tab_id,
201 // Implementation of CleanupForeignSession/CleanupLocalTabs. 166 int tab_node_id);
202 void CleanupSessionImpl(const std::string& session_tag);
203 167
204 // The client of the sync sessions datatype. 168 // The client of the sync sessions datatype.
205 SyncSessionsClient* const sessions_client_; 169 SyncSessionsClient* const sessions_client_;
206 170
207 // The mapping of tab/window to their SessionTab/SessionWindow objects. 171 // The mapping of tab/window ids to their SessionTab/SessionWindow objects.
208 // 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
209 // 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
210 // the |unmapped_tabs_|/|unmapped_windows_| collections. 174 // the |unmapped_tabs_|/|unmapped_windows_| collections.
211 // 175 //
212 // Map: session tag -> (tab/window -> SessionTab*/SessionWindow*) 176 // Map: session tag -> (tab/window id -> SessionTab*/SessionWindow*)
213 std::map<std::string, std::map<SessionID::id_type, sessions::SessionTab*>> 177 std::map<std::string, std::map<SessionID::id_type, sessions::SessionTab*>>
214 synced_tab_map_; 178 synced_tab_map_;
215 std::map<std::string, std::map<SessionID::id_type, sessions::SessionWindow*>> 179 std::map<std::string, std::map<SessionID::id_type, sessions::SessionWindow*>>
216 synced_window_map_; 180 synced_window_map_;
217 181
218 // The collection that owns the SyncedSessions, and transitively, all of the 182 // The collection that owns the SyncedSessions, and transitively, all of the
219 // windows and tabs they contain. 183 // windows and tabs they contain.
220 // 184 //
221 // Map: session tag -> owned SyncedSession 185 // Map: session tag -> owned SyncedSession
222 std::map<std::string, std::unique_ptr<SyncedSession>> synced_session_map_; 186 std::map<std::string, std::unique_ptr<SyncedSession>> synced_session_map_;
223 187
224 // 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
225 // 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
226 // 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
227 // 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.
228 // 192 //
229 // Map: session tag -> (tab/window id -> owned SessionTab/SessionWindow) 193 // Map: session tag -> (tab/window id -> owned SessionTab/SessionWindow)
230 std::map<std::string, 194 std::map<std::string,
231 std::map<SessionID::id_type, std::unique_ptr<sessions::SessionTab>>> 195 std::map<SessionID::id_type, std::unique_ptr<sessions::SessionTab>>>
232 unmapped_tabs_; 196 unmapped_tabs_;
233 std::map< 197 std::map<
234 std::string, 198 std::string,
235 std::map<SessionID::id_type, std::unique_ptr<sessions::SessionWindow>>> 199 std::map<SessionID::id_type, std::unique_ptr<sessions::SessionWindow>>>
236 unmapped_windows_; 200 unmapped_windows_;
237 201
238 // 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
239 // sessions. 203 // sessions.
240 std::string local_session_tag_; 204 std::string local_session_tag_;
241 205
242 // Pool of used/available sync nodes associated with local tabs.
243 TabNodePool local_tab_pool_;
244
245 DISALLOW_COPY_AND_ASSIGN(SyncedSessionTracker); 206 DISALLOW_COPY_AND_ASSIGN(SyncedSessionTracker);
246 }; 207 };
247 208
248 } // namespace sync_sessions 209 } // namespace sync_sessions
249 210
250 #endif // COMPONENTS_SYNC_SESSIONS_SYNCED_SESSION_TRACKER_H_ 211 #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