[email protected] | a22998a | 2013-11-10 05:00:50 | [diff] [blame] | 1 | // 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> |
mostynb | c862da8 | 2016-04-03 15:54:33 | [diff] [blame] | 9 | #include <memory> |
[email protected] | a22998a | 2013-11-10 05:00:50 | [diff] [blame] | 10 | |
avi | 90e658dd | 2015-12-21 07:16:19 | [diff] [blame] | 11 | #include "base/macros.h" |
[email protected] | b64e521 | 2014-04-04 21:09:16 | [diff] [blame] | 12 | #include "base/memory/ref_counted.h" |
[email protected] | 48c2163 | 2013-12-12 21:32:34 | [diff] [blame] | 13 | #include "gin/gin_export.h" |
ulan | 3cbdcd0 | 2015-07-20 11:32:58 | [diff] [blame] | 14 | #include "gin/public/v8_idle_task_runner.h" |
[email protected] | c07006b | 2013-11-20 03:01:44 | [diff] [blame] | 15 | #include "gin/public/wrapper_info.h" |
[email protected] | a22998a | 2013-11-10 05:00:50 | [diff] [blame] | 16 | #include "v8/include/v8.h" |
| 17 | |
[email protected] | b64e521 | 2014-04-04 21:09:16 | [diff] [blame] | 18 | namespace base { |
skyostil | a389986 | 2015-06-12 18:21:58 | [diff] [blame] | 19 | class SingleThreadTaskRunner; |
[email protected] | b64e521 | 2014-04-04 21:09:16 | [diff] [blame] | 20 | } |
| 21 | |
[email protected] | a22998a | 2013-11-10 05:00:50 | [diff] [blame] | 22 | namespace gin { |
| 23 | |
[email protected] | 5c969b8 | 2014-03-12 04:59:05 | [diff] [blame] | 24 | class IndexedPropertyInterceptor; |
| 25 | class NamedPropertyInterceptor; |
| 26 | class WrappableBase; |
| 27 | |
[email protected] | 60531d5 | 2013-11-27 02:10:15 | [diff] [blame] | 28 | // 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] | 48c2163 | 2013-12-12 21:32:34 | [diff] [blame] | 30 | class GIN_EXPORT PerIsolateData { |
[email protected] | a22998a | 2013-11-10 05:00:50 | [diff] [blame] | 31 | public: |
[email protected] | 73dcce9 | 2014-02-20 08:24:04 | [diff] [blame] | 32 | PerIsolateData(v8::Isolate* isolate, v8::ArrayBuffer::Allocator* allocator); |
[email protected] | a22998a | 2013-11-10 05:00:50 | [diff] [blame] | 33 | ~PerIsolateData(); |
| 34 | |
| 35 | static PerIsolateData* From(v8::Isolate* isolate); |
| 36 | |
[email protected] | 60531d5 | 2013-11-27 02:10:15 | [diff] [blame] | 37 | // Each isolate is associated with a collection of v8::ObjectTemplates and |
| 38 | // v8::FunctionTemplates. Typically these template objects are created |
| 39 | // lazily. |
[email protected] | e87f312 | 2013-11-12 00:41:27 | [diff] [blame] | 40 | void SetObjectTemplate(WrapperInfo* info, |
| 41 | v8::Local<v8::ObjectTemplate> object_template); |
[email protected] | 97f21ca | 2013-11-17 17:46:07 | [diff] [blame] | 42 | void SetFunctionTemplate(WrapperInfo* info, |
| 43 | v8::Local<v8::FunctionTemplate> function_template); |
[email protected] | e87f312 | 2013-11-12 00:41:27 | [diff] [blame] | 44 | |
[email protected] | 60531d5 | 2013-11-27 02:10:15 | [diff] [blame] | 45 | // These are low-level functions for retrieving object or function templates |
| 46 | // stored in this object. Because these templates are often created lazily, |
| 47 | // most clients should call higher-level functions that know how to populate |
| 48 | // these templates if they haven't already been created. |
[email protected] | e87f312 | 2013-11-12 00:41:27 | [diff] [blame] | 49 | v8::Local<v8::ObjectTemplate> GetObjectTemplate(WrapperInfo* info); |
[email protected] | 97f21ca | 2013-11-17 17:46:07 | [diff] [blame] | 50 | v8::Local<v8::FunctionTemplate> GetFunctionTemplate(WrapperInfo* info); |
[email protected] | a22998a | 2013-11-10 05:00:50 | [diff] [blame] | 51 | |
[email protected] | 5c969b8 | 2014-03-12 04:59:05 | [diff] [blame] | 52 | // We maintain a map from Wrappable objects that derive from one of the |
| 53 | // interceptor interfaces to the interceptor interface pointers. |
| 54 | void SetIndexedPropertyInterceptor(WrappableBase* base, |
| 55 | IndexedPropertyInterceptor* interceptor); |
| 56 | void SetNamedPropertyInterceptor(WrappableBase* base, |
| 57 | NamedPropertyInterceptor* interceptor); |
| 58 | |
| 59 | void ClearIndexedPropertyInterceptor(WrappableBase* base, |
| 60 | IndexedPropertyInterceptor* interceptor); |
| 61 | void ClearNamedPropertyInterceptor(WrappableBase* base, |
| 62 | NamedPropertyInterceptor* interceptor); |
| 63 | |
| 64 | IndexedPropertyInterceptor* GetIndexedPropertyInterceptor( |
| 65 | WrappableBase* base); |
| 66 | NamedPropertyInterceptor* GetNamedPropertyInterceptor(WrappableBase* base); |
| 67 | |
mostynb | c862da8 | 2016-04-03 15:54:33 | [diff] [blame] | 68 | void EnableIdleTasks(std::unique_ptr<V8IdleTaskRunner> idle_task_runner); |
ulan | 3cbdcd0 | 2015-07-20 11:32:58 | [diff] [blame] | 69 | |
[email protected] | 91cd4fe | 2013-11-28 09:31:58 | [diff] [blame] | 70 | v8::Isolate* isolate() { return isolate_; } |
[email protected] | 73dcce9 | 2014-02-20 08:24:04 | [diff] [blame] | 71 | v8::ArrayBuffer::Allocator* allocator() { return allocator_; } |
skyostil | a389986 | 2015-06-12 18:21:58 | [diff] [blame] | 72 | base::SingleThreadTaskRunner* task_runner() { return task_runner_.get(); } |
ulan | 3cbdcd0 | 2015-07-20 11:32:58 | [diff] [blame] | 73 | V8IdleTaskRunner* idle_task_runner() { |
| 74 | return idle_task_runner_.get(); |
| 75 | } |
[email protected] | 91cd4fe | 2013-11-28 09:31:58 | [diff] [blame] | 76 | |
[email protected] | a22998a | 2013-11-10 05:00:50 | [diff] [blame] | 77 | private: |
| 78 | typedef std::map< |
| 79 | WrapperInfo*, v8::Eternal<v8::ObjectTemplate> > ObjectTemplateMap; |
[email protected] | 97f21ca | 2013-11-17 17:46:07 | [diff] [blame] | 80 | typedef std::map< |
| 81 | WrapperInfo*, v8::Eternal<v8::FunctionTemplate> > FunctionTemplateMap; |
[email protected] | 5c969b8 | 2014-03-12 04:59:05 | [diff] [blame] | 82 | typedef std::map<WrappableBase*, IndexedPropertyInterceptor*> |
| 83 | IndexedPropertyInterceptorMap; |
| 84 | typedef std::map<WrappableBase*, NamedPropertyInterceptor*> |
| 85 | NamedPropertyInterceptorMap; |
[email protected] | a22998a | 2013-11-10 05:00:50 | [diff] [blame] | 86 | |
[email protected] | 60531d5 | 2013-11-27 02:10:15 | [diff] [blame] | 87 | // PerIsolateData doesn't actually own |isolate_|. Instead, the isolate is |
| 88 | // owned by the IsolateHolder, which also owns the PerIsolateData. |
[email protected] | a22998a | 2013-11-10 05:00:50 | [diff] [blame] | 89 | v8::Isolate* isolate_; |
[email protected] | 73dcce9 | 2014-02-20 08:24:04 | [diff] [blame] | 90 | v8::ArrayBuffer::Allocator* allocator_; |
[email protected] | a22998a | 2013-11-10 05:00:50 | [diff] [blame] | 91 | ObjectTemplateMap object_templates_; |
[email protected] | 97f21ca | 2013-11-17 17:46:07 | [diff] [blame] | 92 | FunctionTemplateMap function_templates_; |
[email protected] | 5c969b8 | 2014-03-12 04:59:05 | [diff] [blame] | 93 | IndexedPropertyInterceptorMap indexed_interceptors_; |
| 94 | NamedPropertyInterceptorMap named_interceptors_; |
skyostil | a389986 | 2015-06-12 18:21:58 | [diff] [blame] | 95 | scoped_refptr<base::SingleThreadTaskRunner> task_runner_; |
mostynb | c862da8 | 2016-04-03 15:54:33 | [diff] [blame] | 96 | std::unique_ptr<V8IdleTaskRunner> idle_task_runner_; |
[email protected] | a22998a | 2013-11-10 05:00:50 | [diff] [blame] | 97 | |
| 98 | DISALLOW_COPY_AND_ASSIGN(PerIsolateData); |
| 99 | }; |
| 100 | |
| 101 | } // namespace gin |
| 102 | |
| 103 | #endif // GIN_PER_ISOLATE_DATA_H_ |