2012-07-11 18:31:19 -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/. */
|
|
|
|
|
|
|
|
"use strict";
|
|
|
|
|
2012-10-31 09:13:28 -07:00
|
|
|
this.EXPORTED_SYMBOLS = ["Social"];
|
2012-07-11 18:31:19 -07:00
|
|
|
|
|
|
|
const Ci = Components.interfaces;
|
|
|
|
const Cc = Components.classes;
|
|
|
|
const Cu = Components.utils;
|
|
|
|
|
|
|
|
Cu.import("resource://gre/modules/Services.jsm");
|
|
|
|
Cu.import("resource://gre/modules/XPCOMUtils.jsm");
|
|
|
|
|
|
|
|
XPCOMUtils.defineLazyModuleGetter(this, "SocialService",
|
|
|
|
"resource://gre/modules/SocialService.jsm");
|
|
|
|
|
2012-10-31 09:13:28 -07:00
|
|
|
this.Social = {
|
2012-06-22 15:01:34 -07:00
|
|
|
lastEventReceived: 0,
|
2012-07-11 18:31:19 -07:00
|
|
|
provider: null,
|
2012-09-24 14:57:12 -07:00
|
|
|
_disabledForSafeMode: false,
|
2012-07-11 18:31:19 -07:00
|
|
|
init: function Social_init(callback) {
|
2012-09-24 14:57:12 -07:00
|
|
|
this._disabledForSafeMode = Services.appinfo.inSafeMode && this.enabled;
|
|
|
|
|
2012-07-11 18:31:19 -07:00
|
|
|
if (this.provider) {
|
|
|
|
schedule(callback);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Eventually this might want to retrieve a specific provider, but for now
|
|
|
|
// just use the first available.
|
|
|
|
SocialService.getProviderList(function (providers) {
|
|
|
|
if (providers.length)
|
|
|
|
this.provider = providers[0];
|
|
|
|
callback();
|
|
|
|
}.bind(this));
|
|
|
|
},
|
|
|
|
|
2012-07-15 16:12:13 -07:00
|
|
|
get uiVisible() {
|
2012-09-11 19:48:38 -07:00
|
|
|
return this.provider && this.provider.enabled;
|
2012-07-15 16:12:13 -07:00
|
|
|
},
|
|
|
|
|
2012-06-22 15:01:34 -07:00
|
|
|
set enabled(val) {
|
2012-10-30 13:10:04 -07:00
|
|
|
if (!val) {
|
|
|
|
delete this.errorState;
|
|
|
|
}
|
2012-06-22 15:01:34 -07:00
|
|
|
SocialService.enabled = val;
|
|
|
|
},
|
|
|
|
get enabled() {
|
|
|
|
return SocialService.enabled;
|
|
|
|
},
|
|
|
|
|
|
|
|
get active() {
|
|
|
|
return Services.prefs.getBoolPref("social.active");
|
|
|
|
},
|
|
|
|
set active(val) {
|
|
|
|
Services.prefs.setBoolPref("social.active", !!val);
|
|
|
|
this.enabled = !!val;
|
|
|
|
},
|
|
|
|
|
2012-07-22 20:49:28 -07:00
|
|
|
toggle: function Social_toggle() {
|
2012-09-24 14:57:12 -07:00
|
|
|
this.enabled = this._disabledForSafeMode ? false : !this.enabled;
|
|
|
|
this._disabledForSafeMode = false;
|
2012-07-22 20:49:28 -07:00
|
|
|
},
|
|
|
|
|
2012-07-18 11:40:05 -07:00
|
|
|
toggleSidebar: function SocialSidebar_toggle() {
|
|
|
|
let prefValue = Services.prefs.getBoolPref("social.sidebar.open");
|
|
|
|
Services.prefs.setBoolPref("social.sidebar.open", !prefValue);
|
|
|
|
},
|
|
|
|
|
2012-09-26 17:40:18 -07:00
|
|
|
toggleNotifications: function SocialNotifications_toggle() {
|
|
|
|
let prefValue = Services.prefs.getBoolPref("social.toast-notifications.enabled");
|
|
|
|
Services.prefs.setBoolPref("social.toast-notifications.enabled", !prefValue);
|
|
|
|
},
|
|
|
|
|
2012-07-11 18:31:19 -07:00
|
|
|
// Sharing functionality
|
|
|
|
_getShareablePageUrl: function Social_getShareablePageUrl(aURI) {
|
|
|
|
let uri = aURI.clone();
|
|
|
|
try {
|
|
|
|
// Setting userPass on about:config throws.
|
|
|
|
uri.userPass = "";
|
|
|
|
} catch (e) {}
|
|
|
|
return uri.spec;
|
|
|
|
},
|
|
|
|
|
|
|
|
isPageShared: function Social_isPageShared(aURI) {
|
|
|
|
let url = this._getShareablePageUrl(aURI);
|
|
|
|
return this._sharedUrls.hasOwnProperty(url);
|
|
|
|
},
|
|
|
|
|
|
|
|
sharePage: function Social_sharePage(aURI) {
|
2012-09-11 19:48:38 -07:00
|
|
|
// this should not be called if this.provider or the port is null
|
|
|
|
if (!this.provider) {
|
|
|
|
Cu.reportError("Can't share a page when no provider is current");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
let port = this.provider.getWorkerPort();
|
|
|
|
if (!port) {
|
|
|
|
Cu.reportError("Can't share page as no provider port is available");
|
|
|
|
return;
|
|
|
|
}
|
2012-07-11 18:31:19 -07:00
|
|
|
let url = this._getShareablePageUrl(aURI);
|
|
|
|
this._sharedUrls[url] = true;
|
2012-09-11 19:48:38 -07:00
|
|
|
port.postMessage({
|
2012-07-11 18:31:19 -07:00
|
|
|
topic: "social.user-recommend",
|
|
|
|
data: { url: url }
|
|
|
|
});
|
2012-09-11 19:48:38 -07:00
|
|
|
port.close();
|
2012-07-11 18:31:19 -07:00
|
|
|
},
|
|
|
|
|
|
|
|
unsharePage: function Social_unsharePage(aURI) {
|
2012-09-11 19:48:38 -07:00
|
|
|
// this should not be called if this.provider or the port is null
|
|
|
|
if (!this.provider) {
|
|
|
|
Cu.reportError("Can't unshare a page when no provider is current");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
let port = this.provider.getWorkerPort();
|
|
|
|
if (!port) {
|
|
|
|
Cu.reportError("Can't unshare page as no provider port is available");
|
|
|
|
return;
|
|
|
|
}
|
2012-07-11 18:31:19 -07:00
|
|
|
let url = this._getShareablePageUrl(aURI);
|
|
|
|
delete this._sharedUrls[url];
|
2012-09-11 19:48:38 -07:00
|
|
|
port.postMessage({
|
2012-07-11 18:31:19 -07:00
|
|
|
topic: "social.user-unrecommend",
|
|
|
|
data: { url: url }
|
|
|
|
});
|
2012-09-11 19:48:38 -07:00
|
|
|
port.close();
|
2012-07-11 18:31:19 -07:00
|
|
|
},
|
|
|
|
|
|
|
|
_sharedUrls: {}
|
|
|
|
};
|
|
|
|
|
|
|
|
function schedule(callback) {
|
|
|
|
Services.tm.mainThread.dispatch(callback, Ci.nsIThread.DISPATCH_NORMAL);
|
|
|
|
}
|