Bug 735947 - support the desktop notifications API in b2g [r=vingtetun]

This commit is contained in:
Fabrice Desré 2012-03-16 15:23:28 -07:00
parent 70fd68c58e
commit a1d19c1db5
6 changed files with 137 additions and 1 deletions

View File

@ -66,7 +66,7 @@ function startupHttpd(baseDir, port) {
function addPermissions(urls) {
let permissions = [
'indexedDB', 'indexedDB-unlimited', 'webapps-manage', 'offline-app',
'content-camera', 'webcontacts-manage', 'wifi-manage'
'content-camera', 'webcontacts-manage', 'wifi-manage', 'desktop-notification'
];
urls.forEach(function(url) {
let uri = Services.io.newURI(url, null, null);
@ -165,6 +165,8 @@ var shell = {
dump('Error loading ' + frameScriptUrl + ' as a frame script: ' + e + '\n');
}
CustomEventManager.init();
let browser = this.contentBrowser;
browser.homePage = homeURL;
browser.goHome();
@ -421,3 +423,53 @@ Services.obs.addObserver(function onConsoleAPILogEvent(subject, topic, data) {
serverSocket.asyncListen(listener);
})();
CustomEventManager = {
init: function custevt_init() {
window.addEventListener("ContentStart", (function(evt) {
content.addEventListener("mozContentEvent", this, false, true);
}).bind(this), false);
},
handleEvent: function custevt_handleEvent(evt) {
let detail = evt.detail;
dump("XXX FIXME : Got a mozContentEvent: " + detail.type);
switch(detail.type) {
case "desktop-notification-click":
case "desktop-notification-close":
AlertsHelper.handleEvent(detail);
break;
}
}
}
AlertsHelper = {
_listeners: {},
_count: 0,
handleEvent: function alert_handleEvent(detail) {
if (!detail || !detail.id)
return;
let listener = this._listeners[detail.id];
let topic = detail.type == "desktop-notification-click" ? "alertclickcallback" : "alertfinished";
listener.observer.observe(null, topic, listener.cookie);
// we're done with this notification
if (topic === "alertfinished")
delete this._listeners[detail.id];
},
registerListener: function alert_registerListener(cookie, alertListener) {
let id = "alert" + this._count++;
this._listeners[id] = { observer: alertListener, cookie: cookie };
return id;
},
showAlertNotification: function alert_showAlertNotification(imageUrl, title, text, textClickable,
cookie, alertListener, name) {
let id = this.registerListener(cookie, alertListener);
shell.sendEvent(content, "mozChromeEvent", { type: "desktop-notification", id: id, icon: imageUrl,
title: title, text: text } );
}
}

View File

@ -0,0 +1,27 @@
/* 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/. */
const Ci = Components.interfaces;
const Cu = Components.utils;
Cu.import("resource://gre/modules/XPCOMUtils.jsm");
Cu.import("resource://gre/modules/Services.jsm");
// -----------------------------------------------------------------------
// Alerts Service
// -----------------------------------------------------------------------
function AlertsService() { }
AlertsService.prototype = {
classID: Components.ID("{5dce03b2-8faa-4b6e-9242-6ddb0411750c}"),
QueryInterface: XPCOMUtils.generateQI([Ci.nsIAlertsService]),
showAlertNotification: function(aImageUrl, aTitle, aText, aTextClickable, aCookie, aAlertListener, aName) {
let browser = Services.wm.getMostRecentWindow("navigator:browser");
browser.AlertsHelper.showAlertNotification(aImageUrl, aTitle, aText, aTextClickable, aCookie, aAlertListener, aName);
}
};
const NSGetFactory = XPCOMUtils.generateNSGetFactory([AlertsService]);

View File

@ -5,3 +5,11 @@ category agent-style-sheets browser-content-stylesheet chrome://browser/content/
component {eff4231b-abce-4f7f-a40a-d646e8fde3ce} CameraContent.js
contract @mozilla.org/b2g-camera-content;1 {eff4231b-abce-4f7f-a40a-d646e8fde3ce}
category JavaScript-navigator-property mozCamera @mozilla.org/b2g-camera-content;1
# AlertsService.js
component {5dce03b2-8faa-4b6e-9242-6ddb0411750c} AlertsService.js
contract @mozilla.org/alerts-service;1 {5dce03b2-8faa-4b6e-9242-6ddb0411750c}
# ContentPermissionPrompt.js
component {8c719f03-afe0-4aac-91ff-6c215895d467} ContentPermissionPrompt.js
contract @mozilla.org/content-permission/prompt;1 {8c719f03-afe0-4aac-91ff-6c215895d467}

View File

@ -0,0 +1,45 @@
/* 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/. */
const Ci = Components.interfaces;
const Cr = Components.results;
const Cu = Components.utils;
const Cc = Components.classes;
Cu.import("resource://gre/modules/XPCOMUtils.jsm");
Cu.import("resource://gre/modules/Services.jsm");
function ContentPermissionPrompt() {}
ContentPermissionPrompt.prototype = {
handleExistingPermission: function handleExistingPermission(request) {
let result = Services.perms.testExactPermission(request.uri, request.type);
if (result == Ci.nsIPermissionManager.ALLOW_ACTION) {
request.allow();
return true;
}
if (result == Ci.nsIPermissionManager.DENY_ACTION) {
request.cancel();
return true;
}
return false;
},
prompt: function(request) {
// returns true if the request was handled
if (this.handleExistingPermission(request))
return;
// TODO : show UI to grant or deny permission
},
classID: Components.ID("{8c719f03-afe0-4aac-91ff-6c215895d467}"),
QueryInterface: XPCOMUtils.generateQI([Ci.nsIContentPermissionPrompt])
};
//module initialization
const NSGetFactory = XPCOMUtils.generateNSGetFactory([ContentPermissionPrompt]);

View File

@ -51,6 +51,8 @@ XPIDLSRCS = \
EXTRA_PP_COMPONENTS = \
B2GComponents.manifest \
CameraContent.js \
AlertsService.js \
ContentPermissionPrompt.js \
$(NULL)
include $(topsrcdir)/config/rules.mk

View File

@ -615,3 +615,5 @@ bin/components/@DLL_PREFIX@nkgnomevfs@DLL_SUFFIX@
@BINPATH@/components/B2GComponents.manifest
@BINPATH@/components/B2GComponents.xpt
@BINPATH@/components/CameraContent.js
@BINPATH@/components/AlertsService.js
@BINPATH@/components/ContentPermissionPrompt.js