Class BrowserTab

java.lang.Object
com.vaadin.flow.server.BrowserTab
All Implemented Interfaces:
Serializable

@NullMarked public final class BrowserTab extends Object implements Serializable
Holds state for one browser tab, shared by every UI that is loaded in that tab during the session.

A new UI instance is created whenever the browser loads the application page: on reload, when the user types a URL or opens a bookmark, and when the back button returns from another page. Data stored in the UI, in a view or in a layout is gone after that, unless the route chain is preserved with @PreserveOnRefresh, which only covers a reload of the same URL. Attributes stored in a browser tab stay available to all the UIs that are loaded in the same tab, until the tab is destroyed:

 BrowserTab tab = BrowserTab.getCurrent();
 Booking booking = tab.getAttribute(Booking.class);
 if (booking == null) {
     booking = new Booking();
     tab.setAttribute(Booking.class, booking);
 }
 
The browser tab of a UI is available already in a UIInitListener, before any route target or layout of the UI is created. To initialize every browser tab of the application in one place, register a BrowserTabInitListener with VaadinService.addBrowserTabInitListener(BrowserTabInitListener). It is notified once for each new browser tab, before the UIInitListeners of the UI that is loaded first in the tab.

The tab is identified by the window.name of the browser window, which the Vaadin client sets to a random value when the application is loaded in a window that does not have a name yet. The identification is therefore best effort, with these caveats:

  • Whether a duplicated tab, a reopened closed tab or a tab restored after the browser was quit or crashed keeps its window name is up to the browser. In tests with current Chromium, Firefox and Safari versions, all of them got a new window name and therefore a new browser tab, but do not rely on either outcome.
  • Browsers clear the window name when the tab navigates to another site, so returning to the application from another site, for example after a redirect from a payment provider or a login service, starts a new browser tab.
  • Application code that assigns window.name, or opens the application with a named target, changes the identity of the tab.
  • When the window name of a UI is not known, which is the case for applications that are only embedded as web components, the browser tab lives only as long as that one UI.

The browser does not tell the server when a tab is closed. A closed tab can not be told apart from a tab that navigated to another page and returns later, or from a tab that is being reloaded. A browser tab is therefore destroyed only when no UI of the tab has sent a heartbeat for the same time that it takes to close an inactive UI (three missed heartbeats, see DeploymentConfiguration.getHeartbeatInterval()), or when the session is destroyed. The check runs at the end of a request to the session, so a browser tab can stay around for longer. Use addDestroyListener(SerializableConsumer) to release resources held by the tab.

The browser tab is stored in the VaadinSession and all its methods require the session to be locked, like the session attributes.

See Also:
  • Method Details

    • getCurrent

      public static @Nullable BrowserTab getCurrent()
      Gets the browser tab of the current UI.
      Returns:
      the browser tab of the current UI, or null if there is no current UI
      See Also:
    • get

      public static BrowserTab get(UI ui)
      Gets the browser tab that the given UI is loaded in. Creates the browser tab if none of the UIs in the tab has used it yet, and notifies the BrowserTabInitListeners of the service about it.
      Parameters:
      ui - the UI to get the browser tab for, not null
      Returns:
      the browser tab of the UI, not null
      Throws:
      IllegalStateException - if the UI does not belong to a session
    • getId

      public String getId()
      Gets the id of this browser tab. The id is the window.name of the browser window, or a random value if the window name of the UI that created the browser tab was not known.
      Returns:
      the id of this browser tab, not null
    • getSession

      public VaadinSession getSession()
      Gets the session this browser tab belongs to.
      Returns:
      the session of this browser tab, not null
    • setAttribute

      public void setAttribute(String name, @Nullable Object value)
      Stores a value in this browser tab. A null value removes the attribute.

      The version that takes a string uses the string as the name of the attribute, the version that takes a class uses the fully qualified name of the class.

      Parameters:
      name - the name of the attribute, not null
      value - the value to store, or null to remove the attribute
      Throws:
      IllegalStateException - if this browser tab is being destroyed or has been destroyed
    • setAttribute

      public <T> void setAttribute(Class<T> type, @Nullable T value)
      Stores a value in this browser tab. A null value removes the attribute.

      The version that takes a class uses the fully qualified name of the class as the name of the attribute, the version that takes a string uses the string.

      Type Parameters:
      T - the type of the value
      Parameters:
      type - the type that names the attribute, not null
      value - the value to store, or null to remove the attribute
      Throws:
      IllegalStateException - if this browser tab is being destroyed or has been destroyed
    • getAttribute

      public @Nullable Object getAttribute(String name)
      Gets a value stored in this browser tab.

      The version that takes a string looks up the attribute by the given name, the version that takes a class looks it up by the fully qualified name of the class.

      Parameters:
      name - the name of the attribute, not null
      Returns:
      the stored value, or null if there is none
      Throws:
      IllegalStateException - if this browser tab has been destroyed
    • getAttribute

      public <T> @Nullable T getAttribute(Class<T> type)
      Gets a value stored in this browser tab.

      The version that takes a class looks up the attribute by the fully qualified name of the class, the version that takes a string looks it up by the given name.

      Type Parameters:
      T - the type of the value
      Parameters:
      type - the type that names the attribute, not null
      Returns:
      the stored value, or null if there is none
      Throws:
      IllegalStateException - if this browser tab has been destroyed
    • addDestroyListener

      public Registration addDestroyListener(SerializableConsumer<BrowserTab> listener)
      Adds a listener that is run when this browser tab is destroyed, either because none of its UIs has been active for the heartbeat timeout or because the session is destroyed. The listener receives the destroyed browser tab, whose attributes can still be read, but not changed, while the listeners run. Use the given browser tab rather than getCurrent(), which returns the browser tab of the UI of the request that happens to run the cleanup, or null when the session is being destroyed.
      Parameters:
      listener - the listener to add, not null
      Returns:
      a handle for removing the listener
      Throws:
      IllegalStateException - if this browser tab is being destroyed or has been destroyed