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");
|
|
|
|
|
2013-02-08 11:39:25 -08:00
|
|
|
// Add a pref observer for the enabled state
|
|
|
|
function prefObserver(subject, topic, data) {
|
|
|
|
let enable = Services.prefs.getBoolPref("social.enabled");
|
|
|
|
if (enable && !Social.provider) {
|
|
|
|
Social.provider = Social.defaultProvider;
|
|
|
|
} else if (!enable && Social.provider) {
|
|
|
|
Social.provider = null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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-10-31 09:13:28 -07:00
|
|
|
this.Social = {
|
2013-03-06 22:25:31 -08:00
|
|
|
initialized: false,
|
2012-06-22 15:01:34 -07:00
|
|
|
lastEventReceived: 0,
|
2012-12-07 20:13:27 -08:00
|
|
|
providers: null,
|
2012-09-24 14:57:12 -07:00
|
|
|
_disabledForSafeMode: false,
|
2012-12-07 20:13:27 -08:00
|
|
|
|
|
|
|
get _currentProviderPref() {
|
|
|
|
try {
|
|
|
|
return Services.prefs.getComplexValue("social.provider.current",
|
|
|
|
Ci.nsISupportsString).data;
|
|
|
|
} catch (ex) {}
|
|
|
|
return null;
|
|
|
|
},
|
|
|
|
set _currentProviderPref(val) {
|
|
|
|
let string = Cc["@mozilla.org/supports-string;1"].
|
|
|
|
createInstance(Ci.nsISupportsString);
|
|
|
|
string.data = val;
|
|
|
|
Services.prefs.setComplexValue("social.provider.current",
|
|
|
|
Ci.nsISupportsString, string);
|
|
|
|
},
|
|
|
|
|
|
|
|
_provider: null,
|
|
|
|
get provider() {
|
|
|
|
return this._provider;
|
|
|
|
},
|
|
|
|
set provider(val) {
|
2013-02-19 17:23:47 -08:00
|
|
|
this._setProvider(val);
|
2012-12-07 20:13:27 -08:00
|
|
|
},
|
|
|
|
|
2013-02-08 11:39:25 -08:00
|
|
|
// Sets the current provider and enables it. Also disables the
|
2013-02-19 17:23:47 -08:00
|
|
|
// previously set provider, and notifies observers of the change.
|
|
|
|
_setProvider: function (provider) {
|
2012-12-07 20:13:27 -08:00
|
|
|
if (this._provider == provider)
|
|
|
|
return;
|
|
|
|
|
|
|
|
// Disable the previous provider, if any, since we want only one provider to
|
|
|
|
// be enabled at once.
|
|
|
|
if (this._provider)
|
|
|
|
this._provider.enabled = false;
|
|
|
|
|
|
|
|
this._provider = provider;
|
|
|
|
|
|
|
|
if (this._provider) {
|
2013-02-08 11:39:25 -08:00
|
|
|
this._provider.enabled = true;
|
2012-12-07 20:13:27 -08:00
|
|
|
this._currentProviderPref = this._provider.origin;
|
2013-02-08 11:39:25 -08:00
|
|
|
}
|
|
|
|
let enabled = !!provider;
|
|
|
|
if (enabled != SocialService.enabled) {
|
|
|
|
SocialService.enabled = enabled;
|
|
|
|
Services.prefs.setBoolPref("social.enabled", enabled);
|
2012-12-07 20:13:27 -08:00
|
|
|
}
|
|
|
|
|
2013-02-19 17:23:47 -08:00
|
|
|
let origin = this._provider && this._provider.origin;
|
|
|
|
Services.obs.notifyObservers(null, "social:provider-set", origin);
|
2012-12-07 20:13:27 -08:00
|
|
|
},
|
|
|
|
|
2013-02-08 11:39:25 -08:00
|
|
|
get defaultProvider() {
|
|
|
|
if (this.providers.length == 0)
|
|
|
|
return null;
|
|
|
|
let provider = this._getProviderFromOrigin(this._currentProviderPref);
|
|
|
|
return provider || this.providers[0];
|
|
|
|
},
|
|
|
|
|
2013-02-19 17:23:47 -08:00
|
|
|
init: function Social_init() {
|
2012-09-24 14:57:12 -07:00
|
|
|
this._disabledForSafeMode = Services.appinfo.inSafeMode && this.enabled;
|
|
|
|
|
2013-03-06 22:25:31 -08:00
|
|
|
if (this.initialized) {
|
2012-07-11 18:31:19 -07:00
|
|
|
return;
|
|
|
|
}
|
2013-03-06 22:25:31 -08:00
|
|
|
this.initialized = true;
|
2012-07-11 18:31:19 -07:00
|
|
|
|
2012-12-07 20:13:27 -08:00
|
|
|
// Retrieve the current set of providers, and set the current provider.
|
2012-07-11 18:31:19 -07:00
|
|
|
SocialService.getProviderList(function (providers) {
|
2013-02-19 17:23:47 -08:00
|
|
|
this._updateProviderCache(providers);
|
2012-07-11 18:31:19 -07:00
|
|
|
}.bind(this));
|
2012-12-07 20:13:27 -08:00
|
|
|
|
|
|
|
// Register an observer for changes to the provider list
|
|
|
|
SocialService.registerProviderListener(function providerListener(topic, data) {
|
|
|
|
// An engine change caused by adding/removing a provider should notify
|
2013-02-08 11:39:25 -08:00
|
|
|
if (topic == "provider-added" || topic == "provider-removed") {
|
2013-02-19 17:23:47 -08:00
|
|
|
this._updateProviderCache(data);
|
2013-02-08 11:39:25 -08:00
|
|
|
Services.obs.notifyObservers(null, "social:providers-changed", null);
|
|
|
|
}
|
2012-12-07 20:13:27 -08:00
|
|
|
}.bind(this));
|
|
|
|
},
|
|
|
|
|
|
|
|
// Called to update our cache of providers and set the current provider
|
2013-02-19 17:23:47 -08:00
|
|
|
_updateProviderCache: function (providers) {
|
2012-12-07 20:13:27 -08:00
|
|
|
this.providers = providers;
|
|
|
|
|
2013-03-06 22:25:31 -08:00
|
|
|
// If social is currently disabled there's nothing else to do other than
|
|
|
|
// to notify about the lack of a provider.
|
|
|
|
if (!SocialService.enabled) {
|
|
|
|
Services.obs.notifyObservers(null, "social:provider-set", null);
|
2013-02-08 11:39:25 -08:00
|
|
|
return;
|
2013-03-06 22:25:31 -08:00
|
|
|
}
|
2013-02-08 11:39:25 -08:00
|
|
|
// Otherwise set the provider.
|
2013-02-19 17:23:47 -08:00
|
|
|
this._setProvider(this.defaultProvider);
|
2012-06-22 15:01:34 -07:00
|
|
|
},
|
2013-02-07 06:36:31 -08:00
|
|
|
|
|
|
|
set enabled(val) {
|
2013-02-08 11:39:25 -08:00
|
|
|
// Setting .enabled is just a shortcut for setting the provider to either
|
|
|
|
// the default provider or null...
|
|
|
|
if (val) {
|
|
|
|
if (!this.provider)
|
|
|
|
this.provider = this.defaultProvider;
|
|
|
|
} else {
|
|
|
|
this.provider = null;
|
|
|
|
}
|
2013-02-07 06:36:31 -08:00
|
|
|
},
|
2012-06-22 15:01:34 -07:00
|
|
|
get enabled() {
|
2013-02-08 11:39:25 -08:00
|
|
|
return this.provider != null;
|
2012-06-22 15:01:34 -07:00
|
|
|
},
|
|
|
|
|
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-12-07 20:13:27 -08:00
|
|
|
haveLoggedInUser: function () {
|
|
|
|
return !!(this.provider && this.provider.profile && this.provider.profile.userName);
|
|
|
|
},
|
|
|
|
|
|
|
|
setProviderByOrigin: function (origin) {
|
|
|
|
this.provider = this._getProviderFromOrigin(origin);
|
|
|
|
},
|
|
|
|
|
|
|
|
_getProviderFromOrigin: function (origin) {
|
|
|
|
for (let p of this.providers) {
|
|
|
|
if (p.origin == origin) {
|
|
|
|
return p;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
},
|
|
|
|
|
2013-03-21 13:34:26 -07:00
|
|
|
installProvider: function(doc, data, installCallback) {
|
|
|
|
SocialService.installProvider(doc, data, installCallback);
|
2013-03-05 11:24:30 -08:00
|
|
|
},
|
|
|
|
|
|
|
|
uninstallProvider: function(origin) {
|
|
|
|
SocialService.uninstallProvider(origin);
|
|
|
|
},
|
|
|
|
|
2012-12-07 20:13:27 -08:00
|
|
|
// Activation functionality
|
2013-02-08 11:39:25 -08:00
|
|
|
activateFromOrigin: function (origin, callback) {
|
|
|
|
// For now only "builtin" providers can be activated. It's OK if the
|
|
|
|
// provider has already been activated - we still get called back with it.
|
|
|
|
SocialService.addBuiltinProvider(origin, function(provider) {
|
|
|
|
if (provider) {
|
|
|
|
// No need to activate again if we're already active
|
|
|
|
if (provider == this.provider)
|
|
|
|
return;
|
|
|
|
this.provider = provider;
|
|
|
|
}
|
|
|
|
if (callback)
|
|
|
|
callback(provider);
|
|
|
|
}.bind(this));
|
2012-12-07 20:13:27 -08:00
|
|
|
},
|
|
|
|
|
|
|
|
deactivateFromOrigin: function (origin, oldOrigin) {
|
2013-02-08 11:39:25 -08:00
|
|
|
// if we have the old provider, always set that before trying removal
|
2012-12-07 20:13:27 -08:00
|
|
|
let provider = this._getProviderFromOrigin(origin);
|
2013-02-08 11:39:25 -08:00
|
|
|
let oldProvider = this._getProviderFromOrigin(oldOrigin);
|
|
|
|
if (!oldProvider && this.providers.length)
|
|
|
|
oldProvider = this.providers[0];
|
|
|
|
this.provider = oldProvider;
|
|
|
|
if (provider)
|
|
|
|
SocialService.removeProvider(origin);
|
|
|
|
},
|
|
|
|
|
2013-05-03 10:59:43 -07:00
|
|
|
// Sharing functionality
|
|
|
|
_getShareablePageUrl: function Social_getShareablePageUrl(aURI) {
|
2012-07-11 18:31:19 -07:00
|
|
|
let uri = aURI.clone();
|
|
|
|
try {
|
|
|
|
// Setting userPass on about:config throws.
|
|
|
|
uri.userPass = "";
|
|
|
|
} catch (e) {}
|
|
|
|
return uri.spec;
|
|
|
|
},
|
|
|
|
|
2013-05-03 10:59:43 -07:00
|
|
|
isPageShared: function Social_isPageShared(aURI) {
|
|
|
|
let url = this._getShareablePageUrl(aURI);
|
|
|
|
return this._sharedUrls.hasOwnProperty(url);
|
2012-07-11 18:31:19 -07:00
|
|
|
},
|
|
|
|
|
2013-05-03 10:59:43 -07:00
|
|
|
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) {
|
2013-05-03 10:59:43 -07:00
|
|
|
Cu.reportError("Can't share a page when no provider is current");
|
2012-09-11 19:48:38 -07:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
let port = this.provider.getWorkerPort();
|
|
|
|
if (!port) {
|
2013-05-03 10:59:43 -07:00
|
|
|
Cu.reportError("Can't share page as no provider port is available");
|
2012-09-11 19:48:38 -07:00
|
|
|
return;
|
|
|
|
}
|
2013-05-03 10:59:43 -07:00
|
|
|
let url = this._getShareablePageUrl(aURI);
|
|
|
|
this._sharedUrls[url] = true;
|
|
|
|
port.postMessage({
|
|
|
|
topic: "social.user-recommend",
|
|
|
|
data: { url: url }
|
|
|
|
});
|
|
|
|
port.close();
|
2013-05-02 11:49:11 -07:00
|
|
|
},
|
2013-05-03 10:59:43 -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) {
|
2013-05-03 10:59:43 -07:00
|
|
|
Cu.reportError("Can't unshare a page when no provider is current");
|
2012-09-11 19:48:38 -07:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
let port = this.provider.getWorkerPort();
|
|
|
|
if (!port) {
|
2013-05-03 10:59:43 -07:00
|
|
|
Cu.reportError("Can't unshare page as no provider port is available");
|
2012-09-11 19:48:38 -07:00
|
|
|
return;
|
|
|
|
}
|
2013-05-03 10:59:43 -07:00
|
|
|
let url = this._getShareablePageUrl(aURI);
|
|
|
|
delete this._sharedUrls[url];
|
|
|
|
port.postMessage({
|
|
|
|
topic: "social.user-unrecommend",
|
|
|
|
data: { url: url }
|
|
|
|
});
|
|
|
|
port.close();
|
2013-05-02 11:49:11 -07:00
|
|
|
},
|
2013-04-25 03:19:52 -07:00
|
|
|
|
2013-05-03 10:59:43 -07:00
|
|
|
_sharedUrls: {},
|
|
|
|
|
2013-01-26 12:07:39 -08:00
|
|
|
setErrorListener: function(iframe, errorHandler) {
|
|
|
|
if (iframe.socialErrorListener)
|
|
|
|
return iframe.socialErrorListener;
|
|
|
|
return new SocialErrorListener(iframe, errorHandler);
|
|
|
|
}
|
2012-07-11 18:31:19 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
function schedule(callback) {
|
|
|
|
Services.tm.mainThread.dispatch(callback, Ci.nsIThread.DISPATCH_NORMAL);
|
|
|
|
}
|
2013-01-26 12:07:39 -08:00
|
|
|
|
|
|
|
|
|
|
|
// Error handling class used to listen for network errors in the social frames
|
|
|
|
// and replace them with a social-specific error page
|
|
|
|
function SocialErrorListener(iframe, errorHandler) {
|
|
|
|
this.setErrorMessage = errorHandler;
|
|
|
|
this.iframe = iframe;
|
|
|
|
iframe.socialErrorListener = this;
|
|
|
|
iframe.docShell.QueryInterface(Ci.nsIInterfaceRequestor)
|
|
|
|
.getInterface(Ci.nsIWebProgress)
|
|
|
|
.addProgressListener(this,
|
|
|
|
Ci.nsIWebProgress.NOTIFY_STATE_REQUEST |
|
|
|
|
Ci.nsIWebProgress.NOTIFY_LOCATION);
|
|
|
|
}
|
|
|
|
|
|
|
|
SocialErrorListener.prototype = {
|
|
|
|
QueryInterface: XPCOMUtils.generateQI([Ci.nsIWebProgressListener,
|
|
|
|
Ci.nsISupportsWeakReference,
|
|
|
|
Ci.nsISupports]),
|
|
|
|
|
|
|
|
remove: function() {
|
|
|
|
this.iframe.docShell.QueryInterface(Ci.nsIInterfaceRequestor)
|
|
|
|
.getInterface(Ci.nsIWebProgress)
|
|
|
|
.removeProgressListener(this);
|
|
|
|
delete this.iframe.socialErrorListener;
|
|
|
|
},
|
|
|
|
|
|
|
|
onStateChange: function SPL_onStateChange(aWebProgress, aRequest, aState, aStatus) {
|
|
|
|
let failure = false;
|
|
|
|
if ((aState & Ci.nsIWebProgressListener.STATE_STOP)) {
|
|
|
|
if (aRequest instanceof Ci.nsIHttpChannel) {
|
|
|
|
try {
|
|
|
|
// Change the frame to an error page on 4xx (client errors)
|
|
|
|
// and 5xx (server errors)
|
|
|
|
failure = aRequest.responseStatus >= 400 &&
|
|
|
|
aRequest.responseStatus < 600;
|
|
|
|
} catch (e) {}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Calling cancel() will raise some OnStateChange notifications by itself,
|
|
|
|
// so avoid doing that more than once
|
|
|
|
if (failure && aStatus != Components.results.NS_BINDING_ABORTED) {
|
|
|
|
aRequest.cancel(Components.results.NS_BINDING_ABORTED);
|
|
|
|
Social.provider.errorState = "content-error";
|
|
|
|
this.setErrorMessage(aWebProgress.QueryInterface(Ci.nsIDocShell)
|
|
|
|
.chromeEventHandler);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
onLocationChange: function SPL_onLocationChange(aWebProgress, aRequest, aLocation, aFlags) {
|
|
|
|
let failure = aFlags & Ci.nsIWebProgressListener.LOCATION_CHANGE_ERROR_PAGE;
|
|
|
|
if (failure && Social.provider.errorState != "frameworker-error") {
|
|
|
|
aRequest.cancel(Components.results.NS_BINDING_ABORTED);
|
|
|
|
Social.provider.errorState = "content-error";
|
|
|
|
schedule(function() {
|
|
|
|
this.setErrorMessage(aWebProgress.QueryInterface(Ci.nsIDocShell)
|
|
|
|
.chromeEventHandler);
|
|
|
|
}.bind(this));
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
onProgressChange: function SPL_onProgressChange() {},
|
|
|
|
onStatusChange: function SPL_onStatusChange() {},
|
|
|
|
onSecurityChange: function SPL_onSecurityChange() {},
|
|
|
|
};
|