cefsharp / CefSharp

.NET (WPF and Windows Forms) bindings for the Chromium Embedded Framework
http://cefsharp.github.io/
Other
9.89k stars 2.92k forks source link

ProcessRequestAsync never invoked in custom scheme handler #769

Closed alexey89777 closed 9 years ago

alexey89777 commented 9 years ago

Hello! I need to capture/modify requests/responses. I have register custom scheme handler. The problem is ProcessRequestAsync never invoked? Possible I do something wrong?

class Program
{
    private static ChromiumWebBrowser browser;
    private static bool captureFirstRenderedImage = false;

    internal class CefSharpSchemeHandlerFactory : ISchemeHandlerFactory
    {
        public const string SchemeName = "custom";

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

    internal class CefSharpSchemeHandler : ISchemeHandler
    {
        public bool ProcessRequestAsync(IRequest request, ISchemeHandlerResponse response, OnRequestCompletedHandler requestCompletedCallback)
        {
            return false; // never invoked
        }
    }

    static void Main(string[] args)
    {
        const string testUrl = "https://www.sbobet.com";

        var set = new CefSettings();
        set.RegisterScheme(new CefCustomScheme()
        {
            SchemeName = "custom",
            SchemeHandlerFactory = new CefSharpSchemeHandlerFactory()
        });
        var c = set.CefCustomSchemes;

        Cef.Initialize(set);

        browser = new ChromiumWebBrowser();

        browser.Load(testUrl);

        if (captureFirstRenderedImage)
        {
            browser.ResourceHandler.RegisterHandler(testUrl, ResourceHandler.FromString("<html><body><h1>CefSharp OffScreen</h1></body></html>"));
        }
        else
        {
            browser.FrameLoadEnd += BrowserFrameLoadEnd;
        }

        Console.ReadKey();

        Cef.Shutdown();
    }

    private static void BrowserFrameLoadEnd(object sender, FrameLoadEndEventArgs e)
    {
        if (e.IsMainFrame)
        {
            browser.FrameLoadEnd -= BrowserFrameLoadEnd;
            // this always invoked!
        }
    }
}
amaitland commented 9 years ago

For future reference please read over the Contributing.md document specifically https://github.com/cefsharp/CefSharp/blob/master/CONTRIBUTING.md#what-should-i-include-when-creating-an-issue

When including code limit to small chunks, large blocks post as gist or similar

In future please post code samples as a GIST.

Please also include version and which package your using. In this case it looks like your using CefSharp.OffScreen (Better for you to include as much detail as possible when posting an issue).

Now onto the problem at hand. You've registered the custom schema, and as such all Url's starting with custom:// will be intercepted. Change const string testUrl = "custom://mytesturl";. You can register a custom schema handler for http, though you'd be responsible for serving the content.

More detailed example of SchemaHandler can be found at https://github.com/cefsharp/CefSharp/blob/master/CefSharp.Example/CefSharpSchemeHandler.cs#L39

alexeyk777 commented 9 years ago

Thanks! It helps me. I will be more careful in the future about posting here. Sorry.

amaitland commented 9 years ago

Thanks! It helps me. I will be more careful in the future about posting here. Sorry.

No problem :+1:

amaitland commented 9 years ago

Anything else required before closing this issue?