Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 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 | 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 "chrome/browser/sync/glue/session_model_associator.h" | 5 #include "chrome/browser/sync/glue/session_model_associator.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 #include <set> | 8 #include <set> |
| 9 #include <utility> | 9 #include <utility> |
| 10 | 10 |
| 11 #include "base/location.h" | 11 #include "base/location.h" |
| 12 #include "base/logging.h" | 12 #include "base/logging.h" |
| 13 #include "base/sys_info.h" | 13 #include "base/sys_info.h" |
| 14 #include "chrome/browser/browser_process.h" | 14 #include "chrome/browser/browser_process.h" |
| 15 #include "chrome/browser/profiles/profile.h" | 15 #include "chrome/browser/profiles/profile.h" |
| 16 #include "chrome/browser/sessions/session_id.h" | |
| 16 #include "chrome/browser/sessions/session_service_factory.h" | 17 #include "chrome/browser/sessions/session_service_factory.h" |
| 17 #include "chrome/browser/sync/api/sync_error.h" | 18 #include "chrome/browser/sync/api/sync_error.h" |
| 18 #include "chrome/browser/sync/glue/synced_session.h" | 19 #include "chrome/browser/sync/glue/synced_session.h" |
| 19 #include "chrome/browser/sync/glue/synced_tab_delegate.h" | 20 #include "chrome/browser/sync/glue/synced_tab_delegate.h" |
| 20 #include "chrome/browser/sync/glue/synced_window_delegate.h" | 21 #include "chrome/browser/sync/glue/synced_window_delegate.h" |
| 21 #include "chrome/browser/sync/internal_api/read_node.h" | 22 #include "chrome/browser/sync/internal_api/read_node.h" |
| 22 #include "chrome/browser/sync/internal_api/read_transaction.h" | 23 #include "chrome/browser/sync/internal_api/read_transaction.h" |
| 23 #include "chrome/browser/sync/internal_api/write_node.h" | 24 #include "chrome/browser/sync/internal_api/write_node.h" |
| 24 #include "chrome/browser/sync/internal_api/write_transaction.h" | 25 #include "chrome/browser/sync/internal_api/write_transaction.h" |
| 25 #include "chrome/browser/sync/profile_sync_service.h" | 26 #include "chrome/browser/sync/profile_sync_service.h" |
| (...skipping 111 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 137 #elif defined(OS_MACOSX) | 138 #elif defined(OS_MACOSX) |
| 138 header_s->set_device_type(sync_pb::SessionHeader_DeviceType_TYPE_MAC); | 139 header_s->set_device_type(sync_pb::SessionHeader_DeviceType_TYPE_MAC); |
| 139 #elif defined(OS_WIN) | 140 #elif defined(OS_WIN) |
| 140 header_s->set_device_type(sync_pb::SessionHeader_DeviceType_TYPE_WIN); | 141 header_s->set_device_type(sync_pb::SessionHeader_DeviceType_TYPE_WIN); |
| 141 #elif defined(OS_CHROMEOS) | 142 #elif defined(OS_CHROMEOS) |
| 142 header_s->set_device_type(sync_pb::SessionHeader_DeviceType_TYPE_CROS); | 143 header_s->set_device_type(sync_pb::SessionHeader_DeviceType_TYPE_CROS); |
| 143 #else | 144 #else |
| 144 header_s->set_device_type(sync_pb::SessionHeader_DeviceType_TYPE_OTHER); | 145 header_s->set_device_type(sync_pb::SessionHeader_DeviceType_TYPE_OTHER); |
| 145 #endif | 146 #endif |
| 146 | 147 |
| 147 size_t window_num = 0; | 148 synced_session_tracker_.ResetSessionTracking(local_tag); |
| 148 std::set<SyncedWindowDelegate*> windows = | 149 std::set<SyncedWindowDelegate*> windows = |
| 149 SyncedWindowDelegate::GetSyncedWindowDelegates(); | 150 SyncedWindowDelegate::GetSyncedWindowDelegates(); |
| 150 current_session->windows.reserve(windows.size()); | |
| 151 for (std::set<SyncedWindowDelegate*>::const_iterator i = | 151 for (std::set<SyncedWindowDelegate*>::const_iterator i = |
| 152 windows.begin(); i != windows.end(); ++i) { | 152 windows.begin(); i != windows.end(); ++i) { |
| 153 // Make sure the window has tabs and a viewable window. The viewable window | 153 // Make sure the window has tabs and a viewable window. The viewable window |
| 154 // check is necessary because, for example, when a browser is closed the | 154 // check is necessary because, for example, when a browser is closed the |
| 155 // destructor is not necessarily run immediately. This means its possible | 155 // destructor is not necessarily run immediately. This means its possible |
| 156 // for us to get a handle to a browser that is about to be removed. If | 156 // for us to get a handle to a browser that is about to be removed. If |
| 157 // the tab count is 0 or the window is NULL, the browser is about to be | 157 // the tab count is 0 or the window is NULL, the browser is about to be |
| 158 // deleted, so we ignore it. | 158 // deleted, so we ignore it. |
| 159 if (ShouldSyncWindow(*i) && (*i)->GetTabCount() && | 159 if (ShouldSyncWindow(*i) && (*i)->GetTabCount() && (*i)->HasWindow()) { |
| 160 (*i)->HasWindow()) { | |
| 161 sync_pb::SessionWindow window_s; | 160 sync_pb::SessionWindow window_s; |
| 162 SessionID::id_type window_id = (*i)->GetSessionId(); | 161 SessionID::id_type window_id = (*i)->GetSessionId(); |
| 163 VLOG(1) << "Reassociating window " << window_id << " with " << | 162 VLOG(1) << "Reassociating window " << window_id << " with " << |
| 164 (*i)->GetTabCount() << " tabs."; | 163 (*i)->GetTabCount() << " tabs."; |
| 165 window_s.set_window_id(window_id); | 164 window_s.set_window_id(window_id); |
| 166 window_s.set_selected_tab_index((*i)->GetActiveIndex()); | 165 window_s.set_selected_tab_index((*i)->GetActiveIndex()); |
| 167 if ((*i)->IsTypeTabbed()) { | 166 if ((*i)->IsTypeTabbed()) { |
| 168 window_s.set_browser_type( | 167 window_s.set_browser_type( |
| 169 sync_pb::SessionWindow_BrowserType_TYPE_TABBED); | 168 sync_pb::SessionWindow_BrowserType_TYPE_TABBED); |
| 170 } else { | 169 } else { |
| (...skipping 13 matching lines...) Expand all Loading... | |
| 184 ReassociateTab(*tab); | 183 ReassociateTab(*tab); |
| 185 } | 184 } |
| 186 } | 185 } |
| 187 } | 186 } |
| 188 // Only add a window if it contains valid tabs. | 187 // Only add a window if it contains valid tabs. |
| 189 if (found_tabs) { | 188 if (found_tabs) { |
| 190 sync_pb::SessionWindow* header_window = header_s->add_window(); | 189 sync_pb::SessionWindow* header_window = header_s->add_window(); |
| 191 *header_window = window_s; | 190 *header_window = window_s; |
| 192 | 191 |
| 193 // Update this window's representation in the synced session tracker. | 192 // Update this window's representation in the synced session tracker. |
| 194 if (window_num >= current_session->windows.size()) { | 193 SessionWindow* window_ptr = synced_session_tracker_.GetWindow( |
| 195 // This a new window, create it. | 194 local_tag, window_id); |
| 196 current_session->windows.push_back(new SessionWindow()); | 195 current_session->windows[window_id] = window_ptr; |
| 197 } | |
| 198 PopulateSessionWindowFromSpecifics( | 196 PopulateSessionWindowFromSpecifics( |
| 199 local_tag, | 197 local_tag, |
| 200 window_s, | 198 window_s, |
| 201 base::Time::Now(), | 199 base::Time::Now(), |
| 202 current_session->windows[window_num++], | 200 window_ptr, |
| 203 &synced_session_tracker_); | 201 &synced_session_tracker_); |
| 204 } | 202 } |
| 205 } | 203 } |
| 206 } | 204 } |
| 205 // Free memory for closed windows and tabs. | |
| 206 synced_session_tracker_.CleanupSession(local_tag); | |
| 207 | 207 |
| 208 sync_api::WriteTransaction trans(FROM_HERE, sync_service_->GetUserShare()); | 208 sync_api::WriteTransaction trans(FROM_HERE, sync_service_->GetUserShare()); |
| 209 sync_api::WriteNode header_node(&trans); | 209 sync_api::WriteNode header_node(&trans); |
| 210 if (!header_node.InitByIdLookup(local_session_syncid_)) { | 210 if (!header_node.InitByIdLookup(local_session_syncid_)) { |
| 211 LOG(ERROR) << "Failed to load local session header node."; | 211 LOG(ERROR) << "Failed to load local session header node."; |
| 212 return; | 212 return; |
| 213 } | 213 } |
| 214 header_node.SetSessionSpecifics(specifics); | 214 header_node.SetSessionSpecifics(specifics); |
| 215 if (waiting_for_change_) QuitLoopForSubtleTesting(); | 215 if (waiting_for_change_) QuitLoopForSubtleTesting(); |
| 216 } | 216 } |
| (...skipping 110 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 327 sync_pb::TabNavigation* nav_s = tab_s->add_navigation(); | 327 sync_pb::TabNavigation* nav_s = tab_s->add_navigation(); |
| 328 PopulateSessionSpecificsNavigation(&tab_nav, nav_s); | 328 PopulateSessionSpecificsNavigation(&tab_nav, nav_s); |
| 329 } | 329 } |
| 330 } | 330 } |
| 331 tab_s->set_current_navigation_index(current_index); | 331 tab_s->set_current_navigation_index(current_index); |
| 332 | 332 |
| 333 tab_node.SetSessionSpecifics(session_s); | 333 tab_node.SetSessionSpecifics(session_s); |
| 334 | 334 |
| 335 // Convert to a local representation and store in synced session tracker. | 335 // Convert to a local representation and store in synced session tracker. |
| 336 SessionTab* session_tab = | 336 SessionTab* session_tab = |
| 337 synced_session_tracker_.GetSessionTab(GetCurrentMachineTag(), | 337 synced_session_tracker_.GetTab(GetCurrentMachineTag(), |
| 338 tab_s->tab_id(), | 338 tab_s->tab_id()); |
| 339 false); | |
| 340 PopulateSessionTabFromSpecifics(*tab_s, | 339 PopulateSessionTabFromSpecifics(*tab_s, |
| 341 base::Time::Now(), | 340 base::Time::Now(), |
| 342 session_tab); | 341 session_tab); |
| 343 return true; | 342 return true; |
| 344 } | 343 } |
| 345 | 344 |
| 346 // Static | 345 // Static |
| 347 // TODO(zea): perhaps sync state (scroll position, form entries, etc.) as well? | 346 // TODO(zea): perhaps sync state (scroll position, form entries, etc.) as well? |
| 348 // See http://crbug.com/67068. | 347 // See http://crbug.com/67068. |
| 349 void SessionModelAssociator::PopulateSessionSpecificsNavigation( | 348 void SessionModelAssociator::PopulateSessionSpecificsNavigation( |
| (...skipping 277 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 627 return false; | 626 return false; |
| 628 | 627 |
| 629 if (specifics.has_header()) { | 628 if (specifics.has_header()) { |
| 630 // Read in the header data for this foreign session. | 629 // Read in the header data for this foreign session. |
| 631 // Header data contains window information and ordered tab id's for each | 630 // Header data contains window information and ordered tab id's for each |
| 632 // window. | 631 // window. |
| 633 | 632 |
| 634 // Load (or create) the SyncedSession object for this client. | 633 // Load (or create) the SyncedSession object for this client. |
| 635 SyncedSession* foreign_session = | 634 SyncedSession* foreign_session = |
| 636 synced_session_tracker_.GetSession(foreign_session_tag); | 635 synced_session_tracker_.GetSession(foreign_session_tag); |
| 637 | |
| 638 const sync_pb::SessionHeader& header = specifics.header(); | 636 const sync_pb::SessionHeader& header = specifics.header(); |
| 639 PopulateSessionHeaderFromSpecifics(header, foreign_session); | 637 PopulateSessionHeaderFromSpecifics(header, foreign_session); |
| 640 foreign_session->windows.reserve(header.window_size()); | 638 |
| 639 // Reset the tab/window tracking for this session (must do this before | |
| 640 // we start calling GetWindow and GetTabForWindow so that all | |
| 641 // unused tabs/windows get cleared by the CleanupSession(...) call). | |
| 642 synced_session_tracker_.ResetSessionTracking(foreign_session_tag); | |
| 643 | |
| 644 // Process all the windows and their tab information. | |
| 645 size_t i, num_windows; | |
|
akalin
2011/09/26 18:34:44
int num_windows = header.window_size();
...
for (i
Nicolas Zea
2011/09/26 19:47:36
Done.
| |
| 646 num_windows = static_cast<size_t>(header.window_size()); | |
| 641 VLOG(1) << "Associating " << foreign_session_tag << " with " << | 647 VLOG(1) << "Associating " << foreign_session_tag << " with " << |
| 642 header.window_size() << " windows."; | 648 num_windows << " windows."; |
| 643 size_t i; | 649 for (i = 0; i < num_windows; ++i) { |
| 644 for (i = 0; i < static_cast<size_t>(header.window_size()); ++i) { | |
| 645 if (i >= foreign_session->windows.size()) { | |
| 646 // This a new window, create it. | |
| 647 foreign_session->windows.push_back(new SessionWindow()); | |
| 648 } | |
| 649 const sync_pb::SessionWindow& window_s = header.window(i); | 650 const sync_pb::SessionWindow& window_s = header.window(i); |
| 651 SessionID::id_type window_id = window_s.window_id(); | |
| 652 SessionWindow* window_ptr = | |
| 653 synced_session_tracker_.GetWindow(foreign_session_tag, window_id); | |
| 654 foreign_session->windows[window_id] = window_ptr; | |
| 650 PopulateSessionWindowFromSpecifics(foreign_session_tag, | 655 PopulateSessionWindowFromSpecifics(foreign_session_tag, |
| 651 window_s, | 656 window_s, |
| 652 modification_time, | 657 modification_time, |
| 653 foreign_session->windows[i], | 658 window_ptr, |
| 654 &synced_session_tracker_); | 659 &synced_session_tracker_); |
| 655 } | 660 } |
| 656 // Remove any remaining windows (in case windows were closed) | 661 |
| 657 for (; i < foreign_session->windows.size(); ++i) { | 662 // Delete any closed windows and unused tabs as necessary. |
| 658 delete foreign_session->windows[i]; | 663 synced_session_tracker_.CleanupSession(foreign_session_tag); |
| 659 } | |
| 660 foreign_session->windows.resize(header.window_size()); | |
| 661 } else if (specifics.has_tab()) { | 664 } else if (specifics.has_tab()) { |
| 662 const sync_pb::SessionTab& tab_s = specifics.tab(); | 665 const sync_pb::SessionTab& tab_s = specifics.tab(); |
| 663 SessionID::id_type tab_id = tab_s.tab_id(); | 666 SessionID::id_type tab_id = tab_s.tab_id(); |
| 664 SessionTab* tab = | 667 SessionTab* tab = |
| 665 synced_session_tracker_.GetSessionTab(foreign_session_tag, | 668 synced_session_tracker_.GetTab(foreign_session_tag, tab_id); |
| 666 tab_id, | |
| 667 false); | |
| 668 PopulateSessionTabFromSpecifics(tab_s, modification_time, tab); | 669 PopulateSessionTabFromSpecifics(tab_s, modification_time, tab); |
| 669 } else { | 670 } else { |
| 670 NOTREACHED(); | 671 NOTREACHED(); |
| 671 return false; | 672 return false; |
| 672 } | 673 } |
| 673 | 674 |
| 674 return true; | 675 return true; |
| 675 } | 676 } |
| 676 | 677 |
| 677 void SessionModelAssociator::DisassociateForeignSession( | 678 void SessionModelAssociator::DisassociateForeignSession( |
| (...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 727 session_window->type = 1; | 728 session_window->type = 1; |
| 728 } else { | 729 } else { |
| 729 session_window->type = 2; | 730 session_window->type = 2; |
| 730 } | 731 } |
| 731 } | 732 } |
| 732 session_window->timestamp = mtime; | 733 session_window->timestamp = mtime; |
| 733 session_window->tabs.resize(specifics.tab_size()); | 734 session_window->tabs.resize(specifics.tab_size()); |
| 734 for (int i = 0; i < specifics.tab_size(); i++) { | 735 for (int i = 0; i < specifics.tab_size(); i++) { |
| 735 SessionID::id_type tab_id = specifics.tab(i); | 736 SessionID::id_type tab_id = specifics.tab(i); |
| 736 session_window->tabs[i] = | 737 session_window->tabs[i] = |
| 737 tracker->GetSessionTab(session_tag, tab_id, true); | 738 tracker->GetTabForWindow(session_tag, |
| 739 session_window->window_id.id(), | |
| 740 tab_id); | |
| 738 } | 741 } |
| 739 } | 742 } |
| 740 | 743 |
| 741 // Static | 744 // Static |
| 742 void SessionModelAssociator::PopulateSessionTabFromSpecifics( | 745 void SessionModelAssociator::PopulateSessionTabFromSpecifics( |
| 743 const sync_pb::SessionTab& specifics, | 746 const sync_pb::SessionTab& specifics, |
| 744 const base::Time& mtime, | 747 const base::Time& mtime, |
| 745 SessionTab* tab) { | 748 SessionTab* tab) { |
| 749 DCHECK_EQ(tab->tab_id.id(), specifics.tab_id()); | |
| 746 if (specifics.has_tab_id()) | 750 if (specifics.has_tab_id()) |
| 747 tab->tab_id.set_id(specifics.tab_id()); | 751 tab->tab_id.set_id(specifics.tab_id()); |
| 748 if (specifics.has_window_id()) | 752 if (specifics.has_window_id()) |
| 749 tab->window_id.set_id(specifics.window_id()); | 753 tab->window_id.set_id(specifics.window_id()); |
| 750 if (specifics.has_tab_visual_index()) | 754 if (specifics.has_tab_visual_index()) |
| 751 tab->tab_visual_index = specifics.tab_visual_index(); | 755 tab->tab_visual_index = specifics.tab_visual_index(); |
| 752 if (specifics.has_current_navigation_index()) | 756 if (specifics.has_current_navigation_index()) |
| 753 tab->current_navigation_index = specifics.current_navigation_index(); | 757 tab->current_navigation_index = specifics.current_navigation_index(); |
| 754 if (specifics.has_pinned()) | 758 if (specifics.has_pinned()) |
| 755 tab->pinned = specifics.pinned(); | 759 tab->pinned = specifics.pinned(); |
| (...skipping 155 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 911 // Pool size should always match # of free tab nodes. | 915 // Pool size should always match # of free tab nodes. |
| 912 DCHECK_LT(tab_pool_fp_, static_cast<int64>(tab_syncid_pool_.size())); | 916 DCHECK_LT(tab_pool_fp_, static_cast<int64>(tab_syncid_pool_.size())); |
| 913 tab_syncid_pool_[static_cast<size_t>(++tab_pool_fp_)] = sync_id; | 917 tab_syncid_pool_[static_cast<size_t>(++tab_pool_fp_)] = sync_id; |
| 914 } | 918 } |
| 915 | 919 |
| 916 bool SessionModelAssociator::GetLocalSession( | 920 bool SessionModelAssociator::GetLocalSession( |
| 917 const SyncedSession* * local_session) { | 921 const SyncedSession* * local_session) { |
| 918 DCHECK(CalledOnValidThread()); | 922 DCHECK(CalledOnValidThread()); |
| 919 if (current_machine_tag_.empty()) | 923 if (current_machine_tag_.empty()) |
| 920 return false; | 924 return false; |
| 921 *local_session = | 925 *local_session = synced_session_tracker_.GetSession(GetCurrentMachineTag()); |
| 922 synced_session_tracker_.GetSession(GetCurrentMachineTag()); | |
| 923 return true; | 926 return true; |
| 924 } | 927 } |
| 925 | 928 |
| 926 bool SessionModelAssociator::GetAllForeignSessions( | 929 bool SessionModelAssociator::GetAllForeignSessions( |
| 927 std::vector<const SyncedSession*>* sessions) { | 930 std::vector<const SyncedSession*>* sessions) { |
| 928 DCHECK(CalledOnValidThread()); | 931 DCHECK(CalledOnValidThread()); |
| 929 return synced_session_tracker_.LookupAllForeignSessions(sessions); | 932 return synced_session_tracker_.LookupAllForeignSessions(sessions); |
| 930 } | 933 } |
| 931 | 934 |
| 932 bool SessionModelAssociator::GetForeignSession( | 935 bool SessionModelAssociator::GetForeignSession( |
| (...skipping 257 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1190 char computer_name[MAX_COMPUTERNAME_LENGTH + 1]; | 1193 char computer_name[MAX_COMPUTERNAME_LENGTH + 1]; |
| 1191 DWORD size = sizeof(computer_name); | 1194 DWORD size = sizeof(computer_name); |
| 1192 if (GetComputerNameA(computer_name, &size)) { | 1195 if (GetComputerNameA(computer_name, &size)) { |
| 1193 return computer_name; | 1196 return computer_name; |
| 1194 } | 1197 } |
| 1195 return std::string(); | 1198 return std::string(); |
| 1196 } | 1199 } |
| 1197 #endif | 1200 #endif |
| 1198 | 1201 |
| 1199 } // namespace browser_sync | 1202 } // namespace browser_sync |
| OLD | NEW |