Bug 708538 - Add a local http server on the device at http://localhost:8888. r=cjones

--HG--
rename : netwerk/test/httpserver/httpd.js => b2g/chrome/content/httpd.js
This commit is contained in:
Vivien Nicolas 2011-12-09 09:16:03 +08:00
Родитель 5a2435c113
Коммит a38ad5a40c
3 изменённых файлов: 5282 добавлений и 14 удалений

5211
b2g/chrome/content/httpd.js Normal file

Разница между файлами не показана из-за своего большого размера Загрузить разницу

Просмотреть файл

@ -39,22 +39,55 @@ const Ci = Components.interfaces;
const Cu = Components.utils;
const CC = Components.Constructor;
Cu.import('resource://gre/modules/Services.jsm');
const LocalFile = CC('@mozilla.org/file/local;1',
'nsILocalFile',
'initWithPath');
Cu.import('resource://gre/modules/XPCOMUtils.jsm');
Cu.import('resource://gre/modules/Services.jsm');
XPCOMUtils.defineLazyGetter(Services, 'env', function() {
return Cc['@mozilla.org/process/environment;1']
.getService(Ci.nsIEnvironment);
});
XPCOMUtils.defineLazyGetter(Services, 'ss', function() {
return Cc['@mozilla.org/content/style-sheet-service;1']
.getService(Ci.nsIStyleSheetService);
});
XPCOMUtils.defineLazyGetter(Services, 'fm', function() {
return Cc['@mozilla.org/focus-manager;1']
.getService(Ci.nsIFocusManager);
});
// In order to use http:// scheme instead of file:// scheme
// (that is much more restricted) the following code kick-off
// a local http server listening on http://127.0.0.1:8888 and
// http://localhost:8888.
function startupHttpd(baseDir, port) {
const httpdURL = 'chrome://browser/content/httpd.js';
let httpd = {};
Services.scriptloader.loadSubScript(httpdURL, httpd);
let server = new httpd.nsHttpServer();
server.registerDirectory('/', new LocalFile(baseDir));
server.start(port);
}
// XXX until we have a security model, just let the pre-installed
// app used indexedDB.
function allowIndexedDB(url) {
let uri = Services.io.newURI(url, null, null);
Services.perms.add(uri, 'indexedDB', Ci.nsIPermissionManager.ALLOW_ACTION);
}
var shell = {
get home() {
delete this.home;
return this.home = document.getElementById('homescreen');
},
get homeSrc() {
get homeURL() {
try {
let homeSrc = Cc['@mozilla.org/process/environment;1']
.getService(Ci.nsIEnvironment)
.get('B2G_HOMESCREEN');
let homeSrc = Services.env.get('B2G_HOMESCREEN');
if (homeSrc)
return homeSrc;
} catch (e) {}
@ -73,16 +106,41 @@ var shell = {
},
start: function shell_init() {
let homeURL = this.homeURL;
if (!homeURL) {
let msg = 'Fatal error during startup: [No homescreen found]';
return alert(msg);
}
window.controllers.appendController(this);
window.addEventListener('keypress', this);
this.home.addEventListener('load', this, true);
let ioService = Cc['@mozilla.org/network/io-service;1']
.getService(Ci.nsIIOService2);
ioService.offline = false;
try {
Services.io.offline = false;
let fileScheme = 'file://';
if (homeURL.substring(0, fileScheme.length) == fileScheme) {
homeURL = homeURL.replace(fileScheme, '');
let baseDir = homeURL.split('/');
baseDir.pop();
baseDir = baseDir.join('/');
const SERVER_PORT = 8888;
startupHttpd(baseDir, SERVER_PORT);
let baseHost = 'http://localhost';
homeURL = homeURL.replace(baseDir, baseHost + ':' + SERVER_PORT);
}
allowIndexedDB(homeURL);
} catch (e) {
let msg = 'Fatal error during startup: [' + e + '[' + homeURL + ']';
return alert(msg);
}
let browser = this.home;
browser.homePage = this.homeSrc;
browser.homePage = homeURL;
browser.goHome();
},
@ -183,9 +241,7 @@ var shell = {
let shouldOpen = parseInt(data);
if (shouldOpen && !isKeyboardOpened) {
activeElement = Cc['@mozilla.org/focus-manager;1']
.getService(Ci.nsIFocusManager)
.focusedElement;
activeElement = Services.fm.focusedElement;
if (!activeElement)
return;
@ -198,7 +254,7 @@ var shell = {
}
};
Services.obs.addObserver(constructor, "ime-enabled-state-changed", false);
Services.obs.addObserver(constructor, 'ime-enabled-state-changed', false);
['ContentStart', 'keypress', 'mousedown'].forEach(function vkm_events(type) {
window.addEventListener(type, constructor, true);
});

Просмотреть файл

@ -8,6 +8,7 @@ chrome.jar:
content/shell.js (content/shell.js)
content/touch.js (content/touch.js)
content/commandUtil.js (content/commandUtil.js)
content/httpd.js (content/httpd.js)
% override chrome://global/content/netError.xhtml chrome://browser/content/netError.xhtml
content/netError.xhtml (content/netError.xhtml)