Skip to content

Commit

Permalink
Emit an event to NetLog whenever the proxy settings change.
Browse files Browse the repository at this point in the history
Also removes the operator<< on ProxyConfig.

BUG=52004

Review URL: http://codereview.chromium.org/3144008

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@55999 0039d316-1c4b-4281-b951-d872f2087c98
  • Loading branch information
eroman@chromium.org committed Aug 13, 2010
1 parent d79a982 commit a809f90
Show file tree
Hide file tree
Showing 8 changed files with 219 additions and 145 deletions.
15 changes: 5 additions & 10 deletions chrome/browser/dom_ui/net_internals_ui.cc
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
#include "chrome/browser/dom_ui/net_internals_ui.h"

#include <algorithm>
#include <sstream>
#include <string>
#include <utility>
#include <vector>
Expand Down Expand Up @@ -601,19 +600,15 @@ void NetInternalsMessageHandler::IOThreadImpl::OnGetProxySettings(
URLRequestContext* context = context_getter_->GetURLRequestContext();
net::ProxyService* proxy_service = context->proxy_service();

// TODO(eroman): send a dictionary rather than a flat string, so client can do
// its own presentation.
std::string settings_string;

Value* settings_value = NULL;
if (proxy_service->config_has_been_initialized()) {
// net::ProxyConfig defines an operator<<.
std::ostringstream stream;
stream << proxy_service->config();
settings_string = stream.str();
settings_value = proxy_service->config().ToValue();
} else {
settings_value = Value::CreateStringValue(std::string());
}

CallJavascriptFunction(L"g_browser.receivedProxySettings",
Value::CreateStringValue(settings_string));
settings_value);
}

