OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 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 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> |
11 #include <string> | 11 #include <string> |
12 #include <vector> | 12 #include <vector> |
13 | 13 |
14 #include "base/basictypes.h" | 14 #include "base/basictypes.h" |
15 #include "base/memory/scoped_vector.h" | 15 #include "base/memory/scoped_vector.h" |
16 #include "chrome/browser/sessions/session_id.h" | 16 #include "chrome/browser/sessions/session_id.h" |
17 #include "chrome/browser/sessions/session_types.h" | 17 #include "chrome/browser/sessions/session_types.h" |
18 #include "chrome/browser/sync/glue/synced_session.h" | 18 #include "chrome/browser/sync/glue/synced_session.h" |
19 | 19 |
20 namespace browser_sync { | 20 namespace browser_sync { |
21 | 21 |
22 // Class to manage synced sessions. The tracker will own all SyncedSession | 22 // Class to manage synced sessions. The tracker will own all SyncedSession |
23 // and SessionTab objects it creates, and deletes them appropriately on | 23 // and SyncedSessionTab objects it creates, and deletes them appropriately on |
24 // destruction. | 24 // destruction. |
25 // Note: SyncedSession objects are created for all synced sessions, including | 25 // Note: SyncedSession objects are created for all synced sessions, including |
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); |
(...skipping 16 matching lines...) Expand all Loading... |
50 std::vector<const SessionWindow*>* windows) const; | 50 std::vector<const SessionWindow*>* windows) const; |
51 | 51 |
52 // 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. | 53 // Ownership of the SessionTab remains within the SyncedSessionTracker. |
54 // If lookup succeeds: | 54 // If lookup succeeds: |
55 // - Sets tab to point to the SessionTab, and returns true. | 55 // - Sets tab to point to the SessionTab, and returns true. |
56 // Else | 56 // Else |
57 // - Returns false, tab is set to NULL. | 57 // - Returns false, tab is set to NULL. |
58 bool LookupSessionTab(const std::string& session_tag, | 58 bool LookupSessionTab(const std::string& session_tag, |
59 SessionID::id_type tab_id, | 59 SessionID::id_type tab_id, |
60 const SessionTab** tab) const; | 60 const SyncedSessionTab** tab) const; |
61 | 61 |
62 // Returns a pointer to the SyncedSession object associated with | 62 // Returns a pointer to the SyncedSession object associated with |
63 // |session_tag|. If none exists, creates one. Ownership of the | 63 // |session_tag|. If none exists, creates one. Ownership of the |
64 // SyncedSession remains within the SyncedSessionTracker. | 64 // SyncedSession remains within the SyncedSessionTracker. |
65 SyncedSession* GetSession(const std::string& session_tag); | 65 SyncedSession* GetSession(const std::string& session_tag); |
66 | 66 |
67 // Deletes the session associated with |session_tag| if it exists. | 67 // Deletes the session associated with |session_tag| if it exists. |
68 // Returns true if the session existed and was deleted, false otherwise. | 68 // Returns true if the session existed and was deleted, false otherwise. |
69 bool DeleteSession(const std::string& session_tag); | 69 bool DeleteSession(const std::string& session_tag); |
70 | 70 |
(...skipping 26 matching lines...) Expand all Loading... |
97 // Note: GetSession(..) must have already been called with |session_tag| to | 97 // Note: GetSession(..) must have already been called with |session_tag| to |
98 // ensure we having mapping information for this session. | 98 // ensure we having mapping information for this session. |
99 void PutTabInWindow(const std::string& session_tag, | 99 void PutTabInWindow(const std::string& session_tag, |
100 SessionID::id_type window_id, | 100 SessionID::id_type window_id, |
101 SessionID::id_type tab_id, | 101 SessionID::id_type tab_id, |
102 size_t tab_index); | 102 size_t tab_index); |
103 | 103 |
104 // 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 |
105 // the session specified with |session_tag|. If none exists, creates one. | 105 // the session specified with |session_tag|. If none exists, creates one. |
106 // Ownership of the SessionTab remains within the SyncedSessionTracker. | 106 // Ownership of the SessionTab remains within the SyncedSessionTracker. |
107 SessionTab* GetTab(const std::string& session_tag, | 107 SyncedSessionTab* GetTab(const std::string& session_tag, |
108 SessionID::id_type tab_id); | 108 SessionID::id_type tab_id); |
109 | 109 |
110 // Free the memory for all dynamically allocated objects and clear the | 110 // Free the memory for all dynamically allocated objects and clear the |
111 // tracking structures. | 111 // tracking structures. |
112 void Clear(); | 112 void Clear(); |
113 | 113 |
114 bool Empty() const { | 114 bool Empty() const { |
115 return synced_tab_map_.empty() && synced_session_map_.empty(); | 115 return synced_tab_map_.empty() && synced_session_map_.empty(); |
116 } | 116 } |
117 | 117 |
118 // Includes both foreign sessions and the local session. | 118 // Includes both foreign sessions and the local session. |
(...skipping 13 matching lines...) Expand all Loading... |
132 private: | 132 private: |
133 // Datatypes for accessing session data. Neither of the *Wrappers actually | 133 // Datatypes for accessing session data. Neither of the *Wrappers actually |
134 // have ownership of the Windows/Tabs, they just provide id-based access to | 134 // have ownership of the Windows/Tabs, they just provide id-based access to |
135 // them. The ownership remains within its containing session (for windows and | 135 // them. The ownership remains within its containing session (for windows and |
136 // mapped tabs, unmapped tabs are owned by the unmapped_tabs_ container). | 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 | 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(..) | 138 // what can be deleted (see ResetSessionTracking(..) and CleanupSession(..) |
139 // above). | 139 // above). |
140 struct SessionTabWrapper { | 140 struct SessionTabWrapper { |
141 SessionTabWrapper() : tab_ptr(NULL), owned(false) {} | 141 SessionTabWrapper() : tab_ptr(NULL), owned(false) {} |
142 SessionTabWrapper(SessionTab* tab_ptr, bool owned) | 142 SessionTabWrapper(SyncedSessionTab* tab_ptr, bool owned) |
143 : tab_ptr(tab_ptr), | 143 : tab_ptr(tab_ptr), |
144 owned(owned) {} | 144 owned(owned) {} |
145 SessionTab* tab_ptr; | 145 SyncedSessionTab* tab_ptr; |
146 bool owned; | 146 bool owned; |
147 }; | 147 }; |
148 typedef std::map<SessionID::id_type, SessionTabWrapper> IDToSessionTabMap; | 148 typedef std::map<SessionID::id_type, SessionTabWrapper> IDToSessionTabMap; |
149 typedef std::map<std::string, IDToSessionTabMap> SyncedTabMap; | 149 typedef std::map<std::string, IDToSessionTabMap> SyncedTabMap; |
150 | 150 |
151 struct SessionWindowWrapper { | 151 struct SessionWindowWrapper { |
152 SessionWindowWrapper() : window_ptr(NULL), owned(false) {} | 152 SessionWindowWrapper() : window_ptr(NULL), owned(false) {} |
153 SessionWindowWrapper(SessionWindow* window_ptr, bool owned) | 153 SessionWindowWrapper(SessionWindow* window_ptr, bool owned) |
154 : window_ptr(window_ptr), | 154 : window_ptr(window_ptr), |
155 owned(owned) {} | 155 owned(owned) {} |
(...skipping 22 matching lines...) Expand all Loading... |
178 | 178 |
179 // Per client mapping synced session objects. | 179 // Per client mapping synced session objects. |
180 // Key: session tag. | 180 // Key: session tag. |
181 // Value: SyncedSession object pointer. | 181 // Value: SyncedSession object pointer. |
182 SyncedSessionMap synced_session_map_; | 182 SyncedSessionMap synced_session_map_; |
183 | 183 |
184 // 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 |
185 // have not yet mapped to SyncedSessions. These are temporarily orphaned | 185 // have not yet mapped to SyncedSessions. These are temporarily orphaned |
186 // tabs, and won't be deleted if we delete synced_session_map_, but are still | 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()). | 187 // owned by the SyncedSessionTracker itself (and deleted on Clear()). |
188 std::set<SessionTab*> unmapped_tabs_; | 188 std::set<SyncedSessionTab*> unmapped_tabs_; |
189 | 189 |
190 // 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 |
191 // sessions. | 191 // sessions. |
192 std::string local_session_tag_; | 192 std::string local_session_tag_; |
193 | 193 |
194 DISALLOW_COPY_AND_ASSIGN(SyncedSessionTracker); | 194 DISALLOW_COPY_AND_ASSIGN(SyncedSessionTracker); |
195 }; | 195 }; |
196 | 196 |
197 } // namespace browser_sync | 197 } // namespace browser_sync |
198 | 198 |
199 #endif // CHROME_BROWSER_SYNC_GLUE_SYNCED_SESSION_TRACKER_H_ | 199 #endif // CHROME_BROWSER_SYNC_GLUE_SYNCED_SESSION_TRACKER_H_ |
OLD | NEW |