2012-06-20 18:01:27 -07: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/. */
|
|
|
|
|
2012-10-31 09:13:28 -07:00
|
|
|
this.EXPORTED_SYMBOLS = ["SocialService"];
|
2012-06-20 18:01:27 -07:00
|
|
|
|
|
|
|
const { classes: Cc, interfaces: Ci, utils: Cu } = Components;
|
2012-06-26 19:03:32 -07:00
|
|
|
|
2012-06-20 18:01:27 -07:00
|
|
|
Cu.import("resource://gre/modules/Services.jsm");
|
2012-07-05 14:01:38 -07:00
|
|
|
Cu.import("resource://gre/modules/XPCOMUtils.jsm");
|
2012-07-16 08:43:50 -07:00
|
|
|
|
2012-07-17 18:44:09 -07:00
|
|
|
XPCOMUtils.defineLazyModuleGetter(this, "getFrameWorkerHandle", "resource://gre/modules/FrameWorker.jsm");
|
|
|
|
XPCOMUtils.defineLazyModuleGetter(this, "WorkerAPI", "resource://gre/modules/WorkerAPI.jsm");
|
2012-08-13 08:20:29 -07:00
|
|
|
XPCOMUtils.defineLazyModuleGetter(this, "MozSocialAPI", "resource://gre/modules/MozSocialAPI.jsm");
|
2012-07-17 18:44:09 -07:00
|
|
|
|
2012-07-18 11:40:05 -07:00
|
|
|
/**
|
|
|
|
* The SocialService is the public API to social providers - it tracks which
|
|
|
|
* providers are installed and enabled, and is the entry-point for access to
|
|
|
|
* the provider itself.
|
|
|
|
*/
|
|
|
|
|
|
|
|
// Internal helper methods and state
|
|
|
|
let SocialServiceInternal = {
|
|
|
|
enabled: Services.prefs.getBoolPref("social.enabled"),
|
|
|
|
get providerArray() {
|
|
|
|
return [p for ([, p] of Iterator(this.providers))];
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2012-08-13 08:20:29 -07:00
|
|
|
function initService() {
|
|
|
|
// Add a pref observer for the enabled state
|
2012-07-18 11:40:05 -07:00
|
|
|
function prefObserver(subject, topic, data) {
|
2012-11-06 16:30:04 -08:00
|
|
|
SocialService._setEnabled(Services.prefs.getBoolPref("social.enabled"));
|
2012-07-18 11:40:05 -07:00
|
|
|
}
|
|
|
|
Services.prefs.addObserver("social.enabled", prefObserver, false);
|
|
|
|
Services.obs.addObserver(function xpcomShutdown() {
|
|
|
|
Services.obs.removeObserver(xpcomShutdown, "xpcom-shutdown");
|
|
|
|
Services.prefs.removeObserver("social.enabled", prefObserver);
|
|
|
|
}, "xpcom-shutdown", false);
|
|
|
|
|
2012-07-13 13:51:35 -07:00
|
|
|
// Initialize the MozSocialAPI
|
2012-08-13 08:20:29 -07:00
|
|
|
if (SocialServiceInternal.enabled)
|
|
|
|
MozSocialAPI.enabled = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
XPCOMUtils.defineLazyGetter(SocialServiceInternal, "providers", function () {
|
|
|
|
initService();
|
2012-07-13 13:51:35 -07:00
|
|
|
|
2012-08-08 18:09:37 -07:00
|
|
|
// Don't load any providers from prefs if the test pref is set
|
|
|
|
let skipLoading = false;
|
|
|
|
try {
|
|
|
|
skipLoading = Services.prefs.getBoolPref("social.skipLoadingProviders");
|
|
|
|
} catch (ex) {}
|
|
|
|
|
|
|
|
if (skipLoading)
|
|
|
|
return {};
|
|
|
|
|
2012-08-13 08:20:29 -07:00
|
|
|
// Now retrieve the providers from prefs
|
2012-07-18 11:40:05 -07:00
|
|
|
let providers = {};
|
|
|
|
let MANIFEST_PREFS = Services.prefs.getBranch("social.manifest.");
|
|
|
|
let prefs = MANIFEST_PREFS.getChildList("", {});
|
2012-09-24 14:57:12 -07:00
|
|
|
let appinfo = Cc["@mozilla.org/xre/app-info;1"]
|
|
|
|
.getService(Ci.nsIXULRuntime);
|
2012-07-18 11:40:05 -07:00
|
|
|
prefs.forEach(function (pref) {
|
|
|
|
try {
|
|
|
|
var manifest = JSON.parse(MANIFEST_PREFS.getCharPref(pref));
|
|
|
|
if (manifest && typeof(manifest) == "object") {
|
2012-09-24 14:57:12 -07:00
|
|
|
let provider = new SocialProvider(manifest, appinfo.inSafeMode ? false : SocialServiceInternal.enabled);
|
2012-07-18 11:40:05 -07:00
|
|
|
providers[provider.origin] = provider;
|
|
|
|
}
|
|
|
|
} catch (err) {
|
|
|
|
Cu.reportError("SocialService: failed to load provider: " + pref +
|
|
|
|
", exception: " + err);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
return providers;
|
|
|
|
});
|
|
|
|
|
|
|
|
function schedule(callback) {
|
|
|
|
Services.tm.mainThread.dispatch(callback, Ci.nsIThread.DISPATCH_NORMAL);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Public API
|
2012-10-31 09:13:28 -07:00
|
|
|
this.SocialService = {
|
2012-07-18 11:40:05 -07:00
|
|
|
get enabled() {
|
|
|
|
return SocialServiceInternal.enabled;
|
|
|
|
},
|
|
|
|
set enabled(val) {
|
|
|
|
let enable = !!val;
|
2012-09-24 14:57:12 -07:00
|
|
|
|
|
|
|
// Allow setting to the same value when in safe mode so the
|
|
|
|
// feature can be force enabled.
|
|
|
|
if (enable == SocialServiceInternal.enabled &&
|
|
|
|
!Services.appinfo.inSafeMode)
|
2012-07-18 11:40:05 -07:00
|
|
|
return;
|
|
|
|
|
2012-11-06 16:11:28 -08:00
|
|
|
Services.prefs.setBoolPref("social.enabled", enable);
|
2012-11-06 16:30:04 -08:00
|
|
|
this._setEnabled(enable);
|
2012-07-18 11:40:05 -07:00
|
|
|
},
|
|
|
|
_setEnabled: function _setEnabled(enable) {
|
2012-11-06 16:30:04 -08:00
|
|
|
if (enable == SocialServiceInternal.enabled)
|
|
|
|
return;
|
|
|
|
|
2012-07-18 11:40:05 -07:00
|
|
|
SocialServiceInternal.providerArray.forEach(function (p) p.enabled = enable);
|
|
|
|
SocialServiceInternal.enabled = enable;
|
2012-07-13 13:51:35 -07:00
|
|
|
MozSocialAPI.enabled = enable;
|
2012-07-18 11:40:05 -07:00
|
|
|
Services.obs.notifyObservers(null, "social:pref-changed", enable ? "enabled" : "disabled");
|
2012-08-24 17:24:52 -07:00
|
|
|
Services.telemetry.getHistogramById("SOCIAL_TOGGLED").add(enable);
|
2012-07-18 11:40:05 -07:00
|
|
|
},
|
|
|
|
|
|
|
|
// Adds a provider given a manifest, and returns the added provider.
|
|
|
|
addProvider: function addProvider(manifest, onDone) {
|
|
|
|
if (SocialServiceInternal.providers[manifest.origin])
|
|
|
|
throw new Error("SocialService.addProvider: provider with this origin already exists");
|
|
|
|
|
|
|
|
let provider = new SocialProvider(manifest, SocialServiceInternal.enabled);
|
|
|
|
SocialServiceInternal.providers[provider.origin] = provider;
|
|
|
|
|
|
|
|
schedule(function () {
|
|
|
|
onDone(provider);
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
// Removes a provider with the given origin, and notifies when the removal is
|
|
|
|
// complete.
|
|
|
|
removeProvider: function removeProvider(origin, onDone) {
|
|
|
|
if (!(origin in SocialServiceInternal.providers))
|
|
|
|
throw new Error("SocialService.removeProvider: no provider with this origin exists!");
|
|
|
|
|
|
|
|
let provider = SocialServiceInternal.providers[origin];
|
|
|
|
provider.enabled = false;
|
|
|
|
|
|
|
|
delete SocialServiceInternal.providers[origin];
|
|
|
|
|
|
|
|
if (onDone)
|
|
|
|
schedule(onDone);
|
|
|
|
},
|
|
|
|
|
|
|
|
// Returns a single provider object with the specified origin.
|
|
|
|
getProvider: function getProvider(origin, onDone) {
|
|
|
|
schedule((function () {
|
|
|
|
onDone(SocialServiceInternal.providers[origin] || null);
|
|
|
|
}).bind(this));
|
|
|
|
},
|
|
|
|
|
|
|
|
// Returns an array of installed provider origins.
|
|
|
|
getProviderList: function getProviderList(onDone) {
|
|
|
|
schedule(function () {
|
|
|
|
onDone(SocialServiceInternal.providerArray);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2012-07-17 18:44:09 -07:00
|
|
|
/**
|
|
|
|
* The SocialProvider object represents a social provider, and allows
|
|
|
|
* access to its FrameWorker (if it has one).
|
|
|
|
*
|
|
|
|
* @constructor
|
|
|
|
* @param {jsobj} object representing the manifest file describing this provider
|
|
|
|
* @param {bool} whether the provider should be initially enabled (defaults to true)
|
|
|
|
*/
|
|
|
|
function SocialProvider(input, enabled) {
|
|
|
|
if (!input.name)
|
|
|
|
throw new Error("SocialProvider must be passed a name");
|
|
|
|
if (!input.origin)
|
|
|
|
throw new Error("SocialProvider must be passed an origin");
|
|
|
|
|
|
|
|
this.name = input.name;
|
|
|
|
this.iconURL = input.iconURL;
|
|
|
|
this.workerURL = input.workerURL;
|
2012-07-18 11:40:05 -07:00
|
|
|
this.sidebarURL = input.sidebarURL;
|
2012-07-17 18:44:09 -07:00
|
|
|
this.origin = input.origin;
|
|
|
|
this.ambientNotificationIcons = {};
|
|
|
|
|
|
|
|
// If enabled is |undefined|, default to true.
|
|
|
|
this._enabled = !(enabled == false);
|
|
|
|
if (this._enabled)
|
|
|
|
this._activate();
|
|
|
|
}
|
|
|
|
|
|
|
|
SocialProvider.prototype = {
|
|
|
|
// Provider enabled/disabled state. Disabled providers do not have active
|
|
|
|
// connections to their FrameWorkers.
|
|
|
|
_enabled: true,
|
|
|
|
get enabled() {
|
|
|
|
return this._enabled;
|
|
|
|
},
|
|
|
|
set enabled(val) {
|
|
|
|
let enable = !!val;
|
|
|
|
if (enable == this._enabled)
|
|
|
|
return;
|
|
|
|
|
|
|
|
this._enabled = enable;
|
|
|
|
|
|
|
|
if (enable) {
|
|
|
|
this._activate();
|
|
|
|
} else {
|
|
|
|
this._terminate();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
// Reference to a workerAPI object for this provider. Null if the provider has
|
|
|
|
// no FrameWorker, or is disabled.
|
|
|
|
workerAPI: null,
|
|
|
|
|
|
|
|
// Contains information related to the user's profile. Populated by the
|
2012-10-08 01:57:21 -07:00
|
|
|
// workerAPI via updateUserProfile.
|
2012-07-17 18:44:09 -07:00
|
|
|
// Properties:
|
|
|
|
// iconURL, portrait, userName, displayName, profileURL
|
|
|
|
// See https://github.com/mozilla/socialapi-dev/blob/develop/docs/socialAPI.md
|
2012-10-07 17:15:02 -07:00
|
|
|
// A value of null or an empty object means 'user not logged in'.
|
2012-10-08 01:57:21 -07:00
|
|
|
// A value of undefined means the service has not yet told us the status of
|
|
|
|
// the profile (ie, the service is still loading/initing, or the provider has
|
|
|
|
// no FrameWorker)
|
2012-10-07 17:15:02 -07:00
|
|
|
// This distinction might be used to cache certain data between runs - eg,
|
|
|
|
// browser-social.js caches the notification icons so they can be displayed
|
|
|
|
// quickly at startup without waiting for the provider to initialize -
|
|
|
|
// 'undefined' means 'ok to use cached values' versus 'null' meaning 'cached
|
|
|
|
// values aren't to be used as the user is logged out'.
|
|
|
|
profile: undefined,
|
2012-07-17 18:44:09 -07:00
|
|
|
|
2012-11-14 19:57:28 -08:00
|
|
|
// Contains the information necessary to support our "recommend" feature.
|
|
|
|
// null means no info yet provided (which includes the case of the provider
|
|
|
|
// not supporting the feature) or the provided data is invalid. Updated via
|
|
|
|
// the 'recommendInfo' setter and returned via the getter.
|
|
|
|
_recommendInfo: null,
|
|
|
|
get recommendInfo() {
|
|
|
|
return this._recommendInfo;
|
|
|
|
},
|
|
|
|
set recommendInfo(data) {
|
|
|
|
// Accept *and validate* the user-recommend-prompt-response message from
|
|
|
|
// the provider.
|
|
|
|
let promptImages = {};
|
|
|
|
let promptMessages = {};
|
|
|
|
function reportError(reason) {
|
|
|
|
Cu.reportError("Invalid recommend data from provider: " + reason + ": sharing is disabled for this provider");
|
|
|
|
// and we explicitly reset the recommend data to null to avoid stale
|
|
|
|
// data being used and notify our observers.
|
|
|
|
this._recommendInfo = null;
|
|
|
|
Services.obs.notifyObservers(null, "social:recommend-info-changed", this.origin);
|
|
|
|
}
|
|
|
|
if (!data ||
|
|
|
|
!data.images || typeof data.images != "object" ||
|
|
|
|
!data.messages || typeof data.messages != "object") {
|
|
|
|
reportError("data is missing valid 'images' or 'messages' elements");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
for (let sub of ["share", "unshare"]) {
|
|
|
|
let url = data.images[sub];
|
|
|
|
if (!url || typeof url != "string" || url.length == 0) {
|
|
|
|
reportError('images["' + sub + '"] is missing or not a non-empty string');
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// resolve potentially relative URLs then check the scheme is acceptable.
|
|
|
|
url = Services.io.newURI(this.origin, null, null).resolve(url);
|
|
|
|
let uri = Services.io.newURI(url, null, null);
|
|
|
|
if (!uri.schemeIs("http") && !uri.schemeIs("https") && !uri.schemeIs("data")) {
|
|
|
|
reportError('images["' + sub + '"] does not have a valid scheme');
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
promptImages[sub] = url;
|
|
|
|
}
|
|
|
|
for (let sub of ["shareTooltip", "unshareTooltip",
|
|
|
|
"sharedLabel", "unsharedLabel", "unshareLabel",
|
|
|
|
"portraitLabel",
|
|
|
|
"unshareConfirmLabel", "unshareConfirmAccessKey",
|
|
|
|
"unshareCancelLabel", "unshareCancelAccessKey"]) {
|
|
|
|
if (typeof data.messages[sub] != "string" || data.messages[sub].length == 0) {
|
|
|
|
reportError('messages["' + sub + '"] is not a valid string');
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
promptMessages[sub] = data.messages[sub];
|
|
|
|
}
|
|
|
|
this._recommendInfo = {images: promptImages, messages: promptMessages};
|
|
|
|
Services.obs.notifyObservers(null, "social:recommend-info-changed", this.origin);
|
|
|
|
},
|
|
|
|
|
2012-07-17 18:44:09 -07:00
|
|
|
// Map of objects describing the provider's notification icons, whose
|
|
|
|
// properties include:
|
|
|
|
// name, iconURL, counter, contentPanel
|
2012-10-07 17:15:02 -07:00
|
|
|
// See https://developer.mozilla.org/en-US/docs/Social_API
|
2012-07-17 18:44:09 -07:00
|
|
|
ambientNotificationIcons: null,
|
|
|
|
|
|
|
|
// Called by the workerAPI to update our profile information.
|
|
|
|
updateUserProfile: function(profile) {
|
|
|
|
this.profile = profile;
|
|
|
|
|
2012-08-02 15:30:19 -07:00
|
|
|
// Sanitize the portrait from any potential script-injection.
|
|
|
|
if (profile.portrait) {
|
|
|
|
try {
|
|
|
|
let portraitUri = Services.io.newURI(profile.portrait, null, null);
|
|
|
|
|
|
|
|
let scheme = portraitUri ? portraitUri.scheme : "";
|
|
|
|
if (scheme != "data" && scheme != "http" && scheme != "https") {
|
|
|
|
profile.portrait = "";
|
|
|
|
}
|
|
|
|
} catch (ex) {
|
|
|
|
profile.portrait = "";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-07-17 18:44:09 -07:00
|
|
|
if (profile.iconURL)
|
|
|
|
this.iconURL = profile.iconURL;
|
|
|
|
|
|
|
|
if (!profile.displayName)
|
|
|
|
profile.displayName = profile.userName;
|
|
|
|
|
|
|
|
// if no userName, consider this a logged out state, emtpy the
|
|
|
|
// users ambient notifications. notify both profile and ambient
|
|
|
|
// changes to clear everything
|
|
|
|
if (!profile.userName) {
|
|
|
|
this.profile = {};
|
|
|
|
this.ambientNotificationIcons = {};
|
|
|
|
Services.obs.notifyObservers(null, "social:ambient-notification-changed", this.origin);
|
|
|
|
}
|
|
|
|
|
|
|
|
Services.obs.notifyObservers(null, "social:profile-changed", this.origin);
|
|
|
|
},
|
|
|
|
|
|
|
|
// Called by the workerAPI to add/update a notification icon.
|
|
|
|
setAmbientNotification: function(notification) {
|
|
|
|
if (!this.profile.userName)
|
|
|
|
throw new Error("unable to set notifications while logged out");
|
|
|
|
this.ambientNotificationIcons[notification.name] = notification;
|
|
|
|
|
|
|
|
Services.obs.notifyObservers(null, "social:ambient-notification-changed", this.origin);
|
|
|
|
},
|
|
|
|
|
|
|
|
// Internal helper methods
|
|
|
|
_activate: function _activate() {
|
|
|
|
// Initialize the workerAPI and its port first, so that its initialization
|
|
|
|
// occurs before any other messages are processed by other ports.
|
2012-09-11 19:48:38 -07:00
|
|
|
let workerAPIPort = this.getWorkerPort();
|
2012-07-17 18:44:09 -07:00
|
|
|
if (workerAPIPort)
|
|
|
|
this.workerAPI = new WorkerAPI(this, workerAPIPort);
|
|
|
|
},
|
|
|
|
|
|
|
|
_terminate: function _terminate() {
|
|
|
|
if (this.workerURL) {
|
|
|
|
try {
|
|
|
|
getFrameWorkerHandle(this.workerURL, null).terminate();
|
|
|
|
} catch (e) {
|
|
|
|
Cu.reportError("SocialProvider FrameWorker termination failed: " + e);
|
|
|
|
}
|
|
|
|
}
|
2012-09-11 19:48:38 -07:00
|
|
|
if (this.workerAPI) {
|
|
|
|
this.workerAPI.terminate();
|
|
|
|
}
|
2012-07-17 18:44:09 -07:00
|
|
|
this.workerAPI = null;
|
2012-10-31 21:00:15 -07:00
|
|
|
this.profile = undefined;
|
2012-07-17 18:44:09 -07:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Instantiates a FrameWorker for the provider if one doesn't exist, and
|
|
|
|
* returns a reference to a new port to that FrameWorker.
|
|
|
|
*
|
|
|
|
* Returns null if this provider has no workerURL, or is disabled.
|
|
|
|
*
|
|
|
|
* @param {DOMWindow} window (optional)
|
|
|
|
*/
|
2012-09-11 19:48:38 -07:00
|
|
|
getWorkerPort: function getWorkerPort(window) {
|
2012-07-17 18:44:09 -07:00
|
|
|
if (!this.workerURL || !this.enabled)
|
|
|
|
return null;
|
2012-09-11 19:48:38 -07:00
|
|
|
return getFrameWorkerHandle(this.workerURL, window).port;
|
2012-07-17 18:44:09 -07:00
|
|
|
}
|
|
|
|
}
|