2012-01-06 01:04:03 +00:00
|
|
|
/* -*- Mode: Java; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- /
|
|
|
|
/* vim: set shiftwidth=2 tabstop=2 autoindent cindent expandtab: */
|
2012-02-08 12:37:00 +00:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
|
|
|
|
* You can obtain one at http://mozilla.org/MPL/2.0/. */
|
2011-12-06 23:52:26 +00:00
|
|
|
|
|
|
|
const Cc = Components.classes;
|
|
|
|
const Ci = Components.interfaces;
|
|
|
|
const Cu = Components.utils;
|
|
|
|
const CC = Components.Constructor;
|
|
|
|
|
|
|
|
const LocalFile = CC('@mozilla.org/file/local;1',
|
|
|
|
'nsILocalFile',
|
|
|
|
'initWithPath');
|
2011-12-09 01:16:03 +00:00
|
|
|
|
|
|
|
Cu.import('resource://gre/modules/XPCOMUtils.jsm');
|
|
|
|
Cu.import('resource://gre/modules/Services.jsm');
|
2012-02-02 00:46:50 +00:00
|
|
|
|
2011-12-09 01:16:03 +00:00
|
|
|
XPCOMUtils.defineLazyGetter(Services, 'env', function() {
|
|
|
|
return Cc['@mozilla.org/process/environment;1']
|
|
|
|
.getService(Ci.nsIEnvironment);
|
|
|
|
});
|
2012-02-02 00:46:50 +00:00
|
|
|
|
2011-12-09 01:16:03 +00:00
|
|
|
XPCOMUtils.defineLazyGetter(Services, 'ss', function() {
|
|
|
|
return Cc['@mozilla.org/content/style-sheet-service;1']
|
|
|
|
.getService(Ci.nsIStyleSheetService);
|
|
|
|
});
|
2012-02-03 05:20:36 +00:00
|
|
|
XPCOMUtils.defineLazyGetter(Services, 'idle', function() {
|
|
|
|
return Cc['@mozilla.org/widget/idleservice;1']
|
|
|
|
.getService(Ci.nsIIdleService);
|
|
|
|
});
|
2011-12-09 01:16:03 +00:00
|
|
|
|
|
|
|
// In order to use http:// scheme instead of file:// scheme
|
|
|
|
// (that is much more restricted) the following code kick-off
|
2012-01-06 11:35:53 +00:00
|
|
|
// a local http server listening on http://127.0.0.1:7777 and
|
|
|
|
// http://localhost:7777.
|
2011-12-09 01:16:03 +00:00
|
|
|
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));
|
2011-12-14 10:17:51 +00:00
|
|
|
server.registerContentType('appcache', 'text/cache-manifest');
|
2011-12-09 01:16:03 +00:00
|
|
|
server.start(port);
|
|
|
|
}
|
|
|
|
|
2011-12-14 20:02:48 +00:00
|
|
|
// FIXME Bug 707625
|
|
|
|
// until we have a proper security model, add some rights to
|
2012-02-02 00:46:49 +00:00
|
|
|
// the pre-installed web applications
|
2012-02-07 06:48:50 +00:00
|
|
|
// XXX never grant 'content-camera' to non-gaia apps
|
2011-12-14 20:02:48 +00:00
|
|
|
function addPermissions(urls) {
|
2011-12-26 14:07:41 +00:00
|
|
|
let permissions = [
|
2012-02-07 06:48:50 +00:00
|
|
|
'indexedDB', 'indexedDB-unlimited', 'webapps-manage', 'offline-app', 'content-camera'
|
2011-12-26 14:07:41 +00:00
|
|
|
];
|
2011-12-14 20:02:48 +00:00
|
|
|
urls.forEach(function(url) {
|
|
|
|
let uri = Services.io.newURI(url, null, null);
|
|
|
|
let allow = Ci.nsIPermissionManager.ALLOW_ACTION;
|
2012-02-02 00:46:49 +00:00
|
|
|
|
2011-12-14 20:02:48 +00:00
|
|
|
permissions.forEach(function(permission) {
|
|
|
|
Services.perms.add(uri, permission, allow);
|
|
|
|
});
|
|
|
|
});
|
2011-12-09 01:16:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-12-06 23:52:26 +00:00
|
|
|
var shell = {
|
2012-01-06 01:04:03 +00:00
|
|
|
// FIXME/bug 678695: this should be a system setting
|
|
|
|
preferredScreenBrightness: 1.0,
|
2012-02-08 20:43:49 +00:00
|
|
|
|
|
|
|
isDebug: false,
|
2012-01-06 01:04:03 +00:00
|
|
|
|
2012-02-02 00:46:50 +00:00
|
|
|
get contentBrowser() {
|
|
|
|
delete this.contentBrowser;
|
|
|
|
return this.contentBrowser = document.getElementById('homescreen');
|
2011-12-06 23:52:26 +00:00
|
|
|
},
|
|
|
|
|
2011-12-09 01:16:03 +00:00
|
|
|
get homeURL() {
|
2011-12-06 23:52:26 +00:00
|
|
|
try {
|
2011-12-09 01:16:03 +00:00
|
|
|
let homeSrc = Services.env.get('B2G_HOMESCREEN');
|
2011-12-06 23:52:26 +00:00
|
|
|
if (homeSrc)
|
|
|
|
return homeSrc;
|
|
|
|
} catch (e) {}
|
|
|
|
|
|
|
|
let urls = Services.prefs.getCharPref('browser.homescreenURL').split(',');
|
|
|
|
for (let i = 0; i < urls.length; i++) {
|
|
|
|
let url = urls[i];
|
|
|
|
if (url.substring(0, 7) != 'file://')
|
|
|
|
return url;
|
|
|
|
|
|
|
|
let file = new LocalFile(url.substring(7, url.length));
|
|
|
|
if (file.exists())
|
|
|
|
return url;
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
},
|
|
|
|
|
|
|
|
start: function shell_init() {
|
2011-12-09 01:16:03 +00:00
|
|
|
let homeURL = this.homeURL;
|
|
|
|
if (!homeURL) {
|
|
|
|
let msg = 'Fatal error during startup: [No homescreen found]';
|
|
|
|
return alert(msg);
|
|
|
|
}
|
|
|
|
|
2011-12-06 23:52:26 +00:00
|
|
|
window.controllers.appendController(this);
|
|
|
|
window.addEventListener('keypress', this);
|
2011-12-14 10:17:51 +00:00
|
|
|
window.addEventListener('MozApplicationManifest', this);
|
2012-02-08 05:22:34 +00:00
|
|
|
window.addEventListener("AppCommand", this);
|
2012-02-02 00:46:50 +00:00
|
|
|
this.contentBrowser.addEventListener('load', this, true);
|
2011-12-06 23:52:26 +00:00
|
|
|
|
2011-12-09 01:16:03 +00:00
|
|
|
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('/');
|
|
|
|
|
2012-01-06 18:11:56 +00:00
|
|
|
const SERVER_PORT = 6666;
|
2011-12-09 01:16:03 +00:00
|
|
|
startupHttpd(baseDir, SERVER_PORT);
|
|
|
|
|
|
|
|
let baseHost = 'http://localhost';
|
|
|
|
homeURL = homeURL.replace(baseDir, baseHost + ':' + SERVER_PORT);
|
|
|
|
}
|
2011-12-14 20:02:48 +00:00
|
|
|
addPermissions([homeURL]);
|
2011-12-09 01:16:03 +00:00
|
|
|
} catch (e) {
|
|
|
|
let msg = 'Fatal error during startup: [' + e + '[' + homeURL + ']';
|
|
|
|
return alert(msg);
|
|
|
|
}
|
2011-12-06 23:52:26 +00:00
|
|
|
|
2012-02-02 00:46:49 +00:00
|
|
|
// Load webapi+apps.js as a frame script
|
|
|
|
let frameScriptUrl = 'chrome://browser/content/webapi.js';
|
|
|
|
try {
|
|
|
|
messageManager.loadFrameScript(frameScriptUrl, true);
|
|
|
|
} catch (e) {
|
|
|
|
dump('Error when loading ' + frameScriptUrl + ' as a frame script: ' + e + '\n');
|
|
|
|
}
|
|
|
|
|
2012-02-02 00:46:50 +00:00
|
|
|
let browser = this.contentBrowser;
|
2011-12-09 01:16:03 +00:00
|
|
|
browser.homePage = homeURL;
|
2011-12-06 23:52:26 +00:00
|
|
|
browser.goHome();
|
|
|
|
},
|
|
|
|
|
|
|
|
stop: function shell_stop() {
|
|
|
|
window.controllers.removeController(this);
|
|
|
|
window.removeEventListener('keypress', this);
|
2011-12-14 10:17:51 +00:00
|
|
|
window.removeEventListener('MozApplicationManifest', this);
|
2012-02-08 20:43:49 +00:00
|
|
|
window.removeEventListener('AppCommand', this);
|
2011-12-06 23:52:26 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
supportsCommand: function shell_supportsCommand(cmd) {
|
|
|
|
let isSupported = false;
|
|
|
|
switch (cmd) {
|
|
|
|
case 'cmd_close':
|
|
|
|
isSupported = true;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
isSupported = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return isSupported;
|
|
|
|
},
|
|
|
|
|
|
|
|
isCommandEnabled: function shell_isCommandEnabled(cmd) {
|
|
|
|
return true;
|
|
|
|
},
|
|
|
|
|
|
|
|
doCommand: function shell_doCommand(cmd) {
|
|
|
|
switch (cmd) {
|
|
|
|
case 'cmd_close':
|
2012-02-02 00:46:50 +00:00
|
|
|
content.postMessage('appclose', '*');
|
2011-12-06 23:52:26 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2012-02-08 20:43:49 +00:00
|
|
|
toggleDebug: function shell_toggleDebug() {
|
|
|
|
this.isDebug = !this.isDebug;
|
|
|
|
|
|
|
|
if (this.isDebug) {
|
|
|
|
Services.prefs.setBoolPref("layers.acceleration.draw-fps", true);
|
|
|
|
Services.prefs.setBoolPref("nglayout.debug.paint_flashing", true);
|
|
|
|
} else {
|
|
|
|
Services.prefs.setBoolPref("layers.acceleration.draw-fps", false);
|
|
|
|
Services.prefs.setBoolPref("nglayout.debug.paint_flashing", false);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2011-12-06 23:52:26 +00:00
|
|
|
handleEvent: function shell_handleEvent(evt) {
|
|
|
|
switch (evt.type) {
|
|
|
|
case 'keypress':
|
|
|
|
switch (evt.keyCode) {
|
|
|
|
case evt.DOM_VK_HOME:
|
2012-02-02 00:46:50 +00:00
|
|
|
this.sendEvent(content, 'home');
|
2011-12-06 23:52:26 +00:00
|
|
|
break;
|
|
|
|
case evt.DOM_VK_SLEEP:
|
2012-01-06 01:04:03 +00:00
|
|
|
this.toggleScreen();
|
2012-01-18 16:48:54 +00:00
|
|
|
|
|
|
|
let details = {
|
|
|
|
'enabled': screen.mozEnabled
|
|
|
|
};
|
2012-02-02 00:46:50 +00:00
|
|
|
this.sendEvent(content, 'sleep', details);
|
2011-12-06 23:52:26 +00:00
|
|
|
break;
|
|
|
|
case evt.DOM_VK_ESCAPE:
|
2011-12-08 09:21:32 +00:00
|
|
|
if (evt.defaultPrevented)
|
2011-12-06 23:52:26 +00:00
|
|
|
return;
|
|
|
|
this.doCommand('cmd_close');
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
2012-02-08 05:22:34 +00:00
|
|
|
case 'AppCommand':
|
|
|
|
switch (evt.command) {
|
|
|
|
case 'Menu':
|
|
|
|
this.sendEvent(content, 'menu');
|
|
|
|
break;
|
2012-02-08 20:43:49 +00:00
|
|
|
case 'Search':
|
|
|
|
this.toggleDebug();
|
|
|
|
break;
|
2012-02-08 05:22:34 +00:00
|
|
|
}
|
|
|
|
break;
|
2011-12-06 23:52:26 +00:00
|
|
|
case 'load':
|
2012-02-02 00:46:50 +00:00
|
|
|
this.contentBrowser.removeEventListener('load', this, true);
|
2012-01-06 01:04:03 +00:00
|
|
|
this.turnScreenOn();
|
2012-02-02 00:46:49 +00:00
|
|
|
|
|
|
|
let chromeWindow = window.QueryInterface(Ci.nsIDOMChromeWindow);
|
|
|
|
chromeWindow.browserDOMWindow = new nsBrowserAccess();
|
|
|
|
|
2011-12-06 23:52:26 +00:00
|
|
|
this.sendEvent(window, 'ContentStart');
|
|
|
|
break;
|
2011-12-14 10:17:51 +00:00
|
|
|
case 'MozApplicationManifest':
|
|
|
|
try {
|
2011-12-14 20:02:48 +00:00
|
|
|
if (!Services.prefs.getBoolPref('browser.cache.offline.enable'))
|
|
|
|
return;
|
|
|
|
|
2011-12-14 10:17:51 +00:00
|
|
|
let contentWindow = evt.originalTarget.defaultView;
|
|
|
|
let documentElement = contentWindow.document.documentElement;
|
|
|
|
if (!documentElement)
|
|
|
|
return;
|
|
|
|
|
2012-02-02 00:46:49 +00:00
|
|
|
let manifest = documentElement.getAttribute('manifest');
|
2011-12-14 10:17:51 +00:00
|
|
|
if (!manifest)
|
|
|
|
return;
|
|
|
|
|
|
|
|
let documentURI = contentWindow.document.documentURIObject;
|
2011-12-14 20:02:48 +00:00
|
|
|
if (!Services.perms.testPermission(documentURI, 'offline-app')) {
|
|
|
|
if (Services.prefs.getBoolPref('browser.offline-apps.notify')) {
|
|
|
|
// FIXME Bug 710729 - Add a UI for offline cache notifications
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2011-12-14 10:17:51 +00:00
|
|
|
Services.perms.add(documentURI, 'offline-app',
|
|
|
|
Ci.nsIPermissionManager.ALLOW_ACTION);
|
|
|
|
|
2011-12-14 20:02:48 +00:00
|
|
|
let manifestURI = Services.io.newURI(manifest, null, documentURI);
|
2011-12-14 10:17:51 +00:00
|
|
|
let updateService = Cc['@mozilla.org/offlinecacheupdate-service;1']
|
|
|
|
.getService(Ci.nsIOfflineCacheUpdateService);
|
|
|
|
updateService.scheduleUpdate(manifestURI, documentURI, window);
|
|
|
|
} catch (e) {
|
|
|
|
dump('Error while creating offline cache: ' + e + '\n');
|
|
|
|
}
|
|
|
|
break;
|
2011-12-06 23:52:26 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
sendEvent: function shell_sendEvent(content, type, details) {
|
|
|
|
let event = content.document.createEvent('CustomEvent');
|
|
|
|
event.initCustomEvent(type, true, true, details ? details : {});
|
|
|
|
content.dispatchEvent(event);
|
2012-01-06 01:04:03 +00:00
|
|
|
},
|
|
|
|
toggleScreen: function shell_toggleScreen() {
|
|
|
|
if (screen.mozEnabled)
|
|
|
|
this.turnScreenOff();
|
|
|
|
else
|
|
|
|
this.turnScreenOn();
|
|
|
|
},
|
|
|
|
turnScreenOff: function shell_turnScreenOff() {
|
|
|
|
screen.mozEnabled = false;
|
|
|
|
screen.mozBrightness = 0.0;
|
|
|
|
},
|
|
|
|
turnScreenOn: function shell_turnScreenOn() {
|
|
|
|
screen.mozEnabled = true;
|
|
|
|
screen.mozBrightness = this.preferredScreenBrightness;
|
2012-02-02 00:46:49 +00:00
|
|
|
}
|
2011-12-06 23:52:26 +00:00
|
|
|
};
|
|
|
|
|
2012-02-03 05:20:36 +00:00
|
|
|
(function PowerManager() {
|
|
|
|
let idleHandler = {
|
|
|
|
observe: function(subject, topic, time) {
|
|
|
|
if (topic === "idle") {
|
|
|
|
// TODO: Check wakelock status. See bug 697132.
|
|
|
|
shell.turnScreenOff();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
}
|
|
|
|
let idleTimeout = Services.prefs.getIntPref("power.screen.timeout");
|
|
|
|
if (idleTimeout) {
|
|
|
|
Services.idle.addIdleObserver(idleHandler, idleTimeout);
|
|
|
|
}
|
|
|
|
})();
|
2012-02-02 00:46:49 +00:00
|
|
|
|
|
|
|
function nsBrowserAccess() {
|
|
|
|
}
|
|
|
|
|
|
|
|
nsBrowserAccess.prototype = {
|
|
|
|
QueryInterface: XPCOMUtils.generateQI([Ci.nsIBrowserDOMWindow]),
|
|
|
|
|
|
|
|
openURI: function openURI(uri, opener, where, context) {
|
|
|
|
// TODO This should be replaced by an 'open-browser-window' intent
|
|
|
|
let contentWindow = content.wrappedJSObject;
|
|
|
|
if (!('getApplicationManager' in contentWindow))
|
|
|
|
return null;
|
|
|
|
|
|
|
|
let applicationManager = contentWindow.getApplicationManager();
|
|
|
|
if (!applicationManager)
|
|
|
|
return null;
|
|
|
|
|
|
|
|
let url = uri ? uri.spec : 'about:blank';
|
|
|
|
let window = applicationManager.launch(url, where);
|
|
|
|
return window.contentWindow;
|
|
|
|
},
|
|
|
|
|
|
|
|
openURIInFrame: function openURIInFrame(uri, opener, where, context) {
|
|
|
|
throw new Error('Not Implemented');
|
|
|
|
},
|
|
|
|
|
|
|
|
isTabContentWindow: function isTabContentWindow(contentWindow) {
|
|
|
|
return contentWindow == window;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2012-02-02 19:11:42 +00:00
|
|
|
// Pipe `console` log messages to the nsIConsoleService which writes them
|
|
|
|
// to logcat.
|
|
|
|
Services.obs.addObserver(function onConsoleAPILogEvent(subject, topic, data) {
|
|
|
|
let message = subject.wrappedJSObject;
|
|
|
|
let prefix = "Content JS " + message.level.toUpperCase() +
|
|
|
|
" at " + message.filename + ":" + message.lineNumber +
|
|
|
|
" in " + (message.functionName || "anonymous") + ": ";
|
|
|
|
Services.console.logStringMessage(prefix + Array.join(message.arguments, " "));
|
|
|
|
}, "console-api-log-event", false);
|