| 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/basictypes.h" | 7 #include "base/basictypes.h" |
| 8 #include "base/logging.h" | 8 #include "base/logging.h" |
| 9 #include "base/string_util.h" | 9 #include "base/string_util.h" |
| 10 #include "base/utf_string_conversions.h" | 10 #include "base/utf_string_conversions.h" |
| (...skipping 437 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 448 static v8::Handle<v8::Value> MyIpAddressCallback(const v8::Arguments& args) { | 448 static v8::Handle<v8::Value> MyIpAddressCallback(const v8::Arguments& args) { |
| 449 Context* context = | 449 Context* context = |
| 450 static_cast<Context*>(v8::External::Cast(*args.Data())->Value()); | 450 static_cast<Context*>(v8::External::Cast(*args.Data())->Value()); |
| 451 | 451 |
| 452 std::string result; | 452 std::string result; |
| 453 bool success; | 453 bool success; |
| 454 | 454 |
| 455 { | 455 { |
| 456 v8::Unlocker unlocker; | 456 v8::Unlocker unlocker; |
| 457 | 457 |
| 458 LogEventToCurrentRequest(context, | |
| 459 NetLog::PHASE_BEGIN, | |
| 460 NetLog::TYPE_PROXY_RESOLVER_V8_MY_IP_ADDRESS, | |
| 461 NULL); | |
| 462 | |
| 463 // We shouldn't be called with any arguments, but will not complain if | 458 // We shouldn't be called with any arguments, but will not complain if |
| 464 // we are. | 459 // we are. |
| 465 success = context->js_bindings_->MyIpAddress(&result); | 460 success = context->js_bindings_->MyIpAddress(&result); |
| 466 | |
| 467 LogEventToCurrentRequest(context, | |
| 468 NetLog::PHASE_END, | |
| 469 NetLog::TYPE_PROXY_RESOLVER_V8_MY_IP_ADDRESS, | |
| 470 NULL); | |
| 471 } | 461 } |
| 472 | 462 |
| 473 if (!success) | 463 if (!success) |
| 474 return ASCIILiteralToV8String("127.0.0.1"); | 464 return ASCIILiteralToV8String("127.0.0.1"); |
| 475 return ASCIIStringToV8String(result); | 465 return ASCIIStringToV8String(result); |
| 476 } | 466 } |
| 477 | 467 |
| 478 // V8 callback for when "myIpAddressEx()" is invoked by the PAC script. | 468 // V8 callback for when "myIpAddressEx()" is invoked by the PAC script. |
| 479 static v8::Handle<v8::Value> MyIpAddressExCallback( | 469 static v8::Handle<v8::Value> MyIpAddressExCallback( |
| 480 const v8::Arguments& args) { | 470 const v8::Arguments& args) { |
| 481 Context* context = | 471 Context* context = |
| 482 static_cast<Context*>(v8::External::Cast(*args.Data())->Value()); | 472 static_cast<Context*>(v8::External::Cast(*args.Data())->Value()); |
| 483 | 473 |
| 484 std::string ip_address_list; | 474 std::string ip_address_list; |
| 485 bool success; | 475 bool success; |
| 486 | 476 |
| 487 { | 477 { |
| 488 v8::Unlocker unlocker; | 478 v8::Unlocker unlocker; |
| 489 | 479 |
| 490 LogEventToCurrentRequest(context, | |
| 491 NetLog::PHASE_BEGIN, | |
| 492 NetLog::TYPE_PROXY_RESOLVER_V8_MY_IP_ADDRESS_EX, | |
| 493 NULL); | |
| 494 | |
| 495 // We shouldn't be called with any arguments, but will not complain if | 480 // We shouldn't be called with any arguments, but will not complain if |
| 496 // we are. | 481 // we are. |
| 497 success = context->js_bindings_->MyIpAddressEx(&ip_address_list); | 482 success = context->js_bindings_->MyIpAddressEx(&ip_address_list); |
| 498 | |
| 499 LogEventToCurrentRequest(context, | |
| 500 NetLog::PHASE_END, | |
| 501 NetLog::TYPE_PROXY_RESOLVER_V8_MY_IP_ADDRESS_EX, | |
| 502 NULL); | |
| 503 } | 483 } |
| 504 | 484 |
| 505 if (!success) | 485 if (!success) |
| 506 ip_address_list = std::string(); | 486 ip_address_list = std::string(); |
| 507 return ASCIIStringToV8String(ip_address_list); | 487 return ASCIIStringToV8String(ip_address_list); |
| 508 } | 488 } |
| 509 | 489 |
| 510 // V8 callback for when "dnsResolve()" is invoked by the PAC script. | 490 // V8 callback for when "dnsResolve()" is invoked by the PAC script. |
| 511 static v8::Handle<v8::Value> DnsResolveCallback(const v8::Arguments& args) { | 491 static v8::Handle<v8::Value> DnsResolveCallback(const v8::Arguments& args) { |
| 512 Context* context = | 492 Context* context = |
| 513 static_cast<Context*>(v8::External::Cast(*args.Data())->Value()); | 493 static_cast<Context*>(v8::External::Cast(*args.Data())->Value()); |
| 514 | 494 |
| 515 // We need at least one string argument. | 495 // We need at least one string argument. |
| 516 std::string hostname; | 496 std::string hostname; |
| 517 if (!GetHostnameArgument(args, &hostname)) | 497 if (!GetHostnameArgument(args, &hostname)) |
| 518 return v8::Null(); | 498 return v8::Null(); |
| 519 | 499 |
| 520 std::string ip_address; | 500 std::string ip_address; |
| 521 bool success; | 501 bool success; |
| 522 | 502 |
| 523 { | 503 { |
| 524 v8::Unlocker unlocker; | 504 v8::Unlocker unlocker; |
| 525 | |
| 526 LogEventToCurrentRequest(context, | |
| 527 NetLog::PHASE_BEGIN, | |
| 528 NetLog::TYPE_PROXY_RESOLVER_V8_DNS_RESOLVE, | |
| 529 NULL); | |
| 530 | |
| 531 success = context->js_bindings_->DnsResolve(hostname, &ip_address); | 505 success = context->js_bindings_->DnsResolve(hostname, &ip_address); |
| 532 | |
| 533 LogEventToCurrentRequest(context, | |
| 534 NetLog::PHASE_END, | |
| 535 NetLog::TYPE_PROXY_RESOLVER_V8_DNS_RESOLVE, | |
| 536 NULL); | |
| 537 } | 506 } |
| 538 | 507 |
| 539 return success ? ASCIIStringToV8String(ip_address) : v8::Null(); | 508 return success ? ASCIIStringToV8String(ip_address) : v8::Null(); |
| 540 } | 509 } |
| 541 | 510 |
| 542 // V8 callback for when "dnsResolveEx()" is invoked by the PAC script. | 511 // V8 callback for when "dnsResolveEx()" is invoked by the PAC script. |
| 543 static v8::Handle<v8::Value> DnsResolveExCallback(const v8::Arguments& args) { | 512 static v8::Handle<v8::Value> DnsResolveExCallback(const v8::Arguments& args) { |
| 544 Context* context = | 513 Context* context = |
| 545 static_cast<Context*>(v8::External::Cast(*args.Data())->Value()); | 514 static_cast<Context*>(v8::External::Cast(*args.Data())->Value()); |
| 546 | 515 |
| 547 // We need at least one string argument. | 516 // We need at least one string argument. |
| 548 std::string hostname; | 517 std::string hostname; |
| 549 if (!GetHostnameArgument(args, &hostname)) | 518 if (!GetHostnameArgument(args, &hostname)) |
| 550 return v8::Undefined(); | 519 return v8::Undefined(); |
| 551 | 520 |
| 552 std::string ip_address_list; | 521 std::string ip_address_list; |
| 553 bool success; | 522 bool success; |
| 554 | 523 |
| 555 { | 524 { |
| 556 v8::Unlocker unlocker; | 525 v8::Unlocker unlocker; |
| 557 | |
| 558 LogEventToCurrentRequest(context, | |
| 559 NetLog::PHASE_BEGIN, | |
| 560 NetLog::TYPE_PROXY_RESOLVER_V8_DNS_RESOLVE_EX, | |
| 561 NULL); | |
| 562 | |
| 563 success = context->js_bindings_->DnsResolveEx(hostname, | 526 success = context->js_bindings_->DnsResolveEx(hostname, |
| 564 &ip_address_list); | 527 &ip_address_list); |
| 565 | |
| 566 LogEventToCurrentRequest(context, | |
| 567 NetLog::PHASE_END, | |
| 568 NetLog::TYPE_PROXY_RESOLVER_V8_DNS_RESOLVE_EX, | |
| 569 NULL); | |
| 570 } | 528 } |
| 571 | 529 |
| 572 if (!success) | 530 if (!success) |
| 573 ip_address_list = std::string(); | 531 ip_address_list = std::string(); |
| 574 | 532 |
| 575 return ASCIIStringToV8String(ip_address_list); | 533 return ASCIIStringToV8String(ip_address_list); |
| 576 } | 534 } |
| 577 | 535 |
| 578 static void LogEventToCurrentRequest(Context* context, | |
| 579 NetLog::EventPhase phase, | |
| 580 NetLog::EventType type, | |
| 581 NetLog::EventParameters* params) { | |
| 582 if (context->js_bindings_->current_request_context()) { | |
| 583 context->js_bindings_->current_request_context()->net_log->AddEntry( | |
| 584 type, phase, params); | |
| 585 } | |
| 586 } | |
| 587 | |
| 588 ProxyResolverJSBindings* js_bindings_; | 536 ProxyResolverJSBindings* js_bindings_; |
| 589 v8::Persistent<v8::External> v8_this_; | 537 v8::Persistent<v8::External> v8_this_; |
| 590 v8::Persistent<v8::Context> v8_context_; | 538 v8::Persistent<v8::Context> v8_context_; |
| 591 }; | 539 }; |
| 592 | 540 |
| 593 // ProxyResolverV8 ------------------------------------------------------------ | 541 // ProxyResolverV8 ------------------------------------------------------------ |
| 594 | 542 |
| 595 ProxyResolverV8::ProxyResolverV8( | 543 ProxyResolverV8::ProxyResolverV8( |
| 596 ProxyResolverJSBindings* custom_js_bindings) | 544 ProxyResolverJSBindings* custom_js_bindings) |
| 597 : ProxyResolver(true /*expects_pac_bytes*/), | 545 : ProxyResolver(true /*expects_pac_bytes*/), |
| (...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 653 | 601 |
| 654 // Try parsing the PAC script. | 602 // Try parsing the PAC script. |
| 655 scoped_ptr<Context> context(new Context(js_bindings_.get())); | 603 scoped_ptr<Context> context(new Context(js_bindings_.get())); |
| 656 int rv = context->InitV8(pac_script); | 604 int rv = context->InitV8(pac_script); |
| 657 if (rv == OK) | 605 if (rv == OK) |
| 658 context_.reset(context.release()); | 606 context_.reset(context.release()); |
| 659 return rv; | 607 return rv; |
| 660 } | 608 } |
| 661 | 609 |
| 662 } // namespace net | 610 } // namespace net |
| OLD | NEW |