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 215 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
226 url_canon::RawCanonOutputT<char16, kInitialBufferSize> punycode_output; | 226 url_canon::RawCanonOutputT<char16, kInitialBufferSize> punycode_output; |
227 if (!url_canon::IDNToASCII(hostname_utf16.data(), | 227 if (!url_canon::IDNToASCII(hostname_utf16.data(), |
228 hostname_utf16.length(), | 228 hostname_utf16.length(), |
229 &punycode_output)) { | 229 &punycode_output)) { |
230 return false; | 230 return false; |
231 } | 231 } |
232 | 232 |
233 // |punycode_output| should now be ASCII; convert it to a std::string. | 233 // |punycode_output| should now be ASCII; convert it to a std::string. |
234 // (We could use UTF16ToASCII() instead, but that requires an extra string | 234 // (We could use UTF16ToASCII() instead, but that requires an extra string |
235 // copy. Since ASCII is a subset of UTF8 the following is equivalent). | 235 // copy. Since ASCII is a subset of UTF8 the following is equivalent). |
236 bool success = UTF16ToUTF8(punycode_output.data(), | 236 bool success = base::UTF16ToUTF8(punycode_output.data(), |
237 punycode_output.length(), | 237 punycode_output.length(), |
238 hostname); | 238 hostname); |
239 DCHECK(success); | 239 DCHECK(success); |
240 DCHECK(IsStringASCII(*hostname)); | 240 DCHECK(IsStringASCII(*hostname)); |
241 return success; | 241 return success; |
242 } | 242 } |
243 | 243 |
244 // Wrapper for passing around IP address strings and IPAddressNumber objects. | 244 // Wrapper for passing around IP address strings and IPAddressNumber objects. |
245 struct IPAddress { | 245 struct IPAddress { |
246 IPAddress(const std::string& ip_string, const IPAddressNumber& ip_number) | 246 IPAddress(const std::string& ip_string, const IPAddressNumber& ip_number) |
(...skipping 118 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
365 v8::Isolate::Scope isolate_scope(isolate_); | 365 v8::Isolate::Scope isolate_scope(isolate_); |
366 v8::HandleScope scope(isolate_); | 366 v8::HandleScope scope(isolate_); |
367 | 367 |
368 v8::Local<v8::Context> context = | 368 v8::Local<v8::Context> context = |
369 v8::Local<v8::Context>::New(isolate_, v8_context_); | 369 v8::Local<v8::Context>::New(isolate_, v8_context_); |
370 v8::Context::Scope function_scope(context); | 370 v8::Context::Scope function_scope(context); |
371 | 371 |
372 v8::Local<v8::Value> function; | 372 v8::Local<v8::Value> function; |
373 if (!GetFindProxyForURL(&function)) { | 373 if (!GetFindProxyForURL(&function)) { |
374 js_bindings()->OnError( | 374 js_bindings()->OnError( |
375 -1, ASCIIToUTF16("FindProxyForURL() is undefined.")); | 375 -1, base::ASCIIToUTF16("FindProxyForURL() is undefined.")); |
376 return ERR_PAC_SCRIPT_FAILED; | 376 return ERR_PAC_SCRIPT_FAILED; |
377 } | 377 } |
378 | 378 |
379 v8::Handle<v8::Value> argv[] = { | 379 v8::Handle<v8::Value> argv[] = { |
380 ASCIIStringToV8String(isolate_, query_url.spec()), | 380 ASCIIStringToV8String(isolate_, query_url.spec()), |
381 ASCIIStringToV8String(isolate_, query_url.HostNoBrackets()), | 381 ASCIIStringToV8String(isolate_, query_url.HostNoBrackets()), |
382 }; | 382 }; |
383 | 383 |
384 v8::TryCatch try_catch; | 384 v8::TryCatch try_catch; |
385 v8::Local<v8::Value> ret = v8::Function::Cast(*function)->Call( | 385 v8::Local<v8::Value> ret = v8::Function::Cast(*function)->Call( |
386 context->Global(), arraysize(argv), argv); | 386 context->Global(), arraysize(argv), argv); |
387 | 387 |
388 if (try_catch.HasCaught()) { | 388 if (try_catch.HasCaught()) { |
389 HandleError(try_catch.Message()); | 389 HandleError(try_catch.Message()); |
390 return ERR_PAC_SCRIPT_FAILED; | 390 return ERR_PAC_SCRIPT_FAILED; |
391 } | 391 } |
392 | 392 |
393 if (!ret->IsString()) { | 393 if (!ret->IsString()) { |
394 js_bindings()->OnError( | 394 js_bindings()->OnError( |
395 -1, ASCIIToUTF16("FindProxyForURL() did not return a string.")); | 395 -1, base::ASCIIToUTF16("FindProxyForURL() did not return a string.")); |
396 return ERR_PAC_SCRIPT_FAILED; | 396 return ERR_PAC_SCRIPT_FAILED; |
397 } | 397 } |
398 | 398 |
399 base::string16 ret_str = V8StringToUTF16(ret->ToString()); | 399 base::string16 ret_str = V8StringToUTF16(ret->ToString()); |
400 | 400 |
401 if (!IsStringASCII(ret_str)) { | 401 if (!IsStringASCII(ret_str)) { |
402 // TODO(eroman): Rather than failing when a wide string is returned, we | 402 // TODO(eroman): Rather than failing when a wide string is returned, we |
403 // could extend the parsing to handle IDNA hostnames by | 403 // could extend the parsing to handle IDNA hostnames by |
404 // converting them to ASCII punycode. | 404 // converting them to ASCII punycode. |
405 // crbug.com/47234 | 405 // crbug.com/47234 |
406 base::string16 error_message = | 406 base::string16 error_message = |
407 ASCIIToUTF16("FindProxyForURL() returned a non-ASCII string " | 407 base::ASCIIToUTF16("FindProxyForURL() returned a non-ASCII string " |
408 "(crbug.com/47234): ") + ret_str; | 408 "(crbug.com/47234): ") + ret_str; |
409 js_bindings()->OnError(-1, error_message); | 409 js_bindings()->OnError(-1, error_message); |
410 return ERR_PAC_SCRIPT_FAILED; | 410 return ERR_PAC_SCRIPT_FAILED; |
411 } | 411 } |
412 | 412 |
413 results->UsePacString(UTF16ToASCII(ret_str)); | 413 results->UsePacString(UTF16ToASCII(ret_str)); |
414 return OK; | 414 return OK; |
415 } | 415 } |
416 | 416 |
417 int InitV8(const scoped_refptr<ProxyResolverScriptData>& pac_script) { | 417 int InitV8(const scoped_refptr<ProxyResolverScriptData>& pac_script) { |
418 v8::Locker locked(isolate_); | 418 v8::Locker locked(isolate_); |
(...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
489 rv = | 489 rv = |
490 RunScript(ScriptDataToV8String(isolate_, pac_script), kPacResourceName); | 490 RunScript(ScriptDataToV8String(isolate_, pac_script), kPacResourceName); |
491 if (rv != OK) | 491 if (rv != OK) |
492 return rv; | 492 return rv; |
493 | 493 |
494 // At a minimum, the FindProxyForURL() function must be defined for this | 494 // At a minimum, the FindProxyForURL() function must be defined for this |
495 // to be a legitimiate PAC script. | 495 // to be a legitimiate PAC script. |
496 v8::Local<v8::Value> function; | 496 v8::Local<v8::Value> function; |
497 if (!GetFindProxyForURL(&function)) { | 497 if (!GetFindProxyForURL(&function)) { |
498 js_bindings()->OnError( | 498 js_bindings()->OnError( |
499 -1, ASCIIToUTF16("FindProxyForURL() is undefined.")); | 499 -1, base::ASCIIToUTF16("FindProxyForURL() is undefined.")); |
500 return ERR_PAC_SCRIPT_FAILED; | 500 return ERR_PAC_SCRIPT_FAILED; |
501 } | 501 } |
502 | 502 |
503 return OK; | 503 return OK; |
504 } | 504 } |
505 | 505 |
506 void PurgeMemory() { | 506 void PurgeMemory() { |
507 v8::Locker locked(isolate_); | 507 v8::Locker locked(isolate_); |
508 v8::Isolate::Scope isolate_scope(isolate_); | 508 v8::Isolate::Scope isolate_scope(isolate_); |
509 v8::V8::LowMemoryNotification(); | 509 v8::V8::LowMemoryNotification(); |
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
557 | 557 |
558 // V8 callback for when "alert()" is invoked by the PAC script. | 558 // V8 callback for when "alert()" is invoked by the PAC script. |
559 static void AlertCallback(const v8::FunctionCallbackInfo<v8::Value>& args) { | 559 static void AlertCallback(const v8::FunctionCallbackInfo<v8::Value>& args) { |
560 Context* context = | 560 Context* context = |
561 static_cast<Context*>(v8::External::Cast(*args.Data())->Value()); | 561 static_cast<Context*>(v8::External::Cast(*args.Data())->Value()); |
562 | 562 |
563 // Like firefox we assume "undefined" if no argument was specified, and | 563 // Like firefox we assume "undefined" if no argument was specified, and |
564 // disregard any arguments beyond the first. | 564 // disregard any arguments beyond the first. |
565 base::string16 message; | 565 base::string16 message; |
566 if (args.Length() == 0) { | 566 if (args.Length() == 0) { |
567 message = ASCIIToUTF16("undefined"); | 567 message = base::ASCIIToUTF16("undefined"); |
568 } else { | 568 } else { |
569 if (!V8ObjectToUTF16String(args[0], &message, args.GetIsolate())) | 569 if (!V8ObjectToUTF16String(args[0], &message, args.GetIsolate())) |
570 return; // toString() threw an exception. | 570 return; // toString() threw an exception. |
571 } | 571 } |
572 | 572 |
573 context->js_bindings()->Alert(message); | 573 context->js_bindings()->Alert(message); |
574 } | 574 } |
575 | 575 |
576 // V8 callback for when "myIpAddress()" is invoked by the PAC script. | 576 // V8 callback for when "myIpAddress()" is invoked by the PAC script. |
577 static void MyIpAddressCallback( | 577 static void MyIpAddressCallback( |
(...skipping 246 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
824 return 0; | 824 return 0; |
825 | 825 |
826 v8::Locker locked(g_default_isolate_); | 826 v8::Locker locked(g_default_isolate_); |
827 v8::Isolate::Scope isolate_scope(g_default_isolate_); | 827 v8::Isolate::Scope isolate_scope(g_default_isolate_); |
828 v8::HeapStatistics heap_statistics; | 828 v8::HeapStatistics heap_statistics; |
829 g_default_isolate_->GetHeapStatistics(&heap_statistics); | 829 g_default_isolate_->GetHeapStatistics(&heap_statistics); |
830 return heap_statistics.used_heap_size(); | 830 return heap_statistics.used_heap_size(); |
831 } | 831 } |
832 | 832 |
833 } // namespace net | 833 } // namespace net |
OLD | NEW |