| 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. |
| 22 // Note: We make use of the following "id's" |
| 23 // - a sync_id: an int64 used in |syncer::InitByIdLookup| |
| 24 // - a tab_id: created by session service, unique to this client |
| 25 // - a tab_node_id: the id for a particular sync tab node. This is used |
| 26 // to generate the sync tab node tag through: |
| 27 // tab_tag = StringPrintf("%s_%ui", local_session_tag, tab_node_id); |
| 28 // tab_node_id and sync_id are both unique to a particular sync node. The |
| 29 // difference is that tab_node_id is controlled by the model associator and |
| 30 // is used when creating a new sync node, which returns the sync_id, created |
| 31 // by the sync db. |
| 19 class TabNodePool { | 32 class TabNodePool { |
| 20 public: | 33 public: |
| 21 explicit TabNodePool(ProfileSyncService* sync_service); | 34 explicit TabNodePool(ProfileSyncService* sync_service); |
| 22 ~TabNodePool(); | 35 ~TabNodePool(); |
| 23 | 36 enum InvalidTab { |
| 37 kInvalidTabID = -1 |
| 38 }; |
| 24 // Build a sync tag from tab_node_id. | 39 // Build a sync tag from tab_node_id. |
| 25 static std::string TabIdToTag(const std::string machine_tag, | 40 static std::string TabIdToTag(const std::string machine_tag, |
| 26 size_t tab_node_id); | 41 size_t tab_node_id); |
| 27 | 42 |
| 28 // Add a previously allocated tab sync node to our pool. Increases the size | 43 // Returns the sync_id for the next free tab node. If none are available, |
| 29 // of tab_syncid_pool_ by one and marks the new tab node as free. | 44 // creates a new tab node and adds it to free nodes pool. The free node can |
| 45 // then be used to associate with a tab by calling AssociateTabNode. |
| 46 // Note: The node is considered free until it has been associated. Repeated |
| 47 // calls to GetFreeTabNode will return the same sync_id until node has been |
| 48 // associated. |
| 49 int64 GetFreeTabNode(); |
| 50 |
| 51 // Removes the node from |syncid_tabid_map_| and returns the node to free |
| 52 // pool. |
| 53 void FreeTabNode(int64 sync_id); |
| 54 |
| 55 // Removes |sync_id| from free node pool and associates it with |tab_id|. |
| 56 // sync_id should be id of a free node. In order to associate a non free |
| 57 // sync node, use ReassociateTabNode. |
| 58 void AssociateTabNode(int64 sync_id, SessionID::id_type tab_id); |
| 59 |
| 60 // Associate sync_id with tab_id but does not add it to free node pool. |
| 30 // Note: this should only be called when we discover tab sync nodes from | 61 // Note: this should only be called when we discover tab sync nodes from |
| 31 // previous sessions, not for freeing tab nodes we created through | 62 // previous sessions, not for freeing tab nodes we created through |
| 32 // GetFreeTabNode (use FreeTabNode below for that). | 63 // GetFreeTabNode (use FreeTabNode below for that). |
| 33 void AddTabNode(int64 sync_id); | 64 // The difference between AddTabNode and AssociateTabNode is that |
| 65 // AssociateTabNode requires the sync node to be free to be associated. |
| 66 // AddTabNode just adds the association. |
| 67 void AddTabNode(int64 sync_id, const SessionID& tab_id, size_t tab_node_id); |
| 34 | 68 |
| 35 // Returns the sync_id for the next free tab node. If none are available, | 69 // Returns the tab_id for |sync_id| if it exists else returns kInvalidTabID. |
| 36 // creates a new tab node. | 70 SessionID::id_type GetTabIdFromSyncId(int64 sync_id) const; |
| 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(); | |
| 48 | 71 |
| 49 // Return a tab node to our free pool. | 72 // Reassociates sync node with id |sync_id| with tab node with |tab_id|. |
| 50 // Note: the difference between FreeTabNode and AddTabNode is that | 73 // Returns true if it is able to reassociate the node, false if a sync node |
| 51 // FreeTabNode does not modify the size of |tab_syncid_pool_|, while | 74 // with id |sync_id| is not associated with any tab. |
| 52 // AddTabNode increases it by one. In the case of FreeTabNode, the size of | 75 bool ReassociateTabNode(int64 sync_id, SessionID::id_type tab_id); |
| 53 // the |tab_syncid_pool_| should always be equal to the amount of tab nodes | 76 |
| 54 // associated with this machine. | 77 // Returns any nodes with sync_ids not in |used_synced_ids| to free node pool. |
| 55 void FreeTabNode(int64 sync_id); | 78 void FreeUnusedTabNodes(const std::set<int64>& used_sync_ids); |
| 56 | 79 |
| 57 // Clear tab pool. | 80 // Clear tab pool. |
| 58 void clear() { | 81 void Clear(); |
| 59 tab_syncid_pool_.clear(); | |
| 60 tab_pool_fp_ = -1; | |
| 61 } | |
| 62 | 82 |
| 63 // Return the number of tab nodes this client currently has allocated | 83 // Return the number of tab nodes this client currently has allocated |
| 64 // (including both free and used nodes) | 84 // (including both free and used nodes) |
| 65 size_t capacity() const { return tab_syncid_pool_.size(); } | 85 size_t Capacity() const; |
| 66 | 86 |
| 67 // Return empty status (all tab nodes are in use). | 87 // Return empty status (all tab nodes are in use). |
| 68 bool empty() const { return tab_pool_fp_ == -1; } | 88 bool Empty() const; |
| 69 | 89 |
| 70 // Return full status (no tab nodes are in use). | 90 // Return full status (no tab nodes are in use). |
| 71 bool full() { | 91 bool Full(); |
| 72 return tab_pool_fp_ == static_cast<int64>(tab_syncid_pool_.size())-1; | |
| 73 } | |
| 74 | 92 |
| 75 void set_machine_tag(const std::string& machine_tag) { | 93 void SetMachineTag(const std::string& machine_tag); |
| 76 machine_tag_ = machine_tag; | |
| 77 } | |
| 78 | 94 |
| 79 private: | 95 private: |
| 80 // Pool of all available syncid's for tab's we have created. | 96 friend class SyncTabNodePoolTest; |
| 81 std::vector<int64> tab_syncid_pool_; | 97 typedef std::map<int64, SessionID::id_type> SyncIDToTabIDMap; |
| 82 | 98 |
| 83 // Free pointer for tab pool. Only those node id's, up to and including the | 99 // 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 | 100 // nodes of tab node pool. |
| 85 // |tab_syncid_pool_| is invalid because the nodes are in use. | 101 // 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_--]. | 102 // FreeTabNode(sync_id). |
| 87 int64 tab_pool_fp_; | 103 SyncIDToTabIDMap syncid_tabid_map_; |
| 88 | 104 |
| 89 // The machiine tag associated with this tab pool. Used in the title of new | 105 // The sync ids for the set of free sync nodes. |
| 106 std::set<int64> free_nodes_pool_; |
| 107 |
| 108 // The maximum used tab_node id for a sync node. A new sync node will always |
| 109 // be created with max_used_tab_node_id_ + 1. |
| 110 size_t max_used_tab_node_id_; |
| 111 |
| 112 // The machine tag associated with this tab pool. Used in the title of new |
| 90 // sync nodes. | 113 // sync nodes. |
| 91 std::string machine_tag_; | 114 std::string machine_tag_; |
| 92 | 115 |
| 93 // Our sync service profile (for making changes to the sync db) | 116 // Our sync service profile (for making changes to the sync db) |
| 94 ProfileSyncService* sync_service_; | 117 ProfileSyncService* sync_service_; |
| 95 | 118 |
| 96 DISALLOW_COPY_AND_ASSIGN(TabNodePool); | 119 DISALLOW_COPY_AND_ASSIGN(TabNodePool); |
| 97 }; | 120 }; |
| 98 | 121 |
| 99 } // namespace browser_sync | 122 } // namespace browser_sync |
| 100 | 123 |
| 101 #endif // CHROME_BROWSER_SYNC_GLUE_TAB_NODE_POOL_H_ | 124 #endif // CHROME_BROWSER_SYNC_GLUE_TAB_NODE_POOL_H_ |
| OLD | NEW |