Bug 859601 - mozContacts API - return undefined if the API is unsupported on the platform, not null. r=bent

This commit is contained in:
Gregor Wagner 2013-04-10 11:15:51 -07:00
parent d2afb6f364
commit 6331890d3c
9 changed files with 86 additions and 10 deletions

View File

@ -351,6 +351,8 @@
@BINPATH@/components/BrowserElementParent.js
@BINPATH@/components/ContactManager.js
@BINPATH@/components/ContactManager.manifest
@BINPATH@/components/NavigatorPropertyHelper.js
@BINPATH@/components/NavigatorPropertyHelper.manifest
@BINPATH@/components/PermissionSettings.js
@BINPATH@/components/PermissionSettings.manifest
@BINPATH@/components/PermissionPromptService.js

View File

@ -514,6 +514,8 @@
@BINPATH@/components/PermissionSettings.manifest
@BINPATH@/components/ContactManager.js
@BINPATH@/components/ContactManager.manifest
@BINPATH@/components/NavigatorPropteryHelper.js
@BINPATH@/components/NavigatorPropertyHelper.manifest
@BINPATH@/components/AlarmsManager.js
@BINPATH@/components/AlarmsManager.manifest
@BINPATH@/components/TCPSocket.js

View File

@ -21,6 +21,8 @@ EXTRA_COMPONENTS = \
SiteSpecificUserAgent.manifest \
ConsoleAPI.js \
ConsoleAPI.manifest \
NavigatorPropertyHelper.js \
NavigatorPropertyHelper.manifest \
$(NULL)
EXTRA_JS_MODULES = ConsoleAPIStorage.jsm \

View File

@ -0,0 +1,66 @@
/* 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/. */
"use strict";
const DEBUG = false;
function debug(s) { dump("-*- NavigatorPropertyHelper: " + s + "\n"); }
const Ci = Components.interfaces;
const Cu = Components.utils;
Cu.import("resource://gre/modules/XPCOMUtils.jsm");
Cu.import("resource://gre/modules/Services.jsm");
function NavigatorPropertyHelper() { };
NavigatorPropertyHelper.prototype = {
classID : Components.ID("{f0d03420-a0ce-11e2-9e96-0800200c9a66}"),
QueryInterface : XPCOMUtils.generateQI([Ci.nsIObserver, Ci.nsISupportsWeakReference]),
observe: function observe(subject, topic, data) {
if (DEBUG) debug("topic: " + topic + ", data: " + data);
let cm = Components.classes["@mozilla.org/categorymanager;1"].getService(Ci.nsICategoryManager);
switch (topic) {
case 'app-startup':
let enumerator = cm.enumerateCategory("JavaScript-navigator-property-maybe");
while (enumerator.hasMoreElements()) {
let entry = enumerator.getNext().QueryInterface(Ci.nsISupportsCString);
let keyVal = cm.getCategoryEntry("JavaScript-navigator-property-maybe", entry).split(',');
try {
if (Services.prefs.getBoolPref(entry)) {
if (DEBUG) debug("enable: " + keyVal[0]);
cm.addCategoryEntry("JavaScript-navigator-property", keyVal[0], keyVal[1], false, false);
}
} catch (ex) {
if (DEBUG) debug("no pref found: " + entry);
}
Services.prefs.addObserver(entry, this, true);
}
break;
case 'nsPref:changed':
let keyVal = cm.getCategoryEntry("JavaScript-navigator-property-maybe", data).split(',');
let key = keyVal[0];
let val = keyVal[1];
try {
if (Services.prefs.getBoolPref(data)) {
if (DEBUG) debug("enable: " + key);
cm.addCategoryEntry("JavaScript-navigator-property", key, val, false, false);
} else {
if (DEBUG) debug("disable: " + key);
cm.deleteCategoryEntry("JavaScript-navigator-property", key, false);
}
} catch (ex) {
if (DEBUG) debug("no pref found: " + data);
cm.deleteCategoryEntry("JavaScript-navigator-property", key, false);
}
break;
default:
if (DEBUG) debug("unknown topic: " + topic);
}
},
}
this.NSGetFactory = XPCOMUtils.generateNSGetFactory([NavigatorPropertyHelper])

View File

@ -0,0 +1,3 @@
component {f0d03420-a0ce-11e2-9e96-0800200c9a66} NavigatorPropertyHelper.js
contract @mozilla.org/navigatorPropertyHelper;1 {f0d03420-a0ce-11e2-9e96-0800200c9a66}
category app-startup NavigatorPropertyHelper service,@mozilla.org/navigatorPropertyHelper;1

View File

@ -700,10 +700,6 @@ ContactManager.prototype = {
},
init: function(aWindow) {
// Set navigator.mozContacts to null.
if (!Services.prefs.getBoolPref("dom.mozContacts.enabled"))
return null;
this.initHelper(aWindow, ["Contacts:Find:Return:OK", "Contacts:Find:Return:KO",
"Contacts:Clear:Return:OK", "Contacts:Clear:Return:KO",
"Contact:Save:Return:OK", "Contact:Save:Return:KO",

View File

@ -22,4 +22,4 @@ category JavaScript-global-constructor mozContact @mozilla.org/contact;1
component {1d70322b-f11b-4f19-9586-7bf291f212aa} ContactManager.js
contract @mozilla.org/contactManager;1 {1d70322b-f11b-4f19-9586-7bf291f212aa}
category JavaScript-navigator-property mozContacts @mozilla.org/contactManager;1
category JavaScript-navigator-property-maybe dom.mozContacts.enabled mozContacts,@mozilla.org/contactManager;1

View File

@ -228,11 +228,13 @@ function checkContacts(contact1, contact2) {
}
}
function tests() {
var req;
var index = 0;
var mozContacts = window.navigator.mozContacts;
ok(mozContacts, "mozContacts exists");
var steps = [
function () {
ok(true, "Deleting database");
@ -1245,11 +1247,12 @@ function permissionTest() {
}
}
var gContactsEnabled = SpecialPowers.getBoolPref("dom.mozContacts.enabled");
SimpleTest.waitForExplicitFinish();
addLoadEvent(permissionTest);
permissionTest();
};
ok(true, "test passed");
var gContactsEnabled = SpecialPowers.getBoolPref("dom.mozContacts.enabled");
addLoadEvent(tests);
SimpleTest.waitForExplicitFinish();
</script>
</pre>
</body>

View File

@ -261,6 +261,8 @@
@BINPATH@/components/ConsoleAPI.js
@BINPATH@/components/ContactManager.js
@BINPATH@/components/ContactManager.manifest
@BINPATH@/components/NavigatorPropteryHelper.js
@BINPATH@/components/NavigatorPropertyHelper.manifest
@BINPATH@/components/SettingsManager.js
@BINPATH@/components/SettingsManager.manifest
@BINPATH@/components/SettingsService.js