Skip to content

Webview Api Prototype - #41853

Closed
Matt Bierner (mjbvz) wants to merge 4 commits into
microsoft:masterfrom
mjbvz:webview-api
Closed

Matt Bierner (mjbvz) wants to merge 4 commits into
microsoft:masterfrom
mjbvz:webview-api

Conversation

@mjbvz

@mjbvz Matt Bierner (mjbvz) commented Jan 19, 2018 •

Copy link
Copy Markdown
Contributor

#41047

Proposes a new Api for webviews. This API aims to make the current html preview api more usable, safer, and more extensible for the future.

Besides the new API, the new webviews also differ in several important ways:

  • They are no longer loaded in the file:// origin so they can't read files on disk (you have to use the new protocols for this)
  • Scripts are disabled by default
  • Command uris are disabled by default
  • postMessage inside of webviews now posts back to vscode directly (no more weird click faking required)

TODO

  • Discuss and finalize API
  • Add onFocus and onBlur to the webview object
  • See if implementation can be cleaned up
  • Add dispose method on webview
  • Add editorColumn property on webview
  • Make enableCommandUris option actually enable command uris
  • Add a new protocol for accessing a resource inside of a workspace folder (markdown loading a local image for example)
  • Finish moving markdown extension to use the new API

Matt Bierner added 4 commits January 19, 2018 18:42
Proposes a new Api for creating webviews. This API aims to make the current html preview api more usable, safer, and more extensible for the future.
@mike-lischke

Mike Lischke (mike-lischke) commented Jan 22, 2018 •

Copy link
Copy Markdown

Very nice to see there's further development going on with the previewHTML feature. Btw, that name "previewHTML" is probably not a good choice. I use it not at all for HTML (related) previews but as a custom renderer. A better name is probably "embeddedHTML" or similar...

And here a few points I have:

  • There should be a differentiation between a real document refresh and a pure switch from a different editor tab. Currently the text provider is asked for new HTML content in both cases.
  • I'm not a big fan of disabling scripts by default. It's not that easy to start with previewHTML if you are totally new and having to dig how to enable scripts would be another stumbling stone. Though, why not make it an option when you open such a view?
  • By "command URIs are disabled by default" do you mean there's no communication possible between vscode and the view by default? This goes in the same realm like the scripting. After reworking of previewHTML things should be simpler, not harder. But maybe I misunderstand here.

One big wish I have here, however, is better communication between the HTML view and the extension. Take for instance this diagram I have in my antlr4-vscode extension:

image

This is a D3.js generated graph with interaction (e.g. the nodes can be moved with the mouse, you can scale and pan the view etc.). However, having to reposition the nodes each time you open the view would be a nightmare, so I store the positions when a new provide-html-text request comes in. For that I need a pretty dirty hack, by sending a message to the view, which then sends back status to the extension. There should be a way to directly interact with the view. Maybe implement an eval() method in the extension to allow running some code and return data? That could also be used to set data in case something in the display should change (say, a rename) without re-creating the entire HTML (which might involve show animations). If that could be made possible I'd be super happy.

@mjbvz

Matt Bierner (mjbvz) commented Jan 22, 2018 •

Copy link
Copy Markdown
Contributor Author

Mike Lischke (@mike-lischke) These changes will not effect the existing previewHtml functionality. That will continue to be supported as-is.

As to your points:

  • In this new model, extensions push changes to the webview instead of having the webview pull changes. We still will not persist the webview at this stage however (it will be created and destroyed behind the screens when you switch away from it)
  • The reason for disabling scripts by default is to make the API safer and more explicit for extension authors. It is too easy to accidentally allow unsafe script injection in an html preview. By disabling scripts and other potentially unsafe features by default, we give extension authors a safe base to start with
  • Under this proposal, webview->extension communication would have an official API (see postMessage and onMessage) instead of using the command uri hack

@mike-lischke

Mike Lischke (mike-lischke) commented Jan 22, 2018 •

Copy link
Copy Markdown

In this new model, extensions push changes to the webview instead of having the webview pull changes. We still will not persist the webview at this stage however (it will be created and destroyed behind the screens when you switch away from it)

Ah, a new approach instead of reworking the old one. I'm fine with that as well. Pushing content will allow for much better interaction (e.g to push data changes which is then visualized with animations).

The reason for disabling scripts by default is to make the API safer and more explicit for extension authors. It is too easy to accidentally allow unsafe script injection in an html preview.

To be honest I don't buy this argument. What is safer here? If someone wants to use scripts he will enable them otherwise it doesn't matter if they are enabled or not. It's not like someone by accident adds some dangerous code and by accident doesn't realize what he did and which is by accident then used by a bad mind. All what disabling scripting by default does is to make it's use more difficult for those that actually need it. I revert my opinion. Just look at the Specter and Meltdown desaster to see how important security is, also in this context.

Under this proposal, webview->extension communication would have an official API (see postMessage and onMessage) instead of using the command uri hack

Good, an official API is way better. Still, it would also require to send messages around, which is suboptimal, as it may require to send large pieces of text, URL encoded, as parameters of a URI. All but elegant. There should be something to directly execute JS to set and get values.

@mjbvz

Copy link
Copy Markdown
Contributor Author

Mike Lischke (@mike-lischke)

  • Just to be clear, this new API only allows updating the entire html content or posting messages. It does not add any react-style updating of webviews.

  • Messaging. What we say by default disabling scripts is that scripts are dangerous and that you should only enable them if you really need them. And if you do enable scripts, you are now responsible for the consequences

  • There is no way to avoid posting message here. Any evalish helper we could add would boil down to posting messages. And eval is not a pattern we should encourage in the first place. Far better IMO to have message passing than relying on extensions trying to eval js in a remote context

@jrieken Johannes Rieken (jrieken) left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Quick scan looks good. Not the editor-input-expert but I believe that's how you should use it.

@mjbvz Matt Bierner (mjbvz) mentioned this pull request Feb 1, 2018
6 of 8 tasks
@mjbvz

Copy link
Copy Markdown
Contributor Author

Moved work onto branch on master: #42690

@github-actions github-actions Bot locked and limited conversation to collaborators Mar 30, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants