blob: eddf82a55f26d0e43c7bda80c5a1388af2f5ff2f [file] [log] [blame]
Avi Drissman468e51b62022-09-13 20:47:011// Copyright 2013 The Chromium Authors
[email protected]a22998a2013-11-10 05:00:502// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#ifndef GIN_PER_ISOLATE_DATA_H_
6#define GIN_PER_ISOLATE_DATA_H_
7
8#include <map>
mostynbc862da82016-04-03 15:54:339#include <memory>
[email protected]a22998a2013-11-10 05:00:5010
Keishi Hattori0e45c022021-11-27 09:25:5211#include "base/memory/raw_ptr.h"
[email protected]b64e5212014-04-04 21:09:1612#include "base/memory/ref_counted.h"
Patrick Monette643cdf62021-10-15 19:13:4213#include "base/task/single_thread_task_runner.h"
[email protected]48c21632013-12-12 21:32:3414#include "gin/gin_export.h"
jochen76acff102016-11-08 08:20:3715#include "gin/public/isolate_holder.h"
[email protected]c07006b2013-11-20 03:01:4416#include "gin/public/wrapper_info.h"
Andreas Haasc13cae82017-11-16 12:54:3817#include "gin/v8_foreground_task_runner_base.h"
Dan Elphick05acd602021-08-30 15:22:0718#include "v8/include/v8-array-buffer.h"
19#include "v8/include/v8-forward.h"
[email protected]a22998a2013-11-10 05:00:5020
21namespace gin {
22
Andreas Haasc13cae82017-11-16 12:54:3823class V8IdleTaskRunner;
[email protected]5c969b82014-03-12 04:59:0524class IndexedPropertyInterceptor;
25class NamedPropertyInterceptor;
26class WrappableBase;
27
[email protected]60531d52013-11-27 02:10:1528// 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.
[email protected]48c21632013-12-12 21:32:3430class GIN_EXPORT PerIsolateData {
[email protected]a22998a2013-11-10 05:00:5031 public:
jochen76acff102016-11-08 08:20:3732 PerIsolateData(v8::Isolate* isolate,
33 v8::ArrayBuffer::Allocator* allocator,
altimin124814c2017-01-03 14:06:5434 IsolateHolder::AccessMode access_mode,
35 scoped_refptr<base::SingleThreadTaskRunner> task_runner);
Daniel Hosseinian68c0798d2021-04-16 08:16:0736 PerIsolateData(const PerIsolateData&) = delete;
37 PerIsolateData& operator=(const PerIsolateData&) = delete;
[email protected]a22998a2013-11-10 05:00:5038 ~PerIsolateData();
39
40 static PerIsolateData* From(v8::Isolate* isolate);
41
[email protected]60531d52013-11-27 02:10:1542 // Each isolate is associated with a collection of v8::ObjectTemplates and
43 // v8::FunctionTemplates. Typically these template objects are created
44 // lazily.
[email protected]e87f3122013-11-12 00:41:2745 void SetObjectTemplate(WrapperInfo* info,
46 v8::Local<v8::ObjectTemplate> object_template);
[email protected]97f21ca2013-11-17 17:46:0747 void SetFunctionTemplate(WrapperInfo* info,
48 v8::Local<v8::FunctionTemplate> function_template);
[email protected]e87f3122013-11-12 00:41:2749
[email protected]60531d52013-11-27 02:10:1550 // These are low-level functions for retrieving object or function templates
51 // stored in this object. Because these templates are often created lazily,
52 // most clients should call higher-level functions that know how to populate
53 // these templates if they haven't already been created.
[email protected]e87f3122013-11-12 00:41:2754 v8::Local<v8::ObjectTemplate> GetObjectTemplate(WrapperInfo* info);
[email protected]97f21ca2013-11-17 17:46:0755 v8::Local<v8::FunctionTemplate> GetFunctionTemplate(WrapperInfo* info);
[email protected]a22998a2013-11-10 05:00:5056
[email protected]5c969b82014-03-12 04:59:0557 // We maintain a map from Wrappable objects that derive from one of the
58 // interceptor interfaces to the interceptor interface pointers.
59 void SetIndexedPropertyInterceptor(WrappableBase* base,
60 IndexedPropertyInterceptor* interceptor);
61 void SetNamedPropertyInterceptor(WrappableBase* base,
62 NamedPropertyInterceptor* interceptor);
63
64 void ClearIndexedPropertyInterceptor(WrappableBase* base,
65 IndexedPropertyInterceptor* interceptor);
66 void ClearNamedPropertyInterceptor(WrappableBase* base,
67 NamedPropertyInterceptor* interceptor);
68
69 IndexedPropertyInterceptor* GetIndexedPropertyInterceptor(
70 WrappableBase* base);
71 NamedPropertyInterceptor* GetNamedPropertyInterceptor(WrappableBase* base);
72
mostynbc862da82016-04-03 15:54:3373 void EnableIdleTasks(std::unique_ptr<V8IdleTaskRunner> idle_task_runner);
ulan3cbdcd02015-07-20 11:32:5874
[email protected]91cd4fe2013-11-28 09:31:5875 v8::Isolate* isolate() { return isolate_; }
[email protected]73dcce92014-02-20 08:24:0476 v8::ArrayBuffer::Allocator* allocator() { return allocator_; }
Andreas Haasc13cae82017-11-16 12:54:3877 std::shared_ptr<v8::TaskRunner> task_runner() { return task_runner_; }
jochen76acff102016-11-08 08:20:3778
[email protected]a22998a2013-11-10 05:00:5079 private:
80 typedef std::map<
81 WrapperInfo*, v8::Eternal<v8::ObjectTemplate> > ObjectTemplateMap;
[email protected]97f21ca2013-11-17 17:46:0782 typedef std::map<
83 WrapperInfo*, v8::Eternal<v8::FunctionTemplate> > FunctionTemplateMap;
[email protected]5c969b82014-03-12 04:59:0584 typedef std::map<WrappableBase*, IndexedPropertyInterceptor*>
85 IndexedPropertyInterceptorMap;
86 typedef std::map<WrappableBase*, NamedPropertyInterceptor*>
87 NamedPropertyInterceptorMap;
[email protected]a22998a2013-11-10 05:00:5088
[email protected]60531d52013-11-27 02:10:1589 // PerIsolateData doesn't actually own |isolate_|. Instead, the isolate is
90 // owned by the IsolateHolder, which also owns the PerIsolateData.
Pâris8b159582022-08-31 04:13:2891 raw_ptr<v8::Isolate, DanglingUntriaged> isolate_;
92 raw_ptr<v8::ArrayBuffer::Allocator, DanglingUntriaged> allocator_;
[email protected]a22998a2013-11-10 05:00:5093 ObjectTemplateMap object_templates_;
[email protected]97f21ca2013-11-17 17:46:0794 FunctionTemplateMap function_templates_;
[email protected]5c969b82014-03-12 04:59:0595 IndexedPropertyInterceptorMap indexed_interceptors_;
96 NamedPropertyInterceptorMap named_interceptors_;
Andreas Haasc13cae82017-11-16 12:54:3897 std::shared_ptr<V8ForegroundTaskRunnerBase> task_runner_;
[email protected]a22998a2013-11-10 05:00:5098};
99
100} // namespace gin
101
102#endif // GIN_PER_ISOLATE_DATA_H_