| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "components/sync_sessions/synced_session_tracker.h" | 5 #include "components/sync_sessions/synced_session_tracker.h" |
| 6 | 6 |
| 7 #include "base/logging.h" | 7 #include "base/logging.h" |
| 8 #include "base/stl_util.h" | 8 #include "base/stl_util.h" |
| 9 #include "base/strings/utf_string_conversions.h" | 9 #include "base/strings/utf_string_conversions.h" |
| 10 #include "components/sync_sessions/sync_sessions_client.h" | 10 #include "components/sync_sessions/sync_sessions_client.h" |
| 11 | 11 |
| 12 namespace browser_sync { | 12 namespace sync_sessions { |
| 13 | 13 |
| 14 namespace { | 14 namespace { |
| 15 | 15 |
| 16 // Helper for iterating through all tabs within a window, and all navigations | 16 // Helper for iterating through all tabs within a window, and all navigations |
| 17 // within a tab, to find if there's a valid syncable url. | 17 // within a tab, to find if there's a valid syncable url. |
| 18 bool ShouldSyncSessionWindow(sync_sessions::SyncSessionsClient* sessions_client, | 18 bool ShouldSyncSessionWindow(SyncSessionsClient* sessions_client, |
| 19 const sessions::SessionWindow& window) { | 19 const sessions::SessionWindow& window) { |
| 20 for (sessions::SessionTab* const tab : window.tabs) { | 20 for (sessions::SessionTab* const tab : window.tabs) { |
| 21 for (const sessions::SerializedNavigationEntry& navigation : | 21 for (const sessions::SerializedNavigationEntry& navigation : |
| 22 tab->navigations) { | 22 tab->navigations) { |
| 23 if (sessions_client->ShouldSyncURL(navigation.virtual_url())) { | 23 if (sessions_client->ShouldSyncURL(navigation.virtual_url())) { |
| 24 return true; | 24 return true; |
| 25 } | 25 } |
| 26 } | 26 } |
| 27 } | 27 } |
| 28 return false; | 28 return false; |
| 29 } | 29 } |
| 30 | 30 |
| 31 // Presentable means |foreign_session| must have syncable content. | 31 // Presentable means |foreign_session| must have syncable content. |
| 32 bool IsPresentable(sync_sessions::SyncSessionsClient* sessions_client, | 32 bool IsPresentable(SyncSessionsClient* sessions_client, |
| 33 sync_driver::SyncedSession* foreign_session) { | 33 SyncedSession* foreign_session) { |
| 34 for (sync_driver::SyncedSession::SyncedWindowMap::const_iterator iter = | 34 for (SyncedSession::SyncedWindowMap::const_iterator iter = |
| 35 foreign_session->windows.begin(); | 35 foreign_session->windows.begin(); |
| 36 iter != foreign_session->windows.end(); ++iter) { | 36 iter != foreign_session->windows.end(); ++iter) { |
| 37 if (ShouldSyncSessionWindow(sessions_client, *(iter->second))) { | 37 if (ShouldSyncSessionWindow(sessions_client, *(iter->second))) { |
| 38 return true; | 38 return true; |
| 39 } | 39 } |
| 40 } | 40 } |
| 41 return false; | 41 return false; |
| 42 } | 42 } |
| 43 | 43 |
| 44 } // namespace | 44 } // namespace |
| 45 | 45 |
| 46 SyncedSessionTracker::SyncedSessionTracker( | 46 SyncedSessionTracker::SyncedSessionTracker(SyncSessionsClient* sessions_client) |
| 47 sync_sessions::SyncSessionsClient* sessions_client) | |
| 48 : sessions_client_(sessions_client) {} | 47 : sessions_client_(sessions_client) {} |
| 49 | 48 |
| 50 SyncedSessionTracker::~SyncedSessionTracker() { | 49 SyncedSessionTracker::~SyncedSessionTracker() { |
| 51 Clear(); | 50 Clear(); |
| 52 } | 51 } |
| 53 | 52 |
| 54 void SyncedSessionTracker::SetLocalSessionTag( | 53 void SyncedSessionTracker::SetLocalSessionTag( |
| 55 const std::string& local_session_tag) { | 54 const std::string& local_session_tag) { |
| 56 local_session_tag_ = local_session_tag; | 55 local_session_tag_ = local_session_tag; |
| 57 } | 56 } |
| 58 | 57 |
| 59 bool SyncedSessionTracker::LookupAllForeignSessions( | 58 bool SyncedSessionTracker::LookupAllForeignSessions( |
| 60 std::vector<const sync_driver::SyncedSession*>* sessions, | 59 std::vector<const SyncedSession*>* sessions, |
| 61 SessionLookup lookup) const { | 60 SessionLookup lookup) const { |
| 62 DCHECK(sessions); | 61 DCHECK(sessions); |
| 63 sessions->clear(); | 62 sessions->clear(); |
| 64 for (SyncedSessionMap::const_iterator i = synced_session_map_.begin(); | 63 for (SyncedSessionMap::const_iterator i = synced_session_map_.begin(); |
| 65 i != synced_session_map_.end(); ++i) { | 64 i != synced_session_map_.end(); ++i) { |
| 66 sync_driver::SyncedSession* foreign_session = i->second; | 65 SyncedSession* foreign_session = i->second; |
| 67 if (i->first != local_session_tag_ && | 66 if (i->first != local_session_tag_ && |
| 68 (lookup == RAW || IsPresentable(sessions_client_, foreign_session))) { | 67 (lookup == RAW || IsPresentable(sessions_client_, foreign_session))) { |
| 69 sessions->push_back(foreign_session); | 68 sessions->push_back(foreign_session); |
| 70 } | 69 } |
| 71 } | 70 } |
| 72 return !sessions->empty(); | 71 return !sessions->empty(); |
| 73 } | 72 } |
| 74 | 73 |
| 75 bool SyncedSessionTracker::LookupSessionWindows( | 74 bool SyncedSessionTracker::LookupSessionWindows( |
| 76 const std::string& session_tag, | 75 const std::string& session_tag, |
| 77 std::vector<const sessions::SessionWindow*>* windows) const { | 76 std::vector<const sessions::SessionWindow*>* windows) const { |
| 78 DCHECK(windows); | 77 DCHECK(windows); |
| 79 windows->clear(); | 78 windows->clear(); |
| 80 SyncedSessionMap::const_iterator iter = synced_session_map_.find(session_tag); | 79 SyncedSessionMap::const_iterator iter = synced_session_map_.find(session_tag); |
| 81 if (iter == synced_session_map_.end()) | 80 if (iter == synced_session_map_.end()) |
| 82 return false; | 81 return false; |
| 83 windows->clear(); | 82 windows->clear(); |
| 84 for (sync_driver::SyncedSession::SyncedWindowMap::const_iterator window_iter = | 83 for (SyncedSession::SyncedWindowMap::const_iterator window_iter = |
| 85 iter->second->windows.begin(); | 84 iter->second->windows.begin(); |
| 86 window_iter != iter->second->windows.end(); ++window_iter) { | 85 window_iter != iter->second->windows.end(); ++window_iter) { |
| 87 windows->push_back(window_iter->second); | 86 windows->push_back(window_iter->second); |
| 88 } | 87 } |
| 89 return true; | 88 return true; |
| 90 } | 89 } |
| 91 | 90 |
| 92 bool SyncedSessionTracker::LookupSessionTab( | 91 bool SyncedSessionTracker::LookupSessionTab( |
| 93 const std::string& tag, | 92 const std::string& tag, |
| 94 SessionID::id_type tab_id, | 93 SessionID::id_type tab_id, |
| (...skipping 23 matching lines...) Expand all Loading... |
| 118 synced_session_map_.find(session_tag); | 117 synced_session_map_.find(session_tag); |
| 119 if (session_iter != synced_session_map_.end()) { | 118 if (session_iter != synced_session_map_.end()) { |
| 120 tab_node_ids->insert(session_iter->second->tab_node_ids.begin(), | 119 tab_node_ids->insert(session_iter->second->tab_node_ids.begin(), |
| 121 session_iter->second->tab_node_ids.end()); | 120 session_iter->second->tab_node_ids.end()); |
| 122 } | 121 } |
| 123 // Incase an invalid node id was included, remove it. | 122 // Incase an invalid node id was included, remove it. |
| 124 tab_node_ids->erase(TabNodePool::kInvalidTabNodeID); | 123 tab_node_ids->erase(TabNodePool::kInvalidTabNodeID); |
| 125 } | 124 } |
| 126 | 125 |
| 127 bool SyncedSessionTracker::LookupLocalSession( | 126 bool SyncedSessionTracker::LookupLocalSession( |
| 128 const sync_driver::SyncedSession** output) const { | 127 const SyncedSession** output) const { |
| 129 SyncedSessionMap::const_iterator it = | 128 SyncedSessionMap::const_iterator it = |
| 130 synced_session_map_.find(local_session_tag_); | 129 synced_session_map_.find(local_session_tag_); |
| 131 if (it != synced_session_map_.end()) { | 130 if (it != synced_session_map_.end()) { |
| 132 *output = it->second; | 131 *output = it->second; |
| 133 return true; | 132 return true; |
| 134 } | 133 } |
| 135 return false; | 134 return false; |
| 136 } | 135 } |
| 137 | 136 |
| 138 sync_driver::SyncedSession* SyncedSessionTracker::GetSession( | 137 SyncedSession* SyncedSessionTracker::GetSession( |
| 139 const std::string& session_tag) { | 138 const std::string& session_tag) { |
| 140 sync_driver::SyncedSession* synced_session = NULL; | 139 SyncedSession* synced_session = NULL; |
| 141 if (synced_session_map_.find(session_tag) != synced_session_map_.end()) { | 140 if (synced_session_map_.find(session_tag) != synced_session_map_.end()) { |
| 142 synced_session = synced_session_map_[session_tag]; | 141 synced_session = synced_session_map_[session_tag]; |
| 143 } else { | 142 } else { |
| 144 synced_session = new sync_driver::SyncedSession; | 143 synced_session = new SyncedSession; |
| 145 DVLOG(1) << "Creating new session with tag " << session_tag << " at " | 144 DVLOG(1) << "Creating new session with tag " << session_tag << " at " |
| 146 << synced_session; | 145 << synced_session; |
| 147 synced_session->session_tag = session_tag; | 146 synced_session->session_tag = session_tag; |
| 148 synced_session_map_[session_tag] = synced_session; | 147 synced_session_map_[session_tag] = synced_session; |
| 149 } | 148 } |
| 150 DCHECK(synced_session); | 149 DCHECK(synced_session); |
| 151 return synced_session; | 150 return synced_session; |
| 152 } | 151 } |
| 153 | 152 |
| 154 bool SyncedSessionTracker::DeleteSession(const std::string& session_tag) { | 153 bool SyncedSessionTracker::DeleteSession(const std::string& session_tag) { |
| 155 // Cleanup first, which will take care of orphaned SessionTab and | 154 // Cleanup first, which will take care of orphaned SessionTab and |
| 156 // SessionWindow objects. The SyncedSession destructor will only delete things | 155 // SessionWindow objects. The SyncedSession destructor will only delete things |
| 157 // that it is currently the parent of. | 156 // that it is currently the parent of. |
| 158 CleanupSession(session_tag); | 157 CleanupSession(session_tag); |
| 159 | 158 |
| 160 bool header_existed = false; | 159 bool header_existed = false; |
| 161 SyncedSessionMap::iterator iter = synced_session_map_.find(session_tag); | 160 SyncedSessionMap::iterator iter = synced_session_map_.find(session_tag); |
| 162 if (iter != synced_session_map_.end()) { | 161 if (iter != synced_session_map_.end()) { |
| 163 sync_driver::SyncedSession* session = iter->second; | 162 SyncedSession* session = iter->second; |
| 164 // An implicitly created session that has children tabs but no header node | 163 // An implicitly created session that has children tabs but no header node |
| 165 // will have never had the device_type changed from unset. | 164 // will have never had the device_type changed from unset. |
| 166 header_existed = | 165 header_existed = session->device_type != SyncedSession::TYPE_UNSET; |
| 167 session->device_type != sync_driver::SyncedSession::TYPE_UNSET; | |
| 168 synced_session_map_.erase(iter); | 166 synced_session_map_.erase(iter); |
| 169 // SyncedSession's destructor will trigger deletion of windows which will in | 167 // SyncedSession's destructor will trigger deletion of windows which will in |
| 170 // turn trigger the deletion of tabs. This doesn't affect wrappers. | 168 // turn trigger the deletion of tabs. This doesn't affect wrappers. |
| 171 delete session; | 169 delete session; |
| 172 } | 170 } |
| 173 | 171 |
| 174 // These two erase(...) calls only affect the wrappers. | 172 // These two erase(...) calls only affect the wrappers. |
| 175 synced_window_map_.erase(session_tag); | 173 synced_window_map_.erase(session_tag); |
| 176 synced_tab_map_.erase(session_tag); | 174 synced_tab_map_.erase(session_tag); |
| 177 | 175 |
| (...skipping 258 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 436 // a SyncedSession object. | 434 // a SyncedSession object. |
| 437 base::STLDeleteElements(&unmapped_tabs_); | 435 base::STLDeleteElements(&unmapped_tabs_); |
| 438 | 436 |
| 439 // Get rid of our Window/Tab maps (does not delete the actual Window/Tabs | 437 // Get rid of our Window/Tab maps (does not delete the actual Window/Tabs |
| 440 // themselves; they should have all been deleted above). | 438 // themselves; they should have all been deleted above). |
| 441 synced_window_map_.clear(); | 439 synced_window_map_.clear(); |
| 442 synced_tab_map_.clear(); | 440 synced_tab_map_.clear(); |
| 443 local_session_tag_.clear(); | 441 local_session_tag_.clear(); |
| 444 } | 442 } |
| 445 | 443 |
| 446 } // namespace browser_sync | 444 } // namespace sync_sessions |
| OLD | NEW |