diff options
author | eroman@chromium.org <eroman@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2009-08-02 21:07:12 +0000 |
---|---|---|
committer | eroman@chromium.org <eroman@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2009-08-02 21:07:12 +0000 |
commit | 90ae1fb15d69378587f608f1bce437956b845a12 (patch) | |
tree | 7db5220f440e7b425cc67c2304b860494c1da72a | |
parent | d5726c5273e71ae842c1d7136821210f2f691592 (diff) | |
download | chromium_src-90ae1fb15d69378587f608f1bce437956b845a12.zip chromium_src-90ae1fb15d69378587f608f1bce437956b845a12.tar.gz chromium_src-90ae1fb15d69378587f608f1bce437956b845a12.tar.bz2 |
Rename ProxyResolverV8::JSBindings --> ProxyResolverJSBindings.
(Since it isn't really V8-specific, and could be re-used for impelmentations
using a different JS engine).
Review URL: http://codereview.chromium.org/159773
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@22256 0039d316-1c4b-4281-b951-d872f2087c98
-rw-r--r-- | net/net.gyp | 3 | ||||
-rw-r--r-- | net/proxy/proxy_resolver_js_bindings.cc | 143 | ||||
-rw-r--r-- | net/proxy/proxy_resolver_js_bindings.h | 48 | ||||
-rw-r--r-- | net/proxy/proxy_resolver_js_bindings_unittest.cc | 78 | ||||
-rw-r--r-- | net/proxy/proxy_resolver_perftest.cc | 5 | ||||
-rw-r--r-- | net/proxy/proxy_resolver_v8.cc | 136 | ||||
-rw-r--r-- | net/proxy/proxy_resolver_v8.h | 43 | ||||
-rw-r--r-- | net/proxy/proxy_resolver_v8_unittest.cc | 70 | ||||
-rw-r--r-- | net/proxy/proxy_service.cc | 5 |
9 files changed, 289 insertions, 242 deletions
diff --git a/net/net.gyp b/net/net.gyp index fb01d8b..651c232 100644 --- a/net/net.gyp +++ b/net/net.gyp @@ -264,6 +264,8 @@ 'proxy/proxy_list.cc', 'proxy/proxy_list.h', 'proxy/proxy_resolver.h', + 'proxy/proxy_resolver_js_bindings.cc', + 'proxy/proxy_resolver_js_bindings.h', 'proxy/proxy_resolver_mac.cc', 'proxy/proxy_resolver_mac.h', 'proxy/proxy_resolver_script.h', @@ -497,6 +499,7 @@ 'proxy/proxy_config_service_win_unittest.cc', 'proxy/proxy_config_unittest.cc', 'proxy/proxy_list_unittest.cc', + 'proxy/proxy_resolver_js_bindings_unittest.cc', 'proxy/proxy_resolver_v8_unittest.cc', 'proxy/proxy_script_fetcher_unittest.cc', 'proxy/proxy_server_unittest.cc', diff --git a/net/proxy/proxy_resolver_js_bindings.cc b/net/proxy/proxy_resolver_js_bindings.cc new file mode 100644 index 0000000..6787b1b --- /dev/null +++ b/net/proxy/proxy_resolver_js_bindings.cc @@ -0,0 +1,143 @@ +// Copyright (c) 2009 The Chromium Authors. All rights reserved. Use of this +// source code is governed by a BSD-style license that can be found in the +// LICENSE file. + +#include "net/proxy/proxy_resolver_js_bindings.h" + +#include "base/compiler_specific.h" +#include "base/logging.h" +#include "base/message_loop.h" +#include "base/waitable_event.h" +#include "net/base/address_list.h" +#include "net/base/host_resolver.h" +#include "net/base/net_errors.h" +#include "net/base/net_util.h" + +namespace net { +namespace { + +// Wrapper around HostResolver to give a sync API while running the resolve +// in async mode on |host_resolver_loop|. If |host_resolver_loop| is NULL, +// runs sync on the current thread (this mode is just used by testing). +class SyncHostResolverBridge + : public base::RefCountedThreadSafe<SyncHostResolverBridge> { + public: + SyncHostResolverBridge(HostResolver* host_resolver, + MessageLoop* host_resolver_loop) + : host_resolver_(host_resolver), + host_resolver_loop_(host_resolver_loop), + event_(false, false), + ALLOW_THIS_IN_INITIALIZER_LIST( + callback_(this, &SyncHostResolverBridge::OnResolveCompletion)) { + } + + // Run the resolve on host_resolver_loop, and wait for result. + int Resolve(const std::string& hostname, net::AddressList* addresses) { + // Port number doesn't matter. + HostResolver::RequestInfo info(hostname, 80); + + // Hack for tests -- run synchronously on current thread. + if (!host_resolver_loop_) + return host_resolver_->Resolve(info, addresses, NULL, NULL); + + // Otherwise start an async resolve on the resolver's thread. + host_resolver_loop_->PostTask(FROM_HERE, NewRunnableMethod(this, + &SyncHostResolverBridge::StartResolve, info, addresses)); + + // Wait for the resolve to complete in the resolver's thread. + event_.Wait(); + return err_; + } + + private: + // Called on host_resolver_loop_. + void StartResolve(const HostResolver::RequestInfo& info, + net::AddressList* addresses) { + DCHECK_EQ(host_resolver_loop_, MessageLoop::current()); + int error = host_resolver_->Resolve(info, addresses, &callback_, NULL); + if (error != ERR_IO_PENDING) + OnResolveCompletion(error); // Completed synchronously. + } + + // Called on host_resolver_loop_. + void OnResolveCompletion(int result) { + DCHECK_EQ(host_resolver_loop_, MessageLoop::current()); + err_ = result; + event_.Signal(); + } + + scoped_refptr<HostResolver> host_resolver_; + MessageLoop* host_resolver_loop_; + + // Event to notify completion of resolve request. + base::WaitableEvent event_; + + // Callback for when the resolve completes on host_resolver_loop_. + net::CompletionCallbackImpl<SyncHostResolverBridge> callback_; + + // The result from the result request (set by in host_resolver_loop_). + int err_; +}; + +// ProxyResolverJSBindings implementation. +class DefaultJSBindings : public ProxyResolverJSBindings { + public: + DefaultJSBindings(HostResolver* host_resolver, + MessageLoop* host_resolver_loop) + : host_resolver_(new SyncHostResolverBridge( + host_resolver, host_resolver_loop)) {} + + // Handler for "alert(message)". + virtual void Alert(const std::string& message) { + LOG(INFO) << "PAC-alert: " << message; + } + + // Handler for "myIpAddress()". Returns empty string on failure. + virtual std::string MyIpAddress() { + // DnsResolve("") returns "", so no need to check for failure. + return DnsResolve(GetHostName()); + } + + // Handler for "dnsResolve(host)". Returns empty string on failure. + virtual std::string DnsResolve(const std::string& host) { + // TODO(eroman): Should this return our IP address, or fail, or + // simply be unspecified (works differently on windows and mac os x). + if (host.empty()) + return std::string(); + + // Do a sync resolve of the hostname. + net::AddressList address_list; + int result = host_resolver_->Resolve(host, &address_list); + + if (result != OK) + return std::string(); // Failed. + + if (!address_list.head()) + return std::string(); + + // There may be multiple results; we will just use the first one. + // This returns empty string on failure. + return net::NetAddressToString(address_list.head()); + } + + // Handler for when an error is encountered. |line_number| may be -1. + virtual void OnError(int line_number, const std::string& message) { + if (line_number == -1) + LOG(INFO) << "PAC-error: " << message; + else + LOG(INFO) << "PAC-error: " << "line: " << line_number << ": " << message; + } + + private: + scoped_refptr<SyncHostResolverBridge> host_resolver_; +}; + +} // namespace + +// static +ProxyResolverJSBindings* ProxyResolverJSBindings::CreateDefault( + HostResolver* host_resolver, MessageLoop* host_resolver_loop) { + return new DefaultJSBindings(host_resolver, host_resolver_loop); +} + +} // namespace net diff --git a/net/proxy/proxy_resolver_js_bindings.h b/net/proxy/proxy_resolver_js_bindings.h new file mode 100644 index 0000000..6eff69d --- /dev/null +++ b/net/proxy/proxy_resolver_js_bindings.h @@ -0,0 +1,48 @@ +// Copyright (c) 2009 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#ifndef NET_PROXY_PROXY_JS_BINDINGS_H +#define NET_PROXY_PROXY_JS_BINDINGS_H + +#include <string> + +class MessageLoop; + +namespace net { + +class HostResolver; + +// Interface for the javascript bindings. +class ProxyResolverJSBindings { + public: + virtual ~ProxyResolverJSBindings() {} + + // Handler for "alert(message)" + virtual void Alert(const std::string& message) = 0; + + // Handler for "myIpAddress()". Returns empty string on failure. + virtual std::string MyIpAddress() = 0; + + // Handler for "dnsResolve(host)". Returns empty string on failure. + virtual std::string DnsResolve(const std::string& host) = 0; + + // Handler for when an error is encountered. |line_number| may be -1 + // if a line number is not applicable to this error. + virtual void OnError(int line_number, const std::string& error) = 0; + + // Creates a default javascript bindings implementation that will: + // - Send script error messages to LOG(INFO) + // - Send script alert()s to LOG(INFO) + // - Use the provided host resolver to service dnsResolve(). + // + // |host_resolver| will be used in async mode on |host_resolver_loop|. If + // |host_resolver_loop| is NULL, then |host_resolver| will be used in sync + // mode on the PAC thread. + static ProxyResolverJSBindings* CreateDefault( + HostResolver* host_resolver, MessageLoop* host_resolver_loop); +}; + +} // namespace net + +#endif // NET_PROXY_PROXY_JS_BINDINGS_H diff --git a/net/proxy/proxy_resolver_js_bindings_unittest.cc b/net/proxy/proxy_resolver_js_bindings_unittest.cc new file mode 100644 index 0000000..70b9333 --- /dev/null +++ b/net/proxy/proxy_resolver_js_bindings_unittest.cc @@ -0,0 +1,78 @@ +// Copyright (c) 2009 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "base/scoped_ptr.h" +#include "net/base/mock_host_resolver.h" +#include "net/base/net_errors.h" +#include "net/proxy/proxy_resolver_js_bindings.h" +#include "testing/gtest/include/gtest/gtest.h" + +namespace { + +TEST(ProxyResolverJSBindingsTest, DnsResolve) { + // Get a hold of a DefaultJSBindings* (it is a hidden impl class). + scoped_ptr<net::ProxyResolverJSBindings> bindings( + net::ProxyResolverJSBindings::CreateDefault( + new net::MockHostResolver, NULL)); + + // Considered an error. + EXPECT_EQ("", bindings->DnsResolve("")); + + const struct { + const char* input; + const char* expected; + } tests[] = { + {"www.google.com", "127.0.0.1"}, + {".", ""}, + {"foo@google.com", ""}, + {"@google.com", ""}, + {"foo:pass@google.com", ""}, + {"%", ""}, + {"www.google.com:80", ""}, + {"www.google.com:", ""}, + {"www.google.com.", ""}, + {"#", ""}, + {"127.0.0.1", ""}, + {"this has spaces", ""}, + }; + + for (size_t i = 0; i < ARRAYSIZE_UNSAFE(tests); ++i) { + std::string actual = bindings->DnsResolve(tests[i].input); + + // ######################################################################## + // TODO(eroman) + // ######################################################################## + // THIS TEST IS CURRENTLY FLAWED. + // + // Since we are running in unit-test mode, the HostResolve is using a + // mock HostResolverProc, which will always return 127.0.0.1, without going + // through the real codepaths. + // + // It is important that these tests be run with the real thing, since we + // need to verify that HostResolver doesn't blow up when you send it + // weird inputs. This is necessary since the data reach it is UNSANITIZED. + // It comes directly from the PAC javascript. + // + // For now we just check that it maps to 127.0.0.1. + std::string expected = tests[i].expected; + if (expected == "") + expected = "127.0.0.1"; + EXPECT_EQ(expected, actual); + } +} + +TEST(ProxyResolverV8DefaultBindingsTest, MyIpAddress) { + // Get a hold of a DefaultJSBindings* (it is a hidden impl class). + scoped_ptr<net::ProxyResolverJSBindings> bindings( + net::ProxyResolverJSBindings::CreateDefault( + new net::MockHostResolver, NULL)); + + // Our IP address is always going to be 127.0.0.1, since we are using a + // mock host resolver. + std::string my_ip_address = bindings->MyIpAddress(); + + EXPECT_EQ("127.0.0.1", my_ip_address); +} + +} // namespace diff --git a/net/proxy/proxy_resolver_perftest.cc b/net/proxy/proxy_resolver_perftest.cc index 45d1a7b..cb20ff9 100644 --- a/net/proxy/proxy_resolver_perftest.cc +++ b/net/proxy/proxy_resolver_perftest.cc @@ -4,6 +4,7 @@ #include "base/perftimer.h" #include "net/base/mock_host_resolver.h" +#include "net/proxy/proxy_resolver_js_bindings.h" #include "net/proxy/proxy_resolver_v8.h" #include "net/url_request/url_request_unittest.h" #include "testing/gtest/include/gtest/gtest.h" @@ -185,8 +186,8 @@ TEST(ProxyResolverPerfTest, ProxyResolverMac) { #endif TEST(ProxyResolverPerfTest, ProxyResolverV8) { - net::ProxyResolverV8::JSBindings* js_bindings = - net::ProxyResolverV8::CreateDefaultBindings( + net::ProxyResolverJSBindings* js_bindings = + net::ProxyResolverJSBindings::CreateDefault( new net::MockHostResolver, NULL); net::ProxyResolverV8 resolver(js_bindings); diff --git a/net/proxy/proxy_resolver_v8.cc b/net/proxy/proxy_resolver_v8.cc index b35a8ad..0b77145 100644 --- a/net/proxy/proxy_resolver_v8.cc +++ b/net/proxy/proxy_resolver_v8.cc @@ -4,21 +4,15 @@ #include "net/proxy/proxy_resolver_v8.h" -#include "base/compiler_specific.h" #include "base/logging.h" -#include "base/message_loop.h" -#include "base/waitable_event.h" #include "base/string_util.h" #include "googleurl/src/gurl.h" -#include "net/base/address_list.h" -#include "net/base/host_resolver.h" #include "net/base/net_errors.h" -#include "net/base/net_util.h" #include "net/proxy/proxy_info.h" +#include "net/proxy/proxy_resolver_js_bindings.h" #include "net/proxy/proxy_resolver_script.h" #include "v8/include/v8.h" - namespace net { namespace { @@ -53,129 +47,13 @@ bool V8ObjectToString(v8::Handle<v8::Value> object, std::string* result) { return true; } -// Wrapper around HostResolver to give a sync API while running the resolve -// in async mode on |host_resolver_loop|. If |host_resolver_loop| is NULL, -// runs sync on the current thread (this mode is just used by testing). -class SyncHostResolverBridge - : public base::RefCountedThreadSafe<SyncHostResolverBridge> { - public: - SyncHostResolverBridge(HostResolver* host_resolver, - MessageLoop* host_resolver_loop) - : host_resolver_(host_resolver), - host_resolver_loop_(host_resolver_loop), - event_(false, false), - ALLOW_THIS_IN_INITIALIZER_LIST( - callback_(this, &SyncHostResolverBridge::OnResolveCompletion)) { - } - - // Run the resolve on host_resolver_loop, and wait for result. - int Resolve(const std::string& hostname, net::AddressList* addresses) { - // Port number doesn't matter. - HostResolver::RequestInfo info(hostname, 80); - - // Hack for tests -- run synchronously on current thread. - if (!host_resolver_loop_) - return host_resolver_->Resolve(info, addresses, NULL, NULL); - - // Otherwise start an async resolve on the resolver's thread. - host_resolver_loop_->PostTask(FROM_HERE, NewRunnableMethod(this, - &SyncHostResolverBridge::StartResolve, info, addresses)); - - // Wait for the resolve to complete in the resolver's thread. - event_.Wait(); - return err_; - } - - private: - // Called on host_resolver_loop_. - void StartResolve(const HostResolver::RequestInfo& info, - net::AddressList* addresses) { - DCHECK_EQ(host_resolver_loop_, MessageLoop::current()); - int error = host_resolver_->Resolve(info, addresses, &callback_, NULL); - if (error != ERR_IO_PENDING) - OnResolveCompletion(error); // Completed synchronously. - } - - // Called on host_resolver_loop_. - void OnResolveCompletion(int result) { - DCHECK_EQ(host_resolver_loop_, MessageLoop::current()); - err_ = result; - event_.Signal(); - } - - scoped_refptr<HostResolver> host_resolver_; - MessageLoop* host_resolver_loop_; - - // Event to notify completion of resolve request. - base::WaitableEvent event_; - - // Callback for when the resolve completes on host_resolver_loop_. - net::CompletionCallbackImpl<SyncHostResolverBridge> callback_; - - // The result from the result request (set by in host_resolver_loop_). - int err_; -}; - -// JSBIndings implementation. -class DefaultJSBindings : public ProxyResolverV8::JSBindings { - public: - DefaultJSBindings(HostResolver* host_resolver, - MessageLoop* host_resolver_loop) - : host_resolver_(new SyncHostResolverBridge( - host_resolver, host_resolver_loop)) {} - - // Handler for "alert(message)". - virtual void Alert(const std::string& message) { - LOG(INFO) << "PAC-alert: " << message; - } - - // Handler for "myIpAddress()". Returns empty string on failure. - virtual std::string MyIpAddress() { - // DnsResolve("") returns "", so no need to check for failure. - return DnsResolve(GetHostName()); - } - - // Handler for "dnsResolve(host)". Returns empty string on failure. - virtual std::string DnsResolve(const std::string& host) { - // TODO(eroman): Should this return our IP address, or fail, or - // simply be unspecified (works differently on windows and mac os x). - if (host.empty()) - return std::string(); - - // Do a sync resolve of the hostname. - net::AddressList address_list; - int result = host_resolver_->Resolve(host, &address_list); - - if (result != OK) - return std::string(); // Failed. - - if (!address_list.head()) - return std::string(); - - // There may be multiple results; we will just use the first one. - // This returns empty string on failure. - return net::NetAddressToString(address_list.head()); - } - - // Handler for when an error is encountered. |line_number| may be -1. - virtual void OnError(int line_number, const std::string& message) { - if (line_number == -1) - LOG(INFO) << "PAC-error: " << message; - else - LOG(INFO) << "PAC-error: " << "line: " << line_number << ": " << message; - } - - private: - scoped_refptr<SyncHostResolverBridge> host_resolver_; -}; - } // namespace // ProxyResolverV8::Context --------------------------------------------------- class ProxyResolverV8::Context { public: - Context(JSBindings* js_bindings, const std::string& pac_data) + Context(ProxyResolverJSBindings* js_bindings, const std::string& pac_data) : js_bindings_(js_bindings) { DCHECK(js_bindings != NULL); InitV8(pac_data); @@ -335,7 +213,7 @@ class ProxyResolverV8::Context { return result.empty() ? v8::Null() : StdStringToV8String(result); } - JSBindings* js_bindings_; + ProxyResolverJSBindings* js_bindings_; v8::Persistent<v8::External> v8_this_; v8::Persistent<v8::Context> v8_context_; }; @@ -343,7 +221,7 @@ class ProxyResolverV8::Context { // ProxyResolverV8 ------------------------------------------------------------ ProxyResolverV8::ProxyResolverV8( - ProxyResolverV8::JSBindings* custom_js_bindings) + ProxyResolverJSBindings* custom_js_bindings) : ProxyResolver(true /*expects_pac_bytes*/), js_bindings_(custom_js_bindings) { } @@ -369,12 +247,6 @@ void ProxyResolverV8::CancelRequest(RequestHandle request) { NOTREACHED(); } -// static -ProxyResolverV8::JSBindings* ProxyResolverV8::CreateDefaultBindings( - HostResolver* host_resolver, MessageLoop* host_resolver_loop) { - return new DefaultJSBindings(host_resolver, host_resolver_loop); -} - void ProxyResolverV8::SetPacScriptByDataInternal(const std::string& data) { context_.reset(); if (!data.empty()) diff --git a/net/proxy/proxy_resolver_v8.h b/net/proxy/proxy_resolver_v8.h index 4c364e5..6689191 100644 --- a/net/proxy/proxy_resolver_v8.h +++ b/net/proxy/proxy_resolver_v8.h @@ -15,6 +15,7 @@ class MessageLoop; namespace net { class HostResolver; +class ProxyResolverJSBindings; // Implementation of ProxyResolver that uses V8 to evaluate PAC scripts. // @@ -36,12 +37,10 @@ class HostResolver; // and does not use locking since it expects to be alone. class ProxyResolverV8 : public ProxyResolver { public: - class JSBindings; - // Constructs a ProxyResolverV8 with custom bindings. ProxyResolverV8 takes // ownership of |custom_js_bindings| and deletes it when ProxyResolverV8 // is destroyed. - explicit ProxyResolverV8(JSBindings* custom_js_bindings); + explicit ProxyResolverV8(ProxyResolverJSBindings* custom_js_bindings); virtual ~ProxyResolverV8(); @@ -52,22 +51,7 @@ class ProxyResolverV8 : public ProxyResolver { RequestHandle* /*request*/); virtual void CancelRequest(RequestHandle request); - JSBindings* js_bindings() const { return js_bindings_.get(); } - - // Creates a default javascript bindings implementation that will: - // - Send script error messages to LOG(INFO) - // - Send script alert()s to LOG(INFO) - // - Use the provided host resolver to service dnsResolve(). - // - // For clients that need more control (for example, sending the script output - // to a UI widget), use the ProxyResolverV8(JSBindings*) and specify your - // own bindings. - // - // |host_resolver| will be used in async mode on |host_resolver_loop|. If - // |host_resolver_loop| is NULL, then |host_resolver| will be used in sync - // mode on the PAC thread. - static JSBindings* CreateDefaultBindings(HostResolver* host_resolver, - MessageLoop* host_resolver_loop); + ProxyResolverJSBindings* js_bindings() const { return js_bindings_.get(); } private: // Context holds the Javascript state for the most recently loaded PAC @@ -80,30 +64,11 @@ class ProxyResolverV8 : public ProxyResolver { scoped_ptr<Context> context_; - scoped_ptr<JSBindings> js_bindings_; + scoped_ptr<ProxyResolverJSBindings> js_bindings_; DISALLOW_COPY_AND_ASSIGN(ProxyResolverV8); }; -// Interface for the javascript bindings. -class ProxyResolverV8::JSBindings { - public: - virtual ~JSBindings() {} - - // Handler for "alert(message)" - virtual void Alert(const std::string& message) = 0; - - // Handler for "myIpAddress()". Returns empty string on failure. - virtual std::string MyIpAddress() = 0; - - // Handler for "dnsResolve(host)". Returns empty string on failure. - virtual std::string DnsResolve(const std::string& host) = 0; - - // Handler for when an error is encountered. |line_number| may be -1 - // if a line number is not applicable to this error. - virtual void OnError(int line_number, const std::string& error) = 0; -}; - } // namespace net #endif // NET_PROXY_PROXY_RESOLVER_V8_H_ diff --git a/net/proxy/proxy_resolver_v8_unittest.cc b/net/proxy/proxy_resolver_v8_unittest.cc index db359a4..20d878b 100644 --- a/net/proxy/proxy_resolver_v8_unittest.cc +++ b/net/proxy/proxy_resolver_v8_unittest.cc @@ -6,10 +6,10 @@ #include "base/string_util.h" #include "base/path_service.h" #include "googleurl/src/gurl.h" -#include "net/base/mock_host_resolver.h" #include "net/base/net_errors.h" -#include "net/proxy/proxy_resolver_v8.h" #include "net/proxy/proxy_info.h" +#include "net/proxy/proxy_resolver_js_bindings.h" +#include "net/proxy/proxy_resolver_v8.h" #include "testing/gtest/include/gtest/gtest.h" namespace { @@ -17,7 +17,7 @@ namespace { // Javascript bindings for ProxyResolverV8, which returns mock values. // Each time one of the bindings is called into, we push the input into a // list, for later verification. -class MockJSBindings : public net::ProxyResolverV8::JSBindings { +class MockJSBindings : public net::ProxyResolverJSBindings { public: MockJSBindings() : my_ip_address_count(0) {} @@ -376,67 +376,3 @@ TEST(ProxyResolverV8Test, V8Bindings) { EXPECT_EQ(2, bindings->my_ip_address_count); } -TEST(ProxyResolverV8DefaultBindingsTest, DnsResolve) { - // Get a hold of a DefaultJSBindings* (it is a hidden impl class). - scoped_ptr<net::ProxyResolverV8::JSBindings> bindings( - net::ProxyResolverV8::CreateDefaultBindings( - new net::MockHostResolver, NULL)); - - // Considered an error. - EXPECT_EQ("", bindings->DnsResolve("")); - - const struct { - const char* input; - const char* expected; - } tests[] = { - {"www.google.com", "127.0.0.1"}, - {".", ""}, - {"foo@google.com", ""}, - {"@google.com", ""}, - {"foo:pass@google.com", ""}, - {"%", ""}, - {"www.google.com:80", ""}, - {"www.google.com:", ""}, - {"www.google.com.", ""}, - {"#", ""}, - {"127.0.0.1", ""}, - {"this has spaces", ""}, - }; - - for (size_t i = 0; i < ARRAYSIZE_UNSAFE(tests); ++i) { - std::string actual = bindings->DnsResolve(tests[i].input); - - // ######################################################################## - // TODO(eroman) - // ######################################################################## - // THIS TEST IS CURRENTLY FLAWED. - // - // Since we are running in unit-test mode, the HostResolve is using a - // mock HostResolverProc, which will always return 127.0.0.1, without going - // through the real codepaths. - // - // It is important that these tests be run with the real thing, since we - // need to verify that HostResolver doesn't blow up when you send it - // weird inputs. This is necessary since the data reach it is UNSANITIZED. - // It comes directly from the PAC javascript. - // - // For now we just check that it maps to 127.0.0.1. - std::string expected = tests[i].expected; - if (expected == "") - expected = "127.0.0.1"; - EXPECT_EQ(expected, actual); - } -} - -TEST(ProxyResolverV8DefaultBindingsTest, MyIpAddress) { - // Get a hold of a DefaultJSBindings* (it is a hidden impl class). - scoped_ptr<net::ProxyResolverV8::JSBindings> bindings( - net::ProxyResolverV8::CreateDefaultBindings( - new net::MockHostResolver, NULL)); - - // Our IP address is always going to be 127.0.0.1, since we are using a - // mock host resolver. - std::string my_ip_address = bindings->MyIpAddress(); - - EXPECT_EQ("127.0.0.1", my_ip_address); -} diff --git a/net/proxy/proxy_service.cc b/net/proxy/proxy_service.cc index c79b46a..cdf269c 100644 --- a/net/proxy/proxy_service.cc +++ b/net/proxy/proxy_service.cc @@ -23,6 +23,7 @@ #include "net/proxy/proxy_config_service_linux.h" #endif #include "net/proxy/proxy_resolver.h" +#include "net/proxy/proxy_resolver_js_bindings.h" #include "net/proxy/proxy_resolver_v8.h" #include "net/proxy/single_threaded_proxy_resolver.h" #include "net/url_request/url_request_context.h" @@ -210,8 +211,8 @@ ProxyService* ProxyService::Create( if (use_v8_resolver) { // Send javascript errors and alerts to LOG(INFO). HostResolver* host_resolver = url_request_context->host_resolver(); - ProxyResolverV8::JSBindings* js_bindings = - ProxyResolverV8::CreateDefaultBindings(host_resolver, io_loop); + ProxyResolverJSBindings* js_bindings = + ProxyResolverJSBindings::CreateDefault(host_resolver, io_loop); proxy_resolver = new ProxyResolverV8(js_bindings); } else { |