Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / sync / sessions / tab_node_pool.h
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 CHROME_BROWSER_SYNC_SESSIONS_TAB_NODE_POOL_H_
6 #define CHROME_BROWSER_SYNC_SESSIONS_TAB_NODE_POOL_H_
7
8 #include <map>
9 #include <set>
10 #include <string>
11
12 #include "base/basictypes.h"
13 #include "base/gtest_prod_util.h"
14 #include "chrome/browser/sessions/session_id.h"
15 #include "sync/api/sync_change_processor.h"
16
17 namespace syncer {
18 class SyncChangeProcessor;
19 }
20
21 namespace browser_sync {
22
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);
30 //
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.
40
41 class TabNodePool {
42  public:
43    TabNodePool();
44   ~TabNodePool();
45   enum InvalidTab {
46     kInvalidTabID = -1
47   };
48
49   // If free nodes > kFreeNodesHighWatermark, delete all free nodes until
50   // free nodes <= kFreeNodesLowWatermark.
51   static const size_t kFreeNodesLowWatermark;
52
53   // Maximum limit of FreeNodes allowed on the client.
54   static const size_t kFreeNodesHighWatermark;
55
56   static const int kInvalidTabNodeID;
57
58   // Build a sync tag from tab_node_id.
59   static std::string TabIdToTag(const std::string machine_tag,
60                                 int tab_node_id);
61
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);
74
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);
82
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);
88
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);
94
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;
98
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);
102
103   // Returns true if |tab_node_id| is an unassociated tab node.
104   bool IsUnassociatedTabNode(int tab_node_id);
105
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);
111
112   // Clear tab pool.
113   void Clear();
114
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;
118
119   // Return empty status (all tab nodes are in use).
120   bool Empty() const;
121
122   // Return full status (no tab nodes are in use).
123   bool Full();
124
125   void SetMachineTag(const std::string& machine_tag);
126
127  private:
128   friend class SyncTabNodePoolTest;
129   typedef std::map<int, SessionID::id_type> TabNodeIDToTabIDMap;
130
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);
137
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_;
143
144   // The node ids for the set of free sync nodes.
145   std::set<int> free_nodes_pool_;
146
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_;
151
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_;
155
156   // The machine tag associated with this tab pool. Used in the title of new
157   // sync nodes.
158   std::string machine_tag_;
159
160   DISALLOW_COPY_AND_ASSIGN(TabNodePool);
161 };
162
163 }  // namespace browser_sync
164
165 #endif  // CHROME_BROWSER_SYNC_SESSIONS_TAB_NODE_POOL_H_