npruntime.cpp 5.0 KB

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