Skip to content

Commit

Permalink
Merge pull request #5115 from yurydelendik/e10s
Browse files Browse the repository at this point in the history
Firefox pdf viewer support for e10s (Bug 942707).
  • Loading branch information
brendandahl committed Sep 17, 2014
2 parents 15681ad + 1cda4c7 commit f6745d5
Show file tree
Hide file tree
Showing 10 changed files with 763 additions and 99 deletions.
44 changes: 36 additions & 8 deletions extensions/firefox/bootstrap.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@
*/
/* jshint esnext:true */
/* globals Components, Services, dump, XPCOMUtils, PdfStreamConverter,
PdfRedirector, APP_SHUTDOWN, DEFAULT_PREFERENCES */
PdfRedirector, APP_SHUTDOWN, PdfjsChromeUtils, PdfjsContentUtils,
DEFAULT_PREFERENCES */

'use strict';

Expand Down Expand Up @@ -108,10 +109,10 @@ Factory.prototype = {
}
};

var pdfStreamConverterUrl = null;
var pdfStreamConverterFactory = new Factory();
var pdfRedirectorUrl = null;
var pdfBaseUrl = null;
var pdfRedirectorFactory = new Factory();
var e10sEnabled = false;

// As of Firefox 13 bootstrapped add-ons don't support automatic registering and
// unregistering of resource urls and components/contracts. Until then we do
Expand All @@ -125,29 +126,50 @@ function startup(aData, aReason) {
var aliasURI = ioService.newURI('content/', 'UTF-8', aData.resourceURI);
resProt.setSubstitution(RESOURCE_NAME, aliasURI);

pdfBaseUrl = aData.resourceURI.spec;

Cu.import(pdfBaseUrl + 'content/PdfjsChromeUtils.jsm');
PdfjsChromeUtils.init();
Cu.import(pdfBaseUrl + 'content/PdfjsContentUtils.jsm');
PdfjsContentUtils.init();

// Load the component and register it.
pdfStreamConverterUrl = aData.resourceURI.spec +
'content/PdfStreamConverter.jsm';
var pdfStreamConverterUrl = pdfBaseUrl + 'content/PdfStreamConverter.jsm';
Cu.import(pdfStreamConverterUrl);
pdfStreamConverterFactory.register(PdfStreamConverter);

if (registerOverlayPreview) {
pdfRedirectorUrl = aData.resourceURI.spec +
'content/PdfRedirector.jsm';
var pdfRedirectorUrl = pdfBaseUrl + 'content/PdfRedirector.jsm';
Cu.import(pdfRedirectorUrl);
pdfRedirectorFactory.register(PdfRedirector);

Ph.registerPlayPreviewMimeType('application/pdf', true,
'data:application/x-moz-playpreview-pdfjs;,');
}

try {
let globalMM = Cc['@mozilla.org/globalmessagemanager;1']
.getService(Ci.nsIFrameScriptLoader);
globalMM.loadFrameScript('chrome://pdf.js/content/content.js', true);
e10sEnabled = true;
} catch (ex) {
}

initializeDefaultPreferences();
}

