summaryrefslogtreecommitdiffstats
path: root/chrome/browser/sync/glue/synced_session_tracker.cc
blob: 92b95b7ba49afa71c15567b86f7af5cb7d3fef80 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "base/logging.h"
#include "base/stl_util.h"
#include "base/utf_string_conversions.h"
#include "chrome/browser/sync/glue/synced_session_tracker.h"

namespace browser_sync {

SyncedSessionTracker::SyncedSessionTracker() {
}

SyncedSessionTracker::~SyncedSessionTracker() {
  Clear();
}

void SyncedSessionTracker::SetLocalSessionTag(
    const std::string& local_session_tag) {
  local_session_tag_ = local_session_tag;
}

bool SyncedSessionTracker::LookupAllForeignSessions(
    std::vector<const SyncedSession*>* sessions) const {
  DCHECK(sessions);
  sessions->clear();
  // Fill vector of sessions from our synced session map.
  for (SyncedSessionMap::const_iterator i =
    synced_session_map_.begin(); i != synced_session_map_.end(); ++i) {
    // Only include foreign sessions with open tabs.
    SyncedSession* foreign_session = i->second;
    if (i->first != local_session_tag_ && !foreign_session->windows.empty()) {
      bool found_tabs = false;
      for (SyncedSession::SyncedWindowMap::const_iterator iter =
               foreign_session->windows.begin();
           iter != foreign_session->windows.end(); ++iter) {
        if (!SessionWindowHasNoTabsToSync(*(iter->second))) {
          found_tabs = true;
          break;
        }
      }
      if (found_tabs)
        sessions->push_back(foreign_session);
    }
  }

  return !sessions->empty();
}

bool SyncedSessionTracker::LookupSessionWindows(
    const std::string& session_tag,
    std::vector<const SessionWindow*>* windows) const {
  DCHECK(windows);
  windows->clear();
  SyncedSessionMap::const_iterator iter = synced_session_map_.find(session_tag);
  if (iter == synced_session_map_.end())
    return false;
  windows->clear();
  for (SyncedSession::SyncedWindowMap::const_iterator window_iter =
           iter->second->windows.begin();
       window_iter != iter->second->windows.end(); window_iter++) {
    windows->push_back(window_iter->second);
  }
  return true;
}

bool SyncedSessionTracker::LookupSessionTab(
    const std::string& tag,
    SessionID::id_type tab_id,
    const SessionTab** tab) const {
  DCHECK(tab);
  SyncedTabMap::const_iterator tab_map_iter = synced_tab_map_.find(tag);
  if (tab_map_iter == synced_tab_map_.end()) {
    // We have no record of this session.
    *tab = NULL;
    return false;
  }
  IDToSessionTabMap::const_iterator tab_iter =
      tab_map_iter->second.find(tab_id);
  if (tab_iter == tab_map_iter->second.end()) {
    // We have no record of this tab.
    *tab = NULL;
    return false;
  }
  *tab = tab_iter->second.tab_ptr;
  return true;
}

SyncedSession* SyncedSessionTracker::GetSession(
    const std::string& session_tag) {
  SyncedSession* synced_session = NULL;
  if (synced_session_map_.find(session_tag) !=
      synced_session_map_.end()) {
    synced_session = synced_session_map_[session_tag];
  } else {
    synced_session = new SyncedSession;
    DVLOG(1) << "Creating new session with tag " << session_tag << " at "
             << synced_session;
    synced_session->session_tag = session_tag;
    synced_session_map_[session_tag] = synced_session;
  }
  DCHECK(synced_session);
  return synced_session;
}

bool SyncedSessionTracker::DeleteSession(const std::string& session_tag) {
  bool found_session = false;
  SyncedSessionMap::iterator iter = synced_session_map_.find(session_tag);
  if (iter != synced_session_map_.end()) {
    SyncedSession* session = iter->second;
    synced_session_map_.erase(iter);
    delete session;  // Delete the SyncedSession object.
    found_session = true;
  }
  synced_window_map_.erase(session_tag);
  // It's possible there was no header node but there were tab nodes.
  if (synced_tab_map_.erase(session_tag) > 0) {
    found_session = true;
  }
  return found_session;
}

void SyncedSessionTracker::ResetSessionTracking(
    const std::string& session_tag) {
  // Reset window tracking.
  GetSession(session_tag)->windows.clear();
  SyncedWindowMap::iterator window_iter = synced_window_map_.find(session_tag);
  if (window_iter != synced_window_map_.end()) {
    for (IDToSessionWindowMap::iterator window_map_iter =
             window_iter->second.begin();
         window_map_iter != window_iter->second.end(); ++window_map_iter) {
      window_map_iter->second.owned = false;
      // We clear out the tabs to prevent double referencing of the same tab.
      // All tabs that are in use will be added back as needed.
      window_map_iter->second.window_ptr->tabs.clear();
    }
  }

  // Reset tab tracking.
  SyncedTabMap::iterator tab_iter = synced_tab_map_.find(session_tag);
  if (tab_iter != synced_tab_map_.end()) {
    for (IDToSessionTabMap::iterator tab_map_iter =
             tab_iter->second.begin();
         tab_map_iter != tab_iter->second.end(); ++tab_map_iter) {
      tab_map_iter->second.owned = false;
    }
  }
}

bool SyncedSessionTracker::DeleteOldSessionWindowIfNecessary(
    SessionWindowWrapper window_wrapper) {
  // Clear the tabs first, since we don't want the destructor to destroy
  // them. Their deletion will be handled by DeleteOldSessionTab below.
  if (!window_wrapper.owned) {
    DVLOG(1) << "Deleting closed window "
             << window_wrapper.window_ptr->window_id.id();
    window_wrapper.window_ptr->tabs.clear();
    delete window_wrapper.window_ptr;
    return true;
  }
  return false;
}

bool SyncedSessionTracker::DeleteOldSessionTabIfNecessary(
    SessionTabWrapper tab_wrapper) {
  if (!tab_wrapper.owned) {
    if (VLOG_IS_ON(1)) {
      SessionTab* tab_ptr = tab_wrapper.tab_ptr;
      std::string title;
      if (tab_ptr->navigations.size() > 0) {
        title = " (" + UTF16ToUTF8(
            tab_ptr->navigations[tab_ptr->navigations.size()-1].title()) + ")";
      }
      DVLOG(1) << "Deleting closed tab " << tab_ptr->tab_id.id() << title
               << " from window " << tab_ptr->window_id.id();
    }
    unmapped_tabs_.erase(tab_wrapper.tab_ptr);
    delete tab_wrapper.tab_ptr;
    return true;
  }
  return false;
}

void SyncedSessionTracker::CleanupSession(const std::string& session_tag) {
  // Go through and delete any windows or tabs without owners.
  SyncedWindowMap::iterator window_iter = synced_window_map_.find(session_tag);
  if (window_iter != synced_window_map_.end()) {
    for (IDToSessionWindowMap::iterator iter = window_iter->second.begin();
         iter != window_iter->second.end();) {
      SessionWindowWrapper window_wrapper = iter->second;
      if (DeleteOldSessionWindowIfNecessary(window_wrapper))
        window_iter->second.erase(iter++);
      else
        ++iter;
    }
  }

  SyncedTabMap::iterator tab_iter = synced_tab_map_.find(session_tag);
  if (tab_iter != synced_tab_map_.end()) {
    for (IDToSessionTabMap::iterator iter = tab_iter->second.begin();
         iter != tab_iter->second.end();) {
      SessionTabWrapper tab_wrapper = iter->second;
      if (DeleteOldSessionTabIfNecessary(tab_wrapper))
        tab_iter->second.erase(iter++);
      else
        ++iter;
    }
  }
}

void SyncedSessionTracker::PutWindowInSession(const std::string& session_tag,
                                              SessionID::id_type window_id) {
  SessionWindow* window_ptr = NULL;
  IDToSessionWindowMap::iterator iter =
      synced_window_map_[session_tag].find(window_id);
  if (iter != synced_window_map_[session_tag].end()) {
    iter->second.owned = true;
    window_ptr = iter->second.window_ptr;
    DVLOG(1) << "Putting seen window " << window_id  << " at " << window_ptr
             << "in " << (session_tag == local_session_tag_ ?
                          "local session" : session_tag);
  } else {
    // Create the window.
    window_ptr = new SessionWindow();
    window_ptr->window_id.set_id(window_id);
    synced_window_map_[session_tag][window_id] =
        SessionWindowWrapper(window_ptr, true);
    DVLOG(1) << "Putting new window " << window_id  << " at " << window_ptr
             << "in " << (session_tag == local_session_tag_ ?
                          "local session" : session_tag);
  }
  DCHECK(window_ptr);
  DCHECK_EQ(window_ptr->window_id.id(), window_id);
  DCHECK_EQ(reinterpret_cast<SessionWindow*>(NULL),
            GetSession(session_tag)->windows[window_id]);
  GetSession(session_tag)->windows[window_id] = window_ptr;
}

void SyncedSessionTracker::PutTabInWindow(const std::string& session_tag,
                                          SessionID::id_type window_id,
                                          SessionID::id_type tab_id,
                                          size_t tab_index) {
  SessionTab* tab_ptr = GetTab(session_tag, tab_id);
  unmapped_tabs_.erase(tab_ptr);
  synced_tab_map_[session_tag][tab_id].owned = true;
  tab_ptr->window_id.set_id(window_id);
  DVLOG(1) << "  - tab " << tab_id << " added to window "<< window_id;
  DCHECK(GetSession(session_tag)->windows.find(window_id) !=
         GetSession(session_tag)->windows.end());
  std::vector<SessionTab*>& window_tabs =
      GetSession(session_tag)->windows[window_id]->tabs;
  if (window_tabs.size() <= tab_index) {
    window_tabs.resize(tab_index+1, NULL);
  }
  DCHECK(!window_tabs[tab_index]);
  window_tabs[tab_index] = tab_ptr;
}

SessionTab* SyncedSessionTracker::GetTab(
    const std::string& session_tag,
    SessionID::id_type tab_id) {
  SessionTab* tab_ptr = NULL;
  IDToSessionTabMap::iterator iter =
      synced_tab_map_[session_tag].find(tab_id);
  if (iter != synced_tab_map_[session_tag].end()) {
    tab_ptr = iter->second.tab_ptr;
    if (VLOG_IS_ON(1)) {
      std::string title;
      if (tab_ptr->navigations.size() > 0) {
        title = " (" + UTF16ToUTF8(
            tab_ptr->navigations[tab_ptr->navigations.size()-1].title()) + ")";
      }
      DVLOG(1) << "Getting "
               << (session_tag == local_session_tag_ ?
                   "local session" : session_tag)
               << "'s seen tab " << tab_id  << " at " << tab_ptr << title;
    }
  } else {
    tab_ptr = new SessionTab();
    tab_ptr->tab_id.set_id(tab_id);
    synced_tab_map_[session_tag][tab_id] = SessionTabWrapper(tab_ptr, false);
    unmapped_tabs_.insert(tab_ptr);
    DVLOG(1) << "Getting "
             << (session_tag == local_session_tag_ ?
                 "local session" : session_tag)
             << "'s new tab " << tab_id  << " at " << tab_ptr;
  }
  DCHECK(tab_ptr);
  DCHECK_EQ(tab_ptr->tab_id.id(), tab_id);
  return tab_ptr;
}

void SyncedSessionTracker::Clear() {
  // Delete SyncedSession objects (which also deletes all their windows/tabs).
  STLDeleteValues(&synced_session_map_);

  // Go through and delete any tabs we had allocated but had not yet placed into
  // a SyncedSessionobject.
  STLDeleteElements(&unmapped_tabs_);

  // Get rid of our Window/Tab maps (does not delete the actual Window/Tabs
  // themselves; they should have all been deleted above).
  synced_window_map_.clear();
  synced_tab_map_.clear();

  local_session_tag_.clear();
}

}  // namespace browser_sync