OLD | NEW |
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the 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/net_errors.h" | 10 #include "net/base/net_errors.h" |
(...skipping 276 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
287 context->js_bindings_->Alert(message); | 287 context->js_bindings_->Alert(message); |
288 return v8::Undefined(); | 288 return v8::Undefined(); |
289 } | 289 } |
290 | 290 |
291 // V8 callback for when "myIpAddress()" is invoked by the PAC script. | 291 // V8 callback for when "myIpAddress()" is invoked by the PAC script. |
292 static v8::Handle<v8::Value> MyIpAddressCallback(const v8::Arguments& args) { | 292 static v8::Handle<v8::Value> MyIpAddressCallback(const v8::Arguments& args) { |
293 Context* context = | 293 Context* context = |
294 static_cast<Context*>(v8::External::Cast(*args.Data())->Value()); | 294 static_cast<Context*>(v8::External::Cast(*args.Data())->Value()); |
295 | 295 |
296 context->current_request_net_log_.BeginEvent( | 296 context->current_request_net_log_.BeginEvent( |
297 NetLog::TYPE_PROXY_RESOLVER_V8_MY_IP_ADDRESS); | 297 NetLog::TYPE_PROXY_RESOLVER_V8_MY_IP_ADDRESS, NULL); |
298 | 298 |
299 // We shouldn't be called with any arguments, but will not complain if | 299 // We shouldn't be called with any arguments, but will not complain if |
300 // we are. | 300 // we are. |
301 std::string result = context->js_bindings_->MyIpAddress(); | 301 std::string result = context->js_bindings_->MyIpAddress(); |
302 | 302 |
303 context->current_request_net_log_.EndEvent( | 303 context->current_request_net_log_.EndEvent( |
304 NetLog::TYPE_PROXY_RESOLVER_V8_MY_IP_ADDRESS); | 304 NetLog::TYPE_PROXY_RESOLVER_V8_MY_IP_ADDRESS, NULL); |
305 | 305 |
306 if (result.empty()) | 306 if (result.empty()) |
307 result = "127.0.0.1"; | 307 result = "127.0.0.1"; |
308 return StdStringToV8String(result); | 308 return StdStringToV8String(result); |
309 } | 309 } |
310 | 310 |
311 // V8 callback for when "myIpAddressEx()" is invoked by the PAC script. | 311 // V8 callback for when "myIpAddressEx()" is invoked by the PAC script. |
312 static v8::Handle<v8::Value> MyIpAddressExCallback( | 312 static v8::Handle<v8::Value> MyIpAddressExCallback( |
313 const v8::Arguments& args) { | 313 const v8::Arguments& args) { |
314 Context* context = | 314 Context* context = |
315 static_cast<Context*>(v8::External::Cast(*args.Data())->Value()); | 315 static_cast<Context*>(v8::External::Cast(*args.Data())->Value()); |
316 | 316 |
317 context->current_request_net_log_.BeginEvent( | 317 context->current_request_net_log_.BeginEvent( |
318 NetLog::TYPE_PROXY_RESOLVER_V8_MY_IP_ADDRESS_EX); | 318 NetLog::TYPE_PROXY_RESOLVER_V8_MY_IP_ADDRESS_EX, NULL); |
319 | 319 |
320 // We shouldn't be called with any arguments, but will not complain if | 320 // We shouldn't be called with any arguments, but will not complain if |
321 // we are. | 321 // we are. |
322 std::string result = context->js_bindings_->MyIpAddressEx(); | 322 std::string result = context->js_bindings_->MyIpAddressEx(); |
323 | 323 |
324 context->current_request_net_log_.EndEvent( | 324 context->current_request_net_log_.EndEvent( |
325 NetLog::TYPE_PROXY_RESOLVER_V8_MY_IP_ADDRESS_EX); | 325 NetLog::TYPE_PROXY_RESOLVER_V8_MY_IP_ADDRESS_EX, NULL); |
326 | 326 |
327 return StdStringToV8String(result); | 327 return StdStringToV8String(result); |
328 } | 328 } |
329 | 329 |
330 // V8 callback for when "dnsResolve()" is invoked by the PAC script. | 330 // V8 callback for when "dnsResolve()" is invoked by the PAC script. |
331 static v8::Handle<v8::Value> DnsResolveCallback(const v8::Arguments& args) { | 331 static v8::Handle<v8::Value> DnsResolveCallback(const v8::Arguments& args) { |
332 Context* context = | 332 Context* context = |
333 static_cast<Context*>(v8::External::Cast(*args.Data())->Value()); | 333 static_cast<Context*>(v8::External::Cast(*args.Data())->Value()); |
334 | 334 |
335 // We need at least one argument. | 335 // We need at least one argument. |
336 std::string host; | 336 std::string host; |
337 if (args.Length() == 0) { | 337 if (args.Length() == 0) { |
338 host = "undefined"; | 338 host = "undefined"; |
339 } else { | 339 } else { |
340 if (!V8ObjectToString(args[0], &host)) | 340 if (!V8ObjectToString(args[0], &host)) |
341 return v8::Undefined(); | 341 return v8::Undefined(); |
342 } | 342 } |
343 | 343 |
344 context->current_request_net_log_.BeginEvent( | 344 context->current_request_net_log_.BeginEvent( |
345 NetLog::TYPE_PROXY_RESOLVER_V8_DNS_RESOLVE); | 345 NetLog::TYPE_PROXY_RESOLVER_V8_DNS_RESOLVE, NULL); |
346 | 346 |
347 std::string result = context->js_bindings_->DnsResolve(host); | 347 std::string result = context->js_bindings_->DnsResolve(host); |
348 | 348 |
349 context->current_request_net_log_.EndEvent( | 349 context->current_request_net_log_.EndEvent( |
350 NetLog::TYPE_PROXY_RESOLVER_V8_DNS_RESOLVE); | 350 NetLog::TYPE_PROXY_RESOLVER_V8_DNS_RESOLVE, NULL); |
351 | 351 |
352 // DnsResolve() returns empty string on failure. | 352 // DnsResolve() returns empty string on failure. |
353 return result.empty() ? v8::Null() : StdStringToV8String(result); | 353 return result.empty() ? v8::Null() : StdStringToV8String(result); |
354 } | 354 } |
355 | 355 |
356 // V8 callback for when "dnsResolveEx()" is invoked by the PAC script. | 356 // V8 callback for when "dnsResolveEx()" is invoked by the PAC script. |
357 static v8::Handle<v8::Value> DnsResolveExCallback(const v8::Arguments& args) { | 357 static v8::Handle<v8::Value> DnsResolveExCallback(const v8::Arguments& args) { |
358 Context* context = | 358 Context* context = |
359 static_cast<Context*>(v8::External::Cast(*args.Data())->Value()); | 359 static_cast<Context*>(v8::External::Cast(*args.Data())->Value()); |
360 | 360 |
361 // We need at least one argument. | 361 // We need at least one argument. |
362 std::string host; | 362 std::string host; |
363 if (args.Length() == 0) { | 363 if (args.Length() == 0) { |
364 host = "undefined"; | 364 host = "undefined"; |
365 } else { | 365 } else { |
366 if (!V8ObjectToString(args[0], &host)) | 366 if (!V8ObjectToString(args[0], &host)) |
367 return v8::Undefined(); | 367 return v8::Undefined(); |
368 } | 368 } |
369 | 369 |
370 context->current_request_net_log_.BeginEvent( | 370 context->current_request_net_log_.BeginEvent( |
371 NetLog::TYPE_PROXY_RESOLVER_V8_DNS_RESOLVE_EX); | 371 NetLog::TYPE_PROXY_RESOLVER_V8_DNS_RESOLVE_EX, NULL); |
372 | 372 |
373 std::string result = context->js_bindings_->DnsResolveEx(host); | 373 std::string result = context->js_bindings_->DnsResolveEx(host); |
374 | 374 |
375 context->current_request_net_log_.EndEvent( | 375 context->current_request_net_log_.EndEvent( |
376 NetLog::TYPE_PROXY_RESOLVER_V8_DNS_RESOLVE_EX); | 376 NetLog::TYPE_PROXY_RESOLVER_V8_DNS_RESOLVE_EX, NULL); |
377 | 377 |
378 return StdStringToV8String(result); | 378 return StdStringToV8String(result); |
379 } | 379 } |
380 | 380 |
381 ProxyResolverJSBindings* js_bindings_; | 381 ProxyResolverJSBindings* js_bindings_; |
382 BoundNetLog current_request_net_log_; | 382 BoundNetLog current_request_net_log_; |
383 v8::Persistent<v8::External> v8_this_; | 383 v8::Persistent<v8::External> v8_this_; |
384 v8::Persistent<v8::Context> v8_context_; | 384 v8::Persistent<v8::Context> v8_context_; |
385 }; | 385 }; |
386 | 386 |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
430 | 430 |
431 // Try parsing the PAC script. | 431 // Try parsing the PAC script. |
432 scoped_ptr<Context> context(new Context(js_bindings_.get())); | 432 scoped_ptr<Context> context(new Context(js_bindings_.get())); |
433 int rv = context->InitV8(bytes_utf8); | 433 int rv = context->InitV8(bytes_utf8); |
434 if (rv == OK) | 434 if (rv == OK) |
435 context_.reset(context.release()); | 435 context_.reset(context.release()); |
436 return rv; | 436 return rv; |
437 } | 437 } |
438 | 438 |
439 } // namespace net | 439 } // namespace net |
OLD | NEW |