/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. const Cc = Components.classes;
  23. const Ci = Components.interfaces;
  24. const PREF_DISABLED_PLUGIN_TYPES = "plugin.disable_full_page_plugin_for_types";
  25. const PDF_MIME_TYPE = "application/pdf";
  26. const PROFILE_AFTER_CHANGE = "profile-after-change";
  27. // This file is executed when the user first installs the PDF plugin extenion.
  28. //
  29. // We remove preferences for handling PDF files that would interfere with the
  30. // plugin. For example, the user may have set PDF files to save without asking.
  31. //
  32. // NOTE: This does not overwrite preferences set after the plugin is installed.
  33. // The user can still disable or uninstall the plugin through the add-ons
  34. // window, or change the file handling preferences through the "Applications"
  35. // preferences tab.
  36. function NSGetModule() {
  37. return {
  38. log: function(s) {
  39. var logger = Cc["@mozilla.org/consoleservice;1"].
  40. getService(Ci.nsIConsoleService);
  41. logger.logStringMessage('' + s);
  42. },
  43. registerSelf: function(compMgr, location, loaderStr, type) {
  44. this.log('NSGetModule');
  45. this.observerSvc = Cc["@mozilla.org/observer-service;1"].
  46. getService(Ci.nsIObserverService);
  47. // We need to wait until the profile is loaded in order to modify
  48. // user and mime-type preferences.
  49. this.observerSvc.addObserver(this, PROFILE_AFTER_CHANGE, false);
  50. },
  51. observe: function(subject, topic, data) {
  52. this.observerSvc.removeObserver(this, PROFILE_AFTER_CHANGE);
  53. try {
  54. this.enablePdfType();
  55. } catch (e) {
  56. this.log("Error enabling PDF type: " + e);
  57. }
  58. try {
  59. this.removePdfHandler();
  60. } catch (e) {
  61. this.log("Error enabling PDF type: " + e);
  62. }
  63. },
  64. /** Enables the PDF plugin. */
  65. enablePdfType: function() {
  66. var prefSvc = Cc["@mozilla.org/preferences-service;1"].
  67. getService(Ci.nsIPrefBranch);
  68. if (prefSvc.prefHasUserValue(PREF_DISABLED_PLUGIN_TYPES)) {
  69. var types = prefSvc.getCharPref(PREF_DISABLED_PLUGIN_TYPES);
  70. if (types) {
  71. var filtered = types.split(",").filter(function(v) v != PDF_MIME_TYPE);
  72. prefSvc.setCharPref(PREF_DISABLED_PLUGIN_TYPES, filtered.join(","));
  73. }
  74. }
  75. },
  76. /** Removes any existing handler for the application/pdf mime-type. */
  77. removePdfHandler: function() {
  78. var handlerSvc = Cc["@mozilla.org/uriloader/handler-service;1"].
  79. getService(Ci.nsIHandlerService);
  80. var mimeSvc = Cc["@mozilla.org/mime;1"].
  81. getService(Ci.nsIMIMEService);
  82. let mimeInfo = mimeSvc.getFromTypeAndExtension(PDF_MIME_TYPE, null);
  83. if (mimeInfo && handlerSvc.exists(mimeInfo)) {
  84. handlerSvc.remove(mimeInfo);
  85. }
  86. }
  87. }
  88. }