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 #define V8_DISABLE_DEPRECATIONS 1 | |
6 | |
7 #include "net/proxy/proxy_resolver_v8.h" | 5 #include "net/proxy/proxy_resolver_v8.h" |
8 | 6 |
9 #include <algorithm> | 7 #include <algorithm> |
10 #include <cstdio> | 8 #include <cstdio> |
11 | 9 |
12 #include "base/basictypes.h" | 10 #include "base/basictypes.h" |
13 #include "base/compiler_specific.h" | 11 #include "base/compiler_specific.h" |
14 #include "base/logging.h" | 12 #include "base/logging.h" |
15 #include "base/string_tokenizer.h" | 13 #include "base/string_tokenizer.h" |
16 #include "base/string_util.h" | 14 #include "base/string_util.h" |
(...skipping 310 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
327 | 325 |
328 return IPNumberMatchesPrefix(address, prefix, prefix_length_in_bits); | 326 return IPNumberMatchesPrefix(address, prefix, prefix_length_in_bits); |
329 } | 327 } |
330 | 328 |
331 } // namespace | 329 } // namespace |
332 | 330 |
333 // ProxyResolverV8::Context --------------------------------------------------- | 331 // ProxyResolverV8::Context --------------------------------------------------- |
334 | 332 |
335 class ProxyResolverV8::Context { | 333 class ProxyResolverV8::Context { |
336 public: | 334 public: |
337 explicit Context(ProxyResolverV8* parent) | 335 Context(ProxyResolverV8* parent, v8::Isolate* isolate) |
338 : parent_(parent) { | 336 : parent_(parent), |
| 337 isolate_(isolate) { |
| 338 DCHECK(isolate); |
339 } | 339 } |
340 | 340 |
341 ~Context() { | 341 ~Context() { |
342 v8::Locker locked; | 342 v8::Locker locked(isolate_); |
343 | 343 |
344 v8_this_.Dispose(); | 344 v8_this_.Dispose(isolate_); |
345 v8_context_.Dispose(); | 345 v8_context_.Dispose(isolate_); |
346 } | 346 } |
347 | 347 |
348 JSBindings* js_bindings() { | 348 JSBindings* js_bindings() { |
349 return parent_->js_bindings_; | 349 return parent_->js_bindings_; |
350 } | 350 } |
351 | 351 |
352 int ResolveProxy(const GURL& query_url, ProxyInfo* results) { | 352 int ResolveProxy(const GURL& query_url, ProxyInfo* results) { |
353 v8::Locker locked; | 353 v8::Locker locked(isolate_); |
354 v8::HandleScope scope; | 354 v8::HandleScope scope; |
355 | 355 |
356 v8::Context::Scope function_scope(v8_context_); | 356 v8::Context::Scope function_scope(v8_context_); |
357 | 357 |
358 v8::Local<v8::Value> function; | 358 v8::Local<v8::Value> function; |
359 if (!GetFindProxyForURL(&function)) { | 359 if (!GetFindProxyForURL(&function)) { |
360 js_bindings()->OnError( | 360 js_bindings()->OnError( |
361 -1, ASCIIToUTF16("FindProxyForURL() is undefined.")); | 361 -1, ASCIIToUTF16("FindProxyForURL() is undefined.")); |
362 return ERR_PAC_SCRIPT_FAILED; | 362 return ERR_PAC_SCRIPT_FAILED; |
363 } | 363 } |
(...skipping 30 matching lines...) Expand all Loading... |
394 "(crbug.com/47234): ") + ret_str; | 394 "(crbug.com/47234): ") + ret_str; |
395 js_bindings()->OnError(-1, error_message); | 395 js_bindings()->OnError(-1, error_message); |
396 return ERR_PAC_SCRIPT_FAILED; | 396 return ERR_PAC_SCRIPT_FAILED; |
397 } | 397 } |
398 | 398 |
399 results->UsePacString(UTF16ToASCII(ret_str)); | 399 results->UsePacString(UTF16ToASCII(ret_str)); |
400 return OK; | 400 return OK; |
401 } | 401 } |
402 | 402 |
403 int InitV8(const scoped_refptr<ProxyResolverScriptData>& pac_script) { | 403 int InitV8(const scoped_refptr<ProxyResolverScriptData>& pac_script) { |
404 v8::Locker locked; | 404 v8::Locker locked(isolate_); |
405 v8::HandleScope scope; | 405 v8::HandleScope scope; |
406 | 406 |
407 v8_this_ = v8::Persistent<v8::External>::New(v8::External::New(this)); | 407 v8_this_ = v8::Persistent<v8::External>::New(isolate_, |
| 408 v8::External::New(this)); |
408 v8::Local<v8::ObjectTemplate> global_template = v8::ObjectTemplate::New(); | 409 v8::Local<v8::ObjectTemplate> global_template = v8::ObjectTemplate::New(); |
409 | 410 |
410 // Attach the javascript bindings. | 411 // Attach the javascript bindings. |
411 v8::Local<v8::FunctionTemplate> alert_template = | 412 v8::Local<v8::FunctionTemplate> alert_template = |
412 v8::FunctionTemplate::New(&AlertCallback, v8_this_); | 413 v8::FunctionTemplate::New(&AlertCallback, v8_this_); |
413 global_template->Set(ASCIILiteralToV8String("alert"), alert_template); | 414 global_template->Set(ASCIILiteralToV8String("alert"), alert_template); |
414 | 415 |
415 v8::Local<v8::FunctionTemplate> my_ip_address_template = | 416 v8::Local<v8::FunctionTemplate> my_ip_address_template = |
416 v8::FunctionTemplate::New(&MyIpAddressCallback, v8_this_); | 417 v8::FunctionTemplate::New(&MyIpAddressCallback, v8_this_); |
417 global_template->Set(ASCIILiteralToV8String("myIpAddress"), | 418 global_template->Set(ASCIILiteralToV8String("myIpAddress"), |
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
472 if (!GetFindProxyForURL(&function)) { | 473 if (!GetFindProxyForURL(&function)) { |
473 js_bindings()->OnError( | 474 js_bindings()->OnError( |
474 -1, ASCIIToUTF16("FindProxyForURL() is undefined.")); | 475 -1, ASCIIToUTF16("FindProxyForURL() is undefined.")); |
475 return ERR_PAC_SCRIPT_FAILED; | 476 return ERR_PAC_SCRIPT_FAILED; |
476 } | 477 } |
477 | 478 |
478 return OK; | 479 return OK; |
479 } | 480 } |
480 | 481 |
481 void PurgeMemory() { | 482 void PurgeMemory() { |
482 v8::Locker locked; | 483 v8::Locker locked(isolate_); |
483 v8::V8::LowMemoryNotification(); | 484 v8::V8::LowMemoryNotification(); |
484 } | 485 } |
485 | 486 |
486 private: | 487 private: |
487 bool GetFindProxyForURL(v8::Local<v8::Value>* function) { | 488 bool GetFindProxyForURL(v8::Local<v8::Value>* function) { |
488 *function = v8_context_->Global()->Get( | 489 *function = v8_context_->Global()->Get( |
489 ASCIILiteralToV8String("FindProxyForURL")); | 490 ASCIILiteralToV8String("FindProxyForURL")); |
490 return (*function)->IsFunction(); | 491 return (*function)->IsFunction(); |
491 } | 492 } |
492 | 493 |
(...skipping 169 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
662 if (!IsStringASCII(ip_address)) | 663 if (!IsStringASCII(ip_address)) |
663 return v8::False(); | 664 return v8::False(); |
664 std::string ip_prefix = V8StringToUTF8(args[1]->ToString()); | 665 std::string ip_prefix = V8StringToUTF8(args[1]->ToString()); |
665 if (!IsStringASCII(ip_prefix)) | 666 if (!IsStringASCII(ip_prefix)) |
666 return v8::False(); | 667 return v8::False(); |
667 return IsInNetEx(ip_address, ip_prefix) ? v8::True() : v8::False(); | 668 return IsInNetEx(ip_address, ip_prefix) ? v8::True() : v8::False(); |
668 } | 669 } |
669 | 670 |
670 mutable base::Lock lock_; | 671 mutable base::Lock lock_; |
671 ProxyResolverV8* parent_; | 672 ProxyResolverV8* parent_; |
| 673 v8::Isolate* isolate_; |
672 v8::Persistent<v8::External> v8_this_; | 674 v8::Persistent<v8::External> v8_this_; |
673 v8::Persistent<v8::Context> v8_context_; | 675 v8::Persistent<v8::Context> v8_context_; |
674 }; | 676 }; |
675 | 677 |
676 // ProxyResolverV8 ------------------------------------------------------------ | 678 // ProxyResolverV8 ------------------------------------------------------------ |
677 | 679 |
678 ProxyResolverV8::ProxyResolverV8() | 680 ProxyResolverV8::ProxyResolverV8() |
679 : ProxyResolver(true /*expects_pac_bytes*/), | 681 : ProxyResolver(true /*expects_pac_bytes*/), |
680 js_bindings_(NULL) { | 682 js_bindings_(NULL) { |
681 } | 683 } |
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
722 const scoped_refptr<ProxyResolverScriptData>& script_data, | 724 const scoped_refptr<ProxyResolverScriptData>& script_data, |
723 const CompletionCallback& /*callback*/) { | 725 const CompletionCallback& /*callback*/) { |
724 DCHECK(script_data); | 726 DCHECK(script_data); |
725 DCHECK(js_bindings_); | 727 DCHECK(js_bindings_); |
726 | 728 |
727 context_.reset(); | 729 context_.reset(); |
728 if (script_data->utf16().empty()) | 730 if (script_data->utf16().empty()) |
729 return ERR_PAC_SCRIPT_FAILED; | 731 return ERR_PAC_SCRIPT_FAILED; |
730 | 732 |
731 // Try parsing the PAC script. | 733 // Try parsing the PAC script. |
732 scoped_ptr<Context> context(new Context(this)); | 734 scoped_ptr<Context> context(new Context(this, GetDefaultIsolate())); |
733 int rv = context->InitV8(script_data); | 735 int rv = context->InitV8(script_data); |
734 if (rv == OK) | 736 if (rv == OK) |
735 context_.reset(context.release()); | 737 context_.reset(context.release()); |
736 return rv; | 738 return rv; |
737 } | 739 } |
738 | 740 |
| 741 // static |
| 742 void ProxyResolverV8::RememberDefaultIsolate() { |
| 743 v8::Isolate* isolate = v8::Isolate::GetCurrent(); |
| 744 DCHECK(isolate) |
| 745 << "ProxyResolverV8::RememberDefaultIsolate called on wrong thread"; |
| 746 DCHECK(g_default_isolate_ == NULL || g_default_isolate_ == isolate) |
| 747 << "Default Isolate can not be changed"; |
| 748 g_default_isolate_ = isolate; |
| 749 } |
| 750 |
| 751 // static |
| 752 v8::Isolate* ProxyResolverV8::GetDefaultIsolate() { |
| 753 DCHECK(g_default_isolate_) |
| 754 << "Must call ProxyResolverV8::RememberDefaultIsolate() first"; |
| 755 return g_default_isolate_; |
| 756 } |
| 757 |
| 758 v8::Isolate* ProxyResolverV8::g_default_isolate_ = NULL; |
| 759 |
739 } // namespace net | 760 } // namespace net |
OLD | NEW |