| 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 #ifndef COMPONENTS_SYNC_SESSIONS_SESSIONS_SYNC_MANAGER_H_ | 5 #ifndef COMPONENTS_SYNC_SESSIONS_SESSIONS_SYNC_MANAGER_H_ |
| 6 #define COMPONENTS_SYNC_SESSIONS_SESSIONS_SYNC_MANAGER_H_ | 6 #define COMPONENTS_SYNC_SESSIONS_SESSIONS_SYNC_MANAGER_H_ |
| 7 | 7 |
| 8 #include <stddef.h> | 8 #include <stddef.h> |
| 9 | 9 |
| 10 #include <map> | 10 #include <map> |
| (...skipping 12 matching lines...) Expand all Loading... |
| 23 #include "components/sync/base/sync_prefs.h" | 23 #include "components/sync/base/sync_prefs.h" |
| 24 #include "components/sync/device_info/device_info.h" | 24 #include "components/sync/device_info/device_info.h" |
| 25 #include "components/sync/model/syncable_service.h" | 25 #include "components/sync/model/syncable_service.h" |
| 26 #include "components/sync_sessions/favicon_cache.h" | 26 #include "components/sync_sessions/favicon_cache.h" |
| 27 #include "components/sync_sessions/local_session_event_router.h" | 27 #include "components/sync_sessions/local_session_event_router.h" |
| 28 #include "components/sync_sessions/lost_navigations_recorder.h" | 28 #include "components/sync_sessions/lost_navigations_recorder.h" |
| 29 #include "components/sync_sessions/open_tabs_ui_delegate.h" | 29 #include "components/sync_sessions/open_tabs_ui_delegate.h" |
| 30 #include "components/sync_sessions/revisit/page_revisit_broadcaster.h" | 30 #include "components/sync_sessions/revisit/page_revisit_broadcaster.h" |
| 31 #include "components/sync_sessions/synced_session.h" | 31 #include "components/sync_sessions/synced_session.h" |
| 32 #include "components/sync_sessions/synced_session_tracker.h" | 32 #include "components/sync_sessions/synced_session_tracker.h" |
| 33 #include "components/sync_sessions/tab_node_pool.h" | |
| 34 | 33 |
| 35 namespace syncer { | 34 namespace syncer { |
| 36 class LocalDeviceInfoProvider; | 35 class LocalDeviceInfoProvider; |
| 37 class SyncErrorFactory; | 36 class SyncErrorFactory; |
| 38 class SyncPrefs; | 37 class SyncPrefs; |
| 39 } // namespace syncer | 38 } // namespace syncer |
| 40 | 39 |
| 41 namespace sync_pb { | 40 namespace sync_pb { |
| 42 class SessionHeader; | 41 class SessionHeader; |
| 43 class SessionSpecifics; | 42 class SessionSpecifics; |
| (...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 111 } | 110 } |
| 112 | 111 |
| 113 FaviconCache* GetFaviconCache(); | 112 FaviconCache* GetFaviconCache(); |
| 114 | 113 |
| 115 // Triggers garbage collection of stale sessions (as defined by | 114 // Triggers garbage collection of stale sessions (as defined by |
| 116 // |stale_session_threshold_days_|). This is called every time we see new | 115 // |stale_session_threshold_days_|). This is called every time we see new |
| 117 // sessions data downloaded (sync cycles complete). | 116 // sessions data downloaded (sync cycles complete). |
| 118 void DoGarbageCollection(); | 117 void DoGarbageCollection(); |
| 119 | 118 |
| 120 private: | 119 private: |
| 121 // Keep all the links to local tab data in one place. A tab_node_id and tab | |
| 122 // must be passed at creation. The tab_node_id is not mutable, although | |
| 123 // all other fields are. | |
| 124 class TabLink { | |
| 125 public: | |
| 126 TabLink(int tab_node_id, const SyncedTabDelegate* tab) | |
| 127 : tab_node_id_(tab_node_id), tab_(tab) {} | |
| 128 | |
| 129 void set_tab(const SyncedTabDelegate* tab) { tab_ = tab; } | |
| 130 void set_url(const GURL& url) { url_ = url; } | |
| 131 | |
| 132 int tab_node_id() const { return tab_node_id_; } | |
| 133 const SyncedTabDelegate* tab() const { return tab_; } | |
| 134 const GURL& url() const { return url_; } | |
| 135 | |
| 136 private: | |
| 137 // The id for the sync node this tab is stored in. | |
| 138 const int tab_node_id_; | |
| 139 | |
| 140 // The tab object itself. | |
| 141 const SyncedTabDelegate* tab_; | |
| 142 | |
| 143 // The currently visible url of the tab (used for syncing favicons). | |
| 144 GURL url_; | |
| 145 | |
| 146 DISALLOW_COPY_AND_ASSIGN(TabLink); | |
| 147 }; | |
| 148 | |
| 149 // Container for accessing local tab data by tab id. | |
| 150 typedef std::map<SessionID::id_type, linked_ptr<TabLink>> TabLinksMap; | |
| 151 | |
| 152 friend class extensions::ExtensionSessionsTest; | 120 friend class extensions::ExtensionSessionsTest; |
| 153 friend class SessionsSyncManagerTest; | 121 friend class SessionsSyncManagerTest; |
| 154 FRIEND_TEST_ALL_PREFIXES(SessionsSyncManagerTest, PopulateSessionHeader); | 122 FRIEND_TEST_ALL_PREFIXES(SessionsSyncManagerTest, PopulateSessionHeader); |
| 155 FRIEND_TEST_ALL_PREFIXES(SessionsSyncManagerTest, PopulateSessionWindow); | 123 FRIEND_TEST_ALL_PREFIXES(SessionsSyncManagerTest, PopulateSessionWindow); |
| 156 FRIEND_TEST_ALL_PREFIXES(SessionsSyncManagerTest, ValidTabs); | 124 FRIEND_TEST_ALL_PREFIXES(SessionsSyncManagerTest, ValidTabs); |
| 157 FRIEND_TEST_ALL_PREFIXES(SessionsSyncManagerTest, SetSessionTabFromDelegate); | 125 FRIEND_TEST_ALL_PREFIXES(SessionsSyncManagerTest, SetSessionTabFromDelegate); |
| 158 FRIEND_TEST_ALL_PREFIXES(SessionsSyncManagerTest, | 126 FRIEND_TEST_ALL_PREFIXES(SessionsSyncManagerTest, |
| 159 SetSessionTabFromDelegateNavigationIndex); | 127 SetSessionTabFromDelegateNavigationIndex); |
| 160 FRIEND_TEST_ALL_PREFIXES(SessionsSyncManagerTest, | 128 FRIEND_TEST_ALL_PREFIXES(SessionsSyncManagerTest, |
| 161 SetSessionTabFromDelegateCurrentInvalid); | 129 SetSessionTabFromDelegateCurrentInvalid); |
| (...skipping 13 matching lines...) Expand all Loading... |
| 175 CheckPrerenderedWebContentsSwap); | 143 CheckPrerenderedWebContentsSwap); |
| 176 FRIEND_TEST_ALL_PREFIXES(SessionsSyncManagerTest, | 144 FRIEND_TEST_ALL_PREFIXES(SessionsSyncManagerTest, |
| 177 AssociateWindowsDontReloadTabs); | 145 AssociateWindowsDontReloadTabs); |
| 178 FRIEND_TEST_ALL_PREFIXES(SessionsSyncManagerTest, SwappedOutOnRestore); | 146 FRIEND_TEST_ALL_PREFIXES(SessionsSyncManagerTest, SwappedOutOnRestore); |
| 179 FRIEND_TEST_ALL_PREFIXES(SessionsSyncManagerTest, | 147 FRIEND_TEST_ALL_PREFIXES(SessionsSyncManagerTest, |
| 180 ProcessRemoteDeleteOfLocalSession); | 148 ProcessRemoteDeleteOfLocalSession); |
| 181 FRIEND_TEST_ALL_PREFIXES(SessionsSyncManagerTest, SetVariationIds); | 149 FRIEND_TEST_ALL_PREFIXES(SessionsSyncManagerTest, SetVariationIds); |
| 182 | 150 |
| 183 void InitializeCurrentMachineTag(const std::string& cache_guid); | 151 void InitializeCurrentMachineTag(const std::string& cache_guid); |
| 184 | 152 |
| 185 // Load and add window or tab data for a foreign session to our internal | 153 // Load and add window or tab data from synced specifics to our internal |
| 186 // tracking. | 154 // tracking. |
| 187 void UpdateTrackerWithForeignSession( | 155 void UpdateTrackerWithSpecifics(const sync_pb::SessionSpecifics& specifics, |
| 188 const sync_pb::SessionSpecifics& specifics, | 156 const base::Time& modification_time); |
| 189 const base::Time& modification_time); | |
| 190 | 157 |
| 191 // Returns true if |sync_data| contained a header node for the current | 158 // Returns true if |sync_data| contained a header node for the current |
| 192 // machine, false otherwise. |restored_tabs| is a filtered tab-only | 159 // machine, false otherwise. |new_changes| is a link to the SyncChange |
| 193 // subset of |sync_data| returned by this function for convenience. | 160 // pipeline that exists in the caller's context. This function will append |
| 194 // |new_changes| is a link to the SyncChange pipeline that exists in the | 161 // necessary changes for processing later. |
| 195 // caller's context. This function will append necessary changes for | |
| 196 // processing later. | |
| 197 bool InitFromSyncModel(const syncer::SyncDataList& sync_data, | 162 bool InitFromSyncModel(const syncer::SyncDataList& sync_data, |
| 198 syncer::SyncDataList* restored_tabs, | |
| 199 syncer::SyncChangeList* new_changes); | 163 syncer::SyncChangeList* new_changes); |
| 200 | 164 |
| 201 // Helper to construct a deletion SyncChange for a *tab node*. | 165 // Helper to construct a deletion SyncChange for a *tab node*. |
| 202 // Caller should check IsValid() on the returned change, as it's possible | 166 // Caller should check IsValid() on the returned change, as it's possible |
| 203 // this node could not be deleted. | 167 // this node could not be deleted. |
| 204 syncer::SyncChange TombstoneTab(const sync_pb::SessionSpecifics& tab); | 168 syncer::SyncChange TombstoneTab(const sync_pb::SessionSpecifics& tab); |
| 205 | 169 |
| 206 // Helper method to load the favicon data from the tab specifics. If the | 170 // Helper method to load the favicon data from the tab specifics. If the |
| 207 // favicon is valid, stores the favicon data into the favicon cache. | 171 // favicon is valid, stores the favicon data into the favicon cache. |
| 208 void RefreshFaviconVisitTimesFromForeignTab( | 172 void RefreshFaviconVisitTimesFromForeignTab( |
| (...skipping 28 matching lines...) Expand all Loading... |
| 237 sessions::SessionWindow* session_window); | 201 sessions::SessionWindow* session_window); |
| 238 | 202 |
| 239 // Resync local window information. Updates the local sessions header node | 203 // Resync local window information. Updates the local sessions header node |
| 240 // with the status of open windows and the order of tabs they contain. Should | 204 // with the status of open windows and the order of tabs they contain. Should |
| 241 // only be called for changes that affect a window, not a change within a | 205 // only be called for changes that affect a window, not a change within a |
| 242 // single tab. | 206 // single tab. |
| 243 // | 207 // |
| 244 // RELOAD_TABS will additionally cause a resync of all tabs (same as calling | 208 // RELOAD_TABS will additionally cause a resync of all tabs (same as calling |
| 245 // AssociateTabs with a vector of all tabs). | 209 // AssociateTabs with a vector of all tabs). |
| 246 // | 210 // |
| 247 // |restored_tabs| is a filtered tab-only subset of initial sync data, if | |
| 248 // available (during MergeDataAndStartSyncing). It can be used to obtain | |
| 249 // baseline SessionSpecifics for tabs we can't fully associate any other | |
| 250 // way because they don't yet have a WebContents. | |
| 251 // | |
| 252 // Returns: false if the local session's sync nodes were deleted and | 211 // Returns: false if the local session's sync nodes were deleted and |
| 253 // reassociation is necessary, true otherwise. | 212 // reassociation is necessary, true otherwise. |
| 254 // | 213 // |
| 255 // |change_output| *must* be provided as a link to the SyncChange pipeline | 214 // |change_output| *must* be provided as a link to the SyncChange pipeline |
| 256 // that exists in the caller's context. This function will append necessary | 215 // that exists in the caller's context. This function will append necessary |
| 257 // changes for processing later. | 216 // changes for processing later. |
| 258 enum ReloadTabsOption { RELOAD_TABS, DONT_RELOAD_TABS }; | 217 enum ReloadTabsOption { RELOAD_TABS, DONT_RELOAD_TABS }; |
| 259 void AssociateWindows(ReloadTabsOption option, | 218 void AssociateWindows(ReloadTabsOption option, |
| 260 const syncer::SyncDataList& restored_tabs, | |
| 261 syncer::SyncChangeList* change_output); | 219 syncer::SyncChangeList* change_output); |
| 262 | 220 |
| 263 // Loads and reassociates the local tabs referenced in |tabs|. | 221 // Loads and reassociates the local tabs referenced in |tabs|. |
| 264 // |change_output| *must* be provided as a link to the SyncChange pipeline | 222 // |change_output| *must* be provided as a link to the SyncChange pipeline |
| 265 // that exists in the caller's context. This function will append necessary | 223 // that exists in the caller's context. This function will append necessary |
| 266 // changes for processing later. | 224 // changes for processing later. |
| 267 void AssociateTab(SyncedTabDelegate* const tab, | 225 void AssociateTab(SyncedTabDelegate* const tab, |
| 268 syncer::SyncChangeList* change_output); | 226 syncer::SyncChangeList* change_output); |
| 269 | 227 |
| 270 // Set |session_tab| from |tab_delegate| and |mtime|. | 228 // Set |session_tab| from |tab_delegate| and |mtime|. |
| 271 void SetSessionTabFromDelegate(const SyncedTabDelegate& tab_delegate, | 229 void SetSessionTabFromDelegate(const SyncedTabDelegate& tab_delegate, |
| 272 base::Time mtime, | 230 base::Time mtime, |
| 273 sessions::SessionTab* session_tab); | 231 sessions::SessionTab* session_tab); |
| 274 | 232 |
| 275 // Sets |variation_ids| field of |session_tab| with the ids of the currently | 233 // Sets |variation_ids| field of |session_tab| with the ids of the currently |
| 276 // assigned variations which should be sent to sync. | 234 // assigned variations which should be sent to sync. |
| 277 static void SetVariationIds(sessions::SessionTab* session_tab); | 235 static void SetVariationIds(sessions::SessionTab* session_tab); |
| 278 | 236 |
| 279 // Populates |specifics| based on the data in |tab_delegate|. | 237 // Populates |specifics| based on the data in |tab_delegate|. |
| 280 void LocalTabDelegateToSpecifics(const SyncedTabDelegate& tab_delegate, | 238 void LocalTabDelegateToSpecifics(const SyncedTabDelegate& tab_delegate, |
| 281 sync_pb::SessionSpecifics* specifics); | 239 sync_pb::SessionSpecifics* specifics); |
| 282 | 240 |
| 283 // It's possible that when we associate windows, tabs aren't all loaded | 241 // It's possible that when we associate windows, tabs aren't all loaded |
| 284 // into memory yet (e.g on android) and we don't have a WebContents. In this | 242 // into memory yet (e.g on android) and we don't have a WebContents. In this |
| 285 // case we can't do a full association, but we still want to update tab IDs | 243 // case we can't do a full association, but we still want to update tab IDs |
| 286 // as they may have changed after a session was restored. This method | 244 // as they may have changed after a session was restored. This method |
| 287 // compares new_tab_id and new_window_id against the previously persisted tab | 245 // compares new_tab_id and new_window_id against the previously persisted tab |
| 288 // ID and window ID (from our TabNodePool) and updates them if either differs. | 246 // ID and window ID (from our TabNodePool) and updates them if either differs. |
| 289 // |restored_tabs| is a filtered tab-only subset of initial sync data, if | |
| 290 // available (during MergeDataAndStartSyncing). It can be used to obtain | |
| 291 // baseline SessionSpecifics for tabs we can't fully associate any other | |
| 292 // way because they don't yet have a WebContents. | |
| 293 // TODO(tim): Bug 98892. We should be able to test this for this on android | |
| 294 // even though we didn't have tests for old API-based sessions sync. | |
| 295 void AssociateRestoredPlaceholderTab( | 247 void AssociateRestoredPlaceholderTab( |
| 296 const SyncedTabDelegate& tab_delegate, | 248 const SyncedTabDelegate& tab_delegate, |
| 297 SessionID::id_type new_tab_id, | 249 SessionID::id_type new_tab_id, |
| 298 SessionID::id_type new_window_id, | 250 SessionID::id_type new_window_id, |
| 299 const syncer::SyncDataList& restored_tabs, | |
| 300 syncer::SyncChangeList* change_output); | 251 syncer::SyncChangeList* change_output); |
| 301 | 252 |
| 302 // Stops and re-starts syncing to rebuild association mappings. Returns true | 253 // Stops and re-starts syncing to rebuild association mappings. Returns true |
| 303 // when re-starting succeeds. | 254 // when re-starting succeeds. |
| 304 // See |local_tab_pool_out_of_sync_|. | 255 // See |local_tab_pool_out_of_sync_|. |
| 305 bool RebuildAssociations(); | 256 bool RebuildAssociations(); |
| 306 | 257 |
| 307 // Validates the content of a SessionHeader protobuf. | 258 // Validates the content of a SessionHeader protobuf. |
| 308 // Returns false if validation fails. | 259 // Returns false if validation fails. |
| 309 static bool IsValidSessionHeader(const sync_pb::SessionHeader& header); | 260 static bool IsValidSessionHeader(const sync_pb::SessionHeader& header); |
| 310 | 261 |
| 311 // Calculates the tag hash from a specifics object. Calculating the hash is | 262 // Calculates the tag hash from a specifics object. Calculating the hash is |
| 312 // something we typically want to avoid doing in the model type like this. | 263 // something we typically want to avoid doing in the model type like this. |
| 313 // However, the only place that understands how to generate a tag from the | 264 // However, the only place that understands how to generate a tag from the |
| 314 // specifics is the model type, ie us. We need to generate the tag because it | 265 // specifics is the model type, ie us. We need to generate the tag because it |
| 315 // is not passed over the wire for remote data. The use case this function was | 266 // is not passed over the wire for remote data. The use case this function was |
| 316 // created for is detecting bad tag hashes from remote data, see | 267 // created for is detecting bad tag hashes from remote data, see |
| 317 // crbug.com/604657. | 268 // crbug.com/604657. |
| 318 static std::string TagHashFromSpecifics( | 269 static std::string TagHashFromSpecifics( |
| 319 const sync_pb::SessionSpecifics& specifics); | 270 const sync_pb::SessionSpecifics& specifics); |
| 320 | 271 |
| 321 SyncedWindowDelegatesGetter* synced_window_delegates_getter() const; | 272 SyncedWindowDelegatesGetter* synced_window_delegates_getter() const; |
| 322 | 273 |
| 323 // The client of this sync sessions datatype. | 274 // The client of this sync sessions datatype. |
| 324 SyncSessionsClient* const sessions_client_; | 275 SyncSessionsClient* const sessions_client_; |
| 325 | 276 |
| 326 // Mapping of current open (local) tabs to their sync identifiers. | |
| 327 TabLinksMap local_tab_map_; | |
| 328 | |
| 329 SyncedSessionTracker session_tracker_; | 277 SyncedSessionTracker session_tracker_; |
| 330 FaviconCache favicon_cache_; | 278 FaviconCache favicon_cache_; |
| 331 | 279 |
| 332 // Pool of used/available sync nodes associated with local tabs. | |
| 333 TabNodePool local_tab_pool_; | |
| 334 | |
| 335 // Tracks whether our local representation of which sync nodes map to what | 280 // Tracks whether our local representation of which sync nodes map to what |
| 336 // tabs (belonging to the current local session) is inconsistent. This can | 281 // tabs (belonging to the current local session) is inconsistent. This can |
| 337 // happen if a foreign client deems our session as "stale" and decides to | 282 // happen if a foreign client deems our session as "stale" and decides to |
| 338 // delete it. Rather than respond by bullishly re-creating our nodes | 283 // delete it. Rather than respond by bullishly re-creating our nodes |
| 339 // immediately, which could lead to ping-pong sequences, we give the benefit | 284 // immediately, which could lead to ping-pong sequences, we give the benefit |
| 340 // of the doubt and hold off until another local navigation occurs, which | 285 // of the doubt and hold off until another local navigation occurs, which |
| 341 // proves that we are still relevant. | 286 // proves that we are still relevant. |
| 342 bool local_tab_pool_out_of_sync_; | 287 bool local_tab_pool_out_of_sync_; |
| 343 | 288 |
| 344 syncer::SyncPrefs* sync_prefs_; | 289 syncer::SyncPrefs* sync_prefs_; |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 377 | 322 |
| 378 // Callback to inform sync that a sync data refresh is requested. | 323 // Callback to inform sync that a sync data refresh is requested. |
| 379 base::Closure datatype_refresh_callback_; | 324 base::Closure datatype_refresh_callback_; |
| 380 | 325 |
| 381 DISALLOW_COPY_AND_ASSIGN(SessionsSyncManager); | 326 DISALLOW_COPY_AND_ASSIGN(SessionsSyncManager); |
| 382 }; | 327 }; |
| 383 | 328 |
| 384 } // namespace sync_sessions | 329 } // namespace sync_sessions |
| 385 | 330 |
| 386 #endif // COMPONENTS_SYNC_SESSIONS_SESSIONS_SYNC_MANAGER_H_ | 331 #endif // COMPONENTS_SYNC_SESSIONS_SESSIONS_SYNC_MANAGER_H_ |
| OLD | NEW |