diff options
author | Cary Clark <> | 2009-04-14 06:33:00 -0700 |
---|---|---|
committer | The Android Open Source Project <initial-contribution@android.com> | 2009-04-14 06:33:00 -0700 |
commit | 563af33bc48281d19dce701398dbb88cb54fd7ec (patch) | |
tree | 395b4502f029dea8b25b342d66dc06b5d8f99985 /WebKitTools/DumpRenderTree/TestNetscapePlugIn.subproj | |
parent | 5cfedfef172691d0f4bcf2be5ca3cddd8c9a47f4 (diff) | |
download | external_webkit-563af33bc48281d19dce701398dbb88cb54fd7ec.zip external_webkit-563af33bc48281d19dce701398dbb88cb54fd7ec.tar.gz external_webkit-563af33bc48281d19dce701398dbb88cb54fd7ec.tar.bz2 |
AI 146110: add missing files to webkit
brings it in sync with webkit svn cl 42046
Automated import of CL 146110
Diffstat (limited to 'WebKitTools/DumpRenderTree/TestNetscapePlugIn.subproj')
6 files changed, 1456 insertions, 0 deletions
diff --git a/WebKitTools/DumpRenderTree/TestNetscapePlugIn.subproj/Info.plist b/WebKitTools/DumpRenderTree/TestNetscapePlugIn.subproj/Info.plist new file mode 100644 index 0000000..7444b84 --- /dev/null +++ b/WebKitTools/DumpRenderTree/TestNetscapePlugIn.subproj/Info.plist @@ -0,0 +1,60 @@ +<?xml version="1.0" encoding="UTF-8"?> +<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> +<plist version="1.0"> +<dict> + <key>CFBundleDevelopmentRegion</key> + <string>English</string> + <key>CFBundleExecutable</key> + <string>TestNetscapePlugIn</string> + <key>CFBundleGetInfoString</key> + <string>420+, Copyright 2006-2009 Apple Inc.</string> + <key>CFBundleIconFile</key> + <string></string> + <key>CFBundleIdentifier</key> + <string>com.apple.testnetscapeplugin</string> + <key>CFBundleInfoDictionaryVersion</key> + <string>6.0</string> + <key>CFBundlePackageType</key> + <string>BRPL</string> + <key>CFBundleShortVersionString</key> + <string>1.0</string> + <key>CFBundleSignature</key> + <string>????</string> + <key>CFBundleVersion</key> + <string>1.0</string> + <key>CFPlugInDynamicRegisterFunction</key> + <string></string> + <key>CFPlugInDynamicRegistration</key> + <string>NO</string> + <key>CFPlugInFactories</key> + <dict> + <key>00000000-0000-0000-0000-000000000000</key> + <string>MyFactoryFunction</string> + </dict> + <key>CFPlugInTypes</key> + <dict> + <key>00000000-0000-0000-0000-000000000000</key> + <array> + <string>00000000-0000-0000-0000-000000000000</string> + </array> + </dict> + <key>CFPlugInUnloadFunction</key> + <string></string> + <key>WebPluginDescription</key> + <string>Simple Netscape plug-in that handles test content for WebKit</string> + <key>WebPluginMIMETypes</key> + <dict> + <key>application/x-webkit-test-netscape</key> + <dict> + <key>WebPluginExtensions</key> + <array> + <string>testnetscape</string> + </array> + <key>WebPluginTypeDescription</key> + <string>test netscape content</string> + </dict> + </dict> + <key>WebPluginName</key> + <string>WebKit Test PlugIn</string> +</dict> +</plist> diff --git a/WebKitTools/DumpRenderTree/TestNetscapePlugIn.subproj/PluginObject.cpp b/WebKitTools/DumpRenderTree/TestNetscapePlugIn.subproj/PluginObject.cpp new file mode 100644 index 0000000..e321fee --- /dev/null +++ b/WebKitTools/DumpRenderTree/TestNetscapePlugIn.subproj/PluginObject.cpp @@ -0,0 +1,722 @@ +/* + * Copyright (C) 2006, 2007, 2008 Apple Inc. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY + * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY + * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include "PluginObject.h" + +#include "TestObject.h" +#include <assert.h> +#include <stdio.h> + +#include <string.h> +#include <stdlib.h> + +static void pluginInvalidate(NPObject*); +static bool pluginHasProperty(NPObject*, NPIdentifier name); +static bool pluginHasMethod(NPObject*, NPIdentifier name); +static bool pluginGetProperty(NPObject*, NPIdentifier name, NPVariant*); +static bool pluginSetProperty(NPObject*, NPIdentifier name, const NPVariant*); +static bool pluginInvoke(NPObject*, NPIdentifier name, const NPVariant* args, uint32_t argCount, NPVariant* result); +static bool pluginInvokeDefault(NPObject*, const NPVariant* args, uint32_t argCount, NPVariant* result); +static NPObject* pluginAllocate(NPP npp, NPClass*); +static void pluginDeallocate(NPObject*); + +NPNetscapeFuncs* browser; + +static NPClass pluginClass = { + NP_CLASS_STRUCT_VERSION, + pluginAllocate, + pluginDeallocate, + pluginInvalidate, + pluginHasMethod, + pluginInvoke, + pluginInvokeDefault, + pluginHasProperty, + pluginGetProperty, + pluginSetProperty, +}; + +NPClass *getPluginClass(void) +{ + return &pluginClass; +} + +static bool identifiersInitialized = false; + +#define ID_PROPERTY_PROPERTY 0 +#define ID_PROPERTY_EVENT_LOGGING 1 +#define ID_PROPERTY_HAS_STREAM 2 +#define ID_PROPERTY_TEST_OBJECT 3 +#define ID_PROPERTY_LOG_DESTROY 4 +#define ID_PROPERTY_RETURN_ERROR_FROM_NEWSTREAM 5 +#define NUM_PROPERTY_IDENTIFIERS 6 + +static NPIdentifier pluginPropertyIdentifiers[NUM_PROPERTY_IDENTIFIERS]; +static const NPUTF8 *pluginPropertyIdentifierNames[NUM_PROPERTY_IDENTIFIERS] = { + "property", + "eventLoggingEnabled", + "hasStream", + "testObject", + "logDestroy", + "returnErrorFromNewStream", +}; + +#define ID_TEST_CALLBACK_METHOD 0 +#define ID_TEST_GETURL 1 +#define ID_REMOVE_DEFAULT_METHOD 2 +#define ID_TEST_DOM_ACCESS 3 +#define ID_TEST_GET_URL_NOTIFY 4 +#define ID_TEST_INVOKE_DEFAULT 5 +#define ID_DESTROY_STREAM 6 +#define ID_TEST_ENUMERATE 7 +#define ID_TEST_GETINTIDENTIFIER 8 +#define ID_TEST_GET_PROPERTY 9 +#define ID_TEST_EVALUATE 10 +#define ID_TEST_GET_PROPERTY_RETURN_VALUE 11 +#define ID_TEST_IDENTIFIER_TO_STRING 12 +#define ID_TEST_IDENTIFIER_TO_INT 13 +#define ID_TEST_POSTURL_FILE 14 +#define ID_TEST_CONSTRUCT 15 +#define ID_TEST_THROW_EXCEPTION_METHOD 16 +#define ID_DESTROY_NULL_STREAM 17 +#define NUM_METHOD_IDENTIFIERS 18 + +static NPIdentifier pluginMethodIdentifiers[NUM_METHOD_IDENTIFIERS]; +static const NPUTF8 *pluginMethodIdentifierNames[NUM_METHOD_IDENTIFIERS] = { + "testCallback", + "getURL", + "removeDefaultMethod", + "testDOMAccess", + "getURLNotify", + "testInvokeDefault", + "destroyStream", + "testEnumerate", + "testGetIntIdentifier", + "testGetProperty", + "testEvaluate", + "testGetPropertyReturnValue", + "testIdentifierToString", + "testIdentifierToInt", + "testPostURLFile", + "testConstruct", + "testThrowException", + "destroyNullStream" +}; + +static NPUTF8* createCStringFromNPVariant(const NPVariant* variant) +{ + size_t length = NPVARIANT_TO_STRING(*variant).UTF8Length; + NPUTF8* result = (NPUTF8*)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); +} + +static bool pluginHasProperty(NPObject *obj, NPIdentifier name) +{ + for (int i = 0; i < NUM_PROPERTY_IDENTIFIERS; i++) + if (name == pluginPropertyIdentifiers[i]) + return true; + return false; +} + +static bool pluginHasMethod(NPObject *obj, NPIdentifier name) +{ + for (int i = 0; i < NUM_METHOD_IDENTIFIERS; i++) + if (name == pluginMethodIdentifiers[i]) + return true; + return false; +} + +static bool pluginGetProperty(NPObject* obj, NPIdentifier name, NPVariant* result) +{ + PluginObject* plugin = reinterpret_cast<PluginObject*>(obj); + if (name == pluginPropertyIdentifiers[ID_PROPERTY_PROPERTY]) { + STRINGZ_TO_NPVARIANT("property", *result); + return true; + } else if (name == pluginPropertyIdentifiers[ID_PROPERTY_EVENT_LOGGING]) { + BOOLEAN_TO_NPVARIANT(plugin->eventLogging, *result); + return true; + } else if (name == pluginPropertyIdentifiers[ID_PROPERTY_LOG_DESTROY]) { + BOOLEAN_TO_NPVARIANT(plugin->logDestroy, *result); + return true; + } else if (name == pluginPropertyIdentifiers[ID_PROPERTY_HAS_STREAM]) { + BOOLEAN_TO_NPVARIANT(plugin->stream != 0, *result); + return true; + } else if (name == pluginPropertyIdentifiers[ID_PROPERTY_TEST_OBJECT]) { + NPObject* testObject = plugin->testObject; + browser->retainobject(testObject); + OBJECT_TO_NPVARIANT(testObject, *result); + return true; + } else if (name == pluginPropertyIdentifiers[ID_PROPERTY_RETURN_ERROR_FROM_NEWSTREAM]) { + BOOLEAN_TO_NPVARIANT(plugin->returnErrorFromNewStream, *result); + return true; + } + return false; +} + +static bool pluginSetProperty(NPObject* obj, NPIdentifier name, const NPVariant* variant) +{ + PluginObject* plugin = reinterpret_cast<PluginObject*>(obj); + if (name == pluginPropertyIdentifiers[ID_PROPERTY_EVENT_LOGGING]) { + plugin->eventLogging = NPVARIANT_TO_BOOLEAN(*variant); + return true; + } else if (name == pluginPropertyIdentifiers[ID_PROPERTY_LOG_DESTROY]) { + plugin->logDestroy = NPVARIANT_TO_BOOLEAN(*variant); + return true; + } else if (name == pluginPropertyIdentifiers[ID_PROPERTY_RETURN_ERROR_FROM_NEWSTREAM]) { + plugin->returnErrorFromNewStream = NPVARIANT_TO_BOOLEAN(*variant); + return true; + } + + return false; +} + +static bool testDOMAccess(PluginObject* obj, const NPVariant*, uint32_t, NPVariant* result) +{ + // Get plug-in's DOM element + NPObject* elementObject; + if (browser->getvalue(obj->npp, NPNVPluginElementNPObject, &elementObject) == NPERR_NO_ERROR) { + // Get style + NPVariant styleVariant; + NPIdentifier styleIdentifier = browser->getstringidentifier("style"); + if (browser->getproperty(obj->npp, elementObject, styleIdentifier, &styleVariant) && NPVARIANT_IS_OBJECT(styleVariant)) { + // Set style.border + NPIdentifier borderIdentifier = browser->getstringidentifier("border"); + NPVariant borderVariant; + STRINGZ_TO_NPVARIANT("3px solid red", borderVariant); + browser->setproperty(obj->npp, NPVARIANT_TO_OBJECT(styleVariant), borderIdentifier, &borderVariant); + browser->releasevariantvalue(&styleVariant); + } + + browser->releaseobject(elementObject); + } + VOID_TO_NPVARIANT(*result); + return true; +} + +static NPIdentifier stringVariantToIdentifier(NPVariant variant) +{ + assert(NPVARIANT_IS_STRING(variant)); + NPUTF8* utf8String = createCStringFromNPVariant(&variant); + NPIdentifier identifier = browser->getstringidentifier(utf8String); + free(utf8String); + return identifier; +} + +static NPIdentifier int32VariantToIdentifier(NPVariant variant) +{ + assert(NPVARIANT_IS_INT32(variant)); + int32 integer = NPVARIANT_TO_INT32(variant); + return browser->getintidentifier(integer); +} + +static NPIdentifier doubleVariantToIdentifier(NPVariant variant) +{ + assert(NPVARIANT_IS_DOUBLE(variant)); + double value = NPVARIANT_TO_DOUBLE(variant); + // Sadly there is no "getdoubleidentifier" + int32 integer = static_cast<int32>(value); + return browser->getintidentifier(integer); +} + +static NPIdentifier variantToIdentifier(NPVariant variant) +{ + if (NPVARIANT_IS_STRING(variant)) + return stringVariantToIdentifier(variant); + else if (NPVARIANT_IS_INT32(variant)) + return int32VariantToIdentifier(variant); + else if (NPVARIANT_IS_DOUBLE(variant)) + return doubleVariantToIdentifier(variant); + return 0; +} + +static bool testIdentifierToString(PluginObject*, const NPVariant* args, uint32_t argCount, NPVariant* result) +{ + if (argCount != 1) + return false; + NPIdentifier identifier = variantToIdentifier(args[0]); + if (!identifier) + return false; + NPUTF8* utf8String = browser->utf8fromidentifier(identifier); + if (!utf8String) + return false; + STRINGZ_TO_NPVARIANT(utf8String, *result); + return true; +} + +static bool testIdentifierToInt(PluginObject*, const NPVariant* args, uint32_t argCount, NPVariant* result) +{ + if (argCount != 1) + return false; + NPIdentifier identifier = variantToIdentifier(args[0]); + if (!identifier) + return false; + int32 integer = browser->intfromidentifier(identifier); + INT32_TO_NPVARIANT(integer, *result); + return true; +} + +static bool testCallback(PluginObject* obj, const NPVariant* args, uint32_t argCount, NPVariant* result) +{ + if (argCount == 0 || !NPVARIANT_IS_STRING(args[0])) + return false; + + NPObject* windowScriptObject; + browser->getvalue(obj->npp, NPNVWindowNPObject, &windowScriptObject); + + 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); + + browser->releaseobject(windowScriptObject); + + VOID_TO_NPVARIANT(*result); + return true; +} + +static bool getURL(PluginObject* obj, const NPVariant* args, uint32_t argCount, NPVariant* result) +{ + if (argCount == 2 && NPVARIANT_IS_STRING(args[0]) && NPVARIANT_IS_STRING(args[1])) { + NPUTF8* urlString = createCStringFromNPVariant(&args[0]); + NPUTF8* targetString = createCStringFromNPVariant(&args[1]); + NPError npErr = browser->geturl(obj->npp, urlString, targetString); + free(urlString); + free(targetString); + + INT32_TO_NPVARIANT(npErr, *result); + return true; + } else if (argCount == 1 && NPVARIANT_IS_STRING(args[0])) { + NPUTF8* urlString = createCStringFromNPVariant(&args[0]); + NPError npErr = browser->geturl(obj->npp, urlString, 0); + free(urlString); + + INT32_TO_NPVARIANT(npErr, *result); + return true; + } + return false; +} + +static bool removeDefaultMethod(PluginObject*, const NPVariant* args, uint32_t argCount, NPVariant* result) +{ + pluginClass.invokeDefault = 0; + VOID_TO_NPVARIANT(*result); + return true; +} + +static bool getURLNotify(PluginObject* obj, const NPVariant* args, uint32_t argCount, NPVariant* result) +{ + if (argCount != 3 || !NPVARIANT_IS_STRING(args[0]) + || (!NPVARIANT_IS_STRING(args[1]) && !NPVARIANT_IS_NULL(args[1])) + || !NPVARIANT_IS_STRING(args[2])) + return false; + + NPUTF8* urlString = createCStringFromNPVariant(&args[0]); + NPUTF8* targetString = (NPVARIANT_IS_STRING(args[1]) ? createCStringFromNPVariant(&args[1]) : NULL); + NPUTF8* callbackString = createCStringFromNPVariant(&args[2]); + + NPIdentifier callbackIdentifier = browser->getstringidentifier(callbackString); + browser->geturlnotify(obj->npp, urlString, targetString, callbackIdentifier); + + free(urlString); + free(targetString); + free(callbackString); + + VOID_TO_NPVARIANT(*result); + return true; +} + +static bool testInvokeDefault(PluginObject* obj, const NPVariant* args, uint32_t argCount, NPVariant* result) +{ + if (!NPVARIANT_IS_OBJECT(args[0])) + return false; + + NPObject *callback = NPVARIANT_TO_OBJECT(args[0]); + + NPVariant invokeArgs[1]; + NPVariant browserResult; + + STRINGZ_TO_NPVARIANT("test", invokeArgs[0]); + bool retval = browser->invokeDefault(obj->npp, callback, invokeArgs, 1, &browserResult); + + if (retval) + browser->releasevariantvalue(&browserResult); + + BOOLEAN_TO_NPVARIANT(retval, *result); + return true; +} + +static bool destroyStream(PluginObject* obj, const NPVariant* args, uint32_t argCount, NPVariant* result) +{ + NPError npError = browser->destroystream(obj->npp, obj->stream, NPRES_USER_BREAK); + INT32_TO_NPVARIANT(npError, *result); + return true; +} + +static bool destroyNullStream(PluginObject* obj, const NPVariant* args, uint32_t argCount, NPVariant* result) +{ + NPError npError = browser->destroystream(obj->npp, 0, NPRES_USER_BREAK); + INT32_TO_NPVARIANT(npError, *result); + return true; +} + +static bool testEnumerate(PluginObject* obj, const NPVariant* args, uint32_t argCount, NPVariant* result) +{ + if (argCount != 2 || !NPVARIANT_IS_OBJECT(args[0]) || !NPVARIANT_IS_OBJECT(args[1])) + return false; + + uint32_t count; + NPIdentifier* identifiers; + if (browser->enumerate(obj->npp, NPVARIANT_TO_OBJECT(args[0]), &identifiers, &count)) { + NPObject* outArray = NPVARIANT_TO_OBJECT(args[1]); + NPIdentifier pushIdentifier = browser->getstringidentifier("push"); + + for (uint32_t i = 0; i < count; i++) { + NPUTF8* string = browser->utf8fromidentifier(identifiers[i]); + + if (!string) + continue; + + NPVariant args[1]; + STRINGZ_TO_NPVARIANT(string, args[0]); + NPVariant browserResult; + browser->invoke(obj->npp, outArray, pushIdentifier, args, 1, &browserResult); + browser->releasevariantvalue(&browserResult); + browser->memfree(string); + } + + browser->memfree(identifiers); + } + + VOID_TO_NPVARIANT(*result); + return true; +} + +static bool testGetIntIdentifier(PluginObject*, const NPVariant* args, uint32_t argCount, NPVariant* result) +{ + if (argCount != 1 || !NPVARIANT_IS_DOUBLE(args[0])) + return false; + + NPIdentifier identifier = browser->getintidentifier((int)NPVARIANT_TO_DOUBLE(args[0])); + INT32_TO_NPVARIANT((int32)(long long)identifier, *result); + return true; +} + +static bool testGetProperty(PluginObject* obj, const NPVariant* args, uint32_t argCount, NPVariant* result) +{ + if (argCount == 0) + return false; + + NPObject *object; + browser->getvalue(obj->npp, NPNVWindowNPObject, &object); + + for (uint32_t i = 0; i < argCount; i++) { + assert(NPVARIANT_IS_STRING(args[i])); + NPUTF8* propertyString = createCStringFromNPVariant(&args[i]); + NPIdentifier propertyIdentifier = browser->getstringidentifier(propertyString); + free(propertyString); + + NPVariant variant; + bool retval = browser->getproperty(obj->npp, object, propertyIdentifier, &variant); + browser->releaseobject(object); + + if (!retval) + break; + + if (i + 1 < argCount) { + assert(NPVARIANT_IS_OBJECT(variant)); + object = NPVARIANT_TO_OBJECT(variant); + } else { + *result = variant; + return true; + } + } + + VOID_TO_NPVARIANT(*result); + return false; +} + +static bool testEvaluate(PluginObject* obj, const NPVariant* args, uint32_t argCount, NPVariant* result) +{ + if (argCount != 1 || !NPVARIANT_IS_STRING(args[0])) + return false; + NPObject* windowScriptObject; + browser->getvalue(obj->npp, NPNVWindowNPObject, &windowScriptObject); + + NPString s = NPVARIANT_TO_STRING(args[0]); + + bool retval = browser->evaluate(obj->npp, windowScriptObject, &s, result); + browser->releaseobject(windowScriptObject); + return retval; +} + +static bool testGetPropertyReturnValue(PluginObject* obj, const NPVariant* args, uint32_t argCount, NPVariant* result) +{ + if (argCount != 2 || !NPVARIANT_IS_OBJECT(args[0]) || !NPVARIANT_IS_STRING(args[1])) + return false; + + NPUTF8* propertyString = createCStringFromNPVariant(&args[1]); + NPIdentifier propertyIdentifier = browser->getstringidentifier(propertyString); + free(propertyString); + + NPVariant variant; + bool retval = browser->getproperty(obj->npp, NPVARIANT_TO_OBJECT(args[0]), propertyIdentifier, &variant); + if (retval) + browser->releasevariantvalue(&variant); + + BOOLEAN_TO_NPVARIANT(retval, *result); + return true; +} + +static char* toCString(const NPString& string) +{ + char* result = static_cast<char*>(malloc(string.UTF8Length + 1)); + memcpy(result, string.UTF8Characters, string.UTF8Length); + result[string.UTF8Length] = '\0'; + + return result; +} + +static bool testPostURLFile(PluginObject* obj, const NPVariant* args, uint32_t argCount, NPVariant* result) +{ + if (argCount != 4 || !NPVARIANT_IS_STRING(args[0]) || !NPVARIANT_IS_STRING(args[1]) || !NPVARIANT_IS_STRING(args[2]) || !NPVARIANT_IS_STRING(args[3])) + return false; + + NPString urlString = NPVARIANT_TO_STRING(args[0]); + char* url = toCString(urlString); + + NPString targetString = NPVARIANT_TO_STRING(args[1]); + char* target = toCString(targetString); + + NPString pathString = NPVARIANT_TO_STRING(args[2]); + char* path = toCString(pathString); + + NPString contentsString = NPVARIANT_TO_STRING(args[3]); + + FILE* tempFile = fopen(path, "w"); + if (!tempFile) + return false; + + fwrite(contentsString.UTF8Characters, contentsString.UTF8Length, 1, tempFile); + fclose(tempFile); + + NPError error = browser->posturl(obj->npp, url, target, pathString.UTF8Length, path, TRUE); + + free(path); + free(target); + free(url); + + BOOLEAN_TO_NPVARIANT(error == NPERR_NO_ERROR, *result); + return true; +} + +static bool testConstruct(PluginObject* obj, const NPVariant* args, uint32_t argCount, NPVariant* result) +{ + if (!argCount || !NPVARIANT_IS_OBJECT(args[0])) + return false; + + return browser->construct(obj->npp, NPVARIANT_TO_OBJECT(args[0]), args + 1, argCount - 1, result); +} + +static bool pluginInvoke(NPObject* header, NPIdentifier name, const NPVariant* args, uint32_t argCount, NPVariant* result) +{ + PluginObject* plugin = reinterpret_cast<PluginObject*>(header); + if (name == pluginMethodIdentifiers[ID_TEST_CALLBACK_METHOD]) + return testCallback(plugin, args, argCount, result); + else if (name == pluginMethodIdentifiers[ID_TEST_GETURL]) + return getURL(plugin, args, argCount, result); + else if (name == pluginMethodIdentifiers[ID_REMOVE_DEFAULT_METHOD]) + return removeDefaultMethod(plugin, args, argCount, result); + else if (name == pluginMethodIdentifiers[ID_TEST_DOM_ACCESS]) + return testDOMAccess(plugin, args, argCount, result); + else if (name == pluginMethodIdentifiers[ID_TEST_GET_URL_NOTIFY]) + return getURLNotify(plugin, args, argCount, result); + else if (name == pluginMethodIdentifiers[ID_TEST_INVOKE_DEFAULT]) + return testInvokeDefault(plugin, args, argCount, result); + else if (name == pluginMethodIdentifiers[ID_TEST_ENUMERATE]) + return testEnumerate(plugin, args, argCount, result); + else if (name == pluginMethodIdentifiers[ID_DESTROY_STREAM]) + return destroyStream(plugin, args, argCount, result); + else if (name == pluginMethodIdentifiers[ID_TEST_GETINTIDENTIFIER]) + return testGetIntIdentifier(plugin, args, argCount, result); + else if (name == pluginMethodIdentifiers[ID_TEST_EVALUATE]) + return testEvaluate(plugin, args, argCount, result); + else if (name == pluginMethodIdentifiers[ID_TEST_GET_PROPERTY]) + return testGetProperty(plugin, args, argCount, result); + else if (name == pluginMethodIdentifiers[ID_TEST_GET_PROPERTY_RETURN_VALUE]) + return testGetPropertyReturnValue(plugin, args, argCount, result); + else if (name == pluginMethodIdentifiers[ID_TEST_IDENTIFIER_TO_STRING]) + return testIdentifierToString(plugin, args, argCount, result); + else if (name == pluginMethodIdentifiers[ID_TEST_IDENTIFIER_TO_INT]) + return testIdentifierToInt(plugin, args, argCount, result); + else if (name == pluginMethodIdentifiers[ID_TEST_POSTURL_FILE]) + return testPostURLFile(plugin, args, argCount, result); + else if (name == pluginMethodIdentifiers[ID_TEST_CONSTRUCT]) + return testConstruct(plugin, args, argCount, result); + else if (name == pluginMethodIdentifiers[ID_TEST_THROW_EXCEPTION_METHOD]) { + browser->setexception(header, "plugin object testThrowException SUCCESS"); + return true; + } else if (name == pluginMethodIdentifiers[ID_DESTROY_NULL_STREAM]) + return destroyNullStream(plugin, args, argCount, result); + + return false; +} + +static bool pluginInvokeDefault(NPObject* obj, const NPVariant* args, uint32_t argCount, NPVariant* result) +{ + INT32_TO_NPVARIANT(1, *result); + return true; +} + +static void pluginInvalidate(NPObject* header) +{ + PluginObject* plugin = reinterpret_cast<PluginObject*>(header); + plugin->testObject = 0; +} + +static NPObject *pluginAllocate(NPP npp, NPClass *theClass) +{ + PluginObject* newInstance = (PluginObject*)malloc(sizeof(PluginObject)); + + if (!identifiersInitialized) { + identifiersInitialized = true; + initializeIdentifiers(); + } + + newInstance->npp = npp; + newInstance->testObject = browser->createobject(npp, getTestClass()); + newInstance->eventLogging = FALSE; + newInstance->onStreamLoad = 0; + newInstance->onStreamDestroy = 0; + newInstance->onURLNotify = 0; + newInstance->logDestroy = FALSE; + newInstance->logSetWindow = FALSE; + newInstance->returnErrorFromNewStream = FALSE; + newInstance->stream = 0; + + newInstance->firstUrl = NULL; + newInstance->firstHeaders = NULL; + newInstance->lastUrl = NULL; + newInstance->lastHeaders = NULL; + + return (NPObject*)newInstance; +} + +static void pluginDeallocate(NPObject* header) +{ + PluginObject* plugin = reinterpret_cast<PluginObject*>(header); + if (plugin->testObject) + browser->releaseobject(plugin->testObject); + + free(plugin->firstUrl); + free(plugin->firstHeaders); + free(plugin->lastUrl); + free(plugin->lastHeaders); + free(plugin); +} + +void handleCallback(PluginObject* object, const char *url, NPReason reason, void *notifyData) +{ + assert(object); + + NPVariant args[2]; + + NPObject *windowScriptObject; + browser->getvalue(object->npp, NPNVWindowNPObject, &windowScriptObject); + + NPIdentifier callbackIdentifier = notifyData; + + INT32_TO_NPVARIANT(reason, args[0]); + + char *strHdr = NULL; + if (object->firstUrl && object->firstHeaders && object->lastUrl && object->lastHeaders) { + // Format expected by JavaScript validator: four fields separated by \n\n: + // First URL; first header block; last URL; last header block. + // Note that header blocks already end with \n due to how NPStream::headers works. + int len = strlen(object->firstUrl) + 2 + + strlen(object->firstHeaders) + 1 + + strlen(object->lastUrl) + 2 + + strlen(object->lastHeaders) + 1; + strHdr = (char*)malloc(len + 1); + snprintf(strHdr, len + 1, "%s\n\n%s\n%s\n\n%s\n", + object->firstUrl, object->firstHeaders, object->lastUrl, object->lastHeaders); + STRINGN_TO_NPVARIANT(strHdr, len, args[1]); + } else + NULL_TO_NPVARIANT(args[1]); + + NPVariant browserResult; + browser->invoke(object->npp, windowScriptObject, callbackIdentifier, args, 2, &browserResult); + browser->releasevariantvalue(&browserResult); + + free(strHdr); +} + +void notifyStream(PluginObject* object, const char *url, const char *headers) +{ + if (object->firstUrl == NULL) { + if (url) + object->firstUrl = strdup(url); + if (headers) + object->firstHeaders = strdup(headers); + } else { + free(object->lastUrl); + free(object->lastHeaders); + object->lastUrl = (url ? strdup(url) : NULL); + object->lastHeaders = (headers ? strdup(headers) : NULL); + } +} + +void testNPRuntime(NPP npp) +{ + NPObject* windowScriptObject; + browser->getvalue(npp, NPNVWindowNPObject, &windowScriptObject); + + // Invoke + NPIdentifier testNPInvoke = browser->getstringidentifier("testNPInvoke"); + NPVariant args[7]; + + VOID_TO_NPVARIANT(args[0]); + NULL_TO_NPVARIANT(args[1]); + BOOLEAN_TO_NPVARIANT(true, args[2]); + INT32_TO_NPVARIANT(242, args[3]); + DOUBLE_TO_NPVARIANT(242.242, args[4]); + STRINGZ_TO_NPVARIANT("Hello, World", args[5]); + OBJECT_TO_NPVARIANT(windowScriptObject, args[6]); + + NPVariant result; + if (browser->invoke(npp, windowScriptObject, testNPInvoke, args, 7, &result)) + browser->releasevariantvalue(&result); + + browser->releaseobject(windowScriptObject); +} diff --git a/WebKitTools/DumpRenderTree/TestNetscapePlugIn.subproj/PluginObject.h b/WebKitTools/DumpRenderTree/TestNetscapePlugIn.subproj/PluginObject.h new file mode 100644 index 0000000..77c35e0 --- /dev/null +++ b/WebKitTools/DumpRenderTree/TestNetscapePlugIn.subproj/PluginObject.h @@ -0,0 +1,55 @@ +/* + * Copyright (C) 2006, 2007 Apple Inc. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY + * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY + * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include <WebKit/npfunctions.h> + +extern NPNetscapeFuncs *browser; + +typedef struct { + NPObject header; + + NPP npp; + NPBool eventLogging; + NPBool logSetWindow; + NPBool logDestroy; + NPBool returnErrorFromNewStream; + NPObject* testObject; + NPStream* stream; + char* onStreamLoad; + char* onStreamDestroy; + char* onURLNotify; + char* firstUrl; + char* firstHeaders; + char* lastUrl; + char* lastHeaders; +#ifdef XP_MACOSX + NPEventModel eventModel; +#endif +} PluginObject; + +extern NPClass *getPluginClass(void); +extern void handleCallback(PluginObject* object, const char *url, NPReason reason, void *notifyData); +extern void notifyStream(PluginObject* object, const char *url, const char *headers); +extern void testNPRuntime(NPP npp); diff --git a/WebKitTools/DumpRenderTree/TestNetscapePlugIn.subproj/TestObject.cpp b/WebKitTools/DumpRenderTree/TestNetscapePlugIn.subproj/TestObject.cpp new file mode 100644 index 0000000..af60df4 --- /dev/null +++ b/WebKitTools/DumpRenderTree/TestNetscapePlugIn.subproj/TestObject.cpp @@ -0,0 +1,167 @@ +/* + * Copyright (C) 2007 Apple Inc. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY + * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY + * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include "TestObject.h" +#include "PluginObject.h" + +#include <string.h> +#include <stdlib.h> + +static bool testEnumerate(NPObject *npobj, NPIdentifier **value, uint32_t *count); +static bool testHasMethod(NPObject*, NPIdentifier name); +static bool testInvoke(NPObject*, NPIdentifier name, const NPVariant* args, uint32_t argCount, NPVariant* result); +static bool testHasProperty(NPObject*, NPIdentifier name); +static bool testGetProperty(NPObject*, NPIdentifier name, NPVariant*); +static NPObject *testAllocate(NPP npp, NPClass *theClass); +static void testDeallocate(NPObject *obj); +static bool testConstruct(NPObject* obj, const NPVariant* args, uint32_t argCount, NPVariant* result); + +static NPClass testClass = { + NP_CLASS_STRUCT_VERSION, + testAllocate, + testDeallocate, + 0, + testHasMethod, + testInvoke, + 0, + testHasProperty, + testGetProperty, + 0, + 0, + testEnumerate, + testConstruct +}; + +NPClass *getTestClass(void) +{ + return &testClass; +} + +static bool identifiersInitialized = false; + +#define ID_OBJECT_POINTER 2 + +#define NUM_ENUMERATABLE_TEST_IDENTIFIERS 2 +#define NUM_TEST_IDENTIFIERS 3 + +static NPIdentifier testIdentifiers[NUM_TEST_IDENTIFIERS]; +static const NPUTF8 *testIdentifierNames[NUM_TEST_IDENTIFIERS] = { + "foo", + "bar", + "objectPointer", +}; + +#define ID_THROW_EXCEPTION_METHOD 0 +#define NUM_METHOD_IDENTIFIERS 1 + +static NPIdentifier testMethodIdentifiers[NUM_METHOD_IDENTIFIERS]; +static const NPUTF8 *testMethodIdentifierNames[NUM_METHOD_IDENTIFIERS] = { + "throwException", +}; + +static void initializeIdentifiers(void) +{ + browser->getstringidentifiers(testIdentifierNames, NUM_TEST_IDENTIFIERS, testIdentifiers); + browser->getstringidentifiers(testMethodIdentifierNames, NUM_METHOD_IDENTIFIERS, testMethodIdentifiers); +} + +static NPObject *testAllocate(NPP npp, NPClass *theClass) +{ + NPObject *newInstance = static_cast<NPObject*>(malloc(sizeof(NPObject))); + + if (!identifiersInitialized) { + identifiersInitialized = true; + initializeIdentifiers(); + } + + return newInstance; +} + +static void testDeallocate(NPObject *obj) +{ + free(obj); +} + +static bool testHasMethod(NPObject*, NPIdentifier name) +{ + for (unsigned i = 0; i < NUM_METHOD_IDENTIFIERS; i++) { + if (testMethodIdentifiers[i] == name) + return true; + } + return false; +} + +static bool testInvoke(NPObject* header, NPIdentifier name, const NPVariant* args, uint32_t argCount, NPVariant* result) +{ + if (name == testMethodIdentifiers[ID_THROW_EXCEPTION_METHOD]) { + browser->setexception(header, "test object throwException SUCCESS"); + return true; + } + return false; +} + +static bool testHasProperty(NPObject*, NPIdentifier name) +{ + for (unsigned i = 0; i < NUM_TEST_IDENTIFIERS; i++) { + if (testIdentifiers[i] == name) + return true; + } + + return false; +} + +static bool testGetProperty(NPObject* npobj, NPIdentifier name, NPVariant* result) +{ + if (name == testIdentifiers[ID_OBJECT_POINTER]) { + int32_t objectPointer = static_cast<int32_t>(reinterpret_cast<long long>(npobj)); + + INT32_TO_NPVARIANT(objectPointer, *result); + return true; + } + + return false; +} + + +static bool testEnumerate(NPObject *npobj, NPIdentifier **value, uint32_t *count) +{ + *count = NUM_ENUMERATABLE_TEST_IDENTIFIERS; + + *value = (NPIdentifier*)browser->memalloc(NUM_ENUMERATABLE_TEST_IDENTIFIERS * sizeof(NPIdentifier)); + memcpy(*value, testIdentifiers, sizeof(NPIdentifier) * NUM_ENUMERATABLE_TEST_IDENTIFIERS); + + return true; +} + +static bool testConstruct(NPObject* npobj, const NPVariant* args, uint32_t argCount, NPVariant* result) +{ + browser->retainobject(npobj); + + // Just return the same object. + OBJECT_TO_NPVARIANT(npobj, *result); + return true; +} + + diff --git a/WebKitTools/DumpRenderTree/TestNetscapePlugIn.subproj/TestObject.h b/WebKitTools/DumpRenderTree/TestNetscapePlugIn.subproj/TestObject.h new file mode 100644 index 0000000..1295fb7 --- /dev/null +++ b/WebKitTools/DumpRenderTree/TestNetscapePlugIn.subproj/TestObject.h @@ -0,0 +1,29 @@ +/* + * Copyright (C) 2007 Apple Inc. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY + * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY + * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include <WebKit/npapi.h> +#include <WebKit/npruntime.h> + +NPClass *getTestClass(void); diff --git a/WebKitTools/DumpRenderTree/TestNetscapePlugIn.subproj/main.cpp b/WebKitTools/DumpRenderTree/TestNetscapePlugIn.subproj/main.cpp new file mode 100644 index 0000000..c764cfd --- /dev/null +++ b/WebKitTools/DumpRenderTree/TestNetscapePlugIn.subproj/main.cpp @@ -0,0 +1,423 @@ +/* + * Copyright (C) 2006, 2007 Apple Inc. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY + * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY + * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#import "PluginObject.h" + +static void log(NPP instance, const char* format, ...) +{ + va_list args; + va_start(args, format); + char message[2048] = "PLUGIN: "; + vsprintf(message + strlen(message), format, args); + va_end(args); + + NPObject* windowObject = 0; + NPError error = browser->getvalue(instance, NPNVWindowNPObject, &windowObject); + if (error != NPERR_NO_ERROR) { + fprintf(stderr, "Failed to retrieve window object while logging: %s\n", message); + return; + } + + NPVariant consoleVariant; + if (!browser->getproperty(instance, windowObject, browser->getstringidentifier("console"), &consoleVariant)) { + fprintf(stderr, "Failed to retrieve console object while logging: %s\n", message); + browser->releaseobject(windowObject); + return; + } + + NPObject* consoleObject = NPVARIANT_TO_OBJECT(consoleVariant); + + NPVariant messageVariant; + STRINGZ_TO_NPVARIANT(message, messageVariant); + + NPVariant result; + if (!browser->invoke(instance, consoleObject, browser->getstringidentifier("log"), &messageVariant, 1, &result)) { + fprintf(stderr, "Failed to invoke console.log while logging: %s\n", message); + browser->releaseobject(consoleObject); + browser->releaseobject(windowObject); + return; + } + + browser->releasevariantvalue(&result); + browser->releaseobject(consoleObject); + browser->releaseobject(windowObject); +} + +// Mach-o entry points +extern "C" { + NPError NP_Initialize(NPNetscapeFuncs *browserFuncs); + NPError NP_GetEntryPoints(NPPluginFuncs *pluginFuncs); + void NP_Shutdown(void); +} + +// Mach-o entry points +NPError NP_Initialize(NPNetscapeFuncs *browserFuncs) +{ + browser = browserFuncs; + return NPERR_NO_ERROR; +} + +NPError NP_GetEntryPoints(NPPluginFuncs *pluginFuncs) +{ + pluginFuncs->version = 11; + pluginFuncs->size = sizeof(pluginFuncs); + pluginFuncs->newp = NPP_New; + pluginFuncs->destroy = NPP_Destroy; + pluginFuncs->setwindow = NPP_SetWindow; + pluginFuncs->newstream = NPP_NewStream; + pluginFuncs->destroystream = NPP_DestroyStream; + pluginFuncs->asfile = NPP_StreamAsFile; + pluginFuncs->writeready = NPP_WriteReady; + pluginFuncs->write = (NPP_WriteProcPtr)NPP_Write; + pluginFuncs->print = NPP_Print; + pluginFuncs->event = NPP_HandleEvent; + pluginFuncs->urlnotify = NPP_URLNotify; + pluginFuncs->getvalue = NPP_GetValue; + pluginFuncs->setvalue = NPP_SetValue; + + return NPERR_NO_ERROR; +} + +void NP_Shutdown(void) +{ +} + +NPError NPP_New(NPMIMEType pluginType, NPP instance, uint16 mode, int16 argc, char *argn[], char *argv[], NPSavedData *saved) +{ + bool forceCarbon = false; + + // Always turn on the CG model + NPBool supportsCoreGraphics; + if (browser->getvalue(instance, NPNVsupportsCoreGraphicsBool, &supportsCoreGraphics) != NPERR_NO_ERROR) + supportsCoreGraphics = false; + + if (!supportsCoreGraphics) + return NPERR_INCOMPATIBLE_VERSION_ERROR; + + browser->setvalue(instance, NPPVpluginDrawingModel, (void *)NPDrawingModelCoreGraphics); + + PluginObject* obj = (PluginObject*)browser->createobject(instance, getPluginClass()); + instance->pdata = obj; + + for (int i = 0; i < argc; i++) { + if (strcasecmp(argn[i], "onstreamload") == 0 && !obj->onStreamLoad) + obj->onStreamLoad = strdup(argv[i]); + else if (strcasecmp(argn[i], "onStreamDestroy") == 0 && !obj->onStreamDestroy) + obj->onStreamDestroy = strdup(argv[i]); + else if (strcasecmp(argn[i], "onURLNotify") == 0 && !obj->onURLNotify) + obj->onURLNotify = strdup(argv[i]); + else if (strcasecmp(argn[i], "src") == 0 && + strcasecmp(argv[i], "data:application/x-webkit-test-netscape,returnerrorfromnewstream") == 0) + obj->returnErrorFromNewStream = TRUE; + else if (strcasecmp(argn[i], "logfirstsetwindow") == 0) + obj->logSetWindow = TRUE; + else if (strcasecmp(argn[i], "testnpruntime") == 0) + testNPRuntime(instance); + else if (strcasecmp(argn[i], "forcecarbon") == 0) + forceCarbon = true; + } + + + if (forceCarbon) { + NPBool supportsCarbon; + +#ifndef NP_NO_CARBON + if (browser->getvalue(instance, NPNVsupportsCarbonBool, &supportsCarbon) != NPERR_NO_ERROR) + supportsCarbon = false; +#else + supportsCarbon = false; +#endif + if (!supportsCarbon) + return NPERR_INCOMPATIBLE_VERSION_ERROR; + +#ifndef NP_NO_CARBON + obj->eventModel = NPEventModelCarbon; +#endif + } else { + NPBool supportsCocoa; + if (browser->getvalue(instance, NPNVsupportsCocoaBool, &supportsCocoa) != NPERR_NO_ERROR) + supportsCocoa = FALSE; + + if (!supportsCocoa) + return NPERR_INCOMPATIBLE_VERSION_ERROR; + + obj->eventModel = NPEventModelCocoa; + } + + browser->setvalue(instance, NPPVpluginEventModel, (void *)obj->eventModel); + return NPERR_NO_ERROR; +} + +NPError NPP_Destroy(NPP instance, NPSavedData **save) +{ + PluginObject* obj = static_cast<PluginObject*>(instance->pdata); + if (obj) { + if (obj->onStreamLoad) + free(obj->onStreamLoad); + + if (obj->onStreamDestroy) + free(obj->onStreamDestroy); + + if (obj->onURLNotify) + free(obj->onURLNotify); + + if (obj->logDestroy) + log(instance, "NPP_Destroy"); + + browser->releaseobject(&obj->header); + } + return NPERR_NO_ERROR; +} + +NPError NPP_SetWindow(NPP instance, NPWindow *window) +{ + PluginObject* obj = static_cast<PluginObject*>(instance->pdata); + + if (obj) { + if (obj->logSetWindow) { + log(instance, "NPP_SetWindow: %d %d", (int)window->width, (int)window->height); + obj->logSetWindow = false; + } + } + + return NPERR_NO_ERROR; +} + +static void executeScript(const PluginObject* obj, const char* script) +{ + NPObject *windowScriptObject; + browser->getvalue(obj->npp, NPNVWindowNPObject, &windowScriptObject); + + NPString npScript; + npScript.UTF8Characters = script; + npScript.UTF8Length = strlen(script); + + NPVariant browserResult; + browser->evaluate(obj->npp, windowScriptObject, &npScript, &browserResult); + browser->releasevariantvalue(&browserResult); +} + +NPError NPP_NewStream(NPP instance, NPMIMEType type, NPStream *stream, NPBool seekable, uint16 *stype) +{ + PluginObject* obj = static_cast<PluginObject*>(instance->pdata); + obj->stream = stream; + *stype = NP_ASFILEONLY; + + if (obj->returnErrorFromNewStream) + return NPERR_GENERIC_ERROR; + + if (browser->version >= NPVERS_HAS_RESPONSE_HEADERS) + notifyStream(obj, stream->url, stream->headers); + + if (obj->onStreamLoad) + executeScript(obj, obj->onStreamLoad); + + return NPERR_NO_ERROR; +} + +NPError NPP_DestroyStream(NPP instance, NPStream *stream, NPReason reason) +{ + PluginObject* obj = (PluginObject*)instance->pdata; + + if (obj->onStreamDestroy) + executeScript(obj, obj->onStreamDestroy); + + return NPERR_NO_ERROR; +} + +int32 NPP_WriteReady(NPP instance, NPStream *stream) +{ + return 0; +} + +int32 NPP_Write(NPP instance, NPStream *stream, int32 offset, int32 len, void *buffer) +{ + return 0; +} + +void NPP_StreamAsFile(NPP instance, NPStream *stream, const char *fname) +{ +} + +void NPP_Print(NPP instance, NPPrint *platformPrint) +{ +} + +#ifndef NP_NO_CARBON +static int16_t handleEventCarbon(NPP instance, PluginObject* obj, EventRecord* event) +{ + Point pt = { event->where.v, event->where.h }; + + switch (event->what) { + case nullEvent: + // these are delivered non-deterministically, don't log. + break; + case mouseDown: + GlobalToLocal(&pt); + log(instance, "mouseDown at (%d, %d)", pt.h, pt.v); + break; + case mouseUp: + GlobalToLocal(&pt); + log(instance, "mouseUp at (%d, %d)", pt.h, pt.v); + break; + case keyDown: + log(instance, "keyDown '%c'", (char)(event->message & 0xFF)); + break; + case keyUp: + log(instance, "keyUp '%c'", (char)(event->message & 0xFF)); + break; + case autoKey: + log(instance, "autoKey '%c'", (char)(event->message & 0xFF)); + break; + case updateEvt: + log(instance, "updateEvt"); + break; + case diskEvt: + log(instance, "diskEvt"); + break; + case activateEvt: + log(instance, "activateEvt"); + break; + case osEvt: + printf("PLUGIN: osEvt - "); + switch ((event->message & 0xFF000000) >> 24) { + case suspendResumeMessage: + printf("%s\n", (event->message & 0x1) ? "resume" : "suspend"); + break; + case mouseMovedMessage: + printf("mouseMoved\n"); + break; + default: + printf("%08lX\n", event->message); + } + break; + case kHighLevelEvent: + log(instance, "kHighLevelEvent"); + break; + // NPAPI events + case getFocusEvent: + log(instance, "getFocusEvent"); + break; + case loseFocusEvent: + log(instance, "loseFocusEvent"); + break; + case adjustCursorEvent: + log(instance, "adjustCursorEvent"); + break; + default: + log(instance, "event %d", event->what); + } + + return 0; +} +#endif + +static int16_t handleEventCocoa(NPP instance, PluginObject* obj, NPCocoaEvent* event) +{ + switch (event->type) { + case NPCocoaEventWindowFocusChanged: + + case NPCocoaEventFocusChanged: + if (event->data.focus.hasFocus) + log(instance, "getFocusEvent"); + else + log(instance, "loseFocusEvent"); + return 1; + + case NPCocoaEventDrawRect: + return 1; + + case NPCocoaEventKeyDown: + case NPCocoaEventKeyUp: + case NPCocoaEventFlagsChanged: + return 1; + + case NPCocoaEventMouseDown: + log(instance, "mouseDown at (%d, %d)", + (int)event->data.mouse.pluginX, + (int)event->data.mouse.pluginY); + return 1; + case NPCocoaEventMouseUp: + log(instance, "mouseUp at (%d, %d)", + (int)event->data.mouse.pluginX, + (int)event->data.mouse.pluginY); + return 1; + + case NPCocoaEventMouseMoved: + case NPCocoaEventMouseEntered: + case NPCocoaEventMouseExited: + case NPCocoaEventMouseDragged: + case NPCocoaEventScrollWheel: + case NPCocoaEventTextInput: + return 1; + } + + return 0; +} + +int16 NPP_HandleEvent(NPP instance, void *event) +{ + PluginObject* obj = static_cast<PluginObject*>(instance->pdata); + if (!obj->eventLogging) + return 0; + +#ifndef NP_NO_CARBON + if (obj->eventModel == NPEventModelCarbon) + return handleEventCarbon(instance, obj, static_cast<EventRecord*>(event)); +#endif + + assert(obj->eventModel == NPEventModelCocoa); + return handleEventCocoa(instance, obj, static_cast<NPCocoaEvent*>(event)); +} + +void NPP_URLNotify(NPP instance, const char *url, NPReason reason, void *notifyData) +{ + PluginObject* obj = static_cast<PluginObject*>(instance->pdata); + + if (obj->onURLNotify) + executeScript(obj, obj->onURLNotify); + + handleCallback(obj, url, reason, notifyData); +} + +NPError NPP_GetValue(NPP instance, NPPVariable variable, void *value) +{ + PluginObject* obj = static_cast<PluginObject*>(instance->pdata); + + if (variable == NPPVpluginScriptableNPObject) { + void **v = (void **)value; + // Return value is expected to be retained + browser->retainobject((NPObject *)obj); + *v = obj; + return NPERR_NO_ERROR; + } + + return NPERR_GENERIC_ERROR; +} + +NPError NPP_SetValue(NPP instance, NPNVariable variable, void *value) +{ + return NPERR_GENERIC_ERROR; +} |