Skip to content

Commit

Permalink
Bug 1014332 enable share button by default with default selection of …
Browse files Browse the repository at this point in the history
…share providers, r=jaws
  • Loading branch information
mixedpuppy committed Aug 15, 2014
1 parent be330ee commit 405e400
Show file tree
Hide file tree
Showing 13 changed files with 307 additions and 123 deletions.
9 changes: 9 additions & 0 deletions browser/app/profile/firefox.js
Original file line number Diff line number Diff line change
Expand Up @@ -1588,6 +1588,15 @@ pref("services.push.serverURL", "wss://push.services.mozilla.com/");

pref("social.sidebar.unload_timeout_ms", 10000);

// activation from inside of share panel is possible if activationPanelEnabled
// is true. Pref'd off for release while usage testing is done through beta.
#ifdef RELEASE_BUILD
pref("social.share.activationPanelEnabled", false);
#else
pref("social.share.activationPanelEnabled", true);
#endif
pref("social.shareDirectory", "https://activations.cdn.mozilla.net/en-US/sharePanel.html");

pref("dom.identity.enabled", false);

// Block insecure active content on https pages
Expand Down
23 changes: 12 additions & 11 deletions browser/base/content/aboutSocialError.xhtml
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,17 @@

function parseQueryString() {
let url = document.documentURI;
let queryString = url.replace(/^about:socialerror\??/, "");

let modeMatch = queryString.match(/mode=([^&]+)/);
let mode = modeMatch && modeMatch[1] ? modeMatch[1] : "";
let originMatch = queryString.match(/origin=([^&]+)/);
config.origin = originMatch && originMatch[1] ? decodeURIComponent(originMatch[1]) : "";
var searchParams = new URLSearchParams(url);

let mode = searchParams.get("mode");
config.directory = searchParams.get("directory");
config.origin = searchParams.get("origin");
let encodedURL = searchParams.get("url");
let url = decodeURIComponent(encodedURL);
if (config.directory) {
let URI = Services.io.newURI(url, null, null);
config.origin = Services.scriptSecurityManager.getNoAppCodebasePrincipal(URI).origin;
}

switch (mode) {
case "compactInfo":
Expand All @@ -59,10 +64,6 @@
document.getElementById("btnCloseSidebar").style.display = 'none';
//intentional fall-through
case "tryAgain":
let urlMatch = queryString.match(/url=([^&]+)/);
let encodedURL = urlMatch && urlMatch[1] ? urlMatch[1] : "";
let url = decodeURIComponent(encodedURL);

config.tryAgainCallback = loadQueryURL;
config.queryURL = url;
break;
Expand All @@ -80,7 +81,7 @@

let productName = brandBundle.GetStringFromName("brandShortName");
let provider = Social._getProviderFromOrigin(config.origin);
let providerName = provider && provider.name;
let providerName = provider ? provider.name : config.origin;

// Sets up the error message
let msg = browserBundle.formatStringFromName("social.error.message", [productName, providerName], 2);
Expand Down
2 changes: 1 addition & 1 deletion browser/base/content/browser-sets.inc
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@
#endif
<command id="History:UndoCloseTab" oncommand="undoCloseTab();"/>
<command id="History:UndoCloseWindow" oncommand="undoCloseWindow();"/>
<command id="Social:SharePage" oncommand="SocialShare.sharePage();" disabled="true"/>
<command id="Social:SharePage" oncommand="SocialShare.sharePage();"/>
<command id="Social:ToggleSidebar" oncommand="SocialSidebar.toggleSidebar();" hidden="true"/>
<command id="Social:ToggleNotifications" oncommand="Social.toggleNotifications();" hidden="true"/>
<command id="Social:Addons" oncommand="BrowserOpenAddonsMgr('addons://list/service');"/>
Expand Down
163 changes: 117 additions & 46 deletions browser/base/content/browser-social.js
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ SocialUI = {
// about:home or the share panel, we bypass the enable prompt. Any website
// activation, such as from the activations directory or a providers website
// will still get the prompt.
_activationEventHandler: function SocialUI_activationHandler(e, aBypassUserEnable=false) {
_activationEventHandler: function SocialUI_activationHandler(e, options={}) {
let targetDoc;
let node;
if (e.target instanceof HTMLDocument) {
Expand All @@ -197,7 +197,9 @@ SocialUI = {
if (!(targetDoc instanceof HTMLDocument))
return;

if (!aBypassUserEnable && targetDoc.defaultView != content)
// The share panel iframe will not match "content" so it passes a bypass
// flag
if (!options.bypassContentCheck && targetDoc.defaultView != content)
return;

// If we are in PB mode, we silently do nothing (bug 829404 exists to
Expand Down Expand Up @@ -233,11 +235,25 @@ SocialUI = {
if (provider.sidebarURL) {
SocialSidebar.show(provider.origin);
}
if (provider.shareURL) {
// make this new provider the selected provider. If the panel hasn't
// been opened, we need to make the frame first.
SocialShare._createFrame();
SocialShare.iframe.setAttribute('src', 'data:text/plain;charset=utf8,');
SocialShare.iframe.setAttribute('origin', provider.origin);
// get the right button selected
SocialShare.populateProviderMenu();
if (SocialShare.panel.state == "open") {
SocialShare.sharePage(provider.origin);
}
}
if (provider.postActivationURL) {
openUILinkIn(provider.postActivationURL, "tab");
// if activated from an open share panel, we load the landing page in
// a background tab
gBrowser.loadOneTab(provider.postActivationURL, {inBackground: SocialShare.panel.state == "open"});
}
});
}, aBypassUserEnable);
}, options);
},

showLearnMore: function() {
Expand Down Expand Up @@ -290,10 +306,10 @@ SocialUI = {
// called on tab/urlbar/location changes and after customization. Update
// anything that is tab specific.
updateState: function() {
SocialShare.update();
if (!SocialUI.enabled)
return;
SocialMarks.update();
SocialShare.update();
}
}

Expand Down Expand Up @@ -434,6 +450,12 @@ SocialFlyout = {
}

SocialShare = {
get _dynamicResizer() {
delete this._dynamicResizer;
this._dynamicResizer = new DynamicResizeWatcher();
return this._dynamicResizer;
},

// Share panel may be attached to the overflow or menu button depending on
// customization, we need to manage open state of the anchor.
get anchor() {
Expand All @@ -452,15 +474,27 @@ SocialShare = {
return this.panel.lastChild;
},

get activationPanelEnabled () {
// ability to pref off for release
return Services.prefs.getBoolPref("social.share.activationPanelEnabled");
},

_activationHandler: function(event) {
if (!SocialShare.activationPanelEnabled)
return;
SocialUI._activationEventHandler(event, { bypassContentCheck: true, bypassInstallPanel: true });
},

uninit: function () {
if (this.iframe) {
this.iframe.removeEventListener("ActivateSocialFeature", this._activationHandler, true, true);
this.iframe.remove();
}
},

_createFrame: function() {
let panel = this.panel;
if (!SocialUI.enabled || this.iframe)
if (this.iframe)
return;
this.panel.hidden = false;
// create and initialize the panel for this window
Expand All @@ -472,6 +506,7 @@ SocialShare = {
iframe.setAttribute("disableglobalhistory", "true");
iframe.setAttribute("flex", "1");
panel.appendChild(iframe);
this.iframe.addEventListener("ActivateSocialFeature", this._activationHandler, true, true);
this.populateProviderMenu();
},

Expand All @@ -481,11 +516,19 @@ SocialShare = {
if (lastProviderOrigin) {
provider = Social._getProviderFromOrigin(lastProviderOrigin);
}
// if we are able to activate a provider we don't need to do anything fancy
// here, the user will land on the activation panel if no previously
// selected provider is available.
if (this.activationPanelEnabled)
return provider;

// if they have a provider selected in the sidebar use that for the initial
// default in share
if (!provider)
provider = SocialSidebar.provider;
// if our provider has no shareURL, select the first one that does
// if our provider has no shareURL, select the first one that does. If we
// have no selected provider and activation is available, default to that
// panel.
if (!provider || !provider.shareURL) {
let providers = [p for (p of Social.providers) if (p.shareURL)];
provider = providers.length > 0 && providers[0];
Expand All @@ -498,17 +541,12 @@ SocialShare = {
return;
let providers = [p for (p of Social.providers) if (p.shareURL)];
let hbox = document.getElementById("social-share-provider-buttons");
// selectable providers are inserted before the provider-menu seperator,
// remove any menuitems in that area
while (hbox.firstChild) {
// remove everything before the add-share-provider button (which should also
// be lastChild if any share providers were added)
let addButton = document.getElementById("add-share-provider");
while (hbox.firstChild != addButton) {
hbox.removeChild(hbox.firstChild);
}
// reset our share toolbar
// only show a selection if there is more than one
if (!SocialUI.enabled || providers.length < 2) {
this.panel.firstChild.hidden = true;
return;
}
let selectedProvider = this.getSelectedProvider();
for (let provider of providers) {
let button = document.createElement("toolbarbutton");
Expand All @@ -518,17 +556,16 @@ SocialShare = {
button.setAttribute("image", provider.iconURL);
button.setAttribute("tooltiptext", provider.name);
button.setAttribute("origin", provider.origin);
button.setAttribute("oncommand", "SocialShare.sharePage(this.getAttribute('origin')); this.checked=true;");
button.setAttribute("oncommand", "SocialShare.sharePage(this.getAttribute('origin'));");
if (provider == selectedProvider) {
this.defaultButton = button;
}
hbox.appendChild(button);
hbox.insertBefore(button, addButton);
}
if (!this.defaultButton) {
this.defaultButton = hbox.firstChild
this.defaultButton = this.activationPanelEnabled ? addButton : hbox.firstChild;
}
this.defaultButton.setAttribute("checked", "true");
this.panel.firstChild.hidden = false;
},

get shareButton() {
Expand Down Expand Up @@ -560,8 +597,8 @@ SocialShare = {
let shareButton = widget.forWindow(window).node;
// hidden state is based on available share providers and location of
// button. It's always visible and disabled in the customization palette.
shareButton.hidden = !SocialUI.enabled || (widget.areaType &&
[p for (p of Social.providers) if (p.shareURL)].length == 0);
shareButton.hidden = !this.activationPanelEnabled && (!SocialUI.enabled || (widget.areaType &&
[p for (p of Social.providers) if (p.shareURL)].length == 0));
let disabled = !widget.areaType || shareButton.hidden || !this.canSharePage(gBrowser.currentURI);

// 1. update the relevent command's disabled state so the keyboard
Expand All @@ -577,6 +614,9 @@ SocialShare = {
cmd.removeAttribute("disabled");
shareButton.removeAttribute("disabled");
}

// enable or disable the activation panel
document.getElementById("add-share-provider").hidden = !this.activationPanelEnabled;
},

_onclick: function() {
Expand Down Expand Up @@ -608,10 +648,15 @@ SocialShare = {
if (!iframe)
return;

iframe.removeAttribute("src");
iframe.webNavigation.loadURI("about:socialerror?mode=compactInfo&origin=" +
encodeURIComponent(iframe.getAttribute("origin")),
null, null, null, null);
let url;
let origin = iframe.getAttribute("origin");
if (!origin && this.activationPanelEnabled) {
// directory site is down
url = "about:socialerror?mode=tryAgainOnly&directory=1&url=" + encodeURIComponent(iframe.getAttribute("src"));
} else {
url = "about:socialerror?mode=compactInfo&origin=" + encodeURIComponent(origin);
}
iframe.webNavigation.loadURI(url, null, null, null, null);
sizeSocialPanelToContent(this.panel, iframe);
},

Expand All @@ -621,13 +666,6 @@ SocialShare = {
// will call sharePage with an origin for us to switch to.
this._createFrame();
let iframe = this.iframe;
let provider;
if (providerOrigin)
provider = Social._getProviderFromOrigin(providerOrigin);
else
provider = this.getSelectedProvider();
if (!provider || !provider.shareURL)
return;

// graphData is an optional param that either defines the full set of data
// to be shared, or partial data about the current page. It is set by a call
Expand Down Expand Up @@ -659,20 +697,25 @@ SocialShare = {
}
this.currentShare = pageData;

let provider;
if (providerOrigin)
provider = Social._getProviderFromOrigin(providerOrigin);
else
provider = this.getSelectedProvider();
if (!provider || !provider.shareURL) {
this.showDirectory();
return;
}
// check the menu button
let hbox = document.getElementById("social-share-provider-buttons");
let btn = hbox.querySelector("[origin='" + provider.origin + "']");
btn.checked = true;

let shareEndpoint = OpenGraphBuilder.generateEndpointURL(provider.shareURL, pageData);

let size = provider.getPageSize("share");
if (size) {
if (this._dynamicResizer) {
this._dynamicResizer.stop();
this._dynamicResizer = null;
}
let {width, height} = size;
width += this.panel.boxObject.width - iframe.boxObject.width;
height += this.panel.boxObject.height - iframe.boxObject.height;
this.panel.sizeTo(width, height);
} else {
this._dynamicResizer = new DynamicResizeWatcher();
this._dynamicResizer.stop();
}

// if we've already loaded this provider/page share endpoint, we don't want
Expand All @@ -684,7 +727,7 @@ SocialShare = {
reload = shareEndpoint != iframe.contentDocument.location.spec;
}
if (!reload) {
if (this._dynamicResizer)
if (!size)
this._dynamicResizer.start(this.panel, iframe);
iframe.docShell.isActive = true;
iframe.docShell.isAppTab = true;
Expand All @@ -702,7 +745,13 @@ SocialShare = {
// should close the window when done.
iframe.contentWindow.opener = iframe.contentWindow;
setTimeout(function() {
if (SocialShare._dynamicResizer) { // may go null if hidden quickly
if (size) {
let panel = SocialShare.panel;
let {width, height} = size;
width += panel.boxObject.width - iframe.boxObject.width;
height += panel.boxObject.height - iframe.boxObject.height;
panel.sizeTo(width, height);
} else {
SocialShare._dynamicResizer.start(iframe.parentNode, iframe);
}
}, 0);
Expand All @@ -723,10 +772,32 @@ SocialShare = {
let uri = Services.io.newURI(shareEndpoint, null, null);
iframe.setAttribute("origin", provider.origin);
iframe.setAttribute("src", shareEndpoint);
this._openPanel();
},

showDirectory: function() {
let url = Services.prefs.getCharPref("social.shareDirectory");
this._createFrame();
let iframe = this.iframe;
iframe.removeAttribute("origin");
iframe.setAttribute("src", url);
iframe.addEventListener("load", function panelBrowserOnload(e) {
iframe.removeEventListener("load", panelBrowserOnload, true);
SocialShare._dynamicResizer.start(iframe.parentNode, iframe);

iframe.addEventListener("unload", function panelBrowserOnload(e) {
iframe.removeEventListener("unload", panelBrowserOnload, true);
SocialShare._dynamicResizer.stop();
}, true);

}, true);
this._openPanel();
},

_openPanel: function() {
let anchor = document.getAnonymousElementByAttribute(this.anchor, "class", "toolbarbutton-icon");
this.panel.openPopup(anchor, "bottomcenter topright", 0, 0, false, false);
Social.setErrorListener(iframe, this.setErrorMessage.bind(this));
Social.setErrorListener(this.iframe, this.setErrorMessage.bind(this));
Services.telemetry.getHistogramById("SOCIAL_TOOLBAR_BUTTONS").add(0);
}
};
Expand Down
Loading

0 comments on commit 405e400

Please sign in to comment.