Can I set a local file as src in CefAddCrossOriginWhitelist?

Having problems with building or using CEF's C/C++ APIs? This forum is here to help. Please do not post bug reports or feature requests here.

Can I set a local file as src in CefAddCrossOriginWhitelist?

Postby swtsvn » Wed Nov 18, 2015 6:13 am

I am working on CEF and whitelist entries, and getting an issue regarding allowing cross origin ajax calls to another web page. I read that CefAddCrossOriginWhitelistEntry can be used to do exactly that.

This Q&A viewtopic.php?f=6&t=10433 helped me fix the crash I was getting regarding the same. But it seems like I have to provide source_origin that needs to start with http or https. My issue is that I need to test the same with a local html file. But obviously it crashes because it is not able to find http/https protocol prefix.

My question is that, is there any way I can work with CefAddCrossOriginWhitelistEntry and a local file as source_origin ? Any help will be greatly appreciated. Thanks.

Consider testdomain.com to be hosted in a remote server, and has proper permissions to allow cross origin access or otherwise known as "Access-Control-Allow-Origin".

Code: Select all
 #include "cef_origin_whitelist.h"
    ...
    //inside a function
    // CefAddCrossOriginWhitelistEntry("http://source.mydomain.com", "http", "testdomain.com", true); //this works without crash.
    CefAddCrossOriginWhitelistEntry("H:/test/sample.html", "http", "testdomain.com", true); // this crashes. I am assuming because source is not a http or https url



sample.html:
Code: Select all

      <!DOCTYPE html>
      <html>
      <head>
      <title>sample</title>
      <script type="text/javascript" src="http://code.jquery.com/jquery-1.7.min.js"></script>
      </head>
      <body>
      <h1>sample</h1>
      
      <p><a href="#">Click here</a></p>
      
      <div id="result">
      
      </div>
      
      <script type="text/javascript">
      
      //JavaScript AJAX code here
      
      
   
     $(document).ready(function()
      {
        $('a').click(function()
         {
        $('#result').load('http://www.testdomain.com/index.html');
         });
      });
      
   
      </script>
         
      </body>
      </html>
      
swtsvn
Techie
 
Posts: 21
Joined: Wed Apr 22, 2015 4:56 pm

Re: Can I set a local file as src in CefAddCrossOriginWhitel

Postby magreenblatt » Wed Nov 18, 2015 11:20 am

swtsvn wrote:is there any way I can work with CefAddCrossOriginWhitelistEntry and a local file as source_origin ?

No, but you can disable web security for testing by passing the `--disable-web-security` command-line flag. Alternately, host your local files using custom request handling. See https://bitbucket.org/chromiumembedded/ ... t-handling
magreenblatt
Site Admin
 
Posts: 9831
Joined: Fri May 29, 2009 6:57 pm

Re: Can I set a local file as src in CefAddCrossOriginWhitel

Postby amaitland » Thu Nov 19, 2015 2:56 am

You could potentially use `Request Interception` and access the resources on the same domain, so you avoid any cross site origin requests.

For example you could go `http://mydomain.com/localsite/thispagewillbeservedfromdisk.html` and make requests to the same domain.
Maintainer of the CefSharp project.
amaitland
Master
 
Posts: 708
Joined: Wed Jan 14, 2015 2:35 am

Re: Can I set a local file as src in CefAddCrossOriginWhitel

Postby swtsvn » Thu Nov 19, 2015 7:30 am

@magreenblatt Thank you. I will work on hosting the local file using custom request handling or some other means. disabling web security is not an option because I need to enable cross origin access via CEF whitelisting.
@amaitland Thank you. Unfortunately cross domain requests cannot be omitted in my project, because it needs to support that feature.
swtsvn
Techie
 
Posts: 21
Joined: Wed Apr 22, 2015 4:56 pm

Re: Can I set a local file as src in CefAddCrossOriginWhitel

Postby amaitland » Thu Nov 19, 2015 2:15 pm

With my proposed solution you would actually make the requests from the same domain, so technically they re no longer cross origin. `CEF` allows you to intercept any request as per the link I provided.
Maintainer of the CefSharp project.
amaitland
Master
 
Posts: 708
Joined: Wed Jan 14, 2015 2:35 am

Re: Can I set a local file as src in CefAddCrossOriginWhitel

Postby swtsvn » Thu Nov 19, 2015 4:10 pm

On similar note anyone know how to enable --allow-file-access-from-files using CEF C++ API ?

-Edit
Never mind. Found it in CefBrowserSettings.
swtsvn
Techie
 
Posts: 21
Joined: Wed Apr 22, 2015 4:56 pm

Re: Can I set a local file as src in CefAddCrossOriginWhitel

Postby swtsvn » Tue Dec 08, 2015 4:14 pm

In case anyone else is searching for similar solution: I was able to allow cross domain access from local file (instead of server hosted one) by using CefBrowserSettings::universal_access_from_file_urls set to STATE_ENABLED and passing it to CreateBrowser.
swtsvn
Techie
 
Posts: 21
Joined: Wed Apr 22, 2015 4:56 pm


Return to Support Forum

Who is online

Users browsing this forum: Google [Bot] and 14 guests