PageRenderTime 25ms CodeModel.GetById 17ms app.highlight 5ms RepoModel.GetById 1ms app.codeStats 0ms

/prebuilt/extension/components/PluginInstall.js

http://firefox-mac-pdf.googlecode.com/
JavaScript | 110 lines | 54 code | 22 blank | 34 comment | 5 complexity | bafda6d0f394f77a737f5c2be99d5ae1 MD5 | raw file
  1/*
  2 * Copyright (c) 2009 Samuel Gross.
  3 *
  4 * Permission is hereby granted, free of charge, to any person obtaining a copy
  5 * of this software and associated documentation files (the "Software"), to deal
  6 * in the Software without restriction, including without limitation the rights
  7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8 * copies of the Software, and to permit persons to whom the Software is
  9 * furnished to do so, subject to the following conditions:
 10 *
 11 * The above copyright notice and this permission notice shall be included in
 12 * all copies or substantial portions of the Software.
 13 *
 14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 20 * THE SOFTWARE.
 21 */
 22
 23const Cc = Components.classes;
 24const Ci = Components.interfaces;
 25
 26const PREF_DISABLED_PLUGIN_TYPES = "plugin.disable_full_page_plugin_for_types";
 27const PDF_MIME_TYPE = "application/pdf";
 28const PROFILE_AFTER_CHANGE = "profile-after-change";
 29
 30
 31// This file is executed when the user first installs the PDF plugin extenion.
 32//
 33// We remove preferences for handling PDF files that would interfere with the
 34// plugin. For example, the user may have set PDF files to save without asking.
 35//
 36// NOTE: This does not overwrite preferences set after the plugin is installed.
 37// The user can still disable or uninstall the plugin through the add-ons
 38// window, or change the file handling preferences through the "Applications"
 39// preferences tab.
 40
 41
 42function NSGetModule() {
 43  return {
 44    log: function(s) {
 45      var logger = Cc["@mozilla.org/consoleservice;1"].
 46          getService(Ci.nsIConsoleService);
 47  
 48      logger.logStringMessage('' + s);
 49    },
 50
 51    registerSelf: function(compMgr, location, loaderStr, type) {
 52      this.log('NSGetModule');
 53
 54      this.observerSvc = Cc["@mozilla.org/observer-service;1"].
 55          getService(Ci.nsIObserverService);
 56
 57      // We need to wait until the profile is loaded in order to modify
 58      // user and mime-type preferences.
 59      this.observerSvc.addObserver(this, PROFILE_AFTER_CHANGE, false);
 60    },
 61
 62    observe: function(subject, topic, data) {
 63      this.observerSvc.removeObserver(this, PROFILE_AFTER_CHANGE);
 64
 65      try {
 66        this.enablePdfType();
 67      } catch (e) {
 68        this.log("Error enabling PDF type: " + e);
 69      }
 70
 71      try {
 72        this.removePdfHandler();
 73      } catch (e) {
 74        this.log("Error enabling PDF type: " + e);
 75      }
 76    },
 77
 78    /** Enables the PDF plugin. */
 79    enablePdfType: function() {
 80      var prefSvc = Cc["@mozilla.org/preferences-service;1"].
 81              getService(Ci.nsIPrefBranch);
 82
 83      if (prefSvc.prefHasUserValue(PREF_DISABLED_PLUGIN_TYPES)) {
 84        var types = prefSvc.getCharPref(PREF_DISABLED_PLUGIN_TYPES);
 85
 86        if (types) {
 87          var filtered = types.split(",").filter(function(v) v != PDF_MIME_TYPE);
 88
 89          prefSvc.setCharPref(PREF_DISABLED_PLUGIN_TYPES, filtered.join(","));
 90        }
 91      }
 92    },
 93
 94    /** Removes any existing handler for the application/pdf mime-type. */
 95    removePdfHandler: function() {
 96      var handlerSvc = Cc["@mozilla.org/uriloader/handler-service;1"].
 97          getService(Ci.nsIHandlerService);
 98    
 99      var mimeSvc = Cc["@mozilla.org/mime;1"].
100          getService(Ci.nsIMIMEService);
101
102      let mimeInfo = mimeSvc.getFromTypeAndExtension(PDF_MIME_TYPE, null);
103
104      if (mimeInfo && handlerSvc.exists(mimeInfo)) {
105        handlerSvc.remove(mimeInfo);
106      }
107    }
108  }
109}
110