Addresses review feedback from mozilla central. See bugzilla bug 752676.

This commit is contained in:
Brendan Dahl 2012-06-04 09:38:22 -07:00
parent 121040a5dc
commit da94701c7b
4 changed files with 97 additions and 94 deletions

View File

@ -9,6 +9,7 @@ const Cc = Components.classes;
const Ci = Components.interfaces; const Ci = Components.interfaces;
const Cr = Components.results; const Cr = Components.results;
const Cu = Components.utils; const Cu = Components.utils;
// True only if this is the version of pdf.js that is included with firefox.
const MOZ_CENTRAL = PDFJSSCRIPT_MOZ_CENTRAL; const MOZ_CENTRAL = PDFJSSCRIPT_MOZ_CENTRAL;
const PDFJS_EVENT_ID = 'pdf.js.message'; const PDFJS_EVENT_ID = 'pdf.js.message';
const PDF_CONTENT_TYPE = 'application/pdf'; const PDF_CONTENT_TYPE = 'application/pdf';
@ -21,11 +22,14 @@ Cu.import('resource://gre/modules/XPCOMUtils.jsm');
Cu.import('resource://gre/modules/Services.jsm'); Cu.import('resource://gre/modules/Services.jsm');
Cu.import('resource://gre/modules/NetUtil.jsm'); Cu.import('resource://gre/modules/NetUtil.jsm');
let appInfo = Cc['@mozilla.org/xre/app-info;1'] let appInfo = Cc['@mozilla.org/xre/app-info;1']
.getService(Ci.nsIXULAppInfo); .getService(Ci.nsIXULAppInfo);
let privateBrowsing, inPrivateBrowsing; let privateBrowsing, inPrivateBrowsing;
let mimeService = Cc['@mozilla.org/mime;1'] let Svc = {};
.getService(Ci.nsIMIMEService); XPCOMUtils.defineLazyServiceGetter(Svc, 'mime',
'@mozilla.org/mime;1',
'nsIMIMEService');
if (appInfo.ID === FIREFOX_ID) { if (appInfo.ID === FIREFOX_ID) {
privateBrowsing = Cc['@mozilla.org/privatebrowsing;1'] privateBrowsing = Cc['@mozilla.org/privatebrowsing;1']
@ -75,15 +79,15 @@ function getDOMWindow(aChannel) {
function isEnabled() { function isEnabled() {
if (MOZ_CENTRAL) { if (MOZ_CENTRAL) {
var enabled = getBoolPref(PREF_PREFIX + '.enabled', false); var disabled = getBoolPref(PREF_PREFIX + '.disabled', false);
if (!enabled) if (disabled)
return false; return false;
// To also be considered enabled the "Preview in Firefox" option must be // To also be considered enabled the "Preview in Firefox" option must be
// selected in the Application preferences. // selected in the Application preferences.
var handlerInfo = mimeService. var handlerInfo = Svc.mime
getFromTypeAndExtension('application/pdf', 'pdf'); .getFromTypeAndExtension('application/pdf', 'pdf');
return handlerInfo && (handlerInfo.alwaysAskBeforeHandling == false && return handlerInfo.alwaysAskBeforeHandling == false &&
handlerInfo.preferredAction == Ci.nsIHandlerInfo.handleInternally); handlerInfo.preferredAction == Ci.nsIHandlerInfo.handleInternally;
} }
// Always returns true for the extension since enabling/disabling is handled // Always returns true for the extension since enabling/disabling is handled
// by the add-on manager. // by the add-on manager.
@ -111,9 +115,10 @@ function getLocalizedStrings(path) {
} }
return map; return map;
} }
function getLocalizedString(strings, id) { function getLocalizedString(strings, id, property) {
property = property || 'textContent';
if (id in strings) if (id in strings)
return strings[id]['textContent']; return strings[id][property];
return id; return id;
} }
@ -124,9 +129,8 @@ function ChromeActions(domWindow) {
ChromeActions.prototype = { ChromeActions.prototype = {
download: function(data) { download: function(data) {
let mimeService = Cc['@mozilla.org/mime;1'].getService(Ci.nsIMIMEService); var handlerInfo = Svc.mime
var handlerInfo = mimeService. .getFromTypeAndExtension('application/pdf', 'pdf');
getFromTypeAndExtension('application/pdf', 'pdf');
var uri = NetUtil.newURI(data); var uri = NetUtil.newURI(data);
var extHelperAppSvc = var extHelperAppSvc =
@ -200,10 +204,10 @@ ChromeActions.prototype = {
var win = Services.wm.getMostRecentWindow('navigator:browser'); var win = Services.wm.getMostRecentWindow('navigator:browser');
var browser = win.gBrowser.getBrowserForDocument(domWindow.top.document); var browser = win.gBrowser.getBrowserForDocument(domWindow.top.document);
var notificationBox = win.gBrowser.getNotificationBox(browser); var notificationBox = win.gBrowser.getNotificationBox(browser);
var buttons = [{ var buttons = [{
label: getLocalizedString(strings, 'open_with_different_viewer'), label: getLocalizedString(strings, 'open_with_different_viewer'),
accessKey: null, accessKey: getLocalizedString(strings, 'open_with_different_viewer',
'accessKey'),
callback: function() { callback: function() {
self.download(url); self.download(url);
} }

View File

@ -7,72 +7,82 @@ const Cm = Components.manager;
const Cu = Components.utils; const Cu = Components.utils;
const PREF_PREFIX = 'pdfjs'; const PREF_PREFIX = 'pdfjs';
const PREF_ENABLED = PREF_PREFIX + '.enabled'; const PREF_DISABLED = PREF_PREFIX + '.disabled';
const PREF_FIRST_RUN = PREF_PREFIX + '.firstRun'; const PREF_FIRST_RUN = PREF_PREFIX + '.firstRun';
const PREF_PREVIOUS_ACTION = PREF_PREFIX + '.previousAction'; const PREF_PREVIOUS_ACTION = PREF_PREFIX + '.previousHandler.preferredAction';
const PREF_PREVIOUS_ASK = PREF_PREFIX + '.previousAsk'; const PREF_PREVIOUS_ASK = PREF_PREFIX + '.previousHandler.alwaysAskBeforeHandling';
const PDFJS_HANDLER_CHANGED = 'pdfjs:handlerChanged'; const TOPIC_PDFJS_HANDLER_CHANGED = 'pdfjs:handlerChanged';
Cu.import('resource://gre/modules/XPCOMUtils.jsm');
Cu.import('resource://gre/modules/Services.jsm'); Cu.import('resource://gre/modules/Services.jsm');
Cu.import('resource://pdf.js.components/PdfStreamConverter.js'); Cu.import('resource://pdf.js.components/PdfStreamConverter.js');
let mimeService = Cc["@mozilla.org/mime;1"] let Svc = {};
.getService(Ci.nsIMIMEService); XPCOMUtils.defineLazyServiceGetter(Svc, 'mime',
'@mozilla.org/mime;1',
'nsIMIMEService');
function getBoolPref(pref, def) { function getBoolPref(aPref, aDefaultValue) {
try { try {
return Services.prefs.getBoolPref(pref); return Services.prefs.getBoolPref(aPref);
} catch (ex) { } catch (ex) {
return def; return aDefaultValue;
} }
} }
// Register/unregister a class as a component. // Register/unregister a constructor as a component.
let Factory = { let Factory = {
registrar: null, QueryInterface: XPCOMUtils.generateQI([Ci.nsIFactory]),
aClass: null, _targetConstructor: null,
register: function(aClass) {
if (this.aClass) { register: function register(targetConstructor) {
return; this._targetConstructor = targetConstructor;
} var proto = targetConstructor.prototype;
this.registrar = Cm.QueryInterface(Ci.nsIComponentRegistrar); var registrar = Cm.QueryInterface(Ci.nsIComponentRegistrar);
this.aClass = aClass; registrar.registerFactory(proto.classID, proto.classDescription,
var proto = aClass.prototype; proto.contractID, this);
this.registrar.registerFactory(proto.classID, proto.classDescription,
proto.contractID, this);
}, },
unregister: function() {
if (!this.aClass) { unregister: function unregister() {
return; var proto = this._targetConstructor.prototype;
} var registrar = Cm.QueryInterface(Ci.nsIComponentRegistrar);
var proto = this.aClass.prototype; registrar.unregisterFactory(proto.classID, this);
this.registrar.unregisterFactory(proto.classID, this); this._targetConstructor = null;
this.aClass = null;
}, },
// nsIFactory::createInstance
createInstance: function(outer, iid) { // nsIFactory
if (outer !== null) createInstance: function createInstance(aOuter, iid) {
if (aOuter !== null)
throw Cr.NS_ERROR_NO_AGGREGATION; throw Cr.NS_ERROR_NO_AGGREGATION;
return (new (this.aClass)).QueryInterface(iid); 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;
} }
}; };
let PdfJs = { let PdfJs = {
QueryInterface: XPCOMUtils.generateQI([Ci.nsIObserver]),
_registered: false, _registered: false,
init: function() {
init: function init() {
// On first run make pdf.js the default handler. // On first run make pdf.js the default handler.
if (getBoolPref(PREF_ENABLED, false) && getBoolPref(PREF_FIRST_RUN, false)) { if (!getBoolPref(PREF_DISABLED, true) && getBoolPref(PREF_FIRST_RUN, false)) {
Services.prefs.setBoolPref(PREF_FIRST_RUN, false); Services.prefs.setBoolPref(PREF_FIRST_RUN, false);
let handlerService = Cc['@mozilla.org/uriloader/handler-service;1']. let handlerInfo = Svc.mime.getFromTypeAndExtension('application/pdf', 'pdf');
getService(Ci.nsIHandlerService);
let handlerInfo = mimeService.getFromTypeAndExtension('application/pdf', 'pdf');
// Store the previous settings of preferredAction and // Store the previous settings of preferredAction and
// alwaysAskBeforeHandling in case we need to fall back to it. // alwaysAskBeforeHandling in case we need to revert them in a hotfix that
// would turn pdf.js off.
Services.prefs.setIntPref(PREF_PREVIOUS_ACTION, handlerInfo.preferredAction); Services.prefs.setIntPref(PREF_PREVIOUS_ACTION, handlerInfo.preferredAction);
Services.prefs.setBoolPref(PREF_PREVIOUS_ASK, handlerInfo.alwaysAskBeforeHandling); Services.prefs.setBoolPref(PREF_PREVIOUS_ASK, handlerInfo.alwaysAskBeforeHandling);
let handlerService = Cc['@mozilla.org/uriloader/handler-service;1'].
getService(Ci.nsIHandlerService);
// Change and save mime handler settings. // Change and save mime handler settings.
handlerInfo.alwaysAskBeforeHandling = false; handlerInfo.alwaysAskBeforeHandling = false;
handlerInfo.preferredAction = Ci.nsIHandlerInfo.handleInternally; handlerInfo.preferredAction = Ci.nsIHandlerInfo.handleInternally;
@ -80,42 +90,49 @@ let PdfJs = {
} }
if (this.enabled) if (this.enabled)
this._register(); this._ensureRegistered();
else else
this._unregister(); this._ensureUnregistered();
// Listen for when pdf.js is completely disabled or a different pdf handler // Listen for when pdf.js is completely disabled or a different pdf handler
// is chosen. // is chosen.
Services.prefs.addObserver(PREF_ENABLED, this, false); Services.prefs.addObserver(PREF_DISABLED, this, false);
Services.obs.addObserver(this, PDFJS_HANDLER_CHANGED, false); Services.obs.addObserver(this, TOPIC_PDFJS_HANDLER_CHANGED, false);
}, },
observe: function(subject, topic, data) {
if (topic != 'nsPref:changed' && topic != PDFJS_HANDLER_CHANGED)
return;
// nsIObserver
observe: function observe(aSubject, aTopic, aData) {
if (this.enabled) if (this.enabled)
this._register(); this._ensureRegistered();
else else
this._unregister(); this._ensureUnregistered();
}, },
// pdf.js is only enabled if we're both selected as the pdf viewer and if the
// global switch enabling it is true. /**
* pdf.js is only enabled if it is both selected as the pdf viewer and if the
* global switch enabling it is true.
* @return {boolean} Wether or not it's enabled.
*/
get enabled() { get enabled() {
var handlerInfo = mimeService. var disabled = getBoolPref(PREF_DISABLED, true);
getFromTypeAndExtension('application/pdf', 'pdf'); if (disabled)
return false;
var selectedAsHandler = handlerInfo && (handlerInfo.alwaysAskBeforeHandling == false && var handlerInfo = Svc.mime.
handlerInfo.preferredAction == Ci.nsIHandlerInfo.handleInternally); getFromTypeAndExtension('application/pdf', 'pdf');
return getBoolPref(PREF_ENABLED, false) && selectedAsHandler; return handlerInfo.alwaysAskBeforeHandling == false &&
handlerInfo.preferredAction == Ci.nsIHandlerInfo.handleInternally;
}, },
_register: function() {
_ensureRegistered: function _ensureRegistered() {
if (this._registered) if (this._registered)
return; return;
Factory.register(PdfStreamConverter); Factory.register(PdfStreamConverter);
this._registered = true; this._registered = true;
}, },
_unregister: function() {
_ensureUnregistered: function _ensureUnregistered() {
if (!this._registered) if (!this._registered)
return; return;

View File

@ -1,6 +1,5 @@
/* Any copyright is dedicated to the Public Domain. /* Any copyright is dedicated to the Public Domain.
* http://creativecommons.org/publicdomain/zero/1.0/ * http://creativecommons.org/publicdomain/zero/1.0/ */
*/
const RELATIVE_DIR = "browser/extensions/pdfjs/test/"; const RELATIVE_DIR = "browser/extensions/pdfjs/test/";
const TESTROOT = "http://example.com/browser/" + RELATIVE_DIR; const TESTROOT = "http://example.com/browser/" + RELATIVE_DIR;
@ -8,8 +7,6 @@ const TESTROOT = "http://example.com/browser/" + RELATIVE_DIR;
function test() { function test() {
var tab; var tab;
const Cc = Components.classes;
const Ci = Components.interfaces;
let handlerService = Cc["@mozilla.org/uriloader/handler-service;1"].getService(Ci.nsIHandlerService); let handlerService = Cc["@mozilla.org/uriloader/handler-service;1"].getService(Ci.nsIHandlerService);
let mimeService = Cc["@mozilla.org/mime;1"].getService(Ci.nsIMIMEService); let mimeService = Cc["@mozilla.org/mime;1"].getService(Ci.nsIMIMEService);
let handlerInfo = mimeService.getFromTypeAndExtension('application/pdf', 'pdf'); let handlerInfo = mimeService.getFromTypeAndExtension('application/pdf', 'pdf');
@ -35,9 +32,7 @@ function test() {
// Runs tests after all 'load' event handlers have fired off // Runs tests after all 'load' event handlers have fired off
setTimeout(function() { setTimeout(function() {
runTests(document, window, function() { runTests(document, window, finish);
finish();
});
}, 0); }, 0);
}, true); }, true);
} }

View File

@ -1,17 +1,12 @@
/* Any copyright is dedicated to the Public Domain. /* Any copyright is dedicated to the Public Domain.
* http://creativecommons.org/publicdomain/zero/1.0/ * http://creativecommons.org/publicdomain/zero/1.0/ */
*/
const RELATIVE_DIR = "browser/extensions/pdfjs/test/"; const RELATIVE_DIR = "browser/extensions/pdfjs/test/";
const TESTROOT = "http://example.com/browser/" + RELATIVE_DIR; const TESTROOT = "http://example.com/browser/" + RELATIVE_DIR;
function test() { function test() {
const Cc = Components.classes;
const Ci = Components.interfaces;
var tab;
var oldAction = changeMimeHandler(Ci.nsIHandlerInfo.useSystemDefault, true); var oldAction = changeMimeHandler(Ci.nsIHandlerInfo.useSystemDefault, true);
var tab = gBrowser.addTab(TESTROOT + "file_pdfjs_test.pdf");
// //
// Test: "Open with" dialog comes up when pdf.js is not selected as the default // Test: "Open with" dialog comes up when pdf.js is not selected as the default
// handler. // handler.
@ -23,14 +18,9 @@ function test() {
changeMimeHandler(oldAction[0], oldAction[1]); changeMimeHandler(oldAction[0], oldAction[1]);
gBrowser.removeTab(tab); gBrowser.removeTab(tab);
}); });
tab = gBrowser.addTab(TESTROOT + "file_pdfjs_test.pdf");
var newTabBrowser = gBrowser.getBrowserForTab(tab);
} }
function changeMimeHandler(preferredAction, alwaysAskBeforeHandling) { function changeMimeHandler(preferredAction, alwaysAskBeforeHandling) {
const Cc = Components.classes;
const Ci = Components.interfaces;
let handlerService = Cc["@mozilla.org/uriloader/handler-service;1"].getService(Ci.nsIHandlerService); let handlerService = Cc["@mozilla.org/uriloader/handler-service;1"].getService(Ci.nsIHandlerService);
let mimeService = Cc["@mozilla.org/mime;1"].getService(Ci.nsIMIMEService); let mimeService = Cc["@mozilla.org/mime;1"].getService(Ci.nsIMIMEService);
let handlerInfo = mimeService.getFromTypeAndExtension('application/pdf', 'pdf'); let handlerInfo = mimeService.getFromTypeAndExtension('application/pdf', 'pdf');
@ -44,7 +34,6 @@ function changeMimeHandler(preferredAction, alwaysAskBeforeHandling) {
Services.obs.notifyObservers(null, 'pdfjs:handlerChanged', null); Services.obs.notifyObservers(null, 'pdfjs:handlerChanged', null);
// Refresh data // Refresh data
mimeService = Cc["@mozilla.org/mime;1"].getService(Ci.nsIMIMEService);
handlerInfo = mimeService.getFromTypeAndExtension('application/pdf', 'pdf'); handlerInfo = mimeService.getFromTypeAndExtension('application/pdf', 'pdf');
// //
@ -57,8 +46,6 @@ function changeMimeHandler(preferredAction, alwaysAskBeforeHandling) {
} }
function addWindowListener(aURL, aCallback) { function addWindowListener(aURL, aCallback) {
const Cc = Components.classes;
const Ci = Components.interfaces;
Services.wm.addListener({ Services.wm.addListener({
onOpenWindow: function(aXULWindow) { onOpenWindow: function(aXULWindow) {
info("window opened, waiting for focus"); info("window opened, waiting for focus");