PluginObject.cpp 48 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305
  1. /*
  2. * Copyright (C) 2006, 2007, 2008 Apple Inc. All rights reserved.
  3. * Copyright (C) 2009 Holger Hans Peter Freyther
  4. * Copyright (C) 2010 Collabora Ltd.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions
  8. * are met:
  9. * 1. Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. * 2. Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
  16. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  18. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR
  19. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  20. * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  21. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  22. * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
  23. * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  24. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  25. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. #include "PluginObject.h"
  28. #include "PluginTest.h"
  29. #include "TestObject.h"
  30. #include <assert.h>
  31. #include <stdarg.h>
  32. #include <stdio.h>
  33. #include <stdlib.h>
  34. #include <string.h>
  35. // Helper function which takes in the plugin window object for logging to the console object.
  36. static void pluginLogWithWindowObject(NPObject* windowObject, NPP instance, const char* message)
  37. {
  38. NPVariant consoleVariant;
  39. if (!browser->getproperty(instance, windowObject, browser->getstringidentifier("console"), &consoleVariant)) {
  40. fprintf(stderr, "Failed to retrieve console object while logging: %s\n", message);
  41. return;
  42. }
  43. NPObject* consoleObject = NPVARIANT_TO_OBJECT(consoleVariant);
  44. NPVariant messageVariant;
  45. STRINGZ_TO_NPVARIANT(message, messageVariant);
  46. NPVariant result;
  47. if (!browser->invoke(instance, consoleObject, browser->getstringidentifier("log"), &messageVariant, 1, &result)) {
  48. fprintf(stderr, "Failed to invoke console.log while logging: %s\n", message);
  49. browser->releaseobject(consoleObject);
  50. return;
  51. }
  52. browser->releasevariantvalue(&result);
  53. browser->releaseobject(consoleObject);
  54. }
  55. void pluginLogWithArguments(NPP instance, const char* format, va_list args)
  56. {
  57. const size_t messageBufferSize = 2048;
  58. char message[messageBufferSize] = "PLUGIN: ";
  59. int messageLength = sizeof("PLUGIN: ") - 1;
  60. messageLength += vsnprintf(message + messageLength, messageBufferSize - 1 - messageLength, format, args);
  61. message[messageLength] = '\0';
  62. NPObject* windowObject = 0;
  63. NPError error = browser->getvalue(instance, NPNVWindowNPObject, &windowObject);
  64. if (error != NPERR_NO_ERROR) {
  65. fprintf(stderr, "Failed to retrieve window object while logging: %s\n", message);
  66. return;
  67. }
  68. pluginLogWithWindowObject(windowObject, instance, message);
  69. browser->releaseobject(windowObject);
  70. }
  71. // Helper function to log to the console object.
  72. void pluginLog(NPP instance, const char* format, ...)
  73. {
  74. va_list args;
  75. va_start(args, format);
  76. pluginLogWithArguments(instance, format, args);
  77. va_end(args);
  78. }
  79. static void pluginInvalidate(NPObject*);
  80. static bool pluginHasProperty(NPObject*, NPIdentifier name);
  81. static bool pluginHasMethod(NPObject*, NPIdentifier name);
  82. static bool pluginGetProperty(NPObject*, NPIdentifier name, NPVariant*);
  83. static bool pluginSetProperty(NPObject*, NPIdentifier name, const NPVariant*);
  84. static bool pluginInvoke(NPObject*, NPIdentifier name, const NPVariant* args, uint32_t argCount, NPVariant* result);
  85. static NPObject* pluginAllocate(NPP npp, NPClass*);
  86. static void pluginDeallocate(NPObject*);
  87. NPNetscapeFuncs* browser;
  88. NPPluginFuncs* pluginFunctions;
  89. static NPClass pluginClass = {
  90. NP_CLASS_STRUCT_VERSION,
  91. pluginAllocate,
  92. pluginDeallocate,
  93. pluginInvalidate,
  94. pluginHasMethod,
  95. pluginInvoke,
  96. 0, // NPClass::invokeDefault,
  97. pluginHasProperty,
  98. pluginGetProperty,
  99. pluginSetProperty,
  100. 0, // NPClass::removeProperty
  101. 0, // NPClass::enumerate
  102. 0, // NPClass::construct
  103. };
  104. NPClass* getPluginClass(void)
  105. {
  106. return &pluginClass;
  107. }
  108. static bool identifiersInitialized = false;
  109. enum {
  110. ID_PROPERTY_PROPERTY = 0,
  111. ID_PROPERTY_EVENT_LOGGING,
  112. ID_PROPERTY_HAS_STREAM,
  113. ID_PROPERTY_TEST_OBJECT,
  114. ID_PROPERTY_LOG_DESTROY,
  115. ID_PROPERTY_RETURN_ERROR_FROM_NEWSTREAM,
  116. ID_PROPERTY_RETURN_NEGATIVE_ONE_FROM_WRITE,
  117. ID_PROPERTY_THROW_EXCEPTION_PROPERTY,
  118. ID_LAST_SET_WINDOW_ARGUMENTS,
  119. ID_PROPERTY_WINDOWED_PLUGIN,
  120. ID_PROPERTY_TEST_OBJECT_COUNT,
  121. ID_PROPERTY_DELETE_IN_GET_PROPERTY,
  122. NUM_PROPERTY_IDENTIFIERS
  123. };
  124. static NPIdentifier pluginPropertyIdentifiers[NUM_PROPERTY_IDENTIFIERS];
  125. static const NPUTF8 *pluginPropertyIdentifierNames[NUM_PROPERTY_IDENTIFIERS] = {
  126. "property",
  127. "eventLoggingEnabled",
  128. "hasStream",
  129. "testObject",
  130. "logDestroy",
  131. "returnErrorFromNewStream",
  132. "returnNegativeOneFromWrite",
  133. "testThrowExceptionProperty",
  134. "lastSetWindowArguments",
  135. "windowedPlugin",
  136. "testObjectCount",
  137. "deletePluginInGetProperty"
  138. };
  139. enum {
  140. ID_TEST_CALLBACK_METHOD = 0,
  141. ID_TEST_CALLBACK_METHOD_RETURN,
  142. ID_TEST_GETURL,
  143. ID_TEST_DOM_ACCESS,
  144. ID_TEST_GET_URL_NOTIFY,
  145. ID_TEST_INVOKE_DEFAULT,
  146. ID_DESTROY_STREAM,
  147. ID_TEST_ENUMERATE,
  148. ID_TEST_GETINTIDENTIFIER,
  149. ID_TEST_GET_PROPERTY,
  150. ID_TEST_HAS_PROPERTY,
  151. ID_TEST_HAS_METHOD,
  152. ID_TEST_EVALUATE,
  153. ID_TEST_GET_PROPERTY_RETURN_VALUE,
  154. ID_TEST_IDENTIFIER_TO_STRING,
  155. ID_TEST_IDENTIFIER_TO_INT,
  156. ID_TEST_PASS_TEST_OBJECT,
  157. ID_TEST_POSTURL_FILE,
  158. ID_TEST_CONSTRUCT,
  159. ID_TEST_THROW_EXCEPTION_METHOD,
  160. ID_TEST_FAIL_METHOD,
  161. ID_TEST_CLONE_OBJECT,
  162. ID_TEST_SCRIPT_OBJECT_INVOKE,
  163. ID_TEST_CREATE_TEST_OBJECT,
  164. ID_DESTROY_NULL_STREAM,
  165. ID_TEST_RELOAD_PLUGINS_NO_PAGES,
  166. ID_TEST_RELOAD_PLUGINS_AND_PAGES,
  167. ID_TEST_GET_BROWSER_PROPERTY,
  168. ID_TEST_SET_BROWSER_PROPERTY,
  169. ID_REMEMBER,
  170. ID_GET_REMEMBERED_OBJECT,
  171. ID_GET_AND_FORGET_REMEMBERED_OBJECT,
  172. ID_REF_COUNT,
  173. ID_SET_STATUS,
  174. ID_RESIZE_TO,
  175. ID_NORMALIZE,
  176. ID_INVALIDATE_RECT,
  177. ID_OBJECTS_ARE_SAME,
  178. ID_TEST_DELETE_WITHIN_INVOKE,
  179. NUM_METHOD_IDENTIFIERS
  180. };
  181. static NPIdentifier pluginMethodIdentifiers[NUM_METHOD_IDENTIFIERS];
  182. static const NPUTF8 *pluginMethodIdentifierNames[NUM_METHOD_IDENTIFIERS] = {
  183. "testCallback",
  184. "testCallbackReturn",
  185. "getURL",
  186. "testDOMAccess",
  187. "getURLNotify",
  188. "testInvokeDefault",
  189. "destroyStream",
  190. "testEnumerate",
  191. "testGetIntIdentifier",
  192. "testGetProperty",
  193. "testHasProperty",
  194. "testHasMethod",
  195. "testEvaluate",
  196. "testGetPropertyReturnValue",
  197. "testIdentifierToString",
  198. "testIdentifierToInt",
  199. "testPassTestObject",
  200. "testPostURLFile",
  201. "testConstruct",
  202. "testThrowException",
  203. "testFail",
  204. "testCloneObject",
  205. "testScriptObjectInvoke",
  206. "testCreateTestObject",
  207. "destroyNullStream",
  208. "reloadPluginsNoPages",
  209. "reloadPluginsAndPages",
  210. "testGetBrowserProperty",
  211. "testSetBrowserProperty",
  212. "remember",
  213. "getRememberedObject",
  214. "getAndForgetRememberedObject",
  215. "refCount",
  216. "setStatus",
  217. "resizeTo",
  218. "normalize",
  219. "invalidateRect",
  220. "objectsAreSame",
  221. "testDeleteWithinInvoke"
  222. };
  223. static NPUTF8* createCStringFromNPVariant(const NPVariant* variant)
  224. {
  225. size_t length = NPVARIANT_TO_STRING(*variant).UTF8Length;
  226. NPUTF8* result = (NPUTF8*)malloc(length + 1);
  227. memcpy(result, NPVARIANT_TO_STRING(*variant).UTF8Characters, length);
  228. result[length] = '\0';
  229. return result;
  230. }
  231. static void initializeIdentifiers(void)
  232. {
  233. browser->getstringidentifiers(pluginPropertyIdentifierNames, NUM_PROPERTY_IDENTIFIERS, pluginPropertyIdentifiers);
  234. browser->getstringidentifiers(pluginMethodIdentifierNames, NUM_METHOD_IDENTIFIERS, pluginMethodIdentifiers);
  235. }
  236. static bool callDeletePlugin(NPObject* obj, NPIdentifier name, NPIdentifier identifierToMatch)
  237. {
  238. if (name != identifierToMatch)
  239. return false;
  240. PluginObject* plugin = reinterpret_cast<PluginObject*>(obj);
  241. NPObject* windowScriptObject;
  242. browser->getvalue(plugin->npp, NPNVWindowNPObject, &windowScriptObject);
  243. NPIdentifier callbackIdentifier = browser->getstringidentifier("deletePlugin");
  244. NPVariant browserResult;
  245. if (browser->invoke(plugin->npp, windowScriptObject, callbackIdentifier, 0, 0, &browserResult))
  246. browser->releasevariantvalue(&browserResult);
  247. return true;
  248. }
  249. static bool pluginHasProperty(NPObject *obj, NPIdentifier name)
  250. {
  251. if (callDeletePlugin(obj, name, browser->getstringidentifier("deletePluginReturnTrue")))
  252. return true;
  253. if (callDeletePlugin(obj, name, browser->getstringidentifier("deletePluginReturnFalse")))
  254. return false;
  255. for (int i = 0; i < NUM_PROPERTY_IDENTIFIERS; i++)
  256. if (name == pluginPropertyIdentifiers[i])
  257. return true;
  258. return false;
  259. }
  260. static bool pluginHasMethod(NPObject *obj, NPIdentifier name)
  261. {
  262. if (callDeletePlugin(obj, name, browser->getstringidentifier("deletePluginInHasMethod")))
  263. return true;
  264. for (int i = 0; i < NUM_METHOD_IDENTIFIERS; i++)
  265. if (name == pluginMethodIdentifiers[i])
  266. return true;
  267. return false;
  268. }
  269. static bool pluginGetProperty(NPObject* obj, NPIdentifier name, NPVariant* result)
  270. {
  271. PluginObject* plugin = reinterpret_cast<PluginObject*>(obj);
  272. if (name == pluginPropertyIdentifiers[ID_PROPERTY_PROPERTY]) {
  273. static const char* originalString = "property";
  274. char* buf = static_cast<char*>(browser->memalloc(strlen(originalString) + 1));
  275. strcpy(buf, originalString);
  276. STRINGZ_TO_NPVARIANT(buf, *result);
  277. return true;
  278. }
  279. if (name == pluginPropertyIdentifiers[ID_PROPERTY_EVENT_LOGGING]) {
  280. BOOLEAN_TO_NPVARIANT(plugin->eventLogging, *result);
  281. return true;
  282. }
  283. if (name == pluginPropertyIdentifiers[ID_PROPERTY_LOG_DESTROY]) {
  284. BOOLEAN_TO_NPVARIANT(plugin->logDestroy, *result);
  285. return true;
  286. }
  287. if (name == pluginPropertyIdentifiers[ID_PROPERTY_HAS_STREAM]) {
  288. BOOLEAN_TO_NPVARIANT(plugin->stream, *result);
  289. return true;
  290. }
  291. if (name == pluginPropertyIdentifiers[ID_PROPERTY_TEST_OBJECT]) {
  292. NPObject* testObject = plugin->testObject;
  293. browser->retainobject(testObject);
  294. OBJECT_TO_NPVARIANT(testObject, *result);
  295. return true;
  296. }
  297. if (name == pluginPropertyIdentifiers[ID_PROPERTY_RETURN_ERROR_FROM_NEWSTREAM]) {
  298. BOOLEAN_TO_NPVARIANT(plugin->returnErrorFromNewStream, *result);
  299. return true;
  300. }
  301. if (name == pluginPropertyIdentifiers[ID_PROPERTY_RETURN_NEGATIVE_ONE_FROM_WRITE]) {
  302. BOOLEAN_TO_NPVARIANT(plugin->returnNegativeOneFromWrite, *result);
  303. return true;
  304. }
  305. if (name == pluginPropertyIdentifiers[ID_PROPERTY_THROW_EXCEPTION_PROPERTY]) {
  306. browser->setexception(obj, "plugin object testThrowExceptionProperty SUCCESS");
  307. return true;
  308. }
  309. if (name == pluginPropertyIdentifiers[ID_LAST_SET_WINDOW_ARGUMENTS]) {
  310. char* buf = static_cast<char*>(browser->memalloc(256));
  311. snprintf(buf, 256, "x: %d, y: %d, width: %u, height: %u, clipRect: (%u, %u, %u, %u)", (int)plugin->lastWindow.x, (int)plugin->lastWindow.y, (unsigned)plugin->lastWindow.width, (unsigned)plugin->lastWindow.height,
  312. plugin->lastWindow.clipRect.left, plugin->lastWindow.clipRect.top, plugin->lastWindow.clipRect.right - plugin->lastWindow.clipRect.left, plugin->lastWindow.clipRect.bottom - plugin->lastWindow.clipRect.top);
  313. STRINGZ_TO_NPVARIANT(buf, *result);
  314. return true;
  315. }
  316. if (name == pluginPropertyIdentifiers[ID_PROPERTY_TEST_OBJECT_COUNT]) {
  317. INT32_TO_NPVARIANT(getTestObjectCount(), *result);
  318. return true;
  319. }
  320. if (name == pluginPropertyIdentifiers[ID_PROPERTY_DELETE_IN_GET_PROPERTY]) {
  321. browser->retainobject(obj);
  322. callDeletePlugin(obj, name, pluginPropertyIdentifiers[ID_PROPERTY_DELETE_IN_GET_PROPERTY]);
  323. NPObject* testObject = plugin->testObject;
  324. browser->retainobject(testObject);
  325. OBJECT_TO_NPVARIANT(testObject, *result);
  326. browser->releaseobject(obj);
  327. return true;
  328. }
  329. return false;
  330. }
  331. static bool pluginSetProperty(NPObject* obj, NPIdentifier name, const NPVariant* variant)
  332. {
  333. PluginObject* plugin = reinterpret_cast<PluginObject*>(obj);
  334. if (callDeletePlugin(obj, name, browser->getstringidentifier("deletePluginReturnTrue")))
  335. return true;
  336. if (name == pluginPropertyIdentifiers[ID_PROPERTY_EVENT_LOGGING]) {
  337. plugin->eventLogging = NPVARIANT_TO_BOOLEAN(*variant);
  338. return true;
  339. }
  340. if (name == pluginPropertyIdentifiers[ID_PROPERTY_LOG_DESTROY]) {
  341. plugin->logDestroy = NPVARIANT_TO_BOOLEAN(*variant);
  342. return true;
  343. }
  344. if (name == pluginPropertyIdentifiers[ID_PROPERTY_RETURN_ERROR_FROM_NEWSTREAM]) {
  345. plugin->returnErrorFromNewStream = NPVARIANT_TO_BOOLEAN(*variant);
  346. return true;
  347. }
  348. if (name == pluginPropertyIdentifiers[ID_PROPERTY_RETURN_NEGATIVE_ONE_FROM_WRITE]) {
  349. plugin->returnNegativeOneFromWrite = NPVARIANT_TO_BOOLEAN(*variant);
  350. return true;
  351. }
  352. if (name == pluginPropertyIdentifiers[ID_PROPERTY_THROW_EXCEPTION_PROPERTY]) {
  353. browser->setexception(obj, "plugin object testThrowExceptionProperty SUCCESS");
  354. return true;
  355. }
  356. if (name == pluginPropertyIdentifiers[ID_PROPERTY_WINDOWED_PLUGIN]) {
  357. browser->setvalue(plugin->npp, NPPVpluginWindowBool, (void *)NPVARIANT_TO_BOOLEAN(*variant));
  358. return true;
  359. }
  360. return false;
  361. }
  362. static bool testDOMAccess(PluginObject* obj, const NPVariant*, uint32_t, NPVariant* result)
  363. {
  364. // Get plug-in's DOM element
  365. NPObject* elementObject;
  366. if (browser->getvalue(obj->npp, NPNVPluginElementNPObject, &elementObject) == NPERR_NO_ERROR) {
  367. // Get style
  368. NPVariant styleVariant;
  369. NPIdentifier styleIdentifier = browser->getstringidentifier("style");
  370. if (browser->getproperty(obj->npp, elementObject, styleIdentifier, &styleVariant) && NPVARIANT_IS_OBJECT(styleVariant)) {
  371. // Set style.border
  372. NPIdentifier borderIdentifier = browser->getstringidentifier("border");
  373. NPVariant borderVariant;
  374. STRINGZ_TO_NPVARIANT("3px solid red", borderVariant);
  375. browser->setproperty(obj->npp, NPVARIANT_TO_OBJECT(styleVariant), borderIdentifier, &borderVariant);
  376. browser->releasevariantvalue(&styleVariant);
  377. }
  378. browser->releaseobject(elementObject);
  379. }
  380. VOID_TO_NPVARIANT(*result);
  381. return true;
  382. }
  383. static NPIdentifier stringVariantToIdentifier(NPVariant variant)
  384. {
  385. assert(NPVARIANT_IS_STRING(variant));
  386. NPUTF8* utf8String = createCStringFromNPVariant(&variant);
  387. NPIdentifier identifier = browser->getstringidentifier(utf8String);
  388. free(utf8String);
  389. return identifier;
  390. }
  391. static NPIdentifier int32VariantToIdentifier(NPVariant variant)
  392. {
  393. assert(NPVARIANT_IS_INT32(variant));
  394. int32_t integer = NPVARIANT_TO_INT32(variant);
  395. return browser->getintidentifier(integer);
  396. }
  397. static NPIdentifier doubleVariantToIdentifier(NPVariant variant)
  398. {
  399. assert(NPVARIANT_IS_DOUBLE(variant));
  400. double value = NPVARIANT_TO_DOUBLE(variant);
  401. // Sadly there is no "getdoubleidentifier"
  402. int32_t integer = static_cast<int32_t>(value);
  403. return browser->getintidentifier(integer);
  404. }
  405. static NPIdentifier variantToIdentifier(NPVariant variant)
  406. {
  407. if (NPVARIANT_IS_STRING(variant))
  408. return stringVariantToIdentifier(variant);
  409. if (NPVARIANT_IS_INT32(variant))
  410. return int32VariantToIdentifier(variant);
  411. if (NPVARIANT_IS_DOUBLE(variant))
  412. return doubleVariantToIdentifier(variant);
  413. return 0;
  414. }
  415. static bool testIdentifierToString(PluginObject*, const NPVariant* args, uint32_t argCount, NPVariant* result)
  416. {
  417. if (argCount != 1)
  418. return true;
  419. NPIdentifier identifier = variantToIdentifier(args[0]);
  420. if (!identifier)
  421. return true;
  422. NPUTF8* utf8String = browser->utf8fromidentifier(identifier);
  423. if (!utf8String)
  424. return true;
  425. STRINGZ_TO_NPVARIANT(utf8String, *result);
  426. return true;
  427. }
  428. static bool testIdentifierToInt(PluginObject*, const NPVariant* args, uint32_t argCount, NPVariant* result)
  429. {
  430. if (argCount != 1)
  431. return false;
  432. NPIdentifier identifier = variantToIdentifier(args[0]);
  433. if (!identifier)
  434. return false;
  435. int32_t integer = browser->intfromidentifier(identifier);
  436. INT32_TO_NPVARIANT(integer, *result);
  437. return true;
  438. }
  439. static bool testPassTestObject(PluginObject* obj, const NPVariant* args, uint32_t argCount, NPVariant* result)
  440. {
  441. if (argCount != 2 || !NPVARIANT_IS_STRING(args[0]))
  442. return false;
  443. NPObject* windowScriptObject;
  444. browser->getvalue(obj->npp, NPNVWindowNPObject, &windowScriptObject);
  445. NPUTF8* callbackString = createCStringFromNPVariant(&args[0]);
  446. NPIdentifier callbackIdentifier = browser->getstringidentifier(callbackString);
  447. free(callbackString);
  448. NPVariant browserResult;
  449. browser->invoke(obj->npp, windowScriptObject, callbackIdentifier, &args[1], 1, &browserResult);
  450. browser->releasevariantvalue(&browserResult);
  451. VOID_TO_NPVARIANT(*result);
  452. return true;
  453. }
  454. static bool testCallback(PluginObject* obj, const NPVariant* args, uint32_t argCount, NPVariant* result)
  455. {
  456. if (!argCount || !NPVARIANT_IS_STRING(args[0]))
  457. return false;
  458. NPObject* windowScriptObject;
  459. browser->getvalue(obj->npp, NPNVWindowNPObject, &windowScriptObject);
  460. NPUTF8* callbackString = createCStringFromNPVariant(&args[0]);
  461. NPIdentifier callbackIdentifier = browser->getstringidentifier(callbackString);
  462. free(callbackString);
  463. NPVariant browserResult;
  464. if (browser->invoke(obj->npp, windowScriptObject, callbackIdentifier, 0, 0, &browserResult))
  465. browser->releasevariantvalue(&browserResult);
  466. browser->releaseobject(windowScriptObject);
  467. VOID_TO_NPVARIANT(*result);
  468. return true;
  469. }
  470. static bool testCallbackReturn(PluginObject* obj, const NPVariant* args, uint32_t argCount, NPVariant* result)
  471. {
  472. if (argCount != 1 || !NPVARIANT_IS_STRING(args[0]))
  473. return false;
  474. NPObject* windowScriptObject;
  475. browser->getvalue(obj->npp, NPNVWindowNPObject, &windowScriptObject);
  476. NPUTF8* callbackString = createCStringFromNPVariant(&args[0]);
  477. NPIdentifier callbackIdentifier = browser->getstringidentifier(callbackString);
  478. free(callbackString);
  479. NPVariant callbackArgs[1];
  480. OBJECT_TO_NPVARIANT(windowScriptObject, callbackArgs[0]);
  481. NPVariant browserResult;
  482. browser->invoke(obj->npp, windowScriptObject, callbackIdentifier,
  483. callbackArgs, 1, &browserResult);
  484. if (NPVARIANT_IS_OBJECT(browserResult))
  485. OBJECT_TO_NPVARIANT(NPVARIANT_TO_OBJECT(browserResult), *result);
  486. else {
  487. browser->releasevariantvalue(&browserResult);
  488. VOID_TO_NPVARIANT(*result);
  489. }
  490. return true;
  491. }
  492. static bool getURL(PluginObject* obj, const NPVariant* args, uint32_t argCount, NPVariant* result)
  493. {
  494. if (argCount == 2 && NPVARIANT_IS_STRING(args[0]) && NPVARIANT_IS_STRING(args[1])) {
  495. NPUTF8* urlString = createCStringFromNPVariant(&args[0]);
  496. NPUTF8* targetString = createCStringFromNPVariant(&args[1]);
  497. NPError npErr = browser->geturl(obj->npp, urlString, targetString);
  498. free(urlString);
  499. free(targetString);
  500. INT32_TO_NPVARIANT(npErr, *result);
  501. return true;
  502. }
  503. if (argCount == 1 && NPVARIANT_IS_STRING(args[0])) {
  504. NPUTF8* urlString = createCStringFromNPVariant(&args[0]);
  505. NPError npErr = browser->geturl(obj->npp, urlString, 0);
  506. free(urlString);
  507. INT32_TO_NPVARIANT(npErr, *result);
  508. return true;
  509. }
  510. return false;
  511. }
  512. static bool getURLNotify(PluginObject* obj, const NPVariant* args, uint32_t argCount, NPVariant* result)
  513. {
  514. if (argCount != 3 || !NPVARIANT_IS_STRING(args[0])
  515. || (!NPVARIANT_IS_STRING(args[1]) && !NPVARIANT_IS_NULL(args[1]))
  516. || !NPVARIANT_IS_STRING(args[2]))
  517. return false;
  518. NPUTF8* urlString = createCStringFromNPVariant(&args[0]);
  519. NPUTF8* targetString = (NPVARIANT_IS_STRING(args[1]) ? createCStringFromNPVariant(&args[1]) : 0);
  520. NPUTF8* callbackString = createCStringFromNPVariant(&args[2]);
  521. NPIdentifier callbackIdentifier = browser->getstringidentifier(callbackString);
  522. browser->geturlnotify(obj->npp, urlString, targetString, callbackIdentifier);
  523. free(urlString);
  524. free(targetString);
  525. free(callbackString);
  526. VOID_TO_NPVARIANT(*result);
  527. return true;
  528. }
  529. static bool testInvokeDefault(PluginObject* obj, const NPVariant* args, uint32_t argCount, NPVariant* result)
  530. {
  531. if (!NPVARIANT_IS_OBJECT(args[0]))
  532. return false;
  533. NPObject* callback = NPVARIANT_TO_OBJECT(args[0]);
  534. NPVariant invokeArgs[1];
  535. NPVariant browserResult;
  536. STRINGZ_TO_NPVARIANT("test", invokeArgs[0]);
  537. bool retval = browser->invokeDefault(obj->npp, callback, invokeArgs, 1, &browserResult);
  538. if (retval)
  539. browser->releasevariantvalue(&browserResult);
  540. BOOLEAN_TO_NPVARIANT(retval, *result);
  541. return true;
  542. }
  543. static bool destroyStream(PluginObject* obj, const NPVariant* args, uint32_t argCount, NPVariant* result)
  544. {
  545. NPError npError = browser->destroystream(obj->npp, obj->stream, NPRES_USER_BREAK);
  546. INT32_TO_NPVARIANT(npError, *result);
  547. return true;
  548. }
  549. static bool destroyNullStream(PluginObject* obj, const NPVariant* args, uint32_t argCount, NPVariant* result)
  550. {
  551. NPError npError = browser->destroystream(obj->npp, 0, NPRES_USER_BREAK);
  552. INT32_TO_NPVARIANT(npError, *result);
  553. return true;
  554. }
  555. static bool testEnumerate(PluginObject* obj, const NPVariant* args, uint32_t argCount, NPVariant* result)
  556. {
  557. if (argCount != 2 || !NPVARIANT_IS_OBJECT(args[0]) || !NPVARIANT_IS_OBJECT(args[1]))
  558. return false;
  559. uint32_t count;
  560. NPIdentifier* identifiers;
  561. if (browser->enumerate(obj->npp, NPVARIANT_TO_OBJECT(args[0]), &identifiers, &count)) {
  562. NPObject* outArray = NPVARIANT_TO_OBJECT(args[1]);
  563. NPIdentifier pushIdentifier = browser->getstringidentifier("push");
  564. for (uint32_t i = 0; i < count; i++) {
  565. NPUTF8* string = browser->utf8fromidentifier(identifiers[i]);
  566. if (!string)
  567. continue;
  568. NPVariant args[1];
  569. STRINGZ_TO_NPVARIANT(string, args[0]);
  570. NPVariant browserResult;
  571. if (browser->invoke(obj->npp, outArray, pushIdentifier, args, 1, &browserResult))
  572. browser->releasevariantvalue(&browserResult);
  573. browser->memfree(string);
  574. }
  575. browser->memfree(identifiers);
  576. }
  577. VOID_TO_NPVARIANT(*result);
  578. return true;
  579. }
  580. static bool testGetIntIdentifier(PluginObject*, const NPVariant* args, uint32_t argCount, NPVariant* result)
  581. {
  582. if (argCount != 1 || !NPVARIANT_IS_DOUBLE(args[0]))
  583. return false;
  584. NPIdentifier identifier = browser->getintidentifier((int)NPVARIANT_TO_DOUBLE(args[0]));
  585. INT32_TO_NPVARIANT((int32_t)(long long)identifier, *result);
  586. return true;
  587. }
  588. static bool testGetProperty(PluginObject* obj, const NPVariant* args, uint32_t argCount, NPVariant* result)
  589. {
  590. if (!argCount)
  591. return false;
  592. NPObject* object;
  593. browser->getvalue(obj->npp, NPNVWindowNPObject, &object);
  594. for (uint32_t i = 0; i < argCount; i++) {
  595. assert(NPVARIANT_IS_STRING(args[i]));
  596. NPUTF8* propertyString = createCStringFromNPVariant(&args[i]);
  597. NPIdentifier propertyIdentifier = browser->getstringidentifier(propertyString);
  598. free(propertyString);
  599. NPVariant variant;
  600. bool retval = browser->getproperty(obj->npp, object, propertyIdentifier, &variant);
  601. browser->releaseobject(object);
  602. if (!retval)
  603. break;
  604. if (i + 1 < argCount) {
  605. assert(NPVARIANT_IS_OBJECT(variant));
  606. object = NPVARIANT_TO_OBJECT(variant);
  607. } else {
  608. *result = variant;
  609. return true;
  610. }
  611. }
  612. VOID_TO_NPVARIANT(*result);
  613. return false;
  614. }
  615. static bool testHasProperty(PluginObject* obj, const NPVariant* args, uint32_t argCount, NPVariant* result)
  616. {
  617. if (argCount != 2 || !NPVARIANT_IS_OBJECT(args[0]) || !NPVARIANT_IS_STRING(args[1]))
  618. return false;
  619. NPUTF8* propertyString = createCStringFromNPVariant(&args[1]);
  620. NPIdentifier propertyIdentifier = browser->getstringidentifier(propertyString);
  621. free(propertyString);
  622. bool retval = browser->hasproperty(obj->npp, NPVARIANT_TO_OBJECT(args[0]), propertyIdentifier);
  623. BOOLEAN_TO_NPVARIANT(retval, *result);
  624. return true;
  625. }
  626. static bool testHasMethod(PluginObject* obj, const NPVariant* args, uint32_t argCount, NPVariant* result)
  627. {
  628. if (argCount != 2 || !NPVARIANT_IS_OBJECT(args[0]) || !NPVARIANT_IS_STRING(args[1]))
  629. return false;
  630. NPUTF8* propertyString = createCStringFromNPVariant(&args[1]);
  631. NPIdentifier propertyIdentifier = browser->getstringidentifier(propertyString);
  632. free(propertyString);
  633. bool retval = browser->hasmethod(obj->npp, NPVARIANT_TO_OBJECT(args[0]), propertyIdentifier);
  634. BOOLEAN_TO_NPVARIANT(retval, *result);
  635. return true;
  636. }
  637. static bool testEvaluate(PluginObject* obj, const NPVariant* args, uint32_t argCount, NPVariant* result)
  638. {
  639. if (argCount != 1 || !NPVARIANT_IS_STRING(args[0]))
  640. return false;
  641. NPObject* windowScriptObject;
  642. browser->getvalue(obj->npp, NPNVWindowNPObject, &windowScriptObject);
  643. NPString s = NPVARIANT_TO_STRING(args[0]);
  644. bool retval = browser->evaluate(obj->npp, windowScriptObject, &s, result);
  645. browser->releaseobject(windowScriptObject);
  646. return retval;
  647. }
  648. static bool testGetPropertyReturnValue(PluginObject* obj, const NPVariant* args, uint32_t argCount, NPVariant* result)
  649. {
  650. if (argCount != 2 || !NPVARIANT_IS_OBJECT(args[0]) || !NPVARIANT_IS_STRING(args[1]))
  651. return false;
  652. NPUTF8* propertyString = createCStringFromNPVariant(&args[1]);
  653. NPIdentifier propertyIdentifier = browser->getstringidentifier(propertyString);
  654. free(propertyString);
  655. NPVariant variant;
  656. bool retval = browser->getproperty(obj->npp, NPVARIANT_TO_OBJECT(args[0]), propertyIdentifier, &variant);
  657. if (retval)
  658. browser->releasevariantvalue(&variant);
  659. BOOLEAN_TO_NPVARIANT(retval, *result);
  660. return true;
  661. }
  662. static char* toCString(const NPString& string)
  663. {
  664. char* result = static_cast<char*>(malloc(string.UTF8Length + 1));
  665. memcpy(result, string.UTF8Characters, string.UTF8Length);
  666. result[string.UTF8Length] = '\0';
  667. return result;
  668. }
  669. static bool testPostURLFile(PluginObject* obj, const NPVariant* args, uint32_t argCount, NPVariant* result)
  670. {
  671. if (argCount != 4 || !NPVARIANT_IS_STRING(args[0]) || !NPVARIANT_IS_STRING(args[1]) || !NPVARIANT_IS_STRING(args[2]) || !NPVARIANT_IS_STRING(args[3]))
  672. return false;
  673. NPString urlString = NPVARIANT_TO_STRING(args[0]);
  674. char* url = toCString(urlString);
  675. NPString targetString = NPVARIANT_TO_STRING(args[1]);
  676. char* target = toCString(targetString);
  677. NPString pathString = NPVARIANT_TO_STRING(args[2]);
  678. char* path = toCString(pathString);
  679. NPString contentsString = NPVARIANT_TO_STRING(args[3]);
  680. FILE* tempFile = fopen(path, "w");
  681. if (!tempFile)
  682. return false;
  683. if (!fwrite(contentsString.UTF8Characters, contentsString.UTF8Length, 1, tempFile))
  684. return false;
  685. fclose(tempFile);
  686. NPError error = browser->posturl(obj->npp, url, target, pathString.UTF8Length, path, TRUE);
  687. free(path);
  688. free(target);
  689. free(url);
  690. BOOLEAN_TO_NPVARIANT(error == NPERR_NO_ERROR, *result);
  691. return true;
  692. }
  693. static bool testConstruct(PluginObject* obj, const NPVariant* args, uint32_t argCount, NPVariant* result)
  694. {
  695. if (!argCount || !NPVARIANT_IS_OBJECT(args[0]))
  696. return false;
  697. return browser->construct(obj->npp, NPVARIANT_TO_OBJECT(args[0]), args + 1, argCount - 1, result);
  698. }
  699. // Invoke a script callback to get a script NPObject. Then call a method on the
  700. // script NPObject passing it a freshly created NPObject.
  701. static bool testScriptObjectInvoke(PluginObject* obj, const NPVariant* args, uint32_t argCount, NPVariant* result)
  702. {
  703. if (argCount != 2 || !NPVARIANT_IS_STRING(args[0]) || !NPVARIANT_IS_STRING(args[1]))
  704. return false;
  705. NPObject* windowScriptObject;
  706. browser->getvalue(obj->npp, NPNVWindowNPObject, &windowScriptObject);
  707. // Arg1 is the name of the callback
  708. NPUTF8* callbackString = createCStringFromNPVariant(&args[0]);
  709. NPIdentifier callbackIdentifier = browser->getstringidentifier(callbackString);
  710. free(callbackString);
  711. // Invoke a callback that returns a script object
  712. NPVariant object_result;
  713. browser->invoke(obj->npp, windowScriptObject, callbackIdentifier, &args[1], 1, &object_result);
  714. // Script object returned
  715. NPObject* script_object = object_result.value.objectValue;
  716. // Arg2 is the name of the method to be called on the script object
  717. NPUTF8* object_mehod_string = createCStringFromNPVariant(&args[1]);
  718. NPIdentifier object_method = browser->getstringidentifier(object_mehod_string);
  719. free(object_mehod_string);
  720. // Create a fresh NPObject to be passed as an argument
  721. NPObject* object_arg = browser->createobject(obj->npp, &pluginClass);
  722. NPVariant invoke_args[1];
  723. OBJECT_TO_NPVARIANT(object_arg, invoke_args[0]);
  724. // Invoke the script method
  725. NPVariant object_method_result;
  726. browser->invoke(obj->npp, script_object, object_method, invoke_args, 1, &object_method_result);
  727. browser->releasevariantvalue(&object_result);
  728. VOID_TO_NPVARIANT(*result);
  729. if (NPVARIANT_IS_OBJECT(object_method_result)) {
  730. // Now return the callbacks return value back to our caller.
  731. // BUG 897451: This should be the same as the
  732. // windowScriptObject, but its not (in Chrome) - or at least, it
  733. // has a different refcount. This means Chrome will delete the
  734. // object before returning it and the calling JS gets a garbage
  735. // value. Firefox handles it fine.
  736. OBJECT_TO_NPVARIANT(NPVARIANT_TO_OBJECT(object_method_result), *result);
  737. } else {
  738. browser->releasevariantvalue(&object_method_result);
  739. VOID_TO_NPVARIANT(*result);
  740. }
  741. browser->releaseobject(object_arg);
  742. return true;
  743. }
  744. // Helper function to notify the layout test controller that the test completed.
  745. void notifyTestCompletion(NPP npp, NPObject* object)
  746. {
  747. NPVariant result;
  748. NPString script;
  749. script.UTF8Characters = "javascript:window.testRunner.notifyDone();";
  750. script.UTF8Length = strlen("javascript:window.testRunner.notifyDone();");
  751. browser->evaluate(npp, object, &script, &result);
  752. browser->releasevariantvalue(&result);
  753. }
  754. bool testDocumentOpen(NPP npp)
  755. {
  756. NPIdentifier documentId = browser->getstringidentifier("document");
  757. NPIdentifier openId = browser->getstringidentifier("open");
  758. NPObject* windowObject = 0;
  759. browser->getvalue(npp, NPNVWindowNPObject, &windowObject);
  760. if (!windowObject)
  761. return false;
  762. NPVariant docVariant;
  763. browser->getproperty(npp, windowObject, documentId, &docVariant);
  764. if (docVariant.type != NPVariantType_Object) {
  765. browser->releaseobject(windowObject);
  766. return false;
  767. }
  768. NPObject* documentObject = NPVARIANT_TO_OBJECT(docVariant);
  769. NPVariant openArgs[2];
  770. STRINGZ_TO_NPVARIANT("text/html", openArgs[0]);
  771. STRINGZ_TO_NPVARIANT("_blank", openArgs[1]);
  772. NPVariant result;
  773. if (!browser->invoke(npp, documentObject, openId, openArgs, 2, &result)) {
  774. browser->releaseobject(windowObject);
  775. browser->releaseobject(documentObject);
  776. return false;
  777. }
  778. browser->releaseobject(documentObject);
  779. if (result.type != NPVariantType_Object) {
  780. browser->releaseobject(windowObject);
  781. browser->releasevariantvalue(&result);
  782. return false;
  783. }
  784. pluginLogWithWindowObject(windowObject, npp, "PLUGIN: DOCUMENT OPEN SUCCESS");
  785. notifyTestCompletion(npp, result.value.objectValue);
  786. browser->releaseobject(result.value.objectValue);
  787. browser->releaseobject(windowObject);
  788. return true;
  789. }
  790. bool testWindowOpen(NPP npp)
  791. {
  792. NPIdentifier openId = browser->getstringidentifier("open");
  793. NPObject* windowObject = 0;
  794. browser->getvalue(npp, NPNVWindowNPObject, &windowObject);
  795. if (!windowObject)
  796. return false;
  797. NPVariant openArgs[2];
  798. STRINGZ_TO_NPVARIANT("about:blank", openArgs[0]);
  799. STRINGZ_TO_NPVARIANT("_blank", openArgs[1]);
  800. NPVariant result;
  801. if (!browser->invoke(npp, windowObject, openId, openArgs, 2, &result)) {
  802. browser->releaseobject(windowObject);
  803. return false;
  804. }
  805. if (result.type != NPVariantType_Object) {
  806. browser->releaseobject(windowObject);
  807. browser->releasevariantvalue(&result);
  808. return false;
  809. }
  810. pluginLogWithWindowObject(windowObject, npp, "PLUGIN: WINDOW OPEN SUCCESS");
  811. notifyTestCompletion(npp, result.value.objectValue);
  812. browser->releaseobject(result.value.objectValue);
  813. browser->releaseobject(windowObject);
  814. return true;
  815. }
  816. static bool testSetStatus(PluginObject* obj, const NPVariant* args, uint32_t argCount, NPVariant* result)
  817. {
  818. char* message = 0;
  819. if (argCount && NPVARIANT_IS_STRING(args[0])) {
  820. NPString statusString = NPVARIANT_TO_STRING(args[0]);
  821. message = toCString(statusString);
  822. }
  823. browser->status(obj->npp, message);
  824. free(message);
  825. return true;
  826. }
  827. static bool testResizeTo(PluginObject* obj, const NPVariant* args, uint32_t argCount, NPVariant* result)
  828. {
  829. VOID_TO_NPVARIANT(*result);
  830. NPObject* windowObject;
  831. if (NPERR_NO_ERROR != browser->getvalue(obj->npp, NPNVWindowNPObject, &windowObject))
  832. return false;
  833. NPVariant callResult;
  834. if (browser->invoke(obj->npp, windowObject, browser->getstringidentifier("resizePlugin"), args, argCount, &callResult))
  835. browser->releasevariantvalue(&callResult);
  836. // Force layout.
  837. if (browser->getproperty(obj->npp, windowObject, browser->getstringidentifier("pageYOffset"), &callResult))
  838. browser->releasevariantvalue(&callResult);
  839. return true;
  840. }
  841. static bool normalizeOverride(PluginObject* obj, const NPVariant* args, uint32_t argCount, NPVariant* result)
  842. {
  843. VOID_TO_NPVARIANT(*result);
  844. NPObject* windowObject;
  845. if (NPERR_NO_ERROR != browser->getvalue(obj->npp, NPNVWindowNPObject, &windowObject))
  846. return false;
  847. NPVariant callResult;
  848. if (browser->invoke(obj->npp, windowObject, browser->getstringidentifier("pluginCallback"), args, argCount, &callResult))
  849. browser->releasevariantvalue(&callResult);
  850. return true;
  851. }
  852. static bool invalidateRect(PluginObject* obj, const NPVariant* args, uint32_t argCount, NPVariant* result)
  853. {
  854. if (argCount != 4)
  855. return false;
  856. NPRect rect;
  857. rect.left = static_cast<int>(NPVARIANT_TO_DOUBLE(args[0]));
  858. rect.top = static_cast<int>(NPVARIANT_TO_DOUBLE(args[1]));
  859. rect.right = static_cast<int>(NPVARIANT_TO_DOUBLE(args[2]));
  860. rect.bottom = static_cast<int>(NPVARIANT_TO_DOUBLE(args[3]));
  861. browser->invalidaterect(obj->npp, &rect);
  862. return true;
  863. }
  864. static bool objectsAreSame(PluginObject* obj, const NPVariant* args, uint32_t argCount, NPVariant* result)
  865. {
  866. if (argCount != 2 || !NPVARIANT_IS_OBJECT(args[0]) || !NPVARIANT_IS_OBJECT(args[1]))
  867. return false;
  868. BOOLEAN_TO_NPVARIANT(NPVARIANT_TO_OBJECT(args[0]) == NPVARIANT_TO_OBJECT(args[1]), *result);
  869. return true;
  870. }
  871. static bool pluginInvoke(NPObject* header, NPIdentifier name, const NPVariant* args, uint32_t argCount, NPVariant* result)
  872. {
  873. PluginObject* plugin = reinterpret_cast<PluginObject*>(header);
  874. if (name == pluginMethodIdentifiers[ID_TEST_CALLBACK_METHOD])
  875. return testCallback(plugin, args, argCount, result);
  876. if (name == pluginMethodIdentifiers[ID_TEST_CALLBACK_METHOD_RETURN])
  877. return testCallbackReturn(plugin, args, argCount, result);
  878. if (name == pluginMethodIdentifiers[ID_TEST_GETURL])
  879. return getURL(plugin, args, argCount, result);
  880. if (name == pluginMethodIdentifiers[ID_TEST_DOM_ACCESS])
  881. return testDOMAccess(plugin, args, argCount, result);
  882. if (name == pluginMethodIdentifiers[ID_TEST_GET_URL_NOTIFY])
  883. return getURLNotify(plugin, args, argCount, result);
  884. if (name == pluginMethodIdentifiers[ID_TEST_INVOKE_DEFAULT])
  885. return testInvokeDefault(plugin, args, argCount, result);
  886. if (name == pluginMethodIdentifiers[ID_TEST_ENUMERATE])
  887. return testEnumerate(plugin, args, argCount, result);
  888. if (name == pluginMethodIdentifiers[ID_DESTROY_STREAM])
  889. return destroyStream(plugin, args, argCount, result);
  890. if (name == pluginMethodIdentifiers[ID_TEST_GETINTIDENTIFIER])
  891. return testGetIntIdentifier(plugin, args, argCount, result);
  892. if (name == pluginMethodIdentifiers[ID_TEST_EVALUATE])
  893. return testEvaluate(plugin, args, argCount, result);
  894. if (name == pluginMethodIdentifiers[ID_TEST_GET_PROPERTY])
  895. return testGetProperty(plugin, args, argCount, result);
  896. if (name == pluginMethodIdentifiers[ID_TEST_GET_PROPERTY_RETURN_VALUE])
  897. return testGetPropertyReturnValue(plugin, args, argCount, result);
  898. if (name == pluginMethodIdentifiers[ID_TEST_HAS_PROPERTY])
  899. return testHasProperty(plugin, args, argCount, result);
  900. if (name == pluginMethodIdentifiers[ID_TEST_HAS_METHOD])
  901. return testHasMethod(plugin, args, argCount, result);
  902. if (name == pluginMethodIdentifiers[ID_TEST_IDENTIFIER_TO_STRING])
  903. return testIdentifierToString(plugin, args, argCount, result);
  904. if (name == pluginMethodIdentifiers[ID_TEST_IDENTIFIER_TO_INT])
  905. return testIdentifierToInt(plugin, args, argCount, result);
  906. if (name == pluginMethodIdentifiers[ID_TEST_PASS_TEST_OBJECT])
  907. return testPassTestObject(plugin, args, argCount, result);
  908. if (name == pluginMethodIdentifiers[ID_TEST_POSTURL_FILE])
  909. return testPostURLFile(plugin, args, argCount, result);
  910. if (name == pluginMethodIdentifiers[ID_TEST_CONSTRUCT])
  911. return testConstruct(plugin, args, argCount, result);
  912. if (name == pluginMethodIdentifiers[ID_TEST_SCRIPT_OBJECT_INVOKE])
  913. return testScriptObjectInvoke(plugin, args, argCount, result);
  914. if (name == pluginMethodIdentifiers[ID_TEST_THROW_EXCEPTION_METHOD]) {
  915. browser->setexception(header, "plugin object testThrowException SUCCESS");
  916. return true;
  917. }
  918. if (name == pluginMethodIdentifiers[ID_TEST_FAIL_METHOD]) {
  919. NPObject* windowScriptObject;
  920. browser->getvalue(plugin->npp, NPNVWindowNPObject, &windowScriptObject);
  921. browser->invoke(plugin->npp, windowScriptObject, name, args, argCount, result);
  922. return false;
  923. }
  924. if (name == pluginMethodIdentifiers[ID_TEST_CLONE_OBJECT]) {
  925. NPObject* new_object = browser->createobject(plugin->npp, &pluginClass);
  926. assert(new_object->referenceCount == 1);
  927. OBJECT_TO_NPVARIANT(new_object, *result);
  928. return true;
  929. }
  930. if (name == pluginMethodIdentifiers[ID_TEST_CREATE_TEST_OBJECT]) {
  931. NPObject* testObject = browser->createobject(plugin->npp, getTestClass());
  932. assert(testObject->referenceCount == 1);
  933. OBJECT_TO_NPVARIANT(testObject, *result);
  934. return true;
  935. }
  936. if (name == pluginMethodIdentifiers[ID_DESTROY_NULL_STREAM])
  937. return destroyNullStream(plugin, args, argCount, result);
  938. if (name == pluginMethodIdentifiers[ID_TEST_RELOAD_PLUGINS_NO_PAGES]) {
  939. browser->reloadplugins(false);
  940. return true;
  941. }
  942. if (name == pluginMethodIdentifiers[ID_TEST_RELOAD_PLUGINS_AND_PAGES]) {
  943. browser->reloadplugins(true);
  944. return true;
  945. }
  946. if (name == pluginMethodIdentifiers[ID_TEST_GET_BROWSER_PROPERTY]) {
  947. browser->getproperty(plugin->npp, NPVARIANT_TO_OBJECT(args[0]), stringVariantToIdentifier(args[1]), result);
  948. return true;
  949. }
  950. if (name == pluginMethodIdentifiers[ID_TEST_SET_BROWSER_PROPERTY]) {
  951. browser->setproperty(plugin->npp, NPVARIANT_TO_OBJECT(args[0]), stringVariantToIdentifier(args[1]), &args[2]);
  952. return true;
  953. }
  954. if (name == pluginMethodIdentifiers[ID_REMEMBER]) {
  955. if (plugin->rememberedObject)
  956. browser->releaseobject(plugin->rememberedObject);
  957. plugin->rememberedObject = NPVARIANT_TO_OBJECT(args[0]);
  958. browser->retainobject(plugin->rememberedObject);
  959. VOID_TO_NPVARIANT(*result);
  960. return true;
  961. }
  962. if (name == pluginMethodIdentifiers[ID_GET_REMEMBERED_OBJECT]) {
  963. assert(plugin->rememberedObject);
  964. browser->retainobject(plugin->rememberedObject);
  965. OBJECT_TO_NPVARIANT(plugin->rememberedObject, *result);
  966. return true;
  967. }
  968. if (name == pluginMethodIdentifiers[ID_GET_AND_FORGET_REMEMBERED_OBJECT]) {
  969. assert(plugin->rememberedObject);
  970. OBJECT_TO_NPVARIANT(plugin->rememberedObject, *result);
  971. plugin->rememberedObject = 0;
  972. return true;
  973. }
  974. if (name == pluginMethodIdentifiers[ID_REF_COUNT]) {
  975. uint32_t refCount = NPVARIANT_TO_OBJECT(args[0])->referenceCount;
  976. INT32_TO_NPVARIANT(refCount, *result);
  977. return true;
  978. }
  979. if (name == pluginMethodIdentifiers[ID_SET_STATUS])
  980. return testSetStatus(plugin, args, argCount, result);
  981. if (name == pluginMethodIdentifiers[ID_RESIZE_TO])
  982. return testResizeTo(plugin, args, argCount, result);
  983. if (name == pluginMethodIdentifiers[ID_NORMALIZE])
  984. return normalizeOverride(plugin, args, argCount, result);
  985. if (name == pluginMethodIdentifiers[ID_INVALIDATE_RECT])
  986. return invalidateRect(plugin, args, argCount, result);
  987. if (name == pluginMethodIdentifiers[ID_OBJECTS_ARE_SAME])
  988. return objectsAreSame(plugin, args, argCount, result);
  989. if (name == pluginMethodIdentifiers[ID_TEST_DELETE_WITHIN_INVOKE]) {
  990. NPObject* newObject = browser->createobject(plugin->npp, &pluginClass);
  991. OBJECT_TO_NPVARIANT(newObject, *result);
  992. callDeletePlugin(header, name, pluginMethodIdentifiers[ID_TEST_DELETE_WITHIN_INVOKE]);
  993. return true;
  994. }
  995. return false;
  996. }
  997. static void pluginInvalidate(NPObject* header)
  998. {
  999. PluginObject* plugin = reinterpret_cast<PluginObject*>(header);
  1000. plugin->testObject = 0;
  1001. plugin->rememberedObject = 0;
  1002. }
  1003. static NPObject *pluginAllocate(NPP npp, NPClass *theClass)
  1004. {
  1005. PluginObject* newInstance = (PluginObject*)malloc(sizeof(PluginObject));
  1006. if (!identifiersInitialized) {
  1007. identifiersInitialized = true;
  1008. initializeIdentifiers();
  1009. }
  1010. newInstance->pluginTest = 0;
  1011. newInstance->npp = npp;
  1012. newInstance->testObject = browser->createobject(npp, getTestClass());
  1013. newInstance->rememberedObject = 0;
  1014. newInstance->eventLogging = FALSE;
  1015. newInstance->onStreamLoad = 0;
  1016. newInstance->onStreamDestroy = 0;
  1017. newInstance->onDestroy = 0;
  1018. newInstance->onURLNotify = 0;
  1019. newInstance->onSetWindow = 0;
  1020. newInstance->onPaintEvent = 0;
  1021. newInstance->logDestroy = FALSE;
  1022. newInstance->logSetWindow = FALSE;
  1023. newInstance->returnErrorFromNewStream = FALSE;
  1024. newInstance->returnNegativeOneFromWrite = FALSE;
  1025. newInstance->stream = 0;
  1026. newInstance->firstUrl = 0;
  1027. newInstance->firstHeaders = 0;
  1028. newInstance->lastUrl = 0;
  1029. newInstance->lastHeaders = 0;
  1030. newInstance->testGetURLOnDestroy = FALSE;
  1031. newInstance->testWindowOpen = FALSE;
  1032. newInstance->testKeyboardFocusForPlugins = FALSE;
  1033. newInstance->mouseDownForEvaluateScript = FALSE;
  1034. newInstance->evaluateScriptOnMouseDownOrKeyDown = 0;
  1035. return (NPObject*)newInstance;
  1036. }
  1037. static void pluginDeallocate(NPObject* header)
  1038. {
  1039. PluginObject* plugin = reinterpret_cast<PluginObject*>(header);
  1040. delete plugin->pluginTest;
  1041. if (plugin->testObject)
  1042. browser->releaseobject(plugin->testObject);
  1043. if (plugin->rememberedObject)
  1044. browser->releaseobject(plugin->rememberedObject);
  1045. free(plugin->firstUrl);
  1046. free(plugin->firstHeaders);
  1047. free(plugin->lastUrl);
  1048. free(plugin->lastHeaders);
  1049. free(plugin);
  1050. }
  1051. void handleCallback(PluginObject* object, const char *url, NPReason reason, void *notifyData)
  1052. {
  1053. assert(object);
  1054. NPVariant args[2];
  1055. NPObject* windowScriptObject;
  1056. browser->getvalue(object->npp, NPNVWindowNPObject, &windowScriptObject);
  1057. NPIdentifier callbackIdentifier = notifyData;
  1058. INT32_TO_NPVARIANT(reason, args[0]);
  1059. char* strHdr = 0;
  1060. if (object->firstUrl && object->firstHeaders && object->lastUrl && object->lastHeaders) {
  1061. // Format expected by JavaScript validator: four fields separated by \n\n:
  1062. // First URL; first header block; last URL; last header block.
  1063. // Note that header blocks already end with \n due to how NPStream::headers works.
  1064. int len = strlen(object->firstUrl) + 2
  1065. + strlen(object->firstHeaders) + 1
  1066. + strlen(object->lastUrl) + 2
  1067. + strlen(object->lastHeaders) + 1;
  1068. strHdr = (char*)malloc(len + 1);
  1069. snprintf(strHdr, len + 1, "%s\n\n%s\n%s\n\n%s\n",
  1070. object->firstUrl, object->firstHeaders, object->lastUrl, object->lastHeaders);
  1071. STRINGN_TO_NPVARIANT(strHdr, len, args[1]);
  1072. } else
  1073. NULL_TO_NPVARIANT(args[1]);
  1074. NPVariant browserResult;
  1075. if (browser->invoke(object->npp, windowScriptObject, callbackIdentifier, args, 2, &browserResult))
  1076. browser->releasevariantvalue(&browserResult);
  1077. free(strHdr);
  1078. }
  1079. void notifyStream(PluginObject* object, const char *url, const char *headers)
  1080. {
  1081. if (!object->firstUrl) {
  1082. if (url)
  1083. object->firstUrl = strdup(url);
  1084. if (headers)
  1085. object->firstHeaders = strdup(headers);
  1086. } else {
  1087. free(object->lastUrl);
  1088. free(object->lastHeaders);
  1089. object->lastUrl = (url ? strdup(url) : 0);
  1090. object->lastHeaders = (headers ? strdup(headers) : 0);
  1091. }
  1092. }
  1093. void testNPRuntime(NPP npp)
  1094. {
  1095. NPObject* windowScriptObject;
  1096. browser->getvalue(npp, NPNVWindowNPObject, &windowScriptObject);
  1097. // Invoke
  1098. NPIdentifier testNPInvoke = browser->getstringidentifier("testNPInvoke");
  1099. NPVariant args[7];
  1100. VOID_TO_NPVARIANT(args[0]);
  1101. NULL_TO_NPVARIANT(args[1]);
  1102. BOOLEAN_TO_NPVARIANT(true, args[2]);
  1103. INT32_TO_NPVARIANT(242, args[3]);
  1104. DOUBLE_TO_NPVARIANT(242.242, args[4]);
  1105. STRINGZ_TO_NPVARIANT("Hello, World", args[5]);
  1106. OBJECT_TO_NPVARIANT(windowScriptObject, args[6]);
  1107. NPVariant result;
  1108. if (browser->invoke(npp, windowScriptObject, testNPInvoke, args, 7, &result))
  1109. browser->releasevariantvalue(&result);
  1110. browser->releaseobject(windowScriptObject);
  1111. }