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> |
11 #include <string> | 11 #include <string> |
12 #include <utility> | |
akalin
2011/09/29 21:59:55
no need for this anymore
Nicolas Zea
2011/09/30 22:52:59
Done.
| |
12 #include <vector> | 13 #include <vector> |
13 | 14 |
14 #include "base/basictypes.h" | 15 #include "base/basictypes.h" |
15 #include "base/memory/scoped_vector.h" | 16 #include "base/memory/scoped_vector.h" |
16 #include "chrome/browser/sessions/session_id.h" | 17 #include "chrome/browser/sessions/session_id.h" |
17 #include "chrome/browser/sessions/session_types.h" | 18 #include "chrome/browser/sessions/session_types.h" |
18 #include "chrome/browser/sync/glue/synced_session.h" | 19 #include "chrome/browser/sync/glue/synced_session.h" |
19 | 20 |
20 namespace browser_sync { | 21 namespace browser_sync { |
21 | 22 |
22 // Class to manage synced sessions. The tracker will own all SyncedSession | 23 // Class to manage synced sessions. The tracker will own all SyncedSession |
23 // and SessionTab objects it creates, and deletes them appropriately on | 24 // and SessionTab objects it creates, and deletes them appropriately on |
24 // destruction. | 25 // destruction. |
25 // Note: SyncedSession objects are created for all synced sessions, including | 26 // Note: SyncedSession objects are created for all synced sessions, including |
26 // the local session (whose tag we maintain separately). | 27 // the local session (whose tag we maintain separately). |
27 class SyncedSessionTracker { | 28 class SyncedSessionTracker { |
28 public: | 29 public: |
29 SyncedSessionTracker(); | 30 SyncedSessionTracker(); |
30 ~SyncedSessionTracker(); | 31 ~SyncedSessionTracker(); |
31 | 32 |
32 // We track and distinguish the local session from foreign sessions. | 33 // We track and distinguish the local session from foreign sessions. |
33 void SetLocalSessionTag(const std::string& local_session_tag); | 34 void SetLocalSessionTag(const std::string& local_session_tag); |
34 | 35 |
35 // Fill a preallocated vector with all foreign sessions we're tracking (skips | 36 // Fill a preallocated vector with all foreign sessions we're tracking (skips |
36 // the local session object). | 37 // the local session object). |
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. |
akalin
2011/09/29 21:59:55
Comment that ownership of the SyncedSessions stays
Nicolas Zea
2011/09/30 22:52:59
Done.
| |
38 bool LookupAllForeignSessions(std::vector<const SyncedSession*>* sessions); | 39 bool LookupAllForeignSessions(std::vector<const SyncedSession*>* sessions); |
akalin
2011/09/29 21:59:55
make const?
Nicolas Zea
2011/09/30 22:52:59
Done.
| |
39 | 40 |
40 // Attempts to look up the session windows associatd with the session given | 41 // Attempts to look up the session windows associatd with the session given |
41 // by |session_tag|. | 42 // by |session_tag|. |
42 // If lookup succeeds: | 43 // If lookup succeeds: |
43 // - Fills windows with the SessionWindow pointers, returns true. | 44 // - Fills windows with the SessionWindow pointers, returns true. |
44 // Else | 45 // Else |
45 // - Returns false. | 46 // - Returns false. |
46 bool LookupSessionWindows(const std::string& session_tag, | 47 bool LookupSessionWindows(const std::string& session_tag, |
akalin
2011/09/29 21:59:55
Comment that ownership of the SessionWindows stays
akalin
2011/09/29 21:59:55
make const?
also, do the returned SessionWindow p
Nicolas Zea
2011/09/30 22:52:59
Done.
Nicolas Zea
2011/09/30 22:52:59
Done.
| |
47 std::vector<SessionWindow*>* windows); | 48 std::vector<SessionWindow*>* windows); |
48 | 49 |
49 // Attempts to look up the tab associated with the given tag and tab id. | 50 // Attempts to look up the tab associated with the given tag and tab id. |
50 // If lookup succeeds: | 51 // If lookup succeeds: |
51 // - Sets tab to point to the SessionTab, and returns true. | 52 // - Sets tab to point to the SessionTab, and returns true. |
akalin
2011/09/29 21:59:55
Comment that ownership of the SessionTab stays wit
Nicolas Zea
2011/09/30 22:52:59
Done.
| |
52 // Else | 53 // Else |
53 // - Returns false, tab is set to NULL. | 54 // - Returns false, tab is set to NULL. |
54 bool LookupSessionTab(const std::string& session_tag, | 55 bool LookupSessionTab(const std::string& session_tag, |
55 SessionID::id_type tab_id, | 56 SessionID::id_type tab_id, |
56 const SessionTab** tab); | 57 const SessionTab** tab); |
akalin
2011/09/29 21:59:55
make const?
Nicolas Zea
2011/09/30 22:52:59
Done.
| |
57 | 58 |
58 // Returns a pointer to the SyncedSession object associated with session_tag. | 59 // Returns a pointer to the SyncedSession object associated with |
59 // If none exists, creates one and returns its pointer. | 60 // |session_tag|. If none exists, creates one. |
akalin
2011/09/29 21:59:55
Given the changes below, is this function needed a
Nicolas Zea
2011/09/30 22:52:59
We still need to get the session itself to set som
| |
60 SyncedSession* GetSession(const std::string& session_tag); | 61 SyncedSession* GetSession(const std::string& session_tag); |
61 | 62 |
62 // Deletes the session associated with |session_tag| if it exists. | 63 // Deletes the session associated with |session_tag| if it exists. |
63 // Returns true if the session existed and was deleted, false otherwise. | 64 // Returns true if the session existed and was deleted, false otherwise. |
64 bool DeleteSession(const std::string& session_tag); | 65 bool DeleteSession(const std::string& session_tag); |
65 | 66 |
67 // Resets the tracking information for the session specified by |session_tag|. | |
68 // Once reset, all calls to GetWindow and GetTabForWindow will denote | |
69 // that the requested windows and tabs are in use (by setting the boolean | |
70 // in their SessionWindowWrapper/SessionTabWrapper to true). The next call to | |
71 // CleanupSession(...) will delete those windows and tabs not in use. | |
72 void ResetSessionTracking(const std::string& session_tag); | |
73 | |
74 // Deletes those windows and tabs associated with |session_tag| that are no | |
75 // longer in use from memory (and removes unused windows from their | |
76 // SyncedSession object). | |
77 // See ResetSessionTracking(...). | |
78 void CleanupSession(const std::string& session_tag); | |
79 | |
80 // Returns a pointer to the SessionWindow object associated with |window_id| | |
81 // for the session specified with |session_tag|. If none exists, creates one. | |
82 SessionWindow* GetWindow(const std::string& session_tag, | |
akalin
2011/09/29 21:59:55
Change this to something like:
PutWindowInSession
Nicolas Zea
2011/09/30 22:52:59
Done.
| |
83 SessionID::id_type window_id); | |
84 | |
85 // Same as GetTab, but internally notes that window |window_id| is now | |
86 // holding tab |tab_id|. As a result, when CleanupSession(...) is called, | |
87 // the tab will not be destroyed. | |
88 SessionTab* GetTabForWindow(const std::string& session_tag, | |
akalin
2011/09/29 21:59:55
Similarly, change this to something like:
PutTabI
Nicolas Zea
2011/09/30 22:52:59
Done.
| |
89 SessionID::id_type window_id, | |
90 SessionID::id_type tab_id); | |
91 | |
66 // Returns a pointer to the SessionTab object associated with |tab_id| for | 92 // 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 | 93 // the session specified with |session_tag|. If none exists, creates one. |
68 // returns its pointer. | 94 SessionTab* GetTab(const std::string& session_tag, |
akalin
2011/09/29 21:59:55
Comment that ownership of the SessionTab stays wit
Nicolas Zea
2011/09/30 22:52:59
Done.
| |
69 // |has_window| determines if newly created tabs are added to the pool of | 95 SessionID::id_type tab_id); |
70 // orphaned tabs (those which can't be reached by traversing sessions). | |
71 SessionTab* GetSessionTab(const std::string& session_tag, | |
72 SessionID::id_type tab_id, | |
73 bool has_window); | |
74 | 96 |
75 // Free the memory for all dynamically allocated objects and clear the | 97 // Free the memory for all dynamically allocated objects and clear the |
76 // tracking structures. | 98 // tracking structures. |
77 void clear(); | 99 void clear(); |
akalin
2011/09/29 21:59:55
nit: Clear() is Google style
Nicolas Zea
2011/09/30 22:52:59
Done.
| |
78 | 100 |
79 inline bool empty() { | 101 inline bool empty() { |
akalin
2011/09/29 21:59:55
nit: -inline, make const (if it's in the class def
Nicolas Zea
2011/09/30 22:52:59
Done.
| |
80 return synced_tab_map_.empty() && synced_session_map_.empty(); | 102 return synced_tab_map_.empty() && synced_session_map_.empty(); |
81 } | 103 } |
82 | 104 |
83 // Includes both foreign sessions and the local session. | 105 // Includes both foreign sessions and the local session. |
84 inline size_t num_synced_sessions() { | 106 inline size_t num_synced_sessions() { |
akalin
2011/09/29 21:59:55
make const, -inline
Nicolas Zea
2011/09/30 22:52:59
Done.
| |
85 return synced_session_map_.size(); | 107 return synced_session_map_.size(); |
86 } | 108 } |
87 | 109 |
88 // Returns the number of tabs associated with the specified session tag. | 110 // Returns the number of tabs associated with the specified session tag. |
89 inline size_t num_synced_tabs(const std::string& session_tag) { | 111 inline size_t num_synced_tabs(const std::string& session_tag) { |
akalin
2011/09/29 21:59:55
make const
Nicolas Zea
2011/09/30 22:52:59
Done.
| |
90 if (synced_tab_map_.find(session_tag) != synced_tab_map_.end()) { | 112 if (synced_tab_map_.find(session_tag) != synced_tab_map_.end()) { |
akalin
2011/09/29 21:59:55
probably need to store the iterator in a var and d
Nicolas Zea
2011/09/30 22:52:59
Done.
| |
91 return synced_tab_map_[session_tag]->size(); | 113 return synced_tab_map_[session_tag].size(); |
92 } else { | 114 } else { |
93 return 0; | 115 return 0; |
94 } | 116 } |
95 } | 117 } |
96 private: | 118 private: |
97 // Datatypes for accessing session data. | 119 // Datatypes for accessing session data. |
98 typedef std::map<SessionID::id_type, SessionTab*> IDToSessionTabMap; | 120 // Note, we pair pointers with bools so that we can track what is in use and |
99 typedef std::map<std::string, IDToSessionTabMap*> SyncedTabMap; | 121 // what can be deleted (see ResetSessionTracking(..) and CleanupSession(..) |
122 // above). | |
akalin
2011/09/29 21:59:55
comment that SessionTabWrapper doesn't take owners
Nicolas Zea
2011/09/30 22:52:59
Done.
| |
123 struct SessionTabWrapper { | |
124 SessionTabWrapper() : tab_ptr(NULL), used(false) {} | |
125 SessionTabWrapper(SessionTab* tab_ptr, bool used) | |
126 : tab_ptr(tab_ptr), | |
127 used(used) {} | |
128 SessionTab* tab_ptr; | |
129 bool used; | |
130 }; | |
131 typedef std::map<SessionID::id_type, SessionTabWrapper> IDToSessionTabMap; | |
132 typedef std::map<std::string, IDToSessionTabMap> SyncedTabMap; | |
133 | |
134 struct SessionWindowWrapper { | |
akalin
2011/09/29 21:59:55
comment that SessionWindowWrapper doesn't take own
Nicolas Zea
2011/09/30 22:52:59
Done.
| |
135 SessionWindowWrapper() : window_ptr(NULL), used(false) {} | |
akalin
2011/09/29 21:59:55
used -> owner?
Nicolas Zea
2011/09/30 22:52:59
Done.
| |
136 SessionWindowWrapper(SessionWindow* window_ptr, bool used) | |
137 : window_ptr(window_ptr), | |
138 used(used) {} | |
139 SessionWindow* window_ptr; | |
140 bool used; | |
141 }; | |
142 typedef std::map<SessionID::id_type, SessionWindowWrapper> | |
143 IDToSessionWindowMap; | |
144 typedef std::map<std::string, IDToSessionWindowMap> SyncedWindowMap; | |
145 | |
100 typedef std::map<std::string, SyncedSession*> SyncedSessionMap; | 146 typedef std::map<std::string, SyncedSession*> SyncedSessionMap; |
101 | 147 |
148 // Helper methods for deleting SessionWindows and SessionTabs. | |
149 bool ClearUnusedSessionWindow(SessionWindowWrapper window_wrapper, | |
150 SyncedSession* session); | |
151 bool ClearUnusedSessionTab(SessionTabWrapper tab_wrapper); | |
152 | |
102 // Per client mapping of tab id's to their SessionTab objects. | 153 // Per client mapping of tab id's to their SessionTab objects. |
103 // Key: session tag. | 154 // Key: session tag. |
104 // Value: Tab id to SessionTab map pointer. | 155 // Value: Tab id to SessionTabWrapper map. |
105 SyncedTabMap synced_tab_map_; | 156 SyncedTabMap synced_tab_map_; |
106 | 157 |
158 // Per client mapping of the window id's to their SessionWindow objects. | |
159 // Key: session_tag | |
160 // Value: Window id to SessionWindowWrapper map. | |
161 SyncedWindowMap synced_window_map_; | |
162 | |
107 // Per client mapping synced session objects. | 163 // Per client mapping synced session objects. |
108 // Key: session tag. | 164 // Key: session tag. |
109 // Value: SyncedSession object pointer. | 165 // Value: SyncedSession object pointer. |
110 SyncedSessionMap synced_session_map_; | 166 SyncedSessionMap synced_session_map_; |
111 | 167 |
112 // The set of tabs that we have seen, and created SessionTab objects for, but | 168 // 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 | 169 // have not yet mapped to SyncedSessions. These are temporarily orphaned |
114 // tabs, and won't be deleted if we delete synced_session_map_. | 170 // tabs, and won't be deleted if we delete synced_session_map_. |
akalin
2011/09/29 21:59:55
say that these are owned by the SST itself
Nicolas Zea
2011/09/30 22:52:59
Done.
| |
115 std::set<SessionTab*> unmapped_tabs_; | 171 std::set<SessionTab*> unmapped_tabs_; |
116 | 172 |
117 // The tag for this machine's local session, so we can distinguish the foreign | 173 // The tag for this machine's local session, so we can distinguish the foreign |
118 // sessions. | 174 // sessions. |
119 std::string local_session_tag_; | 175 std::string local_session_tag_; |
120 | 176 |
121 DISALLOW_COPY_AND_ASSIGN(SyncedSessionTracker); | 177 DISALLOW_COPY_AND_ASSIGN(SyncedSessionTracker); |
122 }; | 178 }; |
123 | 179 |
124 } // namespace browser_sync | 180 } // namespace browser_sync |
125 | 181 |
126 #endif // CHROME_BROWSER_SYNC_GLUE_SYNCED_SESSION_TRACKER_H_ | 182 #endif // CHROME_BROWSER_SYNC_GLUE_SYNCED_SESSION_TRACKER_H_ |
OLD | NEW |