1/*
2 * Copyright (C) 2004, 2006 Apple 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 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 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 "IdentifierRep.h"
31#include "npruntime_internal.h"
32#include "npruntime_priv.h"
33
34#include "c_utility.h"
35#include <JavaScriptCore/Identifier.h>
36#include <wtf/Assertions.h>
37
38#pragma GCC visibility push(default)
39#include "npruntime_impl.h"
40#pragma GCC visibility pop
41
42using namespace WebCore;
43
44NPIdentifier _NPN_GetStringIdentifier(const NPUTF8* name)
45{
46 return static_cast<NPIdentifier>(IdentifierRep::get(name));
47}
48
49void _NPN_GetStringIdentifiers(const NPUTF8** names, int32_t nameCount, NPIdentifier* identifiers)
50{
51 ASSERT(names);
52 ASSERT(identifiers);
53
54 if (names && identifiers) {
55 for (int i = 0; i < nameCount; i++)
56 identifiers[i] = _NPN_GetStringIdentifier(names[i]);
57 }
58}
59
60NPIdentifier _NPN_GetIntIdentifier(int32_t intid)
61{
62 return static_cast<NPIdentifier>(IdentifierRep::get(intid));
63}
64
65bool _NPN_IdentifierIsString(NPIdentifier identifier)
66{
67 return static_cast<IdentifierRep*>(identifier)->isString();
68}
69
70NPUTF8 *_NPN_UTF8FromIdentifier(NPIdentifier identifier)
71{
72 const char* string = static_cast<IdentifierRep*>(identifier)->string();
73 if (!string)
74 return 0;
75
76 return strdup(string);
77}
78
79int32_t _NPN_IntFromIdentifier(NPIdentifier identifier)
80{
81 return static_cast<IdentifierRep*>(identifier)->number();
82}
83
84void NPN_InitializeVariantWithStringCopy(NPVariant* variant, const NPString* value)
85{
86 variant->type = NPVariantType_String;
87 variant->value.stringValue.UTF8Length = value->UTF8Length;
88 // Switching to fastMalloc would be better to avoid length check but this is not desirable
89 // as NPN_MemAlloc is using malloc and there might be plugins that mix NPN_MemAlloc and malloc too.
90 variant->value.stringValue.UTF8Characters = (NPUTF8*)malloc(sizeof(NPUTF8) * value->UTF8Length);
91 if (value->UTF8Length && !variant->value.stringValue.UTF8Characters)
92 CRASH();
93 memcpy((void*)variant->value.stringValue.UTF8Characters, value->UTF8Characters, sizeof(NPUTF8) * value->UTF8Length);
94}
95
96void _NPN_ReleaseVariantValue(NPVariant* variant)
97{
98 ASSERT(variant);
99
100 if (variant->type == NPVariantType_Object) {
101 _NPN_ReleaseObject(variant->value.objectValue);
102 variant->value.objectValue = 0;
103 } else if (variant->type == NPVariantType_String) {
104 free((void*)variant->value.stringValue.UTF8Characters);
105 variant->value.stringValue.UTF8Characters = 0;
106 variant->value.stringValue.UTF8Length = 0;
107 }
108
109 variant->type = NPVariantType_Void;
110}
111
112NPObject *_NPN_CreateObject(NPP npp, NPClass* aClass)
113{
114 ASSERT(aClass);
115
116 if (aClass) {
117 NPObject* obj;
118 if (aClass->allocate != NULL)
119 obj = aClass->allocate(npp, aClass);
120 else
121 obj = (NPObject*)malloc(sizeof(NPObject));
122 if (!obj)
123 CRASH();
124 obj->_class = aClass;
125 obj->referenceCount = 1;
126
127 return obj;
128 }
129
130 return 0;
131}
132
133NPObject* _NPN_RetainObject(NPObject* obj)
134{
135 ASSERT(obj);
136
137 if (obj)
138 obj->referenceCount++;
139
140 return obj;
141}
142
143void _NPN_ReleaseObject(NPObject* obj)
144{
145 ASSERT(obj);
146 ASSERT(obj->referenceCount >= 1);
147
148 if (obj && obj->referenceCount >= 1) {
149 if (--obj->referenceCount == 0)
150 _NPN_DeallocateObject(obj);
151 }
152}
153
154void _NPN_DeallocateObject(NPObject *obj)
155{
156 ASSERT(obj);
157
158 if (obj) {
159 if (obj->_class->deallocate)
160 obj->_class->deallocate(obj);
161 else
162 free(obj);
163 }
164}
165
166#endif // ENABLE(NETSCAPE_PLUGIN_API)
167