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