[email protected] | d70fb36 | 2011-06-15 15:07:39 | [diff] [blame^] | 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 |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include "chrome/browser/sync/glue/synced_session_tracker.h" |
| 6 | #include "chrome/browser/sync/glue/session_model_associator.h" |
| 7 | |
| 8 | namespace browser_sync { |
| 9 | |
| 10 | |
| 11 | SyncedSessionTracker::SyncedSessionTracker() { |
| 12 | } |
| 13 | |
| 14 | SyncedSessionTracker::~SyncedSessionTracker() { |
| 15 | clear(); |
| 16 | } |
| 17 | |
| 18 | bool SyncedSessionTracker::LookupAllForeignSessions( |
| 19 | std::vector<const SyncedSession*>* sessions) { |
| 20 | DCHECK(sessions); |
| 21 | // Fill vector of sessions from our synced session map. |
| 22 | for (SyncedSessionMap::const_iterator i = |
| 23 | synced_session_map_.begin(); i != synced_session_map_.end(); ++i) { |
| 24 | // Only include foreign sessions with open tabs. |
| 25 | SyncedSession* foreign_session = i->second; |
| 26 | if (i->first != local_session_tag_ && |
| 27 | !foreign_session->windows.empty() && |
| 28 | !SessionModelAssociator::SessionWindowHasNoTabsToSync( |
| 29 | *foreign_session->windows[0])) { |
| 30 | sessions->push_back(foreign_session); |
| 31 | } |
| 32 | } |
| 33 | |
| 34 | return !sessions->empty(); |
| 35 | } |
| 36 | |
| 37 | bool SyncedSessionTracker::LookupSessionWindows( |
| 38 | const std::string& session_tag, |
| 39 | std::vector<SessionWindow*>* windows) { |
| 40 | DCHECK(windows); |
| 41 | SyncedSessionMap::iterator iter = synced_session_map_.find(session_tag); |
| 42 | if (iter == synced_session_map_.end()) |
| 43 | return false; |
| 44 | *windows = iter->second->windows; |
| 45 | return true; |
| 46 | } |
| 47 | |
| 48 | bool SyncedSessionTracker::LookupSessionTab( |
| 49 | const std::string& tag, |
| 50 | SessionID::id_type tab_id, |
| 51 | const SessionTab** tab) { |
| 52 | DCHECK(tab); |
| 53 | if (synced_tab_map_.find(tag) == synced_tab_map_.end()) { |
| 54 | // We have no record of this session. |
| 55 | *tab = NULL; |
| 56 | return false; |
| 57 | } |
| 58 | if (synced_tab_map_[tag]->find(tab_id) == synced_tab_map_[tag]->end()) { |
| 59 | // We have no record of this tab. |
| 60 | *tab = NULL; |
| 61 | return false; |
| 62 | } |
| 63 | *tab = (*synced_tab_map_[tag])[tab_id]; |
| 64 | return true; |
| 65 | } |
| 66 | |
| 67 | SyncedSession* SyncedSessionTracker::GetSession( |
| 68 | const std::string& session_tag) { |
| 69 | scoped_ptr<SyncedSession> synced_session; |
| 70 | if (synced_session_map_.find(session_tag) != |
| 71 | synced_session_map_.end()) { |
| 72 | synced_session.reset(synced_session_map_[session_tag]); |
| 73 | } else { |
| 74 | synced_session.reset(new SyncedSession); |
| 75 | synced_session->session_tag = session_tag; |
| 76 | synced_session_map_[session_tag] = synced_session.get(); |
| 77 | } |
| 78 | DCHECK(synced_session.get()); |
| 79 | return synced_session.release(); |
| 80 | } |
| 81 | |
| 82 | bool SyncedSessionTracker::DeleteSession( |
| 83 | const std::string& session_tag) { |
| 84 | SyncedSessionMap::iterator iter = |
| 85 | synced_session_map_.find(session_tag); |
| 86 | if (iter != synced_session_map_.end()) { |
| 87 | delete iter->second; // Delete the SyncedSession object. |
| 88 | synced_session_map_.erase(iter); |
| 89 | return true; |
| 90 | } else { |
| 91 | return false; |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | SessionTab* SyncedSessionTracker::GetSessionTab( |
| 96 | const std::string& session_tag, |
| 97 | SessionID::id_type tab_id, |
| 98 | bool has_window) { |
| 99 | if (synced_tab_map_.find(session_tag) == synced_tab_map_.end()) |
| 100 | synced_tab_map_[session_tag] = new IDToSessionTabMap; |
| 101 | scoped_ptr<SessionTab> tab; |
| 102 | IDToSessionTabMap::iterator iter = |
| 103 | synced_tab_map_[session_tag]->find(tab_id); |
| 104 | if (iter != synced_tab_map_[session_tag]->end()) { |
| 105 | tab.reset(iter->second); |
| 106 | if (has_window) // This tab is linked to a window, so it's not an orphan. |
| 107 | unmapped_tabs_.erase(tab.get()); |
| 108 | VLOG(1) << "Associating " << session_tag << "'s seen tab " << |
| 109 | tab_id << " at " << tab.get(); |
| 110 | } else { |
| 111 | tab.reset(new SessionTab()); |
| 112 | (*synced_tab_map_[session_tag])[tab_id] = tab.get(); |
| 113 | if (!has_window) // This tab is not linked to a window, it's an orphan. |
| 114 | unmapped_tabs_.insert(tab.get()); |
| 115 | VLOG(1) << "Associating " << session_tag << "'s new tab " << |
| 116 | tab_id << " at " << tab.get(); |
| 117 | } |
| 118 | DCHECK(tab.get()); |
| 119 | return tab.release(); |
| 120 | } |
| 121 | |
| 122 | void SyncedSessionTracker::clear() { |
| 123 | // Delete SyncedSession objects (which also deletes all their windows/tabs). |
| 124 | STLDeleteContainerPairSecondPointers(synced_session_map_.begin(), |
| 125 | synced_session_map_.end()); |
| 126 | synced_session_map_.clear(); |
| 127 | |
| 128 | // Delete IDToSessTab maps. Does not delete the SessionTab objects, because |
| 129 | // they should already be referenced through synced_session_map_. |
| 130 | STLDeleteContainerPairSecondPointers(synced_tab_map_.begin(), |
| 131 | synced_tab_map_.end()); |
| 132 | synced_tab_map_.clear(); |
| 133 | |
| 134 | // Go through and delete any tabs we had allocated but had not yet placed into |
| 135 | // a SyncedSessionobject. |
| 136 | STLDeleteContainerPointers(unmapped_tabs_.begin(), unmapped_tabs_.end()); |
| 137 | unmapped_tabs_.clear(); |
| 138 | } |
| 139 | |
| 140 | } // namespace browser_sync |