private bool OpenUri(Uri uri)
        {
            if (uri == null)
            {
                throw new ArgumentNullException("uri");
            }

            if (!uri.IsAbsoluteUri)
            {
                return(false);
            }

            /* First try to use the Web Browsing Service. This is not known to work because the
             * CreateExternalWebBrowser method always returns E_NOTIMPL. However, it is presumably
             * safer than a Shell Execute for arbitrary URIs.
             */
            IVsWebBrowsingService service = _serviceProvider.GetService(typeof(SVsWebBrowsingService)) as IVsWebBrowsingService;

            if (service != null)
            {
                __VSCREATEWEBBROWSER createFlags = __VSCREATEWEBBROWSER.VSCWB_AutoShow;
                VSPREVIEWRESOLUTION  resolution  = VSPREVIEWRESOLUTION.PR_Default;
                int result = ErrorHandler.CallWithCOMConvention(() => service.CreateExternalWebBrowser((uint)createFlags, resolution, uri.AbsoluteUri));
                if (ErrorHandler.Succeeded(result))
                {
                    return(true);
                }
            }

            // Fall back to Shell Execute, but only for http or https URIs
            if (uri.Scheme != "http" && uri.Scheme != "https")
            {
                return(false);
            }

            try
            {
                Process.Start(uri.AbsoluteUri);
                return(true);
            }
            catch (Win32Exception)
            {
            }
            catch (FileNotFoundException)
            {
            }

            return(false);
        }
        private bool OpenUri(Uri uri)
        {
            if (uri == null)
            {
                throw new ArgumentNullException(nameof(uri));
            }

            if (string.Equals(uri.Scheme, PrefixOpenInVisualStudio, StringComparison.InvariantCultureIgnoreCase))
            {
                if (File.Exists(uri.LocalPath))
                {
                    DTEHelper.Singleton?.OpenFileInVisualStudio(null, uri.LocalPath);
                }

                return(true);
            }

            if (string.Equals(uri.Scheme, PrefixOpenInVisualStudioRelativePath, StringComparison.InvariantCultureIgnoreCase))
            {
                DTEHelper.Singleton?.OpenFileInVisualStudioRelativePath(null, uri.LocalPath);

                return(true);
            }

            if (string.Equals(uri.Scheme, PrefixOpenInTextEditor, StringComparison.InvariantCultureIgnoreCase))
            {
                if (File.Exists(uri.LocalPath))
                {
                    DTEHelper.Singleton?.OpenFileInTextEditor(null, uri.LocalPath);
                }

                return(true);
            }

            if (string.Equals(uri.Scheme, PrefixOpenInExcel, StringComparison.InvariantCultureIgnoreCase))
            {
                if (File.Exists(uri.LocalPath))
                {
                    DTEHelper.Singleton?.OpenFileInExcel(null, uri.LocalPath);
                }

                return(true);
            }

            if (string.Equals(uri.Scheme, PrefixShowDifference, StringComparison.InvariantCultureIgnoreCase))
            {
                DTEHelper.Singleton?.ShowDifference(uri);

                return(true);
            }

            if (string.Equals(uri.Scheme, PrefixOpenSolution, StringComparison.InvariantCultureIgnoreCase))
            {
                DTEHelper.Singleton?.OpenSolution(uri);

                return(true);
            }

            if (string.Equals(uri.Scheme, PrefixOpenSolutionList, StringComparison.InvariantCultureIgnoreCase))
            {
                DTEHelper.Singleton?.OpenSolutionList(uri);

                return(true);
            }

            if (string.Equals(uri.Scheme, PrefixSelectFileInFolder, StringComparison.InvariantCultureIgnoreCase))
            {
                if (File.Exists(uri.LocalPath))
                {
                    DTEHelper.Singleton?.SelectFileInFolder(null, uri.LocalPath);
                }

                return(true);
            }

            IVsWebBrowsingService service = _provider.ServiceProvider.GetService(typeof(SVsWebBrowsingService)) as IVsWebBrowsingService;

            if (service != null)
            {
                var createFlags = __VSCREATEWEBBROWSER.VSCWB_AutoShow;
                var resolution  = VSPREVIEWRESOLUTION.PR_Default;

                int result = ErrorHandler.CallWithCOMConvention(() => service.CreateExternalWebBrowser((uint)createFlags, resolution, uri.AbsoluteUri));

                if (ErrorHandler.Succeeded(result))
                {
                    return(true);
                }
            }

            if (uri.Scheme != Uri.UriSchemeHttp && uri.Scheme != Uri.UriSchemeHttps)
            {
                return(false);
            }

            try
            {
                Process.Start(uri.AbsoluteUri);
                return(true);
            }
            catch (Exception)
            {
            }

            return(false);
        }
Пример #3
0
 private static bool TryStartBrowser(IVsWebBrowsingService service, Uri uri)
 {
     return ErrorHandler.Succeeded(service.CreateExternalWebBrowser((uint)__VSCREATEWEBBROWSER.VSCWB_ForceNew, VSPREVIEWRESOLUTION.PR_Default, uri.AbsoluteUri));
 }
Пример #4
0
        internal static void GotoSource(this LocationInfo location, IServiceProvider serviceProvider, GeneroLanguageVersion languageVersion)
        {
            if (location.Line > 0 && location.Column > 0)
            {
                VSGeneroPackage.NavigateTo(
                    location.FilePath,
                    Guid.Empty,
                    location.Line - 1,
                    location.Column - 1);
            }
            else if (location.DefinitionURL != null)
            {
                var urlStr = location.GetUrlString(languageVersion);

                Uri definitionUrl;
                if (Uri.TryCreate(urlStr, UriKind.Absolute, out definitionUrl))
                {
                    if (serviceProvider != null)
                    {
                        IVsWebBrowsingService service = serviceProvider.GetService(typeof(SVsWebBrowsingService)) as IVsWebBrowsingService;
                        if (service != null)
                        {
                            if (VSGeneroPackage.Instance.AdvancedOptions4GL.OpenExternalBrowser)
                            {
                                __VSCREATEWEBBROWSER createFlags = __VSCREATEWEBBROWSER.VSCWB_AutoShow;
                                VSPREVIEWRESOLUTION  resolution  = VSPREVIEWRESOLUTION.PR_Default;
                                int result = ErrorHandler.CallWithCOMConvention(() => service.CreateExternalWebBrowser((uint)createFlags, resolution, definitionUrl.AbsoluteUri));
                                if (ErrorHandler.Succeeded(result))
                                {
                                    return;
                                }
                            }
                            else
                            {
                                IVsWindowFrame ppFrame;
                                int            result = ErrorHandler.CallWithCOMConvention(() => service.Navigate(definitionUrl.AbsoluteUri, 0, out ppFrame));
                                if (ErrorHandler.Succeeded(result))
                                {
                                    return;
                                }
                            }
                        }
                    }

                    // Fall back to Shell Execute, but only for http or https URIs
                    if (definitionUrl.Scheme != "http" && definitionUrl.Scheme != "https")
                    {
                        return;
                    }

                    try
                    {
                        Process.Start(definitionUrl.AbsoluteUri);
                    }
                    catch (Win32Exception)
                    {
                    }
                    catch (FileNotFoundException)
                    {
                    }
                }
            }
            else
            {
                VSGeneroPackage.NavigateTo(location.FilePath, Guid.Empty, location.Index);
            }
        }