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