source: [view]
// a host environment specifically built for Mozilla extensions, but derived
// from the browser host environment
if(typeof window != 'undefined'){
dojo.isBrowser = true;
dojo._name = "browser";
// FIXME: PORTME
// http://developer.mozilla.org/en/mozIJSSubScriptLoader
// attempt to figure out the path to dojo if it isn't set in the config
(function(){
var d = dojo;
// this is a scope protection closure. We set browser versions and grab
// the URL we were loaded from here.
// FIXME: need to probably use a different reference to "document" to get the hosting XUL environment
d.baseUrl = d.config.baseUrl;
// fill in the rendering support information in dojo.render.*
var n = navigator;
var dua = n.userAgent;
var dav = n.appVersion;
var tv = parseFloat(dav);
d.isMozilla = d.isMoz = tv;
if(d.isMoz){
d.isFF = parseFloat(dua.split("Firefox/")[1]) || undefined;
}
// FIXME
d.isQuirks = document.compatMode == "BackCompat";
// FIXME
// TODO: is the HTML LANG attribute relevant?
d.locale = dojo.config.locale || n.language.toLowerCase();
d._xhrObj = function(){
return new XMLHttpRequest();
}
// monkey-patch _loadUri to handle file://, chrome://, and resource:// url's
var oldLoadUri = d._loadUri;
d._loadUri = function(uri, cb){
var handleLocal = ["file:", "chrome:", "resource:"].some(function(prefix){
return String(uri).indexOf(prefix) == 0;
});
if(handleLocal){
// see:
// http://developer.mozilla.org/en/mozIJSSubScriptLoader
var l = Components.classes["@mozilla.org/moz/jssubscript-loader;1"]
.getService(Components.interfaces.mozIJSSubScriptLoader);
var value = l.loadSubScript(uri, d.global)
if(cb){ cb(value); }
return true;
}else{
// otherwise, call the pre-existing version
return oldLoadUri.apply(d, arguments);
}
}
// FIXME: PORTME
d._isDocumentOk = function(http){
var stat = http.status || 0;
return (stat >= 200 && stat < 300) || // Boolean
stat == 304 || // allow any 2XX response code
stat == 1223 || // get it out of the cache
(!stat && (location.protocol=="file:" || location.protocol=="chrome:") );
}
// FIXME: PORTME
// var owloc = window.location+"";
// var base = document.getElementsByTagName("base");
// var hasBase = (base && base.length > 0);
var hasBase = false;
d._getText = function(/*URI*/ uri, /*Boolean*/ fail_ok){
// summary: Read the contents of the specified uri and return those contents.
// uri:
// A relative or absolute uri. If absolute, it still must be in
// the same "domain" as we are.
// fail_ok:
// Default false. If fail_ok and loading fails, return null
// instead of throwing.
// returns: The response text. null is returned when there is a
// failure and failure is okay (an exception otherwise)
// alert("_getText: " + uri);
// NOTE: must be declared before scope switches ie. this._xhrObj()
var http = d._xhrObj();
if(!hasBase && dojo._Url){
uri = (new dojo._Url(uri)).toString();
}
if(d.config.cacheBust){
//Make sure we have a string before string methods are used on uri
uri += "";
uri += (uri.indexOf("?") == -1 ? "?" : "&") + String(d.config.cacheBust).replace(/\W+/g,"");
}
var handleLocal = ["file:", "chrome:", "resource:"].some(function(prefix){
return String(uri).indexOf(prefix) == 0;
});
if(handleLocal){
// see:
// http://forums.mozillazine.org/viewtopic.php?p=921150#921150
var ioService = Components.classes["@mozilla.org/network/io-service;1"]
.getService(Components.interfaces.nsIIOService);
var scriptableStream=Components
.classes["@mozilla.org/scriptableinputstream;1"]
.getService(Components.interfaces.nsIScriptableInputStream);
var channel = ioService.newChannel(uri, null, null);
var input = channel.open();
scriptableStream.init(input);
var str = scriptableStream.read(input.available());
scriptableStream.close();
input.close();
return str;
}else{
http.open('GET', uri, false);
try{
http.send(null);
// alert(http);
if(!d._isDocumentOk(http)){
var err = Error("Unable to load "+uri+" status:"+ http.status);
err.status = http.status;
err.responseText = http.responseText;
throw err;
}
}catch(e){
if(fail_ok){ return null; } // null
// rethrow the exception
throw e;
}
return http.responseText; // String
}