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> |
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 |
(...skipping 26 matching lines...) Expand all Loading... |
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. |
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); |
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. |
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, |
| 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, |
| 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, |
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(); |
78 | 100 |
79 inline bool empty() { | 101 inline bool empty() { |
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() { |
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) { |
90 if (synced_tab_map_.find(session_tag) != synced_tab_map_.end()) { | 112 if (synced_tab_map_.find(session_tag) != synced_tab_map_.end()) { |
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 |
| 121 // what can be deleted (see ResetSessionTracking(..) and CleanupSession(..) |
| 122 // above). |
| 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; |
99 typedef std::map<std::string, IDToSessionTabMap*> SyncedTabMap; | 132 typedef std::map<std::string, IDToSessionTabMap*> SyncedTabMap; |
| 133 |
| 134 struct SessionWindowWrapper { |
| 135 SessionWindowWrapper() : window_ptr(NULL), used(false) {} |
| 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 SessionTab map pointer. |
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 SessionWindow map pointer. |
| 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_. |
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 |