function shutdown(aData, aReason) {
if (aReason === APP_SHUTDOWN) {
return;
}

if (e10sEnabled) {
let globalMM = Cc['@mozilla.org/globalmessagemanager;1']
.getService(Ci.nsIMessageBroadcaster);
globalMM.broadcastAsyncMessage('PDFJS:Child:shutdown');
globalMM.removeDelayedFrameScript('chrome://pdf.js/content/content.js');
}

var ioService = Services.io;
var resProt = ioService.getProtocolHandler('resource')
.QueryInterface(Ci.nsIResProtocolHandler);
Expand All @@ -156,16 +178,22 @@ function shutdown(aData, aReason) {
// Remove the contract/component.
pdfStreamConverterFactory.unregister();
// Unload the converter
var pdfStreamConverterUrl = pdfBaseUrl + 'content/PdfStreamConverter.jsm';
Cu.unload(pdfStreamConverterUrl);
pdfStreamConverterUrl = null;

if (registerOverlayPreview) {
pdfRedirectorFactory.unregister();
var pdfRedirectorUrl = pdfBaseUrl + 'content/PdfRedirector.jsm';
Cu.unload(pdfRedirectorUrl);
pdfRedirectorUrl = null;

Ph.unregisterPlayPreviewMimeType('application/pdf');
}

PdfjsContentUtils.uninit();
Cu.unload(pdfBaseUrl + 'content/PdfjsContentUtils.jsm');
PdfjsChromeUtils.uninit();
Cu.unload(pdfBaseUrl + 'content/PdfjsChromeUtils.jsm');
}

function install(aData, aReason) {
Expand Down
2 changes: 2 additions & 0 deletions extensions/firefox/chrome.manifest
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# Additional resources for pdf.js

content pdf.js chrome/

# PDFJS_SUPPORTED_LOCALES
98 changes: 98 additions & 0 deletions extensions/firefox/chrome/content.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
/* -*- Mode: Java; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set shiftwidth=2 tabstop=2 autoindent cindent expandtab: */
/* Copyright 2014 Mozilla Foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/* jshint esnext:true */
/* globals Components, Services, XPCOMUtils, PdfjsContentUtils,
PdfjsContentUtils, PdfStreamConverter, addMessageListener */

'use strict';

const Cc = Components.classes;
const Ci = Components.interfaces;
const Cm = Components.manager;
const Cu = Components.utils;
const Cr = Components.results;

Cu.import('resource://gre/modules/XPCOMUtils.jsm');
Cu.import('resource://gre/modules/Services.jsm');

var isRemote = Services.appinfo.processType ===
Services.appinfo.PROCESS_TYPE_CONTENT;

// Factory that registers/unregisters a constructor as a component.
function Factory() {}

Factory.prototype = {
QueryInterface: XPCOMUtils.generateQI([Ci.nsIFactory]),
_targetConstructor: null,

register: function register(targetConstructor) {
this._targetConstructor = targetConstructor;
var proto = targetConstructor.prototype;
var registrar = Cm.QueryInterface(Ci.nsIComponentRegistrar);
registrar.registerFactory(proto.classID, proto.classDescription,
proto.contractID, this);
},

unregister: function unregister() {
var proto = this._targetConstructor.prototype;
var registrar = Cm.QueryInterface(Ci.nsIComponentRegistrar);
registrar.unregisterFactory(proto.classID, this);
this._targetConstructor = null;
},

// nsIFactory
createInstance: function createInstance(aOuter, iid) {
if (aOuter !== null) {
throw Cr.NS_ERROR_NO_AGGREGATION;
}
return (new (this._targetConstructor)()).QueryInterface(iid);
},

// nsIFactory
lockFactory: function lockFactory(lock) {
// No longer used as of gecko 1.7.
throw Cr.NS_ERROR_NOT_IMPLEMENTED;
}
};

var pdfStreamConverterFactory = new Factory();

function startup() {
Cu.import('resource://pdf.js/PdfjsContentUtils.jsm');
PdfjsContentUtils.init();

Cu.import('resource://pdf.js/PdfStreamConverter.jsm');
pdfStreamConverterFactory.register(PdfStreamConverter);
}

function shutdown() {
// Remove the contract/component.
pdfStreamConverterFactory.unregister();
// Unload the converter
Cu.unload('resource://pdf.js/PdfStreamConverter.jsm');

PdfjsContentUtils.uninit();
Cu.unload('resource://pdf.js/PdfjsContentUtils.jsm');
}

if (isRemote) {
startup();

addMessageListener('PDFJS:Child:shutdown', function (e) {
shutdown();
});
}
75 changes: 60 additions & 15 deletions extensions/firefox/content/PdfJs.jsm
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,12 @@ XPCOMUtils.defineLazyServiceGetter(Svc, 'mime',
XPCOMUtils.defineLazyServiceGetter(Svc, 'pluginHost',
'@mozilla.org/plugin/host;1',
'nsIPluginHost');
XPCOMUtils.defineLazyModuleGetter(this, "BrowserUtils",
"resource://gre/modules/BrowserUtils.jsm");
XPCOMUtils.defineLazyModuleGetter(this, "PdfjsChromeUtils",
"resource://pdf.js/PdfjsChromeUtils.jsm");
XPCOMUtils.defineLazyModuleGetter(this, "PdfjsContentUtils",
"resource://pdf.js/PdfjsContentUtils.jsm");

function getBoolPref(aPref, aDefaultValue) {
try {
Expand All @@ -59,6 +65,13 @@ function getIntPref(aPref, aDefaultValue) {
}
}

function isDefaultHandler() {
if (Services.appinfo.processType == Services.appinfo.PROCESS_TYPE_CONTENT) {
return PdfjsContentUtils.isDefaultHandlerApp();
}
return PdfjsChromeUtils.isDefaultHandlerApp();
}

function initializeDefaultPreferences() {
//#include ../../../web/default_preferences.js

Expand Down Expand Up @@ -105,17 +118,30 @@ Factory.prototype = {
let PdfJs = {
QueryInterface: XPCOMUtils.generateQI([Ci.nsIObserver]),
_registered: false,
_initialized: false,

init: function init(remote) {
if (Services.appinfo.processType != Services.appinfo.PROCESS_TYPE_DEFAULT) {
throw new Error("PdfJs.init should only get called in the parent process.");
}
PdfjsChromeUtils.init();
if (!remote) {
PdfjsContentUtils.init();
}
this.initPrefs();
this.updateRegistration();
},

initPrefs: function initPrefs() {
if (this._initialized) {
return;
}
this._initialized = true;

init: function init() {
if (!getBoolPref(PREF_DISABLED, true)) {
this._migrate();
}

if (this.enabled)
this._ensureRegistered();
else
this._ensureUnregistered();

// Listen for when pdf.js is completely disabled or a different pdf handler
// is chosen.
Services.prefs.addObserver(PREF_DISABLED, this, false);
Expand All @@ -127,6 +153,26 @@ let PdfJs = {
initializeDefaultPreferences();
},

updateRegistration: function updateRegistration() {
if (this.enabled) {
this._ensureRegistered();
} else {
this._ensureUnregistered();
}
},

uninit: function uninit() {
if (this._initialized) {
Services.prefs.removeObserver(PREF_DISABLED, this, false);
Services.prefs.removeObserver(PREF_DISABLED_PLUGIN_TYPES, this, false);
Services.obs.removeObserver(this, TOPIC_PDFJS_HANDLER_CHANGED, false);
Services.obs.removeObserver(this, TOPIC_PLUGINS_LIST_UPDATED, false);
Services.obs.removeObserver(this, TOPIC_PLUGIN_INFO_UPDATED, false);
this._initialized = false;
}
this._ensureUnregistered();
},

_migrate: function migrate() {
const VERSION = 2;
var currentVersion = getIntPref(PREF_MIGRATION_VERSION, 0);
Expand Down Expand Up @@ -189,10 +235,12 @@ let PdfJs = {

// nsIObserver
observe: function observe(aSubject, aTopic, aData) {
if (this.enabled)
this._ensureRegistered();
else
this._ensureUnregistered();
this.updateRegistration();
if (Services.appinfo.processType == Services.appinfo.PROCESS_TYPE_DEFAULT) {
let jsm = "resource://pdf.js/PdfjsChromeUtils.jsm";
let PdfjsChromeUtils = Components.utils.import(jsm, {}).PdfjsChromeUtils;
PdfjsChromeUtils.notifyChildOfSettingsChange();
}
},

/**
Expand All @@ -206,11 +254,8 @@ let PdfJs = {
return false;
}

// the 'application/pdf' handler is selected as internal?
var handlerInfo = Svc.mime
.getFromTypeAndExtension(PDF_CONTENT_TYPE, 'pdf');
if (handlerInfo.alwaysAskBeforeHandling ||
handlerInfo.preferredAction !== Ci.nsIHandlerInfo.handleInternally) {
// Check if the 'application/pdf' preview handler is configured properly.
if (!isDefaultHandler()) {
return false;
}

Expand Down
Loading

0 comments on commit f6745d5

Please sign in to comment.