1 /*
   2  * Copyright (c) 2011, 2017, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 #include "config.h"
  27 #include <wtf/java/JavaEnv.h>
  28 #include "PluginData.h"
  29 
  30 namespace WebCore {
  31 
  32 
  33 void init_plugins(bool, Vector<PluginInfo>*) {
  34 /*
  35     JNIEnv* env = WebCore_GetJavaEnv();
  36 
  37     jclass clsPluginManager
  38         = env->FindClass("com/sun/webkit/plugin/PluginManager");
  39     ASSERT(clsPluginManager);
  40     if (!clsPluginManager) {    // for safety
  41         CheckAndClearException(env);
  42         return;
  43     }
  44 
  45 
  46     static jmethodID midGetCount = 0;
  47     if (!midGetCount) {
  48         midGetCount = env->GetStaticMethodID(clsPluginManager,
  49             "getEnabledPluginCount", "()I");
  50 
  51         ASSERT(midGetCount);
  52         if (!midGetCount) { // for safety
  53             CheckAndClearException(env);
  54             return;
  55         }
  56     }
  57 
  58     jint count = 0;
  59     count = env->CallStaticIntMethod(clsPluginManager, midGetCount);
  60 
  61     if (count > 0) {
  62         static jmethodID midGetPlugin = 0;
  63         if (!midGetPlugin) {
  64             midGetPlugin = env->GetStaticMethodID(clsPluginManager,
  65                 "getEnabledPlugin", "(I)Lcom/sun/webkit/plugin/PluginHandler;");
  66 
  67             ASSERT(midGetPlugin);
  68             if (!midGetPlugin) {    // for safety
  69                 CheckAndClearException(env);
  70                 return;
  71             }
  72         }
  73 
  74         jclass clsPlugin = env->FindClass("com/sun/webkit/plugin/PluginHandler");
  75         ASSERT(clsPlugin);
  76         if (!clsPlugin) {   // for safety
  77             CheckAndClearException(env);
  78             return;
  79         }
  80 
  81         static jmethodID midGetName = 0;
  82         if (!midGetName) {
  83             midGetName = env->GetMethodID(clsPlugin,
  84                 "getName", "()Ljava/lang/String;");
  85 
  86             ASSERT(midGetName);
  87             if (!midGetName) {
  88                 CheckAndClearException(env);
  89                 //return; don't return!
  90             }
  91         }
  92 
  93         static jmethodID midGetFileName = 0;
  94         if (!midGetFileName) {
  95             midGetFileName = env->GetMethodID(clsPlugin,
  96                 "getFileName", "()Ljava/lang/String;");
  97 
  98             ASSERT(midGetFileName);
  99             if (!midGetFileName) {
 100                 CheckAndClearException(env);
 101                 //return; don't return!
 102             }
 103         }
 104 
 105         static jmethodID midGetDescr = 0;
 106         if (!midGetDescr) {
 107             midGetDescr = env->GetMethodID(clsPlugin,
 108                 "getDescription", "()Ljava/lang/String;");
 109 
 110             ASSERT(midGetDescr);
 111             if (!midGetDescr) {
 112                 CheckAndClearException(env);
 113                 //return; don't return!
 114             }
 115         }
 116 
 117         static jmethodID midGetMimeTypes = 0;
 118         if (!midGetMimeTypes) {
 119             midGetMimeTypes = env->GetMethodID(clsPlugin,
 120                 "supportedMIMETypes", "()[Ljava/lang/String;");
 121 
 122             ASSERT(midGetMimeTypes);
 123             if (!midGetMimeTypes) {
 124                 CheckAndClearException(env);
 125                 //return; don't return!
 126             }
 127         }
 128 
 129         // am: TODO: add getSupportedExtensions
 130 
 131         for (jint i=0; i<count; i++) {
 132             jobject plugin = NULL;
 133             plugin = env->CallStaticObjectMethod(clsPluginManager, midGetPlugin, i);
 134             if (plugin) {
 135                 PluginInfo *info = new PluginInfo();
 136 
 137                 jstring jstrName = NULL;
 138                 jstrName = (jstring)env->CallObjectMethod(plugin, midGetName);
 139                 if (jstrName) {
 140                     info->name = String(env, jstrName);
 141                     env->DeleteLocalRef(jstrName);
 142                 } else {
 143                     info->name = "unknown";
 144                 }
 145 
 146                 jstring jstrFileName = NULL;
 147                 jstrFileName = (jstring)env->CallObjectMethod(plugin, midGetFileName);
 148                 if (jstrFileName) {
 149                     info->file = String(env, jstrFileName);
 150                     env->DeleteLocalRef(jstrFileName);
 151                 } else {
 152                     info->file = "unknown";
 153                 }
 154 
 155                 jstring jstrDescr = NULL;
 156                 jstrDescr = (jstring)env->CallObjectMethod(plugin, midGetDescr);
 157                 if (jstrDescr) {
 158                     info->desc = String(env, jstrDescr);
 159                     env->DeleteLocalRef(jstrDescr);
 160                 } else {
 161                     //info->desc = "n/a";
 162                 }
 163 
 164                 jobjectArray jMimes = NULL;
 165                 jMimes = (jobjectArray)env->CallObjectMethod(plugin, midGetMimeTypes);
 166                 if (jMimes) {
 167                     jint n = env->GetArrayLength(jMimes);
 168                     for (jint j=0; j<n; j++) {
 169                         jstring jstrMime = (jstring)env->GetObjectArrayElement(jMimes, j);
 170                         if (jstrMime) {
 171                             MimeClassInfo *mime = new MimeClassInfo;
 172                             mime->type = String(env, jstrMime);
 173                             mime->desc = "--mime type description--";
 174                             //mime->suffixes = ;
 175                             //mime->plugin = info;
 176 
 177                             info->mimes.append(mime);
 178 
 179                             env->DeleteLocalRef(jstrMime);
 180                         }
 181                     }
 182                 }
 183 
 184                 m_plugins.append(info);
 185             }
 186         }
 187     }
 188     CheckAndClearException(env);
 189 */
 190 }
 191 
 192 
 193 class PluginCache {
 194 public:
 195     PluginCache() : m_loaded(false), m_refresh(false) {}
 196     ~PluginCache() { reset(false); }
 197 
 198     void reset(bool refresh)
 199     {
 200         m_plugins.clear();
 201         m_loaded = false;
 202         m_refresh = refresh;
 203     }
 204 
 205     const Vector<PluginInfo>& plugins()
 206     {
 207         if (!m_loaded) {
 208             init_plugins(m_refresh, &m_plugins);
 209             m_loaded = true;
 210             m_refresh = false;
 211         }
 212         return m_plugins;
 213     }
 214 
 215 private:
 216     Vector<PluginInfo> m_plugins;
 217     bool m_loaded;
 218     bool m_refresh;
 219 };
 220 
 221 /*
 222 static PluginCache& pluginCache()
 223 {
 224     LazyNeverDestroyed<PluginCache> cache;
 225     return cache.get();
 226 }
 227 
 228 void PluginData::initPlugins(const Page*)
 229 {
 230     const Vector<PluginInfo>& plugins = pluginCache().plugins();
 231     for (size_t i = 0; i < plugins.size(); ++i)
 232         m_plugins.append(plugins[i]);
 233 }
 234 
 235 void PluginData::refresh()
 236 {
 237     pluginCache().reset(true);
 238     pluginCache().plugins(); // Force the plugins to be reloaded now.
 239 }
 240 */
 241 
 242 }