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

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

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

Powered by Google App Engine
This is Rietveld 408576698