// Copyright © 2016 The CefSharp Authors. All rights reserved. // // Use of this source code is governed by a BSD-style license that can be found in the LICENSE file. using System; using System.Collections.Generic; using System.IO; using CefSharp.Example.Properties; namespace CefSharp.Example { /// /// Demo ISchemeHandlerFactory and IResourceHandlerFactory, reads that from resource in memory, could just /// as easily be from files on disk - only use this method if you already have pre loaded data, don't perform /// any web requests here or database lookups, best for static data only. /// public class InMemorySchemeAndResourceHandlerFactory : ISchemeHandlerFactory, IResourceHandlerFactory { /// /// Just a simple dictionary for resource lookup /// private static readonly IDictionary ResourceDictionary; static InMemorySchemeAndResourceHandlerFactory() { ResourceDictionary = new Dictionary { { "/home.html", Resources.home_html }, { "/assets/css/shCore.css", Resources.assets_css_shCore_css }, { "/assets/css/shCoreDefault.css", Resources.assets_css_shCoreDefault_css }, { "/assets/css/docs.css", Resources.assets_css_docs_css }, { "/assets/js/application.js", Resources.assets_js_application_js }, { "/assets/js/jquery.js", Resources.assets_js_jquery_js }, { "/assets/js/shBrushCSharp.js", Resources.assets_js_shBrushCSharp_js }, { "/assets/js/shBrushJScript.js", Resources.assets_js_shBrushJScript_js }, { "/assets/js/shCore.js", Resources.assets_js_shCore_js }, { "/bootstrap/bootstrap-theme.min.css", Resources.bootstrap_theme_min_css }, { "/bootstrap/bootstrap.min.css", Resources.bootstrap_min_css }, { "/bootstrap/bootstrap.min.js", Resources.bootstrap_min_js }, { "/BindingTest.html", Resources.BindingTest }, { "/ExceptionTest.html", Resources.ExceptionTest }, { "/PopupTest.html", Resources.PopupTest }, { "/SchemeTest.html", Resources.SchemeTest }, { "/TooltipTest.html", Resources.TooltipTest }, { "/FramedWebGLTest.html", Resources.FramedWebGLTest }, { "/MultiBindingTest.html", Resources.MultiBindingTest }, { "/ScriptedMethodsTest.html", Resources.ScriptedMethodsTest }, { "/ResponseFilterTest.html", Resources.ResponseFilterTest }, }; } /// /// The only method required to implement an ISchemeHandlerFactory /// /// browser /// frame /// scheme name this handler was registered for /// request, we'll use this to check the Url and load the appropriate resource /// /// return null to invoke the default behaviour, this is useful when you register a handler on the http/https scheme /// if we have a string that represents our resource in the lookup dictionary then return it as an IResourceHandler /// IResourceHandler ISchemeHandlerFactory.Create(IBrowser browser, IFrame frame, string schemeName, IRequest request) { //You can match the scheme/host if required, we don't need that in this example, so keeping it simple. var uri = new Uri(request.Url); var fileName = uri.AbsolutePath; var extension = Path.GetExtension(fileName); string resource; if (ResourceDictionary.TryGetValue(fileName, out resource) && !string.IsNullOrEmpty(resource)) { //For css/js/etc it's important to specify a mime/type, here we use the file extension to perform a lookup //there are overloads where you can specify more options including Encoding, mimeType return ResourceHandler.FromString(resource, extension); } return null; } /// /// This is typically required for the internal implementation and is here for performance reasons, /// in general you'd just return true /// bool IResourceHandlerFactory.HasHandlers { get { return true; } } /// /// The main method when implementing an IResourceHandlerFactory, use for standard schemes like http/https /// /// the ChromiumWebBrowser instance this handler is assocated with /// browser /// frame /// request, we'll use this to check the Url and load the appropriate resource /// /// return null to invoke the default behaviour, this is important when you want to only intercept a few specific requests /// if we have a string that represents our resource in the lookup dictionary then return it as an IResourceHandler /// IResourceHandler IResourceHandlerFactory.GetResourceHandler(IWebBrowser browserControl, IBrowser browser, IFrame frame, IRequest request) { var uri = new Uri(request.Url); //Compare scheme and host if (string.Equals(uri.Scheme, "http", StringComparison.OrdinalIgnoreCase) && string.Equals(uri.Host, "cefsharp", StringComparison.OrdinalIgnoreCase)) { var fileName = uri.AbsolutePath; var extension = Path.GetExtension(fileName); string resource; if (ResourceDictionary.TryGetValue(fileName, out resource) && !string.IsNullOrEmpty(resource)) { //For css/js/etc it's important to specify a mime/type, here we use the file extension to perform a lookup //there are overloads where you can specify more options including Encoding, mimeType return ResourceHandler.FromString(resource, extension); } } return null; } } }