CefSharp and Crossdomain
Asked Answered
D

2

6

I'm trying to use the CefSharp web browser inside a .NET 4.0 application. I load pages from html and js content stored locally. So the web browser opens the pages with the file:// protocol.

The page transitions are made with jQuery (ajax) and I get a XMLHttpRequest cannot load file:///XXXXXXXX/Debug/res/www/shared/js/src/views/homeView.html. Cross origin requests are only supported for HTTP.

I tried using a Schemehandler and override the ProcessRequest function to handle page loads inside the file:// domain.

class SchemeHandlerFactory : ISchemeHandlerFactory
{
    public ISchemeHandler Create()
    {
        return new SchemeHandler();
    }
}

class SchemeHandler : ISchemeHandler
{
    public SchemeHandler()
    {
    }

    public bool ProcessRequest(IRequest request, ref string mimeType, ref Stream stream)
    {
        var uri = new Uri(request.Url);
        var segments = uri.Segments;
        var file = segments[segments.Length - 1];

        var bytes = File.ReadAllBytes(request.Url.Replace("file:///",""));
            stream = new MemoryStream(bytes);
            mimeType = "text/html";

            return true;
    }
}

And I use it in my form code by adding :

CEF.RegisterScheme("file", new SchemeHandlerFactory());

it doesn't change anything.

I checked the commit logs on the CefSharp repo and it says that the Cross AppDomain is now supported but how do I use it ? https://github.com/cefsharp/CefSharp/pull/35

I also thought on recompiling CefSharp to add the Access-Control-Allow-Origin to all the requests returned by the web browser when it executes GET operation on local files.

I'm out of ideas.

Dispassion answered 30/7, 2013 at 9:9 Comment(0)
D
8

I found the solution, here it is if someone needs help with this :

  BrowserSettings browserSettings = new BrowserSettings();
  browserSettings.FileAccessFromFileUrlsAllowed = true;
  browserSettings.UniversalAccessFromFileUrlsAllowed = true;
  browserSettings.TextAreaResizeDisabled = true;
  string urlToNavigate =
                 Application.StartupPath + @"\res\www\shared\index.html";
  web_view = new WebView(urlToNavigate, browserSettings);

We need to put the FileAccessFromFileUrlsAllowed to true and UniversalAccessFromFileUrlsAllowed.

Dispassion answered 2/8, 2013 at 9:38 Comment(1)
Your answer is probably correct. However, one very very important note to be made: cross-AppDomains are not the same as cross-domains in the web sense. They are two very, very different things. AppDomains is a complex concept which can be used to partition a .NET application into "unloadable", separated parts. So they have similar names but function very differently.Schiller
T
1

This code works on the CEF: r3.2987.1601 (Windows forms)

browser = new ChromiumWebBrowser("www.websites.com")
            {
                Dock = DockStyle.Fill,
            };
toolStripContainer.ContentPanel.Controls.Add(browser);

//browser.BrowserSettings.WebSecurity = CefState.Disabled;
browser.BrowserSettings.FileAccessFromFileUrls = CefState.Enabled;
browser.BrowserSettings.UniversalAccessFromFileUrls = CefState.Enabled;

https://github.com/cefsharp/CefSharp/issues/1397

Be very careful with the:

//browser.BrowserSettings.WebSecurity = CefState.Disabled;

This code leaves your computer vulnerable to malicious attacks.

Tillett answered 11/8, 2017 at 18:51 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.