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
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/sessions/session_id.h"
16 #include "chrome/browser/sessions/session_types.h"
17 #include "chrome/browser/sync/glue/synced_session.h"
19 namespace browser_sync
{
21 // Class to manage synced sessions. The tracker will own all SyncedSession
22 // and SyncedSessionTab objects it creates, and deletes them appropriately on
24 // Note: SyncedSession objects are created for all synced sessions, including
25 // the local session (whose tag we maintain separately).
26 class SyncedSessionTracker
{
28 SyncedSessionTracker();
29 ~SyncedSessionTracker();
31 // We track and distinguish the local session from foreign sessions.
32 void SetLocalSessionTag(const std::string
& local_session_tag
);
34 // Fill a preallocated vector with all foreign sessions we're tracking (skips
35 // the local session object). SyncedSession ownership remains within the
36 // SyncedSessionTracker.
37 // Returns true if we had foreign sessions to fill it with, false otherwise.
38 bool LookupAllForeignSessions(std::vector
<const SyncedSession
*>* sessions
)
41 // Attempts to look up the session windows associatd with the session given
42 // by |session_tag|. Ownership Of SessionWindows stays within the
43 // SyncedSessionTracker.
44 // If lookup succeeds:
45 // - Fills windows with the SessionWindow pointers, returns true.
48 bool LookupSessionWindows(const std::string
& session_tag
,
49 std::vector
<const SessionWindow
*>* windows
) const;
51 // Attempts to look up the tab associated with the given tag and tab id.
52 // Ownership of the SessionTab remains within the SyncedSessionTracker.
53 // If lookup succeeds:
54 // - Sets tab to point to the SessionTab, and returns true.
56 // - Returns false, tab is set to NULL.
57 bool LookupSessionTab(const std::string
& session_tag
,
58 SessionID::id_type tab_id
,
59 const SessionTab
** tab
) const;
61 // Returns a pointer to the SyncedSession object associated with
62 // |session_tag|. If none exists, creates one. Ownership of the
63 // SyncedSession remains within the SyncedSessionTracker.
64 SyncedSession
* GetSession(const std::string
& session_tag
);
66 // Deletes the session associated with |session_tag| if it exists.
67 // Returns true if the session existed and was deleted, false otherwise.
68 bool DeleteSession(const std::string
& session_tag
);
70 // Resets the tracking information for the session specified by |session_tag|.
71 // This involves clearing all the windows and tabs from the session, while
72 // keeping pointers saved in the synced_window_map_ and synced_tab_map_.
73 // Once reset, all calls to PutWindowInSession and PutTabInWindow will denote
74 // that the requested windows and tabs are owned (by setting the boolean
75 // in their SessionWindowWrapper/SessionTabWrapper to true) and add them back
76 // to their session. The next call to CleanupSession(...) will delete those
77 // windows and tabs not owned.
78 void ResetSessionTracking(const std::string
& session_tag
);
80 // Deletes those windows and tabs associated with |session_tag| that are no
82 // See ResetSessionTracking(...).
83 void CleanupSession(const std::string
& session_tag
);
85 // Adds the window with id |window_id| to the session specified by
86 // |session_tag|, and markes the window as being owned. If none existed for
87 // that session, creates one. Similarly, if the session did not exist yet,
88 // creates it. Ownership of the SessionWindow remains within the
89 // SyncedSessionTracker.
90 void PutWindowInSession(const std::string
& session_tag
,
91 SessionID::id_type window_id
);
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 // Note: GetSession(..) must have already been called with |session_tag| to
97 // ensure we having mapping information for this session.
98 void PutTabInWindow(const std::string
& session_tag
,
99 SessionID::id_type window_id
,
100 SessionID::id_type tab_id
,
103 // Returns a pointer to the SessionTab object associated with |tab_id| for
104 // the session specified with |session_tag|. If none exists, creates one.
105 // Ownership of the SessionTab remains within the SyncedSessionTracker.
106 SessionTab
* GetTab(const std::string
& session_tag
,
107 SessionID::id_type tab_id
);
109 // Free the memory for all dynamically allocated objects and clear the
110 // tracking structures.
114 return synced_tab_map_
.empty() && synced_session_map_
.empty();
117 // Includes both foreign sessions and the local session.
118 size_t num_synced_sessions() const {
119 return synced_session_map_
.size();
122 // Returns the number of tabs associated with the specified session tag.
123 size_t num_synced_tabs(const std::string
& session_tag
) const {
124 SyncedTabMap::const_iterator iter
= synced_tab_map_
.find(session_tag
);
125 if (iter
!= synced_tab_map_
.end()) {
126 return iter
->second
.size();
132 // Datatypes for accessing session data. Neither of the *Wrappers actually
133 // have ownership of the Windows/Tabs, they just provide id-based access to
134 // them. The ownership remains within its containing session (for windows and
135 // mapped tabs, unmapped tabs are owned by the unmapped_tabs_ container).
136 // Note, we pair pointers with bools so that we can track what is owned and
137 // what can be deleted (see ResetSessionTracking(..) and CleanupSession(..)
139 struct SessionTabWrapper
{
140 SessionTabWrapper() : tab_ptr(NULL
), owned(false) {}
141 SessionTabWrapper(SessionTab
* tab_ptr
, bool owned
)
147 typedef std::map
<SessionID::id_type
, SessionTabWrapper
> IDToSessionTabMap
;
148 typedef std::map
<std::string
, IDToSessionTabMap
> SyncedTabMap
;
150 struct SessionWindowWrapper
{
151 SessionWindowWrapper() : window_ptr(NULL
), owned(false) {}
152 SessionWindowWrapper(SessionWindow
* window_ptr
, bool owned
)
153 : window_ptr(window_ptr
),
155 SessionWindow
* window_ptr
;
158 typedef std::map
<SessionID::id_type
, SessionWindowWrapper
>
159 IDToSessionWindowMap
;
160 typedef std::map
<std::string
, IDToSessionWindowMap
> SyncedWindowMap
;
162 typedef std::map
<std::string
, SyncedSession
*> SyncedSessionMap
;
164 // Helper methods for deleting SessionWindows and SessionTabs without owners.
165 bool DeleteOldSessionWindowIfNecessary(SessionWindowWrapper window_wrapper
);
166 bool DeleteOldSessionTabIfNecessary(SessionTabWrapper tab_wrapper
);
168 // Per client mapping of tab id's to their SessionTab objects.
170 // Value: Tab id to SessionTabWrapper map.
171 SyncedTabMap synced_tab_map_
;
173 // Per client mapping of the window id's to their SessionWindow objects.
175 // Value: Window id to SessionWindowWrapper map.
176 SyncedWindowMap synced_window_map_
;
178 // Per client mapping synced session objects.
180 // Value: SyncedSession object pointer.
181 SyncedSessionMap synced_session_map_
;
183 // The set of tabs that we have seen, and created SessionTab objects for, but
184 // have not yet mapped to SyncedSessions. These are temporarily orphaned
185 // tabs, and won't be deleted if we delete synced_session_map_, but are still
186 // owned by the SyncedSessionTracker itself (and deleted on Clear()).
187 std::set
<SessionTab
*> unmapped_tabs_
;
189 // The tag for this machine's local session, so we can distinguish the foreign
191 std::string local_session_tag_
;
193 DISALLOW_COPY_AND_ASSIGN(SyncedSessionTracker
);
196 } // namespace browser_sync
198 #endif // CHROME_BROWSER_SYNC_GLUE_SYNCED_SESSION_TRACKER_H_