Skip to content

Commit a1a7553

Browse files
authored
Merge pull request #2959 from MicrosoftEdge/DisableNavigatingBackAndForward
API review: Disable navigating back and forward
2 parents 5242651 + ff47bce commit a1a7553

File tree

1 file changed

+130
-0
lines changed

1 file changed

+130
-0
lines changed
Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
# API spec for disable navigating back/forward
2+
3+
# Background
4+
This problem was first identified by a developer on GitHub, who wants to prevent users navigating
5+
back or forward using any of the built-in shortcut keys or special mouse buttons.
6+
7+
Afterwards, Teams made similar demands. They wanted a mechanism which could support them in
8+
controlling the behaviors of `go back` and `go forward` freely, like disabling them.
9+
10+
@Haichao Zhu has already finished some work on letting application developers handle all input and
11+
decide whether to suppress.
12+
13+
This should be solvable in a generic way. However, this feature hasn’t been released yet, and it might
14+
be better if we could provide a simpler and more direct way.
15+
16+
Therefore, our job is to provide a mechanism for developers to disable navigating back and forward
17+
without excessive effort.
18+
19+
20+
# Examples
21+
#### Win32 C++
22+
23+
##### Use `ICoreWebView2NavigationStartingEventArgs3` in `add_NavigationStarting`
24+
25+
```c++
26+
//! [NavigationStarting]
27+
// Register a handler for the NavigationStarting event.
28+
// This handler will check the navigation status, and if the navigation is
29+
// `GoBack` or `GoForward`, it will be canceled.
30+
CHECK_FAILURE(m_webView->add_NavigationStarting(
31+
Callback<ICoreWebView2NavigationStartingEventHandler>(
32+
[this](ICoreWebView2* sender, ICoreWebView2NavigationStartingEventArgs* args)
33+
-> HRESULT {
34+
wil::com_ptr<ICoreWebView2NavigationStartingEventArgs3> args3;
35+
if (SUCCEEDED(args->QueryInterface(IID_PPV_ARGS(&args3))))
36+
{
37+
COREWEBVIEW2_NAVIGATION_KIND kind;
38+
CHECK_FAILURE(args3->get_NavigationKind(&kind));
39+
// disable navigation if it is back/forward
40+
if (kind == COREWEBVIEW2_NAVIGATION_KIND_BACK_OR_FORWARD)
41+
{
42+
CHECK_FAILURE(args->put_Cancel(true));
43+
}
44+
}
45+
return S_OK;
46+
})
47+
.Get(),
48+
&m_navigationStartingToken));
49+
//! [NavigationStarting]
50+
```
51+
52+
#### .NET and WinRT
53+
54+
#### Use `CoreWebView2NavigationStartingEventArgs` in `NavigationStarting`
55+
56+
```c#
57+
// Register a handler for the NavigationStarting event.
58+
// This handler will check the navigation status, and if the navigation is
59+
// `GoBack` or `GoForward`, it will be canceled.
60+
void WebView_NavigationStarting(object sender, CoreWebView2NavigationStartingEventArgs e)
61+
{
62+
if (e.NavigationKind == CoreWebView2NavigationKind.BackOrForward)
63+
{
64+
e.Cancel = true;
65+
}
66+
}
67+
```
68+
69+
# API Details
70+
#### Win32 C++
71+
72+
```c++
73+
// Enums and structs
74+
[v1_enum]
75+
typedef enum COREWEBVIEW2_NAVIGATION_KIND {
76+
/// A navigation caused by CoreWebView2.Reload(), location.reload(), the end user
77+
/// using F5 or other UX, or other reload mechanisms to reload the current document
78+
/// without modifying the navigation history.
79+
COREWEBVIEW2_NAVIGATION_KIND_RELOAD,
80+
/// A navigation back or forward to a different entry in the session navigation history.
81+
/// For example via CoreWebView2.Back(), location.back(), the end user pressing Alt+Left
82+
/// or other UX, or other mechanisms to navigate forward or backward in the current
83+
/// session navigation history.
84+
COREWEBVIEW2_NAVIGATION_KIND_BACK_OR_FORWARD,
85+
/// A navigation to a different document. This can be caused by CoreWebView2.Navigate(),
86+
/// window.location.href = '...', or other WebView2 or DOM APIs that navigate to a specific URI.
87+
COREWEBVIEW2_NAVIGATION_KIND_DIFFERENT_DOCUMENT,
88+
} COREWEBVIEW2_NAVIGATION_KIND;
89+
90+
/// Extend `NavigationStartingEventArgs` by adding more information.
91+
[uuid(39A27807-2365-470B-AF28-885502121049), object, pointer_default(unique)]
92+
interface ICoreWebView2NavigationStartingEventArgs3 : ICoreWebView2NavigationStartingEventArgs2 {
93+
94+
/// Indicates if this navigation is reload, back/forward or navigating to a different document
95+
[propget] HRESULT NavigationKind(
96+
[out, retval] COREWEBVIEW2_NAVIGATION_KIND* kind);
97+
}
98+
}
99+
```
100+
101+
#### .NET and WinRT
102+
103+
```c# (but really MIDL3)
104+
namespace Microsoft.Web.WebView2.Core
105+
{
106+
enum CoreWebView2NavigationKind
107+
{
108+
Reload = 0,
109+
BackOrForward = 1,
110+
DifferentDocument = 2,
111+
};
112+
// ..
113+
runtimeclass CoreWebView2NavigationStartingEventArgs
114+
{
115+
// ICoreWebView2NavigationStartingEventArgs members
116+
// ..
117+
[interface_name("Microsoft.Web.WebView2.Core.ICoreWebView2NavigationStartingEventArgs3")]
118+
{
119+
// ICoreWebView2NavigationStartingEventArgs3 members
120+
CoreWebView2NavigationKind NavigationKind { get; };
121+
}
122+
}
123+
}
124+
```
125+
126+
127+
# Appendix
128+
Relative scenario could be found here: https://dev.azure.com/microsoft/Edge/_workitems/edit/42081893.
129+
130+
Design doc and reviews could be found here: https://microsoftapc-my.sharepoint.com/:w:/g/personal/pengyuanwang_microsoft_com/Ecu4x6kcjqxNrmvqQW7jr0QBCbHzd1PJ7M3h895rt_l_lg?e=ydF6ez.

0 commit comments

Comments
 (0)