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 <algorithm> | 7 #include <algorithm> |
8 | 8 |
9 #include "base/metrics/field_trial.h" | 9 #include "base/metrics/field_trial.h" |
10 #include "chrome/browser/bookmarks/bookmark_model_factory.h" | 10 #include "chrome/browser/bookmarks/bookmark_model_factory.h" |
11 #include "chrome/browser/chrome_notification_types.h" | 11 #include "chrome/browser/chrome_notification_types.h" |
12 #include "chrome/browser/favicon/favicon_service_factory.h" | 12 #include "chrome/browser/favicon/favicon_service_factory.h" |
13 #include "chrome/browser/history/history_service_factory.h" | 13 #include "chrome/browser/history/history_service_factory.h" |
14 #include "chrome/browser/profiles/profile.h" | 14 #include "chrome/browser/profiles/profile.h" |
15 #include "chrome/browser/search/search.h" | 15 #include "chrome/browser/search/search.h" |
16 #include "chrome/browser/sync/glue/synced_tab_delegate.h" | |
17 #include "chrome/common/url_constants.h" | |
18 #include "components/sessions/content/content_serialized_navigation_builder.h" | |
19 #include "components/sync_driver/glue/synced_window_delegate.h" | 16 #include "components/sync_driver/glue/synced_window_delegate.h" |
20 #include "components/sync_driver/local_device_info_provider.h" | 17 #include "components/sync_driver/local_device_info_provider.h" |
21 #include "components/sync_driver/sessions/synced_window_delegates_getter.h" | 18 #include "components/sync_driver/sessions/synced_window_delegates_getter.h" |
22 #include "content/public/browser/favicon_status.h" | 19 #include "components/sync_sessions/sync_sessions_client.h" |
23 #include "content/public/browser/navigation_entry.h" | 20 #include "components/sync_sessions/synced_tab_delegate.h" |
24 #include "content/public/browser/notification_details.h" | 21 #include "content/public/browser/notification_details.h" |
25 #include "content/public/browser/notification_service.h" | 22 #include "content/public/browser/notification_service.h" |
26 #include "content/public/browser/notification_source.h" | 23 #include "content/public/browser/notification_source.h" |
27 #include "content/public/common/url_constants.h" | |
28 #include "sync/api/sync_error.h" | 24 #include "sync/api/sync_error.h" |
29 #include "sync/api/sync_error_factory.h" | 25 #include "sync/api/sync_error_factory.h" |
30 #include "sync/api/sync_merge_result.h" | 26 #include "sync/api/sync_merge_result.h" |
31 #include "sync/api/time.h" | 27 #include "sync/api/time.h" |
32 | 28 |
33 using content::NavigationEntry; | |
34 using sessions::ContentSerializedNavigationBuilder; | |
35 using sessions::SerializedNavigationEntry; | 29 using sessions::SerializedNavigationEntry; |
36 using sync_driver::DeviceInfo; | 30 using sync_driver::DeviceInfo; |
37 using sync_driver::LocalDeviceInfoProvider; | 31 using sync_driver::LocalDeviceInfoProvider; |
38 using syncer::SyncChange; | 32 using syncer::SyncChange; |
39 using syncer::SyncData; | 33 using syncer::SyncData; |
40 | 34 |
41 namespace browser_sync { | 35 namespace browser_sync { |
42 | 36 |
43 namespace { | 37 namespace { |
44 | 38 |
(...skipping 24 matching lines...) Expand all Loading... |
69 bool SessionsRecencyComparator(const sync_driver::SyncedSession* s1, | 63 bool SessionsRecencyComparator(const sync_driver::SyncedSession* s1, |
70 const sync_driver::SyncedSession* s2) { | 64 const sync_driver::SyncedSession* s2) { |
71 return s1->modified_time > s2->modified_time; | 65 return s1->modified_time > s2->modified_time; |
72 } | 66 } |
73 | 67 |
74 } // namespace | 68 } // namespace |
75 | 69 |
76 // |local_device| is owned by ProfileSyncService, its lifetime exceeds | 70 // |local_device| is owned by ProfileSyncService, its lifetime exceeds |
77 // lifetime of SessionSyncManager. | 71 // lifetime of SessionSyncManager. |
78 SessionsSyncManager::SessionsSyncManager( | 72 SessionsSyncManager::SessionsSyncManager( |
| 73 sync_sessions::SyncSessionsClient* sessions_client, |
79 Profile* profile, | 74 Profile* profile, |
80 LocalDeviceInfoProvider* local_device, | 75 LocalDeviceInfoProvider* local_device, |
81 scoped_ptr<LocalSessionEventRouter> router, | 76 scoped_ptr<LocalSessionEventRouter> router) |
82 scoped_ptr<SyncedWindowDelegatesGetter> synced_window_getter) | 77 : sessions_client_(sessions_client), |
83 : favicon_cache_(FaviconServiceFactory::GetForProfile( | 78 session_tracker_(sessions_client), |
| 79 favicon_cache_(FaviconServiceFactory::GetForProfile( |
84 profile, | 80 profile, |
85 ServiceAccessType::EXPLICIT_ACCESS), | 81 ServiceAccessType::EXPLICIT_ACCESS), |
86 HistoryServiceFactory::GetForProfile( | 82 HistoryServiceFactory::GetForProfile( |
87 profile, | 83 profile, |
88 ServiceAccessType::EXPLICIT_ACCESS), | 84 ServiceAccessType::EXPLICIT_ACCESS), |
89 kMaxSyncFavicons), | 85 kMaxSyncFavicons), |
90 local_tab_pool_out_of_sync_(true), | 86 local_tab_pool_out_of_sync_(true), |
91 sync_prefs_(profile->GetPrefs()), | 87 sync_prefs_(profile->GetPrefs()), |
92 profile_(profile), | 88 profile_(profile), |
93 local_device_(local_device), | 89 local_device_(local_device), |
94 local_session_header_node_id_(TabNodePool::kInvalidTabNodeID), | 90 local_session_header_node_id_(TabNodePool::kInvalidTabNodeID), |
95 stale_session_threshold_days_(kDefaultStaleSessionThresholdDays), | 91 stale_session_threshold_days_(kDefaultStaleSessionThresholdDays), |
96 local_event_router_(router.Pass()), | 92 local_event_router_(router.Pass()), |
97 synced_window_getter_(synced_window_getter.Pass()), | |
98 page_revisit_broadcaster_(this, | 93 page_revisit_broadcaster_(this, |
| 94 sessions_client, |
99 HistoryServiceFactory::GetForProfile( | 95 HistoryServiceFactory::GetForProfile( |
100 profile, | 96 profile, |
101 ServiceAccessType::EXPLICIT_ACCESS), | 97 ServiceAccessType::EXPLICIT_ACCESS), |
102 BookmarkModelFactory::GetForProfile(profile)) {} | 98 BookmarkModelFactory::GetForProfile(profile)) { |
| 99 synced_window_getter_ = sessions_client_->GetSyncedWindowDelegatesGetter(); |
| 100 } |
103 | 101 |
104 LocalSessionEventRouter::~LocalSessionEventRouter() {} | 102 LocalSessionEventRouter::~LocalSessionEventRouter() {} |
105 | 103 |
106 SessionsSyncManager::~SessionsSyncManager() { | 104 SessionsSyncManager::~SessionsSyncManager() { |
107 } | 105 } |
108 | 106 |
109 // Returns the GUID-based string that should be used for | 107 // Returns the GUID-based string that should be used for |
110 // |SessionsSyncManager::current_machine_tag_|. | 108 // |SessionsSyncManager::current_machine_tag_|. |
111 static std::string BuildMachineTag(const std::string& cache_guid) { | 109 static std::string BuildMachineTag(const std::string& cache_guid) { |
112 std::string machine_tag = "session_sync"; | 110 std::string machine_tag = "session_sync"; |
(...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
199 current_session->modified_time = base::Time::Now(); | 197 current_session->modified_time = base::Time::Now(); |
200 header_s->set_client_name(current_session_name_); | 198 header_s->set_client_name(current_session_name_); |
201 // SessionDataTypeController ensures that the local device info | 199 // SessionDataTypeController ensures that the local device info |
202 // is available before activating this datatype. | 200 // is available before activating this datatype. |
203 DCHECK(local_device_); | 201 DCHECK(local_device_); |
204 const DeviceInfo* local_device_info = local_device_->GetLocalDeviceInfo(); | 202 const DeviceInfo* local_device_info = local_device_->GetLocalDeviceInfo(); |
205 header_s->set_device_type(local_device_info->device_type()); | 203 header_s->set_device_type(local_device_info->device_type()); |
206 | 204 |
207 session_tracker_.ResetSessionTracking(local_tag); | 205 session_tracker_.ResetSessionTracking(local_tag); |
208 std::set<const SyncedWindowDelegate*> windows = | 206 std::set<const SyncedWindowDelegate*> windows = |
209 synced_window_getter_->GetSyncedWindowDelegates(); | 207 GetSyncedWindowDelegatesGetter()->GetSyncedWindowDelegates(); |
210 | 208 |
211 for (std::set<const SyncedWindowDelegate*>::const_iterator i = | 209 for (std::set<const SyncedWindowDelegate*>::const_iterator i = |
212 windows.begin(); i != windows.end(); ++i) { | 210 windows.begin(); i != windows.end(); ++i) { |
213 // Make sure the window has tabs and a viewable window. The viewable window | 211 // Make sure the window has tabs and a viewable window. The viewable window |
214 // check is necessary because, for example, when a browser is closed the | 212 // check is necessary because, for example, when a browser is closed the |
215 // destructor is not necessarily run immediately. This means its possible | 213 // destructor is not necessarily run immediately. This means its possible |
216 // for us to get a handle to a browser that is about to be removed. If | 214 // for us to get a handle to a browser that is about to be removed. If |
217 // the tab count is 0 or the window is NULL, the browser is about to be | 215 // the tab count is 0 or the window is NULL, the browser is about to be |
218 // deleted, so we ignore it. | 216 // deleted, so we ignore it. |
219 if ((*i)->ShouldSync() && (*i)->GetTabCount() && (*i)->HasWindow()) { | 217 if ((*i)->ShouldSync() && (*i)->GetTabCount() && (*i)->HasWindow()) { |
(...skipping 16 matching lines...) Expand all Loading... |
236 | 234 |
237 bool found_tabs = false; | 235 bool found_tabs = false; |
238 for (int j = 0; j < (*i)->GetTabCount(); ++j) { | 236 for (int j = 0; j < (*i)->GetTabCount(); ++j) { |
239 SessionID::id_type tab_id = (*i)->GetTabIdAt(j); | 237 SessionID::id_type tab_id = (*i)->GetTabIdAt(j); |
240 SyncedTabDelegate* synced_tab = (*i)->GetTabAt(j); | 238 SyncedTabDelegate* synced_tab = (*i)->GetTabAt(j); |
241 | 239 |
242 // GetTabAt can return a null tab; in that case just skip it. | 240 // GetTabAt can return a null tab; in that case just skip it. |
243 if (!synced_tab) | 241 if (!synced_tab) |
244 continue; | 242 continue; |
245 | 243 |
246 if (!synced_tab->HasWebContents()) { | 244 if (synced_tab->IsPlaceholderTab()) { |
247 // For tabs without WebContents update the |tab_id|, as it could have | 245 // For tabs without WebContents update the |tab_id|, as it could have |
248 // changed after a session restore. | 246 // changed after a session restore. |
249 // Note: We cannot check if a tab is valid if it has no WebContents. | 247 // Note: We cannot check if a tab is valid if it has no WebContents. |
250 // We assume any such tab is valid and leave the contents of | 248 // We assume any such tab is valid and leave the contents of |
251 // corresponding sync node unchanged. | 249 // corresponding sync node unchanged. |
252 if (synced_tab->GetSyncId() > TabNodePool::kInvalidTabNodeID && | 250 if (synced_tab->GetSyncId() > TabNodePool::kInvalidTabNodeID && |
253 tab_id > TabNodePool::kInvalidTabID) { | 251 tab_id > TabNodePool::kInvalidTabID) { |
254 AssociateRestoredPlaceholderTab(*synced_tab, tab_id, | 252 AssociateRestoredPlaceholderTab(*synced_tab, tab_id, |
255 restored_tabs, change_output); | 253 restored_tabs, change_output); |
256 found_tabs = true; | 254 found_tabs = true; |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
295 sync_pb::EntitySpecifics entity; | 293 sync_pb::EntitySpecifics entity; |
296 entity.mutable_session()->CopyFrom(specifics); | 294 entity.mutable_session()->CopyFrom(specifics); |
297 syncer::SyncData data = syncer::SyncData::CreateLocalData( | 295 syncer::SyncData data = syncer::SyncData::CreateLocalData( |
298 current_machine_tag(), current_session_name_, entity); | 296 current_machine_tag(), current_session_name_, entity); |
299 change_output->push_back(syncer::SyncChange( | 297 change_output->push_back(syncer::SyncChange( |
300 FROM_HERE, syncer::SyncChange::ACTION_UPDATE, data)); | 298 FROM_HERE, syncer::SyncChange::ACTION_UPDATE, data)); |
301 } | 299 } |
302 | 300 |
303 void SessionsSyncManager::AssociateTab(SyncedTabDelegate* const tab, | 301 void SessionsSyncManager::AssociateTab(SyncedTabDelegate* const tab, |
304 syncer::SyncChangeList* change_output) { | 302 syncer::SyncChangeList* change_output) { |
305 DCHECK(tab->HasWebContents()); | 303 DCHECK(!tab->IsPlaceholderTab()); |
306 SessionID::id_type tab_id = tab->GetSessionId(); | 304 SessionID::id_type tab_id = tab->GetSessionId(); |
307 if (tab->profile() != profile_) | |
308 return; | |
309 | 305 |
310 if (tab->IsBeingDestroyed()) { | 306 if (tab->IsBeingDestroyed()) { |
311 // This tab is closing. | 307 // This tab is closing. |
312 TabLinksMap::iterator tab_iter = local_tab_map_.find(tab_id); | 308 TabLinksMap::iterator tab_iter = local_tab_map_.find(tab_id); |
313 if (tab_iter == local_tab_map_.end()) { | 309 if (tab_iter == local_tab_map_.end()) { |
314 // We aren't tracking this tab (for example, sync setting page). | 310 // We aren't tracking this tab (for example, sync setting page). |
315 return; | 311 return; |
316 } | 312 } |
317 local_tab_pool_.FreeTabNode(tab_iter->second->tab_node_id(), | 313 local_tab_pool_.FreeTabNode(tab_iter->second->tab_node_id(), |
318 change_output); | 314 change_output); |
319 local_tab_map_.erase(tab_iter); | 315 local_tab_map_.erase(tab_iter); |
320 return; | 316 return; |
321 } | 317 } |
322 | 318 |
323 if (!tab->ShouldSync()) | 319 if (!tab->ShouldSync(sessions_client_)) |
324 return; | 320 return; |
325 | 321 |
326 TabLinksMap::iterator local_tab_map_iter = local_tab_map_.find(tab_id); | 322 TabLinksMap::iterator local_tab_map_iter = local_tab_map_.find(tab_id); |
327 TabLink* tab_link = NULL; | 323 TabLink* tab_link = NULL; |
328 | 324 |
329 if (local_tab_map_iter == local_tab_map_.end()) { | 325 if (local_tab_map_iter == local_tab_map_.end()) { |
330 int tab_node_id = tab->GetSyncId(); | 326 int tab_node_id = tab->GetSyncId(); |
331 // If there is an old sync node for the tab, reuse it. If this is a new | 327 // If there is an old sync node for the tab, reuse it. If this is a new |
332 // tab, get a sync node for it. | 328 // tab, get a sync node for it. |
333 if (!local_tab_pool_.IsUnassociatedTabNode(tab_node_id)) { | 329 if (!local_tab_pool_.IsUnassociatedTabNode(tab_node_id)) { |
(...skipping 19 matching lines...) Expand all Loading... |
353 sync_pb::EntitySpecifics specifics; | 349 sync_pb::EntitySpecifics specifics; |
354 LocalTabDelegateToSpecifics(*tab, specifics.mutable_session()); | 350 LocalTabDelegateToSpecifics(*tab, specifics.mutable_session()); |
355 syncer::SyncData data = syncer::SyncData::CreateLocalData( | 351 syncer::SyncData data = syncer::SyncData::CreateLocalData( |
356 TabNodePool::TabIdToTag(current_machine_tag_, | 352 TabNodePool::TabIdToTag(current_machine_tag_, |
357 tab_link->tab_node_id()), | 353 tab_link->tab_node_id()), |
358 current_session_name_, | 354 current_session_name_, |
359 specifics); | 355 specifics); |
360 change_output->push_back(syncer::SyncChange( | 356 change_output->push_back(syncer::SyncChange( |
361 FROM_HERE, syncer::SyncChange::ACTION_UPDATE, data)); | 357 FROM_HERE, syncer::SyncChange::ACTION_UPDATE, data)); |
362 | 358 |
363 const GURL new_url = GetCurrentVirtualURL(*tab); | 359 int current_index = tab->GetCurrentEntryIndex(); |
| 360 const GURL new_url = tab->GetVirtualURLAtIndex(current_index); |
364 if (new_url != tab_link->url()) { | 361 if (new_url != tab_link->url()) { |
365 tab_link->set_url(new_url); | 362 tab_link->set_url(new_url); |
366 favicon_cache_.OnFaviconVisited(new_url, GetCurrentFaviconURL(*tab)); | 363 favicon_cache_.OnFaviconVisited(new_url, |
| 364 tab->GetFaviconURLAtIndex(current_index)); |
367 page_revisit_broadcaster_.OnPageVisit( | 365 page_revisit_broadcaster_.OnPageVisit( |
368 new_url, tab->GetCurrentEntryMaybePending()->GetTransitionType()); | 366 new_url, tab->GetTransitionAtIndex(current_index)); |
369 } | 367 } |
370 | 368 |
371 session_tracker_.GetSession(current_machine_tag())->modified_time = | 369 session_tracker_.GetSession(current_machine_tag())->modified_time = |
372 base::Time::Now(); | 370 base::Time::Now(); |
373 } | 371 } |
374 | 372 |
375 void SessionsSyncManager::RebuildAssociations() { | 373 void SessionsSyncManager::RebuildAssociations() { |
376 syncer::SyncDataList data( | 374 syncer::SyncDataList data( |
377 sync_processor_->GetAllSyncData(syncer::SESSIONS)); | 375 sync_processor_->GetAllSyncData(syncer::SESSIONS)); |
378 scoped_ptr<syncer::SyncErrorFactory> error_handler(error_handler_.Pass()); | 376 scoped_ptr<syncer::SyncErrorFactory> error_handler(error_handler_.Pass()); |
(...skipping 16 matching lines...) Expand all Loading... |
395 bool success = session_tab_ids.insert(tab_id).second; | 393 bool success = session_tab_ids.insert(tab_id).second; |
396 if (!success) | 394 if (!success) |
397 return false; | 395 return false; |
398 } | 396 } |
399 } | 397 } |
400 | 398 |
401 return true; | 399 return true; |
402 } | 400 } |
403 | 401 |
404 void SessionsSyncManager::OnLocalTabModified(SyncedTabDelegate* modified_tab) { | 402 void SessionsSyncManager::OnLocalTabModified(SyncedTabDelegate* modified_tab) { |
405 const content::NavigationEntry* entry = modified_tab->GetActiveEntry(); | 403 GURL virtual_url = |
406 if (!modified_tab->IsBeingDestroyed() && | 404 modified_tab->GetVirtualURLAtIndex(modified_tab->GetCurrentEntryIndex()); |
407 entry && | 405 if (!modified_tab->IsBeingDestroyed() && virtual_url.is_valid() && |
408 entry->GetVirtualURL().is_valid() && | 406 virtual_url.spec() == kNTPOpenTabSyncURL) { |
409 entry->GetVirtualURL().spec() == kNTPOpenTabSyncURL) { | |
410 DVLOG(1) << "Triggering sync refresh for sessions datatype."; | 407 DVLOG(1) << "Triggering sync refresh for sessions datatype."; |
411 const syncer::ModelTypeSet types(syncer::SESSIONS); | 408 const syncer::ModelTypeSet types(syncer::SESSIONS); |
412 content::NotificationService::current()->Notify( | 409 content::NotificationService::current()->Notify( |
413 chrome::NOTIFICATION_SYNC_REFRESH_LOCAL, | 410 chrome::NOTIFICATION_SYNC_REFRESH_LOCAL, |
414 content::Source<Profile>(profile_), | 411 content::Source<Profile>(profile_), |
415 content::Details<const syncer::ModelTypeSet>(&types)); | 412 content::Details<const syncer::ModelTypeSet>(&types)); |
416 } | 413 } |
417 | 414 |
418 if (local_tab_pool_out_of_sync_) { | 415 if (local_tab_pool_out_of_sync_) { |
419 // If our tab pool is corrupt, pay the price of a full re-association to | 416 // If our tab pool is corrupt, pay the price of a full re-association to |
420 // fix things up. This takes care of the new tab modification as well. | 417 // fix things up. This takes care of the new tab modification as well. |
421 RebuildAssociations(); | 418 RebuildAssociations(); |
422 DCHECK(!local_tab_pool_out_of_sync_); | 419 DCHECK(!local_tab_pool_out_of_sync_); |
423 return; | 420 return; |
424 } | 421 } |
425 | 422 |
426 syncer::SyncChangeList changes; | 423 syncer::SyncChangeList changes; |
427 // Associate tabs first so the synced session tracker is aware of them. | |
428 AssociateTab(modified_tab, &changes); | 424 AssociateTab(modified_tab, &changes); |
429 // Note, we always associate windows because it's possible a tab became | 425 // Note, we always associate windows because it's possible a tab became |
430 // "interesting" by going to a valid URL, in which case it needs to be added | 426 // "interesting" by going to a valid URL, in which case it needs to be added |
431 // to the window's tab information. | 427 // to the window's tab information. Similarly, if a tab became |
| 428 // "uninteresting", we remove it from the window's tab information. |
432 AssociateWindows(DONT_RELOAD_TABS, syncer::SyncDataList(), &changes); | 429 AssociateWindows(DONT_RELOAD_TABS, syncer::SyncDataList(), &changes); |
433 sync_processor_->ProcessSyncChanges(FROM_HERE, changes); | 430 sync_processor_->ProcessSyncChanges(FROM_HERE, changes); |
434 } | 431 } |
435 | 432 |
436 void SessionsSyncManager::OnFaviconsChanged( | 433 void SessionsSyncManager::OnFaviconsChanged( |
437 const std::set<GURL>& page_urls, | 434 const std::set<GURL>& page_urls, |
438 const GURL& /* icon_url */) { | 435 const GURL& /* icon_url */) { |
439 // TODO(zea): consider a separate container for tabs with outstanding favicon | 436 // TODO(zea): consider a separate container for tabs with outstanding favicon |
440 // loads so we don't have to iterate through all tabs comparing urls. | 437 // loads so we don't have to iterate through all tabs comparing urls. |
441 for (const GURL& page_url : page_urls) { | 438 for (const GURL& page_url : page_urls) { |
(...skipping 442 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
884 const std::string& foreign_session_tag) { | 881 const std::string& foreign_session_tag) { |
885 if (foreign_session_tag == current_machine_tag()) { | 882 if (foreign_session_tag == current_machine_tag()) { |
886 DVLOG(1) << "Local session deleted! Doing nothing until a navigation is " | 883 DVLOG(1) << "Local session deleted! Doing nothing until a navigation is " |
887 << "triggered."; | 884 << "triggered."; |
888 return false; | 885 return false; |
889 } | 886 } |
890 DVLOG(1) << "Disassociating session " << foreign_session_tag; | 887 DVLOG(1) << "Disassociating session " << foreign_session_tag; |
891 return session_tracker_.DeleteSession(foreign_session_tag); | 888 return session_tracker_.DeleteSession(foreign_session_tag); |
892 } | 889 } |
893 | 890 |
894 // static | |
895 GURL SessionsSyncManager::GetCurrentVirtualURL( | |
896 const SyncedTabDelegate& tab_delegate) { | |
897 const NavigationEntry* current_entry = | |
898 tab_delegate.GetCurrentEntryMaybePending(); | |
899 return current_entry->GetVirtualURL(); | |
900 } | |
901 | |
902 // static | |
903 GURL SessionsSyncManager::GetCurrentFaviconURL( | |
904 const SyncedTabDelegate& tab_delegate) { | |
905 const NavigationEntry* current_entry = | |
906 tab_delegate.GetCurrentEntryMaybePending(); | |
907 return (current_entry->GetFavicon().valid ? | |
908 current_entry->GetFavicon().url : | |
909 GURL()); | |
910 } | |
911 | |
912 bool SessionsSyncManager::GetForeignSession( | 891 bool SessionsSyncManager::GetForeignSession( |
913 const std::string& tag, | 892 const std::string& tag, |
914 std::vector<const sessions::SessionWindow*>* windows) { | 893 std::vector<const sessions::SessionWindow*>* windows) { |
915 return session_tracker_.LookupSessionWindows(tag, windows); | 894 return session_tracker_.LookupSessionWindows(tag, windows); |
916 } | 895 } |
917 | 896 |
918 bool SessionsSyncManager::GetForeignSessionTabs( | 897 bool SessionsSyncManager::GetForeignSessionTabs( |
919 const std::string& tag, | 898 const std::string& tag, |
920 std::vector<const sessions::SessionTab*>* tabs) { | 899 std::vector<const sessions::SessionTab*>* tabs) { |
921 std::vector<const sessions::SessionWindow*> windows; | 900 std::vector<const sessions::SessionWindow*> windows; |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
957 } | 936 } |
958 | 937 |
959 void SessionsSyncManager::LocalTabDelegateToSpecifics( | 938 void SessionsSyncManager::LocalTabDelegateToSpecifics( |
960 const SyncedTabDelegate& tab_delegate, | 939 const SyncedTabDelegate& tab_delegate, |
961 sync_pb::SessionSpecifics* specifics) { | 940 sync_pb::SessionSpecifics* specifics) { |
962 sessions::SessionTab* session_tab = NULL; | 941 sessions::SessionTab* session_tab = NULL; |
963 session_tab = | 942 session_tab = |
964 session_tracker_.GetTab(current_machine_tag(), | 943 session_tracker_.GetTab(current_machine_tag(), |
965 tab_delegate.GetSessionId(), | 944 tab_delegate.GetSessionId(), |
966 tab_delegate.GetSyncId()); | 945 tab_delegate.GetSyncId()); |
967 SetSessionTabFromDelegate(tab_delegate, base::Time::Now(), session_tab); | 946 SetSessionTabFromDelegate(GetSyncedWindowDelegatesGetter(), tab_delegate, |
| 947 base::Time::Now(), session_tab); |
968 SetVariationIds(session_tab); | 948 SetVariationIds(session_tab); |
969 sync_pb::SessionTab tab_s = session_tab->ToSyncData(); | 949 sync_pb::SessionTab tab_s = session_tab->ToSyncData(); |
970 specifics->set_session_tag(current_machine_tag_); | 950 specifics->set_session_tag(current_machine_tag_); |
971 specifics->set_tab_node_id(tab_delegate.GetSyncId()); | 951 specifics->set_tab_node_id(tab_delegate.GetSyncId()); |
972 specifics->mutable_tab()->CopyFrom(tab_s); | 952 specifics->mutable_tab()->CopyFrom(tab_s); |
973 } | 953 } |
974 | 954 |
975 void SessionsSyncManager::AssociateRestoredPlaceholderTab( | 955 void SessionsSyncManager::AssociateRestoredPlaceholderTab( |
976 const SyncedTabDelegate& tab_delegate, | 956 const SyncedTabDelegate& tab_delegate, |
977 SessionID::id_type new_tab_id, | 957 SessionID::id_type new_tab_id, |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1015 current_session_name_, | 995 current_session_name_, |
1016 entity); | 996 entity); |
1017 change_output->push_back(syncer::SyncChange( | 997 change_output->push_back(syncer::SyncChange( |
1018 FROM_HERE, syncer::SyncChange::ACTION_UPDATE, data)); | 998 FROM_HERE, syncer::SyncChange::ACTION_UPDATE, data)); |
1019 return; | 999 return; |
1020 } | 1000 } |
1021 } | 1001 } |
1022 | 1002 |
1023 // static | 1003 // static |
1024 void SessionsSyncManager::SetSessionTabFromDelegate( | 1004 void SessionsSyncManager::SetSessionTabFromDelegate( |
1025 const SyncedTabDelegate& tab_delegate, | 1005 SyncedWindowDelegatesGetter* synced_window_getter, |
1026 base::Time mtime, | 1006 const SyncedTabDelegate& tab_delegate, |
1027 sessions::SessionTab* session_tab) { | 1007 base::Time mtime, |
| 1008 sessions::SessionTab* session_tab) { |
1028 DCHECK(session_tab); | 1009 DCHECK(session_tab); |
1029 session_tab->window_id.set_id(tab_delegate.GetWindowId()); | 1010 session_tab->window_id.set_id(tab_delegate.GetWindowId()); |
1030 session_tab->tab_id.set_id(tab_delegate.GetSessionId()); | 1011 session_tab->tab_id.set_id(tab_delegate.GetSessionId()); |
1031 session_tab->tab_visual_index = 0; | 1012 session_tab->tab_visual_index = 0; |
1032 // Use -1 to indicate that the index hasn't been set properly yet. | 1013 // Use -1 to indicate that the index hasn't been set properly yet. |
1033 session_tab->current_navigation_index = -1; | 1014 session_tab->current_navigation_index = -1; |
1034 session_tab->pinned = tab_delegate.IsPinned(); | 1015 const SyncedWindowDelegate* window_delegate = |
| 1016 synced_window_getter->FindById(tab_delegate.GetWindowId()); |
| 1017 session_tab->pinned = |
| 1018 window_delegate ? window_delegate->IsTabPinned(&tab_delegate) : false; |
1035 session_tab->extension_app_id = tab_delegate.GetExtensionAppId(); | 1019 session_tab->extension_app_id = tab_delegate.GetExtensionAppId(); |
1036 session_tab->user_agent_override.clear(); | 1020 session_tab->user_agent_override.clear(); |
1037 session_tab->timestamp = mtime; | 1021 session_tab->timestamp = mtime; |
1038 const int current_index = tab_delegate.GetCurrentEntryIndex(); | 1022 const int current_index = tab_delegate.GetCurrentEntryIndex(); |
1039 const int min_index = std::max(0, current_index - kMaxSyncNavigationCount); | 1023 const int min_index = std::max(0, current_index - kMaxSyncNavigationCount); |
1040 const int max_index = std::min(current_index + kMaxSyncNavigationCount, | 1024 const int max_index = std::min(current_index + kMaxSyncNavigationCount, |
1041 tab_delegate.GetEntryCount()); | 1025 tab_delegate.GetEntryCount()); |
1042 bool is_supervised = tab_delegate.ProfileIsSupervised(); | 1026 bool is_supervised = tab_delegate.ProfileIsSupervised(); |
1043 session_tab->navigations.clear(); | 1027 session_tab->navigations.clear(); |
1044 | 1028 |
1045 for (int i = min_index; i < max_index; ++i) { | 1029 for (int i = min_index; i < max_index; ++i) { |
1046 const NavigationEntry* entry = tab_delegate.GetEntryAtIndexMaybePending(i); | 1030 if (!tab_delegate.GetVirtualURLAtIndex(i).is_valid()) |
1047 DCHECK(entry); | |
1048 if (!entry->GetVirtualURL().is_valid()) | |
1049 continue; | 1031 continue; |
| 1032 sessions::SerializedNavigationEntry serialized_entry; |
| 1033 tab_delegate.GetSerializedNavigationAtIndex(i, &serialized_entry); |
1050 | 1034 |
1051 // Set current_navigation_index to the index in navigations. | 1035 // Set current_navigation_index to the index in navigations. |
1052 if (i == current_index) | 1036 if (i == current_index) |
1053 session_tab->current_navigation_index = session_tab->navigations.size(); | 1037 session_tab->current_navigation_index = session_tab->navigations.size(); |
1054 | 1038 |
1055 session_tab->navigations.push_back( | 1039 session_tab->navigations.push_back(serialized_entry); |
1056 ContentSerializedNavigationBuilder::FromNavigationEntry(i, *entry)); | |
1057 if (is_supervised) { | 1040 if (is_supervised) { |
1058 session_tab->navigations.back().set_blocked_state( | 1041 session_tab->navigations.back().set_blocked_state( |
1059 SerializedNavigationEntry::STATE_ALLOWED); | 1042 SerializedNavigationEntry::STATE_ALLOWED); |
1060 } | 1043 } |
1061 } | 1044 } |
1062 | 1045 |
1063 // If the current navigation is invalid, set the index to the end of the | 1046 // If the current navigation is invalid, set the index to the end of the |
1064 // navigation array. | 1047 // navigation array. |
1065 if (session_tab->current_navigation_index < 0) { | 1048 if (session_tab->current_navigation_index < 0) { |
1066 session_tab->current_navigation_index = | 1049 session_tab->current_navigation_index = |
1067 session_tab->navigations.size() - 1; | 1050 session_tab->navigations.size() - 1; |
1068 } | 1051 } |
1069 | 1052 |
1070 if (is_supervised) { | 1053 if (is_supervised) { |
1071 const std::vector<const NavigationEntry*>& blocked_navigations = | 1054 int offset = session_tab->navigations.size(); |
| 1055 const std::vector<const SerializedNavigationEntry*>& blocked_navigations = |
1072 *tab_delegate.GetBlockedNavigations(); | 1056 *tab_delegate.GetBlockedNavigations(); |
1073 int offset = session_tab->navigations.size(); | |
1074 for (size_t i = 0; i < blocked_navigations.size(); ++i) { | 1057 for (size_t i = 0; i < blocked_navigations.size(); ++i) { |
1075 session_tab->navigations.push_back( | 1058 session_tab->navigations.push_back(*blocked_navigations[i]); |
1076 ContentSerializedNavigationBuilder::FromNavigationEntry( | 1059 session_tab->navigations.back().set_index(offset + i); |
1077 i + offset, *blocked_navigations[i])); | |
1078 session_tab->navigations.back().set_blocked_state( | 1060 session_tab->navigations.back().set_blocked_state( |
1079 SerializedNavigationEntry::STATE_BLOCKED); | 1061 SerializedNavigationEntry::STATE_BLOCKED); |
1080 // TODO(bauerb): Add categories | 1062 // TODO(bauerb): Add categories |
1081 } | 1063 } |
1082 } | 1064 } |
1083 session_tab->session_storage_persistent_id.clear(); | 1065 session_tab->session_storage_persistent_id.clear(); |
1084 } | 1066 } |
1085 | 1067 |
1086 // static | 1068 // static |
1087 void SessionsSyncManager::SetVariationIds(sessions::SessionTab* session_tab) { | 1069 void SessionsSyncManager::SetVariationIds(sessions::SessionTab* session_tab) { |
1088 base::FieldTrial::ActiveGroups active_groups; | 1070 base::FieldTrial::ActiveGroups active_groups; |
1089 base::FieldTrialList::GetActiveFieldTrialGroups(&active_groups); | 1071 base::FieldTrialList::GetActiveFieldTrialGroups(&active_groups); |
1090 for (const base::FieldTrial::ActiveGroup& group : active_groups) { | 1072 for (const base::FieldTrial::ActiveGroup& group : active_groups) { |
1091 const variations::VariationID id = | 1073 const variations::VariationID id = |
1092 variations::GetGoogleVariationID(variations::CHROME_SYNC_SERVICE, | 1074 variations::GetGoogleVariationID(variations::CHROME_SYNC_SERVICE, |
1093 group.trial_name, group.group_name); | 1075 group.trial_name, group.group_name); |
1094 if (id != variations::EMPTY_ID) | 1076 if (id != variations::EMPTY_ID) |
1095 session_tab->variation_ids.push_back(id); | 1077 session_tab->variation_ids.push_back(id); |
1096 } | 1078 } |
1097 } | 1079 } |
1098 | 1080 |
1099 FaviconCache* SessionsSyncManager::GetFaviconCache() { | 1081 FaviconCache* SessionsSyncManager::GetFaviconCache() { |
1100 return &favicon_cache_; | 1082 return &favicon_cache_; |
1101 } | 1083 } |
1102 | 1084 |
1103 SyncedWindowDelegatesGetter* | 1085 SyncedWindowDelegatesGetter* |
1104 SessionsSyncManager::GetSyncedWindowDelegatesGetter() const { | 1086 SessionsSyncManager::GetSyncedWindowDelegatesGetter() const { |
1105 return synced_window_getter_.get(); | 1087 return synced_window_getter_; |
1106 } | 1088 } |
1107 | 1089 |
1108 void SessionsSyncManager::DoGarbageCollection() { | 1090 void SessionsSyncManager::DoGarbageCollection() { |
1109 std::vector<const sync_driver::SyncedSession*> sessions; | 1091 std::vector<const sync_driver::SyncedSession*> sessions; |
1110 if (!GetAllForeignSessions(&sessions)) | 1092 if (!GetAllForeignSessions(&sessions)) |
1111 return; // No foreign sessions. | 1093 return; // No foreign sessions. |
1112 | 1094 |
1113 // Iterate through all the sessions and delete any with age older than | 1095 // Iterate through all the sessions and delete any with age older than |
1114 // |stale_session_threshold_days_|. | 1096 // |stale_session_threshold_days_|. |
1115 syncer::SyncChangeList changes; | 1097 syncer::SyncChangeList changes; |
(...skipping 11 matching lines...) Expand all Loading... |
1127 << " with age " << session_age_in_days << ", deleting."; | 1109 << " with age " << session_age_in_days << ", deleting."; |
1128 DeleteForeignSessionInternal(session_tag, &changes); | 1110 DeleteForeignSessionInternal(session_tag, &changes); |
1129 } | 1111 } |
1130 } | 1112 } |
1131 | 1113 |
1132 if (!changes.empty()) | 1114 if (!changes.empty()) |
1133 sync_processor_->ProcessSyncChanges(FROM_HERE, changes); | 1115 sync_processor_->ProcessSyncChanges(FROM_HERE, changes); |
1134 } | 1116 } |
1135 | 1117 |
1136 }; // namespace browser_sync | 1118 }; // namespace browser_sync |
OLD | NEW |