OLD | NEW |
1 /* | 1 /* |
2 * Copyright (C) 2012 Google Inc. All rights reserved. | 2 * Copyright (C) 2012 Google Inc. All rights reserved. |
3 * | 3 * |
4 * Redistribution and use in source and binary forms, with or without | 4 * Redistribution and use in source and binary forms, with or without |
5 * modification, are permitted provided that the following conditions | 5 * modification, are permitted provided that the following conditions |
6 * are met: | 6 * are met: |
7 * | 7 * |
8 * 1. Redistributions of source code must retain the above copyright | 8 * 1. Redistributions of source code must retain the above copyright |
9 * notice, this list of conditions and the following disclaimer. | 9 * notice, this list of conditions and the following disclaimer. |
10 * 2. Redistributions in binary form must reproduce the above copyright | 10 * 2. Redistributions in binary form must reproduce the above copyright |
(...skipping 16 matching lines...) Expand all Loading... |
27 */ | 27 */ |
28 | 28 |
29 #include "config.h" | 29 #include "config.h" |
30 #include "bindings/core/v8/V8DOMConfiguration.h" | 30 #include "bindings/core/v8/V8DOMConfiguration.h" |
31 | 31 |
32 #include "bindings/core/v8/V8ObjectConstructor.h" | 32 #include "bindings/core/v8/V8ObjectConstructor.h" |
33 #include "platform/TraceEvent.h" | 33 #include "platform/TraceEvent.h" |
34 | 34 |
35 namespace blink { | 35 namespace blink { |
36 | 36 |
| 37 namespace { |
| 38 |
| 39 template<class ObjectOrTemplate> |
| 40 void installAttributeInternal(v8::Isolate* isolate, v8::Handle<ObjectOrTemplate>
instanceTemplate, v8::Handle<ObjectOrTemplate> prototype, const V8DOMConfigurat
ion::AttributeConfiguration& attribute, const DOMWrapperWorld& world) |
| 41 { |
| 42 if (attribute.exposeConfiguration == V8DOMConfiguration::OnlyExposedToPrivat
eScript |
| 43 && !world.isPrivateScriptIsolatedWorld()) |
| 44 return; |
| 45 |
| 46 v8::AccessorGetterCallback getter = attribute.getter; |
| 47 v8::AccessorSetterCallback setter = attribute.setter; |
| 48 if (world.isMainWorld()) { |
| 49 if (attribute.getterForMainWorld) |
| 50 getter = attribute.getterForMainWorld; |
| 51 if (attribute.setterForMainWorld) |
| 52 setter = attribute.setterForMainWorld; |
| 53 } |
| 54 v8::Handle<ObjectOrTemplate> target = |
| 55 attribute.instanceOrPrototypeConfiguration == V8DOMConfiguration::OnProt
otype ? |
| 56 prototype : |
| 57 instanceTemplate; |
| 58 target->SetAccessor( |
| 59 v8AtomicString(isolate, attribute.name), |
| 60 getter, |
| 61 setter, |
| 62 v8::External::New(isolate, const_cast<WrapperTypeInfo*>(attribute.data))
, |
| 63 attribute.settings, |
| 64 attribute.attribute); |
| 65 } |
| 66 |
| 67 void installAccessorInternal(v8::Isolate* isolate, v8::Handle<v8::ObjectTemplate
> prototype, v8::Handle<v8::Signature> signature, const V8DOMConfiguration::Acce
ssorConfiguration& accessor, const DOMWrapperWorld& world) |
| 68 { |
| 69 if (accessor.exposeConfiguration == V8DOMConfiguration::OnlyExposedToPrivate
Script |
| 70 && !world.isPrivateScriptIsolatedWorld()) |
| 71 return; |
| 72 |
| 73 v8::FunctionCallback getterCallback = accessor.getter; |
| 74 v8::FunctionCallback setterCallback = accessor.setter; |
| 75 if (world.isMainWorld()) { |
| 76 if (accessor.getterForMainWorld) |
| 77 getterCallback = accessor.getterForMainWorld; |
| 78 if (accessor.setterForMainWorld) |
| 79 setterCallback = accessor.setterForMainWorld; |
| 80 } |
| 81 v8::Local<v8::FunctionTemplate> getter; |
| 82 if (getterCallback) { |
| 83 getter = v8::FunctionTemplate::New(isolate, getterCallback, v8::External
::New(isolate, const_cast<WrapperTypeInfo*>(accessor.data)), signature, 0); |
| 84 getter->RemovePrototype(); |
| 85 } |
| 86 v8::Local<v8::FunctionTemplate> setter; |
| 87 if (setterCallback) { |
| 88 setter = v8::FunctionTemplate::New(isolate, setterCallback, v8::External
::New(isolate, const_cast<WrapperTypeInfo*>(accessor.data)), signature, 1); |
| 89 setter->RemovePrototype(); |
| 90 } |
| 91 prototype->SetAccessorProperty( |
| 92 v8AtomicString(isolate, accessor.name), |
| 93 getter, |
| 94 setter, |
| 95 accessor.attribute, |
| 96 accessor.settings); |
| 97 } |
| 98 |
| 99 void installConstantInternal(v8::Isolate* isolate, v8::Handle<v8::FunctionTempla
te> functionDescriptor, v8::Handle<v8::ObjectTemplate> prototype, const V8DOMCon
figuration::ConstantConfiguration& constant) |
| 100 { |
| 101 v8::Handle<v8::String> constantName = v8AtomicString(isolate, constant.name)
; |
| 102 v8::PropertyAttribute attributes = |
| 103 static_cast<v8::PropertyAttribute>(v8::ReadOnly | v8::DontDelete); |
| 104 v8::Handle<v8::Primitive> value; |
| 105 switch (constant.type) { |
| 106 case V8DOMConfiguration::ConstantTypeShort: |
| 107 case V8DOMConfiguration::ConstantTypeLong: |
| 108 case V8DOMConfiguration::ConstantTypeUnsignedShort: |
| 109 value = v8::Integer::New(isolate, constant.ivalue); |
| 110 break; |
| 111 case V8DOMConfiguration::ConstantTypeUnsignedLong: |
| 112 value = v8::Integer::NewFromUnsigned(isolate, constant.ivalue); |
| 113 break; |
| 114 case V8DOMConfiguration::ConstantTypeFloat: |
| 115 case V8DOMConfiguration::ConstantTypeDouble: |
| 116 value = v8::Number::New(isolate, constant.dvalue); |
| 117 break; |
| 118 case V8DOMConfiguration::ConstantTypeString: |
| 119 value = v8::String::NewFromUtf8(isolate, constant.svalue); |
| 120 break; |
| 121 default: |
| 122 ASSERT_NOT_REACHED(); |
| 123 } |
| 124 functionDescriptor->Set(constantName, value, attributes); |
| 125 prototype->Set(constantName, value, attributes); |
| 126 } |
| 127 |
| 128 void doInstallMethodInternal(v8::Handle<v8::ObjectTemplate> target, v8::Handle<v
8::Name> name, v8::Handle<v8::FunctionTemplate> functionTemplate, v8::PropertyAt
tribute attribute) |
| 129 { |
| 130 target->Set(name, functionTemplate, attribute); |
| 131 } |
| 132 |
| 133 void doInstallMethodInternal(v8::Handle<v8::FunctionTemplate> target, v8::Handle
<v8::Name> name, v8::Handle<v8::FunctionTemplate> functionTemplate, v8::Property
Attribute attribute) |
| 134 { |
| 135 target->Set(name, functionTemplate, attribute); |
| 136 } |
| 137 |
| 138 template<class ObjectOrTemplate, class Configuration> |
| 139 void installMethodInternal(v8::Isolate* isolate, v8::Handle<ObjectOrTemplate> ob
jectOrTemplate, v8::Handle<v8::Signature> signature, v8::PropertyAttribute attri
bute, const Configuration& callback, const DOMWrapperWorld& world) |
| 140 { |
| 141 if (callback.exposeConfiguration == V8DOMConfiguration::OnlyExposedToPrivate
Script |
| 142 && !world.isPrivateScriptIsolatedWorld()) |
| 143 return; |
| 144 |
| 145 v8::Local<v8::FunctionTemplate> functionTemplate = v8::FunctionTemplate::New
(isolate, callback.callbackForWorld(world), v8Undefined(), signature, callback.l
ength); |
| 146 functionTemplate->RemovePrototype(); |
| 147 doInstallMethodInternal(objectOrTemplate, callback.methodName(isolate), func
tionTemplate, attribute); |
| 148 } |
| 149 |
| 150 } // namespace |
| 151 |
37 void V8DOMConfiguration::installAttributes(v8::Isolate* isolate, v8::Handle<v8::
ObjectTemplate> instanceTemplate, v8::Handle<v8::ObjectTemplate> prototype, cons
t AttributeConfiguration* attributes, size_t attributeCount) | 152 void V8DOMConfiguration::installAttributes(v8::Isolate* isolate, v8::Handle<v8::
ObjectTemplate> instanceTemplate, v8::Handle<v8::ObjectTemplate> prototype, cons
t AttributeConfiguration* attributes, size_t attributeCount) |
38 { | 153 { |
| 154 const DOMWrapperWorld& world = DOMWrapperWorld::current(isolate); |
39 for (size_t i = 0; i < attributeCount; ++i) | 155 for (size_t i = 0; i < attributeCount; ++i) |
40 installAttribute(instanceTemplate, prototype, attributes[i], isolate); | 156 installAttributeInternal(isolate, instanceTemplate, prototype, attribute
s[i], world); |
| 157 } |
| 158 |
| 159 void V8DOMConfiguration::installAttribute(v8::Isolate* isolate, v8::Handle<v8::O
bjectTemplate> instanceTemplate, v8::Handle<v8::ObjectTemplate> prototype, const
AttributeConfiguration& attribute) |
| 160 { |
| 161 const DOMWrapperWorld& world = DOMWrapperWorld::current(isolate); |
| 162 installAttributeInternal(isolate, instanceTemplate, prototype, attribute, wo
rld); |
| 163 } |
| 164 |
| 165 void V8DOMConfiguration::installAttribute(v8::Isolate* isolate, v8::Handle<v8::O
bject> instanceTemplate, v8::Handle<v8::Object> prototype, const AttributeConfig
uration& attribute) |
| 166 { |
| 167 const DOMWrapperWorld& world = DOMWrapperWorld::current(isolate); |
| 168 installAttributeInternal(isolate, instanceTemplate, prototype, attribute, wo
rld); |
41 } | 169 } |
42 | 170 |
43 void V8DOMConfiguration::installAccessors(v8::Isolate* isolate, v8::Handle<v8::O
bjectTemplate> prototype, v8::Handle<v8::Signature> signature, const AccessorCon
figuration* accessors, size_t accessorCount) | 171 void V8DOMConfiguration::installAccessors(v8::Isolate* isolate, v8::Handle<v8::O
bjectTemplate> prototype, v8::Handle<v8::Signature> signature, const AccessorCon
figuration* accessors, size_t accessorCount) |
44 { | 172 { |
45 DOMWrapperWorld& world = DOMWrapperWorld::current(isolate); | 173 const DOMWrapperWorld& world = DOMWrapperWorld::current(isolate); |
46 for (size_t i = 0; i < accessorCount; ++i) { | 174 for (size_t i = 0; i < accessorCount; ++i) |
47 if (accessors[i].exposeConfiguration == OnlyExposedToPrivateScript && !w
orld.isPrivateScriptIsolatedWorld()) | 175 installAccessorInternal(isolate, prototype, signature, accessors[i], wor
ld); |
48 continue; | |
49 | |
50 v8::FunctionCallback getterCallback = accessors[i].getter; | |
51 v8::FunctionCallback setterCallback = accessors[i].setter; | |
52 if (world.isMainWorld()) { | |
53 if (accessors[i].getterForMainWorld) | |
54 getterCallback = accessors[i].getterForMainWorld; | |
55 if (accessors[i].setterForMainWorld) | |
56 setterCallback = accessors[i].setterForMainWorld; | |
57 } | |
58 | |
59 v8::Local<v8::FunctionTemplate> getter; | |
60 if (getterCallback) { | |
61 getter = v8::FunctionTemplate::New(isolate, getterCallback, v8::Exte
rnal::New(isolate, const_cast<WrapperTypeInfo*>(accessors[i].data)), signature,
0); | |
62 getter->RemovePrototype(); | |
63 } | |
64 v8::Local<v8::FunctionTemplate> setter; | |
65 if (setterCallback) { | |
66 setter = v8::FunctionTemplate::New(isolate, setterCallback, v8::Exte
rnal::New(isolate, const_cast<WrapperTypeInfo*>(accessors[i].data)), signature,
1); | |
67 setter->RemovePrototype(); | |
68 } | |
69 prototype->SetAccessorProperty(v8AtomicString(isolate, accessors[i].name
), getter, setter, accessors[i].attribute, accessors[i].settings); | |
70 } | |
71 } | 176 } |
72 | 177 |
73 // Constant installation | 178 void V8DOMConfiguration::installAccessor(v8::Isolate* isolate, v8::Handle<v8::Ob
jectTemplate> prototype, v8::Handle<v8::Signature> signature, const AccessorConf
iguration& accessor) |
74 // | 179 { |
75 // installConstants() is be used for simple constants. It installs constants | 180 const DOMWrapperWorld& world = DOMWrapperWorld::current(isolate); |
76 // using v8::Template::Set(), which results in a property that is much faster to | 181 installAccessorInternal(isolate, prototype, signature, accessor, world); |
77 // access from scripts. | 182 } |
78 // installConstant() is used when some C++ code needs to be executed when the | |
79 // constant is accessed, e.g. to handle deprecation or measuring usage. The | |
80 // property appears the same to scripts, but is slower to access. | |
81 | 183 |
82 void V8DOMConfiguration::installConstants(v8::Isolate* isolate, v8::Handle<v8::F
unctionTemplate> functionDescriptor, v8::Handle<v8::ObjectTemplate> prototype, c
onst ConstantConfiguration* constants, size_t constantCount) | 184 void V8DOMConfiguration::installConstants(v8::Isolate* isolate, v8::Handle<v8::F
unctionTemplate> functionDescriptor, v8::Handle<v8::ObjectTemplate> prototype, c
onst ConstantConfiguration* constants, size_t constantCount) |
83 { | 185 { |
84 for (size_t i = 0; i < constantCount; ++i) { | 186 for (size_t i = 0; i < constantCount; ++i) |
85 const ConstantConfiguration* constant = &constants[i]; | 187 installConstantInternal(isolate, functionDescriptor, prototype, constant
s[i]); |
86 v8::Handle<v8::String> constantName = v8AtomicString(isolate, constant->
name); | |
87 switch (constant->type) { | |
88 case ConstantTypeShort: | |
89 case ConstantTypeLong: | |
90 case ConstantTypeUnsignedShort: | |
91 functionDescriptor->Set(constantName, v8::Integer::New(isolate, cons
tant->ivalue), static_cast<v8::PropertyAttribute>(v8::ReadOnly | v8::DontDelete)
); | |
92 prototype->Set(constantName, v8::Integer::New(isolate, constant->iva
lue), static_cast<v8::PropertyAttribute>(v8::ReadOnly | v8::DontDelete)); | |
93 break; | |
94 case ConstantTypeUnsignedLong: | |
95 functionDescriptor->Set(constantName, v8::Integer::NewFromUnsigned(i
solate, constant->ivalue), static_cast<v8::PropertyAttribute>(v8::ReadOnly | v8:
:DontDelete)); | |
96 prototype->Set(constantName, v8::Integer::NewFromUnsigned(isolate, c
onstant->ivalue), static_cast<v8::PropertyAttribute>(v8::ReadOnly | v8::DontDele
te)); | |
97 break; | |
98 case ConstantTypeFloat: | |
99 case ConstantTypeDouble: | |
100 functionDescriptor->Set(constantName, v8::Number::New(isolate, const
ant->dvalue), static_cast<v8::PropertyAttribute>(v8::ReadOnly | v8::DontDelete))
; | |
101 prototype->Set(constantName, v8::Number::New(isolate, constant->dval
ue), static_cast<v8::PropertyAttribute>(v8::ReadOnly | v8::DontDelete)); | |
102 break; | |
103 case ConstantTypeString: | |
104 functionDescriptor->Set(constantName, v8::String::NewFromUtf8(isolat
e, constant->svalue), static_cast<v8::PropertyAttribute>(v8::ReadOnly | v8::Dont
Delete)); | |
105 prototype->Set(constantName, v8::String::NewFromUtf8(isolate, consta
nt->svalue), static_cast<v8::PropertyAttribute>(v8::ReadOnly | v8::DontDelete)); | |
106 break; | |
107 default: | |
108 ASSERT_NOT_REACHED(); | |
109 } | |
110 } | |
111 } | 188 } |
112 | 189 |
113 void V8DOMConfiguration::installConstant(v8::Isolate* isolate, v8::Handle<v8::Fu
nctionTemplate> functionDescriptor, v8::Handle<v8::ObjectTemplate> prototype, co
nst char* name, v8::AccessorGetterCallback getter) | 190 void V8DOMConfiguration::installConstant(v8::Isolate* isolate, v8::Handle<v8::Fu
nctionTemplate> functionDescriptor, v8::Handle<v8::ObjectTemplate> prototype, co
nst ConstantConfiguration& constant) |
| 191 { |
| 192 installConstantInternal(isolate, functionDescriptor, prototype, constant); |
| 193 } |
| 194 |
| 195 void V8DOMConfiguration::installConstantWithGetter(v8::Isolate* isolate, v8::Han
dle<v8::FunctionTemplate> functionDescriptor, v8::Handle<v8::ObjectTemplate> pro
totype, const char* name, v8::AccessorGetterCallback getter) |
114 { | 196 { |
115 v8::Handle<v8::String> constantName = v8AtomicString(isolate, name); | 197 v8::Handle<v8::String> constantName = v8AtomicString(isolate, name); |
116 functionDescriptor->SetNativeDataProperty(constantName, getter, 0, v8::Handl
e<v8::Value>(), static_cast<v8::PropertyAttribute>(v8::ReadOnly | v8::DontDelete
)); | 198 v8::PropertyAttribute attributes = |
117 prototype->SetNativeDataProperty(constantName, getter, 0, v8::Handle<v8::Val
ue>(), static_cast<v8::PropertyAttribute>(v8::ReadOnly | v8::DontDelete)); | 199 static_cast<v8::PropertyAttribute>(v8::ReadOnly | v8::DontDelete); |
| 200 functionDescriptor->SetNativeDataProperty(constantName, getter, 0, v8::Handl
e<v8::Value>(), attributes); |
| 201 prototype->SetNativeDataProperty(constantName, getter, 0, v8::Handle<v8::Val
ue>(), attributes); |
118 } | 202 } |
119 | 203 |
120 void V8DOMConfiguration::installMethods(v8::Isolate* isolate, v8::Handle<v8::Obj
ectTemplate> prototype, v8::Handle<v8::Signature> signature, v8::PropertyAttribu
te attributes, const MethodConfiguration* callbacks, size_t callbackCount) | 204 void V8DOMConfiguration::installMethods(v8::Isolate* isolate, v8::Handle<v8::Obj
ectTemplate> prototype, v8::Handle<v8::Signature> signature, v8::PropertyAttribu
te attribute, const MethodConfiguration* callbacks, size_t callbackCount) |
121 { | 205 { |
| 206 const DOMWrapperWorld& world = DOMWrapperWorld::current(isolate); |
122 for (size_t i = 0; i < callbackCount; ++i) | 207 for (size_t i = 0; i < callbackCount; ++i) |
123 installMethod(prototype, signature, attributes, callbacks[i], isolate); | 208 installMethodInternal(isolate, prototype, signature, attribute, callback
s[i], world); |
124 } | 209 } |
125 | 210 |
126 v8::Handle<v8::FunctionTemplate> V8DOMConfiguration::functionTemplateForCallback
(v8::Isolate* isolate, v8::Handle<v8::Signature> signature, v8::FunctionCallback
callback, int length) | 211 void V8DOMConfiguration::installMethod(v8::Isolate* isolate, v8::Handle<v8::Func
tionTemplate> functionDescriptor, v8::Handle<v8::Signature> signature, v8::Prope
rtyAttribute attribute, const MethodConfiguration& callback) |
127 { | 212 { |
128 v8::Local<v8::FunctionTemplate> functionTemplate = v8::FunctionTemplate::New
(isolate, callback, v8Undefined(), signature, length); | 213 const DOMWrapperWorld& world = DOMWrapperWorld::current(isolate); |
129 functionTemplate->RemovePrototype(); | 214 installMethodInternal(isolate, functionDescriptor, signature, attribute, cal
lback, world); |
130 return functionTemplate; | 215 } |
| 216 |
| 217 void V8DOMConfiguration::installMethod(v8::Isolate* isolate, v8::Handle<v8::Obje
ctTemplate> prototype, v8::Handle<v8::Signature> signature, v8::PropertyAttribut
e attribute, const MethodConfiguration& callback) |
| 218 { |
| 219 const DOMWrapperWorld& world = DOMWrapperWorld::current(isolate); |
| 220 installMethodInternal(isolate, prototype, signature, attribute, callback, wo
rld); |
| 221 } |
| 222 |
| 223 void V8DOMConfiguration::installMethod(v8::Isolate* isolate, v8::Handle<v8::Obje
ctTemplate> prototype, v8::Handle<v8::Signature> signature, v8::PropertyAttribut
e attribute, const SymbolKeyedMethodConfiguration& callback) |
| 224 { |
| 225 const DOMWrapperWorld& world = DOMWrapperWorld::current(isolate); |
| 226 installMethodInternal(isolate, prototype, signature, attribute, callback, wo
rld); |
131 } | 227 } |
132 | 228 |
133 v8::Local<v8::Signature> V8DOMConfiguration::installDOMClassTemplate(v8::Isolate
* isolate, v8::Local<v8::FunctionTemplate> functionDescriptor, const char* inter
faceName, v8::Handle<v8::FunctionTemplate> parentClass, size_t fieldCount, | 229 v8::Local<v8::Signature> V8DOMConfiguration::installDOMClassTemplate(v8::Isolate
* isolate, v8::Local<v8::FunctionTemplate> functionDescriptor, const char* inter
faceName, v8::Handle<v8::FunctionTemplate> parentClass, size_t fieldCount, |
134 const AttributeConfiguration* attributes, size_t attributeCount, | 230 const AttributeConfiguration* attributes, size_t attributeCount, |
135 const AccessorConfiguration* accessors, size_t accessorCount, | 231 const AccessorConfiguration* accessors, size_t accessorCount, |
136 const MethodConfiguration* callbacks, size_t callbackCount) | 232 const MethodConfiguration* callbacks, size_t callbackCount) |
137 { | 233 { |
138 functionDescriptor->SetClassName(v8AtomicString(isolate, interfaceName)); | 234 functionDescriptor->SetClassName(v8AtomicString(isolate, interfaceName)); |
139 v8::Local<v8::ObjectTemplate> instanceTemplate = functionDescriptor->Instanc
eTemplate(); | 235 v8::Local<v8::ObjectTemplate> instanceTemplate = functionDescriptor->Instanc
eTemplate(); |
140 instanceTemplate->SetInternalFieldCount(fieldCount); | 236 instanceTemplate->SetInternalFieldCount(fieldCount); |
(...skipping 24 matching lines...) Expand all Loading... |
165 return result; | 261 return result; |
166 | 262 |
167 TRACE_EVENT_SCOPED_SAMPLING_STATE("blink", "BuildDOMTemplate"); | 263 TRACE_EVENT_SCOPED_SAMPLING_STATE("blink", "BuildDOMTemplate"); |
168 result = v8::FunctionTemplate::New(isolate, V8ObjectConstructor::isValidCons
tructorMode); | 264 result = v8::FunctionTemplate::New(isolate, V8ObjectConstructor::isValidCons
tructorMode); |
169 configureDOMClassTemplate(result, isolate); | 265 configureDOMClassTemplate(result, isolate); |
170 data->setDOMTemplate(wrapperTypeInfo, result); | 266 data->setDOMTemplate(wrapperTypeInfo, result); |
171 return result; | 267 return result; |
172 } | 268 } |
173 | 269 |
174 } // namespace blink | 270 } // namespace blink |
OLD | NEW |