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