Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between BrowserWindow and <Webview> Tag in electron and when is it advisable to use each?

Here are links to specific parts of the electron docs:

Browser Window

Webview Tag

(Edit) The use case I was thinking about is for example if I want to build a browser, would each webpage in a tab be an instance of a Webview or a BrowserWindow? Or for instance if I wanted to build a programming editor, and I wanted to display the rendered HTML page right next to the code, would that be a new BrowserWindow or a Webview?

like image 369
Ziyad Parekh Avatar asked Jun 02 '16 21:06

Ziyad Parekh


People also ask

What is WebView in electron?

The webview tag is used to embed the 'guest' content like web pages in your Electron app. This content is contained within the webview container. An embedded page within your app controls how this content will be displayed. The webview runs in a separate process than your app.

What is WebView in HTML?

WebView is a view that display web pages inside your application. You can also specify HTML string and can show it inside your application using WebView. WebView makes turns your application to a web application. In order to add WebView to your application, you have to add <WebView> element to your xml layout file.

What is BrowserView?

A BrowserView can be used to embed additional web content into a BrowserWindow . It is like a child window, except that it is positioned relative to its owning window. It is meant to be an alternative to the webview tag.

How do I make the electron app full screen?

To make an Electron app run in full-screen mode when it's started, pass the following configuration option when creating the BrowserWindow instance: mainWindow = new BrowserWindow({fullscreen: true});


2 Answers

I can understand why it would be confusing on which of these to host your content in given their similarities. They both start in separate processes and have many similar configurations. The key difference between the BrowserWindow and the webview is that the BrowserWindow is a window on the platform and a webview is an element on an webpage This may be a bit of an obvious, superficial distinction, but much of their differences and usages derive from it.

Much of the reason a webview exists is to allow for untrusted content to be embedded within your application. If you read up on the use cases for the webview, a lot of them point to the fact that the BrowserWindow, by default, has complete access to the Node APIs. Hosting untrusted content within it is handing that content significant access to your system and presents a security concern. The webview, however, does not have Node integration turned on by default, and so it shields your application, and the platform, from the hosted content.

However, this distinction is a bit of a red herring as Node integration can be disabled on BrowserWindow and can be enabled on a webview element. That is to say, you should be able to safely host untrusted content in a BrowserWindow by taking away access to Node and host trusted content in a webview and provide it access to Node.

The key to the webview is that it allows the embedding of untrusted content on a webpage/view in your application. If, within the same view/page, you would like to have some content that is trusted with full access to Node APIs and some content that is untrusted and given limited or no access to Node APIs then this may only be accomplished with the webview element. It is a way to segregate and lock down a piece of a webpage hosted in a BrowserWindow while allowing the rest of it to be open.

Aside from embedding untrusted content, the only other case I can think of for using webviews over BrowserWindows is if you want to open and view multiple separate processes in a single window. An application could choose to create 10 different windows for 10 different processes and have the platform handle layout, focus, etc or it could open 1 window with 10 webviews for 10 different processes and handle the layout, focus, etc itself within that window.

(Edit) To address the edit to the question:

For both of these cases I would suggest using a webview.

In the first scenario, a browser, you mentioned "tabs". There is no easy, cross-platform method that I know of for building a tabbed applications using multiple BrowserWindows because the windows are created by the native OS. You could, however, achieve this by creating a tabbed application within a single webpage, each tab containing a webview. In this case you would want to ensure Node integration is disabled on the webview (it should be by default), since loading content from the web is generally untrusted.

The second scenario, an editor with rendered HTML, is not as clear cut. You could use a webview, an iframe, or render the content directly to a div. Rendering directly to a div may be the best option for something like Markdown or small snippets of HTML so long as you do not need custom css or want to execute JavaScript. It otherwise makes sense to use a webview or an iframe. The difference being that a webview starts in a separate process and may have Node integration or flexed security whereas an iframe is loaded within the same process as the BrowserWindow and, I think, has locked down security. Regardless, to get a side-by-side look without another window you need to use a HTML element like a webview and not a BrowserWindow.

like image 118
Shawn Rakowski Avatar answered Sep 29 '22 21:09

Shawn Rakowski


Electron 5 suggests using browserview/iframe instead of webview.
https://electronjs.org/docs/api/webview-tag

like image 39
Mingzhong Avatar answered Sep 29 '22 22:09

Mingzhong