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 379 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
390 } | 390 } |
391 | 391 |
392 void SyncedSessionTracker::ReassociateLocalTab(int tab_node_id, | 392 void SyncedSessionTracker::ReassociateLocalTab(int tab_node_id, |
393 SessionID::id_type new_tab_id) { | 393 SessionID::id_type new_tab_id) { |
394 DCHECK(!local_session_tag_.empty()); | 394 DCHECK(!local_session_tag_.empty()); |
395 DCHECK_NE(TabNodePool::kInvalidTabNodeID, tab_node_id); | 395 DCHECK_NE(TabNodePool::kInvalidTabNodeID, tab_node_id); |
396 DCHECK_NE(kInvalidTabID, new_tab_id); | 396 DCHECK_NE(kInvalidTabID, new_tab_id); |
397 | 397 |
398 SessionID::id_type old_tab_id = | 398 SessionID::id_type old_tab_id = |
399 local_tab_pool_.GetTabIdFromTabNodeId(tab_node_id); | 399 local_tab_pool_.GetTabIdFromTabNodeId(tab_node_id); |
400 if (new_tab_id == old_tab_id) { | |
401 return; | |
402 } | |
403 | |
400 local_tab_pool_.ReassociateTabNode(tab_node_id, new_tab_id); | 404 local_tab_pool_.ReassociateTabNode(tab_node_id, new_tab_id); |
401 | 405 |
402 sessions::SessionTab* tab_ptr = nullptr; | 406 sessions::SessionTab* tab_ptr = nullptr; |
403 | 407 |
408 auto new_tab_iter = synced_tab_map_[local_session_tag_].find(new_tab_id); | |
404 auto old_tab_iter = synced_tab_map_[local_session_tag_].find(old_tab_id); | 409 auto old_tab_iter = synced_tab_map_[local_session_tag_].find(old_tab_id); |
405 if (old_tab_id != kInvalidTabID && | 410 if (old_tab_id != kInvalidTabID && |
406 old_tab_iter != synced_tab_map_[local_session_tag_].end()) { | 411 old_tab_iter != synced_tab_map_[local_session_tag_].end()) { |
407 tab_ptr = old_tab_iter->second; | 412 tab_ptr = old_tab_iter->second; |
skym
2017/05/04 20:07:20
Is it possible this assigns a nullptr? Cause the n
Nicolas Zea
2017/05/04 23:36:15
Added DCHECK
| |
413 | |
408 // Remove the tab from the synced tab map under the old id. | 414 // Remove the tab from the synced tab map under the old id. |
409 synced_tab_map_[local_session_tag_].erase(old_tab_iter); | 415 synced_tab_map_[local_session_tag_].erase(old_tab_iter); |
410 } else { | 416 |
417 if (new_tab_iter != synced_tab_map_[local_session_tag_].end()) { | |
418 // If both the old and the new tab already exist, delete the new tab | |
419 // and use the old tab in its place. | |
420 auto unmapped_tabs_iter = | |
421 unmapped_tabs_[local_session_tag_].find(new_tab_id); | |
422 if (unmapped_tabs_iter != unmapped_tabs_[local_session_tag_].end()) { | |
423 unmapped_tabs_[local_session_tag_].erase(unmapped_tabs_iter); | |
424 } else { | |
425 sessions::SessionTab* new_tab_ptr = new_tab_iter->second; | |
426 for (auto& window_iter_pair : GetSession(local_session_tag_)->windows) { | |
skym
2017/05/04 20:07:20
Sure would have been nice if things had parent poi
Nicolas Zea
2017/05/04 23:36:15
Yeah, this really motivates moving away from the S
| |
427 auto tab_iter = std::find_if( | |
skym
2017/05/04 20:07:20
Did you consider remove_if?
Nicolas Zea
2017/05/04 23:36:15
I think that would prevent me from breaking out?
| |
428 window_iter_pair.second->wrapped_window.tabs.begin(), | |
skym
2017/05/04 20:07:20
Did you consider assigning window_iter_pair.second
Nicolas Zea
2017/05/04 23:36:15
Done.
| |
429 window_iter_pair.second->wrapped_window.tabs.end(), | |
430 [&new_tab_ptr](const std::unique_ptr<sessions::SessionTab>& tab) { | |
431 return tab.get() == new_tab_ptr; | |
432 }); | |
433 if (tab_iter != window_iter_pair.second->wrapped_window.tabs.end()) { | |
434 window_iter_pair.second->wrapped_window.tabs.erase(tab_iter); | |
435 break; | |
436 } | |
437 } | |
438 } | |
439 | |
440 synced_tab_map_[local_session_tag_].erase(new_tab_iter); | |
441 } | |
442 | |
443 // If the old tab is unmapped, update the tab id under which it is | |
444 // indexed. | |
445 auto unmapped_tabs_iter = | |
446 unmapped_tabs_[local_session_tag_].find(old_tab_id); | |
447 if (old_tab_id != kInvalidTabID && | |
448 unmapped_tabs_iter != unmapped_tabs_[local_session_tag_].end()) { | |
449 std::unique_ptr<sessions::SessionTab> tab = | |
450 std::move(unmapped_tabs_iter->second); | |
451 DCHECK_EQ(tab_ptr, tab.get()); | |
452 unmapped_tabs_[local_session_tag_].erase(unmapped_tabs_iter); | |
453 unmapped_tabs_[local_session_tag_][new_tab_id] = std::move(tab); | |
454 } | |
455 } | |
456 | |
457 if (tab_ptr == nullptr) { | |
411 // It's possible a placeholder is already in place for the new tab. If so, | 458 // It's possible a placeholder is already in place for the new tab. If so, |
412 // reuse it, otherwise create a new one (which will default to unmapped). | 459 // reuse it, otherwise create a new one (which will default to unmapped). |
413 tab_ptr = GetTab(local_session_tag_, new_tab_id); | 460 tab_ptr = GetTab(local_session_tag_, new_tab_id); |
414 } | 461 } |
415 | 462 |
416 // If the old tab is unmapped, update the tab id under which it is indexed. | |
417 auto unmapped_tabs_iter = unmapped_tabs_[local_session_tag_].find(old_tab_id); | |
418 if (old_tab_id != kInvalidTabID && | |
419 unmapped_tabs_iter != unmapped_tabs_[local_session_tag_].end()) { | |
420 std::unique_ptr<sessions::SessionTab> tab = | |
421 std::move(unmapped_tabs_iter->second); | |
422 DCHECK_EQ(tab_ptr, tab.get()); | |
423 unmapped_tabs_[local_session_tag_].erase(unmapped_tabs_iter); | |
424 unmapped_tabs_[local_session_tag_][new_tab_id] = std::move(tab); | |
425 } | |
426 | |
427 // Update the tab id. | 463 // Update the tab id. |
428 if (old_tab_id != kInvalidTabID) { | 464 if (old_tab_id != kInvalidTabID) { |
429 DVLOG(1) << "Remapped tab " << old_tab_id << " with node " << tab_node_id | 465 DVLOG(1) << "Remapped tab " << old_tab_id << " with node " << tab_node_id |
430 << " to tab " << new_tab_id; | 466 << " to tab " << new_tab_id; |
431 } else { | 467 } else { |
432 DVLOG(1) << "Mapped new tab node " << tab_node_id << " to tab " | 468 DVLOG(1) << "Mapped new tab node " << tab_node_id << " to tab " |
433 << new_tab_id; | 469 << new_tab_id; |
434 } | 470 } |
435 tab_ptr->tab_id.set_id(new_tab_id); | 471 tab_ptr->tab_id.set_id(new_tab_id); |
436 | 472 |
(...skipping 13 matching lines...) Expand all Loading... | |
450 // Get rid of our convenience maps (does not delete the actual Window/Tabs | 486 // Get rid of our convenience maps (does not delete the actual Window/Tabs |
451 // themselves; they should have all been deleted above). | 487 // themselves; they should have all been deleted above). |
452 synced_window_map_.clear(); | 488 synced_window_map_.clear(); |
453 synced_tab_map_.clear(); | 489 synced_tab_map_.clear(); |
454 | 490 |
455 local_tab_pool_.Clear(); | 491 local_tab_pool_.Clear(); |
456 local_session_tag_.clear(); | 492 local_session_tag_.clear(); |
457 } | 493 } |
458 | 494 |
459 } // namespace sync_sessions | 495 } // namespace sync_sessions |
OLD | NEW |