Example #1
0
    /// <summary>
    /// Create and add message router handlers.
    /// </summary>
    protected virtual void MessageRouterHandlers()
    {
        if (!CefRuntime.CurrentlyOn(CefThreadId.UI))
        {
            ActionTask.PostTask(CefThreadId.UI, MessageRouterHandlers);
            return;
        }

        _browserMessageRouter = new CefMessageRouterBrowserSide(new CefMessageRouterConfig());

        // Register message router handlers
        var messageRouterHandlers = _handlersResolver?.Invoke(typeof(IChromelyMessageRouter))?.ToList();

        if (messageRouterHandlers is not null && messageRouterHandlers.Any())
        {
            foreach (var handler in messageRouterHandlers)
            {
                var router = handler as CefMessageRouterBrowserSide.Handler;
                if (router is not null)
                {
                    _browserMessageRouter.AddHandler(router);
                }
            }
        }
    }
    protected virtual void RegisterCustomSchemeHandlers()
    {
        if (!CefRuntime.CurrentlyOn(CefThreadId.UI))
        {
            ActionTask.PostTask(CefThreadId.UI, RegisterCustomSchemeHandlers);
            return;
        }

        // Register custom request handlers
        var schemeHandlerList = _handlersResolver?.Invoke(typeof(IChromelySchemeHandler));

        if (schemeHandlerList is not null && schemeHandlerList.Any())
        {
            foreach (var schemeHandlerObj in schemeHandlerList)
            {
                if (schemeHandlerObj is not IChromelySchemeHandler schemehandler ||
                    schemehandler.Scheme is null ||
                    !schemehandler.Scheme.ValidSchemeHost)
                {
                    continue;
                }

                _requestSchemeProvider.Add(schemehandler.Scheme);
                var schemeHandlerFactory = schemehandler.HandlerFactory as CefSchemeHandlerFactory;
                if (schemeHandlerFactory is not null)
                {
                    CefRuntime.RegisterSchemeHandlerFactory(schemehandler.Scheme.Scheme, schemehandler.Scheme.Host, schemeHandlerFactory);
                }
            }
        }
    }
    protected void NativeHost_CreateAndShowWindow()
    {
        if (_config.Platform != ChromelyPlatform.Windows)
        {
            if (!CefRuntime.CurrentlyOn(CefThreadId.UI))
            {
                ActionTask.PostTask(CefThreadId.UI, NativeHost_CreateAndShowWindow);
                return;
            }
        }

        _nativeHost?.CreateWindow();
    }
    protected virtual void RegisterDefaultSchemeHandlers()
    {
        if (!CefRuntime.CurrentlyOn(CefThreadId.UI))
        {
            ActionTask.PostTask(CefThreadId.UI, RegisterDefaultSchemeHandlers);
            return;
        }

        if (_handlersResolver is null)
        {
            return;
        }

        // Register default resource/reguest handlers
        IDictionary <UrlSchemeType, Type> urlTypesMapper = new Dictionary <UrlSchemeType, Type>
        {
            { UrlSchemeType.LocalResource, typeof(IDefaultResourceCustomHandler) },
            { UrlSchemeType.FolderResource, typeof(IDefaultResourceCustomHandler) },
            { UrlSchemeType.AssemblyResource, typeof(IDefaultAssemblyResourceCustomHandler) },
            { UrlSchemeType.LocalRequest, typeof(IDefaultRequestCustomHandler) },
            { UrlSchemeType.Owin, typeof(IDefaultOwinCustomHandler) },
            { UrlSchemeType.ExternalRequest, typeof(IDefaultExernalRequestCustomHandler) }
        };

        foreach (var urlType in urlTypesMapper)
        {
            var handler = _handlersResolver.GetDefaultHandler(typeof(CefSchemeHandlerFactory), urlType.Value);

            if (handler is CefSchemeHandlerFactory schemeHandlerFactory)
            {
                var schemes = _config?.UrlSchemes.GetSchemesByType(urlType.Key);
                if (schemes is not null && schemes.Any())
                {
                    foreach (var item in schemes)
                    {
                        _requestSchemeProvider.Add(item);
                        CefRuntime.RegisterSchemeHandlerFactory(item.Scheme, item.Host, schemeHandlerFactory);
                    }
                }
            }
        }
    }