| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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/sessions_sync_manager.h" | 5 #include "components/sync_sessions/sessions_sync_manager.h" |
| 6 | 6 |
| 7 #include <stdint.h> | 7 #include <stdint.h> |
| 8 | 8 |
| 9 #include <utility> | 9 #include <utility> |
| 10 | 10 |
| (...skipping 531 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 542 if (static_cast<size_t>(i) >= entries_.size()) | 542 if (static_cast<size_t>(i) >= entries_.size()) |
| 543 return; | 543 return; |
| 544 *serialized_entry = | 544 *serialized_entry = |
| 545 sessions::ContentSerializedNavigationBuilder::FromNavigationEntry( | 545 sessions::ContentSerializedNavigationBuilder::FromNavigationEntry( |
| 546 i, *entries_[i]); | 546 i, *entries_[i]); |
| 547 } | 547 } |
| 548 | 548 |
| 549 int GetEntryCount() const override { return entries_.size(); } | 549 int GetEntryCount() const override { return entries_.size(); } |
| 550 | 550 |
| 551 SessionID::id_type GetWindowId() const override { | 551 SessionID::id_type GetWindowId() const override { |
| 552 return SessionID::id_type(); | 552 return window_id_; |
| 553 } |
| 554 void SetWindowId(SessionID::id_type window_id) { |
| 555 window_id_ = window_id; |
| 553 } | 556 } |
| 554 | 557 |
| 555 SessionID::id_type GetSessionId() const override { | 558 SessionID::id_type GetSessionId() const override { |
| 556 return SessionID::id_type(); | 559 return tab_id_; |
| 560 } |
| 561 void SetSessionId(SessionID::id_type id) { |
| 562 tab_id_ = id; |
| 557 } | 563 } |
| 558 | 564 |
| 559 bool IsBeingDestroyed() const override { return false; } | 565 bool IsBeingDestroyed() const override { return false; } |
| 560 std::string GetExtensionAppId() const override { return std::string(); } | 566 std::string GetExtensionAppId() const override { return std::string(); } |
| 561 bool ProfileIsSupervised() const override { return is_supervised_; } | 567 bool ProfileIsSupervised() const override { return is_supervised_; } |
| 562 void set_is_supervised(bool is_supervised) { is_supervised_ = is_supervised; } | 568 void set_is_supervised(bool is_supervised) { is_supervised_ = is_supervised; } |
| 563 const std::vector<std::unique_ptr<const sessions::SerializedNavigationEntry>>* | 569 const std::vector<std::unique_ptr<const sessions::SerializedNavigationEntry>>* |
| 564 GetBlockedNavigations() const override { | 570 GetBlockedNavigations() const override { |
| 565 return &blocked_navigations_; | 571 return &blocked_navigations_; |
| 566 } | 572 } |
| (...skipping 20 matching lines...) Expand all Loading... |
| 587 void reset() { | 593 void reset() { |
| 588 current_entry_index_ = 0; | 594 current_entry_index_ = 0; |
| 589 sync_id_ = -1; | 595 sync_id_ = -1; |
| 590 entries_.clear(); | 596 entries_.clear(); |
| 591 } | 597 } |
| 592 | 598 |
| 593 private: | 599 private: |
| 594 int current_entry_index_; | 600 int current_entry_index_; |
| 595 bool is_supervised_; | 601 bool is_supervised_; |
| 596 int sync_id_; | 602 int sync_id_; |
| 603 SessionID::id_type tab_id_ = 0; |
| 604 SessionID::id_type window_id_ = 0; |
| 597 std::vector<std::unique_ptr<const sessions::SerializedNavigationEntry>> | 605 std::vector<std::unique_ptr<const sessions::SerializedNavigationEntry>> |
| 598 blocked_navigations_; | 606 blocked_navigations_; |
| 599 std::vector<std::unique_ptr<content::NavigationEntry>> entries_; | 607 std::vector<std::unique_ptr<content::NavigationEntry>> entries_; |
| 600 }; | 608 }; |
| 601 | 609 |
| 602 } // namespace | 610 } // namespace |
| 603 | 611 |
| 604 static const base::Time kTime0 = base::Time::FromInternalValue(100); | 612 static const base::Time kTime0 = base::Time::FromInternalValue(100); |
| 605 static const base::Time kTime1 = base::Time::FromInternalValue(110); | 613 static const base::Time kTime1 = base::Time::FromInternalValue(110); |
| 606 static const base::Time kTime2 = base::Time::FromInternalValue(120); | 614 static const base::Time kTime2 = base::Time::FromInternalValue(120); |
| (...skipping 374 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 981 in.push_back(CreateRemoteData(t1_entity)); | 989 in.push_back(CreateRemoteData(t1_entity)); |
| 982 in.push_back(CreateRemoteData(t2_entity)); | 990 in.push_back(CreateRemoteData(t2_entity)); |
| 983 out.clear(); | 991 out.clear(); |
| 984 manager()->StopSyncing(syncer::SESSIONS); | 992 manager()->StopSyncing(syncer::SESSIONS); |
| 985 | 993 |
| 986 const std::set<const SyncedWindowDelegate*>& windows = | 994 const std::set<const SyncedWindowDelegate*>& windows = |
| 987 manager()->synced_window_delegates_getter()->GetSyncedWindowDelegates(); | 995 manager()->synced_window_delegates_getter()->GetSyncedWindowDelegates(); |
| 988 ASSERT_EQ(1U, windows.size()); | 996 ASSERT_EQ(1U, windows.size()); |
| 989 SyncedTabDelegateFake t1_override, t2_override; | 997 SyncedTabDelegateFake t1_override, t2_override; |
| 990 t1_override.SetSyncId(1); // No WebContents by default. | 998 t1_override.SetSyncId(1); // No WebContents by default. |
| 999 t1_override.SetSessionId(kNewTabId); |
| 991 t2_override.SetSyncId(2); // No WebContents by default. | 1000 t2_override.SetSyncId(2); // No WebContents by default. |
| 1001 t2_override.SetSessionId(t2_entity.session().tab().tab_id()); |
| 992 SyncedWindowDelegateOverride window_override(*windows.begin()); | 1002 SyncedWindowDelegateOverride window_override(*windows.begin()); |
| 993 window_override.OverrideTabAt(1, &t1_override, kNewTabId); | 1003 window_override.OverrideTabAt(1, &t1_override, kNewTabId); |
| 994 window_override.OverrideTabAt(2, &t2_override, | 1004 window_override.OverrideTabAt(2, &t2_override, |
| 995 t2_entity.session().tab().tab_id()); | 1005 t2_entity.session().tab().tab_id()); |
| 996 std::set<const SyncedWindowDelegate*> delegates; | 1006 std::set<const SyncedWindowDelegate*> delegates; |
| 997 delegates.insert(&window_override); | 1007 delegates.insert(&window_override); |
| 998 std::unique_ptr<TestSyncedWindowDelegatesGetter> getter( | 1008 std::unique_ptr<TestSyncedWindowDelegatesGetter> getter( |
| 999 new TestSyncedWindowDelegatesGetter(delegates)); | 1009 new TestSyncedWindowDelegatesGetter(delegates)); |
| 1000 set_synced_window_getter(getter.get()); | 1010 set_synced_window_getter(getter.get()); |
| 1001 | 1011 |
| 1002 syncer::SyncMergeResult result = manager()->MergeDataAndStartSyncing( | 1012 syncer::SyncMergeResult result = manager()->MergeDataAndStartSyncing( |
| 1003 syncer::SESSIONS, in, std::unique_ptr<syncer::SyncChangeProcessor>( | 1013 syncer::SESSIONS, in, std::unique_ptr<syncer::SyncChangeProcessor>( |
| 1004 new TestSyncProcessorStub(&out)), | 1014 new TestSyncProcessorStub(&out)), |
| 1005 std::unique_ptr<syncer::SyncErrorFactory>( | 1015 std::unique_ptr<syncer::SyncErrorFactory>( |
| 1006 new syncer::SyncErrorFactoryMock())); | 1016 new syncer::SyncErrorFactoryMock())); |
| 1007 | 1017 |
| 1008 // There should be two changes, one for the fully associated tab, and | 1018 // There should be two changes, one for the fully associated tab, and |
| 1009 // one for the tab_id update to t1. t2 shouldn't need to be updated. | 1019 // one each for the tab_id updates to t1 and t2. |
| 1010 ASSERT_EQ(2U, FilterOutLocalHeaderChanges(&out)->size()); | 1020 ASSERT_EQ(3U, FilterOutLocalHeaderChanges(&out)->size()); |
| 1011 EXPECT_EQ(SyncChange::ACTION_UPDATE, out[0].change_type()); | 1021 EXPECT_EQ(SyncChange::ACTION_UPDATE, out[0].change_type()); |
| 1012 EXPECT_EQ(SyncChange::ACTION_UPDATE, out[1].change_type()); | 1022 EXPECT_EQ(SyncChange::ACTION_UPDATE, out[1].change_type()); |
| 1023 EXPECT_EQ(SyncChange::ACTION_UPDATE, out[2].change_type()); |
| 1013 EXPECT_EQ(kNewTabId, | 1024 EXPECT_EQ(kNewTabId, |
| 1014 out[1].sync_data().GetSpecifics().session().tab().tab_id()); | 1025 out[1].sync_data().GetSpecifics().session().tab().tab_id()); |
| 1026 EXPECT_EQ(t2_entity.session().tab().tab_id(), |
| 1027 out[2].sync_data().GetSpecifics().session().tab().tab_id()); |
| 1015 | 1028 |
| 1016 // Verify TabLinks. | 1029 // Verify TabLinks. |
| 1017 SessionsSyncManager::TabLinksMap tab_map = manager()->local_tab_map_; | 1030 SessionsSyncManager::TabLinksMap tab_map = manager()->local_tab_map_; |
| 1018 ASSERT_EQ(3U, tab_map.size()); | 1031 ASSERT_EQ(3U, tab_map.size()); |
| 1019 int t2_tab_id = t2_entity.session().tab().tab_id(); | 1032 int t2_tab_id = t2_entity.session().tab().tab_id(); |
| 1020 EXPECT_EQ(2, tab_map.find(t2_tab_id)->second->tab_node_id()); | 1033 EXPECT_EQ(2, tab_map.find(t2_tab_id)->second->tab_node_id()); |
| 1021 EXPECT_EQ(1, tab_map.find(kNewTabId)->second->tab_node_id()); | 1034 EXPECT_EQ(1, tab_map.find(kNewTabId)->second->tab_node_id()); |
| 1022 int t0_tab_id = out[0].sync_data().GetSpecifics().session().tab().tab_id(); | 1035 int t0_tab_id = out[0].sync_data().GetSpecifics().session().tab().tab_id(); |
| 1023 EXPECT_EQ(0, tab_map.find(t0_tab_id)->second->tab_node_id()); | 1036 EXPECT_EQ(0, tab_map.find(t0_tab_id)->second->tab_node_id()); |
| 1024 // TODO(tim): Once bug 337057 is fixed, we can issue an OnLocalTabModified | |
| 1025 // from here (using an override similar to above) to return a new tab id | |
| 1026 // and verify that we don't see any node creations in the SyncChangeProcessor | |
| 1027 // (similar to how SessionsSyncManagerTest.OnLocalTabModified works.) | |
| 1028 } | 1037 } |
| 1029 | 1038 |
| 1030 // Ensure model association updates the window ID for tabs whose window's ID has | 1039 // Ensure model association updates the window ID for tabs whose window's ID has |
| 1031 // changed. | 1040 // changed. |
| 1032 TEST_F(SessionsSyncManagerTest, WindowIdUpdatedOnRestore) { | 1041 TEST_F(SessionsSyncManagerTest, WindowIdUpdatedOnRestore) { |
| 1033 const int kNewWindowId = 1337; | 1042 const int kNewWindowId = 1337; |
| 1034 syncer::SyncDataList in; | 1043 syncer::SyncDataList in; |
| 1035 syncer::SyncChangeList out; | 1044 syncer::SyncChangeList out; |
| 1036 | 1045 |
| 1037 // Set up one tab and start sync with it. | 1046 // Set up one tab and start sync with it. |
| 1038 AddTab(browser(), GURL("http://foo1")); | 1047 AddTab(browser(), GURL("http://foo1")); |
| 1039 NavigateAndCommitActiveTab(GURL("http://foo2")); | 1048 NavigateAndCommitActiveTab(GURL("http://foo2")); |
| 1040 InitWithSyncDataTakeOutput(in, &out); | 1049 InitWithSyncDataTakeOutput(in, &out); |
| 1041 | 1050 |
| 1042 // Should be one header add, 1 tab add/update pair, and one header update. | 1051 // Should be one header add, 1 tab add/update pair, and one header update. |
| 1043 ASSERT_EQ(4U, out.size()); | 1052 ASSERT_EQ(4U, out.size()); |
| 1044 const sync_pb::EntitySpecifics t0_entity = out[2].sync_data().GetSpecifics(); | 1053 const sync_pb::EntitySpecifics t0_entity = out[2].sync_data().GetSpecifics(); |
| 1045 | 1054 |
| 1046 in.push_back(CreateRemoteData(t0_entity)); | 1055 in.push_back(CreateRemoteData(t0_entity)); |
| 1047 out.clear(); | 1056 out.clear(); |
| 1048 manager()->StopSyncing(syncer::SESSIONS); | 1057 manager()->StopSyncing(syncer::SESSIONS); |
| 1049 | 1058 |
| 1050 // SyncedTabDelegateFake is a placeholder (no WebContents) by default. | 1059 // SyncedTabDelegateFake is a placeholder (no WebContents) by default. |
| 1051 SyncedTabDelegateFake t0_override; | 1060 SyncedTabDelegateFake t0_override; |
| 1052 t0_override.SetSyncId(t0_entity.session().tab_node_id()); | 1061 t0_override.SetSyncId(t0_entity.session().tab_node_id()); |
| 1062 t0_override.SetWindowId(kNewWindowId); |
| 1053 | 1063 |
| 1054 // Set up the window override with the new window ID and placeholder tab. | 1064 // Set up the window override with the new window ID and placeholder tab. |
| 1055 const std::set<const SyncedWindowDelegate*>& windows = | 1065 const std::set<const SyncedWindowDelegate*>& windows = |
| 1056 get_synced_window_getter()->GetSyncedWindowDelegates(); | 1066 get_synced_window_getter()->GetSyncedWindowDelegates(); |
| 1057 ASSERT_EQ(1U, windows.size()); | 1067 ASSERT_EQ(1U, windows.size()); |
| 1058 SyncedWindowDelegateOverride window_override(*windows.begin()); | 1068 SyncedWindowDelegateOverride window_override(*windows.begin()); |
| 1059 window_override.OverrideSessionId(kNewWindowId); | 1069 window_override.OverrideSessionId(kNewWindowId); |
| 1060 window_override.OverrideTabAt(0, &t0_override, | 1070 window_override.OverrideTabAt(0, &t0_override, |
| 1061 t0_entity.session().tab().tab_id()); | 1071 t0_entity.session().tab().tab_id()); |
| 1062 | 1072 |
| (...skipping 237 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1300 EXPECT_TRUE(changes.empty()); | 1310 EXPECT_TRUE(changes.empty()); |
| 1301 | 1311 |
| 1302 // Fill an instance of session specifics with a foreign session's data. | 1312 // Fill an instance of session specifics with a foreign session's data. |
| 1303 std::vector<sync_pb::SessionSpecifics> tabs; | 1313 std::vector<sync_pb::SessionSpecifics> tabs; |
| 1304 SessionID::id_type n1[] = {5, 10, 13, 17}; | 1314 SessionID::id_type n1[] = {5, 10, 13, 17}; |
| 1305 std::vector<SessionID::id_type> tab_nums1(n1, n1 + arraysize(n1)); | 1315 std::vector<SessionID::id_type> tab_nums1(n1, n1 + arraysize(n1)); |
| 1306 sync_pb::SessionSpecifics meta(helper()->BuildForeignSession( | 1316 sync_pb::SessionSpecifics meta(helper()->BuildForeignSession( |
| 1307 tag, tab_nums1, &tabs)); | 1317 tag, tab_nums1, &tabs)); |
| 1308 | 1318 |
| 1309 // Update associator with the session's meta node, window, and tabs. | 1319 // Update associator with the session's meta node, window, and tabs. |
| 1310 manager()->UpdateTrackerWithForeignSession(meta, base::Time()); | 1320 manager()->UpdateTrackerWithSpecifics(meta, base::Time()); |
| 1311 for (std::vector<sync_pb::SessionSpecifics>::iterator iter = tabs.begin(); | 1321 for (std::vector<sync_pb::SessionSpecifics>::iterator iter = tabs.begin(); |
| 1312 iter != tabs.end(); ++iter) { | 1322 iter != tabs.end(); ++iter) { |
| 1313 manager()->UpdateTrackerWithForeignSession(*iter, base::Time()); | 1323 manager()->UpdateTrackerWithSpecifics(*iter, base::Time()); |
| 1314 } | 1324 } |
| 1315 ASSERT_TRUE(manager()->GetAllForeignSessions(&foreign_sessions)); | 1325 ASSERT_TRUE(manager()->GetAllForeignSessions(&foreign_sessions)); |
| 1316 ASSERT_EQ(1U, foreign_sessions.size()); | 1326 ASSERT_EQ(1U, foreign_sessions.size()); |
| 1317 | 1327 |
| 1318 // Now delete the foreign session. | 1328 // Now delete the foreign session. |
| 1319 manager()->DeleteForeignSessionInternal(tag, &changes); | 1329 manager()->DeleteForeignSessionInternal(tag, &changes); |
| 1320 EXPECT_FALSE(manager()->GetAllForeignSessions(&foreign_sessions)); | 1330 EXPECT_FALSE(manager()->GetAllForeignSessions(&foreign_sessions)); |
| 1321 | 1331 |
| 1322 EXPECT_EQ(5U, changes.size()); | 1332 EXPECT_EQ(5U, changes.size()); |
| 1323 std::set<std::string> expected_tags(&tag, &tag + 1); | 1333 std::set<std::string> expected_tags(&tag, &tag + 1); |
| (...skipping 739 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2063 EXPECT_TRUE(specifics2.has_header()); | 2073 EXPECT_TRUE(specifics2.has_header()); |
| 2064 const sync_pb::SessionHeader& header_s2 = specifics2.header(); | 2074 const sync_pb::SessionHeader& header_s2 = specifics2.header(); |
| 2065 EXPECT_EQ(1, header_s2.window_size()); | 2075 EXPECT_EQ(1, header_s2.window_size()); |
| 2066 | 2076 |
| 2067 // Verify TabLinks. | 2077 // Verify TabLinks. |
| 2068 SessionsSyncManager::TabLinksMap tab_map = manager()->local_tab_map_; | 2078 SessionsSyncManager::TabLinksMap tab_map = manager()->local_tab_map_; |
| 2069 ASSERT_EQ(2U, tab_map.size()); | 2079 ASSERT_EQ(2U, tab_map.size()); |
| 2070 // Tabs are ordered by sessionid in tab_map, so should be able to traverse | 2080 // Tabs are ordered by sessionid in tab_map, so should be able to traverse |
| 2071 // the tree based on order of tabs created | 2081 // the tree based on order of tabs created |
| 2072 SessionsSyncManager::TabLinksMap::iterator iter = tab_map.begin(); | 2082 SessionsSyncManager::TabLinksMap::iterator iter = tab_map.begin(); |
| 2073 ASSERT_EQ(2, iter->second->tab()->GetEntryCount()); | 2083 EXPECT_EQ(GURL("http://foo2"), iter->second->url()); |
| 2074 EXPECT_EQ(GURL("http://foo1"), iter->second->tab()->GetVirtualURLAtIndex(0)); | |
| 2075 EXPECT_EQ(GURL("http://foo2"), iter->second->tab()->GetVirtualURLAtIndex(1)); | |
| 2076 iter++; | 2084 iter++; |
| 2077 ASSERT_EQ(2, iter->second->tab()->GetEntryCount()); | 2085 EXPECT_EQ(GURL("http://bar2"), iter->second->url()); |
| 2078 EXPECT_EQ(GURL("http://bar1"), iter->second->tab()->GetVirtualURLAtIndex(0)); | |
| 2079 EXPECT_EQ(GURL("http://bar2"), iter->second->tab()->GetVirtualURLAtIndex(1)); | |
| 2080 } | 2086 } |
| 2081 | 2087 |
| 2082 TEST_F(SessionsSyncManagerTest, ForeignSessionModifiedTime) { | 2088 TEST_F(SessionsSyncManagerTest, ForeignSessionModifiedTime) { |
| 2083 syncer::SyncDataList foreign_data; | 2089 syncer::SyncDataList foreign_data; |
| 2084 base::Time newest_time = base::Time::Now() - base::TimeDelta::FromDays(1); | 2090 base::Time newest_time = base::Time::Now() - base::TimeDelta::FromDays(1); |
| 2085 base::Time middle_time = base::Time::Now() - base::TimeDelta::FromDays(2); | 2091 base::Time middle_time = base::Time::Now() - base::TimeDelta::FromDays(2); |
| 2086 base::Time oldest_time = base::Time::Now() - base::TimeDelta::FromDays(3); | 2092 base::Time oldest_time = base::Time::Now() - base::TimeDelta::FromDays(3); |
| 2087 | 2093 |
| 2088 { | 2094 { |
| 2089 std::string session_tag = "tag1"; | 2095 std::string session_tag = "tag1"; |
| (...skipping 513 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2603 base::Time last_time; | 2609 base::Time last_time; |
| 2604 for (size_t i = 0; i < tabs.size(); ++i) { | 2610 for (size_t i = 0; i < tabs.size(); ++i) { |
| 2605 base::Time this_time = tabs[i]->timestamp; | 2611 base::Time this_time = tabs[i]->timestamp; |
| 2606 if (i > 0) | 2612 if (i > 0) |
| 2607 ASSERT_GE(last_time, this_time); | 2613 ASSERT_GE(last_time, this_time); |
| 2608 last_time = tabs[i]->timestamp; | 2614 last_time = tabs[i]->timestamp; |
| 2609 } | 2615 } |
| 2610 } | 2616 } |
| 2611 | 2617 |
| 2612 } // namespace sync_sessions | 2618 } // namespace sync_sessions |
| OLD | NEW |