blob: 82048c08a14d05e1f9bd6ad6cc16d16f0905f879 [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
Ashley Newsondeda1812024-01-09 12:04:1811#include "base/check.h"
Keishi Hattori0e45c022021-11-27 09:25:5212#include "base/memory/raw_ptr.h"
[email protected]b64e5212014-04-04 21:09:1613#include "base/memory/ref_counted.h"
Ashley Newsondeda1812024-01-09 12:04:1814#include "base/observer_list.h"
Patrick Monette643cdf62021-10-15 19:13:4215#include "base/task/single_thread_task_runner.h"
[email protected]48c21632013-12-12 21:32:3416#include "gin/gin_export.h"
jochen76acff102016-11-08 08:20:3717#include "gin/public/isolate_holder.h"
[email protected]c07006b2013-11-20 03:01:4418#include "gin/public/wrapper_info.h"
Andreas Haasc13cae82017-11-16 12:54:3819#include "gin/v8_foreground_task_runner_base.h"
Dan Elphick05acd602021-08-30 15:22:0720#include "v8/include/v8-array-buffer.h"
21#include "v8/include/v8-forward.h"
[email protected]a22998a2013-11-10 05:00:5022
23namespace gin {
24
Andreas Haasc13cae82017-11-16 12:54:3825class V8IdleTaskRunner;
[email protected]5c969b82014-03-12 04:59:0526
[email protected]60531d52013-11-27 02:10:1527// There is one instance of PerIsolateData per v8::Isolate managed by Gin. This
28// class stores all the Gin-related data that varies per isolate.
[email protected]48c21632013-12-12 21:32:3429class GIN_EXPORT PerIsolateData {
[email protected]a22998a2013-11-10 05:00:5030 public:
Ashley Newsondeda1812024-01-09 12:04:1831 class DisposeObserver : public base::CheckedObserver {
32 public:
33 // Called just before the isolate is about to be disposed. The isolate will
34 // be entered before the observer is notified, but there will not be a
35 // handle scope by default.
36 virtual void OnBeforeDispose(v8::Isolate* isolate) = 0;
37 // Called just after the isolate has been disposed.
38 virtual void OnDisposed() = 0;
39 };
40
Jiahe Zhange97ba772023-07-27 02:46:4141 PerIsolateData(
42 v8::Isolate* isolate,
43 v8::ArrayBuffer::Allocator* allocator,
44 IsolateHolder::AccessMode access_mode,
45 scoped_refptr<base::SingleThreadTaskRunner> task_runner,
Etienne Pierre-doray8e42980e2024-09-04 20:26:2746 scoped_refptr<base::SingleThreadTaskRunner> user_visible_task_runner,
47 scoped_refptr<base::SingleThreadTaskRunner> best_effort_task_runner);
Daniel Hosseinian68c0798d2021-04-16 08:16:0748 PerIsolateData(const PerIsolateData&) = delete;
49 PerIsolateData& operator=(const PerIsolateData&) = delete;
[email protected]a22998a2013-11-10 05:00:5050 ~PerIsolateData();
51
52 static PerIsolateData* From(v8::Isolate* isolate);
53
[email protected]60531d52013-11-27 02:10:1554 // Each isolate is associated with a collection of v8::ObjectTemplates and
55 // v8::FunctionTemplates. Typically these template objects are created
56 // lazily.
Andreas Haas3c152262025-07-02 12:57:4857 void DeprecatedSetObjectTemplate(
58 DeprecatedWrapperInfo* info,
59 v8::Local<v8::ObjectTemplate> object_template);
Andreas Haas9d612c5a2025-07-03 07:26:5160
Andreas Haasd0e233d2025-07-07 18:01:1261 void SetObjectTemplate(const WrapperInfo* info,
Andreas Haas9d612c5a2025-07-03 07:26:5162 v8::Local<v8::ObjectTemplate> object_template);
63
Andreas Haas3c152262025-07-02 12:57:4864 void SetFunctionTemplate(DeprecatedWrapperInfo* info,
[email protected]97f21ca2013-11-17 17:46:0765 v8::Local<v8::FunctionTemplate> function_template);
[email protected]e87f3122013-11-12 00:41:2766
[email protected]60531d52013-11-27 02:10:1567 // These are low-level functions for retrieving object or function templates
68 // stored in this object. Because these templates are often created lazily,
69 // most clients should call higher-level functions that know how to populate
70 // these templates if they haven't already been created.
Andreas Haas3c152262025-07-02 12:57:4871 v8::Local<v8::ObjectTemplate> DeprecatedGetObjectTemplate(
72 DeprecatedWrapperInfo* info);
Andreas Haas9d612c5a2025-07-03 07:26:5173
Andreas Haasd0e233d2025-07-07 18:01:1274 v8::Local<v8::ObjectTemplate> GetObjectTemplate(const WrapperInfo* info);
Andreas Haas9d612c5a2025-07-03 07:26:5175
Andreas Haas3c152262025-07-02 12:57:4876 v8::Local<v8::FunctionTemplate> GetFunctionTemplate(
77 DeprecatedWrapperInfo* info);
[email protected]a22998a2013-11-10 05:00:5078
Ashley Newsondeda1812024-01-09 12:04:1879 void AddDisposeObserver(DisposeObserver* observer);
80 void RemoveDisposeObserver(DisposeObserver* observer);
81 void NotifyBeforeDispose();
82 void NotifyDisposed();
83
mostynbc862da82016-04-03 15:54:3384 void EnableIdleTasks(std::unique_ptr<V8IdleTaskRunner> idle_task_runner);
ulan3cbdcd02015-07-20 11:32:5885
[email protected]91cd4fe2013-11-28 09:31:5886 v8::Isolate* isolate() { return isolate_; }
[email protected]73dcce92014-02-20 08:24:0487 v8::ArrayBuffer::Allocator* allocator() { return allocator_; }
Andreas Haasc13cae82017-11-16 12:54:3888 std::shared_ptr<v8::TaskRunner> task_runner() { return task_runner_; }
Etienne Pierre-doray8e42980e2024-09-04 20:26:2789 std::shared_ptr<v8::TaskRunner> user_visible_task_runner() {
90 return user_visible_task_runner_;
91 }
92 std::shared_ptr<v8::TaskRunner> best_effort_task_runner() {
93 return best_effort_task_runner_;
Jiahe Zhange97ba772023-07-27 02:46:4194 }
jochen76acff102016-11-08 08:20:3795
[email protected]a22998a2013-11-10 05:00:5096 private:
Andreas Haas3c152262025-07-02 12:57:4897 typedef std::map<DeprecatedWrapperInfo*, v8::Eternal<v8::ObjectTemplate>>
98 DeprecatedObjectTemplateMap;
Andreas Haasd0e233d2025-07-07 18:01:1299 typedef std::map<const WrapperInfo*, v8::Eternal<v8::ObjectTemplate>>
Andreas Haas9d612c5a2025-07-03 07:26:51100 ObjectTemplateMap;
Andreas Haas3c152262025-07-02 12:57:48101 typedef std::map<DeprecatedWrapperInfo*, v8::Eternal<v8::FunctionTemplate>>
102 FunctionTemplateMap;
[email protected]a22998a2013-11-10 05:00:50103
[email protected]60531d52013-11-27 02:10:15104 // PerIsolateData doesn't actually own |isolate_|. Instead, the isolate is
105 // owned by the IsolateHolder, which also owns the PerIsolateData.
Pârise6361d02023-07-19 09:00:43106 raw_ptr<v8::Isolate, AcrossTasksDanglingUntriaged> isolate_;
Pâris8b159582022-08-31 04:13:28107 raw_ptr<v8::ArrayBuffer::Allocator, DanglingUntriaged> allocator_;
Andreas Haas3c152262025-07-02 12:57:48108 DeprecatedObjectTemplateMap deprecated_object_templates_;
Andreas Haas9d612c5a2025-07-03 07:26:51109 ObjectTemplateMap object_templates_;
[email protected]97f21ca2013-11-17 17:46:07110 FunctionTemplateMap function_templates_;
Ashley Newsondeda1812024-01-09 12:04:18111 base::ObserverList<DisposeObserver> dispose_observers_;
Andreas Haasc13cae82017-11-16 12:54:38112 std::shared_ptr<V8ForegroundTaskRunnerBase> task_runner_;
Etienne Pierre-doray8e42980e2024-09-04 20:26:27113 std::shared_ptr<V8ForegroundTaskRunnerBase> user_visible_task_runner_;
114 std::shared_ptr<V8ForegroundTaskRunnerBase> best_effort_task_runner_;
[email protected]a22998a2013-11-10 05:00:50115};
116
117} // namespace gin
118
119#endif // GIN_PER_ISOLATE_DATA_H_