|
| 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 users navigating back or forward using any of the built-in shortcut keys or special mouse buttons. |
| 5 | + |
| 6 | +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. |
| 7 | + |
| 8 | +@Haichao Zhu has already finished some work on letting application developers handle all input and decide whether to suppress. |
| 9 | + |
| 10 | +This should be solvable in a generic way. However, this feature hasn’t been released yet, and it might be better if we could provide a simpler and more direct way. |
| 11 | + |
| 12 | +Therefore, our job is to provide a mechanism for developers to disable navigating back and forward without excessive effort. |
| 13 | + |
| 14 | + |
| 15 | +# Examples |
| 16 | +#### Win32 C++ |
| 17 | + |
| 18 | +##### Use `ICoreWebView2NavigationStartingEventArgs3` in `add_NavigationStarting` |
| 19 | + |
| 20 | +```c++ |
| 21 | +//! [NavigationStarting] |
| 22 | +// Register a handler for the NavigationStarting event. |
| 23 | +// This handler will check the navigation status, and if the navigation is |
| 24 | +// `GoBack` or `GoForward`, it will be canceled. |
| 25 | +CHECK_FAILURE(m_webView->add_NavigationStarting( |
| 26 | + Callback<ICoreWebView2NavigationStartingEventHandler>( |
| 27 | + [this](ICoreWebView2* sender, ICoreWebView2NavigationStartingEventArgs* args) |
| 28 | + -> HRESULT { |
| 29 | + wil::com_ptr<ICoreWebView2NavigationStartingEventArgs3> args3; |
| 30 | + if (SUCCEEDED(args->QueryInterface(IID_PPV_ARGS(&args3)))) |
| 31 | + { |
| 32 | + COREWEBVIEW2_NAVIGATION_HISTORY_CHANGE_KIND history_change = COREWEBVIEW2_NAVIGATION_HISTORY_CHANGE_KIND_OTHER; |
| 33 | + CHECK_FAILURE(args3->get_NavigationHistoryChange(&history_change)); |
| 34 | + if (history_change != COREWEBVIEW2_NAVIGATION_HISTORY_CHANGE_KIND_OTHER) |
| 35 | + { |
| 36 | + CHECK_FAILURE(args->put_Cancel(true)); |
| 37 | + } |
| 38 | + } |
| 39 | + return S_OK; |
| 40 | + }) |
| 41 | + .Get(), |
| 42 | + &m_navigationStartingToken)); |
| 43 | +//! [NavigationStarting] |
| 44 | +``` |
| 45 | +
|
| 46 | +#### .NET and WinRT |
| 47 | +
|
| 48 | +#### Use `CoreWebView2NavigationStartingEventArgs` in `NavigationStarting` |
| 49 | +
|
| 50 | +```c# |
| 51 | +// Register a handler for the NavigationStarting event. |
| 52 | +// This handler will check the navigation status, and if the navigation is |
| 53 | +// `GoBack` or `GoForward`, it will be canceled. |
| 54 | +void WebView_NavigationStarting(object sender, CoreWebView2NavigationStartingEventArgs e) |
| 55 | +{ |
| 56 | + if (e.NavigationHistoryChange != CoreWebView2NavigationHistoryChangeKind.Other) |
| 57 | + { |
| 58 | + e.Cancel = true; |
| 59 | + } |
| 60 | +} |
| 61 | +``` |
| 62 | + |
| 63 | +# API Details |
| 64 | +#### Win32 C++ |
| 65 | + |
| 66 | +```c++ |
| 67 | +// Enums and structs |
| 68 | +[v1_enum] |
| 69 | +typedef enum COREWEBVIEW2_NAVIGATION_HISTORY_CHANGE_KIND { |
| 70 | + /// Indicates a navigation that is going back to a previous entry in the navigation history. For example, a navigation caused by `CoreWebView2.GoBack` or in script `window.history.go(-1)`. |
| 71 | + COREWEBVIEW2_NAVIGATION_HISTORY_CHANGE_KIND_BACK, |
| 72 | + /// Indicates a navigation that is going forward to a later entry in the navigation history. For example, a navigation caused by `CoreWebView2.GoForward` or in script `window.history.go(1)`. |
| 73 | + COREWEBVIEW2_NAVIGATION_HISTORY_CHANGE_KIND_FORWARD, |
| 74 | + /// Indicates a navigation that is not going back or forward to an existing entry in the navigation history. For example, a navigation caused by `CoreWebView2.Navigate`, or `CoreWebView2.Reload` or in script `window.location.href = 'https://example.com/'`. |
| 75 | + COREWEBVIEW2_NAVIGATION_HISTORY_CHANGE_KIND_OTHER, |
| 76 | +} COREWEBVIEW2_NAVIGATION_HISTORY_CHANGE_KIND; |
| 77 | + |
| 78 | +/// Extend `NavigationStartingEventArgs` by adding more information. |
| 79 | +[uuid(39A27807-2365-470B-AF28-885502121049), object, pointer_default(unique)] |
| 80 | +interface ICoreWebView2NavigationStartingEventArgs3 : ICoreWebView2NavigationStartingEventArgs2 { |
| 81 | + |
| 82 | + /// Indicates if this navigation is going back or forward to an existing entry in the navigation history. |
| 83 | + [propget] HRESULT NavigationHistoryChange([out, retval] COREWEBVIEW2_NAVIGATION_HISTORY_CHANGE_KIND* history_change); |
| 84 | +} |
| 85 | +} |
| 86 | +``` |
| 87 | + |
| 88 | +#### .NET and WinRT |
| 89 | + |
| 90 | +```c# (but really MIDL3) |
| 91 | +namespace Microsoft.Web.WebView2.Core |
| 92 | +{ |
| 93 | + enum CoreWebView2NavigationHistoryChangeKind |
| 94 | + { |
| 95 | + Back = 0, |
| 96 | + Forward = 1, |
| 97 | + Other = 2, |
| 98 | + }; |
| 99 | + // .. |
| 100 | + runtimeclass CoreWebView2NavigationStartingEventArgs |
| 101 | + { |
| 102 | + // ICoreWebView2NavigationStartingEventArgs members |
| 103 | + // .. |
| 104 | + [interface_name("Microsoft.Web.WebView2.Core.ICoreWebView2NavigationStartingEventArgs3")] |
| 105 | + { |
| 106 | + // ICoreWebView2NavigationStartingEventArgs3 members |
| 107 | + CoreWebView2NavigationHistoryChangeKind NavigationHistoryChange { get; }; |
| 108 | + } |
| 109 | + } |
| 110 | +} |
| 111 | +``` |
| 112 | + |
| 113 | + |
| 114 | +# Appendix |
| 115 | +Relative scenario could be found here: https://dev.azure.com/microsoft/Edge/_workitems/edit/42081893. |
| 116 | + |
| 117 | +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