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
3 // found in the LICENSE file.
5 #include "chrome/browser/sync/glue/tab_node_pool.h"
7 #include "base/format_macros.h"
8 #include "base/logging.h"
9 #include "base/strings/stringprintf.h"
10 #include "base/strings/utf_string_conversions.h"
11 #include "chrome/browser/sync/profile_sync_service.h"
12 #include "sync/internal_api/public/base/model_type.h"
13 #include "sync/internal_api/public/read_node.h"
14 #include "sync/internal_api/public/write_node.h"
15 #include "sync/internal_api/public/write_transaction.h"
17 namespace browser_sync
{
19 static const char kNoSessionsFolderError
[] =
20 "Server did not create the top-level sessions node. We "
21 "might be running against an out-of-date server.";
23 const size_t TabNodePool::kFreeNodesLowWatermark
= 25;
24 const size_t TabNodePool::kFreeNodesHighWatermark
= 100;
26 TabNodePool::TabNodePool(ProfileSyncService
* sync_service
)
27 : max_used_tab_node_id_(kInvalidTabNodeID
),
28 sync_service_(sync_service
) {}
31 // We start vending tab node IDs at 0.
32 const int TabNodePool::kInvalidTabNodeID
= -1;
34 TabNodePool::~TabNodePool() {}
37 std::string
TabNodePool::TabIdToTag(
38 const std::string machine_tag
, int tab_node_id
) {
39 return base::StringPrintf("%s %d", machine_tag
.c_str(), tab_node_id
);
42 void TabNodePool::AddTabNode(int tab_node_id
) {
43 DCHECK_GT(tab_node_id
, kInvalidTabNodeID
);
44 DCHECK(nodeid_tabid_map_
.find(tab_node_id
) == nodeid_tabid_map_
.end());
45 unassociated_nodes_
.insert(tab_node_id
);
46 if (max_used_tab_node_id_
< tab_node_id
)
47 max_used_tab_node_id_
= tab_node_id
;
50 void TabNodePool::AssociateTabNode(int tab_node_id
,
51 SessionID::id_type tab_id
) {
52 DCHECK_GT(tab_node_id
, kInvalidTabNodeID
);
53 // Remove sync node if it is in unassociated nodes pool.
54 std::set
<int>::iterator u_it
= unassociated_nodes_
.find(tab_node_id
);
55 if (u_it
!= unassociated_nodes_
.end()) {
56 unassociated_nodes_
.erase(u_it
);
58 // This is a new node association, the sync node should be free.
59 // Remove node from free node pool and then associate it with the tab.
60 std::set
<int>::iterator it
= free_nodes_pool_
.find(tab_node_id
);
61 DCHECK(it
!= free_nodes_pool_
.end());
62 free_nodes_pool_
.erase(it
);
64 DCHECK(nodeid_tabid_map_
.find(tab_node_id
) == nodeid_tabid_map_
.end());
65 nodeid_tabid_map_
[tab_node_id
] = tab_id
;
68 int TabNodePool::GetFreeTabNode() {
69 DCHECK_GT(machine_tag_
.length(), 0U);
70 if (free_nodes_pool_
.empty()) {
71 // Tab pool has no free nodes, allocate new one.
72 syncer::WriteTransaction
trans(FROM_HERE
, sync_service_
->GetUserShare());
73 syncer::ReadNode
root(&trans
);
74 if (root
.InitByTagLookup(syncer::ModelTypeToRootTag(syncer::SESSIONS
)) !=
75 syncer::BaseNode::INIT_OK
) {
76 LOG(ERROR
) << kNoSessionsFolderError
;
77 return kInvalidTabNodeID
;
79 int tab_node_id
= ++max_used_tab_node_id_
;
80 std::string tab_node_tag
= TabIdToTag(machine_tag_
, tab_node_id
);
81 syncer::WriteNode
tab_node(&trans
);
82 syncer::WriteNode::InitUniqueByCreationResult result
=
83 tab_node
.InitUniqueByCreation(syncer::SESSIONS
, root
, tab_node_tag
);
84 if (result
!= syncer::WriteNode::INIT_SUCCESS
) {
85 LOG(ERROR
) << "Could not create new node with tag "
86 << tab_node_tag
<< "!";
87 return kInvalidTabNodeID
;
89 // We fill the new node with just enough data so that in case of a crash/bug
90 // we can identify the node as our own on re-association and reuse it.
91 tab_node
.SetTitle(base::UTF8ToWide(tab_node_tag
));
92 sync_pb::SessionSpecifics specifics
;
93 specifics
.set_session_tag(machine_tag_
);
94 specifics
.set_tab_node_id(tab_node_id
);
95 tab_node
.SetSessionSpecifics(specifics
);
97 // Grow the pool by 1 since we created a new node.
98 DVLOG(1) << "Adding sync node " << tab_node_id
99 << " to tab node id pool";
100 free_nodes_pool_
.insert(tab_node_id
);
103 // Return the next free node.
104 return *free_nodes_pool_
.begin();
108 void TabNodePool::FreeTabNode(int tab_node_id
) {
109 TabNodeIDToTabIDMap::iterator it
= nodeid_tabid_map_
.find(tab_node_id
);
110 DCHECK(it
!= nodeid_tabid_map_
.end());
111 nodeid_tabid_map_
.erase(it
);
112 FreeTabNodeInternal(tab_node_id
);
115 void TabNodePool::FreeTabNodeInternal(int tab_node_id
) {
116 DCHECK(free_nodes_pool_
.find(tab_node_id
) == free_nodes_pool_
.end());
117 free_nodes_pool_
.insert(tab_node_id
);
119 // If number of free nodes exceed kFreeNodesHighWatermark,
120 // delete sync nodes till number reaches kFreeNodesLowWatermark.
121 // Note: This logic is to mitigate temporary disassociation issues with old
122 // clients: http://crbug.com/259918. Newer versions do not need this.
123 if (free_nodes_pool_
.size() > kFreeNodesHighWatermark
) {
124 syncer::WriteTransaction
trans(FROM_HERE
, sync_service_
->GetUserShare());
125 for (std::set
<int>::iterator free_it
= free_nodes_pool_
.begin();
126 free_it
!= free_nodes_pool_
.end();) {
127 syncer::WriteNode
tab_node(&trans
);
128 const std::string tab_node_tag
= TabIdToTag(machine_tag_
, *free_it
);
129 if (tab_node
.InitByClientTagLookup(syncer::SESSIONS
, tab_node_tag
) !=
130 syncer::BaseNode::INIT_OK
) {
131 LOG(ERROR
) << "Could not find sync node with tag: " << tab_node_tag
;
134 free_nodes_pool_
.erase(free_it
++);
135 tab_node
.Tombstone();
136 if (free_nodes_pool_
.size() <= kFreeNodesLowWatermark
) {
143 bool TabNodePool::IsUnassociatedTabNode(int tab_node_id
) {
144 return unassociated_nodes_
.find(tab_node_id
) != unassociated_nodes_
.end();
147 void TabNodePool::ReassociateTabNode(int tab_node_id
,
148 SessionID::id_type tab_id
) {
149 // Remove from list of unassociated sync_nodes if present.
150 std::set
<int>::iterator it
= unassociated_nodes_
.find(tab_node_id
);
151 if (it
!= unassociated_nodes_
.end()) {
152 unassociated_nodes_
.erase(it
);
154 // tab_node_id must be an already associated node.
155 DCHECK(nodeid_tabid_map_
.find(tab_node_id
) != nodeid_tabid_map_
.end());
157 nodeid_tabid_map_
[tab_node_id
] = tab_id
;
160 SessionID::id_type
TabNodePool::GetTabIdFromTabNodeId(
161 int tab_node_id
) const {
162 TabNodeIDToTabIDMap::const_iterator it
= nodeid_tabid_map_
.find(tab_node_id
);
163 if (it
!= nodeid_tabid_map_
.end()) {
166 return kInvalidTabID
;
169 void TabNodePool::DeleteUnassociatedTabNodes() {
170 syncer::WriteTransaction
trans(FROM_HERE
, sync_service_
->GetUserShare());
171 for (std::set
<int>::iterator it
= unassociated_nodes_
.begin();
172 it
!= unassociated_nodes_
.end();) {
173 syncer::WriteNode
tab_node(&trans
);
174 const std::string tab_node_tag
= TabIdToTag(machine_tag_
, *it
);
175 if (tab_node
.InitByClientTagLookup(syncer::SESSIONS
, tab_node_tag
) !=
176 syncer::BaseNode::INIT_OK
) {
177 LOG(ERROR
) << "Could not find sync node with tag: " << tab_node_tag
;
179 tab_node
.Tombstone();
181 unassociated_nodes_
.erase(it
++);
183 DCHECK(unassociated_nodes_
.empty());
187 void TabNodePool::Clear() {
188 unassociated_nodes_
.clear();
189 free_nodes_pool_
.clear();
190 nodeid_tabid_map_
.clear();
191 max_used_tab_node_id_
= kInvalidTabNodeID
;
194 size_t TabNodePool::Capacity() const {
195 return nodeid_tabid_map_
.size() + unassociated_nodes_
.size() +
196 free_nodes_pool_
.size();
199 bool TabNodePool::Empty() const { return free_nodes_pool_
.empty(); }
201 bool TabNodePool::Full() { return nodeid_tabid_map_
.empty(); }
203 void TabNodePool::SetMachineTag(const std::string
& machine_tag
) {
204 machine_tag_
= machine_tag
;
207 } // namespace browser_sync