void NetInternalsMessageHandler::IOThreadImpl::OnReloadProxySettings(
Expand Down
16 changes: 2 additions & 14 deletions chrome/browser/importer/firefox_proxy_settings_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include <sstream>

#include "testing/gtest/include/gtest/gtest.h"

#include "base/file_path.h"
Expand Down Expand Up @@ -56,11 +54,6 @@ TEST_F(FirefoxProxySettingsTest, TestParse) {
net::ProxyConfig config;
EXPECT_TRUE(settings.ToProxyConfig(&config));

// Pretty-print |config| to a string (easy way to define the expectations).
std::ostringstream stream;
stream << config;
std::string pretty_printed_config = stream.str();

EXPECT_EQ(
"Automatic settings:\n"
" Auto-detect: No\n"
Expand All @@ -75,7 +68,7 @@ TEST_F(FirefoxProxySettingsTest, TestParse) {
" *localhost\n"
" 127.0.0.1\n"
" *noproxy.com",
pretty_printed_config);
config.ToString());
}

TEST_F(FirefoxProxySettingsTest, TestParseAutoConfigUrl) {
Expand Down Expand Up @@ -108,17 +101,12 @@ TEST_F(FirefoxProxySettingsTest, TestParseAutoConfigUrl) {
net::ProxyConfig config;
EXPECT_TRUE(settings.ToProxyConfig(&config));

// Pretty-print |config| to a string (easy way to define the expectations).
std::ostringstream stream;
stream << config;
std::string pretty_printed_config = stream.str();

EXPECT_EQ(
"Automatic settings:\n"
" Auto-detect: No\n"
" Custom PAC script: http://custom-pac-url/\n"
"Manual settings:\n"
" Proxy server: [None]\n"
" Bypass list: [None]",
pretty_printed_config);
config.ToString());
}
22 changes: 22 additions & 0 deletions chrome/browser/resources/net_internals/logviewpainter.js
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,9 @@ function getTextForExtraParams(entry, doStripCookies) {
case LogEventType.HTTP_TRANSACTION_READ_TUNNEL_RESPONSE_HEADERS:
return getTextForResponseHeadersExtraParam(entry, doStripCookies);

case LogEventType.PROXY_CONFIG_CHANGED:
return getTextForProxyConfigChangedExtraParam(entry);

default:
var out = [];
for (var k in entry.params) {
Expand Down Expand Up @@ -249,6 +252,25 @@ function getTextForResponseHeadersExtraParam(entry, doStripCookies) {
return indentLines(' --> ', headers);
}

function getTextForProxyConfigChangedExtraParam(entry) {
var params = entry.params;
var out = '';
var indentation = ' ';

if (params.old_config) {
// The previous configuration may not be present in the case of
// the initial proxy settings fetch.
out += ' --> old_config =\n' +
indentLines(indentation, params.old_config.split('\n'));
out += '\n';
}

out += ' --> new_config =\n' +
indentLines(indentation, params.new_config.split('\n'));

return out;
}

function getTextForEvent(entry) {
var text = '';

Expand Down
13 changes: 13 additions & 0 deletions net/base/net_log_event_type_list.h
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,19 @@ EVENT_TYPE(PROXY_SERVICE_WAITING_FOR_INIT_PAC)
// }
EVENT_TYPE(PROXY_SERVICE_RESOLVED_PROXY_LIST)

// This event is emitted whenever the proxy settings used by ProxyService
// change.
//
// It contains these parameters:
// {
// "old_config": <Dump of the previous proxy settings>,
// "new_config": <Dump of the new proxy settings>
// }
//
// Note that the "old_config" key will be omitted on the first fetch of the
// proxy settings (since there wasn't a previous value).
EVENT_TYPE(PROXY_CONFIG_CHANGED)

// ------------------------------------------------------------------------
// Proxy Resolver
// ------------------------------------------------------------------------
Expand Down
211 changes: 116 additions & 95 deletions net/proxy/proxy_config.cc
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,122 @@

#include "net/proxy/proxy_config.h"

#include <sstream>

#include "base/logging.h"
#include "base/string_number_conversions.h"
#include "base/string_tokenizer.h"
#include "base/string_util.h"
#include "base/values.h"
#include "net/proxy/proxy_info.h"

namespace net {

namespace {

// Helper to stringize a ProxyServer.
std::ostream& operator<<(std::ostream& out,
const ProxyServer& proxy_server) {
if (proxy_server.is_valid())
out << proxy_server.ToURI();
return out;
}

const char* BoolToYesNoString(bool b) {
return b ? "Yes" : "No";
}

std::ostream& operator<<(std::ostream& out,
const ProxyConfig::ProxyRules& rules) {
// Stringize the type enum.
std::string type;
switch (rules.type) {
case net::ProxyConfig::ProxyRules::TYPE_NO_RULES:
type = "TYPE_NO_RULES";
break;
case net::ProxyConfig::ProxyRules::TYPE_PROXY_PER_SCHEME:
type = "TYPE_PROXY_PER_SCHEME";
break;
case net::ProxyConfig::ProxyRules::TYPE_SINGLE_PROXY:
type = "TYPE_SINGLE_PROXY";
break;
default:
type = base::IntToString(rules.type);
break;
}
return out << " {\n"
<< " type: " << type << "\n"
<< " single_proxy: " << rules.single_proxy << "\n"
<< " proxy_for_http: " << rules.proxy_for_http << "\n"
<< " proxy_for_https: " << rules.proxy_for_https << "\n"
<< " proxy_for_ftp: " << rules.proxy_for_ftp << "\n"
<< " socks_proxy: " << rules.socks_proxy << "\n"
<< " }";
}

std::ostream& operator<<(std::ostream& out, const ProxyConfig& config) {
// "Automatic" settings.
out << "Automatic settings:\n";
out << " Auto-detect: " << BoolToYesNoString(config.auto_detect()) << "\n";
out << " Custom PAC script: ";
if (config.has_pac_url())
out << config.pac_url();
else
out << "[None]";
out << "\n";

// "Manual" settings.
out << "Manual settings:\n";
out << " Proxy server: ";

switch (config.proxy_rules().type) {
case net::ProxyConfig::ProxyRules::TYPE_NO_RULES:
out << "[None]\n";
break;
case net::ProxyConfig::ProxyRules::TYPE_SINGLE_PROXY:
out << config.proxy_rules().single_proxy;
out << "\n";
break;
case net::ProxyConfig::ProxyRules::TYPE_PROXY_PER_SCHEME:
out << "\n";
if (config.proxy_rules().proxy_for_http.is_valid())
out << " HTTP: " << config.proxy_rules().proxy_for_http << "\n";
if (config.proxy_rules().proxy_for_https.is_valid())
out << " HTTPS: " << config.proxy_rules().proxy_for_https << "\n";
if (config.proxy_rules().proxy_for_ftp.is_valid())
out << " FTP: " << config.proxy_rules().proxy_for_ftp << "\n";
if (config.proxy_rules().socks_proxy.is_valid())
out << " SOCKS: " << config.proxy_rules().socks_proxy << "\n";
break;
}

if (config.proxy_rules().reverse_bypass)
out << " Only use proxy for: ";
else
out << " Bypass list: ";
if (config.proxy_rules().bypass_rules.rules().empty()) {
out << "[None]";
} else {
const net::ProxyBypassRules& bypass_rules =
config.proxy_rules().bypass_rules;
net::ProxyBypassRules::RuleList::const_iterator it;
for (it = bypass_rules.rules().begin();
it != bypass_rules.rules().end(); ++it) {
out << "\n " << (*it)->ToString();
}
}

return out;
}

std::string ProxyConfigToString(const ProxyConfig& proxy_config) {
std::ostringstream stream;
stream << proxy_config;
return stream.str();
}

} // namespace

bool ProxyConfig::ProxyRules::Equals(const ProxyRules& other) const {
return type == other.type &&
single_proxy == other.single_proxy &&
Expand Down Expand Up @@ -145,103 +253,16 @@ bool ProxyConfig::MayRequirePACResolver() const {
return auto_detect_ || has_pac_url();
}

} // namespace net

namespace {

// Helper to stringize a ProxyServer.
std::ostream& operator<<(std::ostream& out,
const net::ProxyServer& proxy_server) {
if (proxy_server.is_valid())
out << proxy_server.ToURI();
return out;
}

const char* BoolToYesNoString(bool b) {
return b ? "Yes" : "No";
std::string ProxyConfig::ToString() const {
return ProxyConfigToString(*this);
}

} // namespace

std::ostream& operator<<(std::ostream& out,
const net::ProxyConfig::ProxyRules& rules) {
// Stringize the type enum.
std::string type;
switch (rules.type) {
case net::ProxyConfig::ProxyRules::TYPE_NO_RULES:
type = "TYPE_NO_RULES";
break;
case net::ProxyConfig::ProxyRules::TYPE_PROXY_PER_SCHEME:
type = "TYPE_PROXY_PER_SCHEME";
break;
case net::ProxyConfig::ProxyRules::TYPE_SINGLE_PROXY:
type = "TYPE_SINGLE_PROXY";
break;
default:
type = base::IntToString(rules.type);
break;
}
return out << " {\n"
<< " type: " << type << "\n"
<< " single_proxy: " << rules.single_proxy << "\n"
<< " proxy_for_http: " << rules.proxy_for_http << "\n"
<< " proxy_for_https: " << rules.proxy_for_https << "\n"
<< " proxy_for_ftp: " << rules.proxy_for_ftp << "\n"
<< " socks_proxy: " << rules.socks_proxy << "\n"
<< " }";
Value* ProxyConfig::ToValue() const {
// TODO(eroman): send a dictionary rather than a flat string, so the
// javascript client can do prettier formatting.
// crbug.com/52011
return Value::CreateStringValue(ToString());
}

std::ostream& operator<<(std::ostream& out, const net::ProxyConfig& config) {
// "Automatic" settings.
out << "Automatic settings:\n";
out << " Auto-detect: " << BoolToYesNoString(config.auto_detect()) << "\n";
out << " Custom PAC script: ";
if (config.has_pac_url())
out << config.pac_url();
else
out << "[None]";
out << "\n";

// "Manual" settings.
out << "Manual settings:\n";
out << " Proxy server: ";

switch (config.proxy_rules().type) {
case net::ProxyConfig::ProxyRules::TYPE_NO_RULES:
out << "[None]\n";
break;
case net::ProxyConfig::ProxyRules::TYPE_SINGLE_PROXY:
out << config.proxy_rules().single_proxy;
out << "\n";
break;
case net::ProxyConfig::ProxyRules::TYPE_PROXY_PER_SCHEME:
out << "\n";
if (config.proxy_rules().proxy_for_http.is_valid())
out << " HTTP: " << config.proxy_rules().proxy_for_http << "\n";
if (config.proxy_rules().proxy_for_https.is_valid())
out << " HTTPS: " << config.proxy_rules().proxy_for_https << "\n";
if (config.proxy_rules().proxy_for_ftp.is_valid())
out << " FTP: " << config.proxy_rules().proxy_for_ftp << "\n";
if (config.proxy_rules().socks_proxy.is_valid())
out << " SOCKS: " << config.proxy_rules().socks_proxy << "\n";
break;
}

if (config.proxy_rules().reverse_bypass)
out << " Only use proxy for: ";
else
out << " Bypass list: ";
if (config.proxy_rules().bypass_rules.rules().empty()) {
out << "[None]";
} else {
const net::ProxyBypassRules& bypass_rules =
config.proxy_rules().bypass_rules;
net::ProxyBypassRules::RuleList::const_iterator it;
for (it = bypass_rules.rules().begin();
it != bypass_rules.rules().end(); ++it) {
out << "\n " << (*it)->ToString();
}
}
} // namespace net

return out;
}
Loading

0 comments on commit a809f90

Please sign in to comment.