1 // Copyright 2013 The Chromium Authors
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_PER_ISOLATE_DATA_H_
6 #define GIN_PER_ISOLATE_DATA_H_
11 #include "base/memory/raw_ptr.h"
12 #include "base/memory/ref_counted.h"
13 #include "base/task/single_thread_task_runner.h"
14 #include "gin/gin_export.h"
15 #include "gin/public/isolate_holder.h"
16 #include "gin/public/wrapper_info.h"
17 #include "gin/v8_foreground_task_runner_base.h"
18 #include "v8/include/v8-array-buffer.h"
19 #include "v8/include/v8-forward.h"
23 class V8IdleTaskRunner;
24 class IndexedPropertyInterceptor;
25 class NamedPropertyInterceptor;
28 // There is one instance of PerIsolateData per v8::Isolate managed by Gin. This
29 // class stores all the Gin-related data that varies per isolate.
30 class GIN_EXPORT PerIsolateData {
34 v8::ArrayBuffer::Allocator* allocator,
35 IsolateHolder::AccessMode access_mode,
36 scoped_refptr<base::SingleThreadTaskRunner> task_runner,
37 scoped_refptr<base::SingleThreadTaskRunner> low_priority_task_runner);
38 PerIsolateData(const PerIsolateData&) = delete;
39 PerIsolateData& operator=(const PerIsolateData&) = delete;
42 static PerIsolateData* From(v8::Isolate* isolate);
44 // Each isolate is associated with a collection of v8::ObjectTemplates and
45 // v8::FunctionTemplates. Typically these template objects are created
47 void SetObjectTemplate(WrapperInfo* info,
48 v8::Local<v8::ObjectTemplate> object_template);
49 void SetFunctionTemplate(WrapperInfo* info,
50 v8::Local<v8::FunctionTemplate> function_template);
52 // These are low-level functions for retrieving object or function templates
53 // stored in this object. Because these templates are often created lazily,
54 // most clients should call higher-level functions that know how to populate
55 // these templates if they haven't already been created.
56 v8::Local<v8::ObjectTemplate> GetObjectTemplate(WrapperInfo* info);
57 v8::Local<v8::FunctionTemplate> GetFunctionTemplate(WrapperInfo* info);
59 // We maintain a map from Wrappable objects that derive from one of the
60 // interceptor interfaces to the interceptor interface pointers.
61 void SetIndexedPropertyInterceptor(WrappableBase* base,
62 IndexedPropertyInterceptor* interceptor);
63 void SetNamedPropertyInterceptor(WrappableBase* base,
64 NamedPropertyInterceptor* interceptor);
66 void ClearIndexedPropertyInterceptor(WrappableBase* base,
67 IndexedPropertyInterceptor* interceptor);
68 void ClearNamedPropertyInterceptor(WrappableBase* base,
69 NamedPropertyInterceptor* interceptor);
71 IndexedPropertyInterceptor* GetIndexedPropertyInterceptor(
73 NamedPropertyInterceptor* GetNamedPropertyInterceptor(WrappableBase* base);
75 void EnableIdleTasks(std::unique_ptr<V8IdleTaskRunner> idle_task_runner);
77 v8::Isolate* isolate() { return isolate_; }
78 v8::ArrayBuffer::Allocator* allocator() { return allocator_; }
79 std::shared_ptr<v8::TaskRunner> task_runner() { return task_runner_; }
80 std::shared_ptr<v8::TaskRunner> low_priority_task_runner() {
81 return low_priority_task_runner_;
86 WrapperInfo*, v8::Eternal<v8::ObjectTemplate> > ObjectTemplateMap;
88 WrapperInfo*, v8::Eternal<v8::FunctionTemplate> > FunctionTemplateMap;
89 typedef std::map<WrappableBase*, IndexedPropertyInterceptor*>
90 IndexedPropertyInterceptorMap;
91 typedef std::map<WrappableBase*, NamedPropertyInterceptor*>
92 NamedPropertyInterceptorMap;
94 // PerIsolateData doesn't actually own |isolate_|. Instead, the isolate is
95 // owned by the IsolateHolder, which also owns the PerIsolateData.
96 raw_ptr<v8::Isolate, AcrossTasksDanglingUntriaged> isolate_;
97 raw_ptr<v8::ArrayBuffer::Allocator, DanglingUntriaged> allocator_;
98 ObjectTemplateMap object_templates_;
99 FunctionTemplateMap function_templates_;
100 IndexedPropertyInterceptorMap indexed_interceptors_;
101 NamedPropertyInterceptorMap named_interceptors_;
102 std::shared_ptr<V8ForegroundTaskRunnerBase> task_runner_;
103 std::shared_ptr<V8ForegroundTaskRunnerBase> low_priority_task_runner_;
108 #endif // GIN_PER_ISOLATE_DATA_H_