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