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

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

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

Powered by Google App Engine
This is Rietveld 408576698