TestObject.cpp 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. /*
  2. * Copyright (C) 2007 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. ``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 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 "TestObject.h"
  26. #include "PluginObject.h"
  27. #include <string.h>
  28. #include <stdlib.h>
  29. static bool testEnumerate(NPObject *npobj, NPIdentifier **value, uint32_t *count);
  30. static bool testHasMethod(NPObject*, NPIdentifier name);
  31. static bool testInvoke(NPObject*, NPIdentifier name, const NPVariant* args, uint32_t argCount, NPVariant* result);
  32. static bool testHasProperty(NPObject*, NPIdentifier name);
  33. static bool testGetProperty(NPObject*, NPIdentifier name, NPVariant*);
  34. static NPObject *testAllocate(NPP npp, NPClass *theClass);
  35. static void testDeallocate(NPObject *obj);
  36. static bool testConstruct(NPObject* obj, const NPVariant* args, uint32_t argCount, NPVariant* result);
  37. static NPClass testClass = {
  38. NP_CLASS_STRUCT_VERSION,
  39. testAllocate,
  40. testDeallocate,
  41. 0,
  42. testHasMethod,
  43. testInvoke,
  44. 0,
  45. testHasProperty,
  46. testGetProperty,
  47. 0,
  48. 0,
  49. testEnumerate,
  50. testConstruct
  51. };
  52. NPClass *getTestClass(void)
  53. {
  54. return &testClass;
  55. }
  56. static int testObjectCount = 0;
  57. int getTestObjectCount()
  58. {
  59. return testObjectCount;
  60. }
  61. typedef struct {
  62. NPObject header;
  63. NPObject* testObject;
  64. } TestObject;
  65. static bool identifiersInitialized = false;
  66. #define NUM_ENUMERATABLE_TEST_IDENTIFIERS 2
  67. enum {
  68. ID_PROPERTY_FOO = 0,
  69. ID_PROPERTY_BAR,
  70. ID_PROPERTY_OBJECT_POINTER,
  71. ID_PROPERTY_TEST_OBJECT,
  72. ID_PROPERTY_REF_COUNT,
  73. NUM_TEST_IDENTIFIERS,
  74. };
  75. static NPIdentifier testIdentifiers[NUM_TEST_IDENTIFIERS];
  76. static const NPUTF8 *testIdentifierNames[NUM_TEST_IDENTIFIERS] = {
  77. "foo",
  78. "bar",
  79. "objectPointer",
  80. "testObject",
  81. "refCount",
  82. };
  83. #define ID_THROW_EXCEPTION_METHOD 0
  84. #define NUM_METHOD_IDENTIFIERS 1
  85. static NPIdentifier testMethodIdentifiers[NUM_METHOD_IDENTIFIERS];
  86. static const NPUTF8 *testMethodIdentifierNames[NUM_METHOD_IDENTIFIERS] = {
  87. "throwException",
  88. };
  89. static void initializeIdentifiers(void)
  90. {
  91. browser->getstringidentifiers(testIdentifierNames, NUM_TEST_IDENTIFIERS, testIdentifiers);
  92. browser->getstringidentifiers(testMethodIdentifierNames, NUM_METHOD_IDENTIFIERS, testMethodIdentifiers);
  93. }
  94. static NPObject* testAllocate(NPP /*npp*/, NPClass* /*theClass*/)
  95. {
  96. TestObject* newInstance = static_cast<TestObject*>(malloc(sizeof(TestObject)));
  97. newInstance->testObject = 0;
  98. ++testObjectCount;
  99. if (!identifiersInitialized) {
  100. identifiersInitialized = true;
  101. initializeIdentifiers();
  102. }
  103. return reinterpret_cast<NPObject*>(newInstance);
  104. }
  105. static void testDeallocate(NPObject *obj)
  106. {
  107. TestObject* testObject = reinterpret_cast<TestObject*>(obj);
  108. if (testObject->testObject)
  109. browser->releaseobject(testObject->testObject);
  110. --testObjectCount;
  111. free(obj);
  112. }
  113. static bool testHasMethod(NPObject*, NPIdentifier name)
  114. {
  115. for (unsigned i = 0; i < NUM_METHOD_IDENTIFIERS; i++) {
  116. if (testMethodIdentifiers[i] == name)
  117. return true;
  118. }
  119. return false;
  120. }
  121. static bool testInvoke(NPObject* header, NPIdentifier name, const NPVariant* /*args*/, uint32_t /*argCount*/, NPVariant* /*result*/)
  122. {
  123. if (name == testMethodIdentifiers[ID_THROW_EXCEPTION_METHOD]) {
  124. browser->setexception(header, "test object throwException SUCCESS");
  125. return true;
  126. }
  127. return false;
  128. }
  129. static bool testHasProperty(NPObject*, NPIdentifier name)
  130. {
  131. for (unsigned i = 0; i < NUM_TEST_IDENTIFIERS; i++) {
  132. if (testIdentifiers[i] == name)
  133. return true;
  134. }
  135. return false;
  136. }
  137. static bool testGetProperty(NPObject* npobj, NPIdentifier name, NPVariant* result)
  138. {
  139. if (name == testIdentifiers[ID_PROPERTY_FOO]) {
  140. char* mem = static_cast<char*>(browser->memalloc(4));
  141. strcpy(mem, "foo");
  142. STRINGZ_TO_NPVARIANT(mem, *result);
  143. return true;
  144. }
  145. if (name == testIdentifiers[ID_PROPERTY_OBJECT_POINTER]) {
  146. int32_t objectPointer = static_cast<int32_t>(reinterpret_cast<long long>(npobj));
  147. INT32_TO_NPVARIANT(objectPointer, *result);
  148. return true;
  149. }
  150. if (name == testIdentifiers[ID_PROPERTY_TEST_OBJECT]) {
  151. TestObject* testObject = reinterpret_cast<TestObject*>(npobj);
  152. if (!testObject->testObject)
  153. testObject->testObject = browser->createobject(0, &testClass);
  154. browser->retainobject(testObject->testObject);
  155. OBJECT_TO_NPVARIANT(testObject->testObject, *result);
  156. return true;
  157. }
  158. if (name == testIdentifiers[ID_PROPERTY_REF_COUNT]) {
  159. INT32_TO_NPVARIANT(npobj->referenceCount, *result);
  160. return true;
  161. }
  162. return false;
  163. }
  164. static bool testEnumerate(NPObject* /*npobj*/, NPIdentifier **value, uint32_t *count)
  165. {
  166. *count = NUM_ENUMERATABLE_TEST_IDENTIFIERS;
  167. *value = (NPIdentifier*)browser->memalloc(NUM_ENUMERATABLE_TEST_IDENTIFIERS * sizeof(NPIdentifier));
  168. memcpy(*value, testIdentifiers, sizeof(NPIdentifier) * NUM_ENUMERATABLE_TEST_IDENTIFIERS);
  169. return true;
  170. }
  171. static bool testConstruct(NPObject* npobj, const NPVariant* /*args*/, uint32_t /*argCount*/, NPVariant* result)
  172. {
  173. browser->retainobject(npobj);
  174. // Just return the same object.
  175. OBJECT_TO_NPVARIANT(npobj, *result);
  176. return true;
  177. }