private void registerForForeignSessionUpdates() {
   mForeignSessionHelper.setOnForeignSessionCallback(
       new ForeignSessionCallback() {
         @Override
         public void onUpdated() {
           updateForeignSessions();
           postUpdate();
         }
       });
 }
  /**
   * Create an RecentTabsManager to be used with RecentTabsPage and RecentTabsRowAdapter.
   *
   * @param tab The Tab that is showing this recent tabs page.
   * @param profile Profile that is associated with the current session.
   * @param context the Android context this manager will work in.
   */
  public RecentTabsManager(Tab tab, Profile profile, Context context) {
    mProfile = profile;
    mTab = tab;
    mForeignSessionHelper = buildForeignSessionHelper(mProfile);
    mNewTabPagePrefs = buildNewTabPagePrefs(mProfile);
    mFaviconHelper = buildFaviconHelper();
    mRecentlyClosedBridge = buildRecentlyClosedBridge(mProfile);
    mSignInManager = SigninManager.get(context);
    mContext = context;

    updateRecentlyClosedTabs();
    registerForForeignSessionUpdates();
    updateForeignSessions();
    mForeignSessionHelper.triggerSessionSync();
    registerForSignInAndSyncNotifications();

    InvalidationController.get(mContext).onRecentTabsPageOpened();
  }
  /**
   * Should be called when this object is no longer needed. Performs necessary listener tear down.
   */
  public void destroy() {
    mIsDestroyed = true;
    AndroidSyncSettings.unregisterObserver(mContext, this);

    mSignInManager.removeSignInStateObserver(this);
    mSignInManager = null;

    mFaviconHelper.destroy();
    mFaviconHelper = null;

    mRecentlyClosedBridge.destroy();
    mRecentlyClosedBridge = null;

    mForeignSessionHelper.destroy();
    mForeignSessionHelper = null;

    mUpdatedCallback = null;

    mNewTabPagePrefs.destroy();
    mNewTabPagePrefs = null;

    InvalidationController.get(mContext).onRecentTabsPageClosed();
  }
 /**
  * Remove Foreign session to display. Note that it might reappear during the next sync if the
  * session is not orphaned.
  *
  * <p>This is mainly for when user wants to delete an orphaned session.
  *
  * @param session Session to be deleted.
  */
 public void deleteForeignSession(ForeignSession session) {
   if (mIsDestroyed) return;
   mForeignSessionHelper.deleteForeignSession(session);
 }
 /**
  * Opens a new tab navigating to ForeignSessionTab.
  *
  * @param session The foreign session that the tab belongs to.
  * @param tab The tab to open.
  * @param windowDisposition The WindowOpenDisposition flag.
  */
 public void openForeignSessionTab(
     ForeignSession session, ForeignSessionTab tab, int windowDisposition) {
   if (mIsDestroyed) return;
   NewTabPageUma.recordAction(NewTabPageUma.ACTION_OPENED_FOREIGN_SESSION);
   mForeignSessionHelper.openForeignSessionTab(mTab, session, tab, windowDisposition);
 }
 private void updateForeignSessions() {
   mForeignSessions = mForeignSessionHelper.getForeignSessions();
   if (mForeignSessions == null) {
     mForeignSessions = Collections.emptyList();
   }
 }