OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 <algorithm> | 7 #include <algorithm> |
8 #include <cstdio> | 8 #include <cstdio> |
9 | 9 |
10 #include "base/basictypes.h" | 10 #include "base/basictypes.h" |
(...skipping 413 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
424 int ResolveProxy(const GURL& query_url, ProxyInfo* results) { | 424 int ResolveProxy(const GURL& query_url, ProxyInfo* results) { |
425 v8::Locker locked(isolate_); | 425 v8::Locker locked(isolate_); |
426 v8::Isolate::Scope isolate_scope(isolate_); | 426 v8::Isolate::Scope isolate_scope(isolate_); |
427 v8::HandleScope scope(isolate_); | 427 v8::HandleScope scope(isolate_); |
428 | 428 |
429 v8::Local<v8::Context> context = | 429 v8::Local<v8::Context> context = |
430 v8::Local<v8::Context>::New(isolate_, v8_context_); | 430 v8::Local<v8::Context>::New(isolate_, v8_context_); |
431 v8::Context::Scope function_scope(context); | 431 v8::Context::Scope function_scope(context); |
432 | 432 |
433 v8::Local<v8::Value> function; | 433 v8::Local<v8::Value> function; |
434 if (!GetFindProxyForURL(&function)) { | 434 int rv = GetFindProxyForURL(&function); |
435 js_bindings()->OnError( | 435 if (rv != OK) |
436 -1, base::ASCIIToUTF16("FindProxyForURL() is undefined.")); | 436 return rv; |
437 return ERR_PAC_SCRIPT_FAILED; | |
438 } | |
439 | 437 |
440 v8::Local<v8::Value> argv[] = { | 438 v8::Local<v8::Value> argv[] = { |
441 ASCIIStringToV8String(isolate_, query_url.spec()), | 439 ASCIIStringToV8String(isolate_, query_url.spec()), |
442 ASCIIStringToV8String(isolate_, query_url.HostNoBrackets()), | 440 ASCIIStringToV8String(isolate_, query_url.HostNoBrackets()), |
443 }; | 441 }; |
444 | 442 |
445 v8::TryCatch try_catch; | 443 v8::TryCatch try_catch; |
446 v8::Local<v8::Value> ret = v8::Function::Cast(*function)->Call( | 444 v8::Local<v8::Value> ret = v8::Function::Cast(*function)->Call( |
447 context->Global(), arraysize(argv), argv); | 445 context->Global(), arraysize(argv), argv); |
448 | 446 |
(...skipping 105 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
554 | 552 |
555 // Add the user's PAC code to the environment. | 553 // Add the user's PAC code to the environment. |
556 rv = | 554 rv = |
557 RunScript(ScriptDataToV8String(isolate_, pac_script), kPacResourceName); | 555 RunScript(ScriptDataToV8String(isolate_, pac_script), kPacResourceName); |
558 if (rv != OK) | 556 if (rv != OK) |
559 return rv; | 557 return rv; |
560 | 558 |
561 // At a minimum, the FindProxyForURL() function must be defined for this | 559 // At a minimum, the FindProxyForURL() function must be defined for this |
562 // to be a legitimiate PAC script. | 560 // to be a legitimiate PAC script. |
563 v8::Local<v8::Value> function; | 561 v8::Local<v8::Value> function; |
564 if (!GetFindProxyForURL(&function)) { | 562 return GetFindProxyForURL(&function); |
| 563 } |
| 564 |
| 565 private: |
| 566 int GetFindProxyForURL(v8::Local<v8::Value>* function) { |
| 567 v8::Local<v8::Context> context = |
| 568 v8::Local<v8::Context>::New(isolate_, v8_context_); |
| 569 |
| 570 v8::TryCatch try_catch; |
| 571 |
| 572 *function = |
| 573 context->Global()->Get( |
| 574 ASCIILiteralToV8String(isolate_, "FindProxyForURL")); |
| 575 |
| 576 if (try_catch.HasCaught()) |
| 577 HandleError(try_catch.Message()); |
| 578 |
| 579 // The value should only be empty if an exception was thrown. Code |
| 580 // defensively just in case. |
| 581 DCHECK_EQ(function->IsEmpty(), try_catch.HasCaught()); |
| 582 if (function->IsEmpty() || try_catch.HasCaught()) { |
565 js_bindings()->OnError( | 583 js_bindings()->OnError( |
566 -1, base::ASCIIToUTF16("FindProxyForURL() is undefined.")); | 584 -1, |
| 585 base::ASCIIToUTF16("Accessing FindProxyForURL threw an exception.")); |
| 586 return ERR_PAC_SCRIPT_FAILED; |
| 587 } |
| 588 |
| 589 if (!(*function)->IsFunction()) { |
| 590 js_bindings()->OnError( |
| 591 -1, base::ASCIIToUTF16( |
| 592 "FindProxyForURL is undefined or not a function.")); |
567 return ERR_PAC_SCRIPT_FAILED; | 593 return ERR_PAC_SCRIPT_FAILED; |
568 } | 594 } |
569 | 595 |
570 return OK; | 596 return OK; |
571 } | 597 } |
572 | 598 |
573 private: | |
574 bool GetFindProxyForURL(v8::Local<v8::Value>* function) { | |
575 v8::Local<v8::Context> context = | |
576 v8::Local<v8::Context>::New(isolate_, v8_context_); | |
577 *function = | |
578 context->Global()->Get( | |
579 ASCIILiteralToV8String(isolate_, "FindProxyForURL")); | |
580 return (*function)->IsFunction(); | |
581 } | |
582 | |
583 // Handle an exception thrown by V8. | 599 // Handle an exception thrown by V8. |
584 void HandleError(v8::Local<v8::Message> message) { | 600 void HandleError(v8::Local<v8::Message> message) { |
585 base::string16 error_message; | 601 base::string16 error_message; |
586 int line_number = -1; | 602 int line_number = -1; |
587 | 603 |
588 if (!message.IsEmpty()) { | 604 if (!message.IsEmpty()) { |
589 line_number = message->GetLineNumber(); | 605 line_number = message->GetLineNumber(); |
590 V8ObjectToUTF16String(message->Get(), &error_message, isolate_); | 606 V8ObjectToUTF16String(message->Get(), &error_message, isolate_); |
591 } | 607 } |
592 | 608 |
(...skipping 278 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
871 return 0; | 887 return 0; |
872 | 888 |
873 v8::Locker locked(isolate); | 889 v8::Locker locked(isolate); |
874 v8::Isolate::Scope isolate_scope(isolate); | 890 v8::Isolate::Scope isolate_scope(isolate); |
875 v8::HeapStatistics heap_statistics; | 891 v8::HeapStatistics heap_statistics; |
876 isolate->GetHeapStatistics(&heap_statistics); | 892 isolate->GetHeapStatistics(&heap_statistics); |
877 return heap_statistics.used_heap_size(); | 893 return heap_statistics.used_heap_size(); |
878 } | 894 } |
879 | 895 |
880 } // namespace net | 896 } // namespace net |
OLD | NEW |