Avi Drissman | 8ba1bad | 2022-09-13 19:22:36 | [diff] [blame] | 1 | // Copyright 2014 The Chromium Authors |
[email protected] | bc5223d | 2013-08-20 01:42:52 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
maxbogue | 8ef2508 | 2015-11-16 19:09:58 | [diff] [blame] | 5 | #include "components/sync_sessions/tab_node_pool.h" |
[email protected] | bc5223d | 2013-08-20 01:42:52 | [diff] [blame] | 6 | |
zea | 9b42f431 | 2017-03-01 19:15:44 | [diff] [blame] | 7 | #include <algorithm> |
Mikel Astiz | eca64c98 | 2018-09-27 13:50:05 | [diff] [blame] | 8 | #include <limits> |
Mikel Astiz | 07c2f92 | 2019-03-29 14:12:56 | [diff] [blame] | 9 | #include <utility> |
zea | 9b42f431 | 2017-03-01 19:15:44 | [diff] [blame] | 10 | |
[email protected] | bc5223d | 2013-08-20 01:42:52 | [diff] [blame] | 11 | #include "base/logging.h" |
Max Bogue | fef332d | 2016-07-28 22:09:09 | [diff] [blame] | 12 | #include "components/sync/base/model_type.h" |
| 13 | #include "components/sync/protocol/session_specifics.pb.h" |
pnoland | 1901afa5 | 2017-03-23 21:24:00 | [diff] [blame] | 14 | #include "components/sync_sessions/synced_tab_delegate.h" |
[email protected] | bc5223d | 2013-08-20 01:42:52 | [diff] [blame] | 15 | |
maxbogue | a79d99b7 | 2016-09-15 15:59:16 | [diff] [blame] | 16 | namespace sync_sessions { |
[email protected] | bc5223d | 2013-08-20 01:42:52 | [diff] [blame] | 17 | |
maxbogue | 8ef2508 | 2015-11-16 19:09:58 | [diff] [blame] | 18 | TabNodePool::TabNodePool() : max_used_tab_node_id_(kInvalidTabNodeID) {} |
[email protected] | bc5223d | 2013-08-20 01:42:52 | [diff] [blame] | 19 | |
| 20 | // static |
| 21 | // We start vending tab node IDs at 0. |
[email protected] | f4fa3fc | 2014-04-16 23:16:04 | [diff] [blame] | 22 | const int TabNodePool::kInvalidTabNodeID = -1; |
[email protected] | bc5223d | 2013-08-20 01:42:52 | [diff] [blame] | 23 | |
Victor Hugo Vianna Silva | 988fe732 | 2021-09-23 18:16:56 | [diff] [blame] | 24 | TabNodePool::~TabNodePool() = default; |
[email protected] | bc5223d | 2013-08-20 01:42:52 | [diff] [blame] | 25 | |
[email protected] | f4fa3fc | 2014-04-16 23:16:04 | [diff] [blame] | 26 | void TabNodePool::AddTabNode(int tab_node_id) { |
[email protected] | bc5223d | 2013-08-20 01:42:52 | [diff] [blame] | 27 | DCHECK_GT(tab_node_id, kInvalidTabNodeID); |
Mikel Astiz | eca64c98 | 2018-09-27 13:50:05 | [diff] [blame] | 28 | DCHECK_LE(tab_node_id, max_used_tab_node_id_); |
[email protected] | bc5223d | 2013-08-20 01:42:52 | [diff] [blame] | 29 | DCHECK(nodeid_tabid_map_.find(tab_node_id) == nodeid_tabid_map_.end()); |
zea | 9b42f431 | 2017-03-01 19:15:44 | [diff] [blame] | 30 | DVLOG(1) << "Adding tab node " << tab_node_id << " to pool."; |
zea | 9b42f431 | 2017-03-01 19:15:44 | [diff] [blame] | 31 | free_nodes_pool_.insert(tab_node_id); |
Mikel Astiz | eca64c98 | 2018-09-27 13:50:05 | [diff] [blame] | 32 | missing_nodes_pool_.erase(tab_node_id); |
[email protected] | bc5223d | 2013-08-20 01:42:52 | [diff] [blame] | 33 | } |
| 34 | |
Mikel Astiz | e023c57 | 2018-03-28 07:56:56 | [diff] [blame] | 35 | void TabNodePool::AssociateTabNode(int tab_node_id, SessionID tab_id) { |
[email protected] | bc5223d | 2013-08-20 01:42:52 | [diff] [blame] | 36 | DCHECK_GT(tab_node_id, kInvalidTabNodeID); |
Mikel Astiz | e023c57 | 2018-03-28 07:56:56 | [diff] [blame] | 37 | DCHECK(tab_id.is_valid()); |
zea | 9b42f431 | 2017-03-01 19:15:44 | [diff] [blame] | 38 | |
| 39 | // This is a new node association, the sync node should be free. |
| 40 | // Remove node from free node pool and then associate it with the tab. |
jdoerrie | 3feb185 | 2018-10-05 12:16:44 | [diff] [blame] | 41 | auto it = free_nodes_pool_.find(tab_node_id); |
zea | 9b42f431 | 2017-03-01 19:15:44 | [diff] [blame] | 42 | DCHECK(it != free_nodes_pool_.end()); |
| 43 | free_nodes_pool_.erase(it); |
| 44 | |
[email protected] | bc5223d | 2013-08-20 01:42:52 | [diff] [blame] | 45 | DCHECK(nodeid_tabid_map_.find(tab_node_id) == nodeid_tabid_map_.end()); |
Mikel Astiz | e023c57 | 2018-03-28 07:56:56 | [diff] [blame] | 46 | DVLOG(1) << "Associating tab node " << tab_node_id << " with tab " |
| 47 | << tab_id.id(); |
| 48 | nodeid_tabid_map_.emplace(tab_node_id, tab_id); |
zea | 9b42f431 | 2017-03-01 19:15:44 | [diff] [blame] | 49 | tabid_nodeid_map_[tab_id] = tab_node_id; |
[email protected] | bc5223d | 2013-08-20 01:42:52 | [diff] [blame] | 50 | } |
| 51 | |
Mikel Astiz | aadda591 | 2018-04-13 11:14:37 | [diff] [blame] | 52 | int TabNodePool::GetTabNodeIdFromTabId(SessionID tab_id) const { |
jdoerrie | 3feb185 | 2018-10-05 12:16:44 | [diff] [blame] | 53 | auto it = tabid_nodeid_map_.find(tab_id); |
Mikel Astiz | aadda591 | 2018-04-13 11:14:37 | [diff] [blame] | 54 | if (it != tabid_nodeid_map_.end()) { |
| 55 | return it->second; |
zea | 9b42f431 | 2017-03-01 19:15:44 | [diff] [blame] | 56 | } |
Mikel Astiz | aadda591 | 2018-04-13 11:14:37 | [diff] [blame] | 57 | return kInvalidTabNodeID; |
[email protected] | bc5223d | 2013-08-20 01:42:52 | [diff] [blame] | 58 | } |
| 59 | |
Mikel Astiz | e023c57 | 2018-03-28 07:56:56 | [diff] [blame] | 60 | void TabNodePool::FreeTab(SessionID tab_id) { |
| 61 | DCHECK(tab_id.is_valid()); |
jdoerrie | 3feb185 | 2018-10-05 12:16:44 | [diff] [blame] | 62 | auto it = tabid_nodeid_map_.find(tab_id); |
zea | 9b42f431 | 2017-03-01 19:15:44 | [diff] [blame] | 63 | if (it == tabid_nodeid_map_.end()) { |
| 64 | return; // Already freed. |
| 65 | } |
[email protected] | bc5223d | 2013-08-20 01:42:52 | [diff] [blame] | 66 | |
zea | 9b42f431 | 2017-03-01 19:15:44 | [diff] [blame] | 67 | int tab_node_id = it->second; |
Mikel Astiz | e023c57 | 2018-03-28 07:56:56 | [diff] [blame] | 68 | DVLOG(1) << "Freeing tab " << tab_id.id() << " at node " << tab_node_id; |
zea | 9b42f431 | 2017-03-01 19:15:44 | [diff] [blame] | 69 | nodeid_tabid_map_.erase(nodeid_tabid_map_.find(tab_node_id)); |
| 70 | tabid_nodeid_map_.erase(it); |
[email protected] | bc5223d | 2013-08-20 01:42:52 | [diff] [blame] | 71 | free_nodes_pool_.insert(tab_node_id); |
[email protected] | bc5223d | 2013-08-20 01:42:52 | [diff] [blame] | 72 | } |
| 73 | |
Mikel Astiz | cb491c4cb | 2018-05-24 13:08:26 | [diff] [blame] | 74 | int TabNodePool::AssociateWithFreeTabNode(SessionID tab_id) { |
| 75 | DCHECK_EQ(0U, tabid_nodeid_map_.count(tab_id)); |
| 76 | |
Mikel Astiz | aadda591 | 2018-04-13 11:14:37 | [diff] [blame] | 77 | int tab_node_id; |
Mikel Astiz | eca64c98 | 2018-09-27 13:50:05 | [diff] [blame] | 78 | if (free_nodes_pool_.empty() && missing_nodes_pool_.empty()) { |
| 79 | // Tab pool has neither free nodes nor "holes" within the ID range, so |
| 80 | // allocate a new one by extending the range. |
Mikel Astiz | aadda591 | 2018-04-13 11:14:37 | [diff] [blame] | 81 | tab_node_id = ++max_used_tab_node_id_; |
| 82 | AddTabNode(tab_node_id); |
| 83 | } else { |
Mikel Astiz | eca64c98 | 2018-09-27 13:50:05 | [diff] [blame] | 84 | tab_node_id = std::numeric_limits<int>::max(); |
| 85 | // Take the smallest available, either from the freed pool or from IDs that |
| 86 | // were never associated before (but are within 0..max_used_tab_node_id_). |
| 87 | if (!free_nodes_pool_.empty()) { |
| 88 | tab_node_id = *free_nodes_pool_.begin(); |
Mikel Astiz | 5dc588e | 2018-10-05 13:06:02 | [diff] [blame] | 89 | DCHECK_LE(tab_node_id, max_used_tab_node_id_); |
Mikel Astiz | eca64c98 | 2018-09-27 13:50:05 | [diff] [blame] | 90 | } |
| 91 | if (!missing_nodes_pool_.empty() && |
| 92 | *missing_nodes_pool_.begin() < tab_node_id) { |
| 93 | tab_node_id = *missing_nodes_pool_.begin(); |
Mikel Astiz | 5dc588e | 2018-10-05 13:06:02 | [diff] [blame] | 94 | DCHECK_LE(tab_node_id, max_used_tab_node_id_); |
Mikel Astiz | eca64c98 | 2018-09-27 13:50:05 | [diff] [blame] | 95 | AddTabNode(tab_node_id); |
| 96 | } |
Mikel Astiz | aadda591 | 2018-04-13 11:14:37 | [diff] [blame] | 97 | } |
| 98 | |
| 99 | AssociateTabNode(tab_node_id, tab_id); |
| 100 | return tab_node_id; |
| 101 | } |
| 102 | |
Mikel Astiz | e023c57 | 2018-03-28 07:56:56 | [diff] [blame] | 103 | void TabNodePool::ReassociateTabNode(int tab_node_id, SessionID tab_id) { |
zea | 9b42f431 | 2017-03-01 19:15:44 | [diff] [blame] | 104 | DCHECK_GT(tab_node_id, kInvalidTabNodeID); |
Mikel Astiz | e023c57 | 2018-03-28 07:56:56 | [diff] [blame] | 105 | DCHECK(tab_id.is_valid()); |
zea | 9b42f431 | 2017-03-01 19:15:44 | [diff] [blame] | 106 | |
| 107 | auto tabid_it = tabid_nodeid_map_.find(tab_id); |
| 108 | if (tabid_it != tabid_nodeid_map_.end()) { |
| 109 | if (tabid_it->second == tab_node_id) { |
| 110 | return; // Already associated properly. |
| 111 | } else { |
| 112 | // Another node is already associated with this tab. Free it. |
| 113 | FreeTab(tab_id); |
| 114 | } |
zea | 9e83363 | 2017-02-10 18:59:11 | [diff] [blame] | 115 | } |
zea | 9b42f431 | 2017-03-01 19:15:44 | [diff] [blame] | 116 | |
| 117 | auto nodeid_it = nodeid_tabid_map_.find(tab_node_id); |
| 118 | if (nodeid_it != nodeid_tabid_map_.end()) { |
| 119 | // This node was already associated with another tab. Free it. |
| 120 | FreeTab(nodeid_it->second); |
| 121 | } else { |
Mikel Astiz | eca64c98 | 2018-09-27 13:50:05 | [diff] [blame] | 122 | // This is a new tab node. Add it before association. We also need to |
| 123 | // remember the "holes". |
| 124 | for (int missing_tab_node_id = max_used_tab_node_id_ + 1; |
| 125 | missing_tab_node_id < tab_node_id; ++missing_tab_node_id) { |
| 126 | missing_nodes_pool_.insert(missing_tab_node_id); |
| 127 | } |
| 128 | max_used_tab_node_id_ = std::max(max_used_tab_node_id_, tab_node_id); |
zea | 9b42f431 | 2017-03-01 19:15:44 | [diff] [blame] | 129 | AddTabNode(tab_node_id); |
| 130 | } |
| 131 | |
| 132 | AssociateTabNode(tab_node_id, tab_id); |
[email protected] | bc5223d | 2013-08-20 01:42:52 | [diff] [blame] | 133 | } |
| 134 | |
Mikel Astiz | e023c57 | 2018-03-28 07:56:56 | [diff] [blame] | 135 | SessionID TabNodePool::GetTabIdFromTabNodeId(int tab_node_id) const { |
jdoerrie | 3feb185 | 2018-10-05 12:16:44 | [diff] [blame] | 136 | auto it = nodeid_tabid_map_.find(tab_node_id); |
[email protected] | bc5223d | 2013-08-20 01:42:52 | [diff] [blame] | 137 | if (it != nodeid_tabid_map_.end()) { |
| 138 | return it->second; |
| 139 | } |
Mikel Astiz | e023c57 | 2018-03-28 07:56:56 | [diff] [blame] | 140 | return SessionID::InvalidValue(); |
[email protected] | bc5223d | 2013-08-20 01:42:52 | [diff] [blame] | 141 | } |
| 142 | |
Mohamed Amir Yosef | 7d39a72 | 2019-06-07 08:51:21 | [diff] [blame] | 143 | std::set<int> TabNodePool::CleanupFreeTabNodes() { |
Mahmoud Rashad | fa26bb1d | 2022-02-11 20:17:41 | [diff] [blame] | 144 | // Convert all free nodes into missing nodes, each representing a deletion. |
| 145 | missing_nodes_pool_.insert(free_nodes_pool_.begin(), free_nodes_pool_.end()); |
| 146 | std::set<int> deleted_node_ids = std::move(free_nodes_pool_); |
| 147 | free_nodes_pool_.clear(); |
Mikel Astiz | 5dc588e | 2018-10-05 13:06:02 | [diff] [blame] | 148 | |
Mahmoud Rashad | fa26bb1d | 2022-02-11 20:17:41 | [diff] [blame] | 149 | // As an optimization to save memory, update |max_used_tab_node_id_| and |
| 150 | // shrink |missing_nodes_pool_| appropriately. |
| 151 | if (nodeid_tabid_map_.empty()) { |
| 152 | max_used_tab_node_id_ = kInvalidTabNodeID; |
| 153 | } else { |
| 154 | max_used_tab_node_id_ = nodeid_tabid_map_.rbegin()->first; |
Mikel Astiz | 5dc588e | 2018-10-05 13:06:02 | [diff] [blame] | 155 | } |
| 156 | |
Mahmoud Rashad | fa26bb1d | 2022-02-11 20:17:41 | [diff] [blame] | 157 | missing_nodes_pool_.erase( |
| 158 | missing_nodes_pool_.upper_bound(max_used_tab_node_id_), |
| 159 | missing_nodes_pool_.end()); |
Mikel Astiz | eca64c98 | 2018-09-27 13:50:05 | [diff] [blame] | 160 | |
Mikel Astiz | 07c2f92 | 2019-03-29 14:12:56 | [diff] [blame] | 161 | return deleted_node_ids; |
[email protected] | bc5223d | 2013-08-20 01:42:52 | [diff] [blame] | 162 | } |
| 163 | |
Mikel Astiz | db744b64 | 2018-04-20 10:17:14 | [diff] [blame] | 164 | void TabNodePool::DeleteTabNode(int tab_node_id) { |
jdoerrie | 3feb185 | 2018-10-05 12:16:44 | [diff] [blame] | 165 | auto it = nodeid_tabid_map_.find(tab_node_id); |
Mikel Astiz | db744b64 | 2018-04-20 10:17:14 | [diff] [blame] | 166 | if (it == nodeid_tabid_map_.end()) { |
| 167 | free_nodes_pool_.erase(tab_node_id); |
| 168 | return; |
| 169 | } |
| 170 | |
| 171 | DCHECK_EQ(0U, free_nodes_pool_.count(tab_node_id)); |
| 172 | |
| 173 | SessionID tab_id = it->second; |
| 174 | DVLOG(1) << "Deleting node " << tab_node_id << " with tab ID " << tab_id; |
| 175 | tabid_nodeid_map_.erase(tab_id); |
| 176 | nodeid_tabid_map_.erase(it); |
| 177 | } |
| 178 | |
Mikel Astiz | db744b64 | 2018-04-20 10:17:14 | [diff] [blame] | 179 | std::set<int> TabNodePool::GetAllTabNodeIds() const { |
| 180 | std::set<int> tab_node_ids = free_nodes_pool_; |
Victor Hugo Vianna Silva | 98c8fea | 2022-02-09 17:45:58 | [diff] [blame] | 181 | for (const auto& [tab_node_id, tab_id] : nodeid_tabid_map_) { |
| 182 | tab_node_ids.insert(tab_node_id); |
Mikel Astiz | db744b64 | 2018-04-20 10:17:14 | [diff] [blame] | 183 | } |
| 184 | return tab_node_ids; |
| 185 | } |
| 186 | |
Mikel Astiz | 5dc588e | 2018-10-05 13:06:02 | [diff] [blame] | 187 | int TabNodePool::GetMaxUsedTabNodeIdForTest() const { |
| 188 | return max_used_tab_node_id_; |
| 189 | } |
| 190 | |
maxbogue | a79d99b7 | 2016-09-15 15:59:16 | [diff] [blame] | 191 | } // namespace sync_sessions |