Make pdf.js the default pdf viewer for moz central.
Update the tests to reflect this.
This commit is contained in:
parent
3d7f01d9ca
commit
121040a5dc
@ -8,6 +8,9 @@ const Cu = Components.utils;
|
|||||||
|
|
||||||
const PREF_PREFIX = 'pdfjs';
|
const PREF_PREFIX = 'pdfjs';
|
||||||
const PREF_ENABLED = PREF_PREFIX + '.enabled';
|
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';
|
const PDFJS_HANDLER_CHANGED = 'pdfjs:handlerChanged';
|
||||||
|
|
||||||
Cu.import('resource://gre/modules/Services.jsm');
|
Cu.import('resource://gre/modules/Services.jsm');
|
||||||
@ -30,7 +33,6 @@ let Factory = {
|
|||||||
aClass: null,
|
aClass: null,
|
||||||
register: function(aClass) {
|
register: function(aClass) {
|
||||||
if (this.aClass) {
|
if (this.aClass) {
|
||||||
dump('Cannot register more than one class');
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
this.registrar = Cm.QueryInterface(Ci.nsIComponentRegistrar);
|
this.registrar = Cm.QueryInterface(Ci.nsIComponentRegistrar);
|
||||||
@ -41,7 +43,6 @@ let Factory = {
|
|||||||
},
|
},
|
||||||
unregister: function() {
|
unregister: function() {
|
||||||
if (!this.aClass) {
|
if (!this.aClass) {
|
||||||
dump('Class was never registered.');
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
var proto = this.aClass.prototype;
|
var proto = this.aClass.prototype;
|
||||||
@ -59,6 +60,25 @@ let Factory = {
|
|||||||
let PdfJs = {
|
let PdfJs = {
|
||||||
_registered: false,
|
_registered: false,
|
||||||
init: function() {
|
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)
|
if (this.enabled)
|
||||||
this._register();
|
this._register();
|
||||||
else
|
else
|
||||||
@ -79,7 +99,7 @@ let PdfJs = {
|
|||||||
this._unregister();
|
this._unregister();
|
||||||
},
|
},
|
||||||
// pdf.js is only enabled if we're both selected as the pdf viewer and if the
|
// 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() {
|
get enabled() {
|
||||||
var handlerInfo = mimeService.
|
var handlerInfo = mimeService.
|
||||||
getFromTypeAndExtension('application/pdf', 'pdf');
|
getFromTypeAndExtension('application/pdf', 'pdf');
|
||||||
|
@ -6,16 +6,18 @@ 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() {
|
||||||
var tab, oldAction;
|
var tab;
|
||||||
|
|
||||||
oldAction = changeMimeHandler();
|
const Cc = Components.classes;
|
||||||
|
|
||||||
const Cc = Components.classes;
|
|
||||||
const Ci = Components.interfaces;
|
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');
|
||||||
|
|
||||||
|
// 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);
|
info('Pref action: ' + handlerInfo.preferredAction);
|
||||||
|
|
||||||
waitForExplicitFinish();
|
waitForExplicitFinish();
|
||||||
@ -34,7 +36,6 @@ 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, function() {
|
||||||
revertMimeHandler(oldAction);
|
|
||||||
finish();
|
finish();
|
||||||
});
|
});
|
||||||
}, 0);
|
}, 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) {
|
function runTests(document, window, callback) {
|
||||||
|
|
||||||
//
|
//
|
||||||
|
@ -6,26 +6,21 @@ 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() {
|
||||||
var tab;
|
|
||||||
|
|
||||||
const Cc = Components.classes;
|
const Cc = Components.classes;
|
||||||
const Ci = Components.interfaces;
|
const Ci = Components.interfaces;
|
||||||
let handlerService = Cc["@mozilla.org/uriloader/handler-service;1"].getService(Ci.nsIHandlerService);
|
var tab;
|
||||||
let mimeService = Cc["@mozilla.org/mime;1"].getService(Ci.nsIMIMEService);
|
|
||||||
let handlerInfo = mimeService.getFromTypeAndExtension('application/pdf', 'pdf');
|
var oldAction = changeMimeHandler(Ci.nsIHandlerInfo.useSystemDefault, true);
|
||||||
|
|
||||||
//
|
//
|
||||||
// Test: Default mime handler
|
// Test: "Open with" dialog comes up when pdf.js is not selected as the default
|
||||||
//
|
// handler.
|
||||||
is(handlerInfo.alwaysAskBeforeHandling, true, 'mime handler: default is always-ask prompt');
|
|
||||||
|
|
||||||
//
|
|
||||||
// Test: "Open with" dialog comes up
|
|
||||||
//
|
//
|
||||||
addWindowListener('chrome://mozapps/content/downloads/unknownContentType.xul', finish);
|
addWindowListener('chrome://mozapps/content/downloads/unknownContentType.xul', finish);
|
||||||
|
|
||||||
waitForExplicitFinish();
|
waitForExplicitFinish();
|
||||||
registerCleanupFunction(function() {
|
registerCleanupFunction(function() {
|
||||||
|
changeMimeHandler(oldAction[0], oldAction[1]);
|
||||||
gBrowser.removeTab(tab);
|
gBrowser.removeTab(tab);
|
||||||
});
|
});
|
||||||
|
|
||||||
@ -33,8 +28,37 @@ function test() {
|
|||||||
var newTabBrowser = gBrowser.getBrowserForTab(tab);
|
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) {
|
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");
|
||||||
|
Loading…
Reference in New Issue
Block a user