blob: aaf7312a655301931344ef0c8248f7d85a493691 [file] [log] [blame] [view]
avia41f7af2015-08-31 19:46:581# Tab Helpers
2
3The `content/` layer of Chromium has a class called `WebContents`, which is one
4of the most basic building blocks of all of Chromium. This document describes
5how `WebContents`es are used to build tabs in browser windows.
6
Erik Chen50f7634b2024-04-29 21:14:227Tab Helpers are deprecated for Desktop Chrome. Use TabFeatures instead. See
8[design principles](chrome_browser_design_principles.md).
9
avia41f7af2015-08-31 19:46:5810[TOC]
11
12## Introduction
13
14What is a "tab helper"? It is a `WebContentsObserver` owned by the `WebContents`
15itself. Let's break that down.
16
Elly Fong-Jones8ea0fc762021-01-06 19:14:2317## WebContentsObserver
avia41f7af2015-08-31 19:46:5818
19`WebContentsObserver` is a
Elly Fong-Jones8ea0fc762021-01-06 19:14:2320[simple interface](https://source.chromium.org/chromium/chromium/src/+/HEAD:content/public/browser/web_contents_observer.h)
avia41f7af2015-08-31 19:46:5821that allows an object to observe events in the life of a `WebContents`. As an
22example, if we look at the `TabStripModel`, there are times when it need to
23watch out for WebContents being deleted. So it creates a
Elly Fong-Jones8ea0fc762021-01-06 19:14:2324[TabStripModel::WebContentsData](https://source.chromium.org/chromium/chromium/src/+/HEAD:chrome/browser/ui/tabs/tab_strip_model.cc).
Suman Nelson Kancherla1d0cc352018-12-06 20:26:2825That object overrides `WebContentsDestroyed()`, and when a
26`WebContents` gets destroyed, the callback is called and the object
27processes the message. Note that `TabStripModel::WebContentsData` object is not owned by the
avia41f7af2015-08-31 19:46:5828`WebContents`. It is owned indirectly by the `TabStripModel`.
29
Elly Fong-Jones8ea0fc762021-01-06 19:14:2330## SupportsUserData and WebContentsUserData
avia41f7af2015-08-31 19:46:5831
32There is a mechanism used in Chromium called
Josip Sokcevicba144412020-09-09 20:57:0533[`SupportsUserData`](https://source.chromium.org/chromium/chromium/src/+/HEAD:base/supports_user_data.h)
avia41f7af2015-08-31 19:46:5834that allows attaching of arbitrary objects to an object. The mechanism is
35simple: host objects derive from `SupportsUserData`, and owned objects derive
36from `SupportsUserData::Data`. There are three calls to attach and detach the
37data.
38
39`WebContents` derives from `SupportsUserData`, so that mechanism works for
40attaching objects to a `WebContents`, but the `SupportsUserData` mechanism is a
41bit low-level. A higher level abstraction is
Josip Sokcevicba144412020-09-09 20:57:0542[`WebContentsUserData`](https://source.chromium.org/chromium/chromium/src/+/HEAD:content/public/browser/web_contents_user_data.h),
avia41f7af2015-08-31 19:46:5843which is easy to derive from and has easy-to-use functionality in
44`CreateForWebContents()` and `FromWebContents()`.
45
46## Adding a feature to a browser tab
47
48Let's combine `WebContentsObserver` and `WebContentsUserData` together, to log
49whenever the title of a tab changes.
50
51```
52class TitleLoggerTabHelper
53 : public content::WebContentsObserver,
54 public content::WebContentsUserData<TitleLoggerTabHelper> {
55 public:
Johann16cf06fd2020-09-14 22:55:5556 TitleLoggerTabHelper(const TitleLoggerTabHelper&) = delete;
57 TitleLoggerTabHelper& operator=(const TitleLoggerTabHelper&) = delete;
avia41f7af2015-08-31 19:46:5858 ~TitleLoggerTabHelper() override;
59
60 // content::WebContentsObserver
Avi Drissman93002212017-09-27 03:20:5261 void TitleWasSet(NavigationEntry* entry) override {
avia41f7af2015-08-31 19:46:5862 LOG(INFO) << "Title: " << entry->GetTitle();
63 }
64
65 private:
66 explicit TitleLoggerTabHelper(content::WebContents* web_contents);
67 friend class content::WebContentsUserData<TitleLoggerTabHelper>;
avia41f7af2015-08-31 19:46:5868};
avia41f7af2015-08-31 19:46:5869```
70
71We want each tab to have this `WebContentsObserver` attached to it, so that it
72will properly handle the events it's looking for, and when the tab goes away,
73then this tab helper will go away too.
74
75But how do you hook in to browser tab creation? How can we attach this tab
76helper to the `WebContents`es that are used for the browser tabs?
77
78## AttachTabHelpers
79
80There is a function called
Josip Sokcevicba144412020-09-09 20:57:0581[`AttachTabHelpers()`](https://source.chromium.org/chromium/chromium/src/+/HEAD:chrome/browser/ui/tab_helpers.cc;).
avia41f7af2015-08-31 19:46:5882Whenever a `WebContents` is created for use as a browser tab,
83`AttachTabHelpers()` is called. Every tab helper from around Chromium,
84from ContentSettings to Favicons to History to Prefs, all take this opportunity
85to hook into those `WebContents` used as tabs.
86
87If you are writing a feature that needs to deal with browser tabs, this is where
88you go. Create a tab helper, and add it (in alphabetical order, please!) to
89`AttachTabHelpers()`. Note, though, that you are _never_ allowed to call
90`AttachTabHelpers()` yourself. `AttachTabHelpers()` is only for `WebContents`
91that are in browser tabs, and all of those code paths are already written.
92
Elly Fong-Jones8ea0fc762021-01-06 19:14:2393## Reusing tab helpers with non-browser tab WebContentses
avia41f7af2015-08-31 19:46:5894
95Sometimes it's useful to re-use tab helpers for `WebContents`es that aren't
96browser tabs. For example, the Chrome Apps code wants to be able to print, and
97wants to use the printing code that browser tabs use. So in
Josip Sokcevicba144412020-09-09 20:57:0598[`ChromeAppDelegate::InitWebContents()`](https://source.chromium.org/chromium/chromium/src/+/HEAD:chrome/browser/ui/apps/chrome_app_delegate.cc)
avia41f7af2015-08-31 19:46:5899we see that whenever the Apps code creates a new `WebContents`, it attaches a
100carefully-chosen subset of tab helpers, including two printing ones.
101
102You can do that too. If you are creating a `WebContents`, make a very deliberate
103decision about which tab helpers you need. Chances are, you don't need them all;
104you probably only need a handful. In fact, most tab helpers assume they are
105attached to browser tabs, so only add the bare minimum.
106
Elly Fong-Jones8ea0fc762021-01-06 19:14:23107## Not every WebContents has every tab helper
avia41f7af2015-08-31 19:46:58108
109The other consequence of this design is that you can't make the assumption that
110an arbitrary `WebContents` will have an arbitrary tab helper. The
111`WebContents`es used as browser tabs likely will have most tab helpers (though
112not necessarily all of them!) but a `WebContents` only has a tab helper if it is
113installed on it.
114
115The deeper (false and dangerous) assumption is that every `WebContents` is a
116browser tab. Do not assume that either!
117
118If your code handles `WebContents`es, be aware of their source. It is extremely
119rare to have to be able to handle arbitrary `WebContents`es. Know where they
120come from and what tab helpers are on them, and you'll be fine.