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