Files
WebODF/programs/firefoxextension/components/odfContentHandler.js
T

197 lines
6.4 KiB
JavaScript
Raw Normal View History

2011-10-19 21:00:43 +02:00
/*jslint bitwise: true*/
/*global Components: true, dump: true, Uint8Array: true, Services: true,
XPCOMUtils: true*/
var Cc = Components.classes;
var Ci = Components.interfaces;
var Cr = Components.results;
var Cu = Components.utils;
2011-10-16 21:35:34 +02:00
2011-10-19 21:00:43 +02:00
var ODF_CONTENT_TYPE_PREFIX = 'application/vnd.oasis.opendocument.';
2011-12-05 16:39:05 +01:00
var NS_ERROR_WONT_HANDLE_CONTENT = 0x805d0001;
2011-10-16 21:35:34 +02:00
2011-10-19 21:00:43 +02:00
Cu["import"]('resource://gre/modules/XPCOMUtils.jsm');
Cu["import"]('resource://gre/modules/Services.jsm');
2011-10-16 21:35:34 +02:00
function log(aMsg) {
2011-10-19 21:00:43 +02:00
"use strict";
var msg = 'odfContentHandler.js: ' + (aMsg.join ? aMsg.join('') : aMsg);
Cc['@mozilla.org/consoleservice;1'].getService(Ci.nsIConsoleService)
.logStringMessage(msg);
dump(msg + '\n');
2011-10-16 21:35:34 +02:00
}
function fireEventTo(aName, aData, aWindow) {
2011-10-19 21:00:43 +02:00
"use strict";
var mywindow = aWindow.wrappedJSObject,
evt = mywindow.document.createEvent('CustomEvent');
2011-10-19 21:00:43 +02:00
evt.initCustomEvent('odf' + aName, false, false, aData);
mywindow.document.dispatchEvent(evt);
2011-10-16 21:35:34 +02:00
}
function loadDocument(aWindow, aDocumentUrl) {
2011-10-19 21:00:43 +02:00
"use strict";
var xhr = Cc['@mozilla.org/xmlextras/xmlhttprequest;1']
2011-10-16 21:35:34 +02:00
.createInstance(Ci.nsIXMLHttpRequest);
2011-10-19 21:00:43 +02:00
xhr.onprogress = function updateProgress(evt) {
if (evt.lengthComputable) {
fireEventTo(evt.type, evt.loaded / evt.total, aWindow);
}
};
2011-10-16 21:35:34 +02:00
2011-10-19 21:00:43 +02:00
xhr.onerror = function error(evt) {
fireEventTo(evt.type, false, aWindow);
};
2011-10-16 21:35:34 +02:00
2011-10-19 21:00:43 +02:00
xhr.onload = function load(evt) {
var data = (xhr.mozResponseArrayBuffer || xhr.mozResponse ||
xhr.responseArrayBuffer || xhr.response),
view,
mywindow,
2011-10-19 21:00:43 +02:00
arrayBuffer,
view2,
array,
i;
try {
view = new Uint8Array(data);
mywindow = aWindow.wrappedJSObject;
arrayBuffer = new mywindow.ArrayBuffer(data.byteLength);
view2 = new mywindow.Uint8Array(arrayBuffer);
2011-10-19 21:00:43 +02:00
view2.set(view);
array = [];
array.length = view2.byteLength;
for (i = 0; i < view2.byteLength; i += 1) {
array[i] = view2[i];
}
fireEventTo(evt.type, array, aWindow);
} catch (e) {
log('Error - ' + e);
}
};
2011-10-16 21:35:34 +02:00
2011-10-19 21:00:43 +02:00
xhr.open('GET', aDocumentUrl);
xhr.responseType = 'arraybuffer';
xhr.send(null);
2011-10-16 21:35:34 +02:00
}
2011-10-19 21:00:43 +02:00
var WebProgressListener = {
init: function WebProgressListenerInit(aWindow, aUrl) {
"use strict";
this.locationHasChanged = false;
this.documentUrl = aUrl;
2011-10-16 21:35:34 +02:00
2011-10-19 21:00:43 +02:00
var flags = Ci.nsIWebProgress.NOTIFY_LOCATION |
2011-10-16 21:35:34 +02:00
Ci.nsIWebProgress.NOTIFY_STATE_NETWORK |
2011-10-19 21:00:43 +02:00
Ci.nsIWebProgress.NOTIFY_STATE_DOCUMENT,
docShell = aWindow.QueryInterface(Ci.nsIInterfaceRequestor)
2011-10-16 21:35:34 +02:00
.getInterface(Ci.nsIWebNavigation)
2011-10-19 21:00:43 +02:00
.QueryInterface(Ci.nsIDocShell),
webProgress = docShell.QueryInterface(Ci.nsIInterfaceRequestor)
2011-10-16 21:35:34 +02:00
.getInterface(Ci.nsIWebProgress);
2011-10-19 21:00:43 +02:00
try {
webProgress.removeProgressListener(this);
} catch (e) {
}
webProgress.addProgressListener(this, flags);
},
2011-10-16 21:35:34 +02:00
2011-10-19 21:00:43 +02:00
onStateChange: function onStateChange(aWebProgress, aRequest, aStateFlags,
2011-10-16 21:35:34 +02:00
aStatus) {
2011-10-19 21:00:43 +02:00
"use strict";
var complete = Ci.nsIWebProgressListener.STATE_IS_WINDOW +
2011-10-16 21:35:34 +02:00
Ci.nsIWebProgressListener.STATE_STOP;
2011-10-19 21:00:43 +02:00
if ((aStateFlags & complete) === complete && this.locationHasChanged) {
aWebProgress.removeProgressListener(this);
loadDocument(aWebProgress.DOMWindow, this.documentUrl);
}
},
2011-10-16 21:35:34 +02:00
2011-10-19 21:00:43 +02:00
onProgressChange: function onProgressChange(aWebProgress, aRequest,
aCurSelf, aMaxSelf, aCurTotal,
aMaxTotal) {
"use strict";
},
2011-10-16 21:35:34 +02:00
2011-10-19 21:00:43 +02:00
onLocationChange: function onLocationChange(aWebProgress, aRequest,
2011-10-16 21:35:34 +02:00
aLocationURI) {
2011-10-19 21:00:43 +02:00
"use strict";
this.locationHasChanged = true;
},
2011-10-16 21:35:34 +02:00
2011-10-19 21:00:43 +02:00
onStatusChange: function onStatusChange(aWebProgress, aRequest, aStatus,
2011-10-16 21:35:34 +02:00
aMessage) {
2011-10-19 21:00:43 +02:00
"use strict";
},
2011-10-16 21:35:34 +02:00
2011-10-19 21:00:43 +02:00
onSecurityChange: function onSecurityChange(aWebProgress, aRequest, aState) {
"use strict";
},
2011-10-16 21:35:34 +02:00
2011-10-19 21:00:43 +02:00
QueryInterface: function QueryInterface(aIID) {
"use strict";
if (aIID.equals(Ci.nsIWebProgressListener) ||
aIID.equals(Ci.nsISupportsWeakReference) ||
aIID.equals(Ci.nsISupports)) {
return this;
}
throw Components.results.NS_ERROR_NO_INTERFACE;
2011-10-16 21:35:34 +02:00
}
};
function odfContentHandler() {
2011-10-19 21:00:43 +02:00
"use strict";
2011-10-16 21:35:34 +02:00
}
odfContentHandler.prototype = {
2011-10-19 21:00:43 +02:00
handleContent: function handleContent(aMimetype, aContext, aRequest) {
"use strict";
2011-12-05 16:39:05 +01:00
2011-10-19 21:00:43 +02:00
if (!(aMimetype.indexOf(ODF_CONTENT_TYPE_PREFIX) === 0 ||
aMimetype === "application/octet-stream")) {
2011-12-05 16:39:05 +01:00
throw NS_ERROR_WONT_HANDLE_CONTENT;
2011-10-19 21:00:43 +02:00
}
2011-10-16 21:35:34 +02:00
2011-10-19 21:00:43 +02:00
if (!(aRequest instanceof Ci.nsIChannel)) {
2011-12-05 16:39:05 +01:00
throw NS_ERROR_WONT_HANDLE_CONTENT;
2011-10-19 21:00:43 +02:00
}
2011-10-16 21:35:34 +02:00
var mywindow = null,
2011-12-05 16:39:05 +01:00
callbacks,
2011-10-19 21:00:43 +02:00
uri = aRequest.URI,
2011-12-05 16:39:05 +01:00
targetUrl = uri.spec,
tail = targetUrl.substring(targetUrl.length-9),
2011-10-19 21:00:43 +02:00
url;
2011-12-05 16:39:05 +01:00
// if the url ends with a download parameter, then do not handle it
if (tail === "#download") {
throw NS_ERROR_WONT_HANDLE_CONTENT;
}
callbacks = aRequest.notificationCallbacks ||
aRequest.loadGroup.notificationCallbacks;
2011-10-19 21:00:43 +02:00
if (!callbacks) {
return;
}
2011-10-16 21:35:34 +02:00
mywindow = callbacks.getInterface(Ci.nsIDOMWindow);
2011-12-05 16:39:05 +01:00
WebProgressListener.init(mywindow, uri.spec);
2011-10-16 21:35:34 +02:00
2011-10-19 21:00:43 +02:00
try {
url = Services.prefs.getCharPref('extensions.webodf.js.url');
2011-12-05 16:39:05 +01:00
//url = url.replace('%s', encodeURIComponent(targetUrl));
url = url.replace('%s', targetUrl);
2011-10-19 21:00:43 +02:00
} catch (e) {
log('Error retrieving the webodf base url - ' + e);
2011-12-05 16:39:05 +01:00
throw NS_ERROR_WONT_HANDLE_CONTENT;
2011-10-19 21:00:43 +02:00
}
2011-12-05 16:39:05 +01:00
aRequest.cancel(Cr.NS_BINDING_ABORTED);
mywindow.location = url;
2011-10-19 21:00:43 +02:00
},
2011-10-16 21:35:34 +02:00
2011-10-19 21:00:43 +02:00
classID: Components.ID('{afe5fa21-709d-4916-b51c-56f60d574a0a}'),
QueryInterface: XPCOMUtils.generateQI([Ci.nsIContentHandler])
2011-10-16 21:35:34 +02:00
};
var NSGetFactory = XPCOMUtils.generateNSGetFactory([odfContentHandler]);