Class BrowserTab
- All Implemented Interfaces:
Serializable
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 Summary
Modifier and TypeMethodDescriptionaddDestroyListener(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.static BrowserTabGets the browser tab that the given UI is loaded in.<T> @Nullable TgetAttribute(Class<T> type) Gets a value stored in this browser tab.@Nullable ObjectgetAttribute(String name) Gets a value stored in this browser tab.static @Nullable BrowserTabGets the browser tab of the current UI.getId()Gets the id of this browser tab.Gets the session this browser tab belongs to.<T> voidsetAttribute(Class<T> type, @Nullable T value) Stores a value in this browser tab.voidsetAttribute(String name, @Nullable Object value) Stores a value in this browser tab.
-
Method Details
-
getCurrent
Gets the browser tab of the current UI.- Returns:
- the browser tab of the current UI, or
nullif there is no current UI - See Also:
-
get
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 theBrowserTabInitListeners of the service about it.- Parameters:
ui- the UI to get the browser tab for, notnull- Returns:
- the browser tab of the UI, not
null - Throws:
IllegalStateException- if the UI does not belong to a session
-
getId
Gets the id of this browser tab. The id is thewindow.nameof 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
Gets the session this browser tab belongs to.- Returns:
- the session of this browser tab, not
null
-
setAttribute
Stores a value in this browser tab. Anullvalue 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, notnullvalue- the value to store, ornullto remove the attribute- Throws:
IllegalStateException- if this browser tab is being destroyed or has been destroyed
-
setAttribute
Stores a value in this browser tab. Anullvalue 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, notnullvalue- the value to store, ornullto remove the attribute- Throws:
IllegalStateException- if this browser tab is being destroyed or has been destroyed
-
getAttribute
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, notnull- Returns:
- the stored value, or
nullif there is none - Throws:
IllegalStateException- if this browser tab has been destroyed
-
getAttribute
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, notnull- Returns:
- the stored value, or
nullif there is none - Throws:
IllegalStateException- if this browser tab has been destroyed
-
addDestroyListener
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 thangetCurrent(), which returns the browser tab of the UI of the request that happens to run the cleanup, ornullwhen the session is being destroyed.- Parameters:
listener- the listener to add, notnull- Returns:
- a handle for removing the listener
- Throws:
IllegalStateException- if this browser tab is being destroyed or has been destroyed
-