CEF runtime proxy switches

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.

CEF runtime proxy switches

Postby michaelb » Sun Dec 10, 2023 2:15 pm

Hi

I have been successfully using proxy via CEF via the command line switches in OnBeforeCommandLineProcessing
We have had a requirement to be able to enable/disable proxy as per the network changes during runtime.
I have seen the CEF forums and examples where they do this via SetPreference and I have followed the example, but it does not work for me.
I am doing it on the browser UI thread, I think, at least.
When I check CanSetPreference, for proxy, it comes back as false.
Any idea what am I doing wrong?
Thanks in advance.

Code: Select all
            // Set proxy
            auto hostProcess = _handler->GetBrowser()->GetHost();
            auto requestContext = hostProcess->GetRequestContext();
            CefString error;
            auto proxyDictionary = CefDictionaryValue::Create();

            if (_useProxy.load() && _venueEnv != "")
            {
                std::string proxyUrl = "OUR PROXY HOST:PORT";//this is our proxy but not putting it here
                Logger::getInstance().logMessage(Severity::INFO_LEVEL, "CEF", __FUNCTION__, __FILE__, __LINE__,
                   "Will enable proxy server %s", proxyUrl.c_str());
                proxyDictionary->SetString("mode", "fixed_servers");
                proxyDictionary->SetString("server", proxyUrl.c_str());
            }
            else
            {
                Logger::getInstance().logMessage(Severity::INFO_LEVEL, "CEF", __FUNCTION__, __FILE__, __LINE__,
                    "Will disable proxy server.");
                proxyDictionary->SetString("mode", "direct");

            }
            auto proxyValue = CefValue::Create();
            proxyValue->SetDictionary(proxyDictionary);

            CEF_REQUIRE_UI_THREAD();
            if (requestContext->CanSetPreference("proxy"))
            {
                if (!requestContext->SetPreference("proxy", proxyValue, error))
                {
                    Logger::getInstance().logMessage(Severity::ERROR_LEVEL, "CEF", __FUNCTION__, __FILE__, __LINE__,
                                                     "Can not set proxy %s", error.c_str());
                }
            }
            else
            {
                Logger::getInstance().logMessage(Severity::INFO_LEVEL, "CEF", __FUNCTION__, __FILE__, __LINE__,
                    "Can not set proxy request context preference.");
                CefRefPtr<CefDictionaryValue> dict = requestContext->GetAllPreferences(true);
            }


And when I run it I get in log:
Can not set proxy request context preference.
michaelb
Techie
 
Posts: 21
Joined: Thu May 20, 2021 7:38 am

Re: CEF runtime proxy switches

Postby ndesktop » Mon Dec 11, 2023 2:37 am

Are you 100% sure you are in browser process? Try to check using cefclient, you can see in the preferences test (show all) if the setting has the intended value or not.
ndesktop
Master
 
Posts: 756
Joined: Thu Dec 03, 2015 10:10 am

Re: CEF runtime proxy switches

Postby amaitland » Thu Dec 14, 2023 2:32 am

If you set the proxy via command line args then it cannot be changed via SetPreference.
Maintainer of the CefSharp project.
amaitland
Virtuoso
 
Posts: 1292
Joined: Wed Jan 14, 2015 2:35 am


Return to Support Forum

Who is online

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