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 "IdentifierRep.h"
31#include "npruntime_internal.h"
32#include "npruntime_impl.h"
33#include "npruntime_priv.h"
34
35#include "c_utility.h"
36#include <runtime/Identifier.h>
37#include <runtime/JSLock.h>
38#include <wtf/Assertions.h>
39#include <wtf/HashMap.h>
40
41using namespace JSC::Bindings;
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    variant->value.stringValue.UTF8Characters = (NPUTF8 *)malloc(sizeof(NPUTF8) * value->UTF8Length);
89    if (!variant->value.stringValue.UTF8Characters)
90        CRASH();
91    memcpy((void*)variant->value.stringValue.UTF8Characters, value->UTF8Characters, sizeof(NPUTF8) * value->UTF8Length);
92}
93
94void _NPN_ReleaseVariantValue(NPVariant* variant)
95{
96    ASSERT(variant);
97
98    if (variant->type == NPVariantType_Object) {
99        _NPN_ReleaseObject(variant->value.objectValue);
100        variant->value.objectValue = 0;
101    } else if (variant->type == NPVariantType_String) {
102        free((void*)variant->value.stringValue.UTF8Characters);
103        variant->value.stringValue.UTF8Characters = 0;
104        variant->value.stringValue.UTF8Length = 0;
105    }
106
107    variant->type = NPVariantType_Void;
108}
109
110NPObject *_NPN_CreateObject(NPP npp, NPClass* aClass)
111{
112    ASSERT(aClass);
113
114    if (aClass) {
115        NPObject* obj;
116        if (aClass->allocate != NULL)
117            obj = aClass->allocate(npp, aClass);
118        else
119            obj = (NPObject*)malloc(sizeof(NPObject));
120        if (!obj)
121            CRASH();
122        obj->_class = aClass;
123        obj->referenceCount = 1;
124
125        return obj;
126    }
127
128    return 0;
129}
130
131NPObject* _NPN_RetainObject(NPObject* obj)
132{
133    ASSERT(obj);
134
135    if (obj)
136        obj->referenceCount++;
137
138    return obj;
139}
140
141void _NPN_ReleaseObject(NPObject* obj)
142{
143    ASSERT(obj);
144    ASSERT(obj->referenceCount >= 1);
145
146    if (obj && obj->referenceCount >= 1) {
147        if (--obj->referenceCount == 0)
148            _NPN_DeallocateObject(obj);
149    }
150}
151
152void _NPN_DeallocateObject(NPObject *obj)
153{
154    ASSERT(obj);
155
156    if (obj) {
157        if (obj->_class->deallocate)
158            obj->_class->deallocate(obj);
159        else
160            free(obj);
161    }
162}
163
164#endif // ENABLE(NETSCAPE_PLUGIN_API)
165