Refactoring plugins to use new java interfaces.

This change contains extensive cleanup as we now keep track of a pointer
to the plugin's java entry point (WebkitPlugin.class). Also given that we
track this object and changes to plugin packaging we nolonger need to pass
additional parameters in quite a few methods.
diff --git a/WebCore/plugins/android/PluginViewAndroid.cpp b/WebCore/plugins/android/PluginViewAndroid.cpp
index 3fea59d..08ac7c8 100644
--- a/WebCore/plugins/android/PluginViewAndroid.cpp
+++ b/WebCore/plugins/android/PluginViewAndroid.cpp
@@ -479,12 +479,6 @@
     NPError error = NPERR_GENERIC_ERROR;
 
     switch (variable) {
-        case kSetPluginStubJavaClassName_ANPSetValue: {
-            char* className = reinterpret_cast<char*>(value);
-            if (m_window->setPluginStubJavaClassName(className))
-                error = NPERR_NO_ERROR;
-            break;
-        }
         case kRequestDrawingModel_ANPSetValue: {
             ANPDrawingModel model = reinterpret_cast<ANPDrawingModel>(value);
             if (m_window->setDrawingModel(model))
diff --git a/WebKit/android/jni/WebViewCore.cpp b/WebKit/android/jni/WebViewCore.cpp
index 7874598..2684c52 100644
--- a/WebKit/android/jni/WebViewCore.cpp
+++ b/WebKit/android/jni/WebViewCore.cpp
@@ -190,6 +190,7 @@
     jmethodID   m_geolocationPermissionsHidePrompt;
     jmethodID   m_addMessageToConsole;
     jmethodID   m_getPluginClass;
+    jmethodID   m_createPluginJavaInstance;
     jmethodID   m_startFullScreenPluginActivity;
     jmethodID   m_createSurface;
     jmethodID   m_updateSurface;
@@ -269,8 +270,9 @@
     m_javaGlue->m_geolocationPermissionsHidePrompt = GetJMethod(env, clazz, "geolocationPermissionsHidePrompt", "()V");
     m_javaGlue->m_addMessageToConsole = GetJMethod(env, clazz, "addMessageToConsole", "(Ljava/lang/String;ILjava/lang/String;)V");
     m_javaGlue->m_getPluginClass = GetJMethod(env, clazz, "getPluginClass", "(Ljava/lang/String;Ljava/lang/String;)Ljava/lang/Class;");
-    m_javaGlue->m_startFullScreenPluginActivity = GetJMethod(env, clazz, "startFullScreenPluginActivity", "(Ljava/lang/String;Ljava/lang/String;I)V");
-    m_javaGlue->m_createSurface = GetJMethod(env, clazz, "createSurface", "(Ljava/lang/String;Ljava/lang/String;IIIII)Landroid/webkit/ViewManager$ChildView;");
+    m_javaGlue->m_createPluginJavaInstance = GetJMethod(env, clazz, "createPluginJavaInstance", "(Ljava/lang/String;I)Landroid/webkit/plugin/WebkitPlugin;");
+    m_javaGlue->m_startFullScreenPluginActivity = GetJMethod(env, clazz, "startFullScreenPluginActivity", "(Ljava/lang/String;I)V");
+    m_javaGlue->m_createSurface = GetJMethod(env, clazz, "createSurface", "(Landroid/webkit/plugin/WebkitPlugin;IIII)Landroid/webkit/ViewManager$ChildView;");
     m_javaGlue->m_updateSurface = GetJMethod(env, clazz, "updateSurface", "(Landroid/webkit/ViewManager$ChildView;IIII)V");
     m_javaGlue->m_destroySurface = GetJMethod(env, clazz, "destroySurface", "(Landroid/webkit/ViewManager$ChildView;)V");
     m_javaGlue->m_sendFindAgain = GetJMethod(env, clazz, "sendFindAgain", "()V");
@@ -2438,7 +2440,7 @@
     view->setBaseBackgroundColor(bcolor);
 }
 
-jclass WebViewCore::getPluginClass(const char* libName, const char* className)
+jclass WebViewCore::getPluginClass(const WebCore::String& libName, const char* className)
 {
     JNIEnv* env = JSC::Bindings::getJNIEnv();
     AutoJObject obj = m_javaGlue->object(env);
@@ -2447,7 +2449,7 @@
     if (!obj.get())
         return NULL;
 
-    jstring libString = env->NewStringUTF(libName);
+    jstring libString = env->NewString(libName.characters(), libName.length());
     jstring classString = env->NewStringUTF(className);
     jobject pluginClass = env->CallObjectMethod(obj.get(),
                                            m_javaGlue->m_getPluginClass,
@@ -2461,8 +2463,24 @@
     }
 }
 
-void WebViewCore::startFullScreenPluginActivity(const char* libName,
-                                                const char* className, NPP npp)
+jobject WebViewCore::createPluginJavaInstance(const WebCore::String& libName, NPP npp)
+{
+    JNIEnv* env = JSC::Bindings::getJNIEnv();
+    AutoJObject obj = m_javaGlue->object(env);
+    // if it is called during DESTROY is handled, the real object of WebViewCore
+    // can be gone. Check before using it.
+    if (!obj.get())
+        return 0;
+
+    jstring libString = env->NewString(libName.characters(), libName.length());
+    jobject result = env->CallObjectMethod(obj.get(),
+                                           m_javaGlue->m_createPluginJavaInstance,
+                                           libString, (int) npp);
+    checkException(env);
+    return result;
+}
+
+void WebViewCore::startFullScreenPluginActivity(const char* libName, NPP npp)
 {
     JNIEnv* env = JSC::Bindings::getJNIEnv();
     AutoJObject obj = m_javaGlue->object(env);
@@ -2472,15 +2490,13 @@
         return;
 
     jstring libString = env->NewStringUTF(libName);
-    jstring classString = env->NewStringUTF(className);
     env->CallVoidMethod(obj.get(),
                         m_javaGlue->m_startFullScreenPluginActivity,
-                        libString, classString, (int) npp);
+                        libString, (int) npp);
     checkException(env);
 }
 
