Video Player: Keep the display awake during playing a video
[chromium-blink-merge.git] / gin / isolate_holder.cc
blobd0ebbb84bf274055c474b7f9d077ccc6a7d5e947
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.
5 #include "gin/public/isolate_holder.h"
7 #include <stdlib.h>
8 #include <string.h>
10 #include "base/logging.h"
11 #include "base/rand_util.h"
12 #include "base/sys_info.h"
13 #include "gin/array_buffer.h"
14 #include "gin/function_template.h"
15 #include "gin/per_isolate_data.h"
16 #include "gin/public/v8_platform.h"
18 namespace gin {
20 namespace {
22 bool GenerateEntropy(unsigned char* buffer, size_t amount) {
23 base::RandBytes(buffer, amount);
24 return true;
27 void EnsureV8Initialized(gin::IsolateHolder::ScriptMode mode,
28 bool gin_managed) {
29 static bool v8_is_initialized = false;
30 static bool v8_is_gin_managed = false;
31 if (v8_is_initialized) {
32 CHECK_EQ(v8_is_gin_managed, gin_managed);
33 return;
35 v8_is_initialized = true;
36 v8_is_gin_managed = gin_managed;
37 if (!gin_managed)
38 return;
40 v8::V8::InitializePlatform(V8Platform::Get());
41 v8::V8::SetArrayBufferAllocator(ArrayBufferAllocator::SharedInstance());
42 if (mode == gin::IsolateHolder::kStrictMode) {
43 static const char v8_flags[] = "--use_strict";
44 v8::V8::SetFlagsFromString(v8_flags, sizeof(v8_flags) - 1);
46 v8::V8::SetEntropySource(&GenerateEntropy);
47 v8::V8::Initialize();
50 } // namespace
52 IsolateHolder::IsolateHolder(ScriptMode mode)
53 : isolate_owner_(true) {
54 EnsureV8Initialized(mode, true);
55 isolate_ = v8::Isolate::New();
56 v8::ResourceConstraints constraints;
57 constraints.ConfigureDefaults(base::SysInfo::AmountOfPhysicalMemory(),
58 base::SysInfo::AmountOfVirtualMemory(),
59 base::SysInfo::NumberOfProcessors());
60 v8::SetResourceConstraints(isolate_, &constraints);
61 Init(ArrayBufferAllocator::SharedInstance());
64 IsolateHolder::IsolateHolder(v8::Isolate* isolate,
65 v8::ArrayBuffer::Allocator* allocator)
66 : isolate_owner_(false), isolate_(isolate) {
67 EnsureV8Initialized(kNonStrictMode, false);
68 Init(allocator);
71 IsolateHolder::~IsolateHolder() {
72 isolate_data_.reset();
73 if (isolate_owner_)
74 isolate_->Dispose();
77 void IsolateHolder::Init(v8::ArrayBuffer::Allocator* allocator) {
78 v8::Isolate::Scope isolate_scope(isolate_);
79 v8::HandleScope handle_scope(isolate_);
80 isolate_data_.reset(new PerIsolateData(isolate_, allocator));
83 } // namespace gin