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.
5 #ifndef GIN_PER_ISOLATE_DATA_H_
6 #define GIN_PER_ISOLATE_DATA_H_
10 #include "base/basictypes.h"
11 #include "base/memory/ref_counted.h"
12 #include "base/memory/scoped_ptr.h"
13 #include "gin/gin_export.h"
14 #include "gin/public/v8_idle_task_runner.h"
15 #include "gin/public/wrapper_info.h"
16 #include "v8/include/v8.h"
19 class SingleThreadTaskRunner
;
24 class IndexedPropertyInterceptor
;
25 class NamedPropertyInterceptor
;
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.
30 class GIN_EXPORT PerIsolateData
{
32 PerIsolateData(v8::Isolate
* isolate
, v8::ArrayBuffer::Allocator
* allocator
);
35 static PerIsolateData
* From(v8::Isolate
* isolate
);
37 // Each isolate is associated with a collection of v8::ObjectTemplates and
38 // v8::FunctionTemplates. Typically these template objects are created
40 void SetObjectTemplate(WrapperInfo
* info
,
41 v8::Local
<v8::ObjectTemplate
> object_template
);
42 void SetFunctionTemplate(WrapperInfo
* info
,
43 v8::Local
<v8::FunctionTemplate
> function_template
);
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.
49 v8::Local
<v8::ObjectTemplate
> GetObjectTemplate(WrapperInfo
* info
);
50 v8::Local
<v8::FunctionTemplate
> GetFunctionTemplate(WrapperInfo
* info
);
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
);
59 void ClearIndexedPropertyInterceptor(WrappableBase
* base
,
60 IndexedPropertyInterceptor
* interceptor
);
61 void ClearNamedPropertyInterceptor(WrappableBase
* base
,
62 NamedPropertyInterceptor
* interceptor
);
64 IndexedPropertyInterceptor
* GetIndexedPropertyInterceptor(
66 NamedPropertyInterceptor
* GetNamedPropertyInterceptor(WrappableBase
* base
);
68 void EnableIdleTasks(scoped_ptr
<V8IdleTaskRunner
> idle_task_runner
);
70 v8::Isolate
* isolate() { return isolate_
; }
71 v8::ArrayBuffer::Allocator
* allocator() { return allocator_
; }
72 base::SingleThreadTaskRunner
* task_runner() { return task_runner_
.get(); }
73 V8IdleTaskRunner
* idle_task_runner() {
74 return idle_task_runner_
.get();
79 WrapperInfo
*, v8::Eternal
<v8::ObjectTemplate
> > ObjectTemplateMap
;
81 WrapperInfo
*, v8::Eternal
<v8::FunctionTemplate
> > FunctionTemplateMap
;
82 typedef std::map
<WrappableBase
*, IndexedPropertyInterceptor
*>
83 IndexedPropertyInterceptorMap
;
84 typedef std::map
<WrappableBase
*, NamedPropertyInterceptor
*>
85 NamedPropertyInterceptorMap
;
87 // PerIsolateData doesn't actually own |isolate_|. Instead, the isolate is
88 // owned by the IsolateHolder, which also owns the PerIsolateData.
89 v8::Isolate
* isolate_
;
90 v8::ArrayBuffer::Allocator
* allocator_
;
91 ObjectTemplateMap object_templates_
;
92 FunctionTemplateMap function_templates_
;
93 IndexedPropertyInterceptorMap indexed_interceptors_
;
94 NamedPropertyInterceptorMap named_interceptors_
;
95 scoped_refptr
<base::SingleThreadTaskRunner
> task_runner_
;
96 scoped_ptr
<V8IdleTaskRunner
> idle_task_runner_
;
98 DISALLOW_COPY_AND_ASSIGN(PerIsolateData
);
103 #endif // GIN_PER_ISOLATE_DATA_H_