OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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_SYNCED_SESSION_TRACKER_H_ | 5 #ifndef CHROME_BROWSER_SYNC_GLUE_SYNCED_SESSION_TRACKER_H_ |
6 #define CHROME_BROWSER_SYNC_GLUE_SYNCED_SESSION_TRACKER_H_ | 6 #define CHROME_BROWSER_SYNC_GLUE_SYNCED_SESSION_TRACKER_H_ |
7 #pragma once | 7 #pragma once |
8 | 8 |
9 #include <map> | 9 #include <map> |
10 #include <set> | 10 #include <set> |
(...skipping 15 matching lines...) Expand all Loading... |
26 // the local session (whose tag we maintain separately). | 26 // the local session (whose tag we maintain separately). |
27 class SyncedSessionTracker { | 27 class SyncedSessionTracker { |
28 public: | 28 public: |
29 SyncedSessionTracker(); | 29 SyncedSessionTracker(); |
30 ~SyncedSessionTracker(); | 30 ~SyncedSessionTracker(); |
31 | 31 |
32 // We track and distinguish the local session from foreign sessions. | 32 // We track and distinguish the local session from foreign sessions. |
33 void SetLocalSessionTag(const std::string& local_session_tag); | 33 void SetLocalSessionTag(const std::string& local_session_tag); |
34 | 34 |
35 // Fill a preallocated vector with all foreign sessions we're tracking (skips | 35 // Fill a preallocated vector with all foreign sessions we're tracking (skips |
36 // the local session object). | 36 // the local session object). SyncedSession ownership remains within the |
| 37 // SyncedSessionTracker. |
37 // Returns true if we had foreign sessions to fill it with, false otherwise. | 38 // Returns true if we had foreign sessions to fill it with, false otherwise. |
38 bool LookupAllForeignSessions(std::vector<const SyncedSession*>* sessions); | 39 bool LookupAllForeignSessions(std::vector<const SyncedSession*>* sessions) |
| 40 const; |
39 | 41 |
40 // Attempts to look up the session windows associatd with the session given | 42 // Attempts to look up the session windows associatd with the session given |
41 // by |session_tag|. | 43 // by |session_tag|. Ownership Of SessionWindows stays within the |
| 44 // SyncedSessionTracker. |
42 // If lookup succeeds: | 45 // If lookup succeeds: |
43 // - Fills windows with the SessionWindow pointers, returns true. | 46 // - Fills windows with the SessionWindow pointers, returns true. |
44 // Else | 47 // Else |
45 // - Returns false. | 48 // - Returns false. |
46 bool LookupSessionWindows(const std::string& session_tag, | 49 bool LookupSessionWindows(const std::string& session_tag, |
47 std::vector<SessionWindow*>* windows); | 50 std::vector<const SessionWindow*>* windows) const; |
48 | 51 |
49 // Attempts to look up the tab associated with the given tag and tab id. | 52 // Attempts to look up the tab associated with the given tag and tab id. |
| 53 // Ownership of the SessionTab remains within the SyncedSessionTracker. |
50 // If lookup succeeds: | 54 // If lookup succeeds: |
51 // - Sets tab to point to the SessionTab, and returns true. | 55 // - Sets tab to point to the SessionTab, and returns true. |
52 // Else | 56 // Else |
53 // - Returns false, tab is set to NULL. | 57 // - Returns false, tab is set to NULL. |
54 bool LookupSessionTab(const std::string& session_tag, | 58 bool LookupSessionTab(const std::string& session_tag, |
55 SessionID::id_type tab_id, | 59 SessionID::id_type tab_id, |
56 const SessionTab** tab); | 60 const SessionTab** tab) const; |
57 | 61 |
58 // Returns a pointer to the SyncedSession object associated with session_tag. | 62 // Returns a pointer to the SyncedSession object associated with |
59 // If none exists, creates one and returns its pointer. | 63 // |session_tag|. If none exists, creates one. Ownership of the |
| 64 // SyncedSession remains within the SyncedSessionTracker. |
60 SyncedSession* GetSession(const std::string& session_tag); | 65 SyncedSession* GetSession(const std::string& session_tag); |
61 | 66 |
62 // Deletes the session associated with |session_tag| if it exists. | 67 // Deletes the session associated with |session_tag| if it exists. |
63 // Returns true if the session existed and was deleted, false otherwise. | 68 // Returns true if the session existed and was deleted, false otherwise. |
64 bool DeleteSession(const std::string& session_tag); | 69 bool DeleteSession(const std::string& session_tag); |
65 | 70 |
| 71 // Resets the tracking information for the session specified by |session_tag|. |
| 72 // This involves clearing all the windows and tabs from the session, while |
| 73 // keeping pointers saved in the synced_window_map_ and synced_tab_map_. |
| 74 // Once reset, all calls to PutWindowInSession and PutTabInWindow will denote |
| 75 // that the requested windows and tabs are owned (by setting the boolean |
| 76 // in their SessionWindowWrapper/SessionTabWrapper to true) and add them back |
| 77 // to their session. The next call to CleanupSession(...) will delete those |
| 78 // windows and tabs not owned. |
| 79 void ResetSessionTracking(const std::string& session_tag); |
| 80 |
| 81 // Deletes those windows and tabs associated with |session_tag| that are no |
| 82 // longer owned. |
| 83 // See ResetSessionTracking(...). |
| 84 void CleanupSession(const std::string& session_tag); |
| 85 |
| 86 // Adds the window with id |window_id| to the session specified by |
| 87 // |session_tag|, and markes the window as being owned. If none existed for |
| 88 // that session, creates one. Similarly, if the session did not exist yet, |
| 89 // creates it. Ownership of the SessionWindow remains within the |
| 90 // SyncedSessionTracker. |
| 91 void PutWindowInSession(const std::string& session_tag, |
| 92 SessionID::id_type window_id); |
| 93 |
| 94 // Adds the tab with id |tab_id| to the window |window_id|, and marks it as |
| 95 // being owned. If none existed for that session, creates one. Ownership of |
| 96 // the SessionTab remains within the SyncedSessionTracker. |
| 97 // Note: GetSession(..) must have already been called with |session_tag| to |
| 98 // ensure we having mapping information for this session. |
| 99 void PutTabInWindow(const std::string& session_tag, |
| 100 SessionID::id_type window_id, |
| 101 SessionID::id_type tab_id, |
| 102 size_t tab_index); |
| 103 |
66 // Returns a pointer to the SessionTab object associated with |tab_id| for | 104 // Returns a pointer to the SessionTab object associated with |tab_id| for |
67 // the session specified with |session_tag|. If none exists, creates one and | 105 // the session specified with |session_tag|. If none exists, creates one. |
68 // returns its pointer. | 106 // Ownership of the SessionTab remains within the SyncedSessionTracker. |
69 // |has_window| determines if newly created tabs are added to the pool of | 107 SessionTab* GetTab(const std::string& session_tag, |
70 // orphaned tabs (those which can't be reached by traversing sessions). | 108 SessionID::id_type tab_id); |
71 SessionTab* GetSessionTab(const std::string& session_tag, | |
72 SessionID::id_type tab_id, | |
73 bool has_window); | |
74 | 109 |
75 // Free the memory for all dynamically allocated objects and clear the | 110 // Free the memory for all dynamically allocated objects and clear the |
76 // tracking structures. | 111 // tracking structures. |
77 void clear(); | 112 void Clear(); |
78 | 113 |
79 inline bool empty() { | 114 bool Empty() const { |
80 return synced_tab_map_.empty() && synced_session_map_.empty(); | 115 return synced_tab_map_.empty() && synced_session_map_.empty(); |
81 } | 116 } |
82 | 117 |
83 // Includes both foreign sessions and the local session. | 118 // Includes both foreign sessions and the local session. |
84 inline size_t num_synced_sessions() { | 119 size_t num_synced_sessions() const { |
85 return synced_session_map_.size(); | 120 return synced_session_map_.size(); |
86 } | 121 } |
87 | 122 |
88 // Returns the number of tabs associated with the specified session tag. | 123 // Returns the number of tabs associated with the specified session tag. |
89 inline size_t num_synced_tabs(const std::string& session_tag) { | 124 size_t num_synced_tabs(const std::string& session_tag) const { |
90 if (synced_tab_map_.find(session_tag) != synced_tab_map_.end()) { | 125 SyncedTabMap::const_iterator iter = synced_tab_map_.find(session_tag); |
91 return synced_tab_map_[session_tag]->size(); | 126 if (iter != synced_tab_map_.end()) { |
| 127 return iter->second.size(); |
92 } else { | 128 } else { |
93 return 0; | 129 return 0; |
94 } | 130 } |
95 } | 131 } |
96 private: | 132 private: |
97 // Datatypes for accessing session data. | 133 // Datatypes for accessing session data. Neither of the *Wrappers actually |
98 typedef std::map<SessionID::id_type, SessionTab*> IDToSessionTabMap; | 134 // have ownership of the Windows/Tabs, they just provide id-based access to |
99 typedef std::map<std::string, IDToSessionTabMap*> SyncedTabMap; | 135 // them. The ownership remains within it's containing session (for windows and |
| 136 // mapped tabs, unmapped tabs are owned by the unmapped_tabs_ container). |
| 137 // Note, we pair pointers with bools so that we can track what is owned and |
| 138 // what can be deleted (see ResetSessionTracking(..) and CleanupSession(..) |
| 139 // above). |
| 140 struct SessionTabWrapper { |
| 141 SessionTabWrapper() : tab_ptr(NULL), owned(false) {} |
| 142 SessionTabWrapper(SessionTab* tab_ptr, bool owned) |
| 143 : tab_ptr(tab_ptr), |
| 144 owned(owned) {} |
| 145 SessionTab* tab_ptr; |
| 146 bool owned; |
| 147 }; |
| 148 typedef std::map<SessionID::id_type, SessionTabWrapper> IDToSessionTabMap; |
| 149 typedef std::map<std::string, IDToSessionTabMap> SyncedTabMap; |
| 150 |
| 151 struct SessionWindowWrapper { |
| 152 SessionWindowWrapper() : window_ptr(NULL), owned(false) {} |
| 153 SessionWindowWrapper(SessionWindow* window_ptr, bool owned) |
| 154 : window_ptr(window_ptr), |
| 155 owned(owned) {} |
| 156 SessionWindow* window_ptr; |
| 157 bool owned; |
| 158 }; |
| 159 typedef std::map<SessionID::id_type, SessionWindowWrapper> |
| 160 IDToSessionWindowMap; |
| 161 typedef std::map<std::string, IDToSessionWindowMap> SyncedWindowMap; |
| 162 |
100 typedef std::map<std::string, SyncedSession*> SyncedSessionMap; | 163 typedef std::map<std::string, SyncedSession*> SyncedSessionMap; |
101 | 164 |
| 165 // Helper methods for deleting SessionWindows and SessionTabs without owners. |
| 166 bool DeleteOldSessionWindowIfNecessary(SessionWindowWrapper window_wrapper); |
| 167 bool DeleteOldSessionTabIfNecessary(SessionTabWrapper tab_wrapper); |
| 168 |
102 // Per client mapping of tab id's to their SessionTab objects. | 169 // Per client mapping of tab id's to their SessionTab objects. |
103 // Key: session tag. | 170 // Key: session tag. |
104 // Value: Tab id to SessionTab map pointer. | 171 // Value: Tab id to SessionTabWrapper map. |
105 SyncedTabMap synced_tab_map_; | 172 SyncedTabMap synced_tab_map_; |
106 | 173 |
| 174 // Per client mapping of the window id's to their SessionWindow objects. |
| 175 // Key: session_tag |
| 176 // Value: Window id to SessionWindowWrapper map. |
| 177 SyncedWindowMap synced_window_map_; |
| 178 |
107 // Per client mapping synced session objects. | 179 // Per client mapping synced session objects. |
108 // Key: session tag. | 180 // Key: session tag. |
109 // Value: SyncedSession object pointer. | 181 // Value: SyncedSession object pointer. |
110 SyncedSessionMap synced_session_map_; | 182 SyncedSessionMap synced_session_map_; |
111 | 183 |
112 // The set of tabs that we have seen, and created SessionTab objects for, but | 184 // The set of tabs that we have seen, and created SessionTab objects for, but |
113 // have not yet mapped to SyncedSessions. These are temporarily orphaned | 185 // have not yet mapped to SyncedSessions. These are temporarily orphaned |
114 // tabs, and won't be deleted if we delete synced_session_map_. | 186 // tabs, and won't be deleted if we delete synced_session_map_, but are still |
| 187 // owned by the SyncedSessionTracker itself (and deleted on Clear()). |
115 std::set<SessionTab*> unmapped_tabs_; | 188 std::set<SessionTab*> unmapped_tabs_; |
116 | 189 |
117 // The tag for this machine's local session, so we can distinguish the foreign | 190 // The tag for this machine's local session, so we can distinguish the foreign |
118 // sessions. | 191 // sessions. |
119 std::string local_session_tag_; | 192 std::string local_session_tag_; |
120 | 193 |
121 DISALLOW_COPY_AND_ASSIGN(SyncedSessionTracker); | 194 DISALLOW_COPY_AND_ASSIGN(SyncedSessionTracker); |
122 }; | 195 }; |
123 | 196 |
124 } // namespace browser_sync | 197 } // namespace browser_sync |
125 | 198 |
126 #endif // CHROME_BROWSER_SYNC_GLUE_SYNCED_SESSION_TRACKER_H_ | 199 #endif // CHROME_BROWSER_SYNC_GLUE_SYNCED_SESSION_TRACKER_H_ |
OLD | NEW |