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
3 // found in the LICENSE file.
5 #ifndef CHROME_BROWSER_SYNC_SESSIONS_SESSIONS_SYNC_MANAGER_H_
6 #define CHROME_BROWSER_SYNC_SESSIONS_SESSIONS_SYNC_MANAGER_H_
13 #include "base/basictypes.h"
14 #include "base/gtest_prod_util.h"
15 #include "base/memory/scoped_vector.h"
16 #include "base/memory/weak_ptr.h"
17 #include "base/time/time.h"
18 #include "chrome/browser/sessions/session_id.h"
19 #include "chrome/browser/sessions/session_types.h"
20 #include "chrome/browser/sync/glue/device_info.h"
21 #include "chrome/browser/sync/glue/favicon_cache.h"
22 #include "chrome/browser/sync/glue/synced_session.h"
23 #include "chrome/browser/sync/glue/synced_session_tracker.h"
24 #include "chrome/browser/sync/open_tabs_ui_delegate.h"
25 #include "chrome/browser/sync/sessions/tab_node_pool.h"
26 #include "components/sync_driver/sync_prefs.h"
27 #include "sync/api/syncable_service.h"
32 class SyncErrorFactory;
37 class SessionSpecifics;
41 } // namespace sync_pb
43 namespace browser_sync {
45 class DataTypeErrorHandler;
46 class LocalDeviceInfoProvider;
47 class SyncedTabDelegate;
48 class SyncedWindowDelegate;
49 class SyncedWindowDelegatesGetter;
51 // An interface defining the ways in which local open tab events can interact
52 // with session sync. All local tab events flow to sync via this interface.
53 // In that way it is analogous to sync changes flowing to the local model
54 // via ProcessSyncChanges, just with a more granular breakdown.
55 class LocalSessionEventHandler {
57 // A local navigation event took place that affects the synced session
58 // for this instance of Chrome.
59 virtual void OnLocalTabModified(SyncedTabDelegate* modified_tab) = 0;
61 // A local navigation occurred that triggered updates to favicon data for
62 // each URL in |updated_page_urls|. This is routed through Sessions Sync so
63 // that we can filter (exclude) favicon updates for pages that aren't
64 // currently part of the set of local open tabs, and pass relevant updates
65 // on to FaviconCache for out-of-band favicon syncing.
66 virtual void OnFaviconPageUrlsUpdated(
67 const std::set<GURL>& updated_page_urls) = 0;
70 // The LocalSessionEventRouter is responsible for hooking itself up to various
71 // notification sources in the browser process and forwarding relevant
72 // events to a handler as defined in the LocalSessionEventHandler contract.
73 class LocalSessionEventRouter {
75 virtual ~LocalSessionEventRouter();
76 virtual void StartRoutingTo(LocalSessionEventHandler* handler) = 0;
77 virtual void Stop() = 0;
80 // Contains all logic for associating the Chrome sessions model and
81 // the sync sessions model.
82 class SessionsSyncManager : public syncer::SyncableService,
83 public OpenTabsUIDelegate,
84 public LocalSessionEventHandler {
86 SessionsSyncManager(Profile* profile,
87 LocalDeviceInfoProvider* local_device,
88 scoped_ptr<LocalSessionEventRouter> router);
89 virtual ~SessionsSyncManager();
91 // syncer::SyncableService implementation.
92 virtual syncer::SyncMergeResult MergeDataAndStartSyncing(
93 syncer::ModelType type,
94 const syncer::SyncDataList& initial_sync_data,
95 scoped_ptr<syncer::SyncChangeProcessor> sync_processor,
96 scoped_ptr<syncer::SyncErrorFactory> error_handler) OVERRIDE;
97 virtual void StopSyncing(syncer::ModelType type) OVERRIDE;
98 virtual syncer::SyncDataList GetAllSyncData(
99 syncer::ModelType type) const OVERRIDE;
100 virtual syncer::SyncError ProcessSyncChanges(
101 const tracked_objects::Location& from_here,
102 const syncer::SyncChangeList& change_list) OVERRIDE;
104 // OpenTabsUIDelegate implementation.
105 virtual bool GetSyncedFaviconForPageURL(
106 const std::string& pageurl,
107 scoped_refptr<base::RefCountedMemory>* favicon_png) const OVERRIDE;
108 virtual bool GetAllForeignSessions(
109 std::vector<const SyncedSession*>* sessions) OVERRIDE;
110 virtual bool GetForeignSession(
111 const std::string& tag,
112 std::vector<const SessionWindow*>* windows) OVERRIDE;
113 virtual bool GetForeignTab(const std::string& tag,
114 const SessionID::id_type tab_id,
115 const SessionTab** tab) OVERRIDE;
116 virtual void DeleteForeignSession(const std::string& tag) OVERRIDE;
117 virtual bool GetLocalSession(const SyncedSession* * local_session) OVERRIDE;
119 // LocalSessionEventHandler implementation.
120 virtual void OnLocalTabModified(SyncedTabDelegate* modified_tab) OVERRIDE;
121 virtual void OnFaviconPageUrlsUpdated(
122 const std::set<GURL>& updated_favicon_page_urls) OVERRIDE;
124 // Returns the tag used to uniquely identify this machine's session in the
126 const std::string& current_machine_tag() const {
127 DCHECK(!current_machine_tag_.empty());
128 return current_machine_tag_;
131 // Return the virtual URL of the current tab, even if it's pending.
132 static GURL GetCurrentVirtualURL(const SyncedTabDelegate& tab_delegate);
134 // Return the favicon url of the current tab, even if it's pending.
135 static GURL GetCurrentFaviconURL(const SyncedTabDelegate& tab_delegate);
137 FaviconCache* GetFaviconCache();
139 SyncedWindowDelegatesGetter* GetSyncedWindowDelegatesGetter() const;
141 // Triggers garbage collection of stale sessions (as defined by
142 // |stale_session_threshold_days_|). This is called automatically every
143 // time we start up (via AssociateModels) and when new sessions data is
144 // downloaded (sync cycles complete).
145 void DoGarbageCollection();
148 // Keep all the links to local tab data in one place. A tab_node_id and tab
149 // must be passed at creation. The tab_node_id is not mutable, although
150 // all other fields are.
153 TabLink(int tab_node_id, const SyncedTabDelegate* tab)
154 : tab_node_id_(tab_node_id),
157 void set_tab(const SyncedTabDelegate* tab) { tab_ = tab; }
158 void set_url(const GURL& url) { url_ = url; }
160 int tab_node_id() const { return tab_node_id_; }
161 const SyncedTabDelegate* tab() const { return tab_; }
162 const GURL& url() const { return url_; }
165 // The id for the sync node this tab is stored in.
166 const int tab_node_id_;
168 // The tab object itself.
169 const SyncedTabDelegate* tab_;
171 // The currently visible url of the tab (used for syncing favicons).
174 DISALLOW_COPY_AND_ASSIGN(TabLink);
177 // Container for accessing local tab data by tab id.
178 typedef std::map<SessionID::id_type, linked_ptr<TabLink> > TabLinksMap;
180 FRIEND_TEST_ALL_PREFIXES(SessionsSyncManagerTest, PopulateSessionHeader);
181 FRIEND_TEST_ALL_PREFIXES(SessionsSyncManagerTest, PopulateSessionWindow);
182 FRIEND_TEST_ALL_PREFIXES(SessionsSyncManagerTest, ValidTabs);
183 FRIEND_TEST_ALL_PREFIXES(SessionsSyncManagerTest, SetSessionTabFromDelegate);
184 FRIEND_TEST_ALL_PREFIXES(SessionsSyncManagerTest,
185 SetSessionTabFromDelegateNavigationIndex);
186 FRIEND_TEST_ALL_PREFIXES(SessionsSyncManagerTest,
187 SetSessionTabFromDelegateCurrentInvalid);
188 FRIEND_TEST_ALL_PREFIXES(SessionsSyncManagerTest, BlockedNavigations);
189 FRIEND_TEST_ALL_PREFIXES(SessionsSyncManagerTest, DeleteForeignSession);
190 FRIEND_TEST_ALL_PREFIXES(SessionsSyncManagerTest,
191 SaveUnassociatedNodesForReassociation);
192 FRIEND_TEST_ALL_PREFIXES(SessionsSyncManagerTest, MergeDeletesCorruptNode);
193 FRIEND_TEST_ALL_PREFIXES(SessionsSyncManagerTest,
194 MergeLocalSessionExistingTabs);
195 FRIEND_TEST_ALL_PREFIXES(SessionsSyncManagerTest,
196 CheckPrerenderedWebContentsSwap);
197 FRIEND_TEST_ALL_PREFIXES(SessionsSyncManagerTest,
198 AssociateWindowsDontReloadTabs);
199 FRIEND_TEST_ALL_PREFIXES(SessionsSyncManagerTest,
200 SwappedOutOnRestore);
201 FRIEND_TEST_ALL_PREFIXES(SessionsSyncManagerTest,
202 ProcessRemoteDeleteOfLocalSession);
204 void InitializeCurrentMachineTag();
206 // Load and add window or tab data for a foreign session to our internal
208 void UpdateTrackerWithForeignSession(
209 const sync_pb::SessionSpecifics& specifics,
210 const base::Time& modification_time);
212 // Returns true if |sync_data| contained a header node for the current
213 // machine, false otherwise. |restored_tabs| is a filtered tab-only
214 // subset of |sync_data| returned by this function for convenience.
215 // |new_changes| is a link to the SyncChange pipeline that exists in the
216 // caller's context. This function will append necessary changes for
218 bool InitFromSyncModel(const syncer::SyncDataList& sync_data,
219 syncer::SyncDataList* restored_tabs,
220 syncer::SyncChangeList* new_changes);
222 // Helper to construct a deletion SyncChange for a *tab node*.
223 // Caller should check IsValid() on the returned change, as it's possible
224 // this node could not be deleted.
225 syncer::SyncChange TombstoneTab(const sync_pb::SessionSpecifics& tab);
227 // Helper method to load the favicon data from the tab specifics. If the
228 // favicon is valid, stores the favicon data into the favicon cache.
229 void RefreshFaviconVisitTimesFromForeignTab(
230 const sync_pb::SessionTab& tab, const base::Time& modification_time);
232 // Removes a foreign session from our internal bookkeeping.
233 // Returns true if the session was found and deleted, false if no data was
234 // found for that session. This will *NOT* trigger sync deletions. See
235 // DeleteForeignSession below.
236 bool DisassociateForeignSession(const std::string& foreign_session_tag);
238 // Delete a foreign session and all its sync data.
239 // |change_output| *must* be provided as a link to the SyncChange pipeline
240 // that exists in the caller's context. This function will append necessary
241 // changes for processing later.
242 void DeleteForeignSessionInternal(const std::string& tag,
243 syncer::SyncChangeList* change_output);
245 // Used to populate a session header from the session specifics header
247 static void PopulateSessionHeaderFromSpecifics(
248 const sync_pb::SessionHeader& header_specifics,
250 SyncedSession* session_header);
252 // Builds |session_window| from the session specifics window
253 // provided and updates the SessionTracker with foreign session data created.
254 void BuildSyncedSessionFromSpecifics(
255 const std::string& session_tag,
256 const sync_pb::SessionWindow& specifics,
258 SessionWindow* session_window);
260 // Resync local window information. Updates the local sessions header node
261 // with the status of open windows and the order of tabs they contain. Should
262 // only be called for changes that affect a window, not a change within a
265 // RELOAD_TABS will additionally cause a resync of all tabs (same as calling
266 // AssociateTabs with a vector of all tabs).
268 // |restored_tabs| is a filtered tab-only subset of initial sync data, if
269 // available (during MergeDataAndStartSyncing). It can be used to obtain
270 // baseline SessionSpecifics for tabs we can't fully associate any other
271 // way because they don't yet have a WebContents.
273 // Returns: false if the local session's sync nodes were deleted and
274 // reassociation is necessary, true otherwise.
276 // |change_output| *must* be provided as a link to the SyncChange pipeline
277 // that exists in the caller's context. This function will append necessary
278 // changes for processing later.
279 enum ReloadTabsOption {
283 void AssociateWindows(ReloadTabsOption option,
284 const syncer::SyncDataList& restored_tabs,
285 syncer::SyncChangeList* change_output);
287 // Loads and reassociates the local tabs referenced in |tabs|.
288 // |change_output| *must* be provided as a link to the SyncChange pipeline
289 // that exists in the caller's context. This function will append necessary
290 // changes for processing later.
291 void AssociateTab(SyncedTabDelegate* const tab,
292 syncer::SyncChangeList* change_output);
294 // Set |session_tab| from |tab_delegate| and |mtime|.
295 static void SetSessionTabFromDelegate(
296 const SyncedTabDelegate& tab_delegate,
298 SessionTab* session_tab);
300 // Populates |specifics| based on the data in |tab_delegate|.
301 void LocalTabDelegateToSpecifics(const SyncedTabDelegate& tab_delegate,
302 sync_pb::SessionSpecifics* specifics);
304 // It's possible that when we associate windows, tabs aren't all loaded
305 // into memory yet (e.g on android) and we don't have a WebContents. In this
306 // case we can't do a full association, but we still want to update tab IDs
307 // as they may have changed after a session was restored. This method
308 // compares new_tab_id against the previously persisted tab ID (from
309 // our TabNodePool) and updates it if it differs.
310 // |restored_tabs| is a filtered tab-only subset of initial sync data, if
311 // available (during MergeDataAndStartSyncing). It can be used to obtain
312 // baseline SessionSpecifics for tabs we can't fully associate any other
313 // way because they don't yet have a WebContents.
314 // TODO(tim): Bug 98892. We should be able to test this for this on android
315 // even though we didn't have tests for old API-based sessions sync.
316 void AssociateRestoredPlaceholderTab(
317 const SyncedTabDelegate& tab_delegate,
318 SessionID::id_type new_tab_id,
319 const syncer::SyncDataList& restored_tabs,
320 syncer::SyncChangeList* change_output);
322 // Stops and re-starts syncing to rebuild association mappings.
323 // See |local_tab_pool_out_of_sync_|.
324 void RebuildAssociations();
326 // Mapping of current open (local) tabs to their sync identifiers.
327 TabLinksMap local_tab_map_;
329 SyncedSessionTracker session_tracker_;
330 FaviconCache favicon_cache_;
332 // Pool of used/available sync nodes associated with local tabs.
333 TabNodePool local_tab_pool_;
335 // Tracks whether our local representation of which sync nodes map to what
336 // 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
338 // delete it. Rather than respond by bullishly re-creating our nodes
339 // 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
341 // proves that we are still relevant.
342 bool local_tab_pool_out_of_sync_;
344 sync_driver::SyncPrefs sync_prefs_;
346 const Profile* const profile_;
348 scoped_ptr<syncer::SyncErrorFactory> error_handler_;
349 scoped_ptr<syncer::SyncChangeProcessor> sync_processor_;
351 // Local device info provider, owned by ProfileSyncService.
352 const LocalDeviceInfoProvider* const local_device_;
354 // Unique client tag.
355 std::string current_machine_tag_;
357 // User-visible machine name.
358 std::string current_session_name_;
360 // SyncID for the sync node containing all the window information for this
362 int local_session_header_node_id_;
364 // Number of days without activity after which we consider a session to be
365 // stale and a candidate for garbage collection.
366 size_t stale_session_threshold_days_;
368 scoped_ptr<LocalSessionEventRouter> local_event_router_;
369 scoped_ptr<SyncedWindowDelegatesGetter> synced_window_getter_;
371 DISALLOW_COPY_AND_ASSIGN(SessionsSyncManager);
374 } // namespace browser_sync
376 #endif // CHROME_BROWSER_SYNC_SESSIONS_SESSIONS_SYNC_MANAGER_H_