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/sync/glue/favicon_cache.h"
19 #include "chrome/browser/sync/glue/synced_session_tracker.h"
20 #include "components/sessions/session_id.h"
21 #include "components/sessions/session_types.h"
22 #include "components/sync_driver/device_info.h"
23 #include "components/sync_driver/glue/synced_session.h"
24 #include "components/sync_driver/open_tabs_ui_delegate.h"
25 #include "components/sync_driver/sync_prefs.h"
26 #include "components/sync_driver/tab_node_pool.h"
27 #include "components/variations/variations_associated_data.h"
28 #include "sync/api/syncable_service.h"
33 class SyncErrorFactory
;
36 namespace sync_driver
{
37 class LocalDeviceInfoProvider
;
42 class SessionSpecifics
;
46 } // namespace sync_pb
48 namespace browser_sync
{
50 class DataTypeErrorHandler
;
51 class SyncedTabDelegate
;
52 class SyncedWindowDelegate
;
53 class SyncedWindowDelegatesGetter
;
55 // An interface defining the ways in which local open tab events can interact
56 // with session sync. All local tab events flow to sync via this interface.
57 // In that way it is analogous to sync changes flowing to the local model
58 // via ProcessSyncChanges, just with a more granular breakdown.
59 class LocalSessionEventHandler
{
61 // A local navigation event took place that affects the synced session
62 // for this instance of Chrome.
63 virtual void OnLocalTabModified(SyncedTabDelegate
* modified_tab
) = 0;
65 // A local navigation occurred that triggered updates to favicon data for
66 // each page URL in |page_urls| (e.g. http://www.google.com) and the icon URL
67 // |icon_url| (e.g. http://www.google.com/favicon.ico). This is routed through
68 // Sessions Sync so that we can filter (exclude) favicon updates for pages
69 // that aren't currently part of the set of local open tabs, and pass relevant
70 // updates on to FaviconCache for out-of-band favicon syncing.
71 virtual void OnFaviconsChanged(const std::set
<GURL
>& page_urls
,
72 const GURL
& icon_url
) = 0;
75 // The LocalSessionEventRouter is responsible for hooking itself up to various
76 // notification sources in the browser process and forwarding relevant
77 // events to a handler as defined in the LocalSessionEventHandler contract.
78 class LocalSessionEventRouter
{
80 virtual ~LocalSessionEventRouter();
81 virtual void StartRoutingTo(LocalSessionEventHandler
* handler
) = 0;
82 virtual void Stop() = 0;
85 // Contains all logic for associating the Chrome sessions model and
86 // the sync sessions model.
87 class SessionsSyncManager
: public syncer::SyncableService
,
88 public sync_driver::OpenTabsUIDelegate
,
89 public LocalSessionEventHandler
{
91 SessionsSyncManager(Profile
* profile
,
92 sync_driver::LocalDeviceInfoProvider
* local_device
,
93 scoped_ptr
<LocalSessionEventRouter
> router
);
94 ~SessionsSyncManager() override
;
96 // syncer::SyncableService implementation.
97 syncer::SyncMergeResult
MergeDataAndStartSyncing(
98 syncer::ModelType type
,
99 const syncer::SyncDataList
& initial_sync_data
,
100 scoped_ptr
<syncer::SyncChangeProcessor
> sync_processor
,
101 scoped_ptr
<syncer::SyncErrorFactory
> error_handler
) override
;
102 void StopSyncing(syncer::ModelType type
) override
;
103 syncer::SyncDataList
GetAllSyncData(syncer::ModelType type
) const override
;
104 syncer::SyncError
ProcessSyncChanges(
105 const tracked_objects::Location
& from_here
,
106 const syncer::SyncChangeList
& change_list
) override
;
108 // OpenTabsUIDelegate implementation.
109 bool GetSyncedFaviconForPageURL(
110 const std::string
& pageurl
,
111 scoped_refptr
<base::RefCountedMemory
>* favicon_png
) const override
;
112 bool GetAllForeignSessions(
113 std::vector
<const sync_driver::SyncedSession
*>* sessions
) override
;
114 bool GetForeignSession(
115 const std::string
& tag
,
116 std::vector
<const sessions::SessionWindow
*>* windows
) override
;
117 bool GetForeignTab(const std::string
& tag
,
118 const SessionID::id_type tab_id
,
119 const sessions::SessionTab
** tab
) override
;
120 bool GetForeignSessionTabs(
121 const std::string
& tag
,
122 std::vector
<const sessions::SessionTab
*>* tabs
) override
;
123 void DeleteForeignSession(const std::string
& tag
) override
;
124 bool GetLocalSession(
125 const sync_driver::SyncedSession
** local_session
) override
;
127 // LocalSessionEventHandler implementation.
128 void OnLocalTabModified(SyncedTabDelegate
* modified_tab
) override
;
129 void OnFaviconsChanged(const std::set
<GURL
>& page_urls
,
130 const GURL
& icon_url
) override
;
132 // Returns the tag used to uniquely identify this machine's session in the
134 const std::string
& current_machine_tag() const {
135 DCHECK(!current_machine_tag_
.empty());
136 return current_machine_tag_
;
139 // Return the virtual URL of the current tab, even if it's pending.
140 static GURL
GetCurrentVirtualURL(const SyncedTabDelegate
& tab_delegate
);
142 // Return the favicon url of the current tab, even if it's pending.
143 static GURL
GetCurrentFaviconURL(const SyncedTabDelegate
& tab_delegate
);
145 FaviconCache
* GetFaviconCache();
147 SyncedWindowDelegatesGetter
* GetSyncedWindowDelegatesGetter() const;
149 // Triggers garbage collection of stale sessions (as defined by
150 // |stale_session_threshold_days_|). This is called automatically every
151 // time we start up (via AssociateModels) and when new sessions data is
152 // downloaded (sync cycles complete).
153 void DoGarbageCollection();
156 // Keep all the links to local tab data in one place. A tab_node_id and tab
157 // must be passed at creation. The tab_node_id is not mutable, although
158 // all other fields are.
161 TabLink(int tab_node_id
, const SyncedTabDelegate
* tab
)
162 : tab_node_id_(tab_node_id
),
165 void set_tab(const SyncedTabDelegate
* tab
) { tab_
= tab
; }
166 void set_url(const GURL
& url
) { url_
= url
; }
168 int tab_node_id() const { return tab_node_id_
; }
169 const SyncedTabDelegate
* tab() const { return tab_
; }
170 const GURL
& url() const { return url_
; }
173 // The id for the sync node this tab is stored in.
174 const int tab_node_id_
;
176 // The tab object itself.
177 const SyncedTabDelegate
* tab_
;
179 // The currently visible url of the tab (used for syncing favicons).
182 DISALLOW_COPY_AND_ASSIGN(TabLink
);
185 // Container for accessing local tab data by tab id.
186 typedef std::map
<SessionID::id_type
, linked_ptr
<TabLink
> > TabLinksMap
;
188 FRIEND_TEST_ALL_PREFIXES(SessionsSyncManagerTest
, PopulateSessionHeader
);
189 FRIEND_TEST_ALL_PREFIXES(SessionsSyncManagerTest
, PopulateSessionWindow
);
190 FRIEND_TEST_ALL_PREFIXES(SessionsSyncManagerTest
, ValidTabs
);
191 FRIEND_TEST_ALL_PREFIXES(SessionsSyncManagerTest
, SetSessionTabFromDelegate
);
192 FRIEND_TEST_ALL_PREFIXES(SessionsSyncManagerTest
,
193 SetSessionTabFromDelegateNavigationIndex
);
194 FRIEND_TEST_ALL_PREFIXES(SessionsSyncManagerTest
,
195 SetSessionTabFromDelegateCurrentInvalid
);
196 FRIEND_TEST_ALL_PREFIXES(SessionsSyncManagerTest
, BlockedNavigations
);
197 FRIEND_TEST_ALL_PREFIXES(SessionsSyncManagerTest
, DeleteForeignSession
);
198 FRIEND_TEST_ALL_PREFIXES(SessionsSyncManagerTest
,
199 SaveUnassociatedNodesForReassociation
);
200 FRIEND_TEST_ALL_PREFIXES(SessionsSyncManagerTest
, MergeDeletesCorruptNode
);
201 FRIEND_TEST_ALL_PREFIXES(SessionsSyncManagerTest
,
202 MergeLocalSessionExistingTabs
);
203 FRIEND_TEST_ALL_PREFIXES(SessionsSyncManagerTest
,
204 CheckPrerenderedWebContentsSwap
);
205 FRIEND_TEST_ALL_PREFIXES(SessionsSyncManagerTest
,
206 AssociateWindowsDontReloadTabs
);
207 FRIEND_TEST_ALL_PREFIXES(SessionsSyncManagerTest
,
208 SwappedOutOnRestore
);
209 FRIEND_TEST_ALL_PREFIXES(SessionsSyncManagerTest
,
210 ProcessRemoteDeleteOfLocalSession
);
211 FRIEND_TEST_ALL_PREFIXES(SessionsSyncManagerTest
,
214 void InitializeCurrentMachineTag();
216 // Load and add window or tab data for a foreign session to our internal
218 void UpdateTrackerWithForeignSession(
219 const sync_pb::SessionSpecifics
& specifics
,
220 const base::Time
& modification_time
);
222 // Returns true if |sync_data| contained a header node for the current
223 // machine, false otherwise. |restored_tabs| is a filtered tab-only
224 // subset of |sync_data| returned by this function for convenience.
225 // |new_changes| is a link to the SyncChange pipeline that exists in the
226 // caller's context. This function will append necessary changes for
228 bool InitFromSyncModel(const syncer::SyncDataList
& sync_data
,
229 syncer::SyncDataList
* restored_tabs
,
230 syncer::SyncChangeList
* new_changes
);
232 // Helper to construct a deletion SyncChange for a *tab node*.
233 // Caller should check IsValid() on the returned change, as it's possible
234 // this node could not be deleted.
235 syncer::SyncChange
TombstoneTab(const sync_pb::SessionSpecifics
& tab
);
237 // Helper method to load the favicon data from the tab specifics. If the
238 // favicon is valid, stores the favicon data into the favicon cache.
239 void RefreshFaviconVisitTimesFromForeignTab(
240 const sync_pb::SessionTab
& tab
, const base::Time
& modification_time
);
242 // Removes a foreign session from our internal bookkeeping.
243 // Returns true if the session was found and deleted, false if no data was
244 // found for that session. This will *NOT* trigger sync deletions. See
245 // DeleteForeignSession below.
246 bool DisassociateForeignSession(const std::string
& foreign_session_tag
);
248 // Delete a foreign session and all its sync data.
249 // |change_output| *must* be provided as a link to the SyncChange pipeline
250 // that exists in the caller's context. This function will append necessary
251 // changes for processing later.
252 void DeleteForeignSessionInternal(const std::string
& tag
,
253 syncer::SyncChangeList
* change_output
);
255 // Used to populate a session header from the session specifics header
257 static void PopulateSessionHeaderFromSpecifics(
258 const sync_pb::SessionHeader
& header_specifics
,
260 sync_driver::SyncedSession
* session_header
);
262 // Builds |session_window| from the session specifics window
263 // provided and updates the SessionTracker with foreign session data created.
264 void BuildSyncedSessionFromSpecifics(
265 const std::string
& session_tag
,
266 const sync_pb::SessionWindow
& specifics
,
268 sessions::SessionWindow
* session_window
);
270 // Resync local window information. Updates the local sessions header node
271 // with the status of open windows and the order of tabs they contain. Should
272 // only be called for changes that affect a window, not a change within a
275 // RELOAD_TABS will additionally cause a resync of all tabs (same as calling
276 // AssociateTabs with a vector of all tabs).
278 // |restored_tabs| is a filtered tab-only subset of initial sync data, if
279 // available (during MergeDataAndStartSyncing). It can be used to obtain
280 // baseline SessionSpecifics for tabs we can't fully associate any other
281 // way because they don't yet have a WebContents.
283 // Returns: false if the local session's sync nodes were deleted and
284 // reassociation is necessary, true otherwise.
286 // |change_output| *must* be provided as a link to the SyncChange pipeline
287 // that exists in the caller's context. This function will append necessary
288 // changes for processing later.
289 enum ReloadTabsOption
{
293 void AssociateWindows(ReloadTabsOption option
,
294 const syncer::SyncDataList
& restored_tabs
,
295 syncer::SyncChangeList
* change_output
);
297 // Loads and reassociates the local tabs referenced in |tabs|.
298 // |change_output| *must* be provided as a link to the SyncChange pipeline
299 // that exists in the caller's context. This function will append necessary
300 // changes for processing later.
301 void AssociateTab(SyncedTabDelegate
* const tab
,
302 syncer::SyncChangeList
* change_output
);
304 // Set |session_tab| from |tab_delegate| and |mtime|.
305 static void SetSessionTabFromDelegate(
306 const SyncedTabDelegate
& tab_delegate
,
308 sessions::SessionTab
* session_tab
);
310 // Sets |variation_ids| field of |session_tab| with the ids of the currently
311 // assigned variations which should be sent to sync.
312 static void SetVariationIds(sessions::SessionTab
* session_tab
);
314 // Populates |specifics| based on the data in |tab_delegate|.
315 void LocalTabDelegateToSpecifics(const SyncedTabDelegate
& tab_delegate
,
316 sync_pb::SessionSpecifics
* specifics
);
318 // It's possible that when we associate windows, tabs aren't all loaded
319 // into memory yet (e.g on android) and we don't have a WebContents. In this
320 // case we can't do a full association, but we still want to update tab IDs
321 // as they may have changed after a session was restored. This method
322 // compares new_tab_id against the previously persisted tab ID (from
323 // our TabNodePool) and updates it if it differs.
324 // |restored_tabs| is a filtered tab-only subset of initial sync data, if
325 // available (during MergeDataAndStartSyncing). It can be used to obtain
326 // baseline SessionSpecifics for tabs we can't fully associate any other
327 // way because they don't yet have a WebContents.
328 // TODO(tim): Bug 98892. We should be able to test this for this on android
329 // even though we didn't have tests for old API-based sessions sync.
330 void AssociateRestoredPlaceholderTab(
331 const SyncedTabDelegate
& tab_delegate
,
332 SessionID::id_type new_tab_id
,
333 const syncer::SyncDataList
& restored_tabs
,
334 syncer::SyncChangeList
* change_output
);
336 // Stops and re-starts syncing to rebuild association mappings.
337 // See |local_tab_pool_out_of_sync_|.
338 void RebuildAssociations();
340 // Validates the content of a SessionHeader protobuf.
341 // Returns false if validation fails.
342 static bool IsValidSessionHeader(const sync_pb::SessionHeader
& header
);
344 // Mapping of current open (local) tabs to their sync identifiers.
345 TabLinksMap local_tab_map_
;
347 SyncedSessionTracker session_tracker_
;
348 FaviconCache favicon_cache_
;
350 // Pool of used/available sync nodes associated with local tabs.
351 TabNodePool local_tab_pool_
;
353 // Tracks whether our local representation of which sync nodes map to what
354 // tabs (belonging to the current local session) is inconsistent. This can
355 // happen if a foreign client deems our session as "stale" and decides to
356 // delete it. Rather than respond by bullishly re-creating our nodes
357 // immediately, which could lead to ping-pong sequences, we give the benefit
358 // of the doubt and hold off until another local navigation occurs, which
359 // proves that we are still relevant.
360 bool local_tab_pool_out_of_sync_
;
362 sync_driver::SyncPrefs sync_prefs_
;
364 Profile
* const profile_
;
366 scoped_ptr
<syncer::SyncErrorFactory
> error_handler_
;
367 scoped_ptr
<syncer::SyncChangeProcessor
> sync_processor_
;
369 // Local device info provider, owned by ProfileSyncService.
370 const sync_driver::LocalDeviceInfoProvider
* const local_device_
;
372 // Unique client tag.
373 std::string current_machine_tag_
;
375 // User-visible machine name.
376 std::string current_session_name_
;
378 // SyncID for the sync node containing all the window information for this
380 int local_session_header_node_id_
;
382 // Number of days without activity after which we consider a session to be
383 // stale and a candidate for garbage collection.
384 size_t stale_session_threshold_days_
;
386 scoped_ptr
<LocalSessionEventRouter
> local_event_router_
;
387 scoped_ptr
<SyncedWindowDelegatesGetter
> synced_window_getter_
;
389 DISALLOW_COPY_AND_ASSIGN(SessionsSyncManager
);
392 } // namespace browser_sync
394 #endif // CHROME_BROWSER_SYNC_SESSIONS_SESSIONS_SYNC_MANAGER_H_