2012-07-12 05:31:19 +04: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";
|
|
|
|
|
2016-08-24 01:24:06 +03:00
|
|
|
this.EXPORTED_SYMBOLS = ["Social", "OpenGraphBuilder",
|
2013-12-17 09:21:17 +04:00
|
|
|
"DynamicResizeWatcher", "sizeSocialPanelToContent"];
|
2012-07-12 05:31:19 +04:00
|
|
|
|
|
|
|
const Ci = Components.interfaces;
|
|
|
|
const Cc = Components.classes;
|
|
|
|
const Cu = Components.utils;
|
|
|
|
|
2014-10-09 23:01:48 +04:00
|
|
|
// The minimum sizes for the auto-resize panel code, minimum size necessary to
|
|
|
|
// properly show the error page in the panel.
|
2014-10-23 04:53:00 +04:00
|
|
|
const PANEL_MIN_HEIGHT = 190;
|
2013-09-06 21:56:01 +04:00
|
|
|
const PANEL_MIN_WIDTH = 330;
|
|
|
|
|
2012-07-12 05:31:19 +04:00
|
|
|
Cu.import("resource://gre/modules/Services.jsm");
|
|
|
|
Cu.import("resource://gre/modules/XPCOMUtils.jsm");
|
|
|
|
|
2013-12-17 09:21:17 +04:00
|
|
|
XPCOMUtils.defineLazyModuleGetter(this, "CustomizableUI",
|
|
|
|
"resource:///modules/CustomizableUI.jsm");
|
2012-07-12 05:31:19 +04:00
|
|
|
XPCOMUtils.defineLazyModuleGetter(this, "SocialService",
|
2016-08-24 01:24:07 +03:00
|
|
|
"resource:///modules/SocialService.jsm");
|
2015-03-06 01:43:05 +03:00
|
|
|
XPCOMUtils.defineLazyModuleGetter(this, "PageMetadata",
|
|
|
|
"resource://gre/modules/PageMetadata.jsm");
|
2013-05-05 00:11:18 +04:00
|
|
|
XPCOMUtils.defineLazyModuleGetter(this, "PlacesUtils",
|
|
|
|
"resource://gre/modules/PlacesUtils.jsm");
|
|
|
|
XPCOMUtils.defineLazyModuleGetter(this, "Promise",
|
2013-12-18 22:18:10 +04:00
|
|
|
"resource://gre/modules/Promise.jsm");
|
2012-07-12 05:31:19 +04:00
|
|
|
|
2013-05-07 10:02:58 +04:00
|
|
|
|
2012-10-31 20:13:28 +04:00
|
|
|
this.Social = {
|
2013-03-07 10:25:31 +04:00
|
|
|
initialized: false,
|
2012-06-23 02:01:34 +04:00
|
|
|
lastEventReceived: 0,
|
2013-08-12 23:06:22 +04:00
|
|
|
providers: [],
|
2012-09-25 01:57:12 +04:00
|
|
|
_disabledForSafeMode: false,
|
2012-12-08 08:13:27 +04:00
|
|
|
|
2013-02-20 05:23:47 +04:00
|
|
|
init: function Social_init() {
|
2012-09-25 01:57:12 +04:00
|
|
|
this._disabledForSafeMode = Services.appinfo.inSafeMode && this.enabled;
|
2014-03-12 06:25:45 +04:00
|
|
|
let deferred = Promise.defer();
|
2012-09-25 01:57:12 +04:00
|
|
|
|
2013-03-07 10:25:31 +04:00
|
|
|
if (this.initialized) {
|
2014-03-12 06:25:45 +04:00
|
|
|
deferred.resolve(true);
|
|
|
|
return deferred.promise;
|
2012-07-12 05:31:19 +04:00
|
|
|
}
|
2013-03-07 10:25:31 +04:00
|
|
|
this.initialized = true;
|
2013-08-30 22:11:20 +04:00
|
|
|
// if SocialService.hasEnabledProviders, retreive the providers so the
|
|
|
|
// front-end can generate UI
|
|
|
|
if (SocialService.hasEnabledProviders) {
|
2013-08-12 23:06:22 +04:00
|
|
|
// Retrieve the current set of providers, and set the current provider.
|
|
|
|
SocialService.getOrderedProviderList(function (providers) {
|
2013-08-21 22:32:46 +04:00
|
|
|
Social._updateProviderCache(providers);
|
2016-03-18 02:46:41 +03:00
|
|
|
Social._updateEnabledState(SocialService.enabled);
|
2014-03-12 06:25:45 +04:00
|
|
|
deferred.resolve(false);
|
2013-08-21 22:32:46 +04:00
|
|
|
});
|
2014-03-12 06:25:45 +04:00
|
|
|
} else {
|
|
|
|
deferred.resolve(false);
|
2013-08-12 23:06:22 +04:00
|
|
|
}
|
2012-12-08 08:13:27 +04:00
|
|
|
|
|
|
|
// Register an observer for changes to the provider list
|
2013-09-04 21:01:38 +04:00
|
|
|
SocialService.registerProviderListener(function providerListener(topic, origin, providers) {
|
2013-08-21 22:32:46 +04:00
|
|
|
// An engine change caused by adding/removing a provider should notify.
|
|
|
|
// any providers we receive are enabled in the AddonsManager
|
2013-09-04 21:01:38 +04:00
|
|
|
if (topic == "provider-installed" || topic == "provider-uninstalled") {
|
|
|
|
// installed/uninstalled do not send the providers param
|
|
|
|
Services.obs.notifyObservers(null, "social:" + topic, origin);
|
|
|
|
return;
|
|
|
|
}
|
2013-09-14 21:16:47 +04:00
|
|
|
if (topic == "provider-enabled") {
|
|
|
|
Social._updateProviderCache(providers);
|
2016-03-18 02:46:41 +03:00
|
|
|
Social._updateEnabledState(true);
|
2013-09-14 21:16:47 +04:00
|
|
|
Services.obs.notifyObservers(null, "social:" + topic, origin);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (topic == "provider-disabled") {
|
2016-03-18 02:46:41 +03:00
|
|
|
// a provider was removed from the list of providers, update states
|
2013-09-04 21:01:38 +04:00
|
|
|
Social._updateProviderCache(providers);
|
2016-03-18 02:46:41 +03:00
|
|
|
Social._updateEnabledState(providers.length > 0);
|
2013-09-04 21:01:38 +04:00
|
|
|
Services.obs.notifyObservers(null, "social:" + topic, origin);
|
2013-07-03 21:24:16 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (topic == "provider-update") {
|
2013-08-21 22:32:46 +04:00
|
|
|
// a provider has self-updated its manifest, we need to update our cache
|
|
|
|
// and reload the provider.
|
2013-09-04 21:01:38 +04:00
|
|
|
Social._updateProviderCache(providers);
|
|
|
|
let provider = Social._getProviderFromOrigin(origin);
|
|
|
|
provider.reload();
|
2013-02-08 23:39:25 +04:00
|
|
|
}
|
2013-08-21 22:32:46 +04:00
|
|
|
});
|
2014-03-12 06:25:45 +04:00
|
|
|
return deferred.promise;
|
2013-08-21 22:32:46 +04:00
|
|
|
},
|
|
|
|
|
2016-03-18 02:46:41 +03:00
|
|
|
_updateEnabledState: function(enable) {
|
2015-12-17 21:31:08 +03:00
|
|
|
for (let p of Social.providers) {
|
|
|
|
p.enabled = enable;
|
|
|
|
}
|
2012-12-08 08:13:27 +04:00
|
|
|
},
|
|
|
|
|
|
|
|
// Called to update our cache of providers and set the current provider
|
2013-02-20 05:23:47 +04:00
|
|
|
_updateProviderCache: function (providers) {
|
2012-12-08 08:13:27 +04:00
|
|
|
this.providers = providers;
|
2013-09-14 21:16:47 +04:00
|
|
|
Services.obs.notifyObservers(null, "social:providers-changed", null);
|
2013-02-07 18:36:31 +04:00
|
|
|
},
|
2013-08-21 22:32:46 +04:00
|
|
|
|
2012-06-23 02:01:34 +04:00
|
|
|
get enabled() {
|
2014-03-12 06:25:45 +04:00
|
|
|
return !this._disabledForSafeMode && this.providers.length > 0;
|
2012-07-18 22:40:05 +04:00
|
|
|
},
|
|
|
|
|
2012-12-08 08:13:27 +04:00
|
|
|
_getProviderFromOrigin: function (origin) {
|
|
|
|
for (let p of this.providers) {
|
|
|
|
if (p.origin == origin) {
|
|
|
|
return p;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
},
|
|
|
|
|
2013-09-04 21:01:38 +04:00
|
|
|
getManifestByOrigin: function(origin) {
|
|
|
|
return SocialService.getManifestByOrigin(origin);
|
|
|
|
},
|
|
|
|
|
2014-11-06 04:22:27 +03:00
|
|
|
installProvider: function(data, installCallback, options={}) {
|
|
|
|
SocialService.installProvider(data, installCallback, options);
|
2013-03-05 23:24:30 +04:00
|
|
|
},
|
|
|
|
|
2013-08-27 20:18:35 +04:00
|
|
|
uninstallProvider: function(origin, aCallback) {
|
|
|
|
SocialService.uninstallProvider(origin, aCallback);
|
2013-03-05 23:24:30 +04:00
|
|
|
},
|
|
|
|
|
2012-12-08 08:13:27 +04:00
|
|
|
// Activation functionality
|
2013-02-08 23:39:25 +04:00
|
|
|
activateFromOrigin: function (origin, callback) {
|
2014-07-18 21:27:52 +04:00
|
|
|
// It's OK if the provider has already been activated - we still get called
|
|
|
|
// back with it.
|
|
|
|
SocialService.enableProvider(origin, callback);
|
2013-01-27 00:07:39 +04:00
|
|
|
}
|
2012-07-12 05:31:19 +04:00
|
|
|
};
|
|
|
|
|
2014-12-23 03:11:56 +03:00
|
|
|
function sizeSocialPanelToContent(panel, iframe, requestedSize) {
|
2013-09-06 21:56:01 +04:00
|
|
|
let doc = iframe.contentDocument;
|
|
|
|
if (!doc || !doc.body) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// We need an element to use for sizing our panel. See if the body defines
|
|
|
|
// an id for that element, otherwise use the body itself.
|
|
|
|
let body = doc.body;
|
2014-12-23 03:11:56 +03:00
|
|
|
let docEl = doc.documentElement;
|
2013-09-06 21:56:01 +04:00
|
|
|
let bodyId = body.getAttribute("contentid");
|
|
|
|
if (bodyId) {
|
|
|
|
body = doc.getElementById(bodyId) || doc.body;
|
|
|
|
}
|
|
|
|
// offsetHeight/Width don't include margins, so account for that.
|
|
|
|
let cs = doc.defaultView.getComputedStyle(body);
|
2014-12-23 03:11:56 +03:00
|
|
|
let width = Math.max(PANEL_MIN_WIDTH, docEl.offsetWidth);
|
|
|
|
let height = Math.max(PANEL_MIN_HEIGHT, docEl.offsetHeight);
|
2013-09-06 21:56:01 +04:00
|
|
|
// if the panel is preloaded prior to being shown, cs will be null. in that
|
|
|
|
// case use the minimum size for the panel until it is shown.
|
|
|
|
if (cs) {
|
|
|
|
let computedHeight = parseInt(cs.marginTop) + body.offsetHeight + parseInt(cs.marginBottom);
|
|
|
|
height = Math.max(computedHeight, height);
|
|
|
|
let computedWidth = parseInt(cs.marginLeft) + body.offsetWidth + parseInt(cs.marginRight);
|
|
|
|
width = Math.max(computedWidth, width);
|
|
|
|
}
|
2014-10-20 19:49:20 +04:00
|
|
|
|
2014-12-23 03:11:56 +03:00
|
|
|
// if our scrollHeight is still larger than the iframe, the css calculations
|
|
|
|
// above did not work for this site, increase the height. This can happen if
|
|
|
|
// the site increases its height for additional UI.
|
|
|
|
if (docEl.scrollHeight > iframe.boxObject.height)
|
|
|
|
height = docEl.scrollHeight;
|
|
|
|
|
|
|
|
// if a size was defined in the manifest use it as a minimum
|
|
|
|
if (requestedSize) {
|
|
|
|
if (requestedSize.height)
|
|
|
|
height = Math.max(height, requestedSize.height);
|
|
|
|
if (requestedSize.width)
|
|
|
|
width = Math.max(width, requestedSize.width);
|
|
|
|
}
|
|
|
|
|
|
|
|
// add the extra space used by the panel (toolbar, borders, etc) if the iframe
|
|
|
|
// has been loaded
|
2014-10-20 19:49:20 +04:00
|
|
|
if (iframe.boxObject.width && iframe.boxObject.height) {
|
|
|
|
// add extra space the panel needs if any
|
|
|
|
width += panel.boxObject.width - iframe.boxObject.width;
|
|
|
|
height += panel.boxObject.height - iframe.boxObject.height;
|
|
|
|
}
|
2014-05-13 22:10:47 +04:00
|
|
|
|
2014-12-23 03:11:56 +03:00
|
|
|
// using panel.sizeTo will ignore css transitions, set size via style
|
|
|
|
if (Math.abs(panel.boxObject.width - width) >= 2)
|
|
|
|
panel.style.width = width + "px";
|
|
|
|
if (Math.abs(panel.boxObject.height - height) >= 2)
|
|
|
|
panel.style.height = height + "px";
|
2013-09-06 21:56:01 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
function DynamicResizeWatcher() {
|
|
|
|
this._mutationObserver = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
DynamicResizeWatcher.prototype = {
|
2014-12-23 03:11:56 +03:00
|
|
|
start: function DynamicResizeWatcher_start(panel, iframe, requestedSize) {
|
2013-09-06 21:56:01 +04:00
|
|
|
this.stop(); // just in case...
|
|
|
|
let doc = iframe.contentDocument;
|
2014-12-23 03:11:56 +03:00
|
|
|
this._mutationObserver = new iframe.contentWindow.MutationObserver((mutations) => {
|
|
|
|
sizeSocialPanelToContent(panel, iframe, requestedSize);
|
2013-09-06 21:56:01 +04:00
|
|
|
});
|
|
|
|
// Observe anything that causes the size to change.
|
|
|
|
let config = {attributes: true, characterData: true, childList: true, subtree: true};
|
|
|
|
this._mutationObserver.observe(doc, config);
|
|
|
|
// and since this may be setup after the load event has fired we do an
|
|
|
|
// initial resize now.
|
2014-12-23 03:11:56 +03:00
|
|
|
sizeSocialPanelToContent(panel, iframe, requestedSize);
|
2013-09-06 21:56:01 +04:00
|
|
|
},
|
|
|
|
stop: function DynamicResizeWatcher_stop() {
|
|
|
|
if (this._mutationObserver) {
|
|
|
|
try {
|
|
|
|
this._mutationObserver.disconnect();
|
|
|
|
} catch (ex) {
|
|
|
|
// may get "TypeError: can't access dead object" which seems strange,
|
|
|
|
// but doesn't seem to indicate a real problem, so ignore it...
|
|
|
|
}
|
|
|
|
this._mutationObserver = null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-05-07 10:02:58 +04:00
|
|
|
this.OpenGraphBuilder = {
|
2013-09-06 21:56:01 +04:00
|
|
|
generateEndpointURL: function(URLTemplate, pageData) {
|
|
|
|
// support for existing oexchange style endpoints by supporting their
|
|
|
|
// querystring arguments. parse the query string template and do
|
|
|
|
// replacements where necessary the query names may be different than ours,
|
|
|
|
// so we could see u=%{url} or url=%{url}
|
|
|
|
let [endpointURL, queryString] = URLTemplate.split("?");
|
|
|
|
let query = {};
|
|
|
|
if (queryString) {
|
|
|
|
queryString.split('&').forEach(function (val) {
|
|
|
|
let [name, value] = val.split('=');
|
|
|
|
let p = /%\{(.+)\}/.exec(value);
|
|
|
|
if (!p) {
|
|
|
|
// preserve non-template query vars
|
|
|
|
query[name] = value;
|
|
|
|
} else if (pageData[p[1]]) {
|
2014-05-13 22:10:47 +04:00
|
|
|
if (p[1] == "previews")
|
|
|
|
query[name] = pageData[p[1]][0];
|
|
|
|
else
|
|
|
|
query[name] = pageData[p[1]];
|
2013-09-06 21:56:01 +04:00
|
|
|
} else if (p[1] == "body") {
|
|
|
|
// build a body for emailers
|
|
|
|
let body = "";
|
|
|
|
if (pageData.title)
|
|
|
|
body += pageData.title + "\n\n";
|
|
|
|
if (pageData.description)
|
|
|
|
body += pageData.description + "\n\n";
|
|
|
|
if (pageData.text)
|
|
|
|
body += pageData.text + "\n\n";
|
|
|
|
body += pageData.url;
|
|
|
|
query["body"] = body;
|
|
|
|
}
|
|
|
|
});
|
2015-03-10 18:38:15 +03:00
|
|
|
// if the url template doesn't have title and no text was provided, add the title as the text.
|
|
|
|
if (!query.text && !query.title && pageData.title) {
|
|
|
|
query.text = pageData.title;
|
|
|
|
}
|
2013-09-06 21:56:01 +04:00
|
|
|
}
|
|
|
|
var str = [];
|
|
|
|
for (let p in query)
|
|
|
|
str.push(p + "=" + encodeURIComponent(query[p]));
|
|
|
|
if (str.length)
|
|
|
|
endpointURL = endpointURL + "?" + str.join("&");
|
|
|
|
return endpointURL;
|
|
|
|
},
|
2013-05-07 10:02:58 +04:00
|
|
|
};
|