2 * Copyright (C) 2004, 2006 Apple Computer, Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
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.
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.
28 #if ENABLE(NETSCAPE_API)
30 #include "npruntime_internal.h"
31 #include "npruntime_impl.h"
32 #include "npruntime_priv.h"
35 #include "c_utility.h"
36 #include "identifier.h"
37 #include <wtf/Assertions.h>
38 #include <wtf/HashMap.h>
40 using namespace KJS::Bindings
;
42 typedef HashMap
<RefPtr
<KJS::UString::Rep
>, PrivateIdentifier
*> StringIdentifierMap
;
44 static StringIdentifierMap
* getStringIdentifierMap()
46 static StringIdentifierMap
* stringIdentifierMap
= 0;
47 if (!stringIdentifierMap
)
48 stringIdentifierMap
= new StringIdentifierMap
;
49 return stringIdentifierMap
;
52 typedef HashMap
<int, PrivateIdentifier
*> IntIdentifierMap
;
54 static IntIdentifierMap
* getIntIdentifierMap()
56 static IntIdentifierMap
* intIdentifierMap
= 0;
57 if (!intIdentifierMap
)
58 intIdentifierMap
= new IntIdentifierMap
;
59 return intIdentifierMap
;
62 NPIdentifier
_NPN_GetStringIdentifier(const NPUTF8
* name
)
67 PrivateIdentifier
* identifier
= 0;
71 identifier
= getStringIdentifierMap()->get(identifierFromNPIdentifier(name
).ustring().rep());
72 if (identifier
== 0) {
73 identifier
= (PrivateIdentifier
*)malloc(sizeof(PrivateIdentifier
));
74 // We never release identifier names, so this dictionary will grow, as will
75 // the memory for the identifier name strings.
76 identifier
->isString
= true;
77 identifier
->value
.string
= strdup(name
);
79 getStringIdentifierMap()->set(identifierFromNPIdentifier(name
).ustring().rep(), identifier
);
81 return (NPIdentifier
)identifier
;
87 void _NPN_GetStringIdentifiers(const NPUTF8
** names
, int32_t nameCount
, NPIdentifier
* identifiers
)
92 if (names
&& identifiers
)
93 for (int i
= 0; i
< nameCount
; i
++)
94 identifiers
[i
] = _NPN_GetStringIdentifier(names
[i
]);
97 NPIdentifier
_NPN_GetIntIdentifier(int32_t intid
)
99 PrivateIdentifier
* identifier
;
101 if (intid
== 0 || intid
== -1) {
102 static PrivateIdentifier
* negativeOneAndZeroIdentifiers
[2];
104 identifier
= negativeOneAndZeroIdentifiers
[intid
+ 1];
106 identifier
= (PrivateIdentifier
*)malloc(sizeof(PrivateIdentifier
));
107 identifier
->isString
= false;
108 identifier
->value
.number
= intid
;
110 negativeOneAndZeroIdentifiers
[intid
+ 1] = identifier
;
113 identifier
= getIntIdentifierMap()->get(intid
);
115 identifier
= (PrivateIdentifier
*)malloc(sizeof(PrivateIdentifier
));
116 // We never release identifier names, so this dictionary will grow.
117 identifier
->isString
= false;
118 identifier
->value
.number
= intid
;
120 getIntIdentifierMap()->set(intid
, identifier
);
123 return (NPIdentifier
)identifier
;
126 bool _NPN_IdentifierIsString(NPIdentifier identifier
)
128 PrivateIdentifier
* i
= (PrivateIdentifier
*)identifier
;
132 NPUTF8
*_NPN_UTF8FromIdentifier(NPIdentifier identifier
)
134 PrivateIdentifier
* i
= (PrivateIdentifier
*)identifier
;
135 if (!i
->isString
|| !i
->value
.string
)
138 return (NPUTF8
*)strdup(i
->value
.string
);
141 int32_t _NPN_IntFromIdentifier(NPIdentifier identifier
)
143 PrivateIdentifier
* i
= (PrivateIdentifier
*)identifier
;
146 return i
->value
.number
;
149 void NPN_InitializeVariantWithStringCopy(NPVariant
* variant
, const NPString
* value
)
151 variant
->type
= NPVariantType_String
;
152 variant
->value
.stringValue
.UTF8Length
= value
->UTF8Length
;
153 variant
->value
.stringValue
.UTF8Characters
= (NPUTF8
*)malloc(sizeof(NPUTF8
) * value
->UTF8Length
);
154 memcpy((void*)variant
->value
.stringValue
.UTF8Characters
, value
->UTF8Characters
, sizeof(NPUTF8
) * value
->UTF8Length
);
157 void _NPN_ReleaseVariantValue(NPVariant
* variant
)
161 if (variant
->type
== NPVariantType_Object
) {
162 _NPN_ReleaseObject(variant
->value
.objectValue
);
163 variant
->value
.objectValue
= 0;
164 } else if (variant
->type
== NPVariantType_String
) {
165 free((void*)variant
->value
.stringValue
.UTF8Characters
);
166 variant
->value
.stringValue
.UTF8Characters
= 0;
167 variant
->value
.stringValue
.UTF8Length
= 0;
170 variant
->type
= NPVariantType_Void
;
173 NPObject
*_NPN_CreateObject(NPP npp
, NPClass
* aClass
)
179 if (aClass
->allocate
!= NULL
)
180 obj
= aClass
->allocate(npp
, aClass
);
182 obj
= (NPObject
*)malloc(sizeof(NPObject
));
184 obj
->_class
= aClass
;
185 obj
->referenceCount
= 1;
193 NPObject
* _NPN_RetainObject(NPObject
* obj
)
198 obj
->referenceCount
++;
203 void _NPN_ReleaseObject(NPObject
* obj
)
206 ASSERT(obj
->referenceCount
>= 1);
208 if (obj
&& obj
->referenceCount
>= 1) {
209 if (--obj
->referenceCount
== 0)
210 _NPN_DeallocateObject(obj
);
214 void _NPN_DeallocateObject(NPObject
*obj
)
219 if (obj
->_class
->deallocate
)
220 obj
->_class
->deallocate(obj
);
226 #endif // ENABLE(NETSCAPE_API)