public void BeforeNavigate2(object pDisp, ref object url, ref object flags, ref object targetFrameName, ref object postData, ref object headers, ref bool cancel) { bool newNavigationInitiated = false; bool cancelRequested = false; try { Debug.Assert(url == null || url is string, "invalid url type"); Debug.Assert(targetFrameName == null || targetFrameName is string, "invalid targetFrameName type"); Debug.Assert(headers == null || headers is string, "invalid headers type"); // // Due to a if (targetFrameName == null) { targetFrameName = ""; } if (headers == null) { headers = ""; } string urlString = (string)url; Uri source = String.IsNullOrEmpty(urlString) ? null : new Uri(urlString); UnsafeNativeMethods.IWebBrowser2 axIWebBrowser2 = (UnsafeNativeMethods.IWebBrowser2)pDisp; // If _parent.AxIWebBrowser2 != axIWebBrowser2, navigation happens in a nested [i]frame. // in that case we do not want to enforce site locking as we want the default IE behavior to take // over. if (_parent.AxIWebBrowser2 == axIWebBrowser2) { // The NavigatingToAboutBlank property indicates whether we are navigating to "about:blank" // as a result of navigating to null or stream/string navigation. // We set the NavigatingToAboutBlank bit to true in the WebBrowser DoNavigate method. When the above // conditions occur, the NavigatingToAboutBlank is true and the source must be "about:blank". // // But when end user navigates away from the current about:blank page (by clicking // on a hyperlink, Goback/Forward), or programmatically call GoBack and Forward, // When we get the navigating event, NavigatingToAboutBlank is true, but the source is not "about:blank". // Clear the NavigatingToAboutBlank bit in that case. if ((_parent.NavigatingToAboutBlank) && String.Compare(urlString, WebBrowser.AboutBlankUriString, StringComparison.OrdinalIgnoreCase) != 0) { _parent.NavigatingToAboutBlank = false; } // Site locking for top level WebOC navigation // "about:blank is not enabled in partial trust publicly. // We enable it internally to navigate to null. if ((!_parent.NavigatingToAboutBlank) && !SecurityHelper.CallerHasWebPermission(source) && !IsAllowedScriptScheme(source)) { cancelRequested = true; } else { // When source set to null or navigating to stream/string, we navigate to "about:blank" // internally. Make sure we pass null in the event args. if (_parent.NavigatingToAboutBlank) { source = null; } NavigatingCancelEventArgs e = new NavigatingCancelEventArgs(source, null, null, null, NavigationMode.New, null, null, true); // TFS Dev10 520954 - Launching a navigation from the Navigating event handler causes reentrancy. // For more info, see WebBrowser.LastNavigation. This is a point of possible reentrancy. Whenever // a new navigation is started during the call to the Navigating event handler, we need to cancel // out the current navigation. Guid lastNavigation = _parent.LastNavigation; // Fire navigating event. Events are only fired for top level navigation. _parent.OnNavigating(e); // TFS Dev10 520954 - Launching a navigation from the Navigating event handler causes reentrancy. // For more info, see WebBrowser.LastNavigation. If _lastNavigation has changed during the call to // the event handlers for Navigating, we know a new navigation has been initialized. if (_parent.LastNavigation != lastNavigation) { newNavigationInitiated = true; } cancelRequested = e.Cancel; } } } // We disable this to suppress FXCop warning since in this case we really want to catch all exceptions // please refer to comment below #pragma warning disable 6502 catch { // This is an interesting pattern of putting a try catch block around this that catches everything, // The reason I do this is based on a conversation with Microsoft. What happens here is if there is // an exception in any of the code above then navigation still continues since COM interop eats up // the exception. But what we want is for this navigation to fail. // There fore I catch all exceptions and cancel navigation cancelRequested = true; } #pragma warning restore 6502 finally { // Clean the WebBrowser control state if navigation cancelled. // // TFS Dev10 520954 - Launching a navigation from the Navigating event handler causes reentrancy. // For more info, see WebBrowser.LastNavigation. A new navigation started during event handling for // the Navigating event will have touched the control's global state. In case the navigation was // cancelled in order to start a new navigation, we shouldn't tamper with that new state. if (cancelRequested && !newNavigationInitiated) { _parent.CleanInternalState(); } // TFS Dev10 520954 - Launching a navigation from the Navigating event handler causes reentrancy. // For more info, see WebBrowser.LastNavigation. Whenever a new navigation is started during the // call to the Navigating event handler, we need to cancel out the current navigation. if (cancelRequested || newNavigationInitiated) { cancel = true; } } }