| 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/rand_util.h" | 11 #include "base/rand_util.h" |
| 12 #include "base/sys_info.h" | 12 #include "base/sys_info.h" |
| 13 #include "gin/array_buffer.h" | 13 #include "gin/array_buffer.h" |
| 14 #include "gin/function_template.h" | 14 #include "gin/function_template.h" |
| 15 #include "gin/per_isolate_data.h" | 15 #include "gin/per_isolate_data.h" |
| 16 #include "gin/public/v8_platform.h" | 16 #include "gin/public/v8_platform.h" |
| 17 | 17 |
| 18 namespace gin { | 18 namespace gin { |
| 19 | 19 |
| 20 namespace { | 20 namespace { |
| 21 v8::ArrayBuffer::Allocator* g_array_buffer_allocator = NULL; |
| 21 | 22 |
| 22 bool GenerateEntropy(unsigned char* buffer, size_t amount) { | 23 bool GenerateEntropy(unsigned char* buffer, size_t amount) { |
| 23 base::RandBytes(buffer, amount); | 24 base::RandBytes(buffer, amount); |
| 24 return true; | 25 return true; |
| 25 } | 26 } |
| 26 | 27 |
| 27 void EnsureV8Initialized(bool gin_managed) { | 28 void EnsureV8Initialized(bool gin_managed) { |
| 28 static bool v8_is_initialized = false; | 29 static bool v8_is_initialized = false; |
| 29 static bool v8_is_gin_managed = false; | 30 static bool v8_is_gin_managed = false; |
| 30 if (v8_is_initialized) { | 31 if (v8_is_initialized) { |
| 31 CHECK_EQ(v8_is_gin_managed, gin_managed); | 32 CHECK_EQ(v8_is_gin_managed, gin_managed); |
| 32 return; | 33 return; |
| 33 } | 34 } |
| 34 v8_is_initialized = true; | 35 v8_is_initialized = true; |
| 35 v8_is_gin_managed = gin_managed; | 36 v8_is_gin_managed = gin_managed; |
| 36 } | 37 } |
| 37 | 38 |
| 38 } // namespace | 39 } // namespace |
| 39 | 40 |
| 40 IsolateHolder::IsolateHolder() | 41 IsolateHolder::IsolateHolder() |
| 41 : isolate_owner_(true) { | 42 : isolate_owner_(true) { |
| 42 EnsureV8Initialized(true); | 43 EnsureV8Initialized(true); |
| 43 isolate_ = v8::Isolate::New(); | 44 isolate_ = v8::Isolate::New(); |
| 44 v8::ResourceConstraints constraints; | 45 v8::ResourceConstraints constraints; |
| 45 constraints.ConfigureDefaults(base::SysInfo::AmountOfPhysicalMemory(), | 46 constraints.ConfigureDefaults(base::SysInfo::AmountOfPhysicalMemory(), |
| 46 base::SysInfo::AmountOfVirtualMemory(), | 47 base::SysInfo::AmountOfVirtualMemory(), |
| 47 base::SysInfo::NumberOfProcessors()); | 48 base::SysInfo::NumberOfProcessors()); |
| 48 v8::SetResourceConstraints(isolate_, &constraints); | 49 v8::SetResourceConstraints(isolate_, &constraints); |
| 49 Init(ArrayBufferAllocator::SharedInstance()); | 50 Init(g_array_buffer_allocator); |
| 50 } | 51 } |
| 51 | 52 |
| 52 IsolateHolder::IsolateHolder(v8::Isolate* isolate, | 53 IsolateHolder::IsolateHolder(v8::Isolate* isolate, |
| 53 v8::ArrayBuffer::Allocator* allocator) | 54 v8::ArrayBuffer::Allocator* allocator) |
| 54 : isolate_owner_(false), isolate_(isolate) { | 55 : isolate_owner_(false), isolate_(isolate) { |
| 55 EnsureV8Initialized(false); | 56 EnsureV8Initialized(false); |
| 56 Init(allocator); | 57 Init(allocator); |
| 57 } | 58 } |
| 58 | 59 |
| 59 IsolateHolder::~IsolateHolder() { | 60 IsolateHolder::~IsolateHolder() { |
| 60 isolate_data_.reset(); | 61 isolate_data_.reset(); |
| 61 if (isolate_owner_) | 62 if (isolate_owner_) |
| 62 isolate_->Dispose(); | 63 isolate_->Dispose(); |
| 63 } | 64 } |
| 64 | 65 |
| 65 // static | 66 // static |
| 66 void IsolateHolder::Initialize(ScriptMode mode, | 67 void IsolateHolder::Initialize(ScriptMode mode, |
| 67 v8::ArrayBuffer::Allocator* allocator) { | 68 v8::ArrayBuffer::Allocator* allocator) { |
| 68 static bool v8_is_initialized = false; | 69 static bool v8_is_initialized = false; |
| 69 if (v8_is_initialized) | 70 if (v8_is_initialized) |
| 70 return; | 71 return; |
| 71 v8::V8::InitializePlatform(V8Platform::Get()); | 72 v8::V8::InitializePlatform(V8Platform::Get()); |
| 72 v8::V8::SetArrayBufferAllocator(allocator); | 73 v8::V8::SetArrayBufferAllocator(allocator); |
| 74 g_array_buffer_allocator = allocator; |
| 73 if (mode == gin::IsolateHolder::kStrictMode) { | 75 if (mode == gin::IsolateHolder::kStrictMode) { |
| 74 static const char v8_flags[] = "--use_strict"; | 76 static const char v8_flags[] = "--use_strict"; |
| 75 v8::V8::SetFlagsFromString(v8_flags, sizeof(v8_flags) - 1); | 77 v8::V8::SetFlagsFromString(v8_flags, sizeof(v8_flags) - 1); |
| 76 } | 78 } |
| 77 v8::V8::SetEntropySource(&GenerateEntropy); | 79 v8::V8::SetEntropySource(&GenerateEntropy); |
| 78 v8::V8::Initialize(); | 80 v8::V8::Initialize(); |
| 79 v8_is_initialized = true; | 81 v8_is_initialized = true; |
| 80 } | 82 } |
| 81 | 83 |
| 82 void IsolateHolder::Init(v8::ArrayBuffer::Allocator* allocator) { | 84 void IsolateHolder::Init(v8::ArrayBuffer::Allocator* allocator) { |
| 83 v8::Isolate::Scope isolate_scope(isolate_); | 85 v8::Isolate::Scope isolate_scope(isolate_); |
| 84 v8::HandleScope handle_scope(isolate_); | 86 v8::HandleScope handle_scope(isolate_); |
| 85 isolate_data_.reset(new PerIsolateData(isolate_, allocator)); | 87 isolate_data_.reset(new PerIsolateData(isolate_, allocator)); |
| 86 } | 88 } |
| 87 | 89 |
| 88 } // namespace gin | 90 } // namespace gin |
| OLD | NEW |