Skip to content
This repository has been archived by the owner on Jan 15, 2023. It is now read-only.

Registering Resource Handlers

Robin Gabriël edited this page Feb 23, 2020 · 2 revisions

Resource files (html, css, javasctipt etc) can be loaded in 3 different ways:

  • real website (e.g http://google.com)
  • file protocol (e.g file:///{appDirectory}app/chromely.html)
  • local resource (e.g local://app/chromely.html)

The preferred option is via local resources. For local resource processing a custom (or default) handler must be registered.

Registration of a resource handler requires 2 steps:

  1. Registration of a url scheme
  2. Registration of a custom resource scheme handler factory

1. Registration of a url scheme

You can register a url scheme either in config file or via C# code.

  • Using config file
"urlSchemes": [
    {
      "name": "custom-01",
      "baseUrl": "",
      "scheme": "local",
      "host": "",
      "urlSchemeType": "resource",
      "baseUrlStrict": false
    },
]
  • Using C# code
public class DefaultConfiguration : IChromelyConfiguration
{
    public DefaultConfiguration()
    {
        UrlSchemes.AddRange(new List<UrlScheme>()
        {
            new UrlScheme("custom-01", "local", string.Empty, string.Empty, UrlSchemeType.Resource, false),
        });
        
    }
}

2. Registration of a custom resource handler factory

A registered url scheme must be matched to custom resource handler. If no resource handler is provided, Chromely uses the provided CefGlueResourceSchemeHandler.

Registering a custom scheme requires creating both a custom scheme handler and custom scheme handler factory. The factory is then registered with the IOC container.

Custom scheme handler:

public class CustomResourceSchemeHandler : CefResourceHandler
{
}

Custom scheme handler factory:

See CefGlueResourceSchemeHandlerFactory for more information.

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

Handler factory registration:

public class DemoChromelyApp : ChromelyBasicApp
{
    public override void Configure(IChromelyContainer container)
    {
        base.Configure(container);
        container.RegisterSingleton(typeof(IChromelyResourceHandlerFactory), "custom-01", typeof(CustomResourceSchemeHandlerFactory));
    }
}

Important!

  • IChromelyResourceHandlerFactory is just a placeholder and should not be implemented/used elsewhere.
  • The name ex. "custom-01" used in url scheme must match the key used in factory registration.