tab_node_pool.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. // Copyright 2014 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.
  4. #ifndef COMPONENTS_SYNC_SESSIONS_TAB_NODE_POOL_H_
  5. #define COMPONENTS_SYNC_SESSIONS_TAB_NODE_POOL_H_
  6. #include <stddef.h>
  7. #include <map>
  8. #include <set>
  9. #include "components/sessions/core/session_id.h"
  10. namespace sync_sessions {
  11. // A pool for managing free/used tab sync nodes for the *local* session.
  12. // Performs lazy creation of sync nodes when necessary.
  13. // Note: We make use of the following "id's"
  14. // - a tab_id: created by session service, unique to this client
  15. // - a tab_node_id: the id for a particular sync tab node. This is used
  16. // to generate the sync tab node tag through:
  17. // tab_tag = StringPrintf("%s %d", local_session_tag, tab_node_id);
  18. //
  19. // A sync node can be in one of the two states:
  20. // 1. Associated : Sync node is used and associated with a tab.
  21. // 2. Free : Sync node is unused.
  22. class TabNodePool {
  23. public:
  24. TabNodePool();
  25. TabNodePool(const TabNodePool&) = delete;
  26. TabNodePool& operator=(const TabNodePool&) = delete;
  27. ~TabNodePool();
  28. static const int kInvalidTabNodeID;
  29. // Returns the tab node associated with |tab_id| or kInvalidTabNodeID if
  30. // no association existed.
  31. int GetTabNodeIdFromTabId(SessionID tab_id) const;
  32. // Returns the tab_id for |tab_node_id| if it is associated else returns an
  33. // invalid ID.
  34. SessionID GetTabIdFromTabNodeId(int tab_node_id) const;
  35. // Gets the next free tab node (or creates a new one if needed) and associates
  36. // it to |tab_id|. Returns the tab node ID associated to |tab_id|. |tab_id|
  37. // must not be previously associated.
  38. int AssociateWithFreeTabNode(SessionID tab_id);
  39. // Reassociates |tab_node_id| with |tab_id|. If |tab_node_id| is not already
  40. // known, it is added to the tab node pool before being associated.
  41. void ReassociateTabNode(int tab_node_id, SessionID tab_id);
  42. // Removes association for |tab_id| and returns its tab node to the free node
  43. // pool.
  44. void FreeTab(SessionID tab_id);
  45. // Deletes all free tab nodes. Returns the IDs of the deleted nodes.
  46. std::set<int> CleanupFreeTabNodes();
  47. // Deletes all known mappings for |tab_node_id|. As opposed to FreeTab(), it
  48. // does NOT free the node for later reuse. This is used for foreign sessions
  49. // when remote deletions are received.
  50. void DeleteTabNode(int tab_node_id);
  51. // Returns tab node IDs for all known (used or free) tab nodes.
  52. std::set<int> GetAllTabNodeIds() const;
  53. int GetMaxUsedTabNodeIdForTest() const;
  54. private:
  55. using TabNodeIDToTabIDMap = std::map<int, SessionID>;
  56. using TabIDToTabNodeIDMap = std::map<SessionID, int>;
  57. // Adds |tab_node_id| to the tab 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. // GetTabNodeForTab (use FreeTab for that).
  61. void AddTabNode(int tab_node_id);
  62. // Associates |tab_node_id| with |tab_id|. |tab_node_id| must be free. In
  63. // order to associated a non-free tab node, ReassociateTabNode must be
  64. // used.
  65. void AssociateTabNode(int tab_node_id, SessionID tab_id);
  66. // Stores mapping of node ids associated with tab_ids, these are the used
  67. // nodes of tab node pool.
  68. // The nodes in the map can be returned to free tab node pool by calling
  69. // FreeTab(..).
  70. TabNodeIDToTabIDMap nodeid_tabid_map_;
  71. TabIDToTabNodeIDMap tabid_nodeid_map_;
  72. // The node ids for the set of free sync nodes.
  73. std::set<int> free_nodes_pool_;
  74. // The maximum used tab_node id for a sync node.
  75. int max_used_tab_node_id_;
  76. // Not actual tab nodes, but instead represent "holes", i.e. tab node IDs
  77. // that are not used within the range [0..max_used_tab_node_id_). This
  78. // allows AssociateWithFreeTabNode() to return a compact distribution of IDs.
  79. std::set<int> missing_nodes_pool_;
  80. };
  81. } // namespace sync_sessions
  82. #endif // COMPONENTS_SYNC_SESSIONS_TAB_NODE_POOL_H_