-jobject WebViewCore::createSurface(const char* libName, const char* className,
-                                   NPP npp, int x, int y, int width, int height)
+jobject WebViewCore::createSurface(jobject webkitPlugin, int x, int y, int width, int height)
 {
     JNIEnv* env = JSC::Bindings::getJNIEnv();
     AutoJObject obj = m_javaGlue->object(env);
@@ -2489,11 +2505,9 @@
     if (!obj.get())
         return 0;
 
-    jstring libString = env->NewStringUTF(libName);
-    jstring classString = env->NewStringUTF(className);
     jobject result = env->CallObjectMethod(obj.get(),
-                                           m_javaGlue->m_createSurface, libString,
-                                           classString,(int) npp, x, y, width, height);
+                                           m_javaGlue->m_createSurface,
+                                           webkitPlugin, x, y, width, height);
     checkException(env);
     return result;
 
diff --git a/WebKit/android/jni/WebViewCore.h b/WebKit/android/jni/WebViewCore.h
index 1ff5678..19bca96 100644
--- a/WebKit/android/jni/WebViewCore.h
+++ b/WebKit/android/jni/WebViewCore.h
@@ -371,15 +371,16 @@
         void requestKeyboard(bool);
 
         // Generates a class loader that contains classes from the plugin's apk
-        jclass getPluginClass(const char* libName, const char* className);
+        jclass getPluginClass(const WebCore::String& libName, const char* className);
+
+        // Creates a new instance of the plugin's java component
+        jobject createPluginJavaInstance(const WebCore::String& libName, NPP npp);
 
         // Creates a full screen surface (i.e. View on an Activity) for a plugin
-        void startFullScreenPluginActivity(const char* libName,
-                                           const char* className, NPP npp);
+        void startFullScreenPluginActivity(const char* libName, NPP npp);
 
         // Creates a Surface (i.e. View) for a plugin
-        jobject createSurface(const char* libName, const char* className,
-                              NPP npp, int x, int y, int width, int height);
+        jobject createSurface(jobject webkitPlugin, int x, int y, int width, int height);
 
         // Updates a Surface coordinates and dimensions for a plugin
         void updateSurface(jobject childView, int x, int y, int width, int height);
diff --git a/WebKit/android/plugins/ANPSystemInterface.cpp b/WebKit/android/plugins/ANPSystemInterface.cpp
index 92085cc..42ec9e4 100644
--- a/WebKit/android/plugins/ANPSystemInterface.cpp
+++ b/WebKit/android/plugins/ANPSystemInterface.cpp
@@ -77,12 +77,9 @@
     WebCore::PluginView* pluginView = pluginViewForInstance(instance);
     PluginWidgetAndroid* pluginWidget = pluginView->platformPluginWidget();
 
-    const WebCore::String& libName = pluginView->plugin()->path();
-    SkString skLibName;
-    skLibName.setUTF16(libName.characters(), libName.length());
-
     jclass result;
