Managing custom URL protocols

2,725 views
Skip to first unread message

Fabien Molinet

unread,
Sep 24, 2012, 6:46:44 PM9/24/12
to cef...@googlegroups.com
Hi,

In our application we use custom URL protocols a lot: http://msdn.microsoft.com/en-us/library/aa767914(v=vs.85).aspx 
Custom URL protocols do work with IE, FF, Chrome but in CefGlue they don't work. I googled and found that I might have to use "CefRuntime.RegisterSchemeHandlerFactory" therefore it is quite complicated to understand what should be done.

Would you have any code sample that we could use?
And I would like to know if there's an easy way for such custom protocols, eg: sccx://, to be handled by the system directly? (like in the browsers)

Thanks in advance.

Cheers,
Fabien

Dmitry Azaraev

unread,
Sep 25, 2012, 4:18:49 AM9/25/12
to cef...@googlegroups.com
Hi.

Samples currently not available.

1. You just need register via CefRuntime.RegisterSchemeHandlerFactory scheme handler factory. It usually just create resource handler.

    class MySchemeHandlerFactory : CefSchemeHandlerFactory
    {
        protected override CefResourceHandler Create(CefBrowser browser, CefFrame frame, string schemeName, CefRequest request)
        {
            return new MySchemeHandler();
        }
    }


2. If you use custom scheme (non standard, i.e. sccx - it is custom), - then you need via CefApp.OnRegisterCustomSchemes register scheme in renderer.

        protected override void OnRegisterCustomSchemes(CefSchemeRegistrar registrar)
        {
            registrar.AddCustomScheme("sccx", true, false, false);
        }

   Note, you can register scheme handler on standard scheme + domain. I.e. http://myapp can be routed to handler, in this case you doesn't need register built-in scheme in renderer. Also, note, that body of POST requests by historical and security reasons doesn't passed in custom schemes. So if you need POST in data in you handler - you need register scheme handler on http scheme. It more easily.

3. Simple scheme handler sample:

    class MySchemeHandler : CefResourceHandler
    {
        private static int _no;
        private bool _completed;

        protected override bool ProcessRequest(CefRequest request, CefCallback callback)
        {
            callback.Continue();
            return true;
        }

        protected override void GetResponseHeaders(CefResponse response, out long responseLength, out string redirectUrl)
        {
            response.SetHeaderMap();

            response.Status = 200;
            response.MimeType = "text/html";
            response.StatusText = "OK";
            responseLength = -1; // unknown content-length
            redirectUrl = null;  // no-redirect
        }

        protected override bool ReadResponse(System.IO.Stream response, int bytesToRead, out int bytesRead, CefCallback callback)
        {
            if (_completed)
            {
                bytesRead = 0;
                return false;
            }
            else
            {
                _no++;
                // very simple response with one block
                var content = Encoding.UTF8.GetBytes("Response #" + _no.ToString());
                if (bytesToRead < content.Length) throw new NotImplementedException(); // oops
                response.Write(content, 0, content.Length);
                bytesRead = content.Length;

                _completed = true;

                return true;
            }
        }

        protected override bool CanGetCookie(CefCookie cookie)
        {
            return false;
        }

        protected override bool CanSetCookie(CefCookie cookie)
        {
            return false;
        }

        protected override void Cancel()
        {
        }
    }

--
Best regards,
   Dmitry                             mailto:fdd...@gmail.com

Fabien Molinet

unread,
Sep 25, 2012, 9:21:49 AM9/25/12
to cef...@googlegroups.com, dmitry....@gmail.com
Thanks a lot  Dmitry.
I will try that and let you know.

Cheers,
Fabien

Josh

unread,
Oct 17, 2012, 10:49:09 PM10/17/12
to cef...@googlegroups.com, dmitry....@gmail.com
FYI: I just implemented this (works great) but make sure to include a content size.. returning a responselength of -1 causes CEF to eat up processor and become super sluggish.. Even if you close the connection (response.close()) it stlll sits there slowing things down.. So do all your "work" before getResponseHeaders gets called and return an actual content length and things work great..
 
Josh

Dmitry Azaraev

unread,
Oct 18, 2012, 3:22:28 AM10/18/12
to cef...@googlegroups.com
> responselength of -1 causes CEF to eat up processor and become super sluggish
never seem this effect. may be your handler works wrong?

Fabien Molinet

unread,
Jan 14, 2013, 8:22:51 AM1/14/13
to cef...@googlegroups.com, dmitry....@gmail.com
Thanks Dmitry for your detailed answer.
It's working like a charm ! (I only had time to test it now).

Cheers,
Fabien

liz...@infovision.net

unread,
Mar 19, 2013, 11:25:36 AM3/19/13
to cef...@googlegroups.com, dmitry....@gmail.com
I don't understand your code in MySchemeHandler class. Where in the code should the custom scheme  URL be converted to real URL and how the resource is loaded? In your ReadResponse() function dummy response "Response #n" is passed to the output variable. What is the purpose of this? 

Thanks,
Liz


On Tuesday, September 25, 2012 3:19:11 AM UTC-5, Dmitry Azaraev wrote:

Dmitry Azaraev

unread,
Mar 19, 2013, 12:04:56 PM3/19/13
to cef...@googlegroups.com
Read documentation for CefResourceHandler (can be located in CefResourceHandler.cs) or as xml-doc. Or read http://magpcss.org/ceforum/apidocs3/projects/(default)/CefResourceHandler.html .


--
You received this message because you are subscribed to the Google Groups "CefGlue" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cefglue+u...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 



--
Best regards,
   Dmitry
Reply all
Reply to author
Forward
0 new messages