blob: 31a5cb404f0bbf985ddee732a11000a64c37a4f0 [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"
[email protected]c07006b2013-11-20 03:01:4415#include "gin/public/wrapper_info.h"
Andreas Haasc13cae82017-11-16 12:54:3816#include "gin/v8_foreground_task_runner_base.h"
[email protected]a22998a2013-11-10 05:00:5017#include "v8/include/v8.h"
18
19namespace gin {
20
Andreas Haasc13cae82017-11-16 12:54:3821class V8IdleTaskRunner;
[email protected]5c969b82014-03-12 04:59:0522class IndexedPropertyInterceptor;
23class NamedPropertyInterceptor;
24class WrappableBase;
25
[email protected]60531d52013-11-27 02:10:1526// There is one instance of PerIsolateData per v8::Isolate managed by Gin. This
27// class stores all the Gin-related data that varies per isolate.
[email protected]48c21632013-12-12 21:32:3428class GIN_EXPORT PerIsolateData {
[email protected]a22998a2013-11-10 05:00:5029 public:
jochen76acff102016-11-08 08:20:3730 PerIsolateData(v8::Isolate* isolate,
31 v8::ArrayBuffer::Allocator* allocator,
altimin124814c2017-01-03 14:06:5432 IsolateHolder::AccessMode access_mode,
33 scoped_refptr<base::SingleThreadTaskRunner> task_runner);
[email protected]a22998a2013-11-10 05:00:5034 ~PerIsolateData();
35
36 static PerIsolateData* From(v8::Isolate* isolate);
37
[email protected]60531d52013-11-27 02:10:1538 // Each isolate is associated with a collection of v8::ObjectTemplates and
39 // v8::FunctionTemplates. Typically these template objects are created
40 // lazily.
[email protected]e87f3122013-11-12 00:41:2741 void SetObjectTemplate(WrapperInfo* info,
42 v8::Local<v8::ObjectTemplate> object_template);
[email protected]97f21ca2013-11-17 17:46:0743 void SetFunctionTemplate(WrapperInfo* info,
44 v8::Local<v8::FunctionTemplate> function_template);
[email protected]e87f3122013-11-12 00:41:2745
[email protected]60531d52013-11-27 02:10:1546 // These are low-level functions for retrieving object or function templates
47 // stored in this object. Because these templates are often created lazily,
48 // most clients should call higher-level functions that know how to populate
49 // these templates if they haven't already been created.
[email protected]e87f3122013-11-12 00:41:2750 v8::Local<v8::ObjectTemplate> GetObjectTemplate(WrapperInfo* info);
[email protected]97f21ca2013-11-17 17:46:0751 v8::Local<v8::FunctionTemplate> GetFunctionTemplate(WrapperInfo* info);
[email protected]a22998a2013-11-10 05:00:5052
[email protected]5c969b82014-03-12 04:59:0553 // We maintain a map from Wrappable objects that derive from one of the
54 // interceptor interfaces to the interceptor interface pointers.
55 void SetIndexedPropertyInterceptor(WrappableBase* base,
56 IndexedPropertyInterceptor* interceptor);
57 void SetNamedPropertyInterceptor(WrappableBase* base,
58 NamedPropertyInterceptor* interceptor);
59
60 void ClearIndexedPropertyInterceptor(WrappableBase* base,
61 IndexedPropertyInterceptor* interceptor);
62 void ClearNamedPropertyInterceptor(WrappableBase* base,
63 NamedPropertyInterceptor* interceptor);
64
65 IndexedPropertyInterceptor* GetIndexedPropertyInterceptor(
66 WrappableBase* base);
67 NamedPropertyInterceptor* GetNamedPropertyInterceptor(WrappableBase* base);
68
mostynbc862da82016-04-03 15:54:3369 void EnableIdleTasks(std::unique_ptr<V8IdleTaskRunner> idle_task_runner);
ulan3cbdcd02015-07-20 11:32:5870
[email protected]91cd4fe2013-11-28 09:31:5871 v8::Isolate* isolate() { return isolate_; }
[email protected]73dcce92014-02-20 08:24:0472 v8::ArrayBuffer::Allocator* allocator() { return allocator_; }
Andreas Haasc13cae82017-11-16 12:54:3873 std::shared_ptr<v8::TaskRunner> task_runner() { return task_runner_; }
jochen76acff102016-11-08 08:20:3774
[email protected]a22998a2013-11-10 05:00:5075 private:
76 typedef std::map<
77 WrapperInfo*, v8::Eternal<v8::ObjectTemplate> > ObjectTemplateMap;
[email protected]97f21ca2013-11-17 17:46:0778 typedef std::map<
79 WrapperInfo*, v8::Eternal<v8::FunctionTemplate> > FunctionTemplateMap;
[email protected]5c969b82014-03-12 04:59:0580 typedef std::map<WrappableBase*, IndexedPropertyInterceptor*>
81 IndexedPropertyInterceptorMap;
82 typedef std::map<WrappableBase*, NamedPropertyInterceptor*>
83 NamedPropertyInterceptorMap;
[email protected]a22998a2013-11-10 05:00:5084
[email protected]60531d52013-11-27 02:10:1585 // PerIsolateData doesn't actually own |isolate_|. Instead, the isolate is
86 // owned by the IsolateHolder, which also owns the PerIsolateData.
[email protected]a22998a2013-11-10 05:00:5087 v8::Isolate* isolate_;
[email protected]73dcce92014-02-20 08:24:0488 v8::ArrayBuffer::Allocator* allocator_;
[email protected]a22998a2013-11-10 05:00:5089 ObjectTemplateMap object_templates_;
[email protected]97f21ca2013-11-17 17:46:0790 FunctionTemplateMap function_templates_;
[email protected]5c969b82014-03-12 04:59:0591 IndexedPropertyInterceptorMap indexed_interceptors_;
92 NamedPropertyInterceptorMap named_interceptors_;
Andreas Haasc13cae82017-11-16 12:54:3893 std::shared_ptr<V8ForegroundTaskRunnerBase> task_runner_;
[email protected]a22998a2013-11-10 05:00:5094
95 DISALLOW_COPY_AND_ASSIGN(PerIsolateData);
96};
97
98} // namespace gin
99
100#endif // GIN_PER_ISOLATE_DATA_H_