blob: ac0cf95ab352df50f70bd5b2edafbfa652eeb32f [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"
[email protected]48c21632013-12-12 21:32:3413#include "gin/gin_export.h"
jochen76acff102016-11-08 08:20:3714#include "gin/public/isolate_holder.h"
ulan3cbdcd02015-07-20 11:32:5815#include "gin/public/v8_idle_task_runner.h"
[email protected]c07006b2013-11-20 03:01:4416#include "gin/public/wrapper_info.h"
[email protected]a22998a2013-11-10 05:00:5017#include "v8/include/v8.h"
18
[email protected]b64e5212014-04-04 21:09:1619namespace base {
skyostila3899862015-06-12 18:21:5820class SingleThreadTaskRunner;
[email protected]b64e5212014-04-04 21:09:1621}
22
[email protected]a22998a2013-11-10 05:00:5023namespace gin {
24
[email protected]5c969b82014-03-12 04:59:0525class IndexedPropertyInterceptor;
26class NamedPropertyInterceptor;
27class WrappableBase;
28
[email protected]60531d52013-11-27 02:10:1529// There is one instance of PerIsolateData per v8::Isolate managed by Gin. This
30// class stores all the Gin-related data that varies per isolate.
[email protected]48c21632013-12-12 21:32:3431class GIN_EXPORT PerIsolateData {
[email protected]a22998a2013-11-10 05:00:5032 public:
jochen76acff102016-11-08 08:20:3733 PerIsolateData(v8::Isolate* isolate,
34 v8::ArrayBuffer::Allocator* allocator,
altimin124814c2017-01-03 14:06:5435 IsolateHolder::AccessMode access_mode,
36 scoped_refptr<base::SingleThreadTaskRunner> task_runner);
[email protected]a22998a2013-11-10 05:00:5037 ~PerIsolateData();
38
39 static PerIsolateData* From(v8::Isolate* isolate);
40
[email protected]60531d52013-11-27 02:10:1541 // Each isolate is associated with a collection of v8::ObjectTemplates and
42 // v8::FunctionTemplates. Typically these template objects are created
43 // lazily.
[email protected]e87f3122013-11-12 00:41:2744 void SetObjectTemplate(WrapperInfo* info,
45 v8::Local<v8::ObjectTemplate> object_template);
[email protected]97f21ca2013-11-17 17:46:0746 void SetFunctionTemplate(WrapperInfo* info,
47 v8::Local<v8::FunctionTemplate> function_template);
[email protected]e87f3122013-11-12 00:41:2748
[email protected]60531d52013-11-27 02:10:1549 // These are low-level functions for retrieving object or function templates
50 // stored in this object. Because these templates are often created lazily,
51 // most clients should call higher-level functions that know how to populate
52 // these templates if they haven't already been created.
[email protected]e87f3122013-11-12 00:41:2753 v8::Local<v8::ObjectTemplate> GetObjectTemplate(WrapperInfo* info);
[email protected]97f21ca2013-11-17 17:46:0754 v8::Local<v8::FunctionTemplate> GetFunctionTemplate(WrapperInfo* info);
[email protected]a22998a2013-11-10 05:00:5055
[email protected]5c969b82014-03-12 04:59:0556 // We maintain a map from Wrappable objects that derive from one of the
57 // interceptor interfaces to the interceptor interface pointers.
58 void SetIndexedPropertyInterceptor(WrappableBase* base,
59 IndexedPropertyInterceptor* interceptor);
60 void SetNamedPropertyInterceptor(WrappableBase* base,
61 NamedPropertyInterceptor* interceptor);
62
63 void ClearIndexedPropertyInterceptor(WrappableBase* base,
64 IndexedPropertyInterceptor* interceptor);
65 void ClearNamedPropertyInterceptor(WrappableBase* base,
66 NamedPropertyInterceptor* interceptor);
67
68 IndexedPropertyInterceptor* GetIndexedPropertyInterceptor(
69 WrappableBase* base);
70 NamedPropertyInterceptor* GetNamedPropertyInterceptor(WrappableBase* base);
71
mostynbc862da82016-04-03 15:54:3372 void EnableIdleTasks(std::unique_ptr<V8IdleTaskRunner> idle_task_runner);
ulan3cbdcd02015-07-20 11:32:5873
[email protected]91cd4fe2013-11-28 09:31:5874 v8::Isolate* isolate() { return isolate_; }
[email protected]73dcce92014-02-20 08:24:0475 v8::ArrayBuffer::Allocator* allocator() { return allocator_; }
skyostila3899862015-06-12 18:21:5876 base::SingleThreadTaskRunner* task_runner() { return task_runner_.get(); }
ulan3cbdcd02015-07-20 11:32:5877 V8IdleTaskRunner* idle_task_runner() {
78 return idle_task_runner_.get();
79 }
[email protected]91cd4fe2013-11-28 09:31:5880
jochen76acff102016-11-08 08:20:3781 IsolateHolder::AccessMode access_mode() const { return access_mode_; }
82
[email protected]a22998a2013-11-10 05:00:5083 private:
84 typedef std::map<
85 WrapperInfo*, v8::Eternal<v8::ObjectTemplate> > ObjectTemplateMap;
[email protected]97f21ca2013-11-17 17:46:0786 typedef std::map<
87 WrapperInfo*, v8::Eternal<v8::FunctionTemplate> > FunctionTemplateMap;
[email protected]5c969b82014-03-12 04:59:0588 typedef std::map<WrappableBase*, IndexedPropertyInterceptor*>
89 IndexedPropertyInterceptorMap;
90 typedef std::map<WrappableBase*, NamedPropertyInterceptor*>
91 NamedPropertyInterceptorMap;
[email protected]a22998a2013-11-10 05:00:5092
[email protected]60531d52013-11-27 02:10:1593 // PerIsolateData doesn't actually own |isolate_|. Instead, the isolate is
94 // owned by the IsolateHolder, which also owns the PerIsolateData.
[email protected]a22998a2013-11-10 05:00:5095 v8::Isolate* isolate_;
[email protected]73dcce92014-02-20 08:24:0496 v8::ArrayBuffer::Allocator* allocator_;
jochen76acff102016-11-08 08:20:3797 IsolateHolder::AccessMode access_mode_;
[email protected]a22998a2013-11-10 05:00:5098 ObjectTemplateMap object_templates_;
[email protected]97f21ca2013-11-17 17:46:0799 FunctionTemplateMap function_templates_;
[email protected]5c969b82014-03-12 04:59:05100 IndexedPropertyInterceptorMap indexed_interceptors_;
101 NamedPropertyInterceptorMap named_interceptors_;
skyostila3899862015-06-12 18:21:58102 scoped_refptr<base::SingleThreadTaskRunner> task_runner_;
mostynbc862da82016-04-03 15:54:33103 std::unique_ptr<V8IdleTaskRunner> idle_task_runner_;
[email protected]a22998a2013-11-10 05:00:50104
105 DISALLOW_COPY_AND_ASSIGN(PerIsolateData);
106};
107
108} // namespace gin
109
110#endif // GIN_PER_ISOLATE_DATA_H_