-    result = pluginWidget->webViewCore()->getPluginClass(skLibName.c_str(), className);
+    result = pluginWidget->webViewCore()->getPluginClass(pluginView->plugin()->path(),
+                                                         className);
     return result;
 }
 
diff --git a/WebKit/android/plugins/PluginWidgetAndroid.cpp b/WebKit/android/plugins/PluginWidgetAndroid.cpp
index dcd8943..9c8d25d 100644
--- a/WebKit/android/plugins/PluginWidgetAndroid.cpp
+++ b/WebKit/android/plugins/PluginWidgetAndroid.cpp
@@ -36,6 +36,7 @@
 #include "SkFlipPixelRef.h"
 #include "SkString.h"
 #include "WebViewCore.h"
+#include "jni_utility.h"
 
 #define DEBUG_VISIBLE_RECTS 1 // temporary debug printfs and fixes
 
@@ -52,8 +53,8 @@
     m_pluginBounds.setEmpty();
     m_hasFocus = false;
     m_zoomLevel = 0;
-    m_javaClassName = NULL;
     m_childView = NULL;
+    m_webkitPlugin = NULL;
 }
 
 PluginWidgetAndroid::~PluginWidgetAndroid() {
@@ -63,9 +64,16 @@
             m_core->destroySurface(m_childView);
         }
     }
-    if (m_javaClassName) {
-        free(m_javaClassName);
+
+    // cleanup any remaining JNI References
+    JNIEnv* env = JSC::Bindings::getJNIEnv();
+    if (m_childView) {
+        env->DeleteGlobalRef(m_childView);
     }
+    if (m_webkitPlugin) {
+        env->DeleteGlobalRef(m_webkitPlugin);
+    }
+
     m_flipPixelRef->safeUnref();
 }
 
@@ -74,6 +82,19 @@
     m_core->addPlugin(this);
 }
 
+jobject PluginWidgetAndroid::getJavaPluginInstance() {
+    if (m_webkitPlugin == NULL && m_core != NULL) {
+
+        jobject tempObj = m_core->createPluginJavaInstance(m_pluginView->plugin()->path(),
+                                                           m_pluginView->instance());
+        if (tempObj) {
+            JNIEnv* env = JSC::Bindings::getJNIEnv();
+            m_webkitPlugin = env->NewGlobalRef(tempObj);
+        }
+    }
+    return m_webkitPlugin;
+}
+
 static SkBitmap::Config computeConfig(bool isTransparent) {
     return isTransparent ? SkBitmap::kARGB_8888_Config
                          : SkBitmap::kRGB_565_Config;
@@ -105,15 +126,13 @@
 
         // if the surface does not exist then create a new surface
         } else if(!m_childView) {
-
-            const String& libName = m_pluginView->plugin()->path();
-            SkString skLibName;
-            skLibName.setUTF16(libName.characters(), libName.length());
-
-            m_childView = m_core->createSurface(skLibName.c_str(), m_javaClassName,
-                                                m_pluginView->instance(),
-                                                docPoint.x(), docPoint.y(),
-                                                window->width, window->height);
+            jobject tempObj = m_core->createSurface(getJavaPluginInstance(),
+                                                    docPoint.x(), docPoint.y(),
+                                                    window->width, window->height);
+            if (tempObj) {
+                JNIEnv* env = JSC::Bindings::getJNIEnv();
+                m_childView = env->NewGlobalRef(tempObj);
+            }
         }
     } else {
         m_flipPixelRef->safeUnref();
@@ -122,44 +141,17 @@
     }
 }
 
