Skip to content

Commit 36f9e5d

Browse files
authored
Create DisableNavigatingBackAndForward.md
1 parent 3fd38e3 commit 36f9e5d

File tree

1 file changed

+115
-0
lines changed

1 file changed

+115
-0
lines changed
Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
# API spec for disable navigating back/forward
2+
3+
# Background
4+
This problem was first proposed by a developer on GitHub, who wants to prevent mouse Xbutton1 and XButton2. The reason for their superior level is to prevent users navigating back or forward.
5+
Afterwards, Teams made similar demands. They wanted a mechanism which could support them in controlling the behaviors of `go back` and `go forward` freely, like disabling them.
6+
7+
@Haichao Zhu has already finished some work on letting application developers handle all input and decide whether to suppress. This should be solvable in a generic way, as @Nic Champagne Williamson said. However, this feature hasn’t been released yet, and it might be better if we could provide a simpler and more direct way.
8+
9+
Therefore, our job is to provide a mechanism for developers to disable navigating back and forward without effort.
10+
11+
12+
# Examples
13+
#### Win32 C++
14+
15+
##### Use `ICoreWebView2NavigationStartingEventArgs3` in `add_NavigationStarting`
16+
17+
```c++
18+
//! [NavigationStarting]
19+
// Register a handler for the NavigationStarting event.
20+
// This handler will check the navigation status, and if the navigation is
21+
// `GoBack` or `GoForward`, it will be canceled.
22+
CHECK_FAILURE(m_webView->add_NavigationStarting(
23+
Callback<ICoreWebView2NavigationStartingEventHandler>(
24+
[this](ICoreWebView2* sender, ICoreWebView2NavigationStartingEventArgs* args)
25+
-> HRESULT {
26+
wil::com_ptr<ICoreWebView2NavigationStartingEventArgs3> args3;
27+
if (SUCCEEDED(args->QueryInterface(IID_PPV_ARGS(&args3))))
28+
{
29+
int entry_offset;
30+
CHECK_FAILURE(stage_args->get_NavigationEntryOffset(&entry_offset));
31+
if (entry_offset == -1 || entry_offset == 1) {
32+
CHECK_FAILURE(args->put_Cancel(true));
33+
}
34+
}
35+
return S_OK;
36+
})
37+
.Get(),
38+
&m_navigationStartingToken));
39+
//! [NavigationStarting]
40+
```
41+
42+
#### .NET and WinRT
43+
44+
#### Use `CoreWebView2NavigationStartingEventArgs` in `NavigationStarting`
45+
46+
```c#
47+
// Register a handler for the NavigationStarting event.
48+
// This handler will check the navigation status, and if the navigation is
49+
// `GoBack` or `GoForward`, it will be canceled.
50+
void WebView_NavigationStarting(object sender, CoreWebView2NavigationStartingEventArgs e)
51+
{
52+
if (e.NavigationEntryOffset == -1 || e.NavigationEntryOffset == 1) {
53+
e.Cancel = true;
54+
}
55+
}
56+
```
57+
58+
# API Details
59+
#### Win32 C++
60+
61+
```c++
62+
/// Extend `NavigationStartingEventArgs` by adding more information.
63+
[uuid(39A27807-2365-470B-AF28-885502121049), object, pointer_default(unique)]
64+
interface ICoreWebView2NavigationStartingEventArgs3 : ICoreWebView2NavigationStartingEventArgs2 {
65+
66+
/// Get the entry offset of this navigation, which contains information about whether for back or forward.
67+
///
68+
/// MSOWNERS: [email protected]
69+
[propget] HRESULT NavigationEntryOffset([out, retval] int* entry_offset);
70+
}
71+
```
72+
73+
#### .NET and WinRT
74+
75+
```c#
76+
namespace Microsoft.Web.WebView2.Core
77+
{
78+
public partial class CoreWebView2NavigationStartingEventArgs
79+
{
80+
///
81+
public int NavigationEntryOffset
82+
{
83+
get
84+
{
85+
try
86+
{
87+
return
88+
_nativeICoreWebView2NavigationStartingEventArgs3.NavigationEntryOffset;
89+
90+
}
91+
catch (InvalidCastException ex)
92+
{
93+
if (ex.HResult == -2147467262) // UI_E_WRONG_THREAD
94+
throw new InvalidOperationException($"{nameof(CoreWebView2)} members can only be accessed from the UI thread.", ex);
95+
96+
throw ex;
97+
}
98+
catch (System.Runtime.InteropServices.COMException ex)
99+
{
100+
if (ex.HResult == -2147019873) // 0x8007139F
101+
throw new InvalidOperationException($"{nameof(CoreWebView2)} members cannot be accessed after the WebView2 control is disposed.", ex);
102+
103+
throw ex;
104+
}
105+
}
106+
}
107+
}
108+
}
109+
```
110+
111+
112+
# Appendix
113+
Relative scenario could be found here: https://dev.azure.com/microsoft/Edge/_workitems/edit/42081893.
114+
115+
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)