Chromium Code Reviews| 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" |
| 11 #include "base/compiler_specific.h" | 11 #include "base/compiler_specific.h" |
| 12 #include "base/debug/leak_annotations.h" | |
| 12 #include "base/logging.h" | 13 #include "base/logging.h" |
| 13 #include "base/strings/string_tokenizer.h" | 14 #include "base/strings/string_tokenizer.h" |
| 14 #include "base/strings/string_util.h" | 15 #include "base/strings/string_util.h" |
| 15 #include "base/strings/utf_string_conversions.h" | 16 #include "base/strings/utf_string_conversions.h" |
| 16 #include "base/synchronization/lock.h" | 17 #include "base/synchronization/lock.h" |
| 18 #include "gin/public/isolate_holder.h" | |
| 17 #include "net/base/net_errors.h" | 19 #include "net/base/net_errors.h" |
| 18 #include "net/base/net_log.h" | 20 #include "net/base/net_log.h" |
| 19 #include "net/base/net_util.h" | 21 #include "net/base/net_util.h" |
| 20 #include "net/proxy/proxy_info.h" | 22 #include "net/proxy/proxy_info.h" |
| 21 #include "net/proxy/proxy_resolver_script.h" | 23 #include "net/proxy/proxy_resolver_script.h" |
| 22 #include "url/gurl.h" | 24 #include "url/gurl.h" |
| 23 #include "url/url_canon.h" | 25 #include "url/url_canon.h" |
| 24 #include "v8/include/v8.h" | 26 #include "v8/include/v8.h" |
| 25 | 27 |
| 26 // Notes on the javascript environment: | 28 // Notes on the javascript environment: |
| (...skipping 489 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 516 | 518 |
| 517 // Handle an exception thrown by V8. | 519 // Handle an exception thrown by V8. |
| 518 void HandleError(v8::Handle<v8::Message> message) { | 520 void HandleError(v8::Handle<v8::Message> message) { |
| 519 base::string16 error_message; | 521 base::string16 error_message; |
| 520 int line_number = -1; | 522 int line_number = -1; |
| 521 | 523 |
| 522 if (!message.IsEmpty()) { | 524 if (!message.IsEmpty()) { |
| 523 line_number = message->GetLineNumber(); | 525 line_number = message->GetLineNumber(); |
| 524 V8ObjectToUTF16String(message->Get(), &error_message, isolate_); | 526 V8ObjectToUTF16String(message->Get(), &error_message, isolate_); |
| 525 } | 527 } |
| 528 LOG(ERROR) << line_number << ": " << error_message; | |
| 526 | 529 |
| 527 js_bindings()->OnError(line_number, error_message); | 530 js_bindings()->OnError(line_number, error_message); |
| 528 } | 531 } |
| 529 | 532 |
| 530 // Compiles and runs |script| in the current V8 context. | 533 // Compiles and runs |script| in the current V8 context. |
| 531 // Returns OK on success, otherwise an error code. | 534 // Returns OK on success, otherwise an error code. |
| 532 int RunScript(v8::Handle<v8::String> script, const char* script_name) { | 535 int RunScript(v8::Handle<v8::String> script, const char* script_name) { |
| 533 v8::TryCatch try_catch; | 536 v8::TryCatch try_catch; |
| 534 | 537 |
| 535 // Compile the script. | 538 // Compile the script. |
| (...skipping 221 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 757 | 760 |
| 758 // Try parsing the PAC script. | 761 // Try parsing the PAC script. |
| 759 scoped_ptr<Context> context(new Context(this, GetDefaultIsolate())); | 762 scoped_ptr<Context> context(new Context(this, GetDefaultIsolate())); |
| 760 int rv = context->InitV8(script_data); | 763 int rv = context->InitV8(script_data); |
| 761 if (rv == OK) | 764 if (rv == OK) |
| 762 context_.reset(context.release()); | 765 context_.reset(context.release()); |
| 763 return rv; | 766 return rv; |
| 764 } | 767 } |
| 765 | 768 |
| 766 // static | 769 // static |
| 767 void ProxyResolverV8::RememberDefaultIsolate() { | 770 void ProxyResolverV8::CreateIsolate() { |
| 768 v8::Isolate* isolate = v8::Isolate::GetCurrent(); | 771 DCHECK(g_default_isolate_ == NULL) << "Default Isolate can not be set twice"; |
| 769 DCHECK(isolate) | 772 g_default_isolate_ = |
|
abarth-chromium
2014/04/07 17:13:55
Should we rename this variable? It's now the prox
| |
| 770 << "ProxyResolverV8::RememberDefaultIsolate called on wrong thread"; | 773 new gin::IsolateHolder(gin::IsolateHolder::kNonStrictMode); |
| 771 DCHECK(g_default_isolate_ == NULL || g_default_isolate_ == isolate) | 774 ANNOTATE_LEAKING_OBJECT_PTR(g_default_isolate); |
| 772 << "Default Isolate can not be changed"; | |
| 773 g_default_isolate_ = isolate; | |
| 774 } | 775 } |
| 775 | 776 |
| 776 #if defined(OS_WIN) | |
| 777 // static | |
| 778 void ProxyResolverV8::CreateIsolate() { | |
| 779 v8::Isolate* isolate = v8::Isolate::New(); | |
| 780 DCHECK(isolate); | |
| 781 DCHECK(g_default_isolate_ == NULL) << "Default Isolate can not be set twice"; | |
| 782 | |
| 783 isolate->Enter(); | |
| 784 v8::V8::Initialize(); | |
| 785 | |
| 786 g_default_isolate_ = isolate; | |
| 787 } | |
| 788 #endif // defined(OS_WIN) | |
| 789 | |
| 790 // static | 777 // static |
| 791 v8::Isolate* ProxyResolverV8::GetDefaultIsolate() { | 778 v8::Isolate* ProxyResolverV8::GetDefaultIsolate() { |
| 792 DCHECK(g_default_isolate_) | 779 DCHECK(g_default_isolate_) |
| 793 << "Must call ProxyResolverV8::RememberDefaultIsolate() first"; | 780 << "Must call ProxyResolverV8::CreateIsolate() first"; |
| 794 return g_default_isolate_; | 781 return g_default_isolate_->isolate(); |
| 795 } | 782 } |
| 796 | 783 |
| 797 v8::Isolate* ProxyResolverV8::g_default_isolate_ = NULL; | 784 gin::IsolateHolder* ProxyResolverV8::g_default_isolate_ = NULL; |
| 798 | 785 |
| 799 // static | 786 // static |
| 800 size_t ProxyResolverV8::GetTotalHeapSize() { | 787 size_t ProxyResolverV8::GetTotalHeapSize() { |
| 801 if (!g_default_isolate_) | 788 if (!g_default_isolate_) |
| 802 return 0; | 789 return 0; |
| 803 | 790 |
| 804 v8::Locker locked(g_default_isolate_); | 791 v8::Locker locked(g_default_isolate_->isolate()); |
| 805 v8::Isolate::Scope isolate_scope(g_default_isolate_); | 792 v8::Isolate::Scope isolate_scope(g_default_isolate_->isolate()); |
| 806 v8::HeapStatistics heap_statistics; | 793 v8::HeapStatistics heap_statistics; |
| 807 g_default_isolate_->GetHeapStatistics(&heap_statistics); | 794 g_default_isolate_->isolate()->GetHeapStatistics(&heap_statistics); |
| 808 return heap_statistics.total_heap_size(); | 795 return heap_statistics.total_heap_size(); |
| 809 } | 796 } |
| 810 | 797 |
| 811 // static | 798 // static |
| 812 size_t ProxyResolverV8::GetUsedHeapSize() { | 799 size_t ProxyResolverV8::GetUsedHeapSize() { |
| 813 if (!g_default_isolate_) | 800 if (!g_default_isolate_) |
| 814 return 0; | 801 return 0; |
| 815 | 802 |
| 816 v8::Locker locked(g_default_isolate_); | 803 v8::Locker locked(g_default_isolate_->isolate()); |
| 817 v8::Isolate::Scope isolate_scope(g_default_isolate_); | 804 v8::Isolate::Scope isolate_scope(g_default_isolate_->isolate()); |
| 818 v8::HeapStatistics heap_statistics; | 805 v8::HeapStatistics heap_statistics; |
| 819 g_default_isolate_->GetHeapStatistics(&heap_statistics); | 806 g_default_isolate_->isolate()->GetHeapStatistics(&heap_statistics); |
| 820 return heap_statistics.used_heap_size(); | 807 return heap_statistics.used_heap_size(); |
| 821 } | 808 } |
| 822 | 809 |
| 823 } // namespace net | 810 } // namespace net |
| OLD | NEW |