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 321 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
332 DVLOG(1) << "Getting " | 332 DVLOG(1) << "Getting " |
333 << (session_tag == local_session_tag_ ? "local session" | 333 << (session_tag == local_session_tag_ ? "local session" |
334 : session_tag) | 334 : session_tag) |
335 << "'s new tab " << tab_id << " at " << tab_ptr; | 335 << "'s new tab " << tab_id << " at " << tab_ptr; |
336 } | 336 } |
337 DCHECK(tab_ptr); | 337 DCHECK(tab_ptr); |
338 DCHECK_EQ(tab_ptr->tab_id.id(), tab_id); | 338 DCHECK_EQ(tab_ptr->tab_id.id(), tab_id); |
339 return tab_ptr; | 339 return tab_ptr; |
340 } | 340 } |
341 | 341 |
| 342 bool SyncedSessionTracker::ReassociateTab(const std::string& session_tag, |
| 343 SessionID::id_type old_tab_id, |
| 344 SessionID::id_type new_tab_id) { |
| 345 DCHECK_NE(TabNodePool::kInvalidTabID, old_tab_id); |
| 346 DCHECK_NE(TabNodePool::kInvalidTabID, new_tab_id); |
| 347 |
| 348 sessions::SessionTab* tab_ptr = nullptr; |
| 349 auto old_tab_iter = synced_tab_map_[session_tag].find(old_tab_id); |
| 350 if (old_tab_iter == synced_tab_map_[session_tag].end()) |
| 351 return false; |
| 352 |
| 353 // It's possible a placeholder is already in place for the new tab. If that's |
| 354 // the case, reuse the placeholder SessionTab. Otherwise, reuse the old |
| 355 // SessionTab. |
| 356 auto new_tab_iter = synced_tab_map_[session_tag].find(new_tab_id); |
| 357 if (new_tab_iter != synced_tab_map_[session_tag].end()) { |
| 358 tab_ptr = new_tab_iter->second; |
| 359 } else { |
| 360 tab_ptr = old_tab_iter->second; |
| 361 } |
| 362 |
| 363 // If the old tab is unmapped, update the tab id under which it is indexed. |
| 364 auto unmapped_tabs_iter = unmapped_tabs_[session_tag].find(old_tab_id); |
| 365 if (unmapped_tabs_iter != unmapped_tabs_[session_tag].end()) { |
| 366 std::unique_ptr<sessions::SessionTab> tab = |
| 367 std::move(unmapped_tabs_iter->second); |
| 368 unmapped_tabs_[session_tag].erase(unmapped_tabs_iter); |
| 369 unmapped_tabs_[session_tag][new_tab_id] = std::move(tab); |
| 370 } |
| 371 |
| 372 // Update the tab id. |
| 373 DVLOG(1) << "Remapped tab " << old_tab_id << " to tab " << new_tab_id; |
| 374 tab_ptr->tab_id.set_id(new_tab_id); |
| 375 |
| 376 // Remove the tab from the synced tab map under the old id. |
| 377 synced_tab_map_[session_tag].erase(old_tab_iter); |
| 378 |
| 379 // Add the tab back into the tab map with the new id. |
| 380 synced_tab_map_[session_tag][new_tab_id] = tab_ptr; |
| 381 return true; |
| 382 } |
| 383 |
342 void SyncedSessionTracker::Clear() { | 384 void SyncedSessionTracker::Clear() { |
343 // Cleanup unmapped tabs and windows. | 385 // Cleanup unmapped tabs and windows. |
344 unmapped_windows_.clear(); | 386 unmapped_windows_.clear(); |
345 unmapped_tabs_.clear(); | 387 unmapped_tabs_.clear(); |
346 | 388 |
347 // Delete SyncedSession objects (which also deletes all their windows/tabs). | 389 // Delete SyncedSession objects (which also deletes all their windows/tabs). |
348 synced_session_map_.clear(); | 390 synced_session_map_.clear(); |
349 | 391 |
350 // Get rid of our convenience maps (does not delete the actual Window/Tabs | 392 // Get rid of our convenience maps (does not delete the actual Window/Tabs |
351 // themselves; they should have all been deleted above). | 393 // themselves; they should have all been deleted above). |
352 synced_window_map_.clear(); | 394 synced_window_map_.clear(); |
353 synced_tab_map_.clear(); | 395 synced_tab_map_.clear(); |
354 | 396 |
355 local_session_tag_.clear(); | 397 local_session_tag_.clear(); |
356 } | 398 } |
357 | 399 |
358 } // namespace sync_sessions | 400 } // namespace sync_sessions |
OLD | NEW |