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 "chrome/browser/sync/glue/tab_node_pool.h" | 5 #include "chrome/browser/sync/glue/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 "base/strings/utf_string_conversions.h" | 10 #include "base/strings/utf_string_conversions.h" |
11 #include "chrome/browser/sync/profile_sync_service.h" | 11 #include "chrome/browser/sync/profile_sync_service.h" |
12 #include "sync/internal_api/public/base/model_type.h" | 12 #include "sync/internal_api/public/base/model_type.h" |
13 #include "sync/internal_api/public/read_node.h" | 13 #include "sync/internal_api/public/read_node.h" |
14 #include "sync/internal_api/public/write_node.h" | 14 #include "sync/internal_api/public/write_node.h" |
15 #include "sync/internal_api/public/write_transaction.h" | 15 #include "sync/internal_api/public/write_transaction.h" |
16 | 16 |
17 namespace browser_sync { | 17 namespace browser_sync { |
18 | 18 |
19 static const char kNoSessionsFolderError[] = | 19 static const char kNoSessionsFolderError[] = |
20 "Server did not create the top-level sessions node. We " | 20 "Server did not create the top-level sessions node. We " |
21 "might be running against an out-of-date server."; | 21 "might be running against an out-of-date server."; |
22 | 22 |
23 TabNodePool::TabNodePool( | 23 static const size_t kMaxNumberOfFreeNodes = 25; |
24 ProfileSyncService* sync_service) | 24 |
25 : tab_pool_fp_(-1), | 25 TabNodePool::TabNodePool(ProfileSyncService* sync_service) |
26 sync_service_(sync_service) { | 26 : max_used_tab_node_id_(0), sync_service_(sync_service) {} |
27 } | |
28 | 27 |
29 TabNodePool::~TabNodePool() {} | 28 TabNodePool::~TabNodePool() {} |
30 | 29 |
31 // Static | 30 // Static |
32 std::string TabNodePool::TabIdToTag( | 31 std::string TabNodePool::TabIdToTag( |
33 const std::string machine_tag, | 32 const std::string machine_tag, |
34 size_t tab_node_id) { | 33 size_t tab_node_id) { |
35 return base::StringPrintf("%s %" PRIuS "", machine_tag.c_str(), tab_node_id); | 34 return base::StringPrintf("%s %" PRIuS "", machine_tag.c_str(), tab_node_id); |
36 } | 35 } |
37 | 36 |
38 void TabNodePool::AddTabNode(int64 sync_id) { | 37 void TabNodePool::AddTabNode(int64 sync_id, |
39 tab_syncid_pool_.resize(tab_syncid_pool_.size() + 1); | 38 const SessionID& tab_id, |
40 tab_syncid_pool_[static_cast<size_t>(++tab_pool_fp_)] = sync_id; | 39 size_t tab_node_id) { |
| 40 DCHECK_GT(sync_id, syncer::kInvalidId); |
| 41 DCHECK_GT(tab_id.id(), kInvalidTabID); |
| 42 DCHECK(syncid_tabid_map_.find(sync_id) == syncid_tabid_map_.end()); |
| 43 syncid_tabid_map_[sync_id] = tab_id.id(); |
| 44 if (max_used_tab_node_id_ < tab_node_id) |
| 45 max_used_tab_node_id_ = tab_node_id; |
| 46 } |
| 47 |
| 48 void TabNodePool::AssociateTabNode(int64 sync_id, SessionID::id_type tab_id) { |
| 49 DCHECK_GT(sync_id, 0); |
| 50 // Remove node from free node pool and associate it with tab. |
| 51 std::set<int64>::iterator it = free_nodes_pool_.find(sync_id); |
| 52 DCHECK(it != free_nodes_pool_.end()); |
| 53 free_nodes_pool_.erase(it); |
| 54 DCHECK(syncid_tabid_map_.find(sync_id) == syncid_tabid_map_.end()); |
| 55 syncid_tabid_map_[sync_id] = tab_id; |
41 } | 56 } |
42 | 57 |
43 int64 TabNodePool::GetFreeTabNode() { | 58 int64 TabNodePool::GetFreeTabNode() { |
44 DCHECK_GT(machine_tag_.length(), 0U); | 59 DCHECK_GT(machine_tag_.length(), 0U); |
45 if (tab_pool_fp_ == -1) { | 60 if (free_nodes_pool_.empty()) { |
46 // Tab pool has no free nodes, allocate new one. | 61 // Tab pool has no free nodes, allocate new one. |
47 syncer::WriteTransaction trans(FROM_HERE, sync_service_->GetUserShare()); | 62 syncer::WriteTransaction trans(FROM_HERE, sync_service_->GetUserShare()); |
48 syncer::ReadNode root(&trans); | 63 syncer::ReadNode root(&trans); |
49 if (root.InitByTagLookup(syncer::ModelTypeToRootTag(syncer::SESSIONS)) != | 64 if (root.InitByTagLookup(syncer::ModelTypeToRootTag(syncer::SESSIONS)) != |
50 syncer::BaseNode::INIT_OK) { | 65 syncer::BaseNode::INIT_OK) { |
51 LOG(ERROR) << kNoSessionsFolderError; | 66 LOG(ERROR) << kNoSessionsFolderError; |
52 return syncer::kInvalidId; | 67 return syncer::kInvalidId; |
53 } | 68 } |
54 size_t tab_node_id = tab_syncid_pool_.size(); | 69 size_t tab_node_id = ++max_used_tab_node_id_; |
55 std::string tab_node_tag = TabIdToTag(machine_tag_, tab_node_id); | 70 std::string tab_node_tag = TabIdToTag(machine_tag_, tab_node_id); |
56 syncer::WriteNode tab_node(&trans); | 71 syncer::WriteNode tab_node(&trans); |
57 syncer::WriteNode::InitUniqueByCreationResult result = | 72 syncer::WriteNode::InitUniqueByCreationResult result = |
58 tab_node.InitUniqueByCreation(syncer::SESSIONS, root, tab_node_tag); | 73 tab_node.InitUniqueByCreation(syncer::SESSIONS, root, tab_node_tag); |
59 if (result != syncer::WriteNode::INIT_SUCCESS) { | 74 if (result != syncer::WriteNode::INIT_SUCCESS) { |
60 LOG(ERROR) << "Could not create new node with tag " | 75 LOG(ERROR) << "Could not create new node with tag " |
61 << tab_node_tag << "!"; | 76 << tab_node_tag << "!"; |
62 return syncer::kInvalidId; | 77 return syncer::kInvalidId; |
63 } | 78 } |
64 // We fill the new node with just enough data so that in case of a crash/bug | 79 // We fill the new node with just enough data so that in case of a crash/bug |
65 // we can identify the node as our own on re-association and reuse it. | 80 // we can identify the node as our own on re-association and reuse it. |
66 tab_node.SetTitle(UTF8ToWide(tab_node_tag)); | 81 tab_node.SetTitle(UTF8ToWide(tab_node_tag)); |
67 sync_pb::SessionSpecifics specifics; | 82 sync_pb::SessionSpecifics specifics; |
68 specifics.set_session_tag(machine_tag_); | 83 specifics.set_session_tag(machine_tag_); |
69 specifics.set_tab_node_id(tab_node_id); | 84 specifics.set_tab_node_id(tab_node_id); |
70 tab_node.SetSessionSpecifics(specifics); | 85 tab_node.SetSessionSpecifics(specifics); |
71 | 86 |
72 // Grow the pool by 1 since we created a new node. We don't actually need | 87 // Grow the pool by 1 since we created a new node. |
73 // to put the node's id in the pool now, since the pool is still empty. | 88 DVLOG(1) << "Adding sync node " << tab_node.GetId() |
74 // The id will be added when that tab is closed and the node is freed. | 89 << " to tab syncid pool"; |
75 tab_syncid_pool_.resize(tab_node_id + 1); | 90 free_nodes_pool_.insert(tab_node.GetId()); |
76 DVLOG(1) << "Adding sync node " | |
77 << tab_node.GetId() << " to tab syncid pool"; | |
78 return tab_node.GetId(); | 91 return tab_node.GetId(); |
79 } else { | 92 } else { |
80 // There are nodes available, grab next free and decrement free pointer. | 93 // Return the next free node. |
81 return tab_syncid_pool_[static_cast<size_t>(tab_pool_fp_--)]; | 94 return *free_nodes_pool_.begin(); |
82 } | 95 } |
83 } | 96 } |
84 | 97 |
85 void TabNodePool::FreeTabNode(int64 sync_id) { | 98 void TabNodePool::FreeTabNode(int64 sync_id) { |
86 // Pool size should always match # of free tab nodes. | 99 SyncIDToTabIDMap::iterator it = syncid_tabid_map_.find(sync_id); |
87 DCHECK_LT(tab_pool_fp_, static_cast<int64>(tab_syncid_pool_.size())); | 100 DCHECK(it != syncid_tabid_map_.end()); |
88 tab_syncid_pool_[static_cast<size_t>(++tab_pool_fp_)] = sync_id; | 101 syncid_tabid_map_.erase(it); |
| 102 DCHECK(free_nodes_pool_.find(sync_id) == free_nodes_pool_.end()); |
| 103 |
| 104 // If number of free nodes exceed number of maximum allowed free nodes, |
| 105 // delete the sync node. |
| 106 if (free_nodes_pool_.size() < kMaxNumberOfFreeNodes) { |
| 107 free_nodes_pool_.insert(sync_id); |
| 108 } else { |
| 109 syncer::WriteTransaction trans(FROM_HERE, sync_service_->GetUserShare()); |
| 110 syncer::WriteNode tab_node(&trans); |
| 111 if (tab_node.InitByIdLookup(sync_id) != syncer::BaseNode::INIT_OK) { |
| 112 LOG(ERROR) << "Could not find sync node with id: " << sync_id; |
| 113 return; |
| 114 } |
| 115 tab_node.Tombstone(); |
| 116 } |
| 117 } |
| 118 |
| 119 bool TabNodePool::ReassociateTabNode(int64 sync_id, SessionID::id_type tab_id) { |
| 120 SyncIDToTabIDMap::iterator it = syncid_tabid_map_.find(sync_id); |
| 121 if (it != syncid_tabid_map_.end()) { |
| 122 syncid_tabid_map_[sync_id] = tab_id; |
| 123 return true; |
| 124 } |
| 125 return false; |
| 126 } |
| 127 |
| 128 SessionID::id_type TabNodePool::GetTabIdFromSyncId(int64 sync_id) const { |
| 129 SyncIDToTabIDMap::const_iterator it = syncid_tabid_map_.find(sync_id); |
| 130 if (it != syncid_tabid_map_.end()) { |
| 131 return it->second; |
| 132 } |
| 133 return kInvalidTabID; |
| 134 } |
| 135 |
| 136 void TabNodePool::FreeUnusedTabNodes(const std::set<int64>& used_sync_ids) { |
| 137 for (SyncIDToTabIDMap::iterator it = syncid_tabid_map_.begin(); |
| 138 it != syncid_tabid_map_.end();) { |
| 139 if (used_sync_ids.find(it->first) == used_sync_ids.end()) { |
| 140 // This sync node is not used, return it to free node pool. |
| 141 int64 sync_id = it->first; |
| 142 ++it; |
| 143 FreeTabNode(sync_id); |
| 144 } else { |
| 145 ++it; |
| 146 } |
| 147 } |
| 148 } |
| 149 |
| 150 // Clear tab pool. |
| 151 void TabNodePool::Clear() { |
| 152 free_nodes_pool_.clear(); |
| 153 syncid_tabid_map_.clear(); |
| 154 max_used_tab_node_id_ = 0; |
| 155 } |
| 156 |
| 157 size_t TabNodePool::Capacity() const { |
| 158 return syncid_tabid_map_.size() + free_nodes_pool_.size(); |
| 159 } |
| 160 |
| 161 bool TabNodePool::Empty() const { return free_nodes_pool_.empty(); } |
| 162 |
| 163 bool TabNodePool::Full() { return syncid_tabid_map_.empty(); } |
| 164 |
| 165 void TabNodePool::SetMachineTag(const std::string& machine_tag) { |
| 166 machine_tag_ = machine_tag; |
89 } | 167 } |
90 | 168 |
91 } // namespace browser_sync | 169 } // namespace browser_sync |
OLD | NEW |