2012-06-26 19:03:32 -07:00
|
|
|
/* -*- Mode: JavaScript; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim: set ts=2 et sw=2 tw=80: */
|
|
|
|
/* 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 {classes: Cc, interfaces: Ci, utils: Cu} = Components;
|
|
|
|
|
|
|
|
Cu.import("resource://gre/modules/Services.jsm");
|
|
|
|
Cu.import("resource://gre/modules/FrameWorker.jsm");
|
2012-07-09 00:22:50 -07:00
|
|
|
Cu.import("resource://gre/modules/WorkerAPI.jsm");
|
2012-06-26 19:03:32 -07:00
|
|
|
|
|
|
|
const EXPORTED_SYMBOLS = ["SocialProvider"];
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The SocialProvider object represents a social provider, and allows
|
2012-07-06 08:47:34 -07:00
|
|
|
* access to its FrameWorker (if it has one).
|
2012-06-26 19:03:32 -07:00
|
|
|
*
|
|
|
|
* @constructor
|
|
|
|
* @param {jsobj} object representing the manifest file describing this provider
|
2012-07-11 10:43:56 -07:00
|
|
|
* @param {bool} whether the provider should be initially enabled (defaults to true)
|
2012-06-26 19:03:32 -07:00
|
|
|
*/
|
2012-07-11 10:43:56 -07:00
|
|
|
function SocialProvider(input, enabled) {
|
2012-06-26 19:03:32 -07:00
|
|
|
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;
|
2012-07-14 16:35:19 -07:00
|
|
|
this.iconURL = input.iconURL;
|
2012-06-26 19:03:32 -07:00
|
|
|
this.workerURL = input.workerURL;
|
|
|
|
this.origin = input.origin;
|
2012-07-14 16:35:19 -07:00
|
|
|
this.ambientNotificationIcons = {};
|
2012-07-09 00:22:50 -07:00
|
|
|
|
2012-07-11 10:43:56 -07:00
|
|
|
// If enabled is |undefined|, default to true.
|
|
|
|
this._enabled = !(enabled == false);
|
|
|
|
if (this._enabled)
|
|
|
|
this._activate();
|
2012-06-26 19:03:32 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
SocialProvider.prototype = {
|
2012-07-11 10:43:56 -07:00
|
|
|
// 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();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
// Active port to the provider's FrameWorker. Null if the provider has no
|
|
|
|
// FrameWorker, or is disabled.
|
|
|
|
port: null,
|
|
|
|
|
|
|
|
// Reference to a workerAPI object for this provider. Null if the provider has
|
|
|
|
// no FrameWorker, or is disabled.
|
|
|
|
workerAPI: null,
|
|
|
|
|
2012-07-14 16:35:19 -07:00
|
|
|
// Contains information related to the user's profile. Populated by the
|
|
|
|
// workerAPI via updateUserProfile. Null if the provider has no FrameWorker.
|
|
|
|
// Properties:
|
|
|
|
// iconURL, portrait, userName, displayName, profileURL
|
|
|
|
// See https://github.com/mozilla/socialapi-dev/blob/develop/docs/socialAPI.md
|
|
|
|
profile: null,
|
|
|
|
|
|
|
|
// Map of objects describing the provider's notification icons, whose
|
|
|
|
// properties include:
|
|
|
|
// name, iconURL, counter, contentPanel
|
|
|
|
// See https://github.com/mozilla/socialapi-dev/blob/develop/docs/socialAPI.md
|
|
|
|
ambientNotificationIcons: null,
|
|
|
|
|
|
|
|
// Called by the workerAPI to update our profile information.
|
|
|
|
updateUserProfile: function(profile) {
|
|
|
|
this.profile = profile;
|
|
|
|
|
|
|
|
if (profile.iconURL)
|
|
|
|
this.iconURL = profile.iconURL;
|
|
|
|
|
|
|
|
if (!profile.displayName)
|
|
|
|
profile.displayName = profile.userName;
|
|
|
|
|
2012-07-16 08:36:47 -07:00
|
|
|
// 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);
|
|
|
|
}
|
|
|
|
|
2012-07-14 16:35:19 -07:00
|
|
|
Services.obs.notifyObservers(null, "social:profile-changed", this.origin);
|
|
|
|
},
|
|
|
|
|
|
|
|
// Called by the workerAPI to add/update a notification icon.
|
|
|
|
setAmbientNotification: function(notification) {
|
2012-07-16 08:36:47 -07:00
|
|
|
if (!this.profile.userName)
|
|
|
|
throw new Error("unable to set notifications while logged out");
|
2012-07-14 16:35:19 -07:00
|
|
|
this.ambientNotificationIcons[notification.name] = notification;
|
|
|
|
|
|
|
|
Services.obs.notifyObservers(null, "social:ambient-notification-changed", this.origin);
|
|
|
|
},
|
|
|
|
|
2012-07-11 10:43:56 -07:00
|
|
|
// 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.
|
|
|
|
let workerAPIPort = this._getWorkerPort();
|
|
|
|
if (workerAPIPort)
|
2012-07-14 16:35:19 -07:00
|
|
|
this.workerAPI = new WorkerAPI(this, workerAPIPort);
|
2012-07-11 10:43:56 -07:00
|
|
|
|
|
|
|
this.port = this._getWorkerPort();
|
|
|
|
},
|
|
|
|
|
|
|
|
_terminate: function _terminate() {
|
2012-07-06 08:47:34 -07:00
|
|
|
if (this.workerURL) {
|
|
|
|
try {
|
|
|
|
getFrameWorkerHandle(this.workerURL, null).terminate();
|
|
|
|
} catch (e) {
|
|
|
|
Cu.reportError("SocialProvider FrameWorker termination failed: " + e);
|
|
|
|
}
|
2012-06-26 19:03:32 -07:00
|
|
|
}
|
2012-07-11 10:43:56 -07:00
|
|
|
this.port = null;
|
|
|
|
this.workerAPI = null;
|
2012-06-26 19:03:32 -07:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Instantiates a FrameWorker for the provider if one doesn't exist, and
|
2012-07-11 10:43:56 -07:00
|
|
|
* returns a reference to a new port to that FrameWorker.
|
2012-06-26 19:03:32 -07:00
|
|
|
*
|
2012-07-11 10:43:56 -07:00
|
|
|
* Returns null if this provider has no workerURL, or is disabled.
|
2012-07-06 08:47:34 -07:00
|
|
|
*
|
2012-06-26 19:03:32 -07:00
|
|
|
* @param {DOMWindow} window (optional)
|
|
|
|
*/
|
2012-07-11 10:43:56 -07:00
|
|
|
_getWorkerPort: function _getWorkerPort(window) {
|
|
|
|
if (!this.workerURL || !this.enabled)
|
2012-07-06 08:47:34 -07:00
|
|
|
return null;
|
2012-07-09 00:22:50 -07:00
|
|
|
try {
|
|
|
|
return getFrameWorkerHandle(this.workerURL, window).port;
|
|
|
|
} catch (ex) {
|
|
|
|
Cu.reportError("SocialProvider: retrieving worker port failed:" + ex);
|
|
|
|
return null;
|
|
|
|
}
|
2012-06-26 19:03:32 -07:00
|
|
|
}
|
|
|
|
}
|