1 // Copyright 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #ifndef CHROME_BROWSER_SYNC_GLUE_SYNCED_SESSION_TRACKER_H_
6 #define CHROME_BROWSER_SYNC_GLUE_SYNCED_SESSION_TRACKER_H_
13 #include "base/basictypes.h"
14 #include "base/memory/scoped_vector.h"
15 #include "chrome/browser/sync/sessions/tab_node_pool.h"
16 #include "components/sessions/session_id.h"
17 #include "components/sessions/session_types.h"
18 #include "components/sync_driver/glue/synced_session.h"
20 namespace browser_sync
{
22 // Class to manage synced sessions. The tracker will own all SyncedSession
23 // and SyncedSessionTab objects it creates, and deletes them appropriately on
25 // Note: SyncedSession objects are created for all synced sessions, including
26 // the local session (whose tag we maintain separately).
27 class SyncedSessionTracker
{
29 SyncedSessionTracker();
30 ~SyncedSessionTracker();
32 // We track and distinguish the local session from foreign sessions.
33 void SetLocalSessionTag(const std::string
& local_session_tag
);
35 // Fill a preallocated vector with all foreign sessions we're tracking (skips
36 // the local session object). SyncedSession ownership remains within the
37 // SyncedSessionTracker.
38 // Returns true if we had foreign sessions to fill it with, false otherwise.
39 bool LookupAllForeignSessions(
40 std::vector
<const sync_driver::SyncedSession
*>* sessions
) const;
42 // Attempts to look up the session windows associatd with the session given
43 // by |session_tag|. Ownership Of SessionWindows stays within the
44 // SyncedSessionTracker.
45 // If lookup succeeds:
46 // - Fills windows with the SessionWindow pointers, returns true.
49 bool LookupSessionWindows(
50 const std::string
& session_tag
,
51 std::vector
<const sessions::SessionWindow
*>* windows
) const;
53 // Attempts to look up the tab associated with the given tag and tab id.
54 // Ownership of the SessionTab remains within the SyncedSessionTracker.
55 // If lookup succeeds:
56 // - Sets tab to point to the SessionTab, and returns true.
58 // - Returns false, tab is set to NULL.
59 bool LookupSessionTab(const std::string
& session_tag
,
60 SessionID::id_type tab_id
,
61 const sessions::SessionTab
** tab
) const;
63 // Allows retrieval of existing data for the local session. Unlike GetSession
64 // this won't create-if-not-present.
65 bool LookupLocalSession(const sync_driver::SyncedSession
** output
) const;
67 // Returns a pointer to the SyncedSession object associated with
68 // |session_tag|. If none exists, creates one. Ownership of the
69 // SyncedSession remains within the SyncedSessionTracker.
70 sync_driver::SyncedSession
* GetSession(const std::string
& session_tag
);
72 // Deletes the session associated with |session_tag| if it exists.
73 // Returns true if the session existed and was deleted, false otherwise.
74 bool DeleteSession(const std::string
& session_tag
);
76 // Resets the tracking information for the session specified by |session_tag|.
77 // This involves clearing all the windows and tabs from the session, while
78 // keeping pointers saved in the synced_window_map_ and synced_tab_map_.
79 // Once reset, all calls to PutWindowInSession and PutTabInWindow will denote
80 // that the requested windows and tabs are owned (by setting the boolean
81 // in their SessionWindowWrapper/SessionTabWrapper to true) and add them back
82 // to their session. The next call to CleanupSession(...) will delete those
83 // windows and tabs not owned.
84 void ResetSessionTracking(const std::string
& session_tag
);
86 // Deletes those windows and tabs associated with |session_tag| that are no
88 // See ResetSessionTracking(...).
89 void CleanupSession(const std::string
& session_tag
);
91 // Adds the window with id |window_id| to the session specified by
92 // |session_tag|, and markes the window as being owned. If none existed for
93 // that session, creates one. Similarly, if the session did not exist yet,
94 // creates it. Ownership of the SessionWindow remains within the
95 // SyncedSessionTracker.
96 void PutWindowInSession(const std::string
& session_tag
,
97 SessionID::id_type window_id
);
99 // Adds the tab with id |tab_id| to the window |window_id|, and marks it as
100 // being owned. If none existed for that session, creates one. Ownership of
101 // the SessionTab remains within the SyncedSessionTracker.
102 // Note: GetSession(..) must have already been called with |session_tag| to
103 // ensure we having mapping information for this session.
104 void PutTabInWindow(const std::string
& session_tag
,
105 SessionID::id_type window_id
,
106 SessionID::id_type tab_id
,
109 // Returns a pointer to the SessionTab object associated with |tab_id| for
110 // the session specified with |session_tag|. If none exists, creates one.
111 // Ownership of the SessionTab remains within the SyncedSessionTracker.
112 // |tab_node_id| must be a valid node id for the node backing this tab.
113 sessions::SessionTab
* GetTab(const std::string
& session_tag
,
114 SessionID::id_type tab_id
,
117 // Fills |tab_node_ids| with the tab node ids (see GetTab) for all the tabs*
118 // associated with the session having tag |session_tag|.
119 // Returns false if we don't have any record of the session. If no tabs were
120 // found as part of the session, the return value will be true but
121 // |tab_node_ids| will be empty.
123 // * - note that this only returns the ids we're aware of; it's possible we
124 // don't have the latest tab state from a foreign session and it's also
125 // possible we just haven't updated the tab_node_id for a tab yet, so the
126 // result list should not be treated as authoritative.
127 bool LookupTabNodeIds(const std::string
& session_tag
,
128 std::set
<int>* tab_node_ids
);
130 // Free the memory for all dynamically allocated objects and clear the
131 // tracking structures.
135 return synced_tab_map_
.empty() && synced_session_map_
.empty();
138 // Includes both foreign sessions and the local session.
139 size_t num_synced_sessions() const {
140 return synced_session_map_
.size();
143 // Returns the number of tabs associated with the specified session tag.
144 size_t num_synced_tabs(const std::string
& session_tag
) const {
145 SyncedTabMap::const_iterator iter
= synced_tab_map_
.find(session_tag
);
146 if (iter
!= synced_tab_map_
.end()) {
147 return iter
->second
.size();
153 // Datatypes for accessing session data. Neither of the *Wrappers actually
154 // have ownership of the Windows/Tabs, they just provide id-based access to
155 // them. The ownership remains within its containing session (for windows and
156 // mapped tabs, unmapped tabs are owned by the unmapped_tabs_ container).
157 // Note, we pair pointers with bools so that we can track what is owned and
158 // what can be deleted (see ResetSessionTracking(..) and CleanupSession(..)
160 // The wrappers also serve as a convenient place to augment state stored in
161 // SessionTab for sync purposes, such as |tab_node_id|.
162 // IsOwned is used as a wrapper constructor parameter for readability.
167 struct SessionTabWrapper
{
168 SessionTabWrapper() : tab_ptr(NULL
),
170 tab_node_id(TabNodePool::kInvalidTabNodeID
) {}
171 SessionTabWrapper(sessions::SessionTab
* tab_ptr
,
175 owned(owned
== IS_OWNED
),
176 tab_node_id(tab_node_id
) {}
177 sessions::SessionTab
* tab_ptr
;
179 // This is used as part of a mark-and-sweep approach to garbage
180 // collection for closed tabs that are no longer "in use", or "owned".
181 // ResetSessionTracking will clear |owned| bits, and if it is not claimed
182 // by a window by the time CleanupSession is called it will be deleted.
185 // This lets us identify the sync node that is "backing" this tab in the
186 // sync model, whether it is part of a local or foreign session. The
187 // "tab node id" is described in session_specifics.proto.
190 typedef std::map
<SessionID::id_type
, SessionTabWrapper
> IDToSessionTabMap
;
191 typedef std::map
<std::string
, IDToSessionTabMap
> SyncedTabMap
;
193 struct SessionWindowWrapper
{
194 SessionWindowWrapper() : window_ptr(NULL
), owned(false) {}
195 SessionWindowWrapper(sessions::SessionWindow
* window_ptr
, OwnedState owned
)
196 : window_ptr(window_ptr
),
197 owned(owned
== IS_OWNED
) {}
198 sessions::SessionWindow
* window_ptr
;
201 typedef std::map
<SessionID::id_type
, SessionWindowWrapper
>
202 IDToSessionWindowMap
;
203 typedef std::map
<std::string
, IDToSessionWindowMap
> SyncedWindowMap
;
205 typedef std::map
<std::string
, sync_driver::SyncedSession
*> SyncedSessionMap
;
207 // Helper methods for deleting SessionWindows and SessionTabs without owners.
208 bool DeleteOldSessionWindowIfNecessary(SessionWindowWrapper window_wrapper
);
209 bool DeleteOldSessionTabIfNecessary(SessionTabWrapper tab_wrapper
);
211 // Implementation for GetTab(...) above, permits invalid tab_node_id.
212 sessions::SessionTab
* GetTabImpl(const std::string
& session_tag
,
213 SessionID::id_type tab_id
,
216 // Per client mapping of tab id's to their SessionTab objects.
218 // Value: Tab id to SessionTabWrapper map.
219 SyncedTabMap synced_tab_map_
;
221 // Per client mapping of the window id's to their SessionWindow objects.
223 // Value: Window id to SessionWindowWrapper map.
224 SyncedWindowMap synced_window_map_
;
226 // Per client mapping synced session objects.
228 // Value: SyncedSession object pointer.
229 SyncedSessionMap synced_session_map_
;
231 // The set of tabs that we have seen, and created SessionTab objects for, but
232 // have not yet mapped to SyncedSessions. These are temporarily orphaned
233 // tabs, and won't be deleted if we delete synced_session_map_, but are still
234 // owned by the SyncedSessionTracker itself (and deleted on Clear()).
235 std::set
<sessions::SessionTab
*> unmapped_tabs_
;
237 // The tag for this machine's local session, so we can distinguish the foreign
239 std::string local_session_tag_
;
241 DISALLOW_COPY_AND_ASSIGN(SyncedSessionTracker
);
244 } // namespace browser_sync
246 #endif // CHROME_BROWSER_SYNC_GLUE_SYNCED_SESSION_TRACKER_H_