From b4a23918d35287231c841d3f3f8a41f88b76148c Mon Sep 17 00:00:00 2001 From: Derek Sollenberger Date: Mon, 9 Nov 2009 15:38:58 -0500 Subject: [PATCH] Updating sample plugin to test java class loading. --- .../jni/background/BackgroundPlugin.cpp | 38 ++++++++++++++++++++++ .../jni/background/BackgroundPlugin.h | 1 + samples/BrowserPlugin/jni/main.cpp | 9 +++-- samples/BrowserPlugin/jni/main.h | 1 + .../com/android/sampleplugin/BackgroundTest.java | 11 +++++++ 5 files changed, 55 insertions(+), 5 deletions(-) create mode 100644 samples/BrowserPlugin/src/com/android/sampleplugin/BackgroundTest.java diff --git a/samples/BrowserPlugin/jni/background/BackgroundPlugin.cpp b/samples/BrowserPlugin/jni/background/BackgroundPlugin.cpp index af518a9a..0f90291e 100644 --- a/samples/BrowserPlugin/jni/background/BackgroundPlugin.cpp +++ b/samples/BrowserPlugin/jni/background/BackgroundPlugin.cpp @@ -38,6 +38,7 @@ extern ANPCanvasInterfaceV0 gCanvasI; extern ANPLogInterfaceV0 gLogI; extern ANPPaintInterfaceV0 gPaintI; extern ANPSurfaceInterfaceV0 gSurfaceI; +extern ANPSystemInterfaceV0 gSystemI; extern ANPTypefaceInterfaceV0 gTypefaceI; #define ARRAY_COUNT(array) (sizeof(array) / sizeof(array[0])) @@ -66,6 +67,7 @@ BackgroundPlugin::BackgroundPlugin(NPP inst) : SurfaceSubPlugin(inst) { test_bitmaps(); // android bitmaps test_domAccess(); test_javascript(); + test_loadJavaClass(); } BackgroundPlugin::~BackgroundPlugin() { @@ -421,3 +423,39 @@ void BackgroundPlugin::test_javascript() { // free the memory allocated within the browser browser->memfree(stringMem); } + +/////////////////////////////////////////////////////////////////////////////// +// Load Java Classes Tests +/////////////////////////////////////////////////////////////////////////////// + +void BackgroundPlugin::test_loadJavaClass() { + + JNIEnv* env = NULL; + if (gVM->GetEnv((void**) &env, JNI_VERSION_1_4) != JNI_OK) { + gLogI.log(inst(), kError_ANPLogType, " ---- LoadJavaTest: failed to get env"); + return; + } + + const char* className = "com.android.sampleplugin.BackgroundTest"; + jclass backgroundClass = gSystemI.loadJavaClass(inst(), className); + + if(!backgroundClass) { + gLogI.log(inst(), kError_ANPLogType, " ---- LoadJavaTest: failed to load class"); + return; + } + + jmethodID constructor = env->GetMethodID(backgroundClass, "", "()V"); + jmethodID addMethod = env->GetMethodID(backgroundClass, "addInt", "(II)I"); + jobject backgroundObject = env->NewObject(backgroundClass, constructor); + + if(!backgroundObject) { + gLogI.log(inst(), kError_ANPLogType, " ---- LoadJavaTest: failed to construct object"); + return; + } + + jint result = env->CallIntMethod(backgroundObject, addMethod, 2, 2); + + if (result != 4) { + gLogI.log(inst(), kError_ANPLogType, " ---- LoadJavaTest: invalid result (%d != 4)", result); + } +} diff --git a/samples/BrowserPlugin/jni/background/BackgroundPlugin.h b/samples/BrowserPlugin/jni/background/BackgroundPlugin.h index d3708109..0e8f6f7c 100644 --- a/samples/BrowserPlugin/jni/background/BackgroundPlugin.h +++ b/samples/BrowserPlugin/jni/background/BackgroundPlugin.h @@ -62,6 +62,7 @@ private: void test_bitmap_transparency(const ANPEvent* evt); void test_domAccess(); void test_javascript(); + void test_loadJavaClass(); }; diff --git a/samples/BrowserPlugin/jni/main.cpp b/samples/BrowserPlugin/jni/main.cpp index 6f0ed96a..4bc825e4 100644 --- a/samples/BrowserPlugin/jni/main.cpp +++ b/samples/BrowserPlugin/jni/main.cpp @@ -157,9 +157,8 @@ NPError NPP_New(NPMIMEType pluginType, NPP instance, uint16 mode, int16 argc, // Scripting functions appeared in NPAPI version 14 if (browser->version >= 14) { - instance->pdata = browser->createobject (instance, getPluginClass()); - obj = static_cast(instance->pdata); - bzero(obj, sizeof(*obj)); + instance->pdata = browser->createobject (instance, getPluginClass()); + obj = static_cast(instance->pdata); } /* END: STANDARD PLUGIN FRAMEWORK */ @@ -407,9 +406,9 @@ NPError NPP_GetValue(NPP instance, NPPVariable variable, void *value) PluginObject *obj = (PluginObject*) instance->pdata; if (obj) - browser->retainobject((NPObject*)obj); + browser->retainobject(&obj->header); - *v = obj; + *v = &(obj->header); return NPERR_NO_ERROR; } diff --git a/samples/BrowserPlugin/jni/main.h b/samples/BrowserPlugin/jni/main.h index 5906b0b8..66629e6b 100644 --- a/samples/BrowserPlugin/jni/main.h +++ b/samples/BrowserPlugin/jni/main.h @@ -28,6 +28,7 @@ #include #include "android_npapi.h" #include "ANPSurface_npapi.h" +#include "ANPSystem_npapi.h" extern NPNetscapeFuncs* browser; extern JavaVM* gVM; diff --git a/samples/BrowserPlugin/src/com/android/sampleplugin/BackgroundTest.java b/samples/BrowserPlugin/src/com/android/sampleplugin/BackgroundTest.java new file mode 100644 index 00000000..1f6b0d4e --- /dev/null +++ b/samples/BrowserPlugin/src/com/android/sampleplugin/BackgroundTest.java @@ -0,0 +1,11 @@ +package com.android.sampleplugin; + +public class BackgroundTest { + + public BackgroundTest() {} + + public int addInt(int x, int y) { + return x + y; + } + +} -- 2.11.0