Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(563)

Side by Side Diff: net/proxy/proxy_resolver_v8.cc

Issue 38001: Implement the PAC js-binding for "myIpAddress()". (Closed) Base URL: svn://chrome-svn/chrome/trunk/src/
Patch Set: update unittest for DnsResolve based on new empty string Created 11 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « net/base/net_util_unittest.cc ('k') | net/proxy/proxy_resolver_v8_unittest.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. Use of this 1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. Use of this
2 // source code is governed by a BSD-style license that can be found in the 2 // source code is governed by a BSD-style license that can be found in the
3 // LICENSE file. 3 // LICENSE file.
4 4
5 #include "net/proxy/proxy_resolver_v8.h" 5 #include "net/proxy/proxy_resolver_v8.h"
6 6
7 #include "base/logging.h" 7 #include "base/logging.h"
8 #include "base/string_util.h" 8 #include "base/string_util.h"
9 #include "googleurl/src/gurl.h" 9 #include "googleurl/src/gurl.h"
10 #include "net/base/address_list.h" 10 #include "net/base/address_list.h"
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after
53 // JSBIndings implementation. 53 // JSBIndings implementation.
54 class DefaultJSBindings : public ProxyResolverV8::JSBindings { 54 class DefaultJSBindings : public ProxyResolverV8::JSBindings {
55 public: 55 public:
56 // Handler for "alert(message)". 56 // Handler for "alert(message)".
57 virtual void Alert(const std::string& message) { 57 virtual void Alert(const std::string& message) {
58 LOG(INFO) << "PAC-alert: " << message; 58 LOG(INFO) << "PAC-alert: " << message;
59 } 59 }
60 60
61 // Handler for "myIpAddress()". Returns empty string on failure. 61 // Handler for "myIpAddress()". Returns empty string on failure.
62 virtual std::string MyIpAddress() { 62 virtual std::string MyIpAddress() {
63 // TODO(eroman): 63 // DnsResolve("") returns "", so no need to check for failure.
64 NOTIMPLEMENTED(); 64 return DnsResolve(GetMyHostName());
65 return "127.0.0.1";
66 } 65 }
67 66
68 // Handler for "dnsResolve(host)". Returns empty string on failure. 67 // Handler for "dnsResolve(host)". Returns empty string on failure.
69 virtual std::string DnsResolve(const std::string& host) { 68 virtual std::string DnsResolve(const std::string& host) {
69 // TODO(eroman): Should this return our IP address, or fail, or
70 // simply be unspecified (works differently on windows and mac os x).
71 if (host.empty())
72 return std::string();
73
70 // Try to resolve synchronously. 74 // Try to resolve synchronously.
71 net::AddressList address_list; 75 net::AddressList address_list;
72 const int kPort = 80; // Doesn't matter what this is. 76 const int kPort = 80; // Doesn't matter what this is.
73 int result = host_resolver_.Resolve(host, kPort, &address_list, NULL); 77 int result = host_resolver_.Resolve(host, kPort, &address_list, NULL);
74 78
75 if (result != OK) 79 if (result != OK)
76 return std::string(); // Failed. 80 return std::string(); // Failed.
77 81
78 if (!address_list.head()) 82 if (!address_list.head())
79 return std::string(); 83 return std::string();
(...skipping 149 matching lines...) Expand 10 before | Expand all | Expand 10 after
229 } 233 }
230 234
231 // V8 callback for when "myIpAddress()" is invoked by the PAC script. 235 // V8 callback for when "myIpAddress()" is invoked by the PAC script.
232 static v8::Handle<v8::Value> MyIpAddressCallback(const v8::Arguments& args) { 236 static v8::Handle<v8::Value> MyIpAddressCallback(const v8::Arguments& args) {
233 Context* context = 237 Context* context =
234 static_cast<Context*>(v8::External::Cast(*args.Data())->Value()); 238 static_cast<Context*>(v8::External::Cast(*args.Data())->Value());
235 239
236 // We shouldn't be called with any arguments, but will not complain if 240 // We shouldn't be called with any arguments, but will not complain if
237 // we are. 241 // we are.
238 std::string result = context->js_bindings_->MyIpAddress(); 242 std::string result = context->js_bindings_->MyIpAddress();
243 if (result.empty())
244 result = "127.0.0.1";
239 return StdStringToV8String(result); 245 return StdStringToV8String(result);
240 } 246 }
241 247
242 // V8 callback for when "dnsResolve()" is invoked by the PAC script. 248 // V8 callback for when "dnsResolve()" is invoked by the PAC script.
243 static v8::Handle<v8::Value> DnsResolveCallback(const v8::Arguments& args) { 249 static v8::Handle<v8::Value> DnsResolveCallback(const v8::Arguments& args) {
244 Context* context = 250 Context* context =
245 static_cast<Context*>(v8::External::Cast(*args.Data())->Value()); 251 static_cast<Context*>(v8::External::Cast(*args.Data())->Value());
246 252
247 // We need at least one argument. 253 // We need at least one argument.
248 std::string host; 254 std::string host;
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after
293 return context_->ResolveProxy(query_url, results); 299 return context_->ResolveProxy(query_url, results);
294 } 300 }
295 301
296 void ProxyResolverV8::SetPacScript(const std::string& data) { 302 void ProxyResolverV8::SetPacScript(const std::string& data) {
297 context_.reset(); 303 context_.reset();
298 if (!data.empty()) 304 if (!data.empty())
299 context_.reset(new Context(js_bindings_.get(), data)); 305 context_.reset(new Context(js_bindings_.get(), data));
300 } 306 }
301 307
302 } // namespace net 308 } // namespace net
OLDNEW
« no previous file with comments | « net/base/net_util_unittest.cc ('k') | net/proxy/proxy_resolver_v8_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698