Chromium Code Reviews| 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 <map> | |
| 9 #include <set> | |
| 8 #include <string> | 10 #include <string> |
| 9 #include <vector> | |
| 10 | 11 |
| 11 #include "base/basictypes.h" | 12 #include "base/basictypes.h" |
| 13 #include "base/gtest_prod_util.h" | |
| 14 #include "chrome/browser/sessions/session_id.h" | |
| 12 | 15 |
| 13 class ProfileSyncService; | 16 class ProfileSyncService; |
| 14 | 17 |
| 15 namespace browser_sync { | 18 namespace browser_sync { |
| 16 | 19 |
| 17 // A pool for managing free/used tab sync nodes. Performs lazy creation | 20 // A pool for managing free/used tab sync nodes. Performs lazy creation |
| 18 // of sync nodes when necessary. | 21 // of sync nodes when necessary. |
| 19 class TabNodePool { | 22 class TabNodePool { |
| 20 public: | 23 public: |
| 21 explicit TabNodePool(ProfileSyncService* sync_service); | 24 explicit TabNodePool(ProfileSyncService* sync_service); |
| 22 ~TabNodePool(); | 25 ~TabNodePool(); |
| 23 | 26 enum InvalidTab { |
| 27 kInvalidTabID = -1 | |
| 28 }; | |
| 24 // Build a sync tag from tab_node_id. | 29 // Build a sync tag from tab_node_id. |
| 25 static std::string TabIdToTag(const std::string machine_tag, | 30 static std::string TabIdToTag(const std::string machine_tag, |
| 26 size_t tab_node_id); | 31 size_t tab_node_id); |
| 27 | 32 |
| 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); | |
| 34 | |
| 35 // Returns the sync_id for the next free tab node. If none are available, | 33 // Returns the sync_id for the next free tab node. If none are available, |
| 36 // creates a new tab node. | 34 // creates a new tab node and adds it to free nodes pool. The free node can |
| 35 // then be used to associate with a tab by calling AssociateTabNode. | |
|
Nicolas Zea
2013/06/19 21:35:33
Mention explicitly that the node is still consider
shashi
2013/06/20 00:49:32
Updated the comment. AssociateTabNode does the act
| |
| 37 // Note: We make use of the following "id's" | 36 // Note: We make use of the following "id's" |
|
Nicolas Zea
2013/06/19 21:35:33
I think at this point this section on what the ids
shashi
2013/06/20 00:49:32
Done
On 2013/06/19 21:35:33, Nicolas Zea wrote:
| |
| 38 // - a sync_id: an int64 used in |syncer::InitByIdLookup| | 37 // - a sync_id: an int64 used in |syncer::InitByIdLookup| |
| 39 // - a tab_id: created by session service, unique to this client | 38 // - 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 | 39 // - a tab_node_id: the id for a particular sync tab node. This is used |
| 41 // to generate the sync tab node tag through: | 40 // to generate the sync tab node tag through: |
| 42 // tab_tag = StringPrintf("%s_%ui", local_session_tag, tab_node_id); | 41 // 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 | 42 // 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 | 43 // 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 | 44 // is used when creating a new sync node, which returns the sync_id, created |
| 46 // by the sync db. | 45 // by the sync db. |
| 47 int64 GetFreeTabNode(); | 46 int64 GetFreeTabNode(); |
| 48 | 47 |
| 49 // Return a tab node to our free pool. | 48 // Removes the node from |tab_syncid_tab_id_map_| and returns the node to free |
| 50 // Note: the difference between FreeTabNode and AddTabNode is that | 49 // pool. |
| 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); | 50 void FreeTabNode(int64 sync_id); |
| 56 | 51 |
| 52 // Removes |sync_id| from free node pool and associates it with |tab_id|. | |
| 53 // sync_id should be id of a free node. In order to associate a non free | |
| 54 // sync node, use ReassociateTabNode. | |
| 55 void AssociateTabNode(int64 sync_id, SessionID::id_type tab_id); | |
| 56 | |
| 57 // Associate sync_id with tab_id but does not add it to free node pool. | |
| 58 // Note: this should only be called when we discover tab sync nodes from | |
| 59 // previous sessions, not for freeing tab nodes we created through | |
| 60 // GetFreeTabNode (use FreeTabNode below for that). | |
| 61 // The difference between AddTabNode and AssociateTabNode is that | |
| 62 // AssociateTabNode requires the sync node to be free to be associated. | |
| 63 // AddTabNode just adds the association. | |
| 64 void AddTabNode(int64 sync_id, const SessionID& tab_id, size_t tab_node_id); | |
| 65 | |
| 66 // Returns the tab_id for |sync_id| if it exists else returns kInvalidTabID. | |
| 67 SessionID::id_type TabIDForSyncID(int64 sync_id) const; | |
|
Nicolas Zea
2013/06/19 21:35:33
Rename to GetTabIdFromSyncId
shashi
2013/06/20 00:49:32
Done.
| |
| 68 | |
| 69 // Reassociates sync node with id |sync_id| with tab node with |tab_id|. | |
| 70 // Returns true if it is able to reassociate the node, false if sync node | |
|
Nicolas Zea
2013/06/19 21:35:33
"if a sync node with id |sync_id|"
shashi
2013/06/20 00:49:32
Done.
| |
| 71 // with |sync_id| is not associated with any tab. | |
| 72 bool ReassociateTabNode(int64 sync_id, SessionID::id_type tab_id); | |
| 73 | |
| 74 // Returns any nodes with sync_ids not in |used_synced_ids| to free node pool. | |
| 75 void FreeUnusedTabNodes(const std::set<int64>& used_sync_ids); | |
| 76 | |
| 57 // Clear tab pool. | 77 // Clear tab pool. |
| 58 void clear() { | 78 void clear() { |
|
Nicolas Zea
2013/06/19 21:35:33
nit: this was violating style guide before too, bu
shashi
2013/06/20 00:49:32
Done.
| |
| 59 tab_syncid_pool_.clear(); | 79 free_nodes_pool_.clear(); |
| 60 tab_pool_fp_ = -1; | 80 tab_syncid_tab_id_map_.clear(); |
| 81 max_used_tab_node_id_ = 0; | |
| 61 } | 82 } |
| 62 | 83 |
| 63 // Return the number of tab nodes this client currently has allocated | 84 // Return the number of tab nodes this client currently has allocated |
| 64 // (including both free and used nodes) | 85 // (including both free and used nodes) |
| 65 size_t capacity() const { return tab_syncid_pool_.size(); } | 86 size_t capacity() const { |
| 87 return tab_syncid_tab_id_map_.size() + free_nodes_pool_.size(); | |
| 88 } | |
| 66 | 89 |
| 67 // Return empty status (all tab nodes are in use). | 90 // Return empty status (all tab nodes are in use). |
| 68 bool empty() const { return tab_pool_fp_ == -1; } | 91 bool empty() const { return free_nodes_pool_.empty(); } |
| 69 | 92 |
| 70 // Return full status (no tab nodes are in use). | 93 // Return full status (no tab nodes are in use). |
| 71 bool full() { | 94 bool full() { return tab_syncid_tab_id_map_.empty(); } |
| 72 return tab_pool_fp_ == static_cast<int64>(tab_syncid_pool_.size())-1; | |
| 73 } | |
| 74 | 95 |
| 75 void set_machine_tag(const std::string& machine_tag) { | 96 void set_machine_tag(const std::string& machine_tag) { |
| 76 machine_tag_ = machine_tag; | 97 machine_tag_ = machine_tag; |
| 77 } | 98 } |
| 78 | 99 |
| 79 private: | 100 private: |
| 80 // Pool of all available syncid's for tab's we have created. | 101 FRIEND_TEST_ALL_PREFIXES(SyncTabNodePoolTest, TabNodeIdIncreases); |
|
Nicolas Zea
2013/06/19 21:35:33
I prefer friending a test class, and then having t
shashi
2013/06/20 00:49:32
Thanks, done, it is more cleaner now :).
On 2013/0
| |
| 81 std::vector<int64> tab_syncid_pool_; | 102 typedef std::map<int64, SessionID::id_type> SyncIDToTabIDMap; |
| 82 | 103 |
| 83 // Free pointer for tab pool. Only those node id's, up to and including the | 104 // Stores mapping of sync_ids associated with tab_ids, these are the used |
| 84 // one indexed by the free pointer, are valid and free. The rest of the | 105 // nodes of tab node pool. |
| 85 // |tab_syncid_pool_| is invalid because the nodes are in use. | 106 // The nodes in the map can be returned to free tab node pool by calling |
| 86 // To get the next free node, use tab_syncid_pool_[tab_pool_fp_--]. | 107 // FreeTabNode(sync_id). |
| 87 int64 tab_pool_fp_; | 108 SyncIDToTabIDMap tab_syncid_tab_id_map_; |
|
Nicolas Zea
2013/06/19 21:35:33
I think it's implied that sync ids belong to tabs,
shashi
2013/06/20 00:49:32
Done.
| |
| 88 | 109 |
| 89 // The machiine tag associated with this tab pool. Used in the title of new | 110 // The set of free nodes in the free node pool. |
|
Nicolas Zea
2013/06/19 21:35:33
"The sync ids for the set of free sync nodes" is a
shashi
2013/06/20 00:49:32
On 2013/06/19 21:35:33, Nicolas Zea wrote:
> "The
| |
| 111 std::set<int64> free_nodes_pool_; | |
| 112 | |
| 113 // The maximum used tab_node id for a sync node. A new sync node will always | |
| 114 // be created with max_used_tab_node_id_ + 1. | |
| 115 size_t max_used_tab_node_id_; | |
| 116 | |
| 117 // The machine tag associated with this tab pool. Used in the title of new | |
| 90 // sync nodes. | 118 // sync nodes. |
| 91 std::string machine_tag_; | 119 std::string machine_tag_; |
| 92 | 120 |
| 93 // Our sync service profile (for making changes to the sync db) | 121 // Our sync service profile (for making changes to the sync db) |
| 94 ProfileSyncService* sync_service_; | 122 ProfileSyncService* sync_service_; |
| 95 | 123 |
| 96 DISALLOW_COPY_AND_ASSIGN(TabNodePool); | 124 DISALLOW_COPY_AND_ASSIGN(TabNodePool); |
| 97 }; | 125 }; |
| 98 | 126 |
| 99 } // namespace browser_sync | 127 } // namespace browser_sync |
| 100 | 128 |
| 101 #endif // CHROME_BROWSER_SYNC_GLUE_TAB_NODE_POOL_H_ | 129 #endif // CHROME_BROWSER_SYNC_GLUE_TAB_NODE_POOL_H_ |
| OLD | NEW |