browser/extensions/pdfjs/content/PdfJs.jsm

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 /* Copyright 2012 Mozilla Foundation
michael@0 2 *
michael@0 3 * Licensed under the Apache License, Version 2.0 (the "License");
michael@0 4 * you may not use this file except in compliance with the License.
michael@0 5 * You may obtain a copy of the License at
michael@0 6 *
michael@0 7 * http://www.apache.org/licenses/LICENSE-2.0
michael@0 8 *
michael@0 9 * Unless required by applicable law or agreed to in writing, software
michael@0 10 * distributed under the License is distributed on an "AS IS" BASIS,
michael@0 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
michael@0 12 * See the License for the specific language governing permissions and
michael@0 13 * limitations under the License.
michael@0 14 */
michael@0 15
michael@0 16 var EXPORTED_SYMBOLS = ["PdfJs"];
michael@0 17
michael@0 18 const Cc = Components.classes;
michael@0 19 const Ci = Components.interfaces;
michael@0 20 const Cr = Components.results;
michael@0 21 const Cm = Components.manager;
michael@0 22 const Cu = Components.utils;
michael@0 23
michael@0 24 const PREF_PREFIX = 'pdfjs';
michael@0 25 const PREF_DISABLED = PREF_PREFIX + '.disabled';
michael@0 26 const PREF_MIGRATION_VERSION = PREF_PREFIX + '.migrationVersion';
michael@0 27 const PREF_PREVIOUS_ACTION = PREF_PREFIX + '.previousHandler.preferredAction';
michael@0 28 const PREF_PREVIOUS_ASK = PREF_PREFIX + '.previousHandler.alwaysAskBeforeHandling';
michael@0 29 const PREF_DISABLED_PLUGIN_TYPES = 'plugin.disable_full_page_plugin_for_types';
michael@0 30 const TOPIC_PDFJS_HANDLER_CHANGED = 'pdfjs:handlerChanged';
michael@0 31 const TOPIC_PLUGINS_LIST_UPDATED = "plugins-list-updated";
michael@0 32 const TOPIC_PLUGIN_INFO_UPDATED = "plugin-info-updated";
michael@0 33 const PDF_CONTENT_TYPE = 'application/pdf';
michael@0 34
michael@0 35 Cu.import('resource://gre/modules/XPCOMUtils.jsm');
michael@0 36 Cu.import('resource://gre/modules/Services.jsm');
michael@0 37
michael@0 38 let Svc = {};
michael@0 39 XPCOMUtils.defineLazyServiceGetter(Svc, 'mime',
michael@0 40 '@mozilla.org/mime;1',
michael@0 41 'nsIMIMEService');
michael@0 42 XPCOMUtils.defineLazyServiceGetter(Svc, 'pluginHost',
michael@0 43 '@mozilla.org/plugin/host;1',
michael@0 44 'nsIPluginHost');
michael@0 45
michael@0 46 function getBoolPref(aPref, aDefaultValue) {
michael@0 47 try {
michael@0 48 return Services.prefs.getBoolPref(aPref);
michael@0 49 } catch (ex) {
michael@0 50 return aDefaultValue;
michael@0 51 }
michael@0 52 }
michael@0 53
michael@0 54 function getIntPref(aPref, aDefaultValue) {
michael@0 55 try {
michael@0 56 return Services.prefs.getIntPref(aPref);
michael@0 57 } catch (ex) {
michael@0 58 return aDefaultValue;
michael@0 59 }
michael@0 60 }
michael@0 61
michael@0 62 function initializeDefaultPreferences() {
michael@0 63
michael@0 64 var DEFAULT_PREFERENCES = {
michael@0 65 showPreviousViewOnLoad: true,
michael@0 66 defaultZoomValue: '',
michael@0 67 ifAvailableShowOutlineOnLoad: false,
michael@0 68 enableHandToolOnLoad: false,
michael@0 69 enableWebGL: false
michael@0 70 };
michael@0 71
michael@0 72
michael@0 73 var defaultBranch = Services.prefs.getDefaultBranch(PREF_PREFIX + '.');
michael@0 74 var defaultValue;
michael@0 75 for (var key in DEFAULT_PREFERENCES) {
michael@0 76 defaultValue = DEFAULT_PREFERENCES[key];
michael@0 77 switch (typeof defaultValue) {
michael@0 78 case 'boolean':
michael@0 79 defaultBranch.setBoolPref(key, defaultValue);
michael@0 80 break;
michael@0 81 case 'number':
michael@0 82 defaultBranch.setIntPref(key, defaultValue);
michael@0 83 break;
michael@0 84 case 'string':
michael@0 85 defaultBranch.setCharPref(key, defaultValue);
michael@0 86 break;
michael@0 87 }
michael@0 88 }
michael@0 89 }
michael@0 90
michael@0 91 // Register/unregister a constructor as a factory.
michael@0 92 function Factory() {}
michael@0 93 Factory.prototype = {
michael@0 94 register: function register(targetConstructor) {
michael@0 95 var proto = targetConstructor.prototype;
michael@0 96 this._classID = proto.classID;
michael@0 97
michael@0 98 var factory = XPCOMUtils._getFactory(targetConstructor);
michael@0 99 this._factory = factory;
michael@0 100
michael@0 101 var registrar = Cm.QueryInterface(Ci.nsIComponentRegistrar);
michael@0 102 registrar.registerFactory(proto.classID, proto.classDescription,
michael@0 103 proto.contractID, factory);
michael@0 104 },
michael@0 105
michael@0 106 unregister: function unregister() {
michael@0 107 var registrar = Cm.QueryInterface(Ci.nsIComponentRegistrar);
michael@0 108 registrar.unregisterFactory(this._classID, this._factory);
michael@0 109 this._factory = null;
michael@0 110 }
michael@0 111 };
michael@0 112
michael@0 113 let PdfJs = {
michael@0 114 QueryInterface: XPCOMUtils.generateQI([Ci.nsIObserver]),
michael@0 115 _registered: false,
michael@0 116
michael@0 117 init: function init() {
michael@0 118 if (!getBoolPref(PREF_DISABLED, true)) {
michael@0 119 this._migrate();
michael@0 120 }
michael@0 121
michael@0 122 if (this.enabled)
michael@0 123 this._ensureRegistered();
michael@0 124 else
michael@0 125 this._ensureUnregistered();
michael@0 126
michael@0 127 // Listen for when pdf.js is completely disabled or a different pdf handler
michael@0 128 // is chosen.
michael@0 129 Services.prefs.addObserver(PREF_DISABLED, this, false);
michael@0 130 Services.prefs.addObserver(PREF_DISABLED_PLUGIN_TYPES, this, false);
michael@0 131 Services.obs.addObserver(this, TOPIC_PDFJS_HANDLER_CHANGED, false);
michael@0 132 Services.obs.addObserver(this, TOPIC_PLUGINS_LIST_UPDATED, false);
michael@0 133 Services.obs.addObserver(this, TOPIC_PLUGIN_INFO_UPDATED, false);
michael@0 134
michael@0 135 initializeDefaultPreferences();
michael@0 136 },
michael@0 137
michael@0 138 _migrate: function migrate() {
michael@0 139 const VERSION = 2;
michael@0 140 var currentVersion = getIntPref(PREF_MIGRATION_VERSION, 0);
michael@0 141 if (currentVersion >= VERSION) {
michael@0 142 return;
michael@0 143 }
michael@0 144 // Make pdf.js the default pdf viewer on the first migration.
michael@0 145 if (currentVersion < 1) {
michael@0 146 this._becomeHandler();
michael@0 147 }
michael@0 148 if (currentVersion < 2) {
michael@0 149 // cleaning up of unused database preference (see #3994)
michael@0 150 Services.prefs.clearUserPref(PREF_PREFIX + '.database');
michael@0 151 }
michael@0 152 Services.prefs.setIntPref(PREF_MIGRATION_VERSION, VERSION);
michael@0 153 },
michael@0 154
michael@0 155 _becomeHandler: function _becomeHandler() {
michael@0 156 let handlerInfo = Svc.mime.getFromTypeAndExtension(PDF_CONTENT_TYPE, 'pdf');
michael@0 157 let prefs = Services.prefs;
michael@0 158 if (handlerInfo.preferredAction !== Ci.nsIHandlerInfo.handleInternally &&
michael@0 159 handlerInfo.preferredAction !== false) {
michael@0 160 // Store the previous settings of preferredAction and
michael@0 161 // alwaysAskBeforeHandling in case we need to revert them in a hotfix that
michael@0 162 // would turn pdf.js off.
michael@0 163 prefs.setIntPref(PREF_PREVIOUS_ACTION, handlerInfo.preferredAction);
michael@0 164 prefs.setBoolPref(PREF_PREVIOUS_ASK, handlerInfo.alwaysAskBeforeHandling);
michael@0 165 }
michael@0 166
michael@0 167 let handlerService = Cc['@mozilla.org/uriloader/handler-service;1'].
michael@0 168 getService(Ci.nsIHandlerService);
michael@0 169
michael@0 170 // Change and save mime handler settings.
michael@0 171 handlerInfo.alwaysAskBeforeHandling = false;
michael@0 172 handlerInfo.preferredAction = Ci.nsIHandlerInfo.handleInternally;
michael@0 173 handlerService.store(handlerInfo);
michael@0 174
michael@0 175 // Also disable any plugins for pdfs.
michael@0 176 var stringTypes = '';
michael@0 177 var types = [];
michael@0 178 if (prefs.prefHasUserValue(PREF_DISABLED_PLUGIN_TYPES)) {
michael@0 179 stringTypes = prefs.getCharPref(PREF_DISABLED_PLUGIN_TYPES);
michael@0 180 }
michael@0 181 if (stringTypes !== '') {
michael@0 182 types = stringTypes.split(',');
michael@0 183 }
michael@0 184
michael@0 185 if (types.indexOf(PDF_CONTENT_TYPE) === -1) {
michael@0 186 types.push(PDF_CONTENT_TYPE);
michael@0 187 }
michael@0 188 prefs.setCharPref(PREF_DISABLED_PLUGIN_TYPES, types.join(','));
michael@0 189
michael@0 190 // Update the category manager in case the plugins are already loaded.
michael@0 191 let categoryManager = Cc["@mozilla.org/categorymanager;1"];
michael@0 192 categoryManager.getService(Ci.nsICategoryManager).
michael@0 193 deleteCategoryEntry("Gecko-Content-Viewers",
michael@0 194 PDF_CONTENT_TYPE,
michael@0 195 false);
michael@0 196 },
michael@0 197
michael@0 198 // nsIObserver
michael@0 199 observe: function observe(aSubject, aTopic, aData) {
michael@0 200 if (this.enabled)
michael@0 201 this._ensureRegistered();
michael@0 202 else
michael@0 203 this._ensureUnregistered();
michael@0 204 },
michael@0 205
michael@0 206 /**
michael@0 207 * pdf.js is only enabled if it is both selected as the pdf viewer and if the
michael@0 208 * global switch enabling it is true.
michael@0 209 * @return {boolean} Wether or not it's enabled.
michael@0 210 */
michael@0 211 get enabled() {
michael@0 212 var disabled = getBoolPref(PREF_DISABLED, true);
michael@0 213 if (disabled) {
michael@0 214 return false;
michael@0 215 }
michael@0 216
michael@0 217 // the 'application/pdf' handler is selected as internal?
michael@0 218 var handlerInfo = Svc.mime
michael@0 219 .getFromTypeAndExtension(PDF_CONTENT_TYPE, 'pdf');
michael@0 220 if (handlerInfo.alwaysAskBeforeHandling ||
michael@0 221 handlerInfo.preferredAction !== Ci.nsIHandlerInfo.handleInternally) {
michael@0 222 return false;
michael@0 223 }
michael@0 224
michael@0 225 // Check if we have disabled plugin handling of 'application/pdf' in prefs
michael@0 226 if (Services.prefs.prefHasUserValue(PREF_DISABLED_PLUGIN_TYPES)) {
michael@0 227 let disabledPluginTypes =
michael@0 228 Services.prefs.getCharPref(PREF_DISABLED_PLUGIN_TYPES).split(',');
michael@0 229 if (disabledPluginTypes.indexOf(PDF_CONTENT_TYPE) >= 0) {
michael@0 230 return true;
michael@0 231 }
michael@0 232 }
michael@0 233
michael@0 234 // Check if there is an enabled pdf plugin.
michael@0 235 // Note: this check is performed last because getPluginTags() triggers costly
michael@0 236 // plugin list initialization (bug 881575)
michael@0 237 let tags = Cc["@mozilla.org/plugin/host;1"].
michael@0 238 getService(Ci.nsIPluginHost).
michael@0 239 getPluginTags();
michael@0 240 let enabledPluginFound = tags.some(function(tag) {
michael@0 241 if (tag.disabled) {
michael@0 242 return false;
michael@0 243 }
michael@0 244 let mimeTypes = tag.getMimeTypes();
michael@0 245 return mimeTypes.some(function(mimeType) {
michael@0 246 return mimeType === PDF_CONTENT_TYPE;
michael@0 247 });
michael@0 248 });
michael@0 249
michael@0 250 // Use pdf.js if pdf plugin is not present or disabled
michael@0 251 return !enabledPluginFound;
michael@0 252 },
michael@0 253
michael@0 254 _ensureRegistered: function _ensureRegistered() {
michael@0 255 if (this._registered)
michael@0 256 return;
michael@0 257
michael@0 258 this._pdfStreamConverterFactory = new Factory();
michael@0 259 Cu.import('resource://pdf.js/PdfStreamConverter.jsm');
michael@0 260 this._pdfStreamConverterFactory.register(PdfStreamConverter);
michael@0 261
michael@0 262 this._pdfRedirectorFactory = new Factory();
michael@0 263 Cu.import('resource://pdf.js/PdfRedirector.jsm');
michael@0 264 this._pdfRedirectorFactory.register(PdfRedirector);
michael@0 265
michael@0 266 Svc.pluginHost.registerPlayPreviewMimeType(PDF_CONTENT_TYPE, true,
michael@0 267 'data:application/x-moz-playpreview-pdfjs;,');
michael@0 268
michael@0 269 this._registered = true;
michael@0 270 },
michael@0 271
michael@0 272 _ensureUnregistered: function _ensureUnregistered() {
michael@0 273 if (!this._registered)
michael@0 274 return;
michael@0 275
michael@0 276 this._pdfStreamConverterFactory.unregister();
michael@0 277 Cu.unload('resource://pdf.js/PdfStreamConverter.jsm');
michael@0 278 delete this._pdfStreamConverterFactory;
michael@0 279
michael@0 280 this._pdfRedirectorFactory.unregister();
michael@0 281 Cu.unload('resource://pdf.js/PdfRedirector.jsm');
michael@0 282 delete this._pdfRedirectorFactory;
michael@0 283
michael@0 284 Svc.pluginHost.unregisterPlayPreviewMimeType(PDF_CONTENT_TYPE);
michael@0 285
michael@0 286 this._registered = false;
michael@0 287 }
michael@0 288 };
michael@0 289

mercurial