-bool PluginWidgetAndroid::setPluginStubJavaClassName(const char* className) {
-
-    if (m_javaClassName) {
-        free(m_javaClassName);
-    }
-
-    // don't call strdup() if the className is to be set to NULL
-    if (!className) {
-        m_javaClassName = NULL;
-        return true;
-    }
-
-    // make a local copy of the className
-    m_javaClassName = strdup(className);
-    return (m_javaClassName != NULL);
-}
-
 void PluginWidgetAndroid::requestFullScreenMode() {
 
-    if (!m_javaClassName) {
-        return;
-    }
-
     const String& libName = m_pluginView->plugin()->path();
     SkString skLibName;
     skLibName.setUTF16(libName.characters(), libName.length());
 
-    m_core->startFullScreenPluginActivity(skLibName.c_str(), m_javaClassName,
+    m_core->startFullScreenPluginActivity(skLibName.c_str(),
                                           m_pluginView->instance());
 }
 
 bool PluginWidgetAndroid::setDrawingModel(ANPDrawingModel model) {
-
-    // disallow the surface drawing model if no java class name has been given
-    if (model == kSurface_ANPDrawingModel && m_javaClassName == NULL) {
-        return false;
-    }
-
     m_drawingModel = model;
     return true;
 }
diff --git a/WebKit/android/plugins/PluginWidgetAndroid.h b/WebKit/android/plugins/PluginWidgetAndroid.h
index b7df4f0..da2291f 100644
--- a/WebKit/android/plugins/PluginWidgetAndroid.h
+++ b/WebKit/android/plugins/PluginWidgetAndroid.h
@@ -67,12 +67,6 @@
      */
     void setWindow(NPWindow* window, bool isTransparent);
 
-    /* Called to notify us of the plugin's java class that implements the
-     * PluginStub interface. A local copy is made of the className so the caller
-     * can safely free the memory as soon as the function returns.
-     */
-    bool setPluginStubJavaClassName(const char* className);
-
     /*  Called whenever the plugin itself requests a new drawing model. If the
         hardware does not support the requested model then false is returned,
         otherwise true is returned.
@@ -141,6 +135,7 @@
     WebCore::IntPoint frameToDocumentCoords(int frameX, int frameY) const;
     void computeVisibleFrameRect();
     void scrollToVisibleFrameRect();
+    jobject getJavaPluginInstance();
 
     WebCore::PluginView*    m_pluginView;
     android::WebViewCore*   m_core;
@@ -153,8 +148,8 @@
     SkIRect                 m_requestedFrameRect;
     bool                    m_hasFocus;
     float                   m_zoomLevel;
-    char*                   m_javaClassName;
     jobject                 m_childView;
+    jobject                 m_webkitPlugin;
 
     /* We limit the number of rectangles to minimize storage and ensure adequate
        speed.
diff --git a/WebKit/android/plugins/android_npapi.h b/WebKit/android/plugins/android_npapi.h
index 71913fa..88c45f4 100644
--- a/WebKit/android/plugins/android_npapi.h
+++ b/WebKit/android/plugins/android_npapi.h
@@ -149,10 +149,10 @@
     kBitmap_ANPDrawingModel  = 0,
     /** Draw into a surface (e.g. raster, openGL, etc.) using the Java surface
         interface. When this model is used the browser will invoke the Java
-        class specified by kSetPluginStubJavaClassName_ANPSetValue which will
-        return an instance of a android Java View. The instance is then embedded
-        in the html. The plugin can then manipulate the view as it would any
-        normal Java View in android.
+        class specified in the plugin's apk manifest. From that class the browser
+        will invoke the appropriate method to return an an instance of a android
+        Java View. The instance is then embedded in the html. The plugin can then
+        manipulate the view as it would any normal Java View in android.
 
         Unlike the bitmap model, a surface model is opaque so no html content
         behind the plugin will be  visible. Unless the plugin needs to be
@@ -171,27 +171,13 @@
 };
 typedef int32_t ANPDrawingModel;
 
-/** Set the name of the Java class found in the plugin's apk that implements the
-    PluginStub interface.  The value provided must be a null terminated char*
-    that contains the fully qualified class name (e.g., your.package.className).
-    A local copy is made of the char* so the caller can safely free the memory
-    as soon as the function returns.
-
-    This value must be set prior to selecting the Surface_ANPDrawingModel or
-    requesting to enter full-screen mode.
-
-    NPN_SetValue(inst, kSetPluginStubJavaClassName_ANPSetValue,
-                (void*)nullTerminatedChar*)
- */
-#define kSetPluginStubJavaClassName_ANPSetValue ((NPPVariable)1001)
-
 /** Request to receive/disable events. If the pointer is NULL then all flags will
     be disabled. Otherwise, the event type will be enabled iff its corresponding
     bit in the EventFlags bit field is set.
 
     NPN_SetValue(inst, ANPAcceptEvents, (void*)EventFlags)
  */
-#define kAcceptEvents_ANPSetValue           ((NPPVariable)1002)
+#define kAcceptEvents_ANPSetValue           ((NPPVariable)1001)
 
 /** The EventFlags are a set of bits used to determine which types of events the
     plugin wishes to receive. For example, if the value is 0x03 then both key
@@ -674,8 +660,8 @@
      */
     void    (*showKeyboard)(NPP instance, bool value);
     /** Called when a plugin wishes to enter into full screen mode. The plugin's
-        Java class (set using kSetPluginStubJavaClassName_ANPSetValue) will be
-        called asynchronously to provide a View object to be displayed full screen.
+        Java class (defined in the plugin's apk manifest) will be called
+        asynchronously to provide a View object to be displayed full screen.
      */
     void    (*requestFullScreen)(NPP instance);
 };