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; | |
eroman
2014/04/08 19:47:24
Is this log addition intentional? Wander if it cou
jochen (gone - plz use gerrit)
2014/04/08 20:43:40
oops, that's left over from debugging.
| |
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::EnsureIsolateCreated() { |
768 v8::Isolate* isolate = v8::Isolate::GetCurrent(); | 771 if (g_proxy_resolver_isolate_) |
769 DCHECK(isolate) | 772 return; |
770 << "ProxyResolverV8::RememberDefaultIsolate called on wrong thread"; | 773 g_proxy_resolver_isolate_ = |
771 DCHECK(g_default_isolate_ == NULL || g_default_isolate_ == isolate) | 774 new gin::IsolateHolder(gin::IsolateHolder::kNonStrictMode); |
772 << "Default Isolate can not be changed"; | 775 ANNOTATE_LEAKING_OBJECT_PTR(g_proxy_resolver_isolate_); |
773 g_default_isolate_ = isolate; | |
774 } | 776 } |
775 | 777 |
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 | 778 // static |
791 v8::Isolate* ProxyResolverV8::GetDefaultIsolate() { | 779 v8::Isolate* ProxyResolverV8::GetDefaultIsolate() { |
792 DCHECK(g_default_isolate_) | 780 DCHECK(g_proxy_resolver_isolate_) |
793 << "Must call ProxyResolverV8::RememberDefaultIsolate() first"; | 781 << "Must call ProxyResolverV8::EnsureIsolateCreated() first"; |
794 return g_default_isolate_; | 782 return g_proxy_resolver_isolate_->isolate(); |
795 } | 783 } |
796 | 784 |
797 v8::Isolate* ProxyResolverV8::g_default_isolate_ = NULL; | 785 gin::IsolateHolder* ProxyResolverV8::g_proxy_resolver_isolate_ = NULL; |
798 | 786 |
799 // static | 787 // static |
800 size_t ProxyResolverV8::GetTotalHeapSize() { | 788 size_t ProxyResolverV8::GetTotalHeapSize() { |
801 if (!g_default_isolate_) | 789 if (!g_proxy_resolver_isolate_) |
802 return 0; | 790 return 0; |
803 | 791 |
804 v8::Locker locked(g_default_isolate_); | 792 v8::Locker locked(g_proxy_resolver_isolate_->isolate()); |
805 v8::Isolate::Scope isolate_scope(g_default_isolate_); | 793 v8::Isolate::Scope isolate_scope(g_proxy_resolver_isolate_->isolate()); |
806 v8::HeapStatistics heap_statistics; | 794 v8::HeapStatistics heap_statistics; |
807 g_default_isolate_->GetHeapStatistics(&heap_statistics); | 795 g_proxy_resolver_isolate_->isolate()->GetHeapStatistics(&heap_statistics); |
808 return heap_statistics.total_heap_size(); | 796 return heap_statistics.total_heap_size(); |
809 } | 797 } |
810 | 798 |
811 // static | 799 // static |
812 size_t ProxyResolverV8::GetUsedHeapSize() { | 800 size_t ProxyResolverV8::GetUsedHeapSize() { |
813 if (!g_default_isolate_) | 801 if (!g_proxy_resolver_isolate_) |
814 return 0; | 802 return 0; |
815 | 803 |
816 v8::Locker locked(g_default_isolate_); | 804 v8::Locker locked(g_proxy_resolver_isolate_->isolate()); |
817 v8::Isolate::Scope isolate_scope(g_default_isolate_); | 805 v8::Isolate::Scope isolate_scope(g_proxy_resolver_isolate_->isolate()); |
818 v8::HeapStatistics heap_statistics; | 806 v8::HeapStatistics heap_statistics; |
819 g_default_isolate_->GetHeapStatistics(&heap_statistics); | 807 g_proxy_resolver_isolate_->isolate()->GetHeapStatistics(&heap_statistics); |
820 return heap_statistics.used_heap_size(); | 808 return heap_statistics.used_heap_size(); |
821 } | 809 } |
822 | 810 |
823 } // namespace net | 811 } // namespace net |
OLD | NEW |