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