1

I have a simple custom protocol scheme that I want to use with CefSharp to "start" a file in Windows. The scheme works, but I can't find a way to prevent chromium from navigating to that file url (after it's been invoked successfully by Process.Start in the code below). I've tried just about every combination of CefReturnValue and callback.Dispose and request.Dispose

Here's the code:

public override CefReturnValue ProcessRequestAsync(IRequest request, ICallback callback)
{
    var uri = new Uri(request.Url);
    string requestedPath = WebUtility.UrlDecode(uri.AbsolutePath);

    if (requestedPath.StartsWith("///"))
        requestedPath = requestedPath.Substring(3).Replace("/", @"\");

    if (File.Exists(requestedPath))
        Process.Start(requestedPath);

    callback.Dispose();
    return CefReturnValue.Cancel;
}

Does anyone know how to prevent the subsequent navigation?

  • You cannot prevent the navigation from ResourceHandler, you can use http://cefsharp.github.io/api/79.1.x/html/M_CefSharp_IRequestHandler_OnBeforeBrowse.htm and cancel the navigation. – amaitland May 07 '20 at 19:56
  • 1
    Technically `CEF` has a specific handler for this scenario, unfortunately it's broken currently https://bitbucket.org/chromiumembedded/cef/issues/2715/onprotocolexecution-page-goes-blank-after – amaitland May 07 '20 at 23:59

1 Answers1

3

Thanks to amaitland, here's that answer --

Summary: Who needs a custom protocol?

Detail:

public class Program
{
    // stuff ...

    var settings = new CefSettings();
    settings.RegisterScheme(new CefCustomScheme { SchemeName = "eutp"});

    // more stuff ...

    Cef.Initialize(settings);
}

public class EutpRequestHandler : CefSharp.Handler.RequestHandler
{
    protected override bool OnBeforeBrowse(IWebBrowser chromiumWebBrowser,  IBrowser browser,
        IFrame frame, IRequest request, bool userGesture, bool isRedirect)
    {
        if (request.Url.StartsWith("eutp://file///")) {
            string requestedPath = WebUtility.UrlDecode(request.Url.Substring(14));
            requestedPath = requestedPath.Replace("/", @"\");
            if (File.Exists(requestedPath))
                Process.Start(requestedPath);

            return true; // cancel navigation
        }
        return false; // allows navigation
    }
}

Thanks again to amaitland for pointing out that a custom protocol scheme factory or handler is superfluous. Only the scheme's name is required.

  • Do you actually need the `SchemeHandlerFactory`? I would have though just registering the scheme would have been sufficent. – amaitland May 08 '20 at 00:00