blob: 148a9560b2556a964ce9844667e169b878827a20 [file] [log] [blame]
[email protected]a22998a2013-11-10 05:00:501// 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.
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
avi90e658dd2015-12-21 07:16:1911#include "base/macros.h"
[email protected]b64e5212014-04-04 21:09:1612#include "base/memory/ref_counted.h"
Gabriel Charette14520232018-04-30 23:27:2213#include "base/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"
[email protected]a22998a2013-11-10 05:00:5018#include "v8/include/v8.h"
19
20namespace gin {
21
Andreas Haasc13cae82017-11-16 12:54:3822class V8IdleTaskRunner;
[email protected]5c969b82014-03-12 04:59:0523class IndexedPropertyInterceptor;
24class NamedPropertyInterceptor;
25class WrappableBase;
26
[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:
jochen76acff102016-11-08 08:20:3731 PerIsolateData(v8::Isolate* isolate,
32 v8::ArrayBuffer::Allocator* allocator,
altimin124814c2017-01-03 14:06:5433 IsolateHolder::AccessMode access_mode,
34 scoped_refptr<base::SingleThreadTaskRunner> task_runner);
[email protected]a22998a2013-11-10 05:00:5035 ~PerIsolateData();
36
37 static PerIsolateData* From(v8::Isolate* isolate);
38
[email protected]60531d52013-11-27 02:10:1539 // Each isolate is associated with a collection of v8::ObjectTemplates and
40 // v8::FunctionTemplates. Typically these template objects are created
41 // lazily.
[email protected]e87f3122013-11-12 00:41:2742 void SetObjectTemplate(WrapperInfo* info,
43 v8::Local<v8::ObjectTemplate> object_template);
[email protected]97f21ca2013-11-17 17:46:0744 void SetFunctionTemplate(WrapperInfo* info,
45 v8::Local<v8::FunctionTemplate> function_template);
[email protected]e87f3122013-11-12 00:41:2746
[email protected]60531d52013-11-27 02:10:1547 // These are low-level functions for retrieving object or function templates
48 // stored in this object. Because these templates are often created lazily,
49 // most clients should call higher-level functions that know how to populate
50 // these templates if they haven't already been created.
[email protected]e87f3122013-11-12 00:41:2751 v8::Local<v8::ObjectTemplate> GetObjectTemplate(WrapperInfo* info);
[email protected]97f21ca2013-11-17 17:46:0752 v8::Local<v8::FunctionTemplate> GetFunctionTemplate(WrapperInfo* info);
[email protected]a22998a2013-11-10 05:00:5053
[email protected]5c969b82014-03-12 04:59:0554 // We maintain a map from Wrappable objects that derive from one of the
55 // interceptor interfaces to the interceptor interface pointers.
56 void SetIndexedPropertyInterceptor(WrappableBase* base,
57 IndexedPropertyInterceptor* interceptor);
58 void SetNamedPropertyInterceptor(WrappableBase* base,
59 NamedPropertyInterceptor* interceptor);
60
61 void ClearIndexedPropertyInterceptor(WrappableBase* base,
62 IndexedPropertyInterceptor* interceptor);
63 void ClearNamedPropertyInterceptor(WrappableBase* base,
64 NamedPropertyInterceptor* interceptor);
65
66 IndexedPropertyInterceptor* GetIndexedPropertyInterceptor(
67 WrappableBase* base);
68 NamedPropertyInterceptor* GetNamedPropertyInterceptor(WrappableBase* base);
69
mostynbc862da82016-04-03 15:54:3370 void EnableIdleTasks(std::unique_ptr<V8IdleTaskRunner> idle_task_runner);
ulan3cbdcd02015-07-20 11:32:5871
[email protected]91cd4fe2013-11-28 09:31:5872 v8::Isolate* isolate() { return isolate_; }
[email protected]73dcce92014-02-20 08:24:0473 v8::ArrayBuffer::Allocator* allocator() { return allocator_; }
Andreas Haasc13cae82017-11-16 12:54:3874 std::shared_ptr<v8::TaskRunner> task_runner() { return task_runner_; }
jochen76acff102016-11-08 08:20:3775
[email protected]a22998a2013-11-10 05:00:5076 private:
77 typedef std::map<
78 WrapperInfo*, v8::Eternal<v8::ObjectTemplate> > ObjectTemplateMap;
[email protected]97f21ca2013-11-17 17:46:0779 typedef std::map<
80 WrapperInfo*, v8::Eternal<v8::FunctionTemplate> > FunctionTemplateMap;
[email protected]5c969b82014-03-12 04:59:0581 typedef std::map<WrappableBase*, IndexedPropertyInterceptor*>
82 IndexedPropertyInterceptorMap;
83 typedef std::map<WrappableBase*, NamedPropertyInterceptor*>
84 NamedPropertyInterceptorMap;
[email protected]a22998a2013-11-10 05:00:5085
[email protected]60531d52013-11-27 02:10:1586 // PerIsolateData doesn't actually own |isolate_|. Instead, the isolate is
87 // owned by the IsolateHolder, which also owns the PerIsolateData.
[email protected]a22998a2013-11-10 05:00:5088 v8::Isolate* isolate_;
[email protected]73dcce92014-02-20 08:24:0489 v8::ArrayBuffer::Allocator* allocator_;
[email protected]a22998a2013-11-10 05:00:5090 ObjectTemplateMap object_templates_;
[email protected]97f21ca2013-11-17 17:46:0791 FunctionTemplateMap function_templates_;
[email protected]5c969b82014-03-12 04:59:0592 IndexedPropertyInterceptorMap indexed_interceptors_;
93 NamedPropertyInterceptorMap named_interceptors_;
Andreas Haasc13cae82017-11-16 12:54:3894 std::shared_ptr<V8ForegroundTaskRunnerBase> task_runner_;
[email protected]a22998a2013-11-10 05:00:5095
96 DISALLOW_COPY_AND_ASSIGN(PerIsolateData);
97};
98
99} // namespace gin
100
101#endif // GIN_PER_ISOLATE_DATA_H_