Advertisement
Guest User

limemozillainitializer.java

a guest
Nov 30th, 2019
287
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.27 KB | None | 0 0
  1. package org.limewire.ui.swing.browser;
  2.  
  3. import java.io.BufferedInputStream;
  4. import java.io.File;
  5. import java.io.IOException;
  6. import java.io.InputStream;
  7.  
  8. import org.apache.commons.logging.Log;
  9. import org.apache.commons.logging.LogFactory;
  10. import org.limewire.core.settings.MozillaSettings;
  11. import org.limewire.io.Expand;
  12. import org.limewire.io.IOUtils;
  13. import org.limewire.util.CommonUtils;
  14. import org.limewire.util.FileUtils;
  15. import org.limewire.util.OSUtils;
  16. import org.mozilla.browser.IMozillaWindow;
  17. import org.mozilla.browser.IMozillaWindowFactory;
  18. import org.mozilla.browser.MozillaConfig;
  19. import org.mozilla.browser.MozillaInitialization;
  20. import org.mozilla.browser.MozillaWindow;
  21. import org.mozilla.browser.impl.WindowCreator;
  22.  
  23. public class LimeMozillaInitializer {
  24.  
  25.     private static final Log LOG = LogFactory.getLog(LimeMozillaInitializer.class);
  26.  
  27.     private LimeMozillaInitializer() {
  28.     }
  29.  
  30.     public static boolean shouldInitialize() {
  31.         return (OSUtils.isWindows() || OSUtils.isLinux() || OSUtils.isMacOSX())
  32.                 && is32BitProcess() && MozillaSettings.USE_MOZILLA.getValue();
  33.     }
  34.  
  35.     private static boolean is32BitProcess() {
  36.         return "i386".equals(OSUtils.getOSArch()) || "x86".equals(OSUtils.getOSArch());
  37.     }
  38.  
  39.     public static void initialize() {
  40.         if (!shouldInitialize()) {
  41.             return;
  42.         }
  43.  
  44.         File xulInstallPath = new File(CommonUtils.getUserSettingsDir(), "/browser");
  45.         // Check to see if the correct version of XUL exists.
  46.         File xulFile = new File(xulInstallPath, "xul-v2.0b2.5-do-not-remove-v1");
  47.         if (!xulFile.exists()) {
  48.             if (LOG.isDebugEnabled())
  49.                 LOG.debug("unzip xulrunner to " + xulInstallPath);
  50.             FileUtils.deleteRecursive(xulInstallPath);
  51.             InputStream in = null;
  52.             try {
  53.                 in = new BufferedInputStream(CommonUtils.getResourceStream(getResourceName()));
  54.                 Expand.expandFile(in, xulInstallPath, true, null);
  55.                 xulFile.createNewFile();
  56.             } catch (IOException e) {
  57.                 // This causes errors if the zip file can't be expanded for whatever reason.
  58.                 // It sucks, but no need to report it to us... we fallback to using
  59.                 // JEditorPane where we can.
  60. //                ErrorService.error(e);
  61.             } finally {
  62.                 IOUtils.close(in);
  63.             }
  64.         }
  65.  
  66.         installFlashLinux(xulInstallPath);
  67.  
  68.         String newLibraryPath = System.getProperty("java.library.path") + File.pathSeparator
  69.                 + xulInstallPath.getAbsolutePath();
  70.         System.setProperty("java.library.path", newLibraryPath);
  71.  
  72.         MozillaConfig.setXULRunnerHome(xulInstallPath);
  73.         File profileDir = new File(CommonUtils.getUserSettingsDir(), "/mozilla-profile");
  74.         profileDir.mkdirs();
  75.         MozillaConfig.setProfileDir(profileDir);
  76.         WindowCreator.setWindowFactory(new IMozillaWindowFactory() {
  77.             @Override
  78.             public IMozillaWindow create(boolean attachNewBrowserOnCreation) {
  79.                 MozillaPopupPanel popupPanel = new MozillaPopupPanel(attachNewBrowserOnCreation);
  80.                 MozillaWindow popupWindow = new MozillaWindow(popupPanel);
  81.                 return popupWindow;
  82.             }
  83.         });
  84.         MozillaInitialization.initialize();
  85.  
  86.         if (LOG.isDebugEnabled())
  87.             LOG.debug("Moz Summary: " + MozillaConfig.getConfigSummary());
  88.     }
  89.  
  90.     private static void installFlashLinux(File xulInstallPath) {
  91.         //TODO move logic to .deb? or prompt before doing, this can be a security risk
  92.         File pluginsDir = new File(xulInstallPath, "/xulrunner/plugins");
  93.  
  94.         if (OSUtils.isLinux()) {
  95.             for (File file : pluginsDir.listFiles()) {
  96.                 if (file.isFile() && file.getName().contains("flash")) {
  97.                     return;// flash already installed
  98.                 }
  99.             }
  100.  
  101.             File[] possibleFlashLocations = new File[] { new File("/usr/lib/flash-plugin/"), new File("/usr/lib/firefox/plugins"),
  102.                     new File("/usr/lib/mozilla/plugins"), new File("/usr/lib/iceweasle"),
  103.                     new File("/usr/lib/xulrunner"), new File(CommonUtils.getUserHomeDir(), "/.mozilla/plugins") };
  104.             for (File flashLocation : possibleFlashLocations) {
  105.                 if (flashLocation.exists() && flashLocation.isDirectory()) {
  106.                     boolean foundFlash = false;
  107.                     for (File flashFile : flashLocation.listFiles()) {
  108.                         if (flashFile.getName().contains("flash")) {
  109.                             // TODO make sure we are not using a file we cannot
  110.                             // support, ie 64 bit running in 32 bit jvm
  111.  
  112.                             File linkTarget = new File(pluginsDir, "/" + flashFile.getName());
  113.                             // using a symlink instead of copying, debian had
  114.                             // issues with copying the library.
  115.                             try {
  116.                                 FileUtils.createSymbolicLink(flashFile, linkTarget);
  117.                                 foundFlash = true;
  118.                             } catch (IOException e) {
  119.                                 LOG.debug(e.getMessage(), e);
  120.                             } catch (InterruptedException e) {
  121.                                 LOG.debug(e.getMessage(), e);
  122.                             }
  123.                             // continue looping because there might be more than
  124.                             // 1 file at this location for flash to work
  125.                         }
  126.                     }
  127.                     if (foundFlash) {
  128.                         // flash was found at another location and copied
  129.                         break;
  130.                     }
  131.                 }
  132.             }
  133.         }
  134.     }
  135.  
  136.     private static String getResourceName() {
  137.         if (OSUtils.isWindows()) {
  138.             return "xulrunner-win32.zip";
  139.         } else if (OSUtils.isLinux()) {
  140.             return "xulrunner-linux.zip";
  141.         } else if (OSUtils.isMacOSX()) {
  142.             return "xulrunner-macosx-i386.zip";
  143.         } else {
  144.             throw new IllegalStateException("no resource for OS: " + OSUtils.getOS());
  145.         }
  146.     }
  147. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement