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