Make pdf.js the default pdf viewer for moz central.

Update the tests to reflect this.
This commit is contained in:
Brendan Dahl 2012-06-01 13:21:45 -07:00
parent 3d7f01d9ca
commit 121040a5dc
3 changed files with 64 additions and 64 deletions

View File

@ -8,6 +8,9 @@ const Cu = Components.utils;
const PREF_PREFIX = 'pdfjs';
const PREF_ENABLED = PREF_PREFIX + '.enabled';
const PREF_FIRST_RUN = PREF_PREFIX + '.firstRun';
const PREF_PREVIOUS_ACTION = PREF_PREFIX + '.previousAction';
const PREF_PREVIOUS_ASK = PREF_PREFIX + '.previousAsk';
const PDFJS_HANDLER_CHANGED = 'pdfjs:handlerChanged';
Cu.import('resource://gre/modules/Services.jsm');
@ -30,7 +33,6 @@ let Factory = {
aClass: null,
register: function(aClass) {
if (this.aClass) {
dump('Cannot register more than one class');
return;
}
this.registrar = Cm.QueryInterface(Ci.nsIComponentRegistrar);
@ -41,7 +43,6 @@ let Factory = {
},
unregister: function() {
if (!this.aClass) {
dump('Class was never registered.');
return;
}
var proto = this.aClass.prototype;
@ -59,6 +60,25 @@ let Factory = {
let PdfJs = {
_registered: false,
init: function() {
// On first run make pdf.js the default handler.
if (getBoolPref(PREF_ENABLED, false) && getBoolPref(PREF_FIRST_RUN, false)) {
Services.prefs.setBoolPref(PREF_FIRST_RUN, false);
let handlerService = Cc['@mozilla.org/uriloader/handler-service;1'].
getService(Ci.nsIHandlerService);
let handlerInfo = mimeService.getFromTypeAndExtension('application/pdf', 'pdf');
// Store the previous settings of preferredAction and
// alwaysAskBeforeHandling in case we need to fall back to it.
Services.prefs.setIntPref(PREF_PREVIOUS_ACTION, handlerInfo.preferredAction);
Services.prefs.setBoolPref(PREF_PREVIOUS_ASK, handlerInfo.alwaysAskBeforeHandling);
// Change and save mime handler settings.
handlerInfo.alwaysAskBeforeHandling = false;
handlerInfo.preferredAction = Ci.nsIHandlerInfo.handleInternally;
handlerService.store(handlerInfo);
}
if (this.enabled)
this._register();
else
@ -79,7 +99,7 @@ let PdfJs = {
this._unregister();
},
// pdf.js is only enabled if we're both selected as the pdf viewer and if the
// global switch enabling us is true.
// global switch enabling it is true.
get enabled() {
var handlerInfo = mimeService.
getFromTypeAndExtension('application/pdf', 'pdf');

View File

@ -6,16 +6,18 @@ const RELATIVE_DIR = "browser/extensions/pdfjs/test/";
const TESTROOT = "http://example.com/browser/" + RELATIVE_DIR;
function test() {
var tab, oldAction;
var tab;
oldAction = changeMimeHandler();
const Cc = Components.classes;
const Cc = Components.classes;
const Ci = Components.interfaces;
let handlerService = Cc["@mozilla.org/uriloader/handler-service;1"].getService(Ci.nsIHandlerService);
let mimeService = Cc["@mozilla.org/mime;1"].getService(Ci.nsIMIMEService);
let handlerInfo = mimeService.getFromTypeAndExtension('application/pdf', 'pdf');
// Make sure pdf.js is the default handler.
is(handlerInfo.alwaysAskBeforeHandling, false, 'pdf handler defaults to always-ask is false');
is(handlerInfo.preferredAction, Ci.nsIHandlerInfo.handleInternally, 'pdf handler defaults to internal');
info('Pref action: ' + handlerInfo.preferredAction);
waitForExplicitFinish();
@ -34,7 +36,6 @@ function test() {
// Runs tests after all 'load' event handlers have fired off
setTimeout(function() {
runTests(document, window, function() {
revertMimeHandler(oldAction);
finish();
});
}, 0);
@ -42,51 +43,6 @@ function test() {
}
function changeMimeHandler() {
let oldAction;
const Cc = Components.classes;
const Ci = Components.interfaces;
let handlerService = Cc["@mozilla.org/uriloader/handler-service;1"].getService(Ci.nsIHandlerService);
let mimeService = Cc["@mozilla.org/mime;1"].getService(Ci.nsIMIMEService);
let handlerInfo = mimeService.getFromTypeAndExtension('application/pdf', 'pdf');
oldAction = handlerInfo.preferredAction;
// Change and save mime handler settings
handlerInfo.alwaysAskBeforeHandling = false;
handlerInfo.preferredAction = Ci.nsIHandlerInfo.handleInternally;
handlerService.store(handlerInfo);
Services.obs.notifyObservers(null, 'pdfjs:handlerChanged', null);
// Refresh data
mimeService = Cc["@mozilla.org/mime;1"].getService(Ci.nsIMIMEService);
handlerInfo = mimeService.getFromTypeAndExtension('application/pdf', 'pdf');
//
// Test: Mime handler was updated
//
is(handlerInfo.alwaysAskBeforeHandling, false, 'always-ask prompt change successful');
is(handlerInfo.preferredAction, Ci.nsIHandlerInfo.handleInternally, 'mime handler change successful');
return oldAction;
}
function revertMimeHandler(oldAction) {
const Cc = Components.classes;
const Ci = Components.interfaces;
let handlerService = Cc["@mozilla.org/uriloader/handler-service;1"].getService(Ci.nsIHandlerService);
let mimeService = Cc["@mozilla.org/mime;1"].getService(Ci.nsIMIMEService);
let handlerInfo = mimeService.getFromTypeAndExtension('application/pdf', 'pdf');
// Change and save mime handler settings
handlerInfo.alwaysAskBeforeHandling = true;
handlerInfo.preferredAction = oldAction;
handlerService.store(handlerInfo);
}
function runTests(document, window, callback) {
//

View File

@ -6,26 +6,21 @@ const RELATIVE_DIR = "browser/extensions/pdfjs/test/";
const TESTROOT = "http://example.com/browser/" + RELATIVE_DIR;
function test() {
var tab;
const Cc = Components.classes;
const Ci = Components.interfaces;
let handlerService = Cc["@mozilla.org/uriloader/handler-service;1"].getService(Ci.nsIHandlerService);
let mimeService = Cc["@mozilla.org/mime;1"].getService(Ci.nsIMIMEService);
let handlerInfo = mimeService.getFromTypeAndExtension('application/pdf', 'pdf');
var tab;
var oldAction = changeMimeHandler(Ci.nsIHandlerInfo.useSystemDefault, true);
//
// Test: Default mime handler
//
is(handlerInfo.alwaysAskBeforeHandling, true, 'mime handler: default is always-ask prompt');
//
// Test: "Open with" dialog comes up
// Test: "Open with" dialog comes up when pdf.js is not selected as the default
// handler.
//
addWindowListener('chrome://mozapps/content/downloads/unknownContentType.xul', finish);
waitForExplicitFinish();
registerCleanupFunction(function() {
changeMimeHandler(oldAction[0], oldAction[1]);
gBrowser.removeTab(tab);
});
@ -33,8 +28,37 @@ function test() {
var newTabBrowser = gBrowser.getBrowserForTab(tab);
}
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 mimeService = Cc["@mozilla.org/mime;1"].getService(Ci.nsIMIMEService);
let handlerInfo = mimeService.getFromTypeAndExtension('application/pdf', 'pdf');
var oldAction = [handlerInfo.preferredAction, handlerInfo.alwaysAskBeforeHandling];
// Change and save mime handler settings
handlerInfo.alwaysAskBeforeHandling = alwaysAskBeforeHandling;
handlerInfo.preferredAction = preferredAction;
handlerService.store(handlerInfo);
Services.obs.notifyObservers(null, 'pdfjs:handlerChanged', null);
// Refresh data
mimeService = Cc["@mozilla.org/mime;1"].getService(Ci.nsIMIMEService);
handlerInfo = mimeService.getFromTypeAndExtension('application/pdf', 'pdf');
//
// Test: Mime handler was updated
//
is(handlerInfo.alwaysAskBeforeHandling, alwaysAskBeforeHandling, 'always-ask prompt change successful');
is(handlerInfo.preferredAction, preferredAction, 'mime handler change successful');
return oldAction;
}
function addWindowListener(aURL, aCallback) {
const Cc = Components.classes;
const Ci = Components.interfaces;
Services.wm.addListener({
onOpenWindow: function(aXULWindow) {
info("window opened, waiting for focus");