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