-
Notifications
You must be signed in to change notification settings - Fork 51
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Local filesystem access ? #37
Comments
File URL should work in WebView2 |
In some environments, such as kiosks, local file URL's need to be disabled, so I suggest that WebView2 could have a setting to enable or disable local file URL's in the WebView2 equivalent of Windows.UI.Xaml.Controls.WebViewSettings (alongside IsIndexedDBEnabled and IsJavaScriptEnabled). However, this idea might be rejected as unnecessary because admittedly it is also possible to block URL schemes by setting |
Can definitely visit the idea once there's enough use cases behind it. For now, I think navigationStarting event is exactly for this type of url blocking scenario :). |
I have verified that local filesystem does work. somewhat. I have to specify a full path. I tried to specify a userDataDir to CreateWebView2EnvironmentWithDetails(), and then used "file://somefile.html" where somefile.html was in the EBWebView created by the control. This failed to work, with a very useless WEBVIEW2_ERROR_UNKNOWN_ERROR in the navigationcompleted callback. I suppose the userDataDir is not meant to be a location for resources ? Is there a way to bundle content in the "evergreen" version so that it is readble by the control ? For example, I'd love the ability to specify the working directory of the control. |
What do you think of the following possible solution? Two new properties could be added to IWebView2Settings like this:
Regarding the choice of naming, I've tried to make it consistent with the following members of System.Uri:
I agree. It does make it frustrating/difficult to write code that correctly handles errors when the returned error codes are very vague or ambiguous. For example, with WebView 1, I wanted to make our app behave well when a WebView error is caused by a URI with invalid syntax or invalid characters, but the error code was just "unknown". I've not yet tested what error codes WebView2 returns in response to invalid URI's. The point is, yes, it'd be great if all occurrences of |
There are a couple of issues I can see with that API design:
I suppose the idea of an API to turn off/on file scheme URIs is also redundant with being able to cancel navigation as suggested in the comment above. |
Good point. What if we say that
My intention was that whenever WebView2 navigates to a "file://" URI, it would check whether the URI is an absolute or relative URI, and if it is a relative URI, then it would use
Is that definitely correct? It depends on whether or not file scheme URIs can be used elsewhere in WebView2 in a way that does NOT trigger the NavigationStarting event. For example, frames. However, just now I found this statement in the documentation: "For subframes inside WebView, the only navigation event fired is the NavigationStarting event which gives host the ability to block subframe navigations." |
userDataDir is where the browser store user data such as cookies, not a project root. If you are navigating to a file url, relative url will not work because the browser doesn't know what that url is relative to (as opposed to navigating to file://absolutePath/index.html, which includes to a resource relative to the html file location, that'd work). The easy way to deal with this is just using absolute urls when navigating to a local resource or making a helper method to append path to your urls.
Yes, totally. You can bundle web content with your webview app and use absolute file path to navigate to it.
We can ofc consider an API to let app specify a root, but that's probably not that different from just building your own helper method. I can totally see where you're coming from with regard to the relative path though, so I think we should either include some documentation or expose an API for specifying root. |
I don't know that such an API would help much. To discover the "root", I'd have to figure out my application install location anyway. At which point I can just cache that and use it to reference all my bundled content. Which is why being able to set a working directory for the browser process would be better. If the browser process always set its working directory (or the root for file:// URIs) to the working directory of the parent process, then users can specify either full paths or paths relative to their install. |
I would like this feature, but it only works if there exists a way to determine whether a given "file://" URI is relative or absolute, and this is not quite as simple as I first thought, so my suggestion might have to be rejected, maybe.
However, considering that WebView2's internal code doesn't/can't use
Cross-platform issues come into consideration. So, maybe what @liminzhu said is the best solution:
|
What will happen if the UWP wrapper of WebView2 is navigated to a "ms-appx://" or "ms-appdata://" URI? Will it return an error or will it support these UWP URI's? Various special folders are also accessible, for example There are 2 URI schemes there: "ms-appx" and "ms-appdata". |
I think you're overthinking it. as long as WebView2 clearly documents what the working directory is the path does not matter. Why would WebView2 need to figure out if the path is relative or absolute ? That could actually be dangerous (as path canonicalization can be a can of worms) One other thing I am curious about is what happens when I bundle WebView2 in my app (instead of using the installed version). I presume now my paths will be relative to the install directory, which is not consistent with the other way. |
For example, if you pass the URI
Then I suppose that @liminzhu was right. |
I might be missing something here. Setting the project root as I mentioned or the working directory for the browser process as you mentioned sound the same thing to me. The end result is app developers have to tell WebView where that root is, so that developer can use relative path for file navigation. Either way, it doesn't offer a lot of value considering a helper method is fairly easy.
@david-risney and I were talking this earlier actually. In UWP world webview can know for sure where the app is installed, so maybe we can make UWP URI work. I will consult with the UWP team implementing the wrapper as well. |
To clarify my line of thought: Setting the project root would be equivalent if it only affects relative paths, and let's absolute pass behave as they currently do. You can probably see that the two cases are not equivalent, though. When you set the working directory and don't do anything else, you leave the path resolution to the OS. If you have a custom API, and still want to support all paths, you will have to figure out if a path is relative or absolute, because you will presumably pass a canonicalized path to the browser control. This implies more code to write and test, which I'm always morally opposed to :) If setting the project root acts as a filter for file:// URIs, then I'd rather we keep the current behavior. It's not great, but at least it doesn't block absolute paths. |
@liminzhu wrote:
I guess you already have the following technique in the list of possible solutions:
Obviously the more difficult question is how to handle the case where the
Considering the details of how to make the above redirection operate "invisibly" and reliably, I wonder whether a simpler solution would be to make Win32/C++ WebView2 support
|
@amoldeshpande -- When WebView2 is out of beta, or when the wrappers are released, are you planning to use WebView2 in a UWP, WPF, or WinForms app? I ask because if you're planning to use it in UWP, then you could get what you want by using If you're not planning to use UWP, then you might still be able to use
Although I agree in general, I'd also find it understandable and reasonable if the Win32/C++ version of WebView2 requires more manual configuration than the UWP version of WebView2. The UWP wrapper is expected to add an extra layer of friendliness and convenience atop the lower-level Win32/C++ WebView2 layer. |
@verelpode I'm writing a purely C++, Win32(for now) app. You're right, if I was using UWP I wouldn't have this problem. |
BTW I don't know if there is anything sensitive info in the MSEdge install directory, but a WebView2 control can basically read anything under |
based on example: ...this could allow the host application to "intercept"(hook) the navigation and respond with special response... e.g., something like args->put_Result(string_value). |
There's a lot of great feature requests in this issue. We will want to look into some kind of ms-appdata/ms-appx URI scheme likely with a configurable root path and that should also work in Win32. We will also want something like NavigateToLocalStreamUri where you can implement a callback or event to resolve custom URI schemes into data streams. In the interim as a workaround you can use file URIs and WebResourceRequested. Regarding relative file URIs: Rather than change our interpretation of file URIs we'd prefer a solution like ms-appdata/ms-appx where we can have a default root and the path of the URI is relative to that. We might vary the default depending on UWP vs Win32 and make the root configurable. Regarding access to file URIs: The current behavior matches the browser. If you're not running in an app container and your process has access to most files on the disk, the WebView2 will also. This is just like the browser, but just like the browser, websites cannot read the contents of one another or arbitrary files on your disk (without explicit intervention by the end user via a file control and so on) from the same origin policy. |
NavigateToLocalStreamUri in combination with a IUriToStreamResolver would be really nice! |
Is it possible to use the Win32 C++ NavigateToString(LPCWSTR htmlContent) method to load an HTML file embedded as a .rc resource? |
@carlsound You should be able to, assuming you can get the html string out of resources in your host app to send to the WebView2. Are you running into a problem? If so, could you open a new issue about it? |
This worked:
}` |
Is it possible for HTML to reference a .rc embedded image resource in a Win32 C++ application? |
Hm, unfortunately I'm not sure if this is possible currently. The regular answer would be to capture the request for the image through the WebResourceRequested event and then provide the image from the resource, but there seems to be a bug where WebResourceRequested event doesn't get fired for HTML in NavigateToString. That issue is tracked in #530. Do you have control over the HTML page that you are loading? As a workaround, you may be able to pass the image to the HTML through other mechanisms, such as WebMessageReceived/postMessage + ExecuteScript, and/or by using a host object. |
We have improved support for local file access using the SetVirtualHostNameToFolderMapping API available in 1.0.781-prerelease SDK. Please give it a try and let us know if it isn't working for you. Thanks! |
My use case is a Maps web control, displaying local pictures according to their GPS EXIF info. Will it be possible to use file:// URLs directly in WebView2 ?
AB#27073119
The text was updated successfully, but these errors were encountered: