From 4dd7733b2e1ae4d710329cf151723e502502fa55 Mon Sep 17 00:00:00 2001 From: Sean Lin Date: Fri, 13 Jun 2014 14:19:11 +0800 Subject: [PATCH] Bug 901114 - Convert Alarm API to WebIDL. r=nsm, sr=jst --- dom/alarm/AlarmsManager.js | 38 +++---------------- dom/alarm/AlarmsManager.manifest | 1 - dom/alarm/moz.build | 1 - dom/alarm/nsIDOMAlarmsManager.idl | 15 -------- .../test/test_alarm_non_permitted_app.html | 13 +++++-- dom/alarm/test/test_alarm_permitted_app.html | 17 ++++++--- dom/permission/tests/test_alarms.html | 4 +- .../mochitest/general/test_interfaces.html | 2 + dom/webidl/AlarmsManager.webidl | 17 +++++++++ dom/webidl/moz.build | 1 + 10 files changed, 48 insertions(+), 61 deletions(-) delete mode 100644 dom/alarm/nsIDOMAlarmsManager.idl create mode 100644 dom/webidl/AlarmsManager.webidl diff --git a/dom/alarm/AlarmsManager.js b/dom/alarm/AlarmsManager.js index 56821ddb7b52..23daab1ff0c3 100644 --- a/dom/alarm/AlarmsManager.js +++ b/dom/alarm/AlarmsManager.js @@ -18,11 +18,6 @@ Cu.import("resource://gre/modules/XPCOMUtils.jsm"); Cu.import("resource://gre/modules/Services.jsm"); Cu.import("resource://gre/modules/DOMRequestHelper.jsm"); -const ALARMSMANAGER_CONTRACTID = "@mozilla.org/alarmsManager;1"; -const ALARMSMANAGER_CID = Components.ID("{fea1e884-9b05-11e1-9b64-87a7016c3860}"); -const nsIDOMMozAlarmsManager = Ci.nsIDOMMozAlarmsManager; -const nsIClassInfo = Ci.nsIClassInfo; - function AlarmsManager() { debug("Constructor"); @@ -32,19 +27,14 @@ AlarmsManager.prototype = { __proto__: DOMRequestIpcHelper.prototype, - classID : ALARMSMANAGER_CID, + contractID : "@mozilla.org/alarmsManager;1", - QueryInterface : XPCOMUtils.generateQI([nsIDOMMozAlarmsManager, - Ci.nsIDOMGlobalPropertyInitializer, + classID : Components.ID("{fea1e884-9b05-11e1-9b64-87a7016c3860}"), + + QueryInterface : XPCOMUtils.generateQI([Ci.nsIDOMGlobalPropertyInitializer, Ci.nsISupportsWeakReference, Ci.nsIObserver]), - classInfo : XPCOMUtils.generateCI({ classID: ALARMSMANAGER_CID, - contractID: ALARMSMANAGER_CONTRACTID, - classDescription: "AlarmsManager", - interfaces: [nsIDOMMozAlarmsManager], - flags: nsIClassInfo.DOM_OBJECT }), - add: function add(aDate, aRespectTimezone, aData) { debug("add()"); @@ -155,25 +145,6 @@ AlarmsManager.prototype = { init: function init(aWindow) { debug("init()"); - // Set navigator.mozAlarms to null. - if (!Services.prefs.getBoolPref("dom.mozAlarms.enabled")) { - return null; - } - - // Only pages with perm set can use the alarms. - let principal = aWindow.document.nodePrincipal; - let perm = - Services.perms.testExactPermissionFromPrincipal(principal, "alarms"); - if (perm != Ci.nsIPermissionManager.ALLOW_ACTION) { - return null; - } - - // SystemPrincipal documents do not have any origin. - // Reject them for now. - if (!principal.URI) { - return null; - } - this._cpmm = Cc["@mozilla.org/childprocessmessagemanager;1"] .getService(Ci.nsISyncMessageSender); @@ -186,6 +157,7 @@ AlarmsManager.prototype = { // Get the manifest URL if this is an installed app let appsService = Cc["@mozilla.org/AppsService;1"] .getService(Ci.nsIAppsService); + let principal = aWindow.document.nodePrincipal; this._pageURL = principal.URI.spec; this._manifestURL = appsService.getManifestURLByLocalId(principal.appId); this._window = aWindow; diff --git a/dom/alarm/AlarmsManager.manifest b/dom/alarm/AlarmsManager.manifest index 18cd0293fbfd..2b3087f225ed 100644 --- a/dom/alarm/AlarmsManager.manifest +++ b/dom/alarm/AlarmsManager.manifest @@ -1,3 +1,2 @@ component {fea1e884-9b05-11e1-9b64-87a7016c3860} AlarmsManager.js contract @mozilla.org/alarmsManager;1 {fea1e884-9b05-11e1-9b64-87a7016c3860} -category JavaScript-navigator-property mozAlarms @mozilla.org/alarmsManager;1 diff --git a/dom/alarm/moz.build b/dom/alarm/moz.build index c8e1443d31eb..4bcd64d5d76e 100644 --- a/dom/alarm/moz.build +++ b/dom/alarm/moz.build @@ -8,7 +8,6 @@ TEST_DIRS += ['test'] XPIDL_SOURCES += [ 'nsIAlarmHalService.idl', - 'nsIDOMAlarmsManager.idl', ] XPIDL_MODULE = 'dom_alarm' diff --git a/dom/alarm/nsIDOMAlarmsManager.idl b/dom/alarm/nsIDOMAlarmsManager.idl deleted file mode 100644 index 4ef68691d1ea..000000000000 --- a/dom/alarm/nsIDOMAlarmsManager.idl +++ /dev/null @@ -1,15 +0,0 @@ -/* 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/. */ - -#include "domstubs.idl" - -interface nsIDOMDOMRequest; - -[scriptable, uuid(fea1e884-9b05-11e1-9b64-87a7016c3860)] -interface nsIDOMMozAlarmsManager : nsISupports -{ - nsIDOMDOMRequest getAll(); - nsIDOMDOMRequest add(in jsval date, in DOMString respectTimezone, [optional] in jsval data); - void remove(in unsigned long id); -}; diff --git a/dom/alarm/test/test_alarm_non_permitted_app.html b/dom/alarm/test/test_alarm_non_permitted_app.html index b6c224058004..3ca447bf2af0 100644 --- a/dom/alarm/test/test_alarm_non_permitted_app.html +++ b/dom/alarm/test/test_alarm_non_permitted_app.html @@ -21,12 +21,17 @@ if (SpecialPowers.hasPermission("alarms", document)) { SpecialPowers.pushPrefEnv({"set": [["dom.mozAlarms.enabled", true]]}, function() { SpecialPowers.removePermission("alarms", document); - // mozAlarms is intalled on all platforms except Android for the moment. + // mozAlarms is installed on all platforms except Android for the moment. if (navigator.appVersion.indexOf("Android") != -1) { - ok(!('mozAlarms' in navigator), "navigator.mozAlarms should not exist"); + ok(!('mozAlarms' in navigator), + "navigator.mozAlarms should not exist without permission"); + ok(!('AlarmsManager' in window), + "Interface AlarmsManager should not exist"); } else { - ok('mozAlarms' in navigator, "navigator.mozAlarms should exist"); - is(navigator.mozAlarms, null, "navigator.mozAlarms should return null"); + ok(!('mozAlarms' in navigator), + "navigator.mozAlarms should not exist without permission"); + ok(!('AlarmsManager' in window), + "Interface AlarmsManager should not exist without permission"); } SpecialPowers.addPermission("alarms", true, document); SimpleTest.finish(); diff --git a/dom/alarm/test/test_alarm_permitted_app.html b/dom/alarm/test/test_alarm_permitted_app.html index 77e884e7b556..42caee613247 100644 --- a/dom/alarm/test/test_alarm_permitted_app.html +++ b/dom/alarm/test/test_alarm_permitted_app.html @@ -18,14 +18,21 @@ SimpleTest.waitForExplicitFinish(); SpecialPowers.pushPrefEnv({"set": [["dom.mozAlarms.enabled", true]]}, function() { SpecialPowers.addPermission("alarms", true, document); - // mozAlarms is intalled on all platforms except Android for the moment. + // mozAlarms is installed on all platforms except Android for the moment. if (navigator.appVersion.indexOf("Android") != -1) { - ok(!('mozAlarms' in navigator), "navigator.mozAlarms should not exist"); + ok('AlarmsManager' in window, "Interface AlarmsManager should exist"); + try { + todo('mozAlarms' in navigator, + "mozAlarms is not allowed on Android for now. TODO Bug 863557."); + } catch (e) { + todo(!e, "('mozAlarms' in navigator) should not throw exceptions once " + + "mozAlarms is installed on Android. TODO Bug 863557. " + + "Caught exception: " + e); + } } else { ok('mozAlarms' in navigator, "navigator.mozAlarms should exist"); - ok(navigator.mozAlarms, "navigator.mozAlarms should return an object"); - ok(navigator.mozAlarms instanceof SpecialPowers.Ci.nsIDOMMozAlarmsManager, - "navigator.mozAlarms should be an nsIDOMMozAlarmsManager object"); + ok(navigator.mozAlarms instanceof AlarmsManager, + "navigator.mozAlarms should be an instance of AlarmsManager"); } SimpleTest.finish(); diff --git a/dom/permission/tests/test_alarms.html b/dom/permission/tests/test_alarms.html index d3223a4080c9..284ba874b9a6 100644 --- a/dom/permission/tests/test_alarms.html +++ b/dom/permission/tests/test_alarms.html @@ -20,11 +20,11 @@ var gData = [ { perm: ["alarms"], // AlarmsManager is not enabled on Android yet - // exception on accessing Ci.nsIDOMMozAlarmsManager + // exception on accessing AlarmsManager // See bug 863557 skip: ["Android"], obj: "mozAlarms", - idl: "nsIDOMMozAlarmsManager", + webidl: "AlarmsManager", settings: [["dom.mozAlarms.enabled", true]], }, ] diff --git a/dom/tests/mochitest/general/test_interfaces.html b/dom/tests/mochitest/general/test_interfaces.html index cbd921b77f5e..3d8d39cb00c2 100644 --- a/dom/tests/mochitest/general/test_interfaces.html +++ b/dom/tests/mochitest/general/test_interfaces.html @@ -111,6 +111,8 @@ var legacyMozPrefixedInterfaces = // IMPORTANT: Do not change the list below without review from a DOM peer! var interfaceNamesInGlobalScope = [ +// IMPORTANT: Do not change this list without review from a DOM peer! + {name: "AlarmsManager", pref: "dom.mozAlarms.enabled"}, // IMPORTANT: Do not change this list without review from a DOM peer! "AnalyserNode", // IMPORTANT: Do not change this list without review from a DOM peer! diff --git a/dom/webidl/AlarmsManager.webidl b/dom/webidl/AlarmsManager.webidl new file mode 100644 index 000000000000..eab771302403 --- /dev/null +++ b/dom/webidl/AlarmsManager.webidl @@ -0,0 +1,17 @@ +/* -*- Mode: IDL; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ +/* 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/. + * + * https://wiki.mozilla.org/WebAPI/AlarmAPI + */ + +[NavigatorProperty="mozAlarms", + JSImplementation="@mozilla.org/alarmsManager;1", + CheckPermissions="alarms", + Pref="dom.mozAlarms.enabled"] +interface AlarmsManager { + DOMRequest getAll(); + DOMRequest add(any date, DOMString respectTimezone, optional any data); + void remove(unsigned long id); +}; diff --git a/dom/webidl/moz.build b/dom/webidl/moz.build index a3f3c0148d37..cb65cd605b92 100644 --- a/dom/webidl/moz.build +++ b/dom/webidl/moz.build @@ -17,6 +17,7 @@ PREPROCESSED_WEBIDL_FILES = [ WEBIDL_FILES = [ 'AbstractWorker.webidl', 'ActivityRequestHandler.webidl', + 'AlarmsManager.webidl', 'AnalyserNode.webidl', 'AnimationEvent.webidl', 'AnimationTimeline.webidl',