(Delphi) How to dynamically change proxy with DCEF 3.2623+

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.

(Delphi) How to dynamically change proxy with DCEF 3.2623+

Postby jep » Fri Aug 26, 2016 9:13 am

I keep trying to post this to the DCEF google group, but I think it's decided it's spam. Didn't help that I posted, deleted and reposted a couple of times to fix errors. I think it's decided from now on anything that looks like it is spam. So I'm posting this here in case to help out anyone in the future tackling the same issue. Also I figure people here might be able to point out any grievous errors I might have made while trying to guess my way through it.

---------------------------------------------------
Just thought I'd throw this out there since I couldn't find any good info on it when I was trying to do it. This is how to use DCEF - specifically the branches 2623 and above (because it's not supported earlier) with dynamically set runtime proxies. I believe this only works if you don't pass in proxy settings on the command line.

What I did was open the guiclient demo and drop a combobox on Panel1:

Code: Select all
object cefProxy: TComboBox
  Left = 718
  Top = 1
  Width = 145
  Height = 21
  Align = alRight
  Style = csDropDownList
  ItemHeight = 13
  TabOrder = 1
  OnChange = cefProxyChange
  Items.Strings = (
    'system'
    'direct'
    'auto_detect'
    'fixed_servers'
    'pac_script')
end



With this event handler:

Code: Select all
procedure TMainForm.cefProxyChange(Sender: TObject);
var
  err: ustring;
  proxy: ICefValue;
  val: ICefValue;
begin
  proxy := crm.Browser.Host.RequestContext.GetPreference('proxy');
  val := TCefValueRef.New;
  val.SetString(cefProxy.Items[cefProxy.ItemIndex]);
  proxy.GetDictionary.SetValue('mode', val);
  if cefProxy.Items[cefProxy.ItemIndex] = 'fixed_servers' then
  begin
    proxy.GetDictionary.SetString('server', 'http://127.0.0.1:8888'); // format [scheme://]hostname[:port];etc.
    proxy.GetDictionary.SetString('bypass_list', ''); // example: 'address1;address2'
  end
  else if cefProxy.Items[cefProxy.ItemIndex] = 'pac_script' then
    proxy.GetDictionary.SetString('pac_url', 'http://localhost/test/pacscript.js');

  crm.Browser.Host.RequestContext.SetPreference('proxy', proxy, err);
end;




And in the FormCreate, I'm doing this to initialize the combobox:

Code: Select all
  cefProxy.ItemIndex := cefProxy.Items.IndexOf(crm.Browser.Host.RequestContext.
      GetPreference('proxy').GetDictionary.GetValue('mode').GetString);



Of course, it will always be "system" but I think it's instructive.

Note that you don't want to call crm.Browser before you first load a page because it will be nil. You could probably put this proxy code into OnBeforeBrowse if that worked better for your setup.

I've tested it with direct, system, fixed_server and pac_script. Haven't verified it with auto_detect simply because that's a bit more work to set up (our network doesn't have an auto-detect proxy).

Hope this helps someone else!
jep
Techie
 
Posts: 24
Joined: Mon Apr 30, 2012 5:14 pm

Return to Support Forum

Who is online

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