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 "config.h" 27#include "NPRuntimeUtilities.h" 28 29#if ENABLE(NETSCAPE_PLUGIN_API) 30 31#include <string.h> 32#include <wtf/text/CString.h> 33 34namespace WebKit { 35 36void* npnMemAlloc(uint32_t size) 37{ 38 // We could use fastMalloc here, but there might be plug-ins that mix NPN_MemAlloc/NPN_MemFree with malloc and free, 39 // so having them be equivalent seems like a good idea. 40 return malloc(size); 41} 42 43void npnMemFree(void* ptr) 44{ 45 // We could use fastFree here, but there might be plug-ins that mix NPN_MemAlloc/NPN_MemFree with malloc and free, 46 // so having them be equivalent seems like a good idea. 47 free(ptr); 48} 49 50NPString createNPString(const CString& string) 51{ 52 char* utf8Characters = npnMemNewArray<char>(string.length()); 53 memcpy(utf8Characters, string.data(), string.length()); 54 55 NPString npString; 56 npString.UTF8Characters = utf8Characters; 57 npString.UTF8Length = string.length(); 58 59 return npString; 60} 61 62NPObject* createNPObject(NPP npp, NPClass* npClass) 63{ 64 ASSERT(npClass); 65 66 NPObject* npObject; 67 if (npClass->allocate) 68 npObject = npClass->allocate(npp, npClass); 69 else 70 npObject = npnMemNew<NPObject>(); 71 72 npObject->_class = npClass; 73 npObject->referenceCount = 1; 74 75 return npObject; 76} 77 78void deallocateNPObject(NPObject* npObject) 79{ 80 ASSERT(npObject); 81 if (!npObject) 82 return; 83 84 if (npObject->_class->deallocate) 85 npObject->_class->deallocate(npObject); 86 else 87 npnMemFree(npObject); 88} 89 90void retainNPObject(NPObject* npObject) 91{ 92 ASSERT(npObject); 93 if (!npObject) 94 return; 95 96 npObject->referenceCount++; 97} 98 99bool trySafeReleaseNPObject(NPObject* npObject) 100{ 101 ASSERT(npObject); 102 if (!npObject) 103 return true; 104 105 ASSERT(npObject->referenceCount >= 1); 106 107 npObject->referenceCount--; 108 if (npObject->referenceCount) 109 return true; 110 if (npObject->_class->deallocate) 111 return false; 112 deallocateNPObject(npObject); 113 return true; 114} 115 116void releaseNPObject(NPObject* npObject) 117{ 118 ASSERT(npObject); 119 if (!npObject) 120 return; 121 122 ASSERT(npObject->referenceCount >= 1); 123 npObject->referenceCount--; 124 if (!npObject->referenceCount) 125 deallocateNPObject(npObject); 126} 127 128void releaseNPVariantValue(NPVariant* variant) 129{ 130 ASSERT(variant); 131 132 switch (variant->type) { 133 case NPVariantType_Void: 134 case NPVariantType_Null: 135 case NPVariantType_Bool: 136 case NPVariantType_Int32: 137 case NPVariantType_Double: 138 // Nothing to do. 139 break; 140 141 case NPVariantType_String: 142 npnMemFree(const_cast<NPUTF8*>(variant->value.stringValue.UTF8Characters)); 143 variant->value.stringValue.UTF8Characters = 0; 144 variant->value.stringValue.UTF8Length = 0; 145 break; 146 case NPVariantType_Object: 147 releaseNPObject(variant->value.objectValue); 148 variant->value.objectValue = 0; 149 break; 150 } 151 152 variant->type = NPVariantType_Void; 153} 154 155} // namespace WebKit 156 157#endif // ENABLE(NETSCAPE_PLUGIN_API) 158