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