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.