Skip to content

Commit be6c08f

Browse files
authored
Add OriginalSourceFrameInfo to NewWindowRequestedEventArgs
* add new window request source frame info
1 parent 4adaf66 commit be6c08f

File tree

1 file changed

+139
-0
lines changed

1 file changed

+139
-0
lines changed

specs/NewWindowSourceFrameInfo.md

Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
1+
Source Frame Info for New Window Requested
2+
===
3+
4+
# Background
5+
Currently there is no way to determine the source frame of a new window request. This information
6+
can be useful when deciding how to open the content. For example, you may want to open requests
7+
that originate in a 3rd party frame in the default browser instead of in a new WebView. The
8+
WebView2 team is extending `NewWindowRequestedEventArgs` with an `OriginalSourceFrameInfo` property
9+
to make this easier. Here we described the updated API.
10+
11+
# Examples
12+
## OriginalSourceFrameInfo on NewWindowRequestedEventArgs
13+
```c#
14+
// Register a handler for the NewWindowRequested event.
15+
// This handler will check the source frame info to determine how to open the
16+
// request. Depending on the source frame URI, it will provide a new app window
17+
// or open using the default browser.
18+
webView.CoreWebView2.NewWindowRequested += delegate (
19+
object webView, CoreWebView2NewWindowRequestedEventArgs args)
20+
{
21+
// The host can decide how to open based on source frame info,
22+
// such as URI. For example, if the source is a 3rd party frame, open using
23+
// the default browser.
24+
bool useDefaultBrowser = IsThirdPartySource(
25+
args.OriginalSourceFrameInfo.Source);
26+
if (useDefaultBrowser)
27+
{
28+
ProcessStartInfo startInfo = new ProcessStartInfo
29+
{
30+
FileName = args.OriginalSourceFrameInfo.Source,
31+
// Open the URI in the default browser.
32+
UseShellExecute = true
33+
};
34+
Process.Start(startInfo);
35+
args.Handled = true;
36+
}
37+
else
38+
{
39+
CoreWebView2Deferral deferral = args.GetDeferral();
40+
MainWindow main_window = new MainWindow(
41+
webView.CreationProperties, args.Uri);
42+
main_window.OnWebViewFirstInitialized = () =>
43+
{
44+
using (deferral)
45+
{
46+
args.Handled = true;
47+
args.NewWindow = main_window.webView.CoreWebView2;
48+
}
49+
};
50+
main_window.Show();
51+
}
52+
};
53+
```
54+
```cpp
55+
// Register a handler for the NewWindowRequested event.
56+
// This handler will check the source frame info to determine how to open the
57+
// request. Depending on the source frame URI, it will provide a new app window
58+
// or open using the default browser.
59+
CHECK_FAILURE(m_webView->add_NewWindowRequested(
60+
Callback<ICoreWebView2NewWindowRequestedEventHandler>(
61+
[this](ICoreWebView2* sender, ICoreWebView2NewWindowRequestedEventArgs* args)
62+
{
63+
bool useDefaultBrowser = false;
64+
Microsoft::WRL::ComPtr<ICoreWebView2NewWindowRequestedEventArgs3> args3;
65+
if (SUCCEEDED(args->QueryInterface(IID_PPV_ARGS(&args3))))
66+
{
67+
Microsoft::WRL::ComPtr<ICoreWebView2FrameInfo> frame_info;
68+
if (SUCCEEDED(args3->get_OriginalSourceFrameInfo(&frame_info))
69+
&& frame_info)
70+
{
71+
// The host can decide how to open based on source frame info,
72+
// such as URI. For example, if the source is a 3rd party frame,
73+
// open using the default browser.
74+
wil::unique_cotaskmem_string source;
75+
CHECK_FAILURE(frame_info->get_Source(&source));
76+
useDefaultBrowser = IsThirdPartySource(source.get());
77+
}
78+
}
79+
if (useDefaultBrowser)
80+
{
81+
// Open the URI in the default browser.
82+
ShellExecute(
83+
nullptr, L"open", source.get(), nullptr, nullptr, SW_SHOWNORMAL);
84+
CHECK_FAILURE(args->put_Handled(TRUE));
85+
}
86+
else
87+
{
88+
Microsoft::WRL::ComPtr<ICoreWebView2Deferral> deferral;
89+
CHECK_FAILURE(args->GetDeferral(&deferral));
90+
AppWindow* newAppWindow = new AppWindow(
91+
m_creationModeId, GetWebViewOption(), L"none", m_userDataFolder,
92+
false, nullptr, true, windowRect, !!shouldHaveToolbar);
93+
newAppWindow->m_onWebViewFirstInitialized =
94+
[args, deferral, newAppWindow]()
95+
{
96+
CHECK_FAILURE(args->put_NewWindow(newAppWindow->m_webView.get()));
97+
CHECK_FAILURE(args->put_Handled(TRUE));
98+
CHECK_FAILURE(deferral->Complete());
99+
};
100+
}
101+
return S_OK;
102+
})
103+
.Get(),
104+
nullptr));
105+
```
106+
107+
# API Details
108+
109+
```
110+
/// This is a continuation of the `ICoreWebView2NewWindowRequestedEventArgs` interface.
111+
[uuid(92f08d94-70bd-4d2b-8332-18bd7d3b2b7c), object, pointer_default(unique)]
112+
interface ICoreWebView2NewWindowRequestedEventArgs3 :
113+
ICoreWebView2NewWindowRequestedEventArgs2 {
114+
/// The frame info of the frame where the new window request originated. The
115+
/// `OriginalSourceFrameInfo` is a snapshot of frame information at the time when the
116+
/// new window was requested. See `ICoreWebView2FrameInfo` for details on frame
117+
/// properties.
118+
[propget] HRESULT OriginalSourceFrameInfo([out, retval] ICoreWebView2FrameInfo** frameInfo);
119+
}
120+
```
121+
122+
```c#
123+
namespace Microsoft.Web.WebView2.Core
124+
{
125+
runtimeclass CoreWebView2NewWindowRequestedEventArgs
126+
{
127+
// ...
128+
129+
[interface_name("Microsoft.Web.WebView2.Core.ICoreWebView2NewWindowRequestedEventArgs3")]
130+
{
131+
// The frame info of the frame where the new window request originated. The
132+
// `OriginalSourceFrameInfo` is a snapshot of frame information at the time when the
133+
// new window was requested. See `CoreWebView2FrameInfo` for details on frame
134+
// properties.
135+
CoreWebView2FrameInfo OriginalSourceFrameInfo { get; };
136+
}
137+
}
138+
}
139+
```

0 commit comments

Comments
 (0)