Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2012 The Chromium Authors. All rights reserved. | 1 // Copyright 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 COMPONENTS_SYNC_SESSIONS_SYNCED_SESSION_TRACKER_H_ | 5 #ifndef COMPONENTS_SYNC_SESSIONS_SYNCED_SESSION_TRACKER_H_ |
| 6 #define COMPONENTS_SYNC_SESSIONS_SYNCED_SESSION_TRACKER_H_ | 6 #define COMPONENTS_SYNC_SESSIONS_SYNCED_SESSION_TRACKER_H_ |
| 7 | 7 |
| 8 #include <stddef.h> | 8 #include <stddef.h> |
| 9 | 9 |
| 10 #include <map> | 10 #include <map> |
| (...skipping 22 matching lines...) Expand all Loading... | |
| 33 public: | 33 public: |
| 34 explicit SyncedSessionTracker( | 34 explicit SyncedSessionTracker( |
| 35 sync_sessions::SyncSessionsClient* sessions_client); | 35 sync_sessions::SyncSessionsClient* sessions_client); |
| 36 ~SyncedSessionTracker(); | 36 ~SyncedSessionTracker(); |
| 37 | 37 |
| 38 // We track and distinguish the local session from foreign sessions. | 38 // We track and distinguish the local session from foreign sessions. |
| 39 void SetLocalSessionTag(const std::string& local_session_tag); | 39 void SetLocalSessionTag(const std::string& local_session_tag); |
| 40 | 40 |
| 41 // Fill a preallocated vector with all foreign sessions we're tracking (skips | 41 // Fill a preallocated vector with all foreign sessions we're tracking (skips |
| 42 // the local session object). SyncedSession ownership remains within the | 42 // the local session object). SyncedSession ownership remains within the |
| 43 // SyncedSessionTracker. | 43 // SyncedSessionTracker. Can specify if you want only presentable sessions, |
| 44 // which means they have at least one window with at least one tab that has | |
| 45 // syncable content. | |
| 44 // Returns true if we had foreign sessions to fill it with, false otherwise. | 46 // Returns true if we had foreign sessions to fill it with, false otherwise. |
| 45 bool LookupAllForeignSessions( | 47 bool LookupAllForeignSessions( |
| 46 std::vector<const sync_driver::SyncedSession*>* sessions) const; | 48 std::vector<const sync_driver::SyncedSession*>* sessions, |
| 49 bool presentable) const; | |
| 47 | 50 |
| 48 // Attempts to look up the session windows associatd with the session given | 51 // Attempts to look up the session windows associatd with the session given |
| 49 // by |session_tag|. Ownership Of SessionWindows stays within the | 52 // by |session_tag|. Ownership Of SessionWindows stays within the |
| 50 // SyncedSessionTracker. | 53 // SyncedSessionTracker. |
| 51 // If lookup succeeds: | 54 // If lookup succeeds: |
| 52 // - Fills windows with the SessionWindow pointers, returns true. | 55 // - Fills windows with the SessionWindow pointers, returns true. |
| 53 // Else | 56 // Else |
| 54 // - Returns false. | 57 // - Returns false. |
| 55 bool LookupSessionWindows( | 58 bool LookupSessionWindows( |
| 56 const std::string& session_tag, | 59 const std::string& session_tag, |
| 57 std::vector<const sessions::SessionWindow*>* windows) const; | 60 std::vector<const sessions::SessionWindow*>* windows) const; |
| 58 | 61 |
| 59 // Attempts to look up the tab associated with the given tag and tab id. | 62 // Attempts to look up the tab associated with the given tag and tab id. |
| 60 // Ownership of the SessionTab remains within the SyncedSessionTracker. | 63 // Ownership of the SessionTab remains within the SyncedSessionTracker. |
| 61 // If lookup succeeds: | 64 // If lookup succeeds: |
| 62 // - Sets tab to point to the SessionTab, and returns true. | 65 // - Sets tab to point to the SessionTab, and returns true. |
| 63 // Else | 66 // Else |
| 64 // - Returns false, tab is set to NULL. | 67 // - Returns false, tab is set to NULL. |
| 65 bool LookupSessionTab(const std::string& session_tag, | 68 bool LookupSessionTab(const std::string& session_tag, |
| 66 SessionID::id_type tab_id, | 69 SessionID::id_type tab_id, |
| 67 const sessions::SessionTab** tab) const; | 70 const sessions::SessionTab** tab) const; |
| 68 | 71 |
| 69 // Allows retrieval of existing data for the local session. Unlike GetSession | 72 // Allows retrieval of existing data for the local session. Unlike GetSession |
| 70 // this won't create-if-not-present. | 73 // this won't create-if-not-present. |
| 71 bool LookupLocalSession(const sync_driver::SyncedSession** output) const; | 74 bool LookupLocalSession(const sync_driver::SyncedSession** output) const; |
| 72 | 75 |
| 73 // Returns a pointer to the SyncedSession object associated with | 76 // Returns a pointer to the SyncedSession object associated with |
| 74 // |session_tag|. If none exists, creates one. Ownership of the | 77 // |session_tag|. If none exists, creates one. Ownership of the |
| 75 // SyncedSession remains within the SyncedSessionTracker. | 78 // SyncedSession remains within the SyncedSessionTracker. |
| 76 sync_driver::SyncedSession* GetSession(const std::string& session_tag); | 79 sync_driver::SyncedSession* GetSession(const std::string& session_tag); |
| 77 | 80 |
| 78 // Deletes the session associated with |session_tag| if it exists. | 81 // Deletes the session associated with |session_tag| if it exists. |
| 79 // Returns true if the session existed and was deleted, false otherwise. | 82 // Returns true if the session existed and was deleted, false otherwise. |
| 80 bool DeleteSession(const std::string& session_tag); | 83 bool DeleteSession(const std::string& session_tag); |
| 81 | 84 |
| 82 // Resets the tracking information for the session specified by |session_tag|. | 85 // Resets the tracking information for the session specified by |session_tag|. |
| 83 // This involves clearing all the windows and tabs from the session, while | 86 // This involves clearing all the windows and tabs from the session, while |
| 84 // keeping pointers saved in the synced_window_map_ and synced_tab_map_. | 87 // keeping pointers saved in the synced_window_map_ and synced_tab_map_. |
| 85 // Once reset, all calls to PutWindowInSession and PutTabInWindow will denote | 88 // Once reset, all calls to PutWindowInSession and PutTabInWindow will denote |
| 86 // that the requested windows and tabs are owned (by setting the boolean | 89 // that the requested windows and tabs are owned (by setting the boolean |
| 87 // in their SessionWindowWrapper/SessionTabWrapper to true) and add them back | 90 // in their SessionWindowWrapper/SessionTabWrapper to true) and add them back |
| 88 // to their session. The next call to CleanupSession(...) will delete those | 91 // to their session. The next call to CleanupSession(...) will delete those |
| 89 // windows and tabs not owned. | 92 // windows and tabs not owned. |
| 90 void ResetSessionTracking(const std::string& session_tag); | 93 void ResetSessionTracking(const std::string& session_tag); |
| 91 | 94 |
| 95 // Tracks the deletion of a foreign tab by removing the given |tab_node_id|. | |
| 96 // Doesn't actually remove any tab objects because the header may have or may | |
| 97 // not have already been updated to no longer parent this tab. Regardless, | |
| 98 // when the header is updated then cleanup will remove the actual tab data. | |
| 99 // However, this method always needs to be called upon foreign tab deletion, | |
| 100 // otherwise LookupTabNodeIds(...) may return already deleted tab node ids. | |
| 101 void DeleteForeignTab(const std::string& session_tag, int tab_node_id); | |
| 102 | |
| 92 // Deletes those windows and tabs associated with |session_tag| that are no | 103 // Deletes those windows and tabs associated with |session_tag| that are no |
| 93 // longer owned. | 104 // longer owned. |
| 94 // See ResetSessionTracking(...). | |
|
Nicolas Zea
2016/04/12 20:38:04
Should this remain?
skym
2016/04/12 22:12:14
Yes. Done.
| |
| 95 void CleanupSession(const std::string& session_tag); | 105 void CleanupSession(const std::string& session_tag); |
| 96 | 106 |
| 97 // Adds the window with id |window_id| to the session specified by | 107 // Adds the window with id |window_id| to the session specified by |
| 98 // |session_tag|, and markes the window as being owned. If none existed for | 108 // |session_tag|, and markes the window as being owned. If none existed for |
| 99 // that session, creates one. Similarly, if the session did not exist yet, | 109 // that session, creates one. Similarly, if the session did not exist yet, |
| 100 // creates it. Ownership of the SessionWindow remains within the | 110 // creates it. Ownership of the SessionWindow remains within the |
| 101 // SyncedSessionTracker. | 111 // SyncedSessionTracker. |
| 102 void PutWindowInSession(const std::string& session_tag, | 112 void PutWindowInSession(const std::string& session_tag, |
| 103 SessionID::id_type window_id); | 113 SessionID::id_type window_id); |
| 104 | 114 |
| (...skipping 10 matching lines...) Expand all Loading... | |
| 115 // Returns a pointer to the SessionTab object associated with |tab_id| for | 125 // Returns a pointer to the SessionTab object associated with |tab_id| for |
| 116 // the session specified with |session_tag|. If none exists, creates one. | 126 // the session specified with |session_tag|. If none exists, creates one. |
| 117 // Ownership of the SessionTab remains within the SyncedSessionTracker. | 127 // Ownership of the SessionTab remains within the SyncedSessionTracker. |
| 118 // |tab_node_id| must be a valid node id for the node backing this tab. | 128 // |tab_node_id| must be a valid node id for the node backing this tab. |
| 119 sessions::SessionTab* GetTab(const std::string& session_tag, | 129 sessions::SessionTab* GetTab(const std::string& session_tag, |
| 120 SessionID::id_type tab_id, | 130 SessionID::id_type tab_id, |
| 121 int tab_node_id); | 131 int tab_node_id); |
| 122 | 132 |
| 123 // Fills |tab_node_ids| with the tab node ids (see GetTab) for all the tabs* | 133 // Fills |tab_node_ids| with the tab node ids (see GetTab) for all the tabs* |
| 124 // associated with the session having tag |session_tag|. | 134 // associated with the session having tag |session_tag|. |
| 125 // Returns false if we don't have any record of the session. If no tabs were | 135 void LookupTabNodeIds(const std::string& session_tag, |
| 126 // found as part of the session, the return value will be true but | |
| 127 // |tab_node_ids| will be empty. | |
| 128 // | |
| 129 // * - note that this only returns the ids we're aware of; it's possible we | |
| 130 // don't have the latest tab state from a foreign session and it's also | |
| 131 // possible we just haven't updated the tab_node_id for a tab yet, so the | |
| 132 // result list should not be treated as authoritative. | |
| 133 bool LookupTabNodeIds(const std::string& session_tag, | |
| 134 std::set<int>* tab_node_ids); | 136 std::set<int>* tab_node_ids); |
| 135 | 137 |
| 136 // Free the memory for all dynamically allocated objects and clear the | 138 // Free the memory for all dynamically allocated objects and clear the |
| 137 // tracking structures. | 139 // tracking structures. |
| 138 void Clear(); | 140 void Clear(); |
| 139 | 141 |
| 140 bool Empty() const { | 142 bool Empty() const { |
| 141 return synced_tab_map_.empty() && synced_session_map_.empty(); | 143 return synced_tab_map_.empty() && synced_session_map_.empty(); |
| 142 } | 144 } |
| 143 | 145 |
| (...skipping 25 matching lines...) Expand all Loading... | |
| 169 // orphaned. CleanupSession(...) can then delete any orphanted data objects | 171 // orphaned. CleanupSession(...) can then delete any orphanted data objects |
| 170 // and wrapper for a given session via session tag. | 172 // and wrapper for a given session via session tag. |
| 171 enum OwnedState { IS_OWNED, NOT_OWNED }; | 173 enum OwnedState { IS_OWNED, NOT_OWNED }; |
| 172 | 174 |
| 173 // Datatypes for accessing session data. Neither of the *Wrappers actually | 175 // Datatypes for accessing session data. Neither of the *Wrappers actually |
| 174 // have ownership of the Windows/Tabs, they just provide id-based access to | 176 // have ownership of the Windows/Tabs, they just provide id-based access to |
| 175 // them. The ownership remains within its containing session (for windows and | 177 // them. The ownership remains within its containing session (for windows and |
| 176 // mapped tabs, unmapped tabs are owned by the unmapped_tabs_ container). | 178 // mapped tabs, unmapped tabs are owned by the unmapped_tabs_ container). |
| 177 // Note, we pair pointers with bools so that we can track what is owned and | 179 // Note, we pair pointers with bools so that we can track what is owned and |
| 178 // what can be deleted (see ResetSessionTracking(..) and CleanupSession(..) | 180 // what can be deleted (see ResetSessionTracking(..) and CleanupSession(..) |
| 179 // above). | 181 // above). IsOwned is used as a wrapper constructor parameter for readability. |
| 180 // The wrappers also serve as a convenient place to augment state stored in | |
| 181 // SessionTab for sync purposes, such as |tab_node_id|. | |
| 182 // IsOwned is used as a wrapper constructor parameter for readability. | |
| 183 struct SessionTabWrapper { | 182 struct SessionTabWrapper { |
| 184 SessionTabWrapper() | 183 SessionTabWrapper() : tab_ptr(NULL), owned(false) {} |
| 185 : tab_ptr(NULL), | 184 |
| 186 owned(false), | 185 SessionTabWrapper(sessions::SessionTab* tab_ptr, OwnedState owned) |
| 187 tab_node_id(TabNodePool::kInvalidTabNodeID) {} | 186 : tab_ptr(tab_ptr), owned(owned == IS_OWNED) {} |
| 188 SessionTabWrapper(sessions::SessionTab* tab_ptr, | 187 |
| 189 OwnedState owned, | |
| 190 int tab_node_id) | |
| 191 : tab_ptr(tab_ptr), | |
| 192 owned(owned == IS_OWNED), | |
| 193 tab_node_id(tab_node_id) {} | |
| 194 sessions::SessionTab* tab_ptr; | 188 sessions::SessionTab* tab_ptr; |
| 195 | 189 |
| 196 // This is used as part of a mark-and-sweep approach to garbage | 190 // This is used as part of a mark-and-sweep approach to garbage |
| 197 // collection for closed tabs that are no longer "in use", or "owned". | 191 // collection for closed tabs that are no longer "in use", or "owned". |
| 198 // ResetSessionTracking will clear |owned| bits, and if it is not claimed | 192 // ResetSessionTracking will clear |owned| bits, and if it is not claimed |
| 199 // by a window by the time CleanupSession is called it will be deleted. | 193 // by a window by the time CleanupSession is called it will be deleted. |
| 200 bool owned; | 194 bool owned; |
| 201 | |
| 202 // This lets us identify the sync node that is "backing" this tab in the | |
| 203 // sync model, whether it is part of a local or foreign session. The | |
| 204 // "tab node id" is described in session_specifics.proto. | |
| 205 int tab_node_id; | |
| 206 }; | 195 }; |
| 207 typedef std::map<SessionID::id_type, SessionTabWrapper> IDToSessionTabMap; | 196 typedef std::map<SessionID::id_type, SessionTabWrapper> IDToSessionTabMap; |
| 208 typedef std::map<std::string, IDToSessionTabMap> SyncedTabMap; | 197 typedef std::map<std::string, IDToSessionTabMap> SyncedTabMap; |
| 209 | 198 |
| 210 struct SessionWindowWrapper { | 199 struct SessionWindowWrapper { |
| 211 SessionWindowWrapper() : window_ptr(NULL), owned(false) {} | 200 SessionWindowWrapper() : window_ptr(NULL), owned(false) {} |
| 212 SessionWindowWrapper(sessions::SessionWindow* window_ptr, OwnedState owned) | 201 SessionWindowWrapper(sessions::SessionWindow* window_ptr, OwnedState owned) |
| 213 : window_ptr(window_ptr), owned(owned == IS_OWNED) {} | 202 : window_ptr(window_ptr), owned(owned == IS_OWNED) {} |
| 214 sessions::SessionWindow* window_ptr; | 203 sessions::SessionWindow* window_ptr; |
| 215 bool owned; | 204 bool owned; |
| 216 }; | 205 }; |
| 217 typedef std::map<SessionID::id_type, SessionWindowWrapper> | 206 typedef std::map<SessionID::id_type, SessionWindowWrapper> |
| 218 IDToSessionWindowMap; | 207 IDToSessionWindowMap; |
| 219 typedef std::map<std::string, IDToSessionWindowMap> SyncedWindowMap; | 208 typedef std::map<std::string, IDToSessionWindowMap> SyncedWindowMap; |
| 220 | 209 |
| 221 typedef std::map<std::string, sync_driver::SyncedSession*> SyncedSessionMap; | 210 typedef std::map<std::string, sync_driver::SyncedSession*> SyncedSessionMap; |
| 222 | 211 |
| 223 // Helper methods for deleting SessionWindows and SessionTabs without owners. | 212 // Helper methods for deleting SessionWindows and SessionTabs without owners. |
| 224 bool DeleteOldSessionWindowIfNecessary(SessionWindowWrapper window_wrapper); | 213 bool DeleteOldSessionWindowIfNecessary( |
| 225 bool DeleteOldSessionTabIfNecessary(SessionTabWrapper tab_wrapper); | 214 const SessionWindowWrapper& window_wrapper); |
| 215 bool DeleteOldSessionTabIfNecessary(const SessionTabWrapper& tab_wrapper); | |
| 226 | 216 |
| 227 // Implementation for GetTab(...) above, permits invalid tab_node_id. | 217 // Implementation for GetTab(...) above, permits invalid tab_node_id. |
| 228 sessions::SessionTab* GetTabImpl(const std::string& session_tag, | 218 sessions::SessionTab* GetTabImpl(const std::string& session_tag, |
| 229 SessionID::id_type tab_id, | 219 SessionID::id_type tab_id, |
| 230 int tab_node_id); | 220 int tab_node_id); |
| 231 | 221 |
| 232 // The client of the sync sessions datatype. | 222 // The client of the sync sessions datatype. |
| 233 sync_sessions::SyncSessionsClient* const sessions_client_; | 223 sync_sessions::SyncSessionsClient* const sessions_client_; |
| 234 | 224 |
| 235 // Per client mapping of tab id's to their SessionTab objects. | 225 // Per client mapping of tab id's to their SessionTab objects. |
| (...skipping 20 matching lines...) Expand all Loading... | |
| 256 // The tag for this machine's local session, so we can distinguish the foreign | 246 // The tag for this machine's local session, so we can distinguish the foreign |
| 257 // sessions. | 247 // sessions. |
| 258 std::string local_session_tag_; | 248 std::string local_session_tag_; |
| 259 | 249 |
| 260 DISALLOW_COPY_AND_ASSIGN(SyncedSessionTracker); | 250 DISALLOW_COPY_AND_ASSIGN(SyncedSessionTracker); |
| 261 }; | 251 }; |
| 262 | 252 |
| 263 } // namespace browser_sync | 253 } // namespace browser_sync |
| 264 | 254 |
| 265 #endif // COMPONENTS_SYNC_SESSIONS_SYNCED_SESSION_TRACKER_H_ | 255 #endif // COMPONENTS_SYNC_SESSIONS_SYNCED_SESSION_TRACKER_H_ |
| OLD | NEW |