Ejemplo n.º 1
0
        public static DefaultResourceHandler FromText(string text)
        {
            var handler = new AsyncResourceHandler();

            handler.SetResponse(text);
            return(handler);
        }
            protected override CefResourceHandler GetResourceHandler(CefBrowser browser, CefFrame frame, CefRequest request)
            {
                if (request.Url == OwnerWebView.DefaultLocalUrl)
                {
                    return(AsyncResourceHandler.FromText(OwnerWebView.htmlToLoad ?? ""));
                }

                if (UrlHelper.IsChromeInternalUrl(request.Url))
                {
                    return(null);
                }

                var resourceHandler = new ResourceHandler(request, OwnerWebView.GetRequestUrl(request.Url, (ResourceType)request.ResourceType));

                void TriggerBeforeResourceLoadEvent()
                {
                    var beforeResourceLoad = OwnerWebView.BeforeResourceLoad;

                    if (beforeResourceLoad != null)
                    {
                        OwnerWebView.ExecuteWithAsyncErrorHandling(() => beforeResourceLoad(resourceHandler));
                    }
                }

                if (Uri.TryCreate(resourceHandler.Url, UriKind.Absolute, out var url) && url.Scheme == ResourceUrl.EmbeddedScheme)
                {
                    resourceHandler.BeginAsyncResponse(() => {
                        var urlWithoutQuery = new UriBuilder(url);
                        if (!string.IsNullOrEmpty(url.Query))
                        {
                            urlWithoutQuery.Query = string.Empty;
                        }

                        OwnerWebView.ExecuteWithAsyncErrorHandling(() => ResourcesProvider.LoadEmbeddedResource(resourceHandler, urlWithoutQuery.Uri));

                        TriggerBeforeResourceLoadEvent();

                        if (resourceHandler.Handled || OwnerWebView.IgnoreMissingResources)
                        {
                            return;
                        }

                        var resourceLoadFailed = OwnerWebView.ResourceLoadFailed;
                        if (resourceLoadFailed != null)
                        {
                            resourceLoadFailed(url.ToString());
                        }
                        else
                        {
                            OwnerWebView.ForwardUnhandledAsyncException(new InvalidOperationException("Resource not found: " + url));
                        }
                    });
                }
                else
                {
                    TriggerBeforeResourceLoadEvent();
                }

                return(resourceHandler.Handler);
            }
Ejemplo n.º 3
0
        private static AsyncResourceHandler CreateCefResourceHandler()
        {
            var handler = new AsyncResourceHandler();

            handler.Headers.Add("cache-control", "public, max-age=315360000");
            return(handler);
        }
        private AsyncResourceHandler GetOrCreateCefResourceHandler()
        {
            if (Handler != null)
            {
                return(Handler);
            }

            lock (syncRoot) {
                if (Handler != null)
                {
                    return(Handler);
                }

                var handler = new AsyncResourceHandler();
                handler.Headers.Add("cache-control", "public, max-age=315360000");
                Handler = handler;
                return(handler);
            }
        }