1 /*
   2  * Copyright (c) 2011, 2014, 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 package com.sun.webkit.plugin;
  27 
  28 import com.sun.javafx.logging.PlatformLogger;
  29 import java.net.URL;
  30 import java.util.Collection;
  31 import java.util.HashSet;
  32 import java.util.Iterator;
  33 import java.util.List;
  34 import java.util.ServiceLoader;
  35 import java.util.TreeMap;
  36 import java.util.Vector;
  37 
  38 public final class PluginManager {
  39     private final static PlatformLogger log =
  40             PlatformLogger.getLogger("com.sun.browser.plugin.PluginManager");
  41 
  42     private static final ServiceLoader<PluginHandler> pHandlers =
  43         ServiceLoader.load(PluginHandler.class);
  44 
  45     private static final TreeMap<String,PluginHandler> hndMap =
  46         new TreeMap<String,PluginHandler>();
  47 
  48     private static PluginHandler[] hndArray;
  49 
  50     private static final HashSet<String> disabledPluginHandlers =
  51         new HashSet<String>();
  52 
  53 
  54     private static void updatePluginHandlers() {
  55         log.fine("Update plugin handlers");
  56 
  57         hndMap.clear();
  58 
  59         Iterator<PluginHandler> iter = pHandlers.iterator();
  60         while(iter.hasNext()) {
  61             PluginHandler hnd = iter.next();
  62             if (hnd.isSupportedPlatform() && !isDisabledPlugin(hnd))
  63             {
  64                 String [] types = hnd.supportedMIMETypes();
  65                 for (String type : types) {
  66                     hndMap.put(type, hnd);
  67                     log.fine(type);
  68                 }
  69             }
  70         }
  71         Collection<PluginHandler> vals = hndMap.values();
  72         hndArray = vals.toArray(new PluginHandler[vals.size()]);
  73     }
  74 
  75     static {
  76         if ("false".equalsIgnoreCase(
  77                 System.getProperty("com.sun.browser.plugin")))
  78         {
  79             for(PluginHandler hnd : getAvailablePlugins()) {
  80                 disabledPluginHandlers.add(hnd.getClass().getCanonicalName());
  81             }
  82         }
  83 
  84         updatePluginHandlers();
  85     }
  86 
  87     public static Plugin createPlugin(URL url, String type, String[] pNames,
  88                                         String[] pValues)
  89     {
  90         try {
  91             PluginHandler hnd =  hndMap.get(type);
  92             if (hnd == null) {
  93                 return new DefaultPlugin(url, type, pNames, pValues);
  94             } else {
  95                 Plugin p = hnd.createPlugin(url, type, pNames, pValues);
  96                 if (p == null) {
  97                     return new DefaultPlugin(url, type, pNames, pValues);
  98                 } else {
  99                     return p;
 100                 }
 101             }
 102         } catch (Throwable ex) {
 103             log.fine("Cannot create plugin" , ex);
 104             return new DefaultPlugin(url, type, pNames, pValues);
 105         }
 106     }
 107 
 108 
 109     private static List<PluginHandler> getAvailablePlugins() {
 110         Vector<PluginHandler> res = new Vector<PluginHandler>();
 111         Iterator<PluginHandler> iter = pHandlers.iterator();
 112         while(iter.hasNext()) {
 113             PluginHandler hnd = iter.next();
 114             if (hnd.isSupportedPlatform()) {
 115                 res.add(hnd);
 116             }
 117         }
 118         return res;
 119     }
 120 
 121     private static PluginHandler getEnabledPlugin(int i) {
 122         if (i < 0 || i >= hndArray.length) return null;
 123         return hndArray[i];
 124     }
 125 
 126     private static int getEnabledPluginCount() {
 127         return hndArray.length;
 128     }
 129 
 130     private static void disablePlugin(PluginHandler hnd) {
 131         disabledPluginHandlers.add(hnd.getClass().getCanonicalName());
 132         updatePluginHandlers();
 133     }
 134 
 135     private static void enablePlugin(PluginHandler hnd) {
 136         disabledPluginHandlers.remove(hnd.getClass().getCanonicalName());
 137         updatePluginHandlers();
 138     }
 139 
 140     private static boolean isDisabledPlugin(PluginHandler hnd) {
 141         return disabledPluginHandlers.contains(
 142             hnd.getClass().getCanonicalName());
 143     }
 144 
 145     private static boolean supportsMIMEType(String mimeType) {
 146         return hndMap.containsKey(mimeType);
 147     }
 148 
 149     private static String getPluginNameForMIMEType(String mimeType) {
 150         PluginHandler hnd = hndMap.get(mimeType);
 151         if (hnd != null) return hnd.getName();
 152         return "";
 153     }
 154 }