2015-10-16 01:15:04 +03:00
|
|
|
/* -*- Mode: indent-tabs-mode: nil; js-indent-level: 2 -*- */
|
|
|
|
/* vim: set sts=2 sw=2 et tw=80: */
|
|
|
|
"use strict";
|
|
|
|
|
2015-06-04 01:34:44 +03:00
|
|
|
XPCOMUtils.defineLazyModuleGetter(this, "CustomizableUI",
|
|
|
|
"resource:///modules/CustomizableUI.jsm");
|
|
|
|
|
2015-10-14 02:18:43 +03:00
|
|
|
Cu.import("resource://devtools/shared/event-emitter.js");
|
2015-06-04 01:34:44 +03:00
|
|
|
|
|
|
|
Cu.import("resource://gre/modules/ExtensionUtils.jsm");
|
2015-09-15 21:19:45 +03:00
|
|
|
var {
|
2015-06-04 01:34:44 +03:00
|
|
|
EventManager,
|
|
|
|
} = ExtensionUtils;
|
|
|
|
|
2016-01-16 02:14:25 +03:00
|
|
|
const XUL_NS = "http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul";
|
|
|
|
|
2015-06-04 01:34:44 +03:00
|
|
|
// WeakMap[Extension -> BrowserAction]
|
2015-09-15 21:19:45 +03:00
|
|
|
var browserActionMap = new WeakMap();
|
2015-06-04 01:34:44 +03:00
|
|
|
|
|
|
|
// Responsible for the browser_action section of the manifest as well
|
|
|
|
// as the associated popup.
|
2015-12-03 03:58:53 +03:00
|
|
|
function BrowserAction(options, extension) {
|
2015-06-04 01:34:44 +03:00
|
|
|
this.extension = extension;
|
2016-01-16 02:14:25 +03:00
|
|
|
|
|
|
|
let widgetId = makeWidgetId(extension.id);
|
|
|
|
this.id = `${widgetId}-browser-action`;
|
|
|
|
this.viewId = `PanelUI-webext-${widgetId}-browser-action-view`;
|
2015-06-04 01:34:44 +03:00
|
|
|
this.widget = null;
|
|
|
|
|
2015-12-02 07:37:41 +03:00
|
|
|
this.tabManager = TabManager.for(extension);
|
|
|
|
|
2015-10-16 01:14:49 +03:00
|
|
|
this.defaults = {
|
2015-10-28 12:27:29 +03:00
|
|
|
enabled: true,
|
2016-03-01 06:34:49 +03:00
|
|
|
title: options.default_title || extension.name,
|
2015-10-16 01:14:49 +03:00
|
|
|
badgeText: "",
|
|
|
|
badgeBackgroundColor: null,
|
2016-03-01 06:34:49 +03:00
|
|
|
icon: IconDetails.normalize({path: options.default_icon}, extension),
|
|
|
|
popup: options.default_popup || "",
|
2015-10-16 01:14:49 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
this.tabContext = new TabContext(tab => Object.create(this.defaults),
|
|
|
|
extension);
|
|
|
|
|
|
|
|
EventEmitter.decorate(this);
|
2015-06-04 01:34:44 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
BrowserAction.prototype = {
|
|
|
|
build() {
|
|
|
|
let widget = CustomizableUI.createWidget({
|
|
|
|
id: this.id,
|
2016-01-16 02:14:25 +03:00
|
|
|
viewId: this.viewId,
|
|
|
|
type: "view",
|
2015-06-04 01:34:44 +03:00
|
|
|
removable: true,
|
2016-01-16 02:14:25 +03:00
|
|
|
label: this.defaults.title || this.extension.name,
|
|
|
|
tooltiptext: this.defaults.title || "",
|
2015-06-04 01:34:44 +03:00
|
|
|
defaultArea: CustomizableUI.AREA_NAVBAR,
|
2016-01-16 02:14:25 +03:00
|
|
|
|
|
|
|
onBeforeCreated: document => {
|
|
|
|
let view = document.createElementNS(XUL_NS, "panelview");
|
|
|
|
view.id = this.viewId;
|
|
|
|
view.setAttribute("flex", "1");
|
|
|
|
|
|
|
|
document.getElementById("PanelUI-multiView").appendChild(view);
|
|
|
|
},
|
|
|
|
|
|
|
|
onDestroyed: document => {
|
|
|
|
let view = document.getElementById(this.viewId);
|
|
|
|
if (view) {
|
|
|
|
view.remove();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
onCreated: node => {
|
|
|
|
node.classList.add("badged-button");
|
2015-06-04 01:34:44 +03:00
|
|
|
node.setAttribute("constrain-size", "true");
|
|
|
|
|
2015-10-16 01:14:49 +03:00
|
|
|
this.updateButton(node, this.defaults);
|
2016-01-16 02:14:25 +03:00
|
|
|
},
|
2015-06-04 01:34:44 +03:00
|
|
|
|
2016-01-16 02:14:25 +03:00
|
|
|
onViewShowing: event => {
|
|
|
|
let document = event.target.ownerDocument;
|
2015-06-04 01:34:44 +03:00
|
|
|
let tabbrowser = document.defaultView.gBrowser;
|
|
|
|
|
2016-01-16 02:14:25 +03:00
|
|
|
let tab = tabbrowser.selectedTab;
|
|
|
|
let popupURL = this.getProperty(tab, "popup");
|
|
|
|
this.tabManager.addActiveTabPermission(tab);
|
|
|
|
|
|
|
|
// If the widget has a popup URL defined, we open a popup, but do not
|
|
|
|
// dispatch a click event to the extension.
|
|
|
|
// If it has no popup URL defined, we dispatch a click event, but do not
|
|
|
|
// open a popup.
|
|
|
|
if (popupURL) {
|
|
|
|
try {
|
|
|
|
new ViewPopup(this.extension, event.target, popupURL);
|
|
|
|
} catch (e) {
|
|
|
|
Cu.reportError(e);
|
|
|
|
event.preventDefault();
|
2015-06-04 01:34:44 +03:00
|
|
|
}
|
2016-01-16 02:14:25 +03:00
|
|
|
} else {
|
|
|
|
// This isn't not a hack, but it seems to provide the correct behavior
|
|
|
|
// with the fewest complications.
|
|
|
|
event.preventDefault();
|
|
|
|
this.emit("click");
|
|
|
|
}
|
2015-06-04 01:34:44 +03:00
|
|
|
},
|
|
|
|
});
|
|
|
|
|
2015-12-03 03:58:53 +03:00
|
|
|
this.tabContext.on("tab-select", // eslint-disable-line mozilla/balanced-listeners
|
|
|
|
(evt, tab) => { this.updateWindow(tab.ownerDocument.defaultView); });
|
2015-10-16 01:14:49 +03:00
|
|
|
|
|
|
|
this.widget = widget;
|
2015-08-30 03:15:22 +03:00
|
|
|
},
|
|
|
|
|
2015-10-16 01:14:49 +03:00
|
|
|
// Update the toolbar button |node| with the tab context data
|
|
|
|
// in |tabData|.
|
|
|
|
updateButton(node, tabData) {
|
2016-01-16 08:43:26 +03:00
|
|
|
let title = tabData.title || this.extension.name;
|
|
|
|
node.setAttribute("tooltiptext", title);
|
|
|
|
node.setAttribute("label", title);
|
2015-06-04 01:34:44 +03:00
|
|
|
|
2015-10-16 01:14:49 +03:00
|
|
|
if (tabData.badgeText) {
|
|
|
|
node.setAttribute("badge", tabData.badgeText);
|
2015-06-04 01:34:44 +03:00
|
|
|
} else {
|
|
|
|
node.removeAttribute("badge");
|
|
|
|
}
|
|
|
|
|
2015-10-28 12:27:29 +03:00
|
|
|
if (tabData.enabled) {
|
|
|
|
node.removeAttribute("disabled");
|
|
|
|
} else {
|
|
|
|
node.setAttribute("disabled", "true");
|
|
|
|
}
|
|
|
|
|
2015-06-04 01:34:44 +03:00
|
|
|
let badgeNode = node.ownerDocument.getAnonymousElementByAttribute(node,
|
2015-12-03 03:58:53 +03:00
|
|
|
"class", "toolbarbutton-badge");
|
2015-06-04 01:34:44 +03:00
|
|
|
if (badgeNode) {
|
2015-10-16 01:14:49 +03:00
|
|
|
let color = tabData.badgeBackgroundColor;
|
2015-06-04 01:34:44 +03:00
|
|
|
if (Array.isArray(color)) {
|
|
|
|
color = `rgb(${color[0]}, ${color[1]}, ${color[2]})`;
|
|
|
|
}
|
2015-10-16 01:15:04 +03:00
|
|
|
badgeNode.style.backgroundColor = color || "";
|
2015-06-04 01:34:44 +03:00
|
|
|
}
|
|
|
|
|
2015-10-16 01:14:49 +03:00
|
|
|
let iconURL = IconDetails.getURL(
|
|
|
|
tabData.icon, node.ownerDocument.defaultView, this.extension);
|
2015-06-04 01:34:44 +03:00
|
|
|
node.setAttribute("image", iconURL);
|
|
|
|
},
|
|
|
|
|
|
|
|
// Update the toolbar button for a given window.
|
|
|
|
updateWindow(window) {
|
2015-10-16 01:14:49 +03:00
|
|
|
let widget = this.widget.forWindow(window);
|
|
|
|
if (widget) {
|
|
|
|
let tab = window.gBrowser.selectedTab;
|
|
|
|
this.updateButton(widget.node, this.tabContext.get(tab));
|
|
|
|
}
|
2015-06-04 01:34:44 +03:00
|
|
|
},
|
|
|
|
|
|
|
|
// Update the toolbar button when the extension changes the icon,
|
|
|
|
// title, badge, etc. If it only changes a parameter for a single
|
|
|
|
// tab, |tab| will be that tab. Otherwise it will be null.
|
|
|
|
updateOnChange(tab) {
|
|
|
|
if (tab) {
|
|
|
|
if (tab.selected) {
|
|
|
|
this.updateWindow(tab.ownerDocument.defaultView);
|
|
|
|
}
|
|
|
|
} else {
|
2015-10-16 01:14:49 +03:00
|
|
|
for (let window of WindowListManager.browserWindows()) {
|
|
|
|
this.updateWindow(window);
|
2015-06-04 01:34:44 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
// tab is allowed to be null.
|
|
|
|
// prop should be one of "icon", "title", "badgeText", "popup", or "badgeBackgroundColor".
|
|
|
|
setProperty(tab, prop, value) {
|
2015-10-16 01:14:49 +03:00
|
|
|
if (tab == null) {
|
|
|
|
this.defaults[prop] = value;
|
2016-01-16 08:43:26 +03:00
|
|
|
} else if (value != null) {
|
2016-01-18 11:08:35 +03:00
|
|
|
this.tabContext.get(tab)[prop] = value;
|
2016-01-16 08:43:26 +03:00
|
|
|
} else {
|
|
|
|
delete this.tabContext.get(tab)[prop];
|
2015-10-16 01:14:49 +03:00
|
|
|
}
|
|
|
|
|
2015-06-04 01:34:44 +03:00
|
|
|
this.updateOnChange(tab);
|
|
|
|
},
|
|
|
|
|
|
|
|
// tab is allowed to be null.
|
|
|
|
// prop should be one of "title", "badgeText", "popup", or "badgeBackgroundColor".
|
|
|
|
getProperty(tab, prop) {
|
2015-10-16 01:14:49 +03:00
|
|
|
if (tab == null) {
|
|
|
|
return this.defaults[prop];
|
|
|
|
} else {
|
|
|
|
return this.tabContext.get(tab)[prop];
|
|
|
|
}
|
2015-06-04 01:34:44 +03:00
|
|
|
},
|
|
|
|
|
|
|
|
shutdown() {
|
2015-10-16 01:14:49 +03:00
|
|
|
this.tabContext.shutdown();
|
2015-06-04 01:34:44 +03:00
|
|
|
CustomizableUI.destroyWidget(this.id);
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
2016-03-14 16:54:57 +03:00
|
|
|
BrowserAction.for = (extension) => {
|
|
|
|
return browserActionMap.get(extension);
|
|
|
|
};
|
|
|
|
|
|
|
|
global.browserActionFor = BrowserAction.for;
|
|
|
|
|
2015-12-03 03:58:53 +03:00
|
|
|
/* eslint-disable mozilla/balanced-listeners */
|
2015-06-04 01:34:44 +03:00
|
|
|
extensions.on("manifest_browser_action", (type, directive, extension, manifest) => {
|
|
|
|
let browserAction = new BrowserAction(manifest.browser_action, extension);
|
|
|
|
browserAction.build();
|
|
|
|
browserActionMap.set(extension, browserAction);
|
|
|
|
});
|
|
|
|
|
|
|
|
extensions.on("shutdown", (type, extension) => {
|
|
|
|
if (browserActionMap.has(extension)) {
|
|
|
|
browserActionMap.get(extension).shutdown();
|
|
|
|
browserActionMap.delete(extension);
|
|
|
|
}
|
|
|
|
});
|
2015-12-03 03:58:53 +03:00
|
|
|
/* eslint-enable mozilla/balanced-listeners */
|
2015-06-04 01:34:44 +03:00
|
|
|
|
2015-11-22 19:59:01 +03:00
|
|
|
extensions.registerSchemaAPI("browserAction", null, (extension, context) => {
|
2015-06-04 01:34:44 +03:00
|
|
|
return {
|
|
|
|
browserAction: {
|
|
|
|
onClicked: new EventManager(context, "browserAction.onClicked", fire => {
|
|
|
|
let listener = () => {
|
|
|
|
let tab = TabManager.activeTab;
|
|
|
|
fire(TabManager.convert(extension, tab));
|
|
|
|
};
|
2016-03-14 16:54:57 +03:00
|
|
|
BrowserAction.for(extension).on("click", listener);
|
2015-06-04 01:34:44 +03:00
|
|
|
return () => {
|
2016-03-14 16:54:57 +03:00
|
|
|
BrowserAction.for(extension).off("click", listener);
|
2015-06-04 01:34:44 +03:00
|
|
|
};
|
|
|
|
}).api(),
|
|
|
|
|
2015-10-28 12:27:29 +03:00
|
|
|
enable: function(tabId) {
|
2015-11-22 19:59:01 +03:00
|
|
|
let tab = tabId !== null ? TabManager.getTab(tabId) : null;
|
2016-03-14 16:54:57 +03:00
|
|
|
BrowserAction.for(extension).setProperty(tab, "enabled", true);
|
2015-10-28 12:27:29 +03:00
|
|
|
},
|
|
|
|
|
|
|
|
disable: function(tabId) {
|
2015-11-22 19:59:01 +03:00
|
|
|
let tab = tabId !== null ? TabManager.getTab(tabId) : null;
|
2016-03-14 16:54:57 +03:00
|
|
|
BrowserAction.for(extension).setProperty(tab, "enabled", false);
|
2015-10-28 12:27:29 +03:00
|
|
|
},
|
|
|
|
|
2015-06-04 01:34:44 +03:00
|
|
|
setTitle: function(details) {
|
2015-11-22 19:59:01 +03:00
|
|
|
let tab = details.tabId !== null ? TabManager.getTab(details.tabId) : null;
|
2016-01-16 08:43:26 +03:00
|
|
|
|
|
|
|
let title = details.title;
|
|
|
|
// Clear the tab-specific title when given a null string.
|
|
|
|
if (tab && title == "") {
|
|
|
|
title = null;
|
|
|
|
}
|
2016-03-14 16:54:57 +03:00
|
|
|
BrowserAction.for(extension).setProperty(tab, "title", title);
|
2015-06-04 01:34:44 +03:00
|
|
|
},
|
|
|
|
|
2016-02-02 05:14:05 +03:00
|
|
|
getTitle: function(details) {
|
2015-11-22 19:59:01 +03:00
|
|
|
let tab = details.tabId !== null ? TabManager.getTab(details.tabId) : null;
|
2016-03-14 16:54:57 +03:00
|
|
|
let title = BrowserAction.for(extension).getProperty(tab, "title");
|
2016-02-02 05:14:05 +03:00
|
|
|
return Promise.resolve(title);
|
2015-06-04 01:34:44 +03:00
|
|
|
},
|
|
|
|
|
2016-02-02 05:14:05 +03:00
|
|
|
setIcon: function(details) {
|
2015-11-22 19:59:01 +03:00
|
|
|
let tab = details.tabId !== null ? TabManager.getTab(details.tabId) : null;
|
2015-10-16 01:15:04 +03:00
|
|
|
let icon = IconDetails.normalize(details, extension, context);
|
2016-03-14 16:54:57 +03:00
|
|
|
BrowserAction.for(extension).setProperty(tab, "icon", icon);
|
2016-02-02 05:14:05 +03:00
|
|
|
return Promise.resolve();
|
2015-06-04 01:34:44 +03:00
|
|
|
},
|
|
|
|
|
|
|
|
setBadgeText: function(details) {
|
2015-11-22 19:59:01 +03:00
|
|
|
let tab = details.tabId !== null ? TabManager.getTab(details.tabId) : null;
|
2016-03-14 16:54:57 +03:00
|
|
|
BrowserAction.for(extension).setProperty(tab, "badgeText", details.text);
|
2015-06-04 01:34:44 +03:00
|
|
|
},
|
|
|
|
|
2016-02-02 05:14:05 +03:00
|
|
|
getBadgeText: function(details) {
|
2015-11-22 19:59:01 +03:00
|
|
|
let tab = details.tabId !== null ? TabManager.getTab(details.tabId) : null;
|
2016-03-14 16:54:57 +03:00
|
|
|
let text = BrowserAction.for(extension).getProperty(tab, "badgeText");
|
2016-02-02 05:14:05 +03:00
|
|
|
return Promise.resolve(text);
|
2015-06-04 01:34:44 +03:00
|
|
|
},
|
|
|
|
|
|
|
|
setPopup: function(details) {
|
2015-11-22 19:59:01 +03:00
|
|
|
let tab = details.tabId !== null ? TabManager.getTab(details.tabId) : null;
|
2015-10-16 01:14:49 +03:00
|
|
|
// Note: Chrome resolves arguments to setIcon relative to the calling
|
|
|
|
// context, but resolves arguments to setPopup relative to the extension
|
|
|
|
// root.
|
|
|
|
// For internal consistency, we currently resolve both relative to the
|
|
|
|
// calling context.
|
|
|
|
let url = details.popup && context.uri.resolve(details.popup);
|
2016-03-14 16:54:57 +03:00
|
|
|
BrowserAction.for(extension).setProperty(tab, "popup", url);
|
2015-06-04 01:34:44 +03:00
|
|
|
},
|
|
|
|
|
2016-02-02 05:14:05 +03:00
|
|
|
getPopup: function(details) {
|
2015-11-22 19:59:01 +03:00
|
|
|
let tab = details.tabId !== null ? TabManager.getTab(details.tabId) : null;
|
2016-03-14 16:54:57 +03:00
|
|
|
let popup = BrowserAction.for(extension).getProperty(tab, "popup");
|
2016-02-02 05:14:05 +03:00
|
|
|
return Promise.resolve(popup);
|
2015-06-04 01:34:44 +03:00
|
|
|
},
|
|
|
|
|
|
|
|
setBadgeBackgroundColor: function(details) {
|
2015-11-22 19:59:01 +03:00
|
|
|
let tab = details.tabId !== null ? TabManager.getTab(details.tabId) : null;
|
2016-03-14 16:54:57 +03:00
|
|
|
BrowserAction.for(extension).setProperty(tab, "badgeBackgroundColor", details.color);
|
2015-06-04 01:34:44 +03:00
|
|
|
},
|
|
|
|
|
|
|
|
getBadgeBackgroundColor: function(details, callback) {
|
2015-11-22 19:59:01 +03:00
|
|
|
let tab = details.tabId !== null ? TabManager.getTab(details.tabId) : null;
|
2016-03-14 16:54:57 +03:00
|
|
|
let color = BrowserAction.for(extension).getProperty(tab, "badgeBackgroundColor");
|
2016-02-02 05:14:05 +03:00
|
|
|
return Promise.resolve(color);
|
2015-06-04 01:34:44 +03:00
|
|
|
},
|
2015-12-03 03:58:53 +03:00
|
|
|
},
|
2015-06-04 01:34:44 +03:00
|
|
|
};
|
|
|
|
});
|