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