Fix build break
[chromium-blink-merge.git] / chrome / browser / sync / glue / tab_node_pool.h
blob8327d061e08551ecf4fb835e39b06b80f1a613f5
1 // Copyright (c) 2012 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 CHROME_BROWSER_SYNC_GLUE_TAB_NODE_POOL_H_
6 #define CHROME_BROWSER_SYNC_GLUE_TAB_NODE_POOL_H_
8 #include <string>
9 #include <vector>
11 #include "base/basictypes.h"
13 class ProfileSyncService;
15 namespace browser_sync {
17 // A pool for managing free/used tab sync nodes. Performs lazy creation
18 // of sync nodes when necessary.
19 class TabNodePool {
20 public:
21 explicit TabNodePool(ProfileSyncService* sync_service);
22 ~TabNodePool();
24 // Build a sync tag from tab_node_id.
25 static std::string TabIdToTag(const std::string machine_tag,
26 size_t tab_node_id);
28 // Add a previously allocated tab sync node to our pool. Increases the size
29 // of tab_syncid_pool_ by one and marks the new tab node as free.
30 // Note: this should only be called when we discover tab sync nodes from
31 // previous sessions, not for freeing tab nodes we created through
32 // GetFreeTabNode (use FreeTabNode below for that).
33 void AddTabNode(int64 sync_id);
35 // Returns the sync_id for the next free tab node. If none are available,
36 // creates a new tab node.
37 // Note: We make use of the following "id's"
38 // - a sync_id: an int64 used in |syncer::InitByIdLookup|
39 // - a tab_id: created by session service, unique to this client
40 // - a tab_node_id: the id for a particular sync tab node. This is used
41 // to generate the sync tab node tag through:
42 // tab_tag = StringPrintf("%s_%ui", local_session_tag, tab_node_id);
43 // tab_node_id and sync_id are both unique to a particular sync node. The
44 // difference is that tab_node_id is controlled by the model associator and
45 // is used when creating a new sync node, which returns the sync_id, created
46 // by the sync db.
47 int64 GetFreeTabNode();
49 // Return a tab node to our free pool.
50 // Note: the difference between FreeTabNode and AddTabNode is that
51 // FreeTabNode does not modify the size of |tab_syncid_pool_|, while
52 // AddTabNode increases it by one. In the case of FreeTabNode, the size of
53 // the |tab_syncid_pool_| should always be equal to the amount of tab nodes
54 // associated with this machine.
55 void FreeTabNode(int64 sync_id);
57 // Clear tab pool.
58 void clear() {
59 tab_syncid_pool_.clear();
60 tab_pool_fp_ = -1;
63 // Return the number of tab nodes this client currently has allocated
64 // (including both free and used nodes)
65 size_t capacity() const { return tab_syncid_pool_.size(); }
67 // Return empty status (all tab nodes are in use).
68 bool empty() const { return tab_pool_fp_ == -1; }
70 // Return full status (no tab nodes are in use).
71 bool full() {
72 return tab_pool_fp_ == static_cast<int64>(tab_syncid_pool_.size())-1;
75 void set_machine_tag(const std::string& machine_tag) {
76 machine_tag_ = machine_tag;
79 private:
80 // Pool of all available syncid's for tab's we have created.
81 std::vector<int64> tab_syncid_pool_;
83 // Free pointer for tab pool. Only those node id's, up to and including the
84 // one indexed by the free pointer, are valid and free. The rest of the
85 // |tab_syncid_pool_| is invalid because the nodes are in use.
86 // To get the next free node, use tab_syncid_pool_[tab_pool_fp_--].
87 int64 tab_pool_fp_;
89 // The machiine tag associated with this tab pool. Used in the title of new
90 // sync nodes.
91 std::string machine_tag_;
93 // Our sync service profile (for making changes to the sync db)
94 ProfileSyncService* sync_service_;
96 DISALLOW_COPY_AND_ASSIGN(TabNodePool);
99 } // namespace browser_sync
101 #endif // CHROME_BROWSER_SYNC_GLUE_TAB_NODE_POOL_H_