8 changed files with 250 additions and 8 deletions
@ -1,4 +1,3 @@ |
|||||||
content/ |
|
||||||
metadata.inc |
metadata.inc |
||||||
chrome.manifest.inc |
chrome.manifest.inc |
||||||
locale/ |
locale/ |
||||||
|
@ -1,3 +1,2 @@ |
|||||||
resource pdf.js content/ |
resource pdf.js content/ |
||||||
component {d0c5195d-e798-49d4-b1d3-9324328b2291} components/PdfStreamConverter.js |
resource pdf.js.components components/ |
||||||
contract @mozilla.org/streamconv;1?from=application/pdf&to=*/* {d0c5195d-e798-49d4-b1d3-9324328b2291} |
|
||||||
|
@ -0,0 +1,105 @@ |
|||||||
|
var EXPORTED_SYMBOLS = ["PdfJs"]; |
||||||
|
|
||||||
|
const Cc = Components.classes; |
||||||
|
const Ci = Components.interfaces; |
||||||
|
const Cr = Components.results; |
||||||
|
const Cm = Components.manager; |
||||||
|
const Cu = Components.utils; |
||||||
|
|
||||||
|
const PREF_PREFIX = 'pdfjs'; |
||||||
|
const PREF_ENABLED = PREF_PREFIX + '.enabled'; |
||||||
|
const PDFJS_HANDLER_CHANGED = 'pdfjs:handlerChanged'; |
||||||
|
|
||||||
|
Cu.import('resource://gre/modules/Services.jsm'); |
||||||
|
Cu.import('resource://pdf.js.components/PdfStreamConverter.js'); |
||||||
|
|
||||||
|
let mimeService = Cc["@mozilla.org/mime;1"] |
||||||
|
.getService(Ci.nsIMIMEService); |
||||||
|
|
||||||
|
function getBoolPref(pref, def) { |
||||||
|
try { |
||||||
|
return Services.prefs.getBoolPref(pref); |
||||||
|
} catch (ex) { |
||||||
|
return def; |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
// Register/unregister a class as a component.
|
||||||
|
let Factory = { |
||||||
|
registrar: null, |
||||||
|
aClass: null, |
||||||
|
register: function(aClass) { |
||||||
|
if (this.aClass) { |
||||||
|
dump('Cannot register more than one class'); |
||||||
|
return; |
||||||
|
} |
||||||
|
this.registrar = Cm.QueryInterface(Ci.nsIComponentRegistrar); |
||||||
|
this.aClass = aClass; |
||||||
|
var proto = aClass.prototype; |
||||||
|
this.registrar.registerFactory(proto.classID, proto.classDescription, |
||||||
|
proto.contractID, this); |
||||||
|
}, |
||||||
|
unregister: function() { |
||||||
|
if (!this.aClass) { |
||||||
|
dump('Class was never registered.'); |
||||||
|
return; |
||||||
|
} |
||||||
|
var proto = this.aClass.prototype; |
||||||
|
this.registrar.unregisterFactory(proto.classID, this); |
||||||
|
this.aClass = null; |
||||||
|
}, |
||||||
|
// nsIFactory::createInstance
|
||||||
|
createInstance: function(outer, iid) { |
||||||
|
if (outer !== null) |
||||||
|
throw Cr.NS_ERROR_NO_AGGREGATION; |
||||||
|
return (new (this.aClass)).QueryInterface(iid); |
||||||
|
} |
||||||
|
}; |
||||||
|
|
||||||
|
let PdfJs = { |
||||||
|
_registered: false, |
||||||
|
init: function() { |
||||||
|
if (this.enabled) |
||||||
|
this._register(); |
||||||
|
else |
||||||
|
this._unregister(); |
||||||
|
|
||||||
|
// Listen for when pdf.js is completely disabled or a different pdf handler
|
||||||
|
// is chosen.
|
||||||
|
Services.prefs.addObserver(PREF_ENABLED, this, false); |
||||||
|
Services.obs.addObserver(this, PDFJS_HANDLER_CHANGED, false); |
||||||
|
}, |
||||||
|
observe: function(subject, topic, data) { |
||||||
|
if (topic != 'nsPref:changed' && topic != PDFJS_HANDLER_CHANGED) |
||||||
|
return; |
||||||
|
|
||||||
|
if (this.enabled) |
||||||
|
this._register(); |
||||||
|
else |
||||||
|
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.
|
||||||
|
get enabled() { |
||||||
|
var handlerInfo = mimeService. |
||||||
|
getFromTypeAndExtension('application/pdf', 'pdf'); |
||||||
|
|
||||||
|
var selectedAsHandler = handlerInfo && (handlerInfo.alwaysAskBeforeHandling == false && |
||||||
|
handlerInfo.preferredAction == Ci.nsIHandlerInfo.handleInternally); |
||||||
|
return getBoolPref(PREF_ENABLED, false) && selectedAsHandler; |
||||||
|
}, |
||||||
|
_register: function() { |
||||||
|
if (this._registered) |
||||||
|
return; |
||||||
|
|
||||||
|
Factory.register(PdfStreamConverter); |
||||||
|
this._registered = true; |
||||||
|
}, |
||||||
|
_unregister: function() { |
||||||
|
if (!this._registered) |
||||||
|
return; |
||||||
|
|
||||||
|
Factory.unregister(); |
||||||
|
this._registered = false; |
||||||
|
} |
||||||
|
}; |
@ -0,0 +1,54 @@ |
|||||||
|
/* Any copyright is dedicated to the Public Domain. |
||||||
|
* http://creativecommons.org/publicdomain/zero/1.0/
|
||||||
|
*/ |
||||||
|
|
||||||
|
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'); |
||||||
|
|
||||||
|
//
|
||||||
|
// Test: Default mime 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); |
||||||
|
|
||||||
|
waitForExplicitFinish(); |
||||||
|
registerCleanupFunction(function() { |
||||||
|
gBrowser.removeTab(tab); |
||||||
|
}); |
||||||
|
|
||||||
|
tab = gBrowser.addTab(TESTROOT + "file_pdfjs_test.pdf"); |
||||||
|
var newTabBrowser = gBrowser.getBrowserForTab(tab); |
||||||
|
} |
||||||
|
|
||||||
|
|
||||||
|
function addWindowListener(aURL, aCallback) { |
||||||
|
Services.wm.addListener({ |
||||||
|
onOpenWindow: function(aXULWindow) { |
||||||
|
info("window opened, waiting for focus"); |
||||||
|
Services.wm.removeListener(this); |
||||||
|
|
||||||
|
var domwindow = aXULWindow.QueryInterface(Ci.nsIInterfaceRequestor) |
||||||
|
.getInterface(Ci.nsIDOMWindow); |
||||||
|
waitForFocus(function() { |
||||||
|
is(domwindow.document.location.href, aURL, "should have seen the right window open"); |
||||||
|
domwindow.close(); |
||||||
|
aCallback(); |
||||||
|
}, domwindow); |
||||||
|
}, |
||||||
|
onCloseWindow: function(aXULWindow) { }, |
||||||
|
onWindowTitleChange: function(aXULWindow, aNewTitle) { } |
||||||
|
}); |
||||||
|
} |
Loading…
Reference in new issue