components/guest_view/browser/slim_web_view/README.md
SlimWebview is a minimal, non-extensions-based implementation of <webview>
for Chrome WebUIs.
It is used only on Android mobile, where extensions are unavailable.
When implementing a WebUI that runs both on desktop platforms and
Android mobile, you should check the ENABLE_EXTENSIONS_CORE buildflag to
decide which implementation of <webview> to use.
To enable SlimWebview in your WebUI:
In your MojoWebUIController implementation:
content::BindingsPolicyValue::kSlimWebView bindings policy.kGuestViewSharedResources from
guest_view_shared_resources_map.h
to your WebUIDataSource.Example from glic_ui.cc:
#if !BUILDFLAG(ENABLE_EXTENSIONS_CORE)
auto bindings = web_ui->GetBindings();
bindings.Put(content::BindingsPolicyValue::kSlimWebView);
web_ui->SetBindings(bindings);
source->AddResourcePaths(kGuestViewSharedResources);
#endif
Your WebUIController should directly inherit from guest_view::SlimWebViewPageHandlerFactory.
Example from glic_ui.h:
class GlicUI : public ui::MojoWebUIController,
#if !BUILDFLAG(ENABLE_EXTENSIONS_CORE)
public guest_view::SlimWebViewPageHandlerFactory,
#endif
...
public:
#if !BUILDFLAG(ENABLE_EXTENSIONS_CORE)
using SlimWebViewPageHandlerFactory::BindInterface;
#endif
...
private:
#if !BUILDFLAG(ENABLE_EXTENSIONS_CORE)
using SlimWebViewPageHandlerFactory::CreatePageHandler;
#endif
Register the factory in chrome/browser/chrome_browser_interface_binders_webui_parts_features.cc.
#if !BUILDFLAG(ENABLE_EXTENSIONS_CORE)
RegisterWebUIControllerInterfaceBinder<guest_view::mojom::PageHandlerFactory,
glic::GlicUI,
your_namespace::YourUI>(map);
#endif
You need to add ts_path_mappings for the shared SlimWebview resources and include the dependencies conditionally.
Example from chrome/browser/resources/glic/BUILD.gn:
if (enable_guest_view && !enable_extensions_core) {
ts_deps += [ "//chrome/browser/resources/guest_view_shared:build_ts" ]
ts_path_mappings +=
[ "/shared/guest_view/*|" + rebase_path(
"$root_gen_dir/chrome/browser/resources/guest_view_shared/tsc/*",
target_gen_dir) ]
}
You can use a shared type to support both webview implementations conditionally.
Example from web_view_type.ts:
// <if expr="not enable_extensions_core">
import '/shared/guest_view/slim_webview.js';
// </if>
import type {SlimWebviewElement} from '/shared/guest_view/slim_webview.js';
export type WebViewType = chrome.webviewTag.WebView|SlimWebviewElement;
Use preprocess if expressions to import slim_webview.js only when extensions
are not enabled.
// <if expr="not enable_extensions_core">
import '/shared/guest_view/slim_webview.js';
// </if>
The markup remains the same as a standard <webview>, but it will be backed by
SlimWebviewElement on Android.