Skip to content

Commit b6f5c1b

Browse files
authored
Create HostResourceMapping.md
1 parent 4a0c576 commit b6f5c1b

File tree

1 file changed

+103
-0
lines changed

1 file changed

+103
-0
lines changed

specs/HostResourceMapping.md

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
# Background
2+
We have received feedbacks that there is a need to access local files in WebView2. In this document we describe the APIs for this scenario. We'd appreciate your feedback.
3+
4+
5+
# Description
6+
To access local files in WebView2, define a virutal host name to local folder mapping using the `AddVirtualHostNameToFolderMapping` API,
7+
and then refer to local files under the folder with a normal http/https url with the virtual host name.
8+
9+
# Examples
10+
Suppose the app has index.html file next to its exe file. The following code snippet demonstrates how to define a mapping
11+
for `app-file.invalid` host name so that it can be accessed from pages using normal http/https url like https://app-file.invalid/index.html.
12+
13+
## Win32 C++
14+
```cpp
15+
webview2->AddVirtualHostNameToFolderMapping(
16+
L"app-file.invalid", L".", COREWEBVIEW2_HOST_RESOURCE_ACCESS_KIND_DENY_CORS);
17+
webview2->Navigate("https://app-file.invalid/index.html");
18+
```
19+
20+
## .Net and WinRT
21+
```c#
22+
webView.CoreWebView2.AddVirtualHostNameToFolderMapping(
23+
"app-file.invalid", ".", CoreWebView2HostResourceAccessKind.DenyCors);
24+
webView.Navigate("https://app-file.invalid/index.html");
25+
```
26+
27+
# Remarks
28+
Choose a virtual host name that will not be used by real sites.
29+
If you own a domain (like example.com), an option is to use a sub domain reserved for the app (like my-app.example.com).
30+
If you don't own a domain, [RFC 2606](https://tools.ietf.org/html/rfc2606#section-2) has reserved several top level domain that would
31+
not be used by real sites, like .example, .test, and .invalid.
32+
33+
Give only minimal cross orign access neccessary to run the app. If there is no need to access local resources
34+
from other sites, use COREWEBVIEW2_HOST_RESOURCE_ACCESS_KIND_DENY.
35+
36+
# API Notes
37+
See [API Details](#api-details) section below for API reference.
38+
39+
# API Details
40+
41+
## Win32 C++
42+
43+
```IDL
44+
/// Kind of cross origin resource access allowed for host resources during download.
45+
/// Note that other normal access checks like same origin DOM access check and [Content
46+
/// Security Policy](https://developer.mozilla.org/en-US/docs/Web/HTTP/CSP) still apply.
47+
[v1_enum]
48+
typedef enum COREWEBVIEW2_HOST_RESOURCE_ACCESS_KIND {
49+
/// All cross origin resource access is denied, including normal sub resource access
50+
/// as src of a script or image element.
51+
COREWEBVIEW2_HOST_RESOURCE_ACCESS_KIND_DENY,
52+
53+
/// All cross origin resource access is allowed, including accesses that are
54+
/// subject to Cross-Origin Resource Sharing(CORS) check. The behavior is similar to
55+
/// a web site sends back http header Access-Control-Allow-Origin: *.
56+
COREWEBVIEW2_HOST_RESOURCE_ACCESS_KIND_ALLOW,
57+
58+
/// Cross origin resource access is allowed for normal sub resource access like
59+
/// as src of a script or image element, while any access that subjects to CORS check
60+
/// will be denied.
61+
/// See [Cross-Origin Resource Sharing](https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS)
62+
/// for more information.
63+
COREWEBVIEW2_HOST_RESOURCE_ACCESS_KIND_DENY_CORS,
64+
} COREWEBVIEW2_HOST_RESOURCE_ACCESS_KIND;
65+
66+
interface ICoreWebView2_2 : ICoreWebView2 {
67+
68+
/// Add a host name mapping for host resources in a folder.
69+
/// After adding the mapping, the app can then use http or https urls with the specified hostName as host name
70+
/// of the urls to access files in the local folder specified by folderPath.
71+
/// accessKind specifies the kind of access control to the host resources from other sites.
72+
/// Relative folderPath is supported and interpreted as relative to the exe path of the app.
73+
/// For example, after calling AddVirtualHostNameToFolderMapping(L"app-file.invalid", L".",
74+
/// COREWEBVIEW2_HOST_RESOURCE_ACCESS_KIND_DENY), navigating to https://app-file.invalid/my-local-file.html
75+
/// will show content from my-local-file.html in the same folder as the app's exe file.
76+
HRESULT AddVirtualHostNameToFolderMapping(
77+
[in] LPCWSTR hostName,
78+
[in] LPCWSTR folderPath,
79+
[in] COREWEBVIEW2_HOST_RESOURCE_ACCESS_KIND accessKind);
80+
/// Remove a host name mapping for local folder that was added by AddVirtualHostNameToFolderMapping.
81+
HRESULT RemoveVirtualHostNameToFolderMapping(
82+
[in] LPCWSTR hostName);
83+
}
84+
```
85+
86+
```c#
87+
namespace Microsoft.Web.WebView2.Core
88+
{
89+
public enum CoreWebView2HostResourceAccessKind
90+
{
91+
Deny = 0,
92+
Allow = 1,
93+
DenyCors = 2
94+
}
95+
96+
public partial class CoreWebView2
97+
{
98+
// There are other API in this interface that we are not showing
99+
public void AddVirtualHostNameToFolderMapping(string hostName, string folderPath, CoreWebView2HostResourceAccessKind accessKind);
100+
public void RemoveVirtualHostNameToFolderMapping(string hostName);
101+
}
102+
}
103+
```

0 commit comments

Comments
 (0)