1 /*
2 * Copyright (C) 2004, 2006 Apple Computer, Inc. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 *
13 * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR
17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24 */
25
26 #include "config.h"
27
28 #if ENABLE(NETSCAPE_PLUGIN_API)
29
30 #include "npruntime_internal.h"
31 #include "npruntime_impl.h"
32 #include "npruntime_priv.h"
33
34 #include "c_utility.h"
35 #include <runtime/Identifier.h>
36 #include <runtime/JSLock.h>
37 #include <wtf/Assertions.h>
38 #include <wtf/HashMap.h>
39
40 using namespace JSC::Bindings;
41
42 typedef HashMap<RefPtr<JSC::UString::Rep>, PrivateIdentifier*> StringIdentifierMap;
43
getStringIdentifierMap()44 static StringIdentifierMap* getStringIdentifierMap()
45 {
46 static StringIdentifierMap* stringIdentifierMap = 0;
47 if (!stringIdentifierMap)
48 stringIdentifierMap = new StringIdentifierMap;
49 return stringIdentifierMap;
50 }
51
52 typedef HashMap<int, PrivateIdentifier*> IntIdentifierMap;
53
getIntIdentifierMap()54 static IntIdentifierMap* getIntIdentifierMap()
55 {
56 static IntIdentifierMap* intIdentifierMap = 0;
57 if (!intIdentifierMap)
58 intIdentifierMap = new IntIdentifierMap;
59 return intIdentifierMap;
60 }
61
_NPN_GetStringIdentifier(const NPUTF8 * name)62 NPIdentifier _NPN_GetStringIdentifier(const NPUTF8* name)
63 {
64 ASSERT(name);
65
66 if (name) {
67 PrivateIdentifier* identifier = 0;
68
69 JSC::JSLock lock(false);
70
71 identifier = getStringIdentifierMap()->get(identifierFromNPIdentifier(name).ustring().rep());
72 if (identifier == 0) {
73 identifier = (PrivateIdentifier*)malloc(sizeof(PrivateIdentifier));
74 if (!identifier)
75 CRASH();
76 // We never release identifier names, so this dictionary will grow, as will
77 // the memory for the identifier name strings.
78 identifier->isString = true;
79 identifier->value.string = strdup(name);
80
81 getStringIdentifierMap()->set(identifierFromNPIdentifier(name).ustring().rep(), identifier);
82 }
83 return (NPIdentifier)identifier;
84 }
85
86 return 0;
87 }
88
_NPN_GetStringIdentifiers(const NPUTF8 ** names,int32_t nameCount,NPIdentifier * identifiers)89 void _NPN_GetStringIdentifiers(const NPUTF8** names, int32_t nameCount, NPIdentifier* identifiers)
90 {
91 ASSERT(names);
92 ASSERT(identifiers);
93
94 if (names && identifiers)
95 for (int i = 0; i < nameCount; i++)
96 identifiers[i] = _NPN_GetStringIdentifier(names[i]);
97 }
98
_NPN_GetIntIdentifier(int32_t intid)99 NPIdentifier _NPN_GetIntIdentifier(int32_t intid)
100 {
101 PrivateIdentifier* identifier;
102
103 if (intid == 0 || intid == -1) {
104 static PrivateIdentifier* negativeOneAndZeroIdentifiers[2];
105
106 identifier = negativeOneAndZeroIdentifiers[intid + 1];
107 if (!identifier) {
108 identifier = (PrivateIdentifier*)malloc(sizeof(PrivateIdentifier));
109 if (!identifier)
110 CRASH();
111 identifier->isString = false;
112 identifier->value.number = intid;
113
114 negativeOneAndZeroIdentifiers[intid + 1] = identifier;
115 }
116 } else {
117 identifier = getIntIdentifierMap()->get(intid);
118 if (!identifier) {
119 identifier = (PrivateIdentifier*)malloc(sizeof(PrivateIdentifier));
120 if (!identifier)
121 CRASH();
122 // We never release identifier names, so this dictionary will grow.
123 identifier->isString = false;
124 identifier->value.number = intid;
125
126 getIntIdentifierMap()->set(intid, identifier);
127 }
128 }
129 return (NPIdentifier)identifier;
130 }
131
_NPN_IdentifierIsString(NPIdentifier identifier)132 bool _NPN_IdentifierIsString(NPIdentifier identifier)
133 {
134 PrivateIdentifier* i = (PrivateIdentifier*)identifier;
135 return i->isString;
136 }
137
_NPN_UTF8FromIdentifier(NPIdentifier identifier)138 NPUTF8 *_NPN_UTF8FromIdentifier(NPIdentifier identifier)
139 {
140 PrivateIdentifier* i = (PrivateIdentifier*)identifier;
141 if (!i->isString || !i->value.string)
142 return NULL;
143
144 return (NPUTF8 *)strdup(i->value.string);
145 }
146
_NPN_IntFromIdentifier(NPIdentifier identifier)147 int32_t _NPN_IntFromIdentifier(NPIdentifier identifier)
148 {
149 PrivateIdentifier* i = (PrivateIdentifier*)identifier;
150 if (i->isString)
151 return 0;
152 return i->value.number;
153 }
154
NPN_InitializeVariantWithStringCopy(NPVariant * variant,const NPString * value)155 void NPN_InitializeVariantWithStringCopy(NPVariant* variant, const NPString* value)
156 {
157 variant->type = NPVariantType_String;
158 variant->value.stringValue.UTF8Length = value->UTF8Length;
159 variant->value.stringValue.UTF8Characters = (NPUTF8 *)malloc(sizeof(NPUTF8) * value->UTF8Length);
160 if (!variant->value.stringValue.UTF8Characters)
161 CRASH();
162 memcpy((void*)variant->value.stringValue.UTF8Characters, value->UTF8Characters, sizeof(NPUTF8) * value->UTF8Length);
163 }
164
_NPN_ReleaseVariantValue(NPVariant * variant)165 void _NPN_ReleaseVariantValue(NPVariant* variant)
166 {
167 ASSERT(variant);
168
169 if (variant->type == NPVariantType_Object) {
170 _NPN_ReleaseObject(variant->value.objectValue);
171 variant->value.objectValue = 0;
172 } else if (variant->type == NPVariantType_String) {
173 free((void*)variant->value.stringValue.UTF8Characters);
174 variant->value.stringValue.UTF8Characters = 0;
175 variant->value.stringValue.UTF8Length = 0;
176 }
177
178 variant->type = NPVariantType_Void;
179 }
180
_NPN_CreateObject(NPP npp,NPClass * aClass)181 NPObject *_NPN_CreateObject(NPP npp, NPClass* aClass)
182 {
183 ASSERT(aClass);
184
185 if (aClass) {
186 NPObject* obj;
187 if (aClass->allocate != NULL)
188 obj = aClass->allocate(npp, aClass);
189 else
190 obj = (NPObject*)malloc(sizeof(NPObject));
191 if (!obj)
192 CRASH();
193 obj->_class = aClass;
194 obj->referenceCount = 1;
195
196 return obj;
197 }
198
199 return 0;
200 }
201
_NPN_RetainObject(NPObject * obj)202 NPObject* _NPN_RetainObject(NPObject* obj)
203 {
204 ASSERT(obj);
205
206 if (obj)
207 obj->referenceCount++;
208
209 return obj;
210 }
211
_NPN_ReleaseObject(NPObject * obj)212 void _NPN_ReleaseObject(NPObject* obj)
213 {
214 ASSERT(obj);
215 ASSERT(obj->referenceCount >= 1);
216
217 if (obj && obj->referenceCount >= 1) {
218 if (--obj->referenceCount == 0)
219 _NPN_DeallocateObject(obj);
220 }
221 }
222
_NPN_DeallocateObject(NPObject * obj)223 void _NPN_DeallocateObject(NPObject *obj)
224 {
225 ASSERT(obj);
226
227 if (obj) {
228 if (obj->_class->deallocate)
229 obj->_class->deallocate(obj);
230 else
231 free(obj);
232 }
233 }
234
235 #endif // ENABLE(NETSCAPE_PLUGIN_API)
236