OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 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 CHROME_BROWSER_SYNC_GLUE_TAB_NODE_POOL_H_ | 5 #ifndef CHROME_BROWSER_SYNC_GLUE_TAB_NODE_POOL_H_ |
6 #define CHROME_BROWSER_SYNC_GLUE_TAB_NODE_POOL_H_ | 6 #define CHROME_BROWSER_SYNC_GLUE_TAB_NODE_POOL_H_ |
7 | 7 |
8 #include <string> | 8 #include <string> |
9 #include <vector> | 9 #include <vector> |
10 | 10 |
11 #include "base/basictypes.h" | 11 #include "base/basictypes.h" |
12 | 12 |
13 class ProfileSyncService; | 13 class ProfileSyncService; |
14 | 14 |
15 namespace browser_sync { | 15 namespace browser_sync { |
16 | 16 |
17 // A pool for managing free/used tab sync nodes. Performs lazy creation | 17 // A pool for managing free/used tab sync nodes. Performs lazy creation |
18 // of sync nodes when necessary. | 18 // of sync nodes when necessary. |
19 class TabNodePool { | 19 class TabNodePool { |
20 public: | 20 public: |
21 explicit TabNodePool(ProfileSyncService* sync_service); | 21 explicit TabNodePool(ProfileSyncService* sync_service); |
22 ~TabNodePool(); | 22 ~TabNodePool(); |
23 | 23 |
24 // Build a sync tag from tab_node_id. | 24 // Build a sync tag from tab_node_id. |
25 static std::string TabIdToTag(const std::string machine_tag, | 25 static std::string TabIdToTag(const std::string machine_tag, |
26 size_t tab_node_id); | 26 int32 tab_node_id); |
27 | 27 |
28 // Add a previously allocated tab sync node to our pool. Increases the size | 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. | 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 | 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 | 31 // previous sessions, not for freeing tab nodes we created through |
32 // GetFreeTabNode (use FreeTabNode below for that). | 32 // GetFreeTabNode (use FreeTabNode below for that). |
33 void AddTabNode(int64 sync_id); | 33 void AddTabNode(int64 sync_id, int32 tab_node_id); |
34 | 34 |
35 // Returns the sync_id for the next free tab node. If none are available, | 35 // Returns the sync_id for the next free tab node. If none are available, |
36 // creates a new tab node. | 36 // creates a new tab node. |
37 // Note: We make use of the following "id's" | 37 // Note: We make use of the following "id's" |
38 // - a sync_id: an int64 used in |syncer::InitByIdLookup| | 38 // - a sync_id: an int64 used in |syncer::InitByIdLookup| |
39 // - a tab_id: created by session service, unique to this client | 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 | 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: | 41 // to generate the sync tab node tag through: |
42 // tab_tag = StringPrintf("%s_%ui", local_session_tag, tab_node_id); | 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 | 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 | 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 | 45 // is used when creating a new sync node, which returns the sync_id, created |
46 // by the sync db. | 46 // by the sync db. |
47 int64 GetFreeTabNode(); | 47 int64 GetFreeTabNode(); |
48 | 48 |
49 // Return a tab node to our free pool. | 49 // Return a tab node to our free pool. |
50 // Note: the difference between FreeTabNode and AddTabNode is that | 50 // Note: the difference between FreeTabNode and AddTabNode is that |
51 // FreeTabNode does not modify the size of |tab_syncid_pool_|, while | 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 | 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 | 53 // the |tab_syncid_pool_| should always be equal to the amount of tab nodes |
54 // associated with this machine. | 54 // associated with this machine. |
55 void FreeTabNode(int64 sync_id); | 55 void FreeTabNode(int64 sync_id); |
| 56 int64 GetOldSyncNode(const int64 sync_node_id); |
| 57 |
| 58 void PurgeOldSyncNodes(std::vector<int64>& used_sync_ids); |
56 | 59 |
57 // Clear tab pool. | 60 // Clear tab pool. |
58 void clear() { | 61 void clear() { |
59 tab_syncid_pool_.clear(); | 62 tab_syncid_pool_.clear(); |
| 63 old_sync_ids.clear(); |
60 tab_pool_fp_ = -1; | 64 tab_pool_fp_ = -1; |
61 } | 65 } |
62 | 66 |
63 // Return the number of tab nodes this client currently has allocated | 67 // Return the number of tab nodes this client currently has allocated |
64 // (including both free and used nodes) | 68 // (including both free and used nodes) |
65 size_t capacity() const { return tab_syncid_pool_.size(); } | 69 size_t capacity() const { |
| 70 return tab_syncid_pool_.size() + old_sync_ids.size(); |
| 71 } |
66 | 72 |
67 // Return empty status (all tab nodes are in use). | 73 // Return empty status (all tab nodes are in use). |
68 bool empty() const { return tab_pool_fp_ == -1; } | 74 bool empty() const { return tab_pool_fp_ == -1; } |
69 | 75 |
70 // Return full status (no tab nodes are in use). | 76 // Return full status (no tab nodes are in use). |
71 bool full() { | 77 bool full() { |
72 return tab_pool_fp_ == static_cast<int64>(tab_syncid_pool_.size())-1; | 78 return tab_pool_fp_ == static_cast<int64>(tab_syncid_pool_.size())-1; |
73 } | 79 } |
74 | 80 |
75 void set_machine_tag(const std::string& machine_tag) { | 81 void set_machine_tag(const std::string& machine_tag) { |
76 machine_tag_ = machine_tag; | 82 machine_tag_ = machine_tag; |
77 } | 83 } |
78 | 84 |
79 private: | 85 private: |
| 86 |
| 87 /* The maximum used tab node id, when creating a new sync node we used |
| 88 * max_used_tab_node_id_ + 1 */ |
| 89 int32 max_used_tab_node_id_; |
| 90 |
80 // Pool of all available syncid's for tab's we have created. | 91 // Pool of all available syncid's for tab's we have created. |
81 std::vector<int64> tab_syncid_pool_; | 92 std::vector<int64> tab_syncid_pool_; |
82 | 93 |
| 94 // Pool containing unused sync nodes as a result of session restore. |
| 95 std::vector<int64> session_restore_syncid_nodes_pool_; |
| 96 |
| 97 // typedef std::pair<int64, int32> sync_node_pair; |
| 98 std::vector<int64> old_sync_ids; |
| 99 |
83 // Free pointer for tab pool. Only those node id's, up to and including the | 100 // 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 | 101 // 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. | 102 // |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_--]. | 103 // To get the next free node, use tab_syncid_pool_[tab_pool_fp_--]. |
87 int64 tab_pool_fp_; | 104 int64 tab_pool_fp_; |
88 | 105 |
89 // The machiine tag associated with this tab pool. Used in the title of new | 106 // The machiine tag associated with this tab pool. Used in the title of new |
90 // sync nodes. | 107 // sync nodes. |
91 std::string machine_tag_; | 108 std::string machine_tag_; |
92 | 109 |
93 // Our sync service profile (for making changes to the sync db) | 110 // Our sync service profile (for making changes to the sync db) |
94 ProfileSyncService* sync_service_; | 111 ProfileSyncService* sync_service_; |
95 | 112 |
96 DISALLOW_COPY_AND_ASSIGN(TabNodePool); | 113 DISALLOW_COPY_AND_ASSIGN(TabNodePool); |
97 }; | 114 }; |
98 | 115 |
99 } // namespace browser_sync | 116 } // namespace browser_sync |
100 | 117 |
101 #endif // CHROME_BROWSER_SYNC_GLUE_TAB_NODE_POOL_H_ | 118 #endif // CHROME_BROWSER_SYNC_GLUE_TAB_NODE_POOL_H_ |
OLD | NEW |