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

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

Issue 1443453002: [Sync] Move sessions files from sync_driver to sync_sessions. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Run formatter. Created 5 years, 1 month 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 2014 The Chromium Authors. All rights reserved. 1 // Copyright 2014 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_driver/tab_node_pool.h" 5 #include "components/sync_sessions/tab_node_pool.h"
6 6
7 #include "base/format_macros.h" 7 #include "base/format_macros.h"
8 #include "base/logging.h" 8 #include "base/logging.h"
9 #include "base/strings/stringprintf.h" 9 #include "base/strings/stringprintf.h"
10 #include "sync/api/sync_change.h" 10 #include "sync/api/sync_change.h"
11 #include "sync/api/sync_data.h" 11 #include "sync/api/sync_data.h"
12 #include "sync/internal_api/public/base/model_type.h" 12 #include "sync/internal_api/public/base/model_type.h"
13 #include "sync/protocol/session_specifics.pb.h" 13 #include "sync/protocol/session_specifics.pb.h"
14 #include "sync/protocol/sync.pb.h" 14 #include "sync/protocol/sync.pb.h"
15 15
16 namespace browser_sync { 16 namespace browser_sync {
17 17
18 const size_t TabNodePool::kFreeNodesLowWatermark = 25; 18 const size_t TabNodePool::kFreeNodesLowWatermark = 25;
19 const size_t TabNodePool::kFreeNodesHighWatermark = 100; 19 const size_t TabNodePool::kFreeNodesHighWatermark = 100;
20 20
21 TabNodePool::TabNodePool() 21 TabNodePool::TabNodePool() : max_used_tab_node_id_(kInvalidTabNodeID) {}
22 : max_used_tab_node_id_(kInvalidTabNodeID) {}
23 22
24 // static 23 // static
25 // We start vending tab node IDs at 0. 24 // We start vending tab node IDs at 0.
26 const int TabNodePool::kInvalidTabNodeID = -1; 25 const int TabNodePool::kInvalidTabNodeID = -1;
27 26
28 TabNodePool::~TabNodePool() {} 27 TabNodePool::~TabNodePool() {}
29 28
30 // Static 29 // Static
31 std::string TabNodePool::TabIdToTag(const std::string& machine_tag, 30 std::string TabNodePool::TabIdToTag(const std::string& machine_tag,
32 int tab_node_id) { 31 int tab_node_id) {
33 return base::StringPrintf("%s %d", machine_tag.c_str(), tab_node_id); 32 return base::StringPrintf("%s %d", machine_tag.c_str(), tab_node_id);
34 } 33 }
35 34
36 void TabNodePool::AddTabNode(int tab_node_id) { 35 void TabNodePool::AddTabNode(int tab_node_id) {
37 DCHECK_GT(tab_node_id, kInvalidTabNodeID); 36 DCHECK_GT(tab_node_id, kInvalidTabNodeID);
38 DCHECK(nodeid_tabid_map_.find(tab_node_id) == nodeid_tabid_map_.end()); 37 DCHECK(nodeid_tabid_map_.find(tab_node_id) == nodeid_tabid_map_.end());
39 unassociated_nodes_.insert(tab_node_id); 38 unassociated_nodes_.insert(tab_node_id);
40 if (max_used_tab_node_id_ < tab_node_id) 39 if (max_used_tab_node_id_ < tab_node_id)
41 max_used_tab_node_id_ = tab_node_id; 40 max_used_tab_node_id_ = tab_node_id;
42 } 41 }
43 42
44 void TabNodePool::AssociateTabNode(int tab_node_id, 43 void TabNodePool::AssociateTabNode(int tab_node_id, SessionID::id_type tab_id) {
45 SessionID::id_type tab_id) {
46 DCHECK_GT(tab_node_id, kInvalidTabNodeID); 44 DCHECK_GT(tab_node_id, kInvalidTabNodeID);
47 // Remove sync node if it is in unassociated nodes pool. 45 // Remove sync node if it is in unassociated nodes pool.
48 std::set<int>::iterator u_it = unassociated_nodes_.find(tab_node_id); 46 std::set<int>::iterator u_it = unassociated_nodes_.find(tab_node_id);
49 if (u_it != unassociated_nodes_.end()) { 47 if (u_it != unassociated_nodes_.end()) {
50 unassociated_nodes_.erase(u_it); 48 unassociated_nodes_.erase(u_it);
51 } else { 49 } else {
52 // This is a new node association, the sync node should be free. 50 // This is a new node association, the sync node should be free.
53 // Remove node from free node pool and then associate it with the tab. 51 // Remove node from free node pool and then associate it with the tab.
54 std::set<int>::iterator it = free_nodes_pool_.find(tab_node_id); 52 std::set<int>::iterator it = free_nodes_pool_.find(tab_node_id);
55 DCHECK(it != free_nodes_pool_.end()); 53 DCHECK(it != free_nodes_pool_.end());
(...skipping 11 matching lines...) Expand all
67 int tab_node_id = ++max_used_tab_node_id_; 65 int tab_node_id = ++max_used_tab_node_id_;
68 std::string tab_node_tag = TabIdToTag(machine_tag_, tab_node_id); 66 std::string tab_node_tag = TabIdToTag(machine_tag_, tab_node_id);
69 67
70 // We fill the new node with just enough data so that in case of a crash/bug 68 // We fill the new node with just enough data so that in case of a crash/bug
71 // we can identify the node as our own on re-association and reuse it. 69 // we can identify the node as our own on re-association and reuse it.
72 sync_pb::EntitySpecifics entity; 70 sync_pb::EntitySpecifics entity;
73 sync_pb::SessionSpecifics* specifics = entity.mutable_session(); 71 sync_pb::SessionSpecifics* specifics = entity.mutable_session();
74 specifics->set_session_tag(machine_tag_); 72 specifics->set_session_tag(machine_tag_);
75 specifics->set_tab_node_id(tab_node_id); 73 specifics->set_tab_node_id(tab_node_id);
76 append_changes->push_back(syncer::SyncChange( 74 append_changes->push_back(syncer::SyncChange(
77 FROM_HERE, 75 FROM_HERE, syncer::SyncChange::ACTION_ADD,
78 syncer::SyncChange::ACTION_ADD, 76 syncer::SyncData::CreateLocalData(tab_node_tag, tab_node_tag, entity)));
79 syncer::SyncData::CreateLocalData(tab_node_tag,
80 tab_node_tag,
81 entity)));
82 77
83 // Grow the pool by 1 since we created a new node. 78 // Grow the pool by 1 since we created a new node.
84 DVLOG(1) << "Adding sync node " << tab_node_id 79 DVLOG(1) << "Adding sync node " << tab_node_id << " to tab node id pool";
85 << " to tab node id pool";
86 free_nodes_pool_.insert(tab_node_id); 80 free_nodes_pool_.insert(tab_node_id);
87 return tab_node_id; 81 return tab_node_id;
88 } else { 82 } else {
89 // Return the next free node. 83 // Return the next free node.
90 return *free_nodes_pool_.begin(); 84 return *free_nodes_pool_.begin();
91 } 85 }
92 } 86 }
93 87
94 void TabNodePool::FreeTabNode(int tab_node_id, 88 void TabNodePool::FreeTabNode(int tab_node_id,
95 syncer::SyncChangeList* append_changes) { 89 syncer::SyncChangeList* append_changes) {
96 DCHECK(append_changes); 90 DCHECK(append_changes);
97 TabNodeIDToTabIDMap::iterator it = nodeid_tabid_map_.find(tab_node_id); 91 TabNodeIDToTabIDMap::iterator it = nodeid_tabid_map_.find(tab_node_id);
98 DCHECK(it != nodeid_tabid_map_.end()); 92 DCHECK(it != nodeid_tabid_map_.end());
99 nodeid_tabid_map_.erase(it); 93 nodeid_tabid_map_.erase(it);
100 FreeTabNodeInternal(tab_node_id, append_changes); 94 FreeTabNodeInternal(tab_node_id, append_changes);
101 } 95 }
102 96
103 void TabNodePool::FreeTabNodeInternal( 97 void TabNodePool::FreeTabNodeInternal(int tab_node_id,
104 int tab_node_id, 98 syncer::SyncChangeList* append_changes) {
105 syncer::SyncChangeList* append_changes) {
106 DCHECK(free_nodes_pool_.find(tab_node_id) == free_nodes_pool_.end()); 99 DCHECK(free_nodes_pool_.find(tab_node_id) == free_nodes_pool_.end());
107 DCHECK(append_changes); 100 DCHECK(append_changes);
108 free_nodes_pool_.insert(tab_node_id); 101 free_nodes_pool_.insert(tab_node_id);
109 102
110 // If number of free nodes exceed kFreeNodesHighWatermark, 103 // If number of free nodes exceed kFreeNodesHighWatermark,
111 // delete sync nodes till number reaches kFreeNodesLowWatermark. 104 // delete sync nodes till number reaches kFreeNodesLowWatermark.
112 // Note: This logic is to mitigate temporary disassociation issues with old 105 // Note: This logic is to mitigate temporary disassociation issues with old
113 // clients: http://crbug.com/259918. Newer versions do not need this. 106 // clients: http://crbug.com/259918. Newer versions do not need this.
114 if (free_nodes_pool_.size() > kFreeNodesHighWatermark) { 107 if (free_nodes_pool_.size() > kFreeNodesHighWatermark) {
115 for (std::set<int>::iterator free_it = free_nodes_pool_.begin(); 108 for (std::set<int>::iterator free_it = free_nodes_pool_.begin();
116 free_it != free_nodes_pool_.end();) { 109 free_it != free_nodes_pool_.end();) {
117 const std::string tab_node_tag = TabIdToTag(machine_tag_, *free_it); 110 const std::string tab_node_tag = TabIdToTag(machine_tag_, *free_it);
118 append_changes->push_back(syncer::SyncChange( 111 append_changes->push_back(syncer::SyncChange(
119 FROM_HERE, 112 FROM_HERE, syncer::SyncChange::ACTION_DELETE,
120 syncer::SyncChange::ACTION_DELETE, 113 syncer::SyncData::CreateLocalDelete(tab_node_tag, syncer::SESSIONS)));
121 syncer::SyncData::CreateLocalDelete(tab_node_tag,
122 syncer::SESSIONS)));
123 free_nodes_pool_.erase(free_it++); 114 free_nodes_pool_.erase(free_it++);
124 if (free_nodes_pool_.size() <= kFreeNodesLowWatermark) { 115 if (free_nodes_pool_.size() <= kFreeNodesLowWatermark) {
125 return; 116 return;
126 } 117 }
127 } 118 }
128 } 119 }
129 } 120 }
130 121
131 bool TabNodePool::IsUnassociatedTabNode(int tab_node_id) { 122 bool TabNodePool::IsUnassociatedTabNode(int tab_node_id) {
132 return unassociated_nodes_.find(tab_node_id) != unassociated_nodes_.end(); 123 return unassociated_nodes_.find(tab_node_id) != unassociated_nodes_.end();
133 } 124 }
134 125
135 void TabNodePool::ReassociateTabNode(int tab_node_id, 126 void TabNodePool::ReassociateTabNode(int tab_node_id,
136 SessionID::id_type tab_id) { 127 SessionID::id_type tab_id) {
137 // Remove from list of unassociated sync_nodes if present. 128 // Remove from list of unassociated sync_nodes if present.
138 std::set<int>::iterator it = unassociated_nodes_.find(tab_node_id); 129 std::set<int>::iterator it = unassociated_nodes_.find(tab_node_id);
139 if (it != unassociated_nodes_.end()) { 130 if (it != unassociated_nodes_.end()) {
140 unassociated_nodes_.erase(it); 131 unassociated_nodes_.erase(it);
141 } else { 132 } else {
142 // tab_node_id must be an already associated node. 133 // tab_node_id must be an already associated node.
143 DCHECK(nodeid_tabid_map_.find(tab_node_id) != nodeid_tabid_map_.end()); 134 DCHECK(nodeid_tabid_map_.find(tab_node_id) != nodeid_tabid_map_.end());
144 } 135 }
145 nodeid_tabid_map_[tab_node_id] = tab_id; 136 nodeid_tabid_map_[tab_node_id] = tab_id;
146 } 137 }
147 138
148 SessionID::id_type TabNodePool::GetTabIdFromTabNodeId( 139 SessionID::id_type TabNodePool::GetTabIdFromTabNodeId(int tab_node_id) const {
149 int tab_node_id) const {
150 TabNodeIDToTabIDMap::const_iterator it = nodeid_tabid_map_.find(tab_node_id); 140 TabNodeIDToTabIDMap::const_iterator it = nodeid_tabid_map_.find(tab_node_id);
151 if (it != nodeid_tabid_map_.end()) { 141 if (it != nodeid_tabid_map_.end()) {
152 return it->second; 142 return it->second;
153 } 143 }
154 return kInvalidTabID; 144 return kInvalidTabID;
155 } 145 }
156 146
157 void TabNodePool::DeleteUnassociatedTabNodes( 147 void TabNodePool::DeleteUnassociatedTabNodes(
158 syncer::SyncChangeList* append_changes) { 148 syncer::SyncChangeList* append_changes) {
159 for (std::set<int>::iterator it = unassociated_nodes_.begin(); 149 for (std::set<int>::iterator it = unassociated_nodes_.begin();
(...skipping 10 matching lines...) Expand all
170 free_nodes_pool_.clear(); 160 free_nodes_pool_.clear();
171 nodeid_tabid_map_.clear(); 161 nodeid_tabid_map_.clear();
172 max_used_tab_node_id_ = kInvalidTabNodeID; 162 max_used_tab_node_id_ = kInvalidTabNodeID;
173 } 163 }
174 164
175 size_t TabNodePool::Capacity() const { 165 size_t TabNodePool::Capacity() const {
176 return nodeid_tabid_map_.size() + unassociated_nodes_.size() + 166 return nodeid_tabid_map_.size() + unassociated_nodes_.size() +
177 free_nodes_pool_.size(); 167 free_nodes_pool_.size();
178 } 168 }
179 169
180 bool TabNodePool::Empty() const { return free_nodes_pool_.empty(); } 170 bool TabNodePool::Empty() const {
171 return free_nodes_pool_.empty();
172 }
181 173
182 bool TabNodePool::Full() { return nodeid_tabid_map_.empty(); } 174 bool TabNodePool::Full() {
175 return nodeid_tabid_map_.empty();
176 }
183 177
184 void TabNodePool::SetMachineTag(const std::string& machine_tag) { 178 void TabNodePool::SetMachineTag(const std::string& machine_tag) {
185 machine_tag_ = machine_tag; 179 machine_tag_ = machine_tag;
186 } 180 }
187 181
188 } // namespace browser_sync 182 } // namespace browser_sync
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698