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 "chrome/browser/sync/sessions/sessions_sync_manager.h" | 5 #include "chrome/browser/sync/sessions/sessions_sync_manager.h" |
6 | 6 |
7 #include "chrome/browser/chrome_notification_types.h" | 7 #include "chrome/browser/chrome_notification_types.h" |
8 #include "chrome/browser/profiles/profile.h" | 8 #include "chrome/browser/profiles/profile.h" |
9 #include "chrome/browser/sync/glue/local_device_info_provider.h" | |
10 #include "chrome/browser/sync/glue/synced_tab_delegate.h" | 9 #include "chrome/browser/sync/glue/synced_tab_delegate.h" |
11 #include "chrome/browser/sync/glue/synced_window_delegate.h" | 10 #include "chrome/browser/sync/glue/synced_window_delegate.h" |
12 #include "chrome/browser/sync/sessions/sessions_util.h" | 11 #include "chrome/browser/sync/sessions/sessions_util.h" |
13 #include "chrome/browser/sync/sessions/synced_window_delegates_getter.h" | 12 #include "chrome/browser/sync/sessions/synced_window_delegates_getter.h" |
14 #include "chrome/common/url_constants.h" | 13 #include "chrome/common/url_constants.h" |
15 #include "content/public/browser/favicon_status.h" | 14 #include "content/public/browser/favicon_status.h" |
16 #include "content/public/browser/navigation_entry.h" | 15 #include "content/public/browser/navigation_entry.h" |
17 #include "content/public/browser/notification_details.h" | 16 #include "content/public/browser/notification_details.h" |
18 #include "content/public/browser/notification_service.h" | 17 #include "content/public/browser/notification_service.h" |
19 #include "content/public/browser/notification_source.h" | 18 #include "content/public/browser/notification_source.h" |
(...skipping 18 matching lines...) Expand all Loading... |
38 static const int kMaxSyncNavigationCount = 6; | 37 static const int kMaxSyncNavigationCount = 6; |
39 | 38 |
40 // The URL at which the set of synced tabs is displayed. We treat it differently | 39 // The URL at which the set of synced tabs is displayed. We treat it differently |
41 // from all other URL's as accessing it triggers a sync refresh of Sessions. | 40 // from all other URL's as accessing it triggers a sync refresh of Sessions. |
42 static const char kNTPOpenTabSyncURL[] = "chrome://newtab/#open_tabs"; | 41 static const char kNTPOpenTabSyncURL[] = "chrome://newtab/#open_tabs"; |
43 | 42 |
44 // Default number of days without activity after which a session is considered | 43 // Default number of days without activity after which a session is considered |
45 // stale and becomes a candidate for garbage collection. | 44 // stale and becomes a candidate for garbage collection. |
46 static const size_t kDefaultStaleSessionThresholdDays = 14; // 2 weeks. | 45 static const size_t kDefaultStaleSessionThresholdDays = 14; // 2 weeks. |
47 | 46 |
48 // |local_device| is owned by ProfileSyncService, its lifetime exceeds | |
49 // lifetime of SessionSyncManager. | |
50 SessionsSyncManager::SessionsSyncManager( | 47 SessionsSyncManager::SessionsSyncManager( |
51 Profile* profile, | 48 Profile* profile, |
52 LocalDeviceInfoProvider* local_device, | 49 SyncInternalApiDelegate* delegate, |
53 scoped_ptr<LocalSessionEventRouter> router) | 50 scoped_ptr<LocalSessionEventRouter> router) |
54 : favicon_cache_(profile, kMaxSyncFavicons), | 51 : favicon_cache_(profile, kMaxSyncFavicons), |
55 local_tab_pool_out_of_sync_(true), | 52 local_tab_pool_out_of_sync_(true), |
56 sync_prefs_(profile->GetPrefs()), | 53 sync_prefs_(profile->GetPrefs()), |
57 profile_(profile), | 54 profile_(profile), |
58 local_device_(local_device), | 55 delegate_(delegate), |
59 local_session_header_node_id_(TabNodePool::kInvalidTabNodeID), | 56 local_session_header_node_id_(TabNodePool::kInvalidTabNodeID), |
60 stale_session_threshold_days_(kDefaultStaleSessionThresholdDays), | 57 stale_session_threshold_days_(kDefaultStaleSessionThresholdDays), |
61 local_event_router_(router.Pass()), | 58 local_event_router_(router.Pass()), |
62 synced_window_getter_(new SyncedWindowDelegatesGetter()) { | 59 synced_window_getter_(new SyncedWindowDelegatesGetter()) { |
63 } | 60 } |
64 | 61 |
65 LocalSessionEventRouter::~LocalSessionEventRouter() {} | 62 LocalSessionEventRouter::~LocalSessionEventRouter() {} |
66 | 63 |
67 SessionsSyncManager::~SessionsSyncManager() { | 64 SessionsSyncManager::~SessionsSyncManager() { |
68 } | 65 } |
(...skipping 12 matching lines...) Expand all Loading... |
81 scoped_ptr<syncer::SyncChangeProcessor> sync_processor, | 78 scoped_ptr<syncer::SyncChangeProcessor> sync_processor, |
82 scoped_ptr<syncer::SyncErrorFactory> error_handler) { | 79 scoped_ptr<syncer::SyncErrorFactory> error_handler) { |
83 syncer::SyncMergeResult merge_result(type); | 80 syncer::SyncMergeResult merge_result(type); |
84 DCHECK(session_tracker_.Empty()); | 81 DCHECK(session_tracker_.Empty()); |
85 DCHECK_EQ(0U, local_tab_pool_.Capacity()); | 82 DCHECK_EQ(0U, local_tab_pool_.Capacity()); |
86 | 83 |
87 error_handler_ = error_handler.Pass(); | 84 error_handler_ = error_handler.Pass(); |
88 sync_processor_ = sync_processor.Pass(); | 85 sync_processor_ = sync_processor.Pass(); |
89 | 86 |
90 local_session_header_node_id_ = TabNodePool::kInvalidTabNodeID; | 87 local_session_header_node_id_ = TabNodePool::kInvalidTabNodeID; |
| 88 scoped_ptr<DeviceInfo> local_device_info(delegate_->GetLocalDeviceInfo()); |
| 89 syncer::SyncChangeList new_changes; |
91 | 90 |
92 // Make sure we have a machine tag. We do this now (versus earlier) as it's | 91 // Make sure we have a machine tag. We do this now (versus earlier) as it's |
93 // a conveniently safe time to assert sync is ready and the cache_guid is | 92 // a conveniently safe time to assert sync is ready and the cache_guid is |
94 // initialized. | 93 // initialized. |
95 if (current_machine_tag_.empty()) { | 94 if (current_machine_tag_.empty()) |
96 InitializeCurrentMachineTag(); | 95 InitializeCurrentMachineTag(); |
97 } | |
98 | |
99 // SessionDataTypeController ensures that the local device info | |
100 // is available before activating this datatype. | |
101 DCHECK(local_device_); | |
102 const DeviceInfo* local_device_info = local_device_->GetLocalDeviceInfo(); | |
103 if (local_device_info) { | 96 if (local_device_info) { |
104 current_session_name_ = local_device_info->client_name(); | 97 current_session_name_ = local_device_info->client_name(); |
105 } else { | 98 } else { |
106 merge_result.set_error(error_handler_->CreateAndUploadError( | 99 merge_result.set_error(error_handler_->CreateAndUploadError( |
107 FROM_HERE, | 100 FROM_HERE, |
108 "Failed to get local device info.")); | 101 "Failed to get device info for machine tag.")); |
109 return merge_result; | 102 return merge_result; |
110 } | 103 } |
111 | |
112 session_tracker_.SetLocalSessionTag(current_machine_tag_); | 104 session_tracker_.SetLocalSessionTag(current_machine_tag_); |
113 | 105 |
114 syncer::SyncChangeList new_changes; | |
115 | |
116 // First, we iterate over sync data to update our session_tracker_. | 106 // First, we iterate over sync data to update our session_tracker_. |
117 syncer::SyncDataList restored_tabs; | 107 syncer::SyncDataList restored_tabs; |
118 if (!InitFromSyncModel(initial_sync_data, &restored_tabs, &new_changes)) { | 108 if (!InitFromSyncModel(initial_sync_data, &restored_tabs, &new_changes)) { |
119 // The sync db didn't have a header node for us. Create one. | 109 // The sync db didn't have a header node for us. Create one. |
120 sync_pb::EntitySpecifics specifics; | 110 sync_pb::EntitySpecifics specifics; |
121 sync_pb::SessionSpecifics* base_specifics = specifics.mutable_session(); | 111 sync_pb::SessionSpecifics* base_specifics = specifics.mutable_session(); |
122 base_specifics->set_session_tag(current_machine_tag()); | 112 base_specifics->set_session_tag(current_machine_tag()); |
123 sync_pb::SessionHeader* header_s = base_specifics->mutable_header(); | 113 sync_pb::SessionHeader* header_s = base_specifics->mutable_header(); |
124 header_s->set_client_name(current_session_name_); | 114 header_s->set_client_name(current_session_name_); |
125 header_s->set_device_type(DeviceInfo::GetLocalDeviceType()); | 115 header_s->set_device_type(DeviceInfo::GetLocalDeviceType()); |
126 syncer::SyncData data = syncer::SyncData::CreateLocalData( | 116 syncer::SyncData data = syncer::SyncData::CreateLocalData( |
127 current_machine_tag(), current_session_name_, specifics); | 117 current_machine_tag(), current_session_name_, specifics); |
128 new_changes.push_back(syncer::SyncChange( | 118 new_changes.push_back(syncer::SyncChange( |
129 FROM_HERE, syncer::SyncChange::ACTION_ADD, data)); | 119 FROM_HERE, syncer::SyncChange::ACTION_ADD, data)); |
130 } | 120 } |
131 | 121 |
132 #if defined(OS_ANDROID) | 122 #if defined(OS_ANDROID) |
133 std::string sync_machine_tag(BuildMachineTag( | 123 std::string sync_machine_tag(BuildMachineTag( |
134 local_device_->GetLocalSyncCacheGUID())); | 124 delegate_->GetLocalSyncCacheGUID())); |
135 if (current_machine_tag_.compare(sync_machine_tag) != 0) | 125 if (current_machine_tag_.compare(sync_machine_tag) != 0) |
136 DeleteForeignSessionInternal(sync_machine_tag, &new_changes); | 126 DeleteForeignSessionInternal(sync_machine_tag, &new_changes); |
137 #endif | 127 #endif |
138 | 128 |
139 // Check if anything has changed on the local client side. | 129 // Check if anything has changed on the local client side. |
140 AssociateWindows(RELOAD_TABS, restored_tabs, &new_changes); | 130 AssociateWindows(RELOAD_TABS, restored_tabs, &new_changes); |
141 local_tab_pool_out_of_sync_ = false; | 131 local_tab_pool_out_of_sync_ = false; |
142 | 132 |
143 merge_result.set_error( | 133 merge_result.set_error( |
144 sync_processor_->ProcessSyncChanges(FROM_HERE, new_changes)); | 134 sync_processor_->ProcessSyncChanges(FROM_HERE, new_changes)); |
(...skipping 497 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
642 } | 632 } |
643 | 633 |
644 void SessionsSyncManager::InitializeCurrentMachineTag() { | 634 void SessionsSyncManager::InitializeCurrentMachineTag() { |
645 DCHECK(current_machine_tag_.empty()); | 635 DCHECK(current_machine_tag_.empty()); |
646 std::string persisted_guid; | 636 std::string persisted_guid; |
647 persisted_guid = sync_prefs_.GetSyncSessionsGUID(); | 637 persisted_guid = sync_prefs_.GetSyncSessionsGUID(); |
648 if (!persisted_guid.empty()) { | 638 if (!persisted_guid.empty()) { |
649 current_machine_tag_ = persisted_guid; | 639 current_machine_tag_ = persisted_guid; |
650 DVLOG(1) << "Restoring persisted session sync guid: " << persisted_guid; | 640 DVLOG(1) << "Restoring persisted session sync guid: " << persisted_guid; |
651 } else { | 641 } else { |
652 DCHECK(local_device_); | 642 current_machine_tag_ = BuildMachineTag(delegate_->GetLocalSyncCacheGUID()); |
653 std::string cache_guid = local_device_->GetLocalSyncCacheGUID(); | |
654 DCHECK(!cache_guid.empty()); | |
655 current_machine_tag_ = BuildMachineTag(cache_guid); | |
656 DVLOG(1) << "Creating session sync guid: " << current_machine_tag_; | 643 DVLOG(1) << "Creating session sync guid: " << current_machine_tag_; |
657 sync_prefs_.SetSyncSessionsGUID(current_machine_tag_); | 644 sync_prefs_.SetSyncSessionsGUID(current_machine_tag_); |
658 } | 645 } |
659 | 646 |
660 local_tab_pool_.SetMachineTag(current_machine_tag_); | 647 local_tab_pool_.SetMachineTag(current_machine_tag_); |
661 } | 648 } |
662 | 649 |
663 // static | 650 // static |
664 void SessionsSyncManager::PopulateSessionHeaderFromSpecifics( | 651 void SessionsSyncManager::PopulateSessionHeaderFromSpecifics( |
665 const sync_pb::SessionHeader& header_specifics, | 652 const sync_pb::SessionHeader& header_specifics, |
(...skipping 294 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
960 // TODO(bauerb): Add categories | 947 // TODO(bauerb): Add categories |
961 } | 948 } |
962 } | 949 } |
963 session_tab->session_storage_persistent_id.clear(); | 950 session_tab->session_storage_persistent_id.clear(); |
964 } | 951 } |
965 | 952 |
966 FaviconCache* SessionsSyncManager::GetFaviconCache() { | 953 FaviconCache* SessionsSyncManager::GetFaviconCache() { |
967 return &favicon_cache_; | 954 return &favicon_cache_; |
968 } | 955 } |
969 | 956 |
970 SyncedWindowDelegatesGetter* | |
971 SessionsSyncManager::GetSyncedWindowDelegatesGetter() const { | |
972 return synced_window_getter_.get(); | |
973 } | |
974 | |
975 void SessionsSyncManager::DoGarbageCollection() { | 957 void SessionsSyncManager::DoGarbageCollection() { |
976 std::vector<const SyncedSession*> sessions; | 958 std::vector<const SyncedSession*> sessions; |
977 if (!GetAllForeignSessions(&sessions)) | 959 if (!GetAllForeignSessions(&sessions)) |
978 return; // No foreign sessions. | 960 return; // No foreign sessions. |
979 | 961 |
980 // Iterate through all the sessions and delete any with age older than | 962 // Iterate through all the sessions and delete any with age older than |
981 // |stale_session_threshold_days_|. | 963 // |stale_session_threshold_days_|. |
982 syncer::SyncChangeList changes; | 964 syncer::SyncChangeList changes; |
983 for (std::vector<const SyncedSession*>::const_iterator iter = | 965 for (std::vector<const SyncedSession*>::const_iterator iter = |
984 sessions.begin(); iter != sessions.end(); ++iter) { | 966 sessions.begin(); iter != sessions.end(); ++iter) { |
985 const SyncedSession* session = *iter; | 967 const SyncedSession* session = *iter; |
986 int session_age_in_days = | 968 int session_age_in_days = |
987 (base::Time::Now() - session->modified_time).InDays(); | 969 (base::Time::Now() - session->modified_time).InDays(); |
988 std::string session_tag = session->session_tag; | 970 std::string session_tag = session->session_tag; |
989 if (session_age_in_days > 0 && // If false, local clock is not trustworty. | 971 if (session_age_in_days > 0 && // If false, local clock is not trustworty. |
990 static_cast<size_t>(session_age_in_days) > | 972 static_cast<size_t>(session_age_in_days) > |
991 stale_session_threshold_days_) { | 973 stale_session_threshold_days_) { |
992 DVLOG(1) << "Found stale session " << session_tag | 974 DVLOG(1) << "Found stale session " << session_tag |
993 << " with age " << session_age_in_days << ", deleting."; | 975 << " with age " << session_age_in_days << ", deleting."; |
994 DeleteForeignSessionInternal(session_tag, &changes); | 976 DeleteForeignSessionInternal(session_tag, &changes); |
995 } | 977 } |
996 } | 978 } |
997 | 979 |
998 if (!changes.empty()) | 980 if (!changes.empty()) |
999 sync_processor_->ProcessSyncChanges(FROM_HERE, changes); | 981 sync_processor_->ProcessSyncChanges(FROM_HERE, changes); |
1000 } | 982 } |
1001 | 983 |
1002 }; // namespace browser_sync | 984 }; // namespace browser_sync |
OLD | NEW |