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 #include "base/logging.h"
6 #include "base/message_loop/message_loop_proxy.h"
7 #include "gin/per_isolate_data.h"
8 #include "gin/public/gin_embedders.h"
9
10 using v8::ArrayBuffer;
11 using v8::Eternal;
12 using v8::Isolate;
13 using v8::Local;
14 using v8::Object;
15 using v8::FunctionTemplate;
16 using v8::ObjectTemplate;
17
18 namespace gin {
19
PerIsolateData(Isolate * isolate,ArrayBuffer::Allocator * allocator)20 PerIsolateData::PerIsolateData(Isolate* isolate,
21 ArrayBuffer::Allocator* allocator)
22 : isolate_(isolate),
23 allocator_(allocator),
24 message_loop_proxy_(base::MessageLoopProxy::current()) {
25 isolate_->SetData(kEmbedderNativeGin, this);
26 }
27
~PerIsolateData()28 PerIsolateData::~PerIsolateData() {
29 isolate_->SetData(kEmbedderNativeGin, NULL);
30 }
31
From(Isolate * isolate)32 PerIsolateData* PerIsolateData::From(Isolate* isolate) {
33 return static_cast<PerIsolateData*>(isolate->GetData(kEmbedderNativeGin));
34 }
35
SetObjectTemplate(WrapperInfo * info,Local<ObjectTemplate> templ)36 void PerIsolateData::SetObjectTemplate(WrapperInfo* info,
37 Local<ObjectTemplate> templ) {
38 object_templates_[info] = Eternal<ObjectTemplate>(isolate_, templ);
39 }
40
SetFunctionTemplate(WrapperInfo * info,Local<FunctionTemplate> templ)41 void PerIsolateData::SetFunctionTemplate(WrapperInfo* info,
42 Local<FunctionTemplate> templ) {
43 function_templates_[info] = Eternal<FunctionTemplate>(isolate_, templ);
44 }
45
GetObjectTemplate(WrapperInfo * info)46 v8::Local<v8::ObjectTemplate> PerIsolateData::GetObjectTemplate(
47 WrapperInfo* info) {
48 ObjectTemplateMap::iterator it = object_templates_.find(info);
49 if (it == object_templates_.end())
50 return v8::Local<v8::ObjectTemplate>();
51 return it->second.Get(isolate_);
52 }
53
GetFunctionTemplate(WrapperInfo * info)54 v8::Local<v8::FunctionTemplate> PerIsolateData::GetFunctionTemplate(
55 WrapperInfo* info) {
56 FunctionTemplateMap::iterator it = function_templates_.find(info);
57 if (it == function_templates_.end())
58 return v8::Local<v8::FunctionTemplate>();
59 return it->second.Get(isolate_);
60 }
61
SetIndexedPropertyInterceptor(WrappableBase * base,IndexedPropertyInterceptor * interceptor)62 void PerIsolateData::SetIndexedPropertyInterceptor(
63 WrappableBase* base,
64 IndexedPropertyInterceptor* interceptor) {
65 indexed_interceptors_[base] = interceptor;
66 }
67
SetNamedPropertyInterceptor(WrappableBase * base,NamedPropertyInterceptor * interceptor)68 void PerIsolateData::SetNamedPropertyInterceptor(
69 WrappableBase* base,
70 NamedPropertyInterceptor* interceptor) {
71 named_interceptors_[base] = interceptor;
72 }
73
ClearIndexedPropertyInterceptor(WrappableBase * base,IndexedPropertyInterceptor * interceptor)74 void PerIsolateData::ClearIndexedPropertyInterceptor(
75 WrappableBase* base,
76 IndexedPropertyInterceptor* interceptor) {
77 IndexedPropertyInterceptorMap::iterator it = indexed_interceptors_.find(base);
78 if (it != indexed_interceptors_.end())
79 indexed_interceptors_.erase(it);
80 else
81 NOTREACHED();
82 }
83
ClearNamedPropertyInterceptor(WrappableBase * base,NamedPropertyInterceptor * interceptor)84 void PerIsolateData::ClearNamedPropertyInterceptor(
85 WrappableBase* base,
86 NamedPropertyInterceptor* interceptor) {
87 NamedPropertyInterceptorMap::iterator it = named_interceptors_.find(base);
88 if (it != named_interceptors_.end())
89 named_interceptors_.erase(it);
90 else
91 NOTREACHED();
92 }
93
GetIndexedPropertyInterceptor(WrappableBase * base)94 IndexedPropertyInterceptor* PerIsolateData::GetIndexedPropertyInterceptor(
95 WrappableBase* base) {
96 IndexedPropertyInterceptorMap::iterator it = indexed_interceptors_.find(base);
97 if (it != indexed_interceptors_.end())
98 return it->second;
99 else
100 return NULL;
101 }
102
GetNamedPropertyInterceptor(WrappableBase * base)103 NamedPropertyInterceptor* PerIsolateData::GetNamedPropertyInterceptor(
104 WrappableBase* base) {
105 NamedPropertyInterceptorMap::iterator it = named_interceptors_.find(base);
106 if (it != named_interceptors_.end())
107 return it->second;
108 else
109 return NULL;
110 }
111
112 } // namespace gin
113