+2006-03-09 Darin Adler <darin@apple.com>
+
+ Reviewed by John Sullivan.
+
+ - fix http://bugzilla.opendarwin.org/show_bug.cgi?id=7681
+ memory leak in the plug-in tests
+
+ * DumpRenderTree/TestNetscapePlugIn.subproj/main.c:
+ (NPP_Destroy): Added code to release the plug-in object. This is the leak fix.
+ (NPP_SetWindow): Remove unneeded code to store the window pointer.
+
+ * DumpRenderTree/TestNetscapePlugIn.subproj/PluginObject.c:
+ Moved the browser global in here since it's declared in this file's header.
+ Changed the code to set up the pluginClass structure to not use function
+ pointer casts. Those are dangerous because they can hide many types of mismatch.
+ And indeed when I did this I discovered that many functions were missing their
+ boolean return values or had parameter declarations with the wrong types.
+ (pluginGetProperty): Use STRINGZ_TO_NPVARIANT macro for greater simplicity and
+ clarity. Added boolean return value: return true when successful and false when not.
+ (pluginSetProperty): Added boolean return value, return false since we have no
+ properties we can set.
+ (pluginInvoke): Added boolean return value. Return true when successful and false
+ when not. Use NPVARIANT macros where appropriate. Added a missing release for the
+ return value from calling the browser. Changed code to put the strings in malloc
+ buffers instead of relying on GCC's extension that allows variable-sized arrays
+ on the stack.
+ (pluginInvokeDefault): Added boolean return value, return false since we have no
+ default function to call.
+ (pluginInvalidate): Added missing parameter. Removed comment.
+ (pluginAllocate): Removed unneeded cast. This is C code, not C++, so you don't have
+ to cast the result of malloc.
+ (pluginDeallocate): Removed uneeded cast.
+
+ * DumpRenderTree/TestNetscapePlugIn.subproj/PluginObject.h: Removed some unneeded
+ includes. Changed our PluginObject to use NPObject instead of re-declaring fields
+ that match NPObject's fields. Removed unused NPWindow pointer.
+
2006-03-09 Mitz Pettel <opendarwin.org@mitzpettel.com>
Test: fast/events/event-sender-mouse-click.html
WHETHER UNDER THEORY OF CONTRACT, TORT (INCLUDING NEGLIGENCE), STRICT LIABILITY OR
OTHERWISE, EVEN IF APPLE HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
+
#import "PluginObject.h"
-void pluginInvalidate ();
-bool pluginHasProperty (NPClass *theClass, NPIdentifier name);
-bool pluginHasMethod (NPClass *theClass, NPIdentifier name);
-void pluginGetProperty (PluginObject *obj, NPIdentifier name, NPVariant *variant);
-void pluginSetProperty (PluginObject *obj, NPIdentifier name, const NPVariant *variant);
-void pluginInvoke (PluginObject *obj, NPIdentifier name, NPVariant *args, uint32_t argCount, NPVariant *result);
-void pluginInvokeDefault (PluginObject *obj, NPVariant *args, uint32_t argCount, NPVariant *result);
-NPObject *pluginAllocate (NPP npp, NPClass *theClass);
-void pluginDeallocate (PluginObject *obj);
-
-static NPClass _pluginFunctionPtrs = {
+static void pluginInvalidate(NPObject *obj);
+static bool pluginHasProperty(NPObject *obj, NPIdentifier name);
+static bool pluginHasMethod(NPObject *obj, NPIdentifier name);
+static bool pluginGetProperty(NPObject *obj, NPIdentifier name, NPVariant *variant);
+static bool pluginSetProperty(NPObject *obj, NPIdentifier name, const NPVariant *variant);
+static bool pluginInvoke(NPObject *obj, NPIdentifier name, const NPVariant *args, uint32_t argCount, NPVariant *result);
+static bool pluginInvokeDefault(NPObject *obj, const NPVariant *args, uint32_t argCount, NPVariant *result);
+static NPObject *pluginAllocate(NPP npp, NPClass *theClass);
+static void pluginDeallocate(NPObject *obj);
+
+NPNetscapeFuncs *browser;
+
+static NPClass pluginClass = {
NP_CLASS_STRUCT_VERSION,
- (NPAllocateFunctionPtr) pluginAllocate,
- (NPDeallocateFunctionPtr) pluginDeallocate,
- (NPInvalidateFunctionPtr) pluginInvalidate,
- (NPHasMethodFunctionPtr) pluginHasMethod,
- (NPInvokeFunctionPtr) pluginInvoke,
- (NPInvokeDefaultFunctionPtr) pluginInvokeDefault,
- (NPHasPropertyFunctionPtr) pluginHasProperty,
- (NPGetPropertyFunctionPtr) pluginGetProperty,
- (NPSetPropertyFunctionPtr) pluginSetProperty,
+ pluginAllocate,
+ pluginDeallocate,
+ pluginInvalidate,
+ pluginHasMethod,
+ pluginInvoke,
+ pluginInvokeDefault,
+ pluginHasProperty,
+ pluginGetProperty,
+ pluginSetProperty,
};
NPClass *getPluginClass(void)
{
- return &_pluginFunctionPtrs;
+ return &pluginClass;
}
static bool identifiersInitialized = false;
"getURL"
};
-static void initializeIdentifiers()
+static NPUTF8* createCStringFromNPVariant(const NPVariant *variant)
{
- browser->getstringidentifiers (pluginPropertyIdentifierNames, NUM_PROPERTY_IDENTIFIERS, pluginPropertyIdentifiers);
- browser->getstringidentifiers (pluginMethodIdentifierNames, NUM_METHOD_IDENTIFIERS, pluginMethodIdentifiers);
-};
+ size_t length = NPVARIANT_TO_STRING(*variant).UTF8Length;
+ NPUTF8* result = malloc(length + 1);
+ memcpy(result, NPVARIANT_TO_STRING(*variant).UTF8Characters, length);
+ result[length] = '\0';
+ return result;
+}
+
+static void initializeIdentifiers(void)
+{
+ browser->getstringidentifiers(pluginPropertyIdentifierNames, NUM_PROPERTY_IDENTIFIERS, pluginPropertyIdentifiers);
+ browser->getstringidentifiers(pluginMethodIdentifierNames, NUM_METHOD_IDENTIFIERS, pluginMethodIdentifiers);
+}
-bool pluginHasProperty (NPClass *theClass, NPIdentifier name)
+static bool pluginHasProperty(NPObject *obj, NPIdentifier name)
{
- for (int i = 0; i < NUM_PROPERTY_IDENTIFIERS; i++) {
+ for (int i = 0; i < NUM_PROPERTY_IDENTIFIERS; i++)
if (name == pluginPropertyIdentifiers[i])
return true;
- }
return false;
}
-bool pluginHasMethod (NPClass *theClass, NPIdentifier name)
+static bool pluginHasMethod(NPObject *obj, NPIdentifier name)
{
- for (int i = 0; i < NUM_METHOD_IDENTIFIERS; i++) {
+ for (int i = 0; i < NUM_METHOD_IDENTIFIERS; i++)
if (name == pluginMethodIdentifiers[i])
return true;
- }
return false;
}
-void pluginGetProperty (PluginObject *obj, NPIdentifier name, NPVariant *variant)
+static bool pluginGetProperty(NPObject *obj, NPIdentifier name, NPVariant *variant)
{
if (name == pluginPropertyIdentifiers[ID_PROPERTY_PROPERTY]) {
- // just return "property"
- NPString propertyNameString;
- propertyNameString.UTF8Characters = "property";
- propertyNameString.UTF8Length = sizeof("property") - 1;
- variant->type = NPVariantType_String;
- variant->value.stringValue = propertyNameString;
- } else
- variant->type = NPVariantType_Void;
+ STRINGZ_TO_NPVARIANT("property", *variant);
+ return true;
+ }
+ return false;
}
-void pluginSetProperty (PluginObject *obj, NPIdentifier name, const NPVariant *variant)
+static bool pluginSetProperty(NPObject *obj, NPIdentifier name, const NPVariant *variant)
{
+ return false;
}
-void pluginInvoke (PluginObject *obj, NPIdentifier name, NPVariant *args, unsigned argCount, NPVariant *result)
+static bool pluginInvoke(NPObject *header, NPIdentifier name, const NPVariant *args, uint32_t argCount, NPVariant *result)
{
+ PluginObject *obj = (PluginObject *)header;
if (name == pluginMethodIdentifiers[ID_TEST_CALLBACK_METHOD]) {
// call whatever method name we're given
- if (argCount > 0 && args->type == NPVariantType_String) {
- NPVariant browserResult;
-
+ if (argCount > 0 && NPVARIANT_IS_STRING(args[0])) {
NPObject *windowScriptObject;
browser->getvalue(obj->npp, NPPVpluginScriptableNPObject, &windowScriptObject);
- NPString argString = args[0].value.stringValue;
- int size = argString.UTF8Length + 1;
- NPUTF8 callbackString[size];
- strncpy(callbackString, argString.UTF8Characters, argString.UTF8Length);
- callbackString[size - 1] = '\0';
+ NPUTF8* callbackString = createCStringFromNPVariant(&args[0]);
+ NPIdentifier callbackIdentifier = browser->getstringidentifier(callbackString);
+ free(callbackString);
+
+ NPVariant browserResult;
+ browser->invoke(obj->npp, windowScriptObject, callbackIdentifier, 0, 0, &browserResult);
+ browser->releasevariantvalue(&browserResult);
- NPIdentifier callbackMethodID = browser->getstringidentifier(callbackString);
- browser->invoke(obj->npp, windowScriptObject, callbackMethodID, 0, 0, &browserResult);
+ VOID_TO_NPVARIANT(*result);
+ return true;
}
} else if (name == pluginMethodIdentifiers[ID_TEST_GETURL]) {
- if (argCount == 2 && args[0].type == NPVariantType_String && args[1].type == NPVariantType_String) {
- NPString argString = args[0].value.stringValue;
- int size = argString.UTF8Length + 1;
- NPUTF8 urlString[size];
- strncpy(urlString, argString.UTF8Characters, argString.UTF8Length);
- urlString[size - 1] = '\0';
-
- argString = args[1].value.stringValue;
- size = argString.UTF8Length + 1;
- NPUTF8 targetString[size];
- strncpy(targetString, argString.UTF8Characters, argString.UTF8Length);
- targetString[size - 1] = '\0';
-
+ if (argCount == 2 && NPVARIANT_IS_STRING(args[0]) && NPVARIANT_IS_STRING(args[1])) {
+ NPUTF8* urlString = createCStringFromNPVariant(&args[0]);
+ NPUTF8* targetString = createCStringFromNPVariant(&args[1]);
browser->geturl(obj->npp, urlString, targetString);
+ free(urlString);
+ free(targetString);
+
+ VOID_TO_NPVARIANT(*result);
+ return true;
}
}
-
- result->type = NPVariantType_Void;
+ return false;
}
-void pluginInvokeDefault (PluginObject *obj, NPVariant *args, unsigned argCount, NPVariant *result)
+static bool pluginInvokeDefault(NPObject *obj, const NPVariant *args, uint32_t argCount, NPVariant *result)
{
- result->type = NPVariantType_Void;
+ return false;
}
-void pluginInvalidate ()
+static void pluginInvalidate(NPObject *obj)
{
- // Make sure we've released any remainging references to JavaScript
- // objects.
}
-NPObject *pluginAllocate (NPP npp, NPClass *theClass)
+static NPObject *pluginAllocate(NPP npp, NPClass *theClass)
{
- PluginObject *newInstance = (PluginObject *)malloc (sizeof(PluginObject));
+ PluginObject *newInstance = malloc(sizeof(PluginObject));
if (!identifiersInitialized) {
identifiersInitialized = true;
return (NPObject *)newInstance;
}
-void pluginDeallocate (PluginObject *obj)
+static void pluginDeallocate(NPObject *obj)
{
- free ((void *)obj);
+ free(obj);
}
-