| OLD | NEW |
| (Empty) |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #ifndef COMPONENTS_SYNC_DRIVER_TAB_NODE_POOL_H_ | |
| 6 #define COMPONENTS_SYNC_DRIVER_TAB_NODE_POOL_H_ | |
| 7 | |
| 8 #include <map> | |
| 9 #include <set> | |
| 10 #include <string> | |
| 11 | |
| 12 #include "base/basictypes.h" | |
| 13 #include "components/sessions/core/session_id.h" | |
| 14 #include "sync/api/sync_change_processor.h" | |
| 15 | |
| 16 namespace syncer { | |
| 17 class SyncChangeProcessor; | |
| 18 } | |
| 19 | |
| 20 namespace browser_sync { | |
| 21 | |
| 22 // A pool for managing free/used tab sync nodes for the *local* session. | |
| 23 // Performs lazy creation of sync nodes when necessary. | |
| 24 // Note: We make use of the following "id's" | |
| 25 // - a tab_id: created by session service, unique to this client | |
| 26 // - a tab_node_id: the id for a particular sync tab node. This is used | |
| 27 // to generate the sync tab node tag through: | |
| 28 // tab_tag = StringPrintf("%s_%ui", local_session_tag, tab_node_id); | |
| 29 // | |
| 30 // A sync node can be in one of the three states: | |
| 31 // 1. Associated : Sync node is used and associated with a tab. | |
| 32 // 2. Unassociated : Sync node is used but currently unassociated with any tab. | |
| 33 // This is true for old nodes that remain from a session | |
| 34 // restart. Nodes are only unassociated temporarily while the | |
| 35 // model associator figures out which tabs belong to which | |
| 36 // nodes. Eventually any remaining unassociated nodes are | |
| 37 // freed. | |
| 38 // 3. Free : Sync node is unused. | |
| 39 | |
| 40 class TabNodePool { | |
| 41 public: | |
| 42 TabNodePool(); | |
| 43 ~TabNodePool(); | |
| 44 enum InvalidTab { | |
| 45 kInvalidTabID = -1 | |
| 46 }; | |
| 47 | |
| 48 // If free nodes > kFreeNodesHighWatermark, delete all free nodes until | |
| 49 // free nodes <= kFreeNodesLowWatermark. | |
| 50 static const size_t kFreeNodesLowWatermark; | |
| 51 | |
| 52 // Maximum limit of FreeNodes allowed on the client. | |
| 53 static const size_t kFreeNodesHighWatermark; | |
| 54 | |
| 55 static const int kInvalidTabNodeID; | |
| 56 | |
| 57 // Build a sync tag from tab_node_id. | |
| 58 static std::string TabIdToTag(const std::string& machine_tag, | |
| 59 int tab_node_id); | |
| 60 | |
| 61 // Returns the tab_node_id for the next free tab node. If none are available, | |
| 62 // creates a new tab node and adds it to free nodes pool. The free node can | |
| 63 // then be used to associate with a tab by calling AssociateTabNode. | |
| 64 // Note: The node is considered free until it has been associated. Repeated | |
| 65 // calls to GetFreeTabNode will return the same id until node has been | |
| 66 // associated. | |
| 67 // |change_output| *must* be provided. It is the TabNodePool's link to | |
| 68 // the SyncChange pipeline that exists in the caller context. If the need | |
| 69 // to create nodes arises in the implementation, associated SyncChanges will | |
| 70 // be appended to this list for later application by the caller via the | |
| 71 // SyncChangeProcessor. | |
| 72 int GetFreeTabNode(syncer::SyncChangeList* change_output); | |
| 73 | |
| 74 // Removes association for |tab_node_id| and returns it to the free node pool. | |
| 75 // |change_output| *must* be provided. It is the TabNodePool's link to | |
| 76 // the SyncChange pipeline that exists in the caller's context. If the need | |
| 77 // to delete sync nodes arises in the implementation, associated SyncChanges | |
| 78 // will be appended to this list for later application by the caller via the | |
| 79 // SyncChangeProcessor. | |
| 80 void FreeTabNode(int tab_node_id, syncer::SyncChangeList* change_output); | |
| 81 | |
| 82 // Associates |tab_node_id| with |tab_id|. |tab_node_id| should either be | |
| 83 // unassociated or free. If |tab_node_id| is free, |tab_node_id| is removed | |
| 84 // from the free node pool In order to associate a non free sync node, | |
| 85 // use ReassociateTabNode. | |
| 86 void AssociateTabNode(int tab_node_id, SessionID::id_type tab_id); | |
| 87 | |
| 88 // Adds |tab_node_id| as an unassociated sync node. | |
| 89 // Note: this should only be called when we discover tab sync nodes from | |
| 90 // previous sessions, not for freeing tab nodes we created through | |
| 91 // GetFreeTabNode (use FreeTabNode below for that). | |
| 92 void AddTabNode(int tab_node_id); | |
| 93 | |
| 94 // Returns the tab_id for |tab_node_id| if it is associated else returns | |
| 95 // kInvalidTabID. | |
| 96 SessionID::id_type GetTabIdFromTabNodeId(int tab_node_id) const; | |
| 97 | |
| 98 // Reassociates |tab_node_id| with |tab_id|. |tab_node_id| must be either | |
| 99 // associated with a tab or in the set of unassociated nodes. | |
| 100 void ReassociateTabNode(int tab_node_id, SessionID::id_type tab_id); | |
| 101 | |
| 102 // Returns true if |tab_node_id| is an unassociated tab node. | |
| 103 bool IsUnassociatedTabNode(int tab_node_id); | |
| 104 | |
| 105 // Returns any unassociated nodes to the free node pool. | |
| 106 // |change_output| *must* be provided. It is the TabNodePool's link to | |
| 107 // the SyncChange pipeline that exists in the caller's context. | |
| 108 // See FreeTabNode for more detail. | |
| 109 void DeleteUnassociatedTabNodes(syncer::SyncChangeList* change_output); | |
| 110 | |
| 111 // Clear tab pool. | |
| 112 void Clear(); | |
| 113 | |
| 114 // Return the number of tab nodes this client currently has allocated | |
| 115 // (including both free, unassociated and associated nodes) | |
| 116 size_t Capacity() const; | |
| 117 | |
| 118 // Return empty status (all tab nodes are in use). | |
| 119 bool Empty() const; | |
| 120 | |
| 121 // Return full status (no tab nodes are in use). | |
| 122 bool Full(); | |
| 123 | |
| 124 void SetMachineTag(const std::string& machine_tag); | |
| 125 | |
| 126 private: | |
| 127 friend class SyncTabNodePoolTest; | |
| 128 typedef std::map<int, SessionID::id_type> TabNodeIDToTabIDMap; | |
| 129 | |
| 130 // Adds |tab_node_id| to free node pool. | |
| 131 // |change_output| *must* be provided. It is the TabNodePool's link to | |
| 132 // the SyncChange pipeline that exists in the caller's context. | |
| 133 // See FreeTabNode for more detail. | |
| 134 void FreeTabNodeInternal(int tab_node_id, | |
| 135 syncer::SyncChangeList* change_output); | |
| 136 | |
| 137 // Stores mapping of node ids associated with tab_ids, these are the used | |
| 138 // nodes of tab node pool. | |
| 139 // The nodes in the map can be returned to free tab node pool by calling | |
| 140 // FreeTabNode(tab_node_id). | |
| 141 TabNodeIDToTabIDMap nodeid_tabid_map_; | |
| 142 | |
| 143 // The node ids for the set of free sync nodes. | |
| 144 std::set<int> free_nodes_pool_; | |
| 145 | |
| 146 // The node ids that are added to pool using AddTabNode and are currently | |
| 147 // not associated with any tab. They can be reassociated using | |
| 148 // ReassociateTabNode. | |
| 149 std::set<int> unassociated_nodes_; | |
| 150 | |
| 151 // The maximum used tab_node id for a sync node. A new sync node will always | |
| 152 // be created with max_used_tab_node_id_ + 1. | |
| 153 int max_used_tab_node_id_; | |
| 154 | |
| 155 // The machine tag associated with this tab pool. Used in the title of new | |
| 156 // sync nodes. | |
| 157 std::string machine_tag_; | |
| 158 | |
| 159 DISALLOW_COPY_AND_ASSIGN(TabNodePool); | |
| 160 }; | |
| 161 | |
| 162 } // namespace browser_sync | |
| 163 | |
| 164 #endif // COMPONENTS_SYNC_DRIVER_TAB_NODE_POOL_H_ | |
| OLD | NEW |