NPRuntimeUtilities.cpp revision 65f03d4f644ce73618e5f4f50dd694b26f55ae12
1/*
2 * Copyright (C) 2010 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. AND ITS CONTRIBUTORS ``AS IS''
14 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
15 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
17 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
19 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
21 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
22 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
23 * THE POSSIBILITY OF SUCH DAMAGE.
24 */
25
26#include "NPRuntimeUtilities.h"
27
28#include <wtf/text/CString.h>
29
30namespace WebKit {
31
32void* npnMemAlloc(uint32_t size)
33{
34    // We could use fastMalloc here, but there might be plug-ins that mix NPN_MemAlloc/NPN_MemFree with malloc and free,
35    // so having them be equivalent seems like a good idea.
36    return malloc(size);
37}
38
39void npnMemFree(void* ptr)
40{
41    // We could use fastFree here, but there might be plug-ins that mix NPN_MemAlloc/NPN_MemFree with malloc and free,
42    // so having them be equivalent seems like a good idea.
43    free(ptr);
44}
45
46NPString createNPString(const CString& string)
47{
48    char* utf8Characters = npnMemNewArray<char>(string.length());
49    memcpy(utf8Characters, string.data(), string.length());
50
51    NPString npString;
52    npString.UTF8Characters = utf8Characters;
53    npString.UTF8Length = string.length();
54
55    return npString;
56}
57
58NPObject* createNPObject(NPP npp, NPClass* npClass)
59{
60    ASSERT(npClass);
61
62    NPObject* npObject;
63    if (npClass->allocate)
64        npObject = npClass->allocate(npp, npClass);
65    else
66        npObject = npnMemNew<NPObject>();
67
68    npObject->_class = npClass;
69    npObject->referenceCount = 1;
70
71    return npObject;
72}
73
74void deallocateNPObject(NPObject* npObject)
75{
76    ASSERT(npObject);
77    if (!npObject)
78        return;
79
80    if (npObject->_class->deallocate)
81        npObject->_class->deallocate(npObject);
82    else
83        npnMemFree(npObject);
84}
85
86void retainNPObject(NPObject* npObject)
87{
88    ASSERT(npObject);
89    if (!npObject)
90        return;
91
92    npObject->referenceCount++;
93}
94
95void releaseNPObject(NPObject* npObject)
96{
97    ASSERT(npObject);
98    if (!npObject)
99        return;
100
101    ASSERT(npObject->referenceCount >= 1);
102    npObject->referenceCount--;
103    if (!npObject->referenceCount)
104        deallocateNPObject(npObject);
105}
106
107void releaseNPVariantValue(NPVariant* variant)
108{
109    ASSERT(variant);
110
111    switch (variant->type) {
112    case NPVariantType_Void:
113    case NPVariantType_Null:
114    case NPVariantType_Bool:
115    case NPVariantType_Int32:
116    case NPVariantType_Double:
117        // Nothing to do.
118        break;
119
120    case NPVariantType_String:
121        npnMemFree(const_cast<NPUTF8*>(variant->value.stringValue.UTF8Characters));
122        variant->value.stringValue.UTF8Characters = 0;
123        variant->value.stringValue.UTF8Length = 0;
124        break;
125    case NPVariantType_Object:
126        releaseNPObject(variant->value.objectValue);
127        variant->value.objectValue = 0;
128        break;
129    }
130
131    variant->type = NPVariantType_Void;
132}
133
134} // namespace WebKit
135