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 #ifndef GIN_PUBLIC_ISOLATE_HOLDER_H_
6 #define GIN_PUBLIC_ISOLATE_HOLDER_H_
8 #include "base/basictypes.h"
9 #include "base/memory/scoped_ptr.h"
10 #include "gin/gin_export.h"
11 #include "v8/include/v8.h"
16 class RunMicrotasksObserver
;
17 class V8IsolateMemoryDumpProvider
;
19 // To embed Gin, first initialize gin using IsolateHolder::Initialize and then
20 // create an instance of IsolateHolder to hold the v8::Isolate in which you
21 // will execute JavaScript. You might wish to subclass IsolateHolder if you
22 // want to tie more state to the lifetime of the isolate.
23 class GIN_EXPORT IsolateHolder
{
25 // Controls whether or not V8 should only accept strict mode scripts.
31 // Stores whether the client uses v8::Locker to access the isolate.
38 explicit IsolateHolder(AccessMode access_mode
);
41 // Should be invoked once before creating IsolateHolder instances to
42 // initialize V8 and Gin. In case V8_USE_EXTERNAL_STARTUP_DATA is
43 // defined, V8's initial natives should be loaded (by calling
44 // V8Initializer::LoadV8NativesFromFD or
45 // V8Initializer::LoadV8Natives) before calling this method. If the
46 // snapshot file is available, it should also be loaded (by calling
47 // V8Initializer::LoadV8SnapshotFromFD or
48 // V8Initializer::LoadV8Snapshot) before calling this method.
49 static void Initialize(ScriptMode mode
,
50 v8::ArrayBuffer::Allocator
* allocator
);
52 v8::Isolate
* isolate() { return isolate_
; }
54 // The implementations of Object.observe() and Promise enqueue v8 Microtasks
55 // that should be executed just before control is returned to the message
56 // loop. This method adds a MessageLoop TaskObserver which runs any pending
57 // Microtasks each time a Task is completed. This method should be called
58 // once, when a MessageLoop is created and it should be called on the
59 // MessageLoop's thread.
60 void AddRunMicrotasksObserver();
62 // This method should also only be called once, and on the MessageLoop's
64 void RemoveRunMicrotasksObserver();
66 // This method returns if v8::Locker is needed to access isolate.
67 AccessMode
access_mode() const { return access_mode_
; }
69 // This method returns V8IsolateMemoryDumpProvider of this isolate, used for
71 V8IsolateMemoryDumpProvider
* isolate_memory_dump_provider_for_testing()
73 return isolate_memory_dump_provider_
.get();
77 v8::Isolate
* isolate_
;
78 scoped_ptr
<PerIsolateData
> isolate_data_
;
79 scoped_ptr
<RunMicrotasksObserver
> task_observer_
;
80 scoped_ptr
<V8IsolateMemoryDumpProvider
> isolate_memory_dump_provider_
;
81 AccessMode access_mode_
;
83 DISALLOW_COPY_AND_ASSIGN(IsolateHolder
);
88 #endif // GIN_PUBLIC_ISOLATE_HOLDER_H_