| 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" | 
| 11 #include "base/strings/utf_string_conversions.h" | 11 #include "base/strings/utf_string_conversions.h" | 
| 12 #include "components/sync_sessions/sync_sessions_client.h" | 12 #include "components/sync_sessions/sync_sessions_client.h" | 
| 13 | 13 #include "components/sync_sessions/synced_tab_delegate.h" | 
| 14 namespace sync_sessions { | 14 namespace sync_sessions { | 
| 15 | 15 | 
| 16 namespace { | 16 namespace { | 
| 17 | 17 | 
| 18 // Helper for iterating through all tabs within a window, and all navigations | 18 // Helper for iterating through all tabs within a window, and all navigations | 
| 19 // within a tab, to find if there's a valid syncable url. | 19 // within a tab, to find if there's a valid syncable url. | 
| 20 bool ShouldSyncSessionWindow(SyncSessionsClient* sessions_client, | 20 bool ShouldSyncSessionWindow(SyncSessionsClient* sessions_client, | 
| 21                              const sessions::SessionWindow& window) { | 21                              const sessions::SessionWindow& window) { | 
| 22   for (const auto& tab : window.tabs) { | 22   for (const auto& tab : window.tabs) { | 
| 23     for (const sessions::SerializedNavigationEntry& navigation : | 23     for (const sessions::SerializedNavigationEntry& navigation : | 
| (...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after  Loading... | 
| 85   for (const auto& window_pair : iter->second->windows) | 85   for (const auto& window_pair : iter->second->windows) | 
| 86     windows->push_back(window_pair.second.get()); | 86     windows->push_back(window_pair.second.get()); | 
| 87 | 87 | 
| 88   return true; | 88   return true; | 
| 89 } | 89 } | 
| 90 | 90 | 
| 91 bool SyncedSessionTracker::LookupSessionTab( | 91 bool SyncedSessionTracker::LookupSessionTab( | 
| 92     const std::string& tag, | 92     const std::string& tag, | 
| 93     SessionID::id_type tab_id, | 93     SessionID::id_type tab_id, | 
| 94     const sessions::SessionTab** tab) const { | 94     const sessions::SessionTab** tab) const { | 
| 95   if (tab_id == TabNodePool::kInvalidTabID) | 95   if (tab_id == kInvalidTabID) | 
| 96     return false; | 96     return false; | 
| 97 | 97 | 
| 98   DCHECK(tab); | 98   DCHECK(tab); | 
| 99   auto tab_map_iter = synced_tab_map_.find(tag); | 99   auto tab_map_iter = synced_tab_map_.find(tag); | 
| 100   if (tab_map_iter == synced_tab_map_.end()) { | 100   if (tab_map_iter == synced_tab_map_.end()) { | 
| 101     // We have no record of this session. | 101     // We have no record of this session. | 
| 102     *tab = nullptr; | 102     *tab = nullptr; | 
| 103     return false; | 103     return false; | 
| 104   } | 104   } | 
| 105   auto tab_iter = tab_map_iter->second.find(tab_id); | 105   auto tab_iter = tab_map_iter->second.find(tab_id); | 
| (...skipping 269 matching lines...) Expand 10 before | Expand all | Expand 10 after  Loading... | 
| 375   bool reused_existing_tab = | 375   bool reused_existing_tab = | 
| 376       local_tab_pool_.GetTabNodeForTab(tab_id, tab_node_id); | 376       local_tab_pool_.GetTabNodeForTab(tab_id, tab_node_id); | 
| 377   DCHECK_NE(TabNodePool::kInvalidTabNodeID, *tab_node_id); | 377   DCHECK_NE(TabNodePool::kInvalidTabNodeID, *tab_node_id); | 
| 378   GetSession(local_session_tag_)->tab_node_ids.insert(*tab_node_id); | 378   GetSession(local_session_tag_)->tab_node_ids.insert(*tab_node_id); | 
| 379   return reused_existing_tab; | 379   return reused_existing_tab; | 
| 380 } | 380 } | 
| 381 | 381 | 
| 382 bool SyncedSessionTracker::IsLocalTabNodeAssociated(int tab_node_id) { | 382 bool SyncedSessionTracker::IsLocalTabNodeAssociated(int tab_node_id) { | 
| 383   if (tab_node_id == TabNodePool::kInvalidTabNodeID) | 383   if (tab_node_id == TabNodePool::kInvalidTabNodeID) | 
| 384     return false; | 384     return false; | 
| 385   return local_tab_pool_.GetTabIdFromTabNodeId(tab_node_id) != | 385   return local_tab_pool_.GetTabIdFromTabNodeId(tab_node_id) != kInvalidTabID; | 
| 386          TabNodePool::kInvalidTabID; |  | 
| 387 } | 386 } | 
| 388 | 387 | 
| 389 void SyncedSessionTracker::ReassociateLocalTab(int tab_node_id, | 388 void SyncedSessionTracker::ReassociateLocalTab(int tab_node_id, | 
| 390                                                SessionID::id_type new_tab_id) { | 389                                                SessionID::id_type new_tab_id) { | 
| 391   DCHECK(!local_session_tag_.empty()); | 390   DCHECK(!local_session_tag_.empty()); | 
| 392   DCHECK_NE(TabNodePool::kInvalidTabNodeID, tab_node_id); | 391   DCHECK_NE(TabNodePool::kInvalidTabNodeID, tab_node_id); | 
| 393   DCHECK_NE(TabNodePool::kInvalidTabID, new_tab_id); | 392   DCHECK_NE(kInvalidTabID, new_tab_id); | 
| 394 | 393 | 
| 395   SessionID::id_type old_tab_id = | 394   SessionID::id_type old_tab_id = | 
| 396       local_tab_pool_.GetTabIdFromTabNodeId(tab_node_id); | 395       local_tab_pool_.GetTabIdFromTabNodeId(tab_node_id); | 
| 397   local_tab_pool_.ReassociateTabNode(tab_node_id, new_tab_id); | 396   local_tab_pool_.ReassociateTabNode(tab_node_id, new_tab_id); | 
| 398 | 397 | 
| 399   sessions::SessionTab* tab_ptr = nullptr; | 398   sessions::SessionTab* tab_ptr = nullptr; | 
| 400 | 399 | 
| 401   auto old_tab_iter = synced_tab_map_[local_session_tag_].find(old_tab_id); | 400   auto old_tab_iter = synced_tab_map_[local_session_tag_].find(old_tab_id); | 
| 402   if (old_tab_id != TabNodePool::kInvalidTabID && | 401   if (old_tab_id != kInvalidTabID && | 
| 403       old_tab_iter != synced_tab_map_[local_session_tag_].end()) { | 402       old_tab_iter != synced_tab_map_[local_session_tag_].end()) { | 
| 404     tab_ptr = old_tab_iter->second; | 403     tab_ptr = old_tab_iter->second; | 
| 405     // Remove the tab from the synced tab map under the old id. | 404     // Remove the tab from the synced tab map under the old id. | 
| 406     synced_tab_map_[local_session_tag_].erase(old_tab_iter); | 405     synced_tab_map_[local_session_tag_].erase(old_tab_iter); | 
| 407   } else { | 406   } else { | 
| 408     // It's possible a placeholder is already in place for the new tab. If so, | 407     // 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). | 408     // reuse it, otherwise create a new one (which will default to unmapped). | 
| 410     tab_ptr = GetTab(local_session_tag_, new_tab_id); | 409     tab_ptr = GetTab(local_session_tag_, new_tab_id); | 
| 411   } | 410   } | 
| 412 | 411 | 
| 413   // If the old tab is unmapped, update the tab id under which it is indexed. | 412   // 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); | 413   auto unmapped_tabs_iter = unmapped_tabs_[local_session_tag_].find(old_tab_id); | 
| 415   if (old_tab_id != TabNodePool::kInvalidTabID && | 414   if (old_tab_id != kInvalidTabID && | 
| 416       unmapped_tabs_iter != unmapped_tabs_[local_session_tag_].end()) { | 415       unmapped_tabs_iter != unmapped_tabs_[local_session_tag_].end()) { | 
| 417     std::unique_ptr<sessions::SessionTab> tab = | 416     std::unique_ptr<sessions::SessionTab> tab = | 
| 418         std::move(unmapped_tabs_iter->second); | 417         std::move(unmapped_tabs_iter->second); | 
| 419     DCHECK_EQ(tab_ptr, tab.get()); | 418     DCHECK_EQ(tab_ptr, tab.get()); | 
| 420     unmapped_tabs_[local_session_tag_].erase(unmapped_tabs_iter); | 419     unmapped_tabs_[local_session_tag_].erase(unmapped_tabs_iter); | 
| 421     unmapped_tabs_[local_session_tag_][new_tab_id] = std::move(tab); | 420     unmapped_tabs_[local_session_tag_][new_tab_id] = std::move(tab); | 
| 422   } | 421   } | 
| 423 | 422 | 
| 424   // Update the tab id. | 423   // Update the tab id. | 
| 425   if (old_tab_id != TabNodePool::kInvalidTabID) { | 424   if (old_tab_id != kInvalidTabID) { | 
| 426     DVLOG(1) << "Remapped tab " << old_tab_id << " with node " << tab_node_id | 425     DVLOG(1) << "Remapped tab " << old_tab_id << " with node " << tab_node_id | 
| 427              << " to tab " << new_tab_id; | 426              << " to tab " << new_tab_id; | 
| 428   } else { | 427   } else { | 
| 429     DVLOG(1) << "Mapped new tab node " << tab_node_id << " to tab " | 428     DVLOG(1) << "Mapped new tab node " << tab_node_id << " to tab " | 
| 430              << new_tab_id; | 429              << new_tab_id; | 
| 431   } | 430   } | 
| 432   tab_ptr->tab_id.set_id(new_tab_id); | 431   tab_ptr->tab_id.set_id(new_tab_id); | 
| 433 | 432 | 
| 434   // Add the tab back into the tab map with the new id. | 433   // Add the tab back into the tab map with the new id. | 
| 435   synced_tab_map_[local_session_tag_][new_tab_id] = tab_ptr; | 434   synced_tab_map_[local_session_tag_][new_tab_id] = tab_ptr; | 
| (...skipping 11 matching lines...) Expand all  Loading... | 
| 447   // Get rid of our convenience maps (does not delete the actual Window/Tabs | 446   // Get rid of our convenience maps (does not delete the actual Window/Tabs | 
| 448   // themselves; they should have all been deleted above). | 447   // themselves; they should have all been deleted above). | 
| 449   synced_window_map_.clear(); | 448   synced_window_map_.clear(); | 
| 450   synced_tab_map_.clear(); | 449   synced_tab_map_.clear(); | 
| 451 | 450 | 
| 452   local_tab_pool_.Clear(); | 451   local_tab_pool_.Clear(); | 
| 453   local_session_tag_.clear(); | 452   local_session_tag_.clear(); | 
| 454 } | 453 } | 
| 455 | 454 | 
| 456 }  // namespace sync_sessions | 455 }  // namespace sync_sessions | 
| OLD | NEW | 
|---|