Launch external application in ISchemeHandler.ProcessRequestAsync

407 views
Skip to first unread message

dmitry...@gmail.com

unread,
Mar 17, 2015, 6:25:03 AM3/17/15
to cefs...@googlegroups.com
Hi CefSharp group!

I have a task to handle custom url protocol in CefSharp. On request there should be external application launch.
I've implemented my SchemeHandler and SchemeHandlerFactory and registered that scheme in settings:

    internal class MyCustomSchemeHandler : ISchemeHandler
   
{
       
public bool ProcessRequestAsync(IRequest request, ISchemeHandlerResponse response, OnRequestCompletedHandler requestCompletedCallback)
       
{
           
// External application launch should be here
           
return true;
       
}
   
}

   
internal class MyCustomSchemeHandlerFactory : ISchemeHandlerFactory
   
{
       
public const string SchemeName = "myapp";

       
public ISchemeHandler Create()
       
{
           
return new MyCustomSchemeHandler();
       
}
   
}

   
var settings = new CefSettings();
    settings
.RegisterScheme(new CefCustomScheme
   
{
       
SchemeName = MyCustomSchemeHandlerFactory.SchemeName,
       
SchemeHandlerFactory = new MyCustomSchemeHandlerFactory()
   
});
   
Cef.Initialize(settings);


My questions are:
1. Am I correct that I don't need to invoke requestCompletedCallback in ProcessRequestAsync? (as I do not return any response, I just launch some external app)
2. What should I return from ProcessRequestAsync method - true or false? And what will be the difference in returning true or false from ProcessRequestAsync method?

Alex Maitland

unread,
Mar 17, 2015, 8:50:24 AM3/17/15
to cefs...@googlegroups.com
Source code is the best documentation, the xml comments in this case see https://github.com/cefsharp/CefSharp/blob/master/CefSharp/ISchemeHandler.cs#L21

In this case I'd return false to cancel the request, if you return true then it's expected that at some point you'll call the requestCompletedCallback.

I'd actually recommend a different approach. I'd implement `IRequestHandler`. Return false for all other methods to invoke their default behavior and in `OnBeforeResourceLoad do something like

bool IRequestHandler.OnBeforeResourceLoad(IWebBrowser browser, IRequest request, IResponse response)
{
   
if(request.Url.StartsWith("myapp"))
   
{
       
//Launch App Here
       
return true;
   
}

   
// Return false to execute the default behaviour
   
return false;
}

Alex Maitland

unread,
Mar 17, 2015, 8:52:32 AM3/17/15
to cefs...@googlegroups.com
Also a similar question was asked the other day, here is the relevant commit to use as an example https://github.com/jacodv/CefSharp.MinimalExample/commit/0976a4069ec041c6d001e918f0fa152120e200e5

dmitry...@gmail.com

unread,
Mar 17, 2015, 10:10:52 AM3/17/15
to cefs...@googlegroups.com
Alex,
Thanks for the answer.
Could you please also explain (or direct to the information source) what the difference between approaches using ISchemeHandler and IRequestHandler is?
Are there any advantages of IRequestHandler approach over ISchemeHandler approach?
Do I understand correctly that IRequestHandler approach is synchronous and ISchemeHandler approach is asynchrounous?

Thanks in advance

Alex Maitland

unread,
Mar 17, 2015, 6:46:54 PM3/17/15
to cefs...@googlegroups.com
`OnBeforeResourceLoad` is called before every request, regardless of scheme, so you can intercept anything.

What makes `SchemeHandler` async is the callback, you would typically spawn a Task, then when your done call requestCompletedCallback. As your not using the callback, speed wise it would be the same.

If you need to perform  little more intensive checking in `OnBeforeResourceLoad`, then I would check the url, spawn off a task and then return true.

`IRequestHandler` in my opinion is the simpler approach, only requires one class.

Alex Maitland

unread,
Mar 17, 2015, 7:20:34 PM3/17/15
to cefs...@googlegroups.com
I guess my preference for `IRequestHandler` comes from the ability to return false and fallback to the default behavior.

`SchemeHandler` you can cancel or handle, there's no default behavior.

Go with whatever fits your scenario.

dmitry...@gmail.com

unread,
Mar 18, 2015, 4:02:50 AM3/18/15
to cefs...@googlegroups.com
Alex, thanks a lot!
Reply all
Reply to author
Forward
0 new messages