Allow plugins to load java classes from their apk.

Relocate ANPSystemInterface into its own file and provide the
ability to call into java and load a plugin's java class from
their apk.

see http://b/2215696
diff --git a/WebCore/plugins/android/PluginViewAndroid.cpp b/WebCore/plugins/android/PluginViewAndroid.cpp
index 529458b..69f76b8 100644
--- a/WebCore/plugins/android/PluginViewAndroid.cpp
+++ b/WebCore/plugins/android/PluginViewAndroid.cpp
@@ -75,6 +75,7 @@
 
 #include "android_npapi.h"
 #include "ANPSurface_npapi.h"
+#include "ANPSystem_npapi.h"
 #include "SkANP.h"
 #include "SkFlipPixelRef.h"
 
@@ -165,8 +166,6 @@
 
 bool PluginView::platformStart()
 {
-    android::WebViewCore* c = android::WebViewCore::getWebViewCore(this->parent());
-    m_window->init(c);
     return true;
 }
 
@@ -362,8 +361,15 @@
 {
     Widget::setParent(parent);
 
-    if (parent)
+    if (parent) {
+        // the widget needs initialized now so that the plugin has access to
+        // WebViewCore when NPP_New is called
+        if (m_window && !m_window->webViewCore()) {
+            android::WebViewCore* c = android::WebViewCore::getWebViewCore(this->parent());
+            m_window->init(c);
+        }
         init();
+    }
 }
 
 void PluginView::setNPWindowRect(const IntRect& rect)
diff --git a/WebKit/android/jni/WebViewCore.cpp b/WebKit/android/jni/WebViewCore.cpp
index 8f5427a..07f5d62 100644
--- a/WebKit/android/jni/WebViewCore.cpp
+++ b/WebKit/android/jni/WebViewCore.cpp
@@ -189,6 +189,7 @@
     jmethodID   m_geolocationPermissionsShowPrompt;
     jmethodID   m_geolocationPermissionsHidePrompt;
     jmethodID   m_addMessageToConsole;
+    jmethodID   m_getPluginClass;
     jmethodID   m_startFullScreenPluginActivity;
     jmethodID   m_createSurface;
     jmethodID   m_updateSurface;
@@ -267,6 +268,7 @@
     m_javaGlue->m_geolocationPermissionsShowPrompt = GetJMethod(env, clazz, "geolocationPermissionsShowPrompt", "(Ljava/lang/String;)V");
     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_updateSurface = GetJMethod(env, clazz, "updateSurface", "(Landroid/webkit/ViewManager$ChildView;IIII)V");
@@ -2425,18 +2427,40 @@
     view->setBaseBackgroundColor(bcolor);
 }
 
+jclass WebViewCore::getPluginClass(const char* libName, const char* className)
+{
+    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 NULL;
+
+    jstring libString = env->NewStringUTF(libName);
+    jstring classString = env->NewStringUTF(className);
+    jobject pluginClass = env->CallObjectMethod(obj.get(),
+                                           m_javaGlue->m_getPluginClass,
+                                           libString, classString);
+    checkException(env);
+
+    if (pluginClass != NULL) {
+        return static_cast<jclass>(pluginClass);
+    } else {
+        return NULL;
+    }
+}
+
 void WebViewCore::startFullScreenPluginActivity(const char* libName,
                                                 const char* className, NPP npp)
 {
     JNIEnv* env = JSC::Bindings::getJNIEnv();
-
-    jstring libString = env->NewStringUTF(libName);
     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;
 
+    jstring libString = env->NewStringUTF(libName);
     jstring classString = env->NewStringUTF(className);
     env->CallVoidMethod(obj.get(),
                         m_javaGlue->m_startFullScreenPluginActivity,
diff --git a/WebKit/android/jni/WebViewCore.h b/WebKit/android/jni/WebViewCore.h
index a7855b8..308ddfc 100644
--- a/WebKit/android/jni/WebViewCore.h
+++ b/WebKit/android/jni/WebViewCore.h
@@ -367,6 +367,9 @@
         // Notify the Java side that webkit is requesting a keyboard
         void requestKeyboard(bool);
 
+        // Generates a class loader that contains classes from the plugin's apk
+        jclass getPluginClass(const char* libName, const char* className);
+
         // Creates a full screen surface (i.e. View on an Activity) for a plugin
         void startFullScreenPluginActivity(const char* libName,
                                            const char* className, NPP npp);
diff --git a/WebKit/android/plugins/ANPSystemInterface.cpp b/WebKit/android/plugins/ANPSystemInterface.cpp
index a5b9bcb..a7bf1cb 100644
--- a/WebKit/android/plugins/ANPSystemInterface.cpp
+++ b/WebKit/android/plugins/ANPSystemInterface.cpp
@@ -26,10 +26,16 @@
 // must include config.h first for webkit to fiddle with new/delete
 #include "config.h"
 
-#include "android_npapi.h"
 #include "CString.h"
 #include "JavaSharedClient.h"
 #include "PluginClient.h"
+#include "PluginPackage.h"
+#include "PluginView.h"
+#include "PluginWidgetAndroid.h"
+#include "SkString.h"
+#include "WebViewCore.h"
+
+#include "ANPSystem_npapi.h"
 
 static const char* gApplicationDataDir = NULL;
 
@@ -61,6 +67,25 @@
     return gApplicationDataDir;
 }
 
+static WebCore::PluginView* pluginViewForInstance(NPP instance) {
+    if (instance && instance->ndata)
+        return static_cast<WebCore::PluginView*>(instance->ndata);
+    return PluginView::currentPluginView();
+}
+
+static jclass anp_loadJavaClass(NPP instance, const char* className) {
+    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);
+    return result;
+}
+
 ///////////////////////////////////////////////////////////////////////////////
 
 #define ASSIGN(obj, name)   (obj)->name = anp_##name
