OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 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 #include "components/sync_sessions/synced_session_tracker.h" | 5 #include "components/sync_sessions/synced_session_tracker.h" |
6 | 6 |
7 #include <utility> | 7 #include <utility> |
8 | 8 |
9 #include "base/logging.h" | 9 #include "base/logging.h" |
10 #include "base/memory/ptr_util.h" | 10 #include "base/memory/ptr_util.h" |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
46 | 46 |
47 SyncedSessionTracker::SyncedSessionTracker(SyncSessionsClient* sessions_client) | 47 SyncedSessionTracker::SyncedSessionTracker(SyncSessionsClient* sessions_client) |
48 : sessions_client_(sessions_client) {} | 48 : sessions_client_(sessions_client) {} |
49 | 49 |
50 SyncedSessionTracker::~SyncedSessionTracker() { | 50 SyncedSessionTracker::~SyncedSessionTracker() { |
51 Clear(); | 51 Clear(); |
52 } | 52 } |
53 | 53 |
54 void SyncedSessionTracker::SetLocalSessionTag( | 54 void SyncedSessionTracker::SetLocalSessionTag( |
55 const std::string& local_session_tag) { | 55 const std::string& local_session_tag) { |
56 DCHECK(local_session_tag_.empty()); | |
57 DCHECK(!local_session_tag.empty()); | |
58 local_session_tag_ = local_session_tag; | 56 local_session_tag_ = local_session_tag; |
59 } | 57 } |
60 | 58 |
61 bool SyncedSessionTracker::LookupAllForeignSessions( | 59 bool SyncedSessionTracker::LookupAllForeignSessions( |
62 std::vector<const SyncedSession*>* sessions, | 60 std::vector<const SyncedSession*>* sessions, |
63 SessionLookup lookup) const { | 61 SessionLookup lookup) const { |
64 DCHECK(sessions); | 62 DCHECK(sessions); |
65 sessions->clear(); | 63 sessions->clear(); |
66 for (const auto& session_pair : synced_session_map_) { | 64 for (const auto& session_pair : synced_session_map_) { |
67 SyncedSession* foreign_session = session_pair.second.get(); | 65 SyncedSession* foreign_session = session_pair.second.get(); |
(...skipping 17 matching lines...) Expand all Loading... |
85 for (const auto& window_pair : iter->second->windows) | 83 for (const auto& window_pair : iter->second->windows) |
86 windows->push_back(window_pair.second.get()); | 84 windows->push_back(window_pair.second.get()); |
87 | 85 |
88 return true; | 86 return true; |
89 } | 87 } |
90 | 88 |
91 bool SyncedSessionTracker::LookupSessionTab( | 89 bool SyncedSessionTracker::LookupSessionTab( |
92 const std::string& tag, | 90 const std::string& tag, |
93 SessionID::id_type tab_id, | 91 SessionID::id_type tab_id, |
94 const sessions::SessionTab** tab) const { | 92 const sessions::SessionTab** tab) const { |
95 if (tab_id == TabNodePool::kInvalidTabID) | |
96 return false; | |
97 | |
98 DCHECK(tab); | 93 DCHECK(tab); |
99 auto tab_map_iter = synced_tab_map_.find(tag); | 94 auto tab_map_iter = synced_tab_map_.find(tag); |
100 if (tab_map_iter == synced_tab_map_.end()) { | 95 if (tab_map_iter == synced_tab_map_.end()) { |
101 // We have no record of this session. | 96 // We have no record of this session. |
102 *tab = nullptr; | 97 *tab = nullptr; |
103 return false; | 98 return false; |
104 } | 99 } |
105 auto tab_iter = tab_map_iter->second.find(tab_id); | 100 auto tab_iter = tab_map_iter->second.find(tab_id); |
106 if (tab_iter == tab_map_iter->second.end()) { | 101 if (tab_iter == tab_map_iter->second.end()) { |
107 // We have no record of this tab. | 102 // We have no record of this tab. |
108 *tab = nullptr; | 103 *tab = nullptr; |
109 return false; | 104 return false; |
110 } | 105 } |
111 *tab = tab_iter->second; | 106 *tab = tab_iter->second; |
112 return true; | 107 return true; |
113 } | 108 } |
114 | 109 |
115 void SyncedSessionTracker::LookupForeignTabNodeIds( | 110 void SyncedSessionTracker::LookupTabNodeIds(const std::string& session_tag, |
116 const std::string& session_tag, | 111 std::set<int>* tab_node_ids) { |
117 std::set<int>* tab_node_ids) const { | |
118 tab_node_ids->clear(); | 112 tab_node_ids->clear(); |
119 auto session_iter = synced_session_map_.find(session_tag); | 113 auto session_iter = synced_session_map_.find(session_tag); |
120 if (session_iter != synced_session_map_.end()) { | 114 if (session_iter != synced_session_map_.end()) { |
121 tab_node_ids->insert(session_iter->second->tab_node_ids.begin(), | 115 tab_node_ids->insert(session_iter->second->tab_node_ids.begin(), |
122 session_iter->second->tab_node_ids.end()); | 116 session_iter->second->tab_node_ids.end()); |
123 } | 117 } |
124 // In case an invalid node id was included, remove it. | 118 // In case an invalid node id was included, remove it. |
125 tab_node_ids->erase(TabNodePool::kInvalidTabNodeID); | 119 tab_node_ids->erase(TabNodePool::kInvalidTabNodeID); |
126 } | 120 } |
127 | 121 |
(...skipping 15 matching lines...) Expand all Loading... |
143 std::unique_ptr<SyncedSession> synced_session = | 137 std::unique_ptr<SyncedSession> synced_session = |
144 base::MakeUnique<SyncedSession>(); | 138 base::MakeUnique<SyncedSession>(); |
145 DVLOG(1) << "Creating new session with tag " << session_tag << " at " | 139 DVLOG(1) << "Creating new session with tag " << session_tag << " at " |
146 << synced_session.get(); | 140 << synced_session.get(); |
147 synced_session->session_tag = session_tag; | 141 synced_session->session_tag = session_tag; |
148 synced_session_map_[session_tag] = std::move(synced_session); | 142 synced_session_map_[session_tag] = std::move(synced_session); |
149 | 143 |
150 return synced_session_map_[session_tag].get(); | 144 return synced_session_map_[session_tag].get(); |
151 } | 145 } |
152 | 146 |
153 bool SyncedSessionTracker::DeleteForeignSession( | 147 bool SyncedSessionTracker::DeleteSession(const std::string& session_tag) { |
154 const std::string& session_tag) { | |
155 DCHECK_NE(local_session_tag_, session_tag); | |
156 unmapped_windows_.erase(session_tag); | 148 unmapped_windows_.erase(session_tag); |
157 unmapped_tabs_.erase(session_tag); | 149 unmapped_tabs_.erase(session_tag); |
158 | 150 |
159 bool header_existed = false; | 151 bool header_existed = false; |
160 auto iter = synced_session_map_.find(session_tag); | 152 auto iter = synced_session_map_.find(session_tag); |
161 if (iter != synced_session_map_.end()) { | 153 if (iter != synced_session_map_.end()) { |
162 // An implicitly created session that has children tabs but no header node | 154 // An implicitly created session that has children tabs but no header node |
163 // will have never had the device_type changed from unset. | 155 // will have never had the device_type changed from unset. |
164 header_existed = iter->second->device_type != SyncedSession::TYPE_UNSET; | 156 header_existed = iter->second->device_type != SyncedSession::TYPE_UNSET; |
165 // SyncedSession's destructor will trigger deletion of windows which will in | 157 // SyncedSession's destructor will trigger deletion of windows which will in |
(...skipping 23 matching lines...) Expand all Loading... |
189 | 181 |
190 // Then unmap the window itself. | 182 // Then unmap the window itself. |
191 unmapped_windows_[session_tag][window_pair.first] = | 183 unmapped_windows_[session_tag][window_pair.first] = |
192 std::move(window_pair.second); | 184 std::move(window_pair.second); |
193 } | 185 } |
194 session->windows.clear(); | 186 session->windows.clear(); |
195 } | 187 } |
196 | 188 |
197 void SyncedSessionTracker::DeleteForeignTab(const std::string& session_tag, | 189 void SyncedSessionTracker::DeleteForeignTab(const std::string& session_tag, |
198 int tab_node_id) { | 190 int tab_node_id) { |
199 DCHECK_NE(local_session_tag_, session_tag); | |
200 auto session_iter = synced_session_map_.find(session_tag); | 191 auto session_iter = synced_session_map_.find(session_tag); |
201 if (session_iter != synced_session_map_.end()) { | 192 if (session_iter != synced_session_map_.end()) { |
202 session_iter->second->tab_node_ids.erase(tab_node_id); | 193 session_iter->second->tab_node_ids.erase(tab_node_id); |
203 } | 194 } |
204 } | 195 } |
205 | 196 |
206 void SyncedSessionTracker::CleanupSessionImpl(const std::string& session_tag) { | 197 void SyncedSessionTracker::CleanupSession(const std::string& session_tag) { |
207 for (const auto& window_pair : unmapped_windows_[session_tag]) | 198 for (const auto& window_pair : unmapped_windows_[session_tag]) |
208 synced_window_map_[session_tag].erase(window_pair.first); | 199 synced_window_map_[session_tag].erase(window_pair.first); |
209 unmapped_windows_[session_tag].clear(); | 200 unmapped_windows_[session_tag].clear(); |
210 | 201 |
211 for (const auto& tab_pair : unmapped_tabs_[session_tag]) | 202 for (const auto& tab_pair : unmapped_tabs_[session_tag]) |
212 synced_tab_map_[session_tag].erase(tab_pair.first); | 203 synced_tab_map_[session_tag].erase(tab_pair.first); |
213 unmapped_tabs_[session_tag].clear(); | 204 unmapped_tabs_[session_tag].clear(); |
214 } | 205 } |
215 | 206 |
216 bool SyncedSessionTracker::IsTabUnmappedForTesting(SessionID::id_type tab_id) { | |
217 auto it = unmapped_tabs_[local_session_tag_].find(tab_id); | |
218 return it != unmapped_tabs_[local_session_tag_].end(); | |
219 } | |
220 | |
221 void SyncedSessionTracker::PutWindowInSession(const std::string& session_tag, | 207 void SyncedSessionTracker::PutWindowInSession(const std::string& session_tag, |
222 SessionID::id_type window_id) { | 208 SessionID::id_type window_id) { |
223 std::unique_ptr<sessions::SessionWindow> window; | 209 std::unique_ptr<sessions::SessionWindow> window; |
224 | 210 |
225 auto iter = unmapped_windows_[session_tag].find(window_id); | 211 auto iter = unmapped_windows_[session_tag].find(window_id); |
226 if (iter != unmapped_windows_[session_tag].end()) { | 212 if (iter != unmapped_windows_[session_tag].end()) { |
227 DCHECK_EQ(synced_window_map_[session_tag][window_id], iter->second.get()); | 213 DCHECK_EQ(synced_window_map_[session_tag][window_id], iter->second.get()); |
228 window = std::move(iter->second); | 214 window = std::move(iter->second); |
229 unmapped_windows_[session_tag].erase(iter); | 215 unmapped_windows_[session_tag].erase(iter); |
230 DVLOG(1) << "Putting seen window " << window_id << " at " << window.get() | 216 DVLOG(1) << "Putting seen window " << window_id << " at " << window.get() |
231 << "in " << (session_tag == local_session_tag_ ? "local session" | 217 << "in " << (session_tag == local_session_tag_ ? "local session" |
232 : session_tag); | 218 : session_tag); |
233 } else { | 219 } else { |
234 // Create the window. | 220 // Create the window. |
235 window = base::MakeUnique<sessions::SessionWindow>(); | 221 window = base::MakeUnique<sessions::SessionWindow>(); |
236 window->window_id.set_id(window_id); | 222 window->window_id.set_id(window_id); |
237 synced_window_map_[session_tag][window_id] = window.get(); | 223 synced_window_map_[session_tag][window_id] = window.get(); |
238 DVLOG(1) << "Putting new window " << window_id << " at " << window.get() | 224 DVLOG(1) << "Putting new window " << window_id << " at " << window.get() |
239 << "in " << (session_tag == local_session_tag_ ? "local session" | 225 << "in " << (session_tag == local_session_tag_ ? "local session" |
240 : session_tag); | 226 : session_tag); |
241 } | 227 } |
242 DCHECK_EQ(window->window_id.id(), window_id); | 228 DCHECK_EQ(window->window_id.id(), window_id); |
243 DCHECK(GetSession(session_tag)->windows.end() == | 229 DCHECK(GetSession(session_tag)->windows.end() == |
244 GetSession(session_tag)->windows.find(window_id)); | 230 GetSession(session_tag)->windows.find(window_id)); |
245 GetSession(session_tag)->windows[window_id] = std::move(window); | 231 GetSession(session_tag)->windows[window_id] = std::move(window); |
246 } | 232 } |
247 | 233 |
248 void SyncedSessionTracker::PutTabInWindow(const std::string& session_tag, | 234 void SyncedSessionTracker::PutTabInWindow(const std::string& session_tag, |
249 SessionID::id_type window_id, | 235 SessionID::id_type window_id, |
250 SessionID::id_type tab_id) { | 236 SessionID::id_type tab_id, |
| 237 size_t tab_index) { |
251 // We're called here for two reasons. 1) We've received an update to the | 238 // We're called here for two reasons. 1) We've received an update to the |
252 // SessionWindow information of a SessionHeader node for a session, | 239 // SessionWindow information of a SessionHeader node for a foreign session, |
253 // and 2) The SessionHeader node for our local session changed. In both cases | 240 // and 2) The SessionHeader node for our local session changed. In both cases |
254 // we need to update our tracking state to reflect the change. | 241 // we need to update our tracking state to reflect the change. |
255 // | 242 // |
256 // Because the SessionHeader nodes are separate from the individual tab nodes | 243 // Because the SessionHeader nodes are separate from the individual tab nodes |
257 // and we don't store tab_node_ids in the header / SessionWindow specifics, | 244 // and we don't store tab_node_ids in the header / SessionWindow specifics, |
258 // the tab_node_ids are not always available when processing headers. We know | 245 // the tab_node_ids are not always available when processing headers. |
259 // that we will eventually process (via GetTab) every single tab node in the | 246 // We know that we will eventually process (via GetTab) every single tab node |
260 // system, so we permit ourselves to just call GetTab and ignore the result, | 247 // in the system, so we permit ourselves to use kInvalidTabNodeID here and |
261 // creating a placeholder SessionTab in the process. | 248 // rely on the later update to build the mapping (or a restart). |
262 GetTab(session_tag, tab_id); | 249 GetTabImpl(session_tag, tab_id, TabNodePool::kInvalidTabNodeID); |
263 | 250 |
264 // The tab should be unmapped. | 251 // The tab should be unmapped. |
265 std::unique_ptr<sessions::SessionTab> tab; | 252 std::unique_ptr<sessions::SessionTab> tab; |
266 auto it = unmapped_tabs_[session_tag].find(tab_id); | 253 auto it = unmapped_tabs_[session_tag].find(tab_id); |
267 if (it != unmapped_tabs_[session_tag].end()) { | 254 if (it != unmapped_tabs_[session_tag].end()) { |
268 tab = std::move(it->second); | 255 tab = std::move(it->second); |
269 unmapped_tabs_[session_tag].erase(it); | 256 unmapped_tabs_[session_tag].erase(it); |
270 } | 257 } |
271 CHECK(tab) << "crbug.com/673618 Attempting to map tab " << tab_id | 258 DCHECK(tab); |
272 << " multiple times!"; | |
273 | 259 |
274 tab->window_id.set_id(window_id); | 260 tab->window_id.set_id(window_id); |
275 DVLOG(1) << " - tab " << tab_id << " added to window " << window_id; | 261 DVLOG(1) << " - tab " << tab_id << " added to window " << window_id; |
276 DCHECK(GetSession(session_tag)->windows.find(window_id) != | 262 DCHECK(GetSession(session_tag)->windows.find(window_id) != |
277 GetSession(session_tag)->windows.end()); | 263 GetSession(session_tag)->windows.end()); |
278 auto& window_tabs = GetSession(session_tag)->windows[window_id]->tabs; | 264 auto& window_tabs = GetSession(session_tag)->windows[window_id]->tabs; |
279 window_tabs.push_back(std::move(tab)); | 265 if (window_tabs.size() <= tab_index) { |
280 } | 266 window_tabs.resize(tab_index + 1); |
281 | 267 } |
282 void SyncedSessionTracker::OnTabNodeSeen(const std::string& session_tag, | 268 DCHECK(!window_tabs[tab_index]); |
283 int tab_node_id) { | 269 window_tabs[tab_index] = std::move(tab); |
284 GetSession(session_tag)->tab_node_ids.insert(tab_node_id); | |
285 } | 270 } |
286 | 271 |
287 sessions::SessionTab* SyncedSessionTracker::GetTab( | 272 sessions::SessionTab* SyncedSessionTracker::GetTab( |
288 const std::string& session_tag, | 273 const std::string& session_tag, |
289 SessionID::id_type tab_id) { | 274 SessionID::id_type tab_id, |
290 CHECK_NE(TabNodePool::kInvalidTabNodeID, tab_id) << "crbug.com/673618"; | 275 int tab_node_id) { |
| 276 DCHECK_NE(TabNodePool::kInvalidTabNodeID, tab_node_id); |
| 277 return GetTabImpl(session_tag, tab_id, tab_node_id); |
| 278 } |
| 279 |
| 280 sessions::SessionTab* SyncedSessionTracker::GetTabImpl( |
| 281 const std::string& session_tag, |
| 282 SessionID::id_type tab_id, |
| 283 int tab_node_id) { |
291 sessions::SessionTab* tab_ptr = nullptr; | 284 sessions::SessionTab* tab_ptr = nullptr; |
292 auto iter = synced_tab_map_[session_tag].find(tab_id); | 285 auto iter = synced_tab_map_[session_tag].find(tab_id); |
293 if (iter != synced_tab_map_[session_tag].end()) { | 286 if (iter != synced_tab_map_[session_tag].end()) { |
294 tab_ptr = iter->second; | 287 tab_ptr = iter->second; |
| 288 if (tab_node_id != TabNodePool::kInvalidTabNodeID && |
| 289 tab_id != TabNodePool::kInvalidTabID) { |
| 290 // TabIDs are not stable across restarts of a client. Consider this |
| 291 // example with two tabs: |
| 292 // |
| 293 // http://a.com TabID1 --> NodeIDA |
| 294 // http://b.com TabID2 --> NodeIDB |
| 295 // |
| 296 // After restart, tab ids are reallocated. e.g, one possibility: |
| 297 // http://a.com TabID2 --> NodeIDA |
| 298 // http://b.com TabID1 --> NodeIDB |
| 299 // |
| 300 // If that happend on a remote client, here we will see an update to |
| 301 // TabID1 with tab_node_id changing from NodeIDA to NodeIDB, and TabID2 |
| 302 // with tab_node_id changing from NodeIDB to NodeIDA. |
| 303 // |
| 304 // We can also wind up here if we created this tab as an out-of-order |
| 305 // update to the header node for this session before actually associating |
| 306 // the tab itself, so the tab node id wasn't available at the time and |
| 307 // is currently kInvalidTabNodeID. |
| 308 // |
| 309 // In both cases, we can safely throw it into the set of node ids. |
| 310 GetSession(session_tag)->tab_node_ids.insert(tab_node_id); |
| 311 } |
295 | 312 |
296 if (VLOG_IS_ON(1)) { | 313 if (VLOG_IS_ON(1)) { |
297 std::string title; | 314 std::string title; |
298 if (tab_ptr->navigations.size() > 0) { | 315 if (tab_ptr->navigations.size() > 0) { |
299 title = | 316 title = |
300 " (" + base::UTF16ToUTF8(tab_ptr->navigations.back().title()) + ")"; | 317 " (" + base::UTF16ToUTF8(tab_ptr->navigations.back().title()) + ")"; |
301 } | 318 } |
302 DVLOG(1) << "Getting " | 319 DVLOG(1) << "Getting " |
303 << (session_tag == local_session_tag_ ? "local session" | 320 << (session_tag == local_session_tag_ ? "local session" |
304 : session_tag) | 321 : session_tag) |
305 << "'s seen tab " << tab_id << " at " << tab_ptr << " " << title; | 322 << "'s seen tab " << tab_id << " at " << tab_ptr << " " << title; |
306 } | 323 } |
307 } else { | 324 } else { |
308 std::unique_ptr<sessions::SessionTab> tab = | 325 std::unique_ptr<sessions::SessionTab> tab = |
309 base::MakeUnique<sessions::SessionTab>(); | 326 base::MakeUnique<sessions::SessionTab>(); |
310 tab_ptr = tab.get(); | 327 tab_ptr = tab.get(); |
311 tab->tab_id.set_id(tab_id); | 328 tab->tab_id.set_id(tab_id); |
312 synced_tab_map_[session_tag][tab_id] = tab_ptr; | 329 synced_tab_map_[session_tag][tab_id] = tab_ptr; |
313 unmapped_tabs_[session_tag][tab_id] = std::move(tab); | 330 unmapped_tabs_[session_tag][tab_id] = std::move(tab); |
| 331 GetSession(session_tag)->tab_node_ids.insert(tab_node_id); |
314 DVLOG(1) << "Getting " | 332 DVLOG(1) << "Getting " |
315 << (session_tag == local_session_tag_ ? "local session" | 333 << (session_tag == local_session_tag_ ? "local session" |
316 : session_tag) | 334 : session_tag) |
317 << "'s new tab " << tab_id << " at " << tab_ptr; | 335 << "'s new tab " << tab_id << " at " << tab_ptr; |
318 } | 336 } |
319 DCHECK(tab_ptr); | 337 DCHECK(tab_ptr); |
320 DCHECK_EQ(tab_ptr->tab_id.id(), tab_id); | 338 DCHECK_EQ(tab_ptr->tab_id.id(), tab_id); |
321 return tab_ptr; | 339 return tab_ptr; |
322 } | 340 } |
323 | 341 |
324 void SyncedSessionTracker::CleanupForeignSession( | |
325 const std::string& session_tag) { | |
326 DCHECK_NE(local_session_tag_, session_tag); | |
327 CleanupSessionImpl(session_tag); | |
328 } | |
329 | |
330 void SyncedSessionTracker::CleanupLocalTabs(std::set<int>* deleted_node_ids) { | |
331 DCHECK(!local_session_tag_.empty()); | |
332 for (const auto& tab_pair : unmapped_tabs_[local_session_tag_]) | |
333 local_tab_pool_.FreeTab(tab_pair.first); | |
334 CleanupSessionImpl(local_session_tag_); | |
335 local_tab_pool_.CleanupTabNodes(deleted_node_ids); | |
336 for (int tab_node_id : *deleted_node_ids) { | |
337 GetSession(local_session_tag_)->tab_node_ids.erase(tab_node_id); | |
338 } | |
339 } | |
340 | |
341 bool SyncedSessionTracker::GetTabNodeFromLocalTabId(SessionID::id_type tab_id, | |
342 int* tab_node_id) { | |
343 DCHECK(!local_session_tag_.empty()); | |
344 // Ensure a placeholder SessionTab is in place, if not already. | |
345 // Although we don't need a SessionTab to fulfill this request, this forces | |
346 // the | |
347 // creation of one if it doesn't already exist. This helps to make sure we're | |
348 // tracking this |tab_id| if |local_tab_pool_| is, and everyone's data | |
349 // structures | |
350 // are kept in sync and as consistent as possible. | |
351 GetTab(local_session_tag_, tab_id); // Ignore result. | |
352 | |
353 bool reused_existing_tab = | |
354 local_tab_pool_.GetTabNodeForTab(tab_id, tab_node_id); | |
355 DCHECK_NE(TabNodePool::kInvalidTabNodeID, *tab_node_id); | |
356 GetSession(local_session_tag_)->tab_node_ids.insert(*tab_node_id); | |
357 return reused_existing_tab; | |
358 } | |
359 | |
360 bool SyncedSessionTracker::IsLocalTabNodeAssociated(int tab_node_id) { | |
361 if (tab_node_id == TabNodePool::kInvalidTabNodeID) | |
362 return false; | |
363 return local_tab_pool_.GetTabIdFromTabNodeId(tab_node_id) != | |
364 TabNodePool::kInvalidTabID; | |
365 } | |
366 | |
367 void SyncedSessionTracker::ReassociateLocalTab(int tab_node_id, | |
368 SessionID::id_type new_tab_id) { | |
369 DCHECK(!local_session_tag_.empty()); | |
370 DCHECK_NE(TabNodePool::kInvalidTabNodeID, tab_node_id); | |
371 DCHECK_NE(TabNodePool::kInvalidTabID, new_tab_id); | |
372 | |
373 SessionID::id_type old_tab_id = | |
374 local_tab_pool_.GetTabIdFromTabNodeId(tab_node_id); | |
375 local_tab_pool_.ReassociateTabNode(tab_node_id, new_tab_id); | |
376 | |
377 sessions::SessionTab* tab_ptr = nullptr; | |
378 | |
379 auto old_tab_iter = synced_tab_map_[local_session_tag_].find(old_tab_id); | |
380 if (old_tab_id != TabNodePool::kInvalidTabID && | |
381 old_tab_iter != synced_tab_map_[local_session_tag_].end()) { | |
382 tab_ptr = old_tab_iter->second; | |
383 // Remove the tab from the synced tab map under the old id. | |
384 synced_tab_map_[local_session_tag_].erase(old_tab_iter); | |
385 } else { | |
386 // It's possible a placeholder is already in place for the new tab. If so, | |
387 // reuse it, otherwise create a new one (which will default to unmapped). | |
388 tab_ptr = GetTab(local_session_tag_, new_tab_id); | |
389 } | |
390 | |
391 // If the old tab is unmapped, update the tab id under which it is indexed. | |
392 auto unmapped_tabs_iter = unmapped_tabs_[local_session_tag_].find(old_tab_id); | |
393 if (old_tab_id != TabNodePool::kInvalidTabID && | |
394 unmapped_tabs_iter != unmapped_tabs_[local_session_tag_].end()) { | |
395 std::unique_ptr<sessions::SessionTab> tab = | |
396 std::move(unmapped_tabs_iter->second); | |
397 DCHECK_EQ(tab_ptr, tab.get()); | |
398 unmapped_tabs_[local_session_tag_].erase(unmapped_tabs_iter); | |
399 unmapped_tabs_[local_session_tag_][new_tab_id] = std::move(tab); | |
400 } | |
401 | |
402 // Update the tab id. | |
403 if (old_tab_id != TabNodePool::kInvalidTabID) { | |
404 DVLOG(1) << "Remapped tab " << old_tab_id << " with node " << tab_node_id | |
405 << " to tab " << new_tab_id; | |
406 } else { | |
407 DVLOG(1) << "Mapped new tab node " << tab_node_id << " to tab " | |
408 << new_tab_id; | |
409 } | |
410 tab_ptr->tab_id.set_id(new_tab_id); | |
411 | |
412 // Add the tab back into the tab map with the new id. | |
413 synced_tab_map_[local_session_tag_][new_tab_id] = tab_ptr; | |
414 GetSession(local_session_tag_)->tab_node_ids.insert(tab_node_id); | |
415 } | |
416 | |
417 void SyncedSessionTracker::Clear() { | 342 void SyncedSessionTracker::Clear() { |
418 // Cleanup unmapped tabs and windows. | 343 // Cleanup unmapped tabs and windows. |
419 unmapped_windows_.clear(); | 344 unmapped_windows_.clear(); |
420 unmapped_tabs_.clear(); | 345 unmapped_tabs_.clear(); |
421 | 346 |
422 // Delete SyncedSession objects (which also deletes all their windows/tabs). | 347 // Delete SyncedSession objects (which also deletes all their windows/tabs). |
423 synced_session_map_.clear(); | 348 synced_session_map_.clear(); |
424 | 349 |
425 // Get rid of our convenience maps (does not delete the actual Window/Tabs | 350 // Get rid of our convenience maps (does not delete the actual Window/Tabs |
426 // themselves; they should have all been deleted above). | 351 // themselves; they should have all been deleted above). |
427 synced_window_map_.clear(); | 352 synced_window_map_.clear(); |
428 synced_tab_map_.clear(); | 353 synced_tab_map_.clear(); |
429 | 354 |
430 local_tab_pool_.Clear(); | |
431 local_session_tag_.clear(); | 355 local_session_tag_.clear(); |
432 } | 356 } |
433 | 357 |
434 } // namespace sync_sessions | 358 } // namespace sync_sessions |
OLD | NEW |