2011-09-28 04:15:06 +09:00
|
|
|
/* -*- Mode: Java; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim: set shiftwidth=2 tabstop=2 autoindent cindent expandtab: */
|
|
|
|
|
|
|
|
'use strict';
|
|
|
|
|
2011-09-07 07:00:24 +09:00
|
|
|
const Cc = Components.classes;
|
|
|
|
const Ci = Components.interfaces;
|
|
|
|
const Cr = Components.results;
|
|
|
|
const Cu = Components.utils;
|
|
|
|
|
2011-09-28 04:15:06 +09:00
|
|
|
const PDF_CONTENT_TYPE = 'application/pdf';
|
2011-09-07 07:00:24 +09:00
|
|
|
|
2011-09-28 04:15:06 +09:00
|
|
|
Cu.import('resource://gre/modules/XPCOMUtils.jsm');
|
|
|
|
Cu.import('resource://gre/modules/Services.jsm');
|
2011-09-07 07:00:24 +09:00
|
|
|
|
|
|
|
function log(aMsg) {
|
2011-09-28 04:15:06 +09:00
|
|
|
let msg = 'pdfContentHandler.js: ' + (aMsg.join ? aMsg.join('') : aMsg);
|
|
|
|
Cc['@mozilla.org/consoleservice;1'].getService(Ci.nsIConsoleService)
|
2011-09-07 07:00:24 +09:00
|
|
|
.logStringMessage(msg);
|
2011-09-28 04:15:06 +09:00
|
|
|
dump(msg + '\n');
|
|
|
|
}
|
2011-09-07 07:00:24 +09:00
|
|
|
|
2012-01-26 10:40:08 +09:00
|
|
|
const NS_ERROR_NOT_IMPLEMENTED = 0x80004001;
|
2012-01-24 09:50:45 +09:00
|
|
|
|
2011-09-07 07:00:24 +09:00
|
|
|
function pdfContentHandler() {
|
2012-01-24 10:52:53 +09:00
|
|
|
}
|
2011-09-07 07:00:24 +09:00
|
|
|
|
|
|
|
pdfContentHandler.prototype = {
|
|
|
|
|
2012-01-24 09:50:45 +09:00
|
|
|
// properties required for XPCOM registration:
|
|
|
|
classID: Components.ID('{2278dfd0-b75c-11e0-8257-1ba3d93c9f1a}'),
|
|
|
|
classDescription: 'pdf.js Component',
|
|
|
|
contractID: '@mozilla.org/streamconv;1?from=application/pdf&to=*/*',
|
2012-01-24 10:52:53 +09:00
|
|
|
|
2012-01-24 09:50:45 +09:00
|
|
|
QueryInterface: XPCOMUtils.generateQI([
|
|
|
|
Ci.nsISupports,
|
|
|
|
Ci.nsIStreamConverter,
|
|
|
|
Ci.nsIStreamListener,
|
|
|
|
Ci.nsIRequestObserver
|
|
|
|
]),
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This component works as such:
|
|
|
|
* 1. asyncConvertData stores the listener
|
|
|
|
* 2. onStartRequest creates a new channel, streams the viewer and cancels
|
|
|
|
* the request so pdf.js can do the request
|
|
|
|
* Since the request is cancelled onDataAvailable should not be called. The
|
|
|
|
* onStopRequest does nothing. The convert function just returns the stream,
|
|
|
|
* it's just the synchronous version of asyncConvertData.
|
|
|
|
*/
|
|
|
|
|
|
|
|
// nsIStreamConverter::convert
|
2012-01-24 10:52:53 +09:00
|
|
|
convert: function(aFromStream, aFromType, aToType, aCtxt) {
|
2012-01-26 10:40:08 +09:00
|
|
|
return aFromStream;
|
2012-01-24 09:50:45 +09:00
|
|
|
},
|
2011-09-07 07:00:24 +09:00
|
|
|
|
2012-01-24 09:50:45 +09:00
|
|
|
// nsIStreamConverter::asyncConvertData
|
2012-01-24 10:52:53 +09:00
|
|
|
asyncConvertData: function(aFromType, aToType, aListener, aCtxt) {
|
2012-01-26 10:40:08 +09:00
|
|
|
if (!Services.prefs.getBoolPref('extensions.pdf.js.active'))
|
|
|
|
throw NS_ERROR_NOT_IMPLEMENTED;
|
2012-01-24 09:50:45 +09:00
|
|
|
// Store the listener passed to us
|
|
|
|
this.listener = aListener;
|
|
|
|
},
|
2011-10-19 10:20:50 +09:00
|
|
|
|
2012-01-24 09:50:45 +09:00
|
|
|
// nsIStreamListener::onDataAvailable
|
2012-01-24 10:52:53 +09:00
|
|
|
onDataAvailable: function(aRequest, aContext, aInputStream, aOffset, aCount) {
|
2012-01-24 09:50:45 +09:00
|
|
|
// Do nothing since all the data loading is handled by the viewer.
|
2012-01-24 10:52:53 +09:00
|
|
|
log('SANITY CHECK: onDataAvailable SHOULD NOT BE CALLED!');
|
2012-01-24 09:50:45 +09:00
|
|
|
},
|
2011-10-19 10:20:50 +09:00
|
|
|
|
2012-01-24 09:50:45 +09:00
|
|
|
// nsIRequestObserver::onStartRequest
|
2012-01-24 10:52:53 +09:00
|
|
|
onStartRequest: function(aRequest, aContext) {
|
2012-01-24 09:50:45 +09:00
|
|
|
// Setup the request so we can use it below.
|
|
|
|
aRequest.QueryInterface(Ci.nsIChannel);
|
2012-01-25 14:33:03 +09:00
|
|
|
// Cancel the request so the viewer can handle it.
|
|
|
|
aRequest.cancel(Cr.NS_BINDING_ABORTED);
|
|
|
|
|
2012-01-24 09:50:45 +09:00
|
|
|
// Create a new channel that is viewer loaded as a resource.
|
2012-01-24 10:52:53 +09:00
|
|
|
var ioService = Cc['@mozilla.org/network/io-service;1']
|
2012-01-24 09:50:45 +09:00
|
|
|
.getService(Ci.nsIIOService);
|
|
|
|
var channel = ioService.newChannel(
|
|
|
|
'resource://pdf.js/web/viewer.html', null, null);
|
|
|
|
// Keep the URL the same so the browser sees it as the same.
|
|
|
|
channel.originalURI = aRequest.originalURI;
|
|
|
|
channel.asyncOpen(this.listener, aContext);
|
2011-09-07 07:00:24 +09:00
|
|
|
},
|
|
|
|
|
2012-01-24 09:50:45 +09:00
|
|
|
// nsIRequestObserver::onStopRequest
|
2012-01-24 10:52:53 +09:00
|
|
|
onStopRequest: function(aRequest, aContext, aStatusCode) {
|
2012-01-24 09:50:45 +09:00
|
|
|
// Do nothing.
|
|
|
|
return;
|
|
|
|
}
|
2011-09-07 07:00:24 +09:00
|
|
|
};
|
|
|
|
|
|
|
|
var NSGetFactory = XPCOMUtils.generateNSGetFactory([pdfContentHandler]);
|