@@ -69,4 +94,5 @@
     ANPSystemInterfaceV0* i = reinterpret_cast<ANPSystemInterfaceV0*>(v);
 
     ASSIGN(i, getApplicationDataDirectory);
+    ASSIGN(i, loadJavaClass);
 }
diff --git a/WebKit/android/plugins/ANPSystem_npapi.h b/WebKit/android/plugins/ANPSystem_npapi.h
new file mode 100644
index 0000000..1507ef6
--- /dev/null
+++ b/WebKit/android/plugins/ANPSystem_npapi.h
@@ -0,0 +1,25 @@
+#ifndef ANPSystem_npapi_H
+#define ANPSystem_npapi_H
+
+#include "android_npapi.h"
+#include <jni.h>
+
+struct ANPSystemInterfaceV0 : ANPInterface {
+    /** Return the path name for the current Application's plugin data directory,
+        or NULL if not supported
+     */
+    const char* (*getApplicationDataDirectory)();
+
+    /** A helper function to load java classes from the plugin's apk.  The
+        function looks for a class given the fully qualified and null terminated
+        string representing the className. For example,
+
+        const char* className = "com.android.mypackage.MyClass";
+
+        If the class cannot be found or there is a problem loading the class
+        NULL will be returned.
+     */
+    jclass (*loadJavaClass)(NPP instance, const char* className);
+};
+
+#endif //ANPSystem_npapi_H
diff --git a/WebKit/android/plugins/ANPWindowInterface.cpp b/WebKit/android/plugins/ANPWindowInterface.cpp
index 7a2f94c..3f4dff3 100644
--- a/WebKit/android/plugins/ANPWindowInterface.cpp
+++ b/WebKit/android/plugins/ANPWindowInterface.cpp
@@ -68,9 +68,9 @@
 }
 
 static void anp_showKeyboard(NPP instance, bool value) {
-    ScrollView* scrollView = pluginViewForInstance(instance)->parent();
-    android::WebViewCore* core = android::WebViewCore::getWebViewCore(scrollView);
-    core->requestKeyboard(value);
+    PluginView* pluginView = pluginViewForInstance(instance);
+    PluginWidgetAndroid* pluginWidget = pluginView->platformPluginWidget();
+    pluginWidget->webViewCore()->requestKeyboard(value);
 }
 
 static void anp_requestFullScreen(NPP instance) {
diff --git a/WebKit/android/plugins/android_npapi.h b/WebKit/android/plugins/android_npapi.h
index 04f4ef6..9ab814f 100644
--- a/WebKit/android/plugins/android_npapi.h
+++ b/WebKit/android/plugins/android_npapi.h
@@ -764,13 +764,6 @@
     bool (*isStopped)(ANPAudioTrack*);
 };
 
-struct ANPSystemInterfaceV0 : ANPInterface {
-    /** Return the path name for the current Application's plugin data directory,
-     *  or NULL if not supported
-     */
-    const char* (*getApplicationDataDirectory)();
-};
-
 ///////////////////////////////////////////////////////////////////////////////
 // DEFINITION OF VALUES PASSED THROUGH NPP_HandleEvent