user1647411
user1647411

Reputation:

Proxy Settings Chromium Embedded

How to set manually proxy settings Ip:Port to Chromium Embedded. That will affect only the control not globaly like it is if you set it for IE.

Thanks

Upvotes: 4

Views: 7548

Answers (3)

Cristian Amarie
Cristian Amarie

Reputation: 160

The greater control is obtainable using a previously existing class, CefProxyHandler.
Newest implementations uses only command line flags (I found these inferior). You can find previous code on older SVN branches of CEF.

I have implemented in this way (the following are C++ directions on Windows - I won't paste much code since it can be read from the URL above):
- implement exported CefProxyHandler class following the other CEF class example (such as CefApp); class have a single method, GetProxyForUrl
- in cef_types.h define the cef_proxy_type_t enumeration (direct, named, PAC) and the cef_proxy_info_t struct (contains type and string list)
- in cef_types_wrappers.h define CefProxyInfoTraits over cef_proxy_info_t (follow the other structures example), and class CefProxyInfo : public CefStructBase
- in libcef/browser/browser_main.cc in PreMainMessageLoopRun replace ProxyServiceFactory::CreateProxyConfigService with CefProxyServiceFactory::CreateProxyConfigService; Cef factory will create a CefProxyService, which finally creates a CefProxyConfigService - inspired from src/net/proxy/proxy_config_service_win.cc implement class CefProxyConfigServiceWin; main thing is

static void GetCurrentProxyConfig(net::ProxyConfig* config) {
  CefRefPtr<CefApp> app = CefContentClient::Get()->application();
  if(app.get()) {
    CefRefPtr<CefProxyHandler> handler = app->GetProxyHandler();
    if(handler.get()) {
      // ... use handler->GetProxyForUrl etc.
    }
  }

  WINHTTP_CURRENT_USER_IE_PROXY_CONFIG ie_config = {0};
  if (!WinHttpGetIEProxyConfigForCurrentUser(&ie_config)) {
    LOG(ERROR) << "WinHttpGetIEProxyConfigForCurrentUser failed: " <<
      GetLastError();
    *config = net::ProxyConfig::CreateDirect();
    config->set_source(net::PROXY_CONFIG_SOURCE_SYSTEM_FAILED);
    return;
  }
  SetFromIEConfig(config, ie_config);
  FreeIEConfig(&ie_config);
} 


- re-add net::ProxyConfigServiceWin::set_force_auto_detect which was present in older releases
- in libcef/browser/url_request_context_getter.cc (inspire from older CEF branches) implement ProxyConfigServiceNull : public net::ProxyConfigService (all empty implementation), class CefProxyResolver : public net::ProxyResolver (here is where GetProxyForUrl is), and in CefURLRequestContextGetter::GetURLRequestContext() check for custom proxy handler, such as:

bool fCustomProxyHandler = false;
CefRefPtr<CefApp> app = CefContentClient::Get()->application();
if(app.get()) {
    CefRefPtr<CefProxyHandler> handler = app->GetProxyHandler();
    if(handler.get()) {
    #if defined(OS_WIN)
      // Force auto-detect so the client resolver will be called.
      net::ProxyConfigServiceWin::set_force_auto_detect(true);
    #endif
      // The client will provide proxy resolution.
      storage_->set_proxy_service(
          new net::ProxyService(
                                net::ProxyService::CreateSystemProxyConfigService(io_loop_->message_loop_proxy(), file_loop_), 
                                new CefProxyResolver(handler), 
                                NULL
                               )
      );
      fCustomProxyHandler = true;
    }
}
if(!fCustomProxyHandler) {
    //  custom proxy resolution not provided
    scoped_ptr<net::ProxyService> system_proxy_service;
    system_proxy_service.reset(
        ProxyServiceFactory::CreateProxyService(
            NULL,
            url_request_context_.get(),
            url_request_context_->network_delegate(),
            CefContentBrowserClient::Get()->proxy_config_service().release(),
            command_line));
    storage_->set_proxy_service(system_proxy_service.release());
}

The key thing is to provide storage_->set_proxy_service your own implementation. Last thing to do is to provide CefProxyHandler (previous versions could be added in CefApp or CefClient, similarly with life span handler, request handler etc). The example above is providing it from CefApp.

Newest branches (2357, maybe lower) separates more between browser process handler and app. It's up to you, all is needed is acces the proxy handler provider (usually CefApp) via available global context getters and from application either get proxy handler directly and call GetProxyHandler, or browser process handler (if you plan to add proxy getter there), CefClient etc. After that, you can easily have your GetProxyHandler query your app for proxy settings (post task, PostMessage with WaitForSingleObject if you're on Windows etc.) so proxy server/port/user/pass are retrieved via GetProxyForUrl.

Doing so, although not easy, will offer you complete control - you can even handle proxy per url if you want to, change dynamically inside the same browser/renderer/plugin without need for restarting (keep in mind things such as pending downloads or even plugin streaming may be impacted).

Upvotes: 1

Czarek Tomczak
Czarek Tomczak

Reputation: 20645

In CEF 3 you can use the --proxy-server command line switch. An example value would be socks5://127.0.0.1:8888. You can set it programmatically in CefApp::OnBeforeCommandLineProcessing. When setting it in this callback name shouldn't contain the -- prefix.

Upvotes: 1

user1393469
user1393469

Reputation:

uses ceflib;

procedure AppCefGetProxyForUrl(const url: ustring; var proxyType: TCefProxyType;
  var proxyList: ustring );
begin
  proxyType := CEF_PROXY_TYPE_NAMED;
  proxyList := 'ip:port';
end;

initialization
  CefGetProxyForUrl := @AppCefGetProxyForUrl;

end.

Upvotes: 0

Related Questions