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) |
akalin
2011/10/01 01:54:14
There's no need to differentiate between this retu
akalin
2011/10/01 01:57:15
Ah, this is used in the foreign_session_handler, o
| |
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, |
akalin
2011/10/01 01:54:14
Ditto
| |
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's remains within the SyncedSessionTracker. | |
akalin
2011/10/01 01:54:14
no "'s" since there's only one
Nicolas Zea
2011/10/03 18:36:50
Done.
| |
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, |
akalin
2011/10/01 01:54:14
what about returning the tab directly, and NULL if
Nicolas Zea
2011/10/03 18:36:50
I kind of prefer being consistent with the other "
| |
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); |
akalin
2011/10/01 01:54:14
WIth my comments in the model associator file, I t
| |
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 | |
akalin
2011/10/01 01:54:14
Specify that the session is created as needed also
Nicolas Zea
2011/10/03 18:36:50
Done.
| |
88 // that session, creates one. Ownership of the SessionWindow remains within | |
89 // the SyncedSessionTracker. | |
90 void PutWindowInSession(const std::string& session_tag, | |
91 SessionID::id_type window_id); | |
92 | |
93 // Adds the tab with id |tab_id| to the window |window_id|, and marks it as | |
94 // being owned. If none existed for that session, creates one. Ownership of | |
95 // the SessionTab remains within the SyncedSessionTracker. | |
96 void PutTabInWindow(const std::string& session_tag, | |
akalin
2011/10/01 01:54:14
Would it make sense for PutTabInWindow to auto-cre
Nicolas Zea
2011/10/03 18:36:50
The window/session should be created before this i
| |
97 SessionID::id_type window_id, | |
98 SessionID::id_type tab_id, | |
99 size_t tab_index); | |
100 | |
66 // Returns a pointer to the SessionTab object associated with |tab_id| for | 101 // 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 | 102 // the session specified with |session_tag|. If none exists, creates one. |
68 // returns its pointer. | 103 // Ownership of the SessionTab remains within the SyncedSessionTracker. |
69 // |has_window| determines if newly created tabs are added to the pool of | 104 SessionTab* GetTab(const std::string& session_tag, |
70 // orphaned tabs (those which can't be reached by traversing sessions). | 105 SessionID::id_type tab_id); |
71 SessionTab* GetSessionTab(const std::string& session_tag, | |
72 SessionID::id_type tab_id, | |
73 bool has_window); | |
74 | 106 |
75 // Free the memory for all dynamically allocated objects and clear the | 107 // Free the memory for all dynamically allocated objects and clear the |
76 // tracking structures. | 108 // tracking structures. |
77 void clear(); | 109 void Clear(); |
78 | 110 |
79 inline bool empty() { | 111 bool Empty() const { |
80 return synced_tab_map_.empty() && synced_session_map_.empty(); | 112 return synced_tab_map_.empty() && synced_session_map_.empty(); |
81 } | 113 } |
82 | 114 |
83 // Includes both foreign sessions and the local session. | 115 // Includes both foreign sessions and the local session. |
84 inline size_t num_synced_sessions() { | 116 size_t num_synced_sessions() const { |
85 return synced_session_map_.size(); | 117 return synced_session_map_.size(); |
86 } | 118 } |
87 | 119 |
88 // Returns the number of tabs associated with the specified session tag. | 120 // Returns the number of tabs associated with the specified session tag. |
89 inline size_t num_synced_tabs(const std::string& session_tag) { | 121 size_t num_synced_tabs(const std::string& session_tag) const { |
90 if (synced_tab_map_.find(session_tag) != synced_tab_map_.end()) { | 122 SyncedTabMap::const_iterator iter = synced_tab_map_.find(session_tag); |
91 return synced_tab_map_[session_tag]->size(); | 123 if (iter != synced_tab_map_.end()) { |
124 return iter->second.size(); | |
92 } else { | 125 } else { |
93 return 0; | 126 return 0; |
94 } | 127 } |
95 } | 128 } |
96 private: | 129 private: |
97 // Datatypes for accessing session data. | 130 // Datatypes for accessing session data. Neither of the *Wrappers actually |
98 typedef std::map<SessionID::id_type, SessionTab*> IDToSessionTabMap; | 131 // have ownership of the Windows/Tabs, they just provide id-based access to |
99 typedef std::map<std::string, IDToSessionTabMap*> SyncedTabMap; | 132 // them. The ownership remains within it's containing session (for windows and |
133 // mapped tabs, unmapped tabs are owned by the unmapped_tabs_ container). | |
134 // Note, we pair pointers with bools so that we can track what is owned and | |
135 // what can be deleted (see ResetSessionTracking(..) and CleanupSession(..) | |
136 // above). | |
137 struct SessionTabWrapper { | |
138 SessionTabWrapper() : tab_ptr(NULL), owned(false) {} | |
139 SessionTabWrapper(SessionTab* tab_ptr, bool owned) | |
140 : tab_ptr(tab_ptr), | |
141 owned(owned) {} | |
142 SessionTab* tab_ptr; | |
143 bool owned; | |
144 }; | |
145 typedef std::map<SessionID::id_type, SessionTabWrapper> IDToSessionTabMap; | |
146 typedef std::map<std::string, IDToSessionTabMap> SyncedTabMap; | |
147 | |
148 struct SessionWindowWrapper { | |
149 SessionWindowWrapper() : window_ptr(NULL), owned(false) {} | |
150 SessionWindowWrapper(SessionWindow* window_ptr, bool owned) | |
151 : window_ptr(window_ptr), | |
152 owned(owned) {} | |
153 SessionWindow* window_ptr; | |
154 bool owned; | |
155 }; | |
156 typedef std::map<SessionID::id_type, SessionWindowWrapper> | |
157 IDToSessionWindowMap; | |
158 typedef std::map<std::string, IDToSessionWindowMap> SyncedWindowMap; | |
159 | |
100 typedef std::map<std::string, SyncedSession*> SyncedSessionMap; | 160 typedef std::map<std::string, SyncedSession*> SyncedSessionMap; |
101 | 161 |
162 // Helper methods for deleting SessionWindows and SessionTabs without owners. | |
163 bool DeleteOldSessionWindowIfNecessary(SessionWindowWrapper window_wrapper); | |
164 bool DeleteOldSessionTabIfNecessary(SessionTabWrapper tab_wrapper); | |
165 | |
102 // Per client mapping of tab id's to their SessionTab objects. | 166 // Per client mapping of tab id's to their SessionTab objects. |
103 // Key: session tag. | 167 // Key: session tag. |
104 // Value: Tab id to SessionTab map pointer. | 168 // Value: Tab id to SessionTabWrapper map. |
105 SyncedTabMap synced_tab_map_; | 169 SyncedTabMap synced_tab_map_; |
106 | 170 |
171 // Per client mapping of the window id's to their SessionWindow objects. | |
172 // Key: session_tag | |
173 // Value: Window id to SessionWindowWrapper map. | |
174 SyncedWindowMap synced_window_map_; | |
175 | |
107 // Per client mapping synced session objects. | 176 // Per client mapping synced session objects. |
108 // Key: session tag. | 177 // Key: session tag. |
109 // Value: SyncedSession object pointer. | 178 // Value: SyncedSession object pointer. |
110 SyncedSessionMap synced_session_map_; | 179 SyncedSessionMap synced_session_map_; |
111 | 180 |
112 // The set of tabs that we have seen, and created SessionTab objects for, but | 181 // 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 | 182 // have not yet mapped to SyncedSessions. These are temporarily orphaned |
114 // tabs, and won't be deleted if we delete synced_session_map_. | 183 // tabs, and won't be deleted if we delete synced_session_map_, but are still |
184 // owned by the SyncedSessionTracker itself (and deleted on Clear()). | |
115 std::set<SessionTab*> unmapped_tabs_; | 185 std::set<SessionTab*> unmapped_tabs_; |
116 | 186 |
117 // The tag for this machine's local session, so we can distinguish the foreign | 187 // The tag for this machine's local session, so we can distinguish the foreign |
118 // sessions. | 188 // sessions. |
119 std::string local_session_tag_; | 189 std::string local_session_tag_; |
120 | 190 |
121 DISALLOW_COPY_AND_ASSIGN(SyncedSessionTracker); | 191 DISALLOW_COPY_AND_ASSIGN(SyncedSessionTracker); |
122 }; | 192 }; |
123 | 193 |
124 } // namespace browser_sync | 194 } // namespace browser_sync |
125 | 195 |
126 #endif // CHROME_BROWSER_SYNC_GLUE_SYNCED_SESSION_TRACKER_H_ | 196 #endif // CHROME_BROWSER_SYNC_GLUE_SYNCED_SESSION_TRACKER_H_ |
OLD | NEW |