| 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> |
| 11 #include <set> | 11 #include <set> |
| 12 #include <string> | 12 #include <string> |
| 13 #include <vector> | 13 #include <vector> |
| 14 | 14 |
| 15 #include "base/macros.h" | 15 #include "base/macros.h" |
| 16 #include "components/sessions/core/session_id.h" | 16 #include "components/sessions/core/session_id.h" |
| 17 #include "components/sessions/core/session_types.h" | 17 #include "components/sessions/core/session_types.h" |
| 18 #include "components/sync_sessions/synced_session.h" | 18 #include "components/sync_sessions/synced_session.h" |
| 19 #include "components/sync_sessions/tab_node_pool.h" | 19 #include "components/sync_sessions/tab_node_pool.h" |
| 20 | 20 |
| 21 namespace sync_sessions { | 21 namespace sync_sessions { |
| 22 |
| 22 class SyncSessionsClient; | 23 class SyncSessionsClient; |
| 23 } | |
| 24 | |
| 25 namespace browser_sync { | |
| 26 | 24 |
| 27 // Class to manage synced sessions. The tracker will own all SyncedSession | 25 // Class to manage synced sessions. The tracker will own all SyncedSession |
| 28 // and SyncedSessionTab objects it creates, and deletes them appropriately on | 26 // and SyncedSessionTab objects it creates, and deletes them appropriately on |
| 29 // destruction. | 27 // destruction. |
| 30 // Note: SyncedSession objects are created for all synced sessions, including | 28 // Note: SyncedSession objects are created for all synced sessions, including |
| 31 // the local session (whose tag we maintain separately). | 29 // the local session (whose tag we maintain separately). |
| 32 class SyncedSessionTracker { | 30 class SyncedSessionTracker { |
| 33 public: | 31 public: |
| 34 // Different ways to lookup/filter tabs. | 32 // Different ways to lookup/filter tabs. |
| 35 enum SessionLookup { | 33 enum SessionLookup { |
| 36 RAW, // Return all foreign sessions. | 34 RAW, // Return all foreign sessions. |
| 37 PRESENTABLE // Have one window with at least one tab with syncable content. | 35 PRESENTABLE // Have one window with at least one tab with syncable content. |
| 38 }; | 36 }; |
| 39 | 37 |
| 40 explicit SyncedSessionTracker( | 38 explicit SyncedSessionTracker(SyncSessionsClient* sessions_client); |
| 41 sync_sessions::SyncSessionsClient* sessions_client); | |
| 42 ~SyncedSessionTracker(); | 39 ~SyncedSessionTracker(); |
| 43 | 40 |
| 44 // We track and distinguish the local session from foreign sessions. | 41 // We track and distinguish the local session from foreign sessions. |
| 45 void SetLocalSessionTag(const std::string& local_session_tag); | 42 void SetLocalSessionTag(const std::string& local_session_tag); |
| 46 | 43 |
| 47 // Fill a preallocated vector with all foreign sessions we're tracking (skips | 44 // Fill a preallocated vector with all foreign sessions we're tracking (skips |
| 48 // the local session object). SyncedSession ownership remains within the | 45 // the local session object). SyncedSession ownership remains within the |
| 49 // SyncedSessionTracker. Lookup parameter is used to decide which foreign tabs | 46 // SyncedSessionTracker. Lookup parameter is used to decide which foreign tabs |
| 50 // should be include. | 47 // should be include. |
| 51 // Returns true if we had foreign sessions to fill it with, false otherwise. | 48 // Returns true if we had foreign sessions to fill it with, false otherwise. |
| 52 bool LookupAllForeignSessions( | 49 bool LookupAllForeignSessions(std::vector<const SyncedSession*>* sessions, |
| 53 std::vector<const sync_driver::SyncedSession*>* sessions, | 50 SessionLookup lookup) const; |
| 54 SessionLookup lookup) const; | |
| 55 | 51 |
| 56 // Attempts to look up the session windows associatd with the session given | 52 // Attempts to look up the session windows associatd with the session given |
| 57 // by |session_tag|. Ownership Of SessionWindows stays within the | 53 // by |session_tag|. Ownership Of SessionWindows stays within the |
| 58 // SyncedSessionTracker. | 54 // SyncedSessionTracker. |
| 59 // If lookup succeeds: | 55 // If lookup succeeds: |
| 60 // - Fills windows with the SessionWindow pointers, returns true. | 56 // - Fills windows with the SessionWindow pointers, returns true. |
| 61 // Else | 57 // Else |
| 62 // - Returns false. | 58 // - Returns false. |
| 63 bool LookupSessionWindows( | 59 bool LookupSessionWindows( |
| 64 const std::string& session_tag, | 60 const std::string& session_tag, |
| 65 std::vector<const sessions::SessionWindow*>* windows) const; | 61 std::vector<const sessions::SessionWindow*>* windows) const; |
| 66 | 62 |
| 67 // Attempts to look up the tab associated with the given tag and tab id. | 63 // Attempts to look up the tab associated with the given tag and tab id. |
| 68 // Ownership of the SessionTab remains within the SyncedSessionTracker. | 64 // Ownership of the SessionTab remains within the SyncedSessionTracker. |
| 69 // If lookup succeeds: | 65 // If lookup succeeds: |
| 70 // - Sets tab to point to the SessionTab, and returns true. | 66 // - Sets tab to point to the SessionTab, and returns true. |
| 71 // Else | 67 // Else |
| 72 // - Returns false, tab is set to NULL. | 68 // - Returns false, tab is set to NULL. |
| 73 bool LookupSessionTab(const std::string& session_tag, | 69 bool LookupSessionTab(const std::string& session_tag, |
| 74 SessionID::id_type tab_id, | 70 SessionID::id_type tab_id, |
| 75 const sessions::SessionTab** tab) const; | 71 const sessions::SessionTab** tab) const; |
| 76 | 72 |
| 77 // Allows retrieval of existing data for the local session. Unlike GetSession | 73 // Allows retrieval of existing data for the local session. Unlike GetSession |
| 78 // this won't create-if-not-present. | 74 // this won't create-if-not-present. |
| 79 bool LookupLocalSession(const sync_driver::SyncedSession** output) const; | 75 bool LookupLocalSession(const SyncedSession** output) const; |
| 80 | 76 |
| 81 // Returns a pointer to the SyncedSession object associated with | 77 // Returns a pointer to the SyncedSession object associated with |
| 82 // |session_tag|. If none exists, creates one. Ownership of the | 78 // |session_tag|. If none exists, creates one. Ownership of the |
| 83 // SyncedSession remains within the SyncedSessionTracker. | 79 // SyncedSession remains within the SyncedSessionTracker. |
| 84 sync_driver::SyncedSession* GetSession(const std::string& session_tag); | 80 SyncedSession* GetSession(const std::string& session_tag); |
| 85 | 81 |
| 86 // Deletes the session associated with |session_tag| if it exists. | 82 // Deletes the session associated with |session_tag| if it exists. |
| 87 // Returns true if the session existed and was deleted, false otherwise. | 83 // Returns true if the session existed and was deleted, false otherwise. |
| 88 bool DeleteSession(const std::string& session_tag); | 84 bool DeleteSession(const std::string& session_tag); |
| 89 | 85 |
| 90 // Resets the tracking information for the session specified by |session_tag|. | 86 // Resets the tracking information for the session specified by |session_tag|. |
| 91 // This involves clearing all the windows and tabs from the session, while | 87 // This involves clearing all the windows and tabs from the session, while |
| 92 // keeping pointers saved in the synced_window_map_ and synced_tab_map_. | 88 // keeping pointers saved in the synced_window_map_ and synced_tab_map_. |
| 93 // Once reset, all calls to PutWindowInSession and PutTabInWindow will denote | 89 // Once reset, all calls to PutWindowInSession and PutTabInWindow will denote |
| 94 // that the requested windows and tabs are owned (by setting the boolean | 90 // that the requested windows and tabs are owned (by setting the boolean |
| (...skipping 112 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 207 SessionWindowWrapper() : window_ptr(NULL), owned(false) {} | 203 SessionWindowWrapper() : window_ptr(NULL), owned(false) {} |
| 208 SessionWindowWrapper(sessions::SessionWindow* window_ptr, OwnedState owned) | 204 SessionWindowWrapper(sessions::SessionWindow* window_ptr, OwnedState owned) |
| 209 : window_ptr(window_ptr), owned(owned == IS_OWNED) {} | 205 : window_ptr(window_ptr), owned(owned == IS_OWNED) {} |
| 210 sessions::SessionWindow* window_ptr; | 206 sessions::SessionWindow* window_ptr; |
| 211 bool owned; | 207 bool owned; |
| 212 }; | 208 }; |
| 213 typedef std::map<SessionID::id_type, SessionWindowWrapper> | 209 typedef std::map<SessionID::id_type, SessionWindowWrapper> |
| 214 IDToSessionWindowMap; | 210 IDToSessionWindowMap; |
| 215 typedef std::map<std::string, IDToSessionWindowMap> SyncedWindowMap; | 211 typedef std::map<std::string, IDToSessionWindowMap> SyncedWindowMap; |
| 216 | 212 |
| 217 typedef std::map<std::string, sync_driver::SyncedSession*> SyncedSessionMap; | 213 typedef std::map<std::string, SyncedSession*> SyncedSessionMap; |
| 218 | 214 |
| 219 // Helper methods for deleting SessionWindows and SessionTabs without owners. | 215 // Helper methods for deleting SessionWindows and SessionTabs without owners. |
| 220 bool DeleteOldSessionWindowIfNecessary( | 216 bool DeleteOldSessionWindowIfNecessary( |
| 221 const SessionWindowWrapper& window_wrapper); | 217 const SessionWindowWrapper& window_wrapper); |
| 222 bool DeleteOldSessionTabIfNecessary(const SessionTabWrapper& tab_wrapper); | 218 bool DeleteOldSessionTabIfNecessary(const SessionTabWrapper& tab_wrapper); |
| 223 | 219 |
| 224 // Implementation for GetTab(...) above, permits invalid tab_node_id. | 220 // Implementation for GetTab(...) above, permits invalid tab_node_id. |
| 225 sessions::SessionTab* GetTabImpl(const std::string& session_tag, | 221 sessions::SessionTab* GetTabImpl(const std::string& session_tag, |
| 226 SessionID::id_type tab_id, | 222 SessionID::id_type tab_id, |
| 227 int tab_node_id); | 223 int tab_node_id); |
| 228 | 224 |
| 229 // The client of the sync sessions datatype. | 225 // The client of the sync sessions datatype. |
| 230 sync_sessions::SyncSessionsClient* const sessions_client_; | 226 SyncSessionsClient* const sessions_client_; |
| 231 | 227 |
| 232 // Per client mapping of tab id's to their SessionTab objects. | 228 // Per client mapping of tab id's to their SessionTab objects. |
| 233 // Key: session tag. | 229 // Key: session tag. |
| 234 // Value: Tab id to SessionTabWrapper map. | 230 // Value: Tab id to SessionTabWrapper map. |
| 235 SyncedTabMap synced_tab_map_; | 231 SyncedTabMap synced_tab_map_; |
| 236 | 232 |
| 237 // Per client mapping of the window id's to their SessionWindow objects. | 233 // Per client mapping of the window id's to their SessionWindow objects. |
| 238 // Key: session_tag | 234 // Key: session_tag |
| 239 // Value: Window id to SessionWindowWrapper map. | 235 // Value: Window id to SessionWindowWrapper map. |
| 240 SyncedWindowMap synced_window_map_; | 236 SyncedWindowMap synced_window_map_; |
| 241 | 237 |
| 242 // Per client mapping synced session objects. | 238 // Per client mapping synced session objects. |
| 243 // Key: session tag. | 239 // Key: session tag. |
| 244 // Value: SyncedSession object pointer. | 240 // Value: SyncedSession object pointer. |
| 245 SyncedSessionMap synced_session_map_; | 241 SyncedSessionMap synced_session_map_; |
| 246 | 242 |
| 247 // The set of tabs that we have seen, and created SessionTab objects for, but | 243 // The set of tabs that we have seen, and created SessionTab objects for, but |
| 248 // have not yet mapped to SyncedSessions. These are temporarily orphaned | 244 // have not yet mapped to SyncedSessions. These are temporarily orphaned |
| 249 // tabs, and won't be deleted if we delete synced_session_map_, but are still | 245 // tabs, and won't be deleted if we delete synced_session_map_, but are still |
| 250 // owned by the SyncedSessionTracker itself (and deleted on Clear()). | 246 // owned by the SyncedSessionTracker itself (and deleted on Clear()). |
| 251 std::set<sessions::SessionTab*> unmapped_tabs_; | 247 std::set<sessions::SessionTab*> unmapped_tabs_; |
| 252 | 248 |
| 253 // The tag for this machine's local session, so we can distinguish the foreign | 249 // The tag for this machine's local session, so we can distinguish the foreign |
| 254 // sessions. | 250 // sessions. |
| 255 std::string local_session_tag_; | 251 std::string local_session_tag_; |
| 256 | 252 |
| 257 DISALLOW_COPY_AND_ASSIGN(SyncedSessionTracker); | 253 DISALLOW_COPY_AND_ASSIGN(SyncedSessionTracker); |
| 258 }; | 254 }; |
| 259 | 255 |
| 260 } // namespace browser_sync | 256 } // namespace sync_sessions |
| 261 | 257 |
| 262 #endif // COMPONENTS_SYNC_SESSIONS_SYNCED_SESSION_TRACKER_H_ | 258 #endif // COMPONENTS_SYNC_SESSIONS_SYNCED_SESSION_TRACKER_H_ |
| OLD | NEW |