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

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

Issue 2856913007: [Sync] Handle reassociation of tabs where the old tab is already mapped. (Closed)
Patch Set: Address comments Created 3 years, 7 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 379 matching lines...) Expand 10 before | Expand all | Expand 10 after
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;
413 DCHECK(tab_ptr);
414
408 // Remove the tab from the synced tab map under the old id. 415 // Remove the tab from the synced tab map under the old id.
409 synced_tab_map_[local_session_tag_].erase(old_tab_iter); 416 synced_tab_map_[local_session_tag_].erase(old_tab_iter);
410 } else { 417
418 if (new_tab_iter != synced_tab_map_[local_session_tag_].end()) {
419 // If both the old and the new tab already exist, delete the new tab
420 // and use the old tab in its place.
421 auto unmapped_tabs_iter =
422 unmapped_tabs_[local_session_tag_].find(new_tab_id);
423 if (unmapped_tabs_iter != unmapped_tabs_[local_session_tag_].end()) {
424 unmapped_tabs_[local_session_tag_].erase(unmapped_tabs_iter);
425 } else {
426 sessions::SessionTab* new_tab_ptr = new_tab_iter->second;
427 for (auto& window_iter_pair : GetSession(local_session_tag_)->windows) {
428 auto& window_tabs = window_iter_pair.second->wrapped_window.tabs;
429 auto tab_iter = std::find_if(
430 window_tabs.begin(), window_tabs.end(),
431 [&new_tab_ptr](const std::unique_ptr<sessions::SessionTab>& tab) {
432 return tab.get() == new_tab_ptr;
433 });
434 if (tab_iter != window_tabs.end()) {
435 window_tabs.erase(tab_iter);
436 break;
437 }
438 }
439 }
440
441 synced_tab_map_[local_session_tag_].erase(new_tab_iter);
442 }
443
444 // If the old tab is unmapped, update the tab id under which it is
445 // indexed.
446 auto unmapped_tabs_iter =
447 unmapped_tabs_[local_session_tag_].find(old_tab_id);
448 if (old_tab_id != kInvalidTabID &&
449 unmapped_tabs_iter != unmapped_tabs_[local_session_tag_].end()) {
450 std::unique_ptr<sessions::SessionTab> tab =
451 std::move(unmapped_tabs_iter->second);
452 DCHECK_EQ(tab_ptr, tab.get());
453 unmapped_tabs_[local_session_tag_].erase(unmapped_tabs_iter);
454 unmapped_tabs_[local_session_tag_][new_tab_id] = std::move(tab);
455 }
456 }
457
458 if (tab_ptr == nullptr) {
411 // It's possible a placeholder is already in place for the new tab. If so, 459 // 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). 460 // reuse it, otherwise create a new one (which will default to unmapped).
413 tab_ptr = GetTab(local_session_tag_, new_tab_id); 461 tab_ptr = GetTab(local_session_tag_, new_tab_id);
414 } 462 }
415 463
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. 464 // Update the tab id.
428 if (old_tab_id != kInvalidTabID) { 465 if (old_tab_id != kInvalidTabID) {
429 DVLOG(1) << "Remapped tab " << old_tab_id << " with node " << tab_node_id 466 DVLOG(1) << "Remapped tab " << old_tab_id << " with node " << tab_node_id
430 << " to tab " << new_tab_id; 467 << " to tab " << new_tab_id;
431 } else { 468 } else {
432 DVLOG(1) << "Mapped new tab node " << tab_node_id << " to tab " 469 DVLOG(1) << "Mapped new tab node " << tab_node_id << " to tab "
433 << new_tab_id; 470 << new_tab_id;
434 } 471 }
435 tab_ptr->tab_id.set_id(new_tab_id); 472 tab_ptr->tab_id.set_id(new_tab_id);
436 473
(...skipping 13 matching lines...) Expand all
450 // Get rid of our convenience maps (does not delete the actual Window/Tabs 487 // Get rid of our convenience maps (does not delete the actual Window/Tabs
451 // themselves; they should have all been deleted above). 488 // themselves; they should have all been deleted above).
452 synced_window_map_.clear(); 489 synced_window_map_.clear();
453 synced_tab_map_.clear(); 490 synced_tab_map_.clear();
454 491
455 local_tab_pool_.Clear(); 492 local_tab_pool_.Clear();
456 local_session_tag_.clear(); 493 local_session_tag_.clear();
457 } 494 }
458 495
459 } // namespace sync_sessions 496 } // 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