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 #include "gin/per_isolate_data.h"
7 #include "base/logging.h"
8 #include "base/single_thread_task_runner.h"
9 #include "base/thread_task_runner_handle.h"
10 #include "gin/public/gin_embedders.h"
12 using v8::ArrayBuffer
;
17 using v8::FunctionTemplate
;
18 using v8::ObjectTemplate
;
22 PerIsolateData::PerIsolateData(Isolate
* isolate
,
23 ArrayBuffer::Allocator
* allocator
)
25 allocator_(allocator
),
26 task_runner_(base::ThreadTaskRunnerHandle::Get()) {
27 isolate_
->SetData(kEmbedderNativeGin
, this);
30 PerIsolateData::~PerIsolateData() {
31 isolate_
->SetData(kEmbedderNativeGin
, NULL
);
34 PerIsolateData
* PerIsolateData::From(Isolate
* isolate
) {
35 return static_cast<PerIsolateData
*>(isolate
->GetData(kEmbedderNativeGin
));
38 void PerIsolateData::SetObjectTemplate(WrapperInfo
* info
,
39 Local
<ObjectTemplate
> templ
) {
40 object_templates_
[info
] = Eternal
<ObjectTemplate
>(isolate_
, templ
);
43 void PerIsolateData::SetFunctionTemplate(WrapperInfo
* info
,
44 Local
<FunctionTemplate
> templ
) {
45 function_templates_
[info
] = Eternal
<FunctionTemplate
>(isolate_
, templ
);
48 v8::Local
<v8::ObjectTemplate
> PerIsolateData::GetObjectTemplate(
50 ObjectTemplateMap::iterator it
= object_templates_
.find(info
);
51 if (it
== object_templates_
.end())
52 return v8::Local
<v8::ObjectTemplate
>();
53 return it
->second
.Get(isolate_
);
56 v8::Local
<v8::FunctionTemplate
> PerIsolateData::GetFunctionTemplate(
58 FunctionTemplateMap::iterator it
= function_templates_
.find(info
);
59 if (it
== function_templates_
.end())
60 return v8::Local
<v8::FunctionTemplate
>();
61 return it
->second
.Get(isolate_
);
64 void PerIsolateData::SetIndexedPropertyInterceptor(
66 IndexedPropertyInterceptor
* interceptor
) {
67 indexed_interceptors_
[base
] = interceptor
;
70 void PerIsolateData::SetNamedPropertyInterceptor(
72 NamedPropertyInterceptor
* interceptor
) {
73 named_interceptors_
[base
] = interceptor
;
76 void PerIsolateData::ClearIndexedPropertyInterceptor(
78 IndexedPropertyInterceptor
* interceptor
) {
79 IndexedPropertyInterceptorMap::iterator it
= indexed_interceptors_
.find(base
);
80 if (it
!= indexed_interceptors_
.end())
81 indexed_interceptors_
.erase(it
);
86 void PerIsolateData::ClearNamedPropertyInterceptor(
88 NamedPropertyInterceptor
* interceptor
) {
89 NamedPropertyInterceptorMap::iterator it
= named_interceptors_
.find(base
);
90 if (it
!= named_interceptors_
.end())
91 named_interceptors_
.erase(it
);
96 IndexedPropertyInterceptor
* PerIsolateData::GetIndexedPropertyInterceptor(
97 WrappableBase
* base
) {
98 IndexedPropertyInterceptorMap::iterator it
= indexed_interceptors_
.find(base
);
99 if (it
!= indexed_interceptors_
.end())
105 NamedPropertyInterceptor
* PerIsolateData::GetNamedPropertyInterceptor(
106 WrappableBase
* base
) {
107 NamedPropertyInterceptorMap::iterator it
= named_interceptors_
.find(base
);
108 if (it
!= named_interceptors_
.end())
114 void PerIsolateData::EnableIdleTasks(
115 scoped_ptr
<V8IdleTaskRunner
> idle_task_runner
) {
116 idle_task_runner_
= idle_task_runner
.Pass();