| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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 "gin/public/isolate_holder.h" | 5 #include "gin/public/isolate_holder.h" |
| 6 | 6 |
| 7 #include <stdlib.h> | 7 #include <stdlib.h> |
| 8 #include <string.h> | 8 #include <string.h> |
| 9 | 9 |
| 10 #include "base/logging.h" | 10 #include "base/logging.h" |
| 11 #include "base/message_loop/message_loop.h" |
| 11 #include "base/rand_util.h" | 12 #include "base/rand_util.h" |
| 12 #include "base/sys_info.h" | 13 #include "base/sys_info.h" |
| 13 #include "gin/array_buffer.h" | 14 #include "gin/array_buffer.h" |
| 14 #include "gin/debug_impl.h" | 15 #include "gin/debug_impl.h" |
| 15 #include "gin/function_template.h" | 16 #include "gin/function_template.h" |
| 16 #include "gin/per_isolate_data.h" | 17 #include "gin/per_isolate_data.h" |
| 17 #include "gin/public/v8_platform.h" | 18 #include "gin/public/v8_platform.h" |
| 19 #include "gin/run_microtasks_observer.h" |
| 18 | 20 |
| 19 namespace gin { | 21 namespace gin { |
| 20 | 22 |
| 21 namespace { | 23 namespace { |
| 22 | 24 |
| 23 v8::ArrayBuffer::Allocator* g_array_buffer_allocator = NULL; | 25 v8::ArrayBuffer::Allocator* g_array_buffer_allocator = NULL; |
| 24 | 26 |
| 25 bool GenerateEntropy(unsigned char* buffer, size_t amount) { | 27 bool GenerateEntropy(unsigned char* buffer, size_t amount) { |
| 26 base::RandBytes(buffer, amount); | 28 base::RandBytes(buffer, amount); |
| 27 return true; | 29 return true; |
| 28 } | 30 } |
| 29 | 31 |
| 30 } // namespace | 32 } // namespace |
| 31 | 33 |
| 32 IsolateHolder::IsolateHolder() { | 34 IsolateHolder::IsolateHolder() { |
| 33 CHECK(g_array_buffer_allocator) | 35 CHECK(g_array_buffer_allocator) |
| 34 << "You need to invoke gin::IsolateHolder::Initialize first"; | 36 << "You need to invoke gin::IsolateHolder::Initialize first"; |
| 35 v8::Isolate::CreateParams params; | 37 v8::Isolate::CreateParams params; |
| 36 params.entry_hook = DebugImpl::GetFunctionEntryHook(); | 38 params.entry_hook = DebugImpl::GetFunctionEntryHook(); |
| 37 params.code_event_handler = DebugImpl::GetJitCodeEventHandler(); | 39 params.code_event_handler = DebugImpl::GetJitCodeEventHandler(); |
| 38 params.constraints.ConfigureDefaults(base::SysInfo::AmountOfPhysicalMemory(), | 40 params.constraints.ConfigureDefaults(base::SysInfo::AmountOfPhysicalMemory(), |
| 39 base::SysInfo::AmountOfVirtualMemory(), | 41 base::SysInfo::AmountOfVirtualMemory(), |
| 40 base::SysInfo::NumberOfProcessors()); | 42 base::SysInfo::NumberOfProcessors()); |
| 41 isolate_ = v8::Isolate::New(params); | 43 isolate_ = v8::Isolate::New(params); |
| 42 isolate_data_.reset(new PerIsolateData(isolate_, g_array_buffer_allocator)); | 44 isolate_data_.reset(new PerIsolateData(isolate_, g_array_buffer_allocator)); |
| 43 } | 45 } |
| 44 | 46 |
| 45 IsolateHolder::~IsolateHolder() { | 47 IsolateHolder::~IsolateHolder() { |
| 48 if (task_observer_.get()) |
| 49 base::MessageLoop::current()->RemoveTaskObserver(task_observer_.get()); |
| 46 isolate_data_.reset(); | 50 isolate_data_.reset(); |
| 47 isolate_->Dispose(); | 51 isolate_->Dispose(); |
| 48 } | 52 } |
| 49 | 53 |
| 50 // static | 54 // static |
| 51 void IsolateHolder::Initialize(ScriptMode mode, | 55 void IsolateHolder::Initialize(ScriptMode mode, |
| 52 v8::ArrayBuffer::Allocator* allocator) { | 56 v8::ArrayBuffer::Allocator* allocator) { |
| 53 CHECK(allocator); | 57 CHECK(allocator); |
| 54 static bool v8_is_initialized = false; | 58 static bool v8_is_initialized = false; |
| 55 if (v8_is_initialized) | 59 if (v8_is_initialized) |
| 56 return; | 60 return; |
| 57 v8::V8::InitializePlatform(V8Platform::Get()); | 61 v8::V8::InitializePlatform(V8Platform::Get()); |
| 58 v8::V8::SetArrayBufferAllocator(allocator); | 62 v8::V8::SetArrayBufferAllocator(allocator); |
| 59 g_array_buffer_allocator = allocator; | 63 g_array_buffer_allocator = allocator; |
| 60 if (mode == gin::IsolateHolder::kStrictMode) { | 64 if (mode == gin::IsolateHolder::kStrictMode) { |
| 61 static const char v8_flags[] = "--use_strict"; | 65 static const char v8_flags[] = "--use_strict"; |
| 62 v8::V8::SetFlagsFromString(v8_flags, sizeof(v8_flags) - 1); | 66 v8::V8::SetFlagsFromString(v8_flags, sizeof(v8_flags) - 1); |
| 63 } | 67 } |
| 64 v8::V8::SetEntropySource(&GenerateEntropy); | 68 v8::V8::SetEntropySource(&GenerateEntropy); |
| 65 v8::V8::Initialize(); | 69 v8::V8::Initialize(); |
| 66 v8_is_initialized = true; | 70 v8_is_initialized = true; |
| 67 } | 71 } |
| 68 | 72 |
| 73 void IsolateHolder::AddRunMicrotasksObserver() { |
| 74 DCHECK(!task_observer_.get()); |
| 75 task_observer_.reset(new RunMicrotasksObserver(isolate_));; |
| 76 base::MessageLoop::current()->AddTaskObserver(task_observer_.get()); |
| 77 } |
| 78 |
| 79 void IsolateHolder::RemoveRunMicrotasksObserver() { |
| 80 DCHECK(task_observer_.get()); |
| 81 base::MessageLoop::current()->RemoveTaskObserver(task_observer_.get()); |
| 82 task_observer_.reset(); |
| 83 } |
| 84 |
| 69 } // namespace gin | 85 } // namespace gin |
| OLD | NEW |