2013-05-29 14:50:59 -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/.
|
|
|
|
|
2013-12-10 23:35:11 -08:00
|
|
|
"use strict";
|
|
|
|
|
2013-05-29 14:50:59 -07:00
|
|
|
this.EXPORTED_SYMBOLS = ["UITour"];
|
|
|
|
|
|
|
|
const {classes: Cc, interfaces: Ci, utils: Cu, results: Cr} = Components;
|
|
|
|
|
|
|
|
Cu.import("resource://gre/modules/Services.jsm");
|
|
|
|
Cu.import("resource://gre/modules/XPCOMUtils.jsm");
|
2013-12-10 23:35:11 -08:00
|
|
|
Cu.import("resource://gre/modules/Promise.jsm");
|
2013-05-29 14:50:59 -07:00
|
|
|
|
|
|
|
XPCOMUtils.defineLazyModuleGetter(this, "LightweightThemeManager",
|
|
|
|
"resource://gre/modules/LightweightThemeManager.jsm");
|
|
|
|
XPCOMUtils.defineLazyModuleGetter(this, "PermissionsUtils",
|
|
|
|
"resource://gre/modules/PermissionsUtils.jsm");
|
2013-12-10 23:35:11 -08:00
|
|
|
XPCOMUtils.defineLazyModuleGetter(this, "CustomizableUI",
|
|
|
|
"resource:///modules/CustomizableUI.jsm");
|
2014-02-06 19:19:12 -08:00
|
|
|
XPCOMUtils.defineLazyModuleGetter(this, "UITelemetry",
|
|
|
|
"resource://gre/modules/UITelemetry.jsm");
|
|
|
|
XPCOMUtils.defineLazyModuleGetter(this, "BrowserUITelemetry",
|
|
|
|
"resource:///modules/BrowserUITelemetry.jsm");
|
2013-05-29 14:50:59 -07:00
|
|
|
|
|
|
|
|
|
|
|
const UITOUR_PERMISSION = "uitour";
|
|
|
|
const PREF_PERM_BRANCH = "browser.uitour.";
|
2014-02-27 01:25:44 -08:00
|
|
|
const PREF_SEENPAGEIDS = "browser.uitour.seenPageIDs";
|
2014-01-05 15:27:25 -08:00
|
|
|
const MAX_BUTTONS = 4;
|
2013-05-29 14:50:59 -07:00
|
|
|
|
2014-02-06 19:19:12 -08:00
|
|
|
const BUCKET_NAME = "UITour";
|
|
|
|
const BUCKET_TIMESTEPS = [
|
|
|
|
1 * 60 * 1000, // Until 1 minute after tab is closed/inactive.
|
|
|
|
3 * 60 * 1000, // Until 3 minutes after tab is closed/inactive.
|
|
|
|
10 * 60 * 1000, // Until 10 minutes after tab is closed/inactive.
|
|
|
|
60 * 60 * 1000, // Until 1 hour after tab is closed/inactive.
|
|
|
|
];
|
|
|
|
|
2014-02-27 01:25:44 -08:00
|
|
|
// Time after which seen Page IDs expire.
|
|
|
|
const SEENPAGEID_EXPIRY = 2 * 7 * 24 * 60 * 60 * 1000; // 2 weeks.
|
2014-02-06 19:19:12 -08:00
|
|
|
|
2013-05-29 14:50:59 -07:00
|
|
|
|
|
|
|
this.UITour = {
|
2014-03-05 17:41:43 -08:00
|
|
|
url: null,
|
2014-02-27 01:25:44 -08:00
|
|
|
seenPageIDs: null,
|
2014-02-06 19:19:12 -08:00
|
|
|
pageIDSourceTabs: new WeakMap(),
|
|
|
|
pageIDSourceWindows: new WeakMap(),
|
2014-02-20 14:42:33 -08:00
|
|
|
/* Map from browser windows to a set of tabs in which a tour is open */
|
2013-05-29 14:50:59 -07:00
|
|
|
originTabs: new WeakMap(),
|
2014-02-20 14:42:33 -08:00
|
|
|
/* Map from browser windows to a set of pinned tabs opened by (a) tour(s) */
|
2013-05-29 14:50:59 -07:00
|
|
|
pinnedTabs: new WeakMap(),
|
|
|
|
urlbarCapture: new WeakMap(),
|
2013-12-10 23:35:11 -08:00
|
|
|
appMenuOpenForAnnotation: new Set(),
|
2014-03-03 17:39:35 -08:00
|
|
|
availableTargetsCache: new WeakMap(),
|
2013-05-29 14:50:59 -07:00
|
|
|
|
2014-02-20 14:42:33 -08:00
|
|
|
_detachingTab: false,
|
2014-03-05 21:59:00 -08:00
|
|
|
_annotationPanelMutationObservers: new WeakMap(),
|
2014-02-20 14:42:33 -08:00
|
|
|
_queuedEvents: [],
|
|
|
|
_pendingDoc: null,
|
|
|
|
|
2013-12-10 23:35:31 -08:00
|
|
|
highlightEffects: ["random", "wobble", "zoom", "color"],
|
2013-05-29 14:50:59 -07:00
|
|
|
targets: new Map([
|
2014-01-30 22:25:00 -08:00
|
|
|
["accountStatus", {
|
|
|
|
query: (aDocument) => {
|
|
|
|
let statusButton = aDocument.getElementById("PanelUI-fxa-status");
|
|
|
|
return aDocument.getAnonymousElementByAttribute(statusButton,
|
|
|
|
"class",
|
|
|
|
"toolbarbutton-icon");
|
|
|
|
},
|
|
|
|
widgetName: "PanelUI-fxa-status",
|
|
|
|
}],
|
2013-12-10 23:35:11 -08:00
|
|
|
["addons", {query: "#add-ons-button"}],
|
2014-03-13 06:29:24 -07:00
|
|
|
["appMenu", {query: "#PanelUI-button"}],
|
2013-12-10 23:35:11 -08:00
|
|
|
["backForward", {
|
|
|
|
query: "#back-button",
|
|
|
|
widgetName: "urlbar-container",
|
|
|
|
}],
|
|
|
|
["bookmarks", {query: "#bookmarks-menu-button"}],
|
|
|
|
["customize", {
|
|
|
|
query: (aDocument) => {
|
|
|
|
let customizeButton = aDocument.getElementById("PanelUI-customize");
|
|
|
|
return aDocument.getAnonymousElementByAttribute(customizeButton,
|
|
|
|
"class",
|
|
|
|
"toolbarbutton-icon");
|
|
|
|
},
|
|
|
|
widgetName: "PanelUI-customize",
|
|
|
|
}],
|
|
|
|
["help", {query: "#PanelUI-help"}],
|
|
|
|
["home", {query: "#home-button"}],
|
|
|
|
["quit", {query: "#PanelUI-quit"}],
|
|
|
|
["search", {
|
|
|
|
query: "#searchbar",
|
|
|
|
widgetName: "search-container",
|
|
|
|
}],
|
|
|
|
["searchProvider", {
|
|
|
|
query: (aDocument) => {
|
|
|
|
let searchbar = aDocument.getElementById("searchbar");
|
|
|
|
return aDocument.getAnonymousElementByAttribute(searchbar,
|
|
|
|
"anonid",
|
|
|
|
"searchbar-engine-button");
|
|
|
|
},
|
|
|
|
widgetName: "search-container",
|
|
|
|
}],
|
2014-01-13 07:28:18 -08:00
|
|
|
["selectedTabIcon", {
|
|
|
|
query: (aDocument) => {
|
|
|
|
let selectedtab = aDocument.defaultView.gBrowser.selectedTab;
|
|
|
|
let element = aDocument.getAnonymousElementByAttribute(selectedtab,
|
|
|
|
"anonid",
|
|
|
|
"tab-icon-image");
|
2014-03-03 17:39:35 -08:00
|
|
|
if (!element || !UITour.isElementVisible(element)) {
|
2014-01-13 07:28:18 -08:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
return element;
|
|
|
|
},
|
|
|
|
}],
|
2013-12-10 23:35:11 -08:00
|
|
|
["urlbar", {
|
|
|
|
query: "#urlbar",
|
|
|
|
widgetName: "urlbar-container",
|
2013-05-29 14:50:59 -07:00
|
|
|
}],
|
|
|
|
]),
|
|
|
|
|
2014-02-06 19:19:12 -08:00
|
|
|
init: function() {
|
2014-02-27 01:25:44 -08:00
|
|
|
// Lazy getter is initialized here so it can be replicated any time
|
|
|
|
// in a test.
|
|
|
|
delete this.seenPageIDs;
|
|
|
|
Object.defineProperty(this, "seenPageIDs", {
|
|
|
|
get: this.restoreSeenPageIDs.bind(this),
|
|
|
|
configurable: true,
|
|
|
|
});
|
|
|
|
|
2014-03-05 17:41:43 -08:00
|
|
|
delete this.url;
|
|
|
|
XPCOMUtils.defineLazyGetter(this, "url", function () {
|
|
|
|
return Services.urlFormatter.formatURLPref("browser.uitour.url");
|
|
|
|
});
|
|
|
|
|
2014-02-06 19:19:12 -08:00
|
|
|
UITelemetry.addSimpleMeasureFunction("UITour",
|
|
|
|
this.getTelemetry.bind(this));
|
2014-03-03 17:39:35 -08:00
|
|
|
|
|
|
|
// Clear the availableTargetsCache on widget changes.
|
|
|
|
let listenerMethods = [
|
|
|
|
"onWidgetAdded",
|
|
|
|
"onWidgetMoved",
|
|
|
|
"onWidgetRemoved",
|
|
|
|
"onWidgetReset",
|
|
|
|
"onAreaReset",
|
|
|
|
];
|
|
|
|
CustomizableUI.addListener(listenerMethods.reduce((listener, method) => {
|
|
|
|
listener[method] = () => this.availableTargetsCache.clear();
|
|
|
|
return listener;
|
|
|
|
}, {}));
|
2014-02-06 19:19:12 -08:00
|
|
|
},
|
|
|
|
|
2014-02-27 01:25:44 -08:00
|
|
|
restoreSeenPageIDs: function() {
|
|
|
|
delete this.seenPageIDs;
|
|
|
|
|
|
|
|
if (UITelemetry.enabled) {
|
|
|
|
let dateThreshold = Date.now() - SEENPAGEID_EXPIRY;
|
|
|
|
|
|
|
|
try {
|
|
|
|
let data = Services.prefs.getCharPref(PREF_SEENPAGEIDS);
|
|
|
|
data = new Map(JSON.parse(data));
|
|
|
|
|
|
|
|
for (let [pageID, details] of data) {
|
|
|
|
|
|
|
|
if (typeof pageID != "string" ||
|
|
|
|
typeof details != "object" ||
|
|
|
|
typeof details.lastSeen != "number" ||
|
|
|
|
details.lastSeen < dateThreshold) {
|
|
|
|
|
|
|
|
data.delete(pageID);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
this.seenPageIDs = data;
|
|
|
|
} catch (e) {}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!this.seenPageIDs)
|
|
|
|
this.seenPageIDs = new Map();
|
|
|
|
|
|
|
|
this.persistSeenIDs();
|
|
|
|
|
|
|
|
return this.seenPageIDs;
|
|
|
|
},
|
|
|
|
|
|
|
|
addSeenPageID: function(aPageID) {
|
|
|
|
if (!UITelemetry.enabled)
|
|
|
|
return;
|
|
|
|
|
|
|
|
this.seenPageIDs.set(aPageID, {
|
|
|
|
lastSeen: Date.now(),
|
|
|
|
});
|
|
|
|
|
|
|
|
this.persistSeenIDs();
|
|
|
|
},
|
|
|
|
|
|
|
|
persistSeenIDs: function() {
|
|
|
|
if (this.seenPageIDs.size === 0) {
|
|
|
|
Services.prefs.clearUserPref(PREF_SEENPAGEIDS);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
Services.prefs.setCharPref(PREF_SEENPAGEIDS,
|
|
|
|
JSON.stringify([...this.seenPageIDs]));
|
|
|
|
},
|
|
|
|
|
2013-05-29 14:50:59 -07:00
|
|
|
onPageEvent: function(aEvent) {
|
|
|
|
let contentDocument = null;
|
|
|
|
if (aEvent.target instanceof Ci.nsIDOMHTMLDocument)
|
|
|
|
contentDocument = aEvent.target;
|
|
|
|
else if (aEvent.target instanceof Ci.nsIDOMHTMLElement)
|
|
|
|
contentDocument = aEvent.target.ownerDocument;
|
|
|
|
else
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// Ignore events if they're not from a trusted origin.
|
|
|
|
if (!this.ensureTrustedOrigin(contentDocument))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (typeof aEvent.detail != "object")
|
|
|
|
return false;
|
|
|
|
|
|
|
|
let action = aEvent.detail.action;
|
|
|
|
if (typeof action != "string" || !action)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
let data = aEvent.detail.data;
|
|
|
|
if (typeof data != "object")
|
|
|
|
return false;
|
|
|
|
|
|
|
|
let window = this.getChromeWindow(contentDocument);
|
2014-02-20 14:42:33 -08:00
|
|
|
// Do this before bailing if there's no tab, so later we can pick up the pieces:
|
|
|
|
window.gBrowser.tabContainer.addEventListener("TabSelect", this);
|
2014-02-06 19:19:12 -08:00
|
|
|
let tab = window.gBrowser._getTabForContentWindow(contentDocument.defaultView);
|
2014-02-20 14:42:33 -08:00
|
|
|
if (!tab) {
|
|
|
|
// This should only happen while detaching a tab:
|
|
|
|
if (this._detachingTab) {
|
|
|
|
this._queuedEvents.push(aEvent);
|
|
|
|
this._pendingDoc = Cu.getWeakReference(contentDocument);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
Cu.reportError("Discarding tabless UITour event (" + action + ") while not detaching a tab." +
|
|
|
|
"This shouldn't happen!");
|
|
|
|
return;
|
|
|
|
}
|
2013-05-29 14:50:59 -07:00
|
|
|
|
|
|
|
switch (action) {
|
2014-02-06 19:19:12 -08:00
|
|
|
case "registerPageID": {
|
2014-02-27 01:25:44 -08:00
|
|
|
// This is only relevant if Telemtry is enabled.
|
|
|
|
if (!UITelemetry.enabled)
|
|
|
|
break;
|
|
|
|
|
2014-02-06 19:19:12 -08:00
|
|
|
// We don't want to allow BrowserUITelemetry.BUCKET_SEPARATOR in the
|
|
|
|
// pageID, as it could make parsing the telemetry bucket name difficult.
|
|
|
|
if (typeof data.pageID == "string" &&
|
|
|
|
!data.pageID.contains(BrowserUITelemetry.BUCKET_SEPARATOR)) {
|
2014-02-27 01:25:44 -08:00
|
|
|
this.addSeenPageID(data.pageID);
|
2014-02-06 19:19:12 -08:00
|
|
|
|
|
|
|
// Store tabs and windows separately so we don't need to loop over all
|
|
|
|
// tabs when a window is closed.
|
|
|
|
this.pageIDSourceTabs.set(tab, data.pageID);
|
|
|
|
this.pageIDSourceWindows.set(window, data.pageID);
|
|
|
|
|
|
|
|
this.setTelemetryBucket(data.pageID);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2013-05-29 14:50:59 -07:00
|
|
|
case "showHighlight": {
|
2013-12-10 23:35:11 -08:00
|
|
|
let targetPromise = this.getTarget(window, data.target);
|
|
|
|
targetPromise.then(target => {
|
|
|
|
if (!target.node) {
|
|
|
|
Cu.reportError("UITour: Target could not be resolved: " + data.target);
|
|
|
|
return;
|
|
|
|
}
|
2013-12-10 23:35:31 -08:00
|
|
|
let effect = undefined;
|
|
|
|
if (this.highlightEffects.indexOf(data.effect) !== -1) {
|
|
|
|
effect = data.effect;
|
|
|
|
}
|
|
|
|
this.showHighlight(target, effect);
|
2013-12-10 23:35:11 -08:00
|
|
|
}).then(null, Cu.reportError);
|
2013-05-29 14:50:59 -07:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case "hideHighlight": {
|
|
|
|
this.hideHighlight(window);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case "showInfo": {
|
2013-12-10 23:35:11 -08:00
|
|
|
let targetPromise = this.getTarget(window, data.target, true);
|
|
|
|
targetPromise.then(target => {
|
|
|
|
if (!target.node) {
|
|
|
|
Cu.reportError("UITour: Target could not be resolved: " + data.target);
|
|
|
|
return;
|
|
|
|
}
|
2014-01-05 15:27:25 -08:00
|
|
|
|
|
|
|
let iconURL = null;
|
|
|
|
if (typeof data.icon == "string")
|
|
|
|
iconURL = this.resolveURL(contentDocument, data.icon);
|
|
|
|
|
|
|
|
let buttons = [];
|
|
|
|
if (Array.isArray(data.buttons) && data.buttons.length > 0) {
|
|
|
|
for (let buttonData of data.buttons) {
|
|
|
|
if (typeof buttonData == "object" &&
|
|
|
|
typeof buttonData.label == "string" &&
|
|
|
|
typeof buttonData.callbackID == "string") {
|
|
|
|
let button = {
|
|
|
|
label: buttonData.label,
|
|
|
|
callbackID: buttonData.callbackID,
|
|
|
|
};
|
|
|
|
|
|
|
|
if (typeof buttonData.icon == "string")
|
|
|
|
button.iconURL = this.resolveURL(contentDocument, buttonData.icon);
|
|
|
|
|
2014-01-30 18:25:45 -08:00
|
|
|
if (typeof buttonData.style == "string")
|
|
|
|
button.style = buttonData.style;
|
|
|
|
|
2014-01-05 15:27:25 -08:00
|
|
|
buttons.push(button);
|
|
|
|
|
|
|
|
if (buttons.length == MAX_BUTTONS)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-03-07 10:00:18 -08:00
|
|
|
let infoOptions = {};
|
|
|
|
|
|
|
|
if (typeof data.closeButtonCallbackID == "string")
|
|
|
|
infoOptions.closeButtonCallbackID = data.closeButtonCallbackID;
|
|
|
|
|
|
|
|
this.showInfo(contentDocument, target, data.title, data.text, iconURL, buttons, infoOptions);
|
2013-12-10 23:35:11 -08:00
|
|
|
}).then(null, Cu.reportError);
|
2013-05-29 14:50:59 -07:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case "hideInfo": {
|
|
|
|
this.hideInfo(window);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case "previewTheme": {
|
|
|
|
this.previewTheme(data.theme);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case "resetTheme": {
|
|
|
|
this.resetTheme();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case "addPinnedTab": {
|
|
|
|
this.ensurePinnedTab(window, true);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case "removePinnedTab": {
|
|
|
|
this.removePinnedTab(window);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case "showMenu": {
|
|
|
|
this.showMenu(window, data.name);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2013-11-15 16:26:59 -08:00
|
|
|
case "hideMenu": {
|
|
|
|
this.hideMenu(window, data.name);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2013-05-29 14:50:59 -07:00
|
|
|
case "startUrlbarCapture": {
|
|
|
|
if (typeof data.text != "string" || !data.text ||
|
|
|
|
typeof data.url != "string" || !data.url) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
let uri = null;
|
|
|
|
try {
|
|
|
|
uri = Services.io.newURI(data.url, null, null);
|
|
|
|
} catch (e) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
let secman = Services.scriptSecurityManager;
|
|
|
|
let principal = contentDocument.nodePrincipal;
|
|
|
|
let flags = secman.DISALLOW_INHERIT_PRINCIPAL;
|
|
|
|
try {
|
|
|
|
secman.checkLoadURIWithPrincipal(principal, uri, flags);
|
|
|
|
} catch (e) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.startUrlbarCapture(window, data.text, data.url);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case "endUrlbarCapture": {
|
|
|
|
this.endUrlbarCapture(window);
|
|
|
|
break;
|
|
|
|
}
|
2014-01-17 03:21:33 -08:00
|
|
|
|
|
|
|
case "getConfiguration": {
|
|
|
|
if (typeof data.configuration != "string") {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.getConfiguration(contentDocument, data.configuration, data.callbackID);
|
|
|
|
break;
|
|
|
|
}
|
2013-05-29 14:50:59 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!this.originTabs.has(window))
|
|
|
|
this.originTabs.set(window, new Set());
|
|
|
|
|
2014-02-20 14:42:33 -08:00
|
|
|
this.originTabs.get(window).add(tab);
|
2013-05-29 14:50:59 -07:00
|
|
|
tab.addEventListener("TabClose", this);
|
2014-02-20 14:42:33 -08:00
|
|
|
tab.addEventListener("TabBecomingWindow", this);
|
2013-05-29 14:50:59 -07:00
|
|
|
window.addEventListener("SSWindowClosing", this);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
},
|
|
|
|
|
|
|
|
handleEvent: function(aEvent) {
|
|
|
|
switch (aEvent.type) {
|
|
|
|
case "pagehide": {
|
|
|
|
let window = this.getChromeWindow(aEvent.target);
|
|
|
|
this.teardownTour(window);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2014-02-20 14:42:33 -08:00
|
|
|
case "TabBecomingWindow":
|
|
|
|
this._detachingTab = true;
|
|
|
|
// Fall through
|
2013-05-29 14:50:59 -07:00
|
|
|
case "TabClose": {
|
2014-02-06 19:19:12 -08:00
|
|
|
let tab = aEvent.target;
|
|
|
|
if (this.pageIDSourceTabs.has(tab)) {
|
|
|
|
let pageID = this.pageIDSourceTabs.get(tab);
|
|
|
|
|
|
|
|
// Delete this from the window cache, so if the window is closed we
|
|
|
|
// don't expire this page ID twice.
|
|
|
|
let window = tab.ownerDocument.defaultView;
|
|
|
|
if (this.pageIDSourceWindows.get(window) == pageID)
|
|
|
|
this.pageIDSourceWindows.delete(window);
|
|
|
|
|
|
|
|
this.setExpiringTelemetryBucket(pageID, "closed");
|
|
|
|
}
|
|
|
|
|
|
|
|
let window = tab.ownerDocument.defaultView;
|
2013-05-29 14:50:59 -07:00
|
|
|
this.teardownTour(window);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case "TabSelect": {
|
2014-02-06 19:19:12 -08:00
|
|
|
if (aEvent.detail && aEvent.detail.previousTab) {
|
|
|
|
let previousTab = aEvent.detail.previousTab;
|
|
|
|
|
|
|
|
if (this.pageIDSourceTabs.has(previousTab)) {
|
|
|
|
let pageID = this.pageIDSourceTabs.get(previousTab);
|
|
|
|
this.setExpiringTelemetryBucket(pageID, "inactive");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-05-29 14:50:59 -07:00
|
|
|
let window = aEvent.target.ownerDocument.defaultView;
|
2014-02-20 14:42:33 -08:00
|
|
|
let selectedTab = window.gBrowser.selectedTab;
|
2013-05-29 14:50:59 -07:00
|
|
|
let pinnedTab = this.pinnedTabs.get(window);
|
2014-02-20 14:42:33 -08:00
|
|
|
if (pinnedTab && pinnedTab.tab == selectedTab)
|
2013-05-29 14:50:59 -07:00
|
|
|
break;
|
|
|
|
let originTabs = this.originTabs.get(window);
|
2014-02-20 14:42:33 -08:00
|
|
|
if (originTabs && originTabs.has(selectedTab))
|
2013-05-29 14:50:59 -07:00
|
|
|
break;
|
|
|
|
|
2014-02-20 14:42:33 -08:00
|
|
|
let pendingDoc;
|
|
|
|
if (this._detachingTab && this._pendingDoc && (pendingDoc = this._pendingDoc.get())) {
|
|
|
|
if (selectedTab.linkedBrowser.contentDocument == pendingDoc) {
|
|
|
|
if (!this.originTabs.get(window)) {
|
|
|
|
this.originTabs.set(window, new Set());
|
|
|
|
}
|
|
|
|
this.originTabs.get(window).add(selectedTab);
|
|
|
|
this.pendingDoc = null;
|
|
|
|
this._detachingTab = false;
|
|
|
|
while (this._queuedEvents.length) {
|
|
|
|
try {
|
|
|
|
this.onPageEvent(this._queuedEvents.shift());
|
|
|
|
} catch (ex) {
|
|
|
|
Cu.reportError(ex);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-05-29 14:50:59 -07:00
|
|
|
this.teardownTour(window);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case "SSWindowClosing": {
|
|
|
|
let window = aEvent.target;
|
2014-02-06 19:19:12 -08:00
|
|
|
if (this.pageIDSourceWindows.has(window)) {
|
|
|
|
let pageID = this.pageIDSourceWindows.get(window);
|
|
|
|
this.setExpiringTelemetryBucket(pageID, "closed");
|
|
|
|
}
|
|
|
|
|
2013-05-29 14:50:59 -07:00
|
|
|
this.teardownTour(window, true);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case "input": {
|
|
|
|
if (aEvent.target.id == "urlbar") {
|
|
|
|
let window = aEvent.target.ownerDocument.defaultView;
|
|
|
|
this.handleUrlbarInput(window);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2014-02-06 19:19:12 -08:00
|
|
|
setTelemetryBucket: function(aPageID) {
|
|
|
|
let bucket = BUCKET_NAME + BrowserUITelemetry.BUCKET_SEPARATOR + aPageID;
|
|
|
|
BrowserUITelemetry.setBucket(bucket);
|
|
|
|
},
|
|
|
|
|
|
|
|
setExpiringTelemetryBucket: function(aPageID, aType) {
|
|
|
|
let bucket = BUCKET_NAME + BrowserUITelemetry.BUCKET_SEPARATOR + aPageID +
|
|
|
|
BrowserUITelemetry.BUCKET_SEPARATOR + aType;
|
|
|
|
|
|
|
|
BrowserUITelemetry.setExpiringBucket(bucket,
|
|
|
|
BUCKET_TIMESTEPS);
|
|
|
|
},
|
|
|
|
|
|
|
|
getTelemetry: function() {
|
|
|
|
return {
|
2014-02-27 01:25:44 -08:00
|
|
|
seenPageIDs: [...this.seenPageIDs.keys()],
|
2014-02-06 19:19:12 -08:00
|
|
|
};
|
|
|
|
},
|
|
|
|
|
2013-05-29 14:50:59 -07:00
|
|
|
teardownTour: function(aWindow, aWindowClosing = false) {
|
|
|
|
aWindow.gBrowser.tabContainer.removeEventListener("TabSelect", this);
|
2014-02-06 21:43:28 -08:00
|
|
|
aWindow.PanelUI.panel.removeEventListener("popuphiding", this.hidePanelAnnotations);
|
|
|
|
aWindow.PanelUI.panel.removeEventListener("ViewShowing", this.hidePanelAnnotations);
|
2013-05-29 14:50:59 -07:00
|
|
|
aWindow.removeEventListener("SSWindowClosing", this);
|
|
|
|
|
|
|
|
let originTabs = this.originTabs.get(aWindow);
|
|
|
|
if (originTabs) {
|
2014-02-20 14:42:33 -08:00
|
|
|
for (let tab of originTabs) {
|
2013-05-29 14:50:59 -07:00
|
|
|
tab.removeEventListener("TabClose", this);
|
2014-02-20 14:42:33 -08:00
|
|
|
tab.removeEventListener("TabBecomingWindow", this);
|
|
|
|
}
|
2013-05-29 14:50:59 -07:00
|
|
|
}
|
|
|
|
this.originTabs.delete(aWindow);
|
|
|
|
|
|
|
|
if (!aWindowClosing) {
|
|
|
|
this.hideHighlight(aWindow);
|
|
|
|
this.hideInfo(aWindow);
|
2014-03-14 16:52:15 -07:00
|
|
|
// Ensure the menu panel is hidden before calling recreatePopup so popup events occur.
|
|
|
|
this.hideMenu(aWindow, "appMenu");
|
2013-05-29 14:50:59 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
this.endUrlbarCapture(aWindow);
|
|
|
|
this.removePinnedTab(aWindow);
|
|
|
|
this.resetTheme();
|
|
|
|
},
|
|
|
|
|
|
|
|
getChromeWindow: function(aContentDocument) {
|
|
|
|
return aContentDocument.defaultView
|
|
|
|
.window
|
|
|
|
.QueryInterface(Ci.nsIInterfaceRequestor)
|
|
|
|
.getInterface(Ci.nsIWebNavigation)
|
|
|
|
.QueryInterface(Ci.nsIDocShellTreeItem)
|
|
|
|
.rootTreeItem
|
|
|
|
.QueryInterface(Ci.nsIInterfaceRequestor)
|
|
|
|
.getInterface(Ci.nsIDOMWindow)
|
|
|
|
.wrappedJSObject;
|
|
|
|
},
|
|
|
|
|
|
|
|
importPermissions: function() {
|
|
|
|
try {
|
|
|
|
PermissionsUtils.importFromPrefs(PREF_PERM_BRANCH, UITOUR_PERMISSION);
|
|
|
|
} catch (e) {
|
|
|
|
Cu.reportError(e);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
ensureTrustedOrigin: function(aDocument) {
|
|
|
|
if (aDocument.defaultView.top != aDocument.defaultView)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
let uri = aDocument.documentURIObject;
|
|
|
|
|
|
|
|
if (uri.schemeIs("chrome"))
|
|
|
|
return true;
|
|
|
|
|
2014-01-05 15:27:25 -08:00
|
|
|
if (!this.isSafeScheme(uri))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
this.importPermissions();
|
|
|
|
let permission = Services.perms.testPermission(uri, UITOUR_PERMISSION);
|
|
|
|
return permission == Services.perms.ALLOW_ACTION;
|
|
|
|
},
|
|
|
|
|
|
|
|
isSafeScheme: function(aURI) {
|
2013-11-04 18:23:25 -08:00
|
|
|
let allowedSchemes = new Set(["https"]);
|
|
|
|
if (!Services.prefs.getBoolPref("browser.uitour.requireSecure"))
|
|
|
|
allowedSchemes.add("http");
|
|
|
|
|
2014-01-05 15:27:25 -08:00
|
|
|
if (!allowedSchemes.has(aURI.scheme))
|
2013-05-29 14:50:59 -07:00
|
|
|
return false;
|
|
|
|
|
2014-01-05 15:27:25 -08:00
|
|
|
return true;
|
|
|
|
},
|
|
|
|
|
|
|
|
resolveURL: function(aDocument, aURL) {
|
|
|
|
try {
|
|
|
|
let uri = Services.io.newURI(aURL, null, aDocument.documentURIObject);
|
|
|
|
|
|
|
|
if (!this.isSafeScheme(uri))
|
|
|
|
return null;
|
|
|
|
|
|
|
|
return uri.spec;
|
|
|
|
} catch (e) {}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
},
|
|
|
|
|
|
|
|
sendPageCallback: function(aDocument, aCallbackID, aData = {}) {
|
|
|
|
let detail = Cu.createObjectIn(aDocument.defaultView);
|
|
|
|
detail.data = Cu.createObjectIn(detail);
|
|
|
|
|
|
|
|
for (let key of Object.keys(aData))
|
|
|
|
detail.data[key] = aData[key];
|
|
|
|
|
|
|
|
Cu.makeObjectPropsNormal(detail.data);
|
|
|
|
Cu.makeObjectPropsNormal(detail);
|
|
|
|
|
|
|
|
detail.callbackID = aCallbackID;
|
|
|
|
|
|
|
|
let event = new aDocument.defaultView.CustomEvent("mozUITourResponse", {
|
|
|
|
bubbles: true,
|
|
|
|
detail: detail
|
|
|
|
});
|
|
|
|
|
|
|
|
aDocument.dispatchEvent(event);
|
2013-05-29 14:50:59 -07:00
|
|
|
},
|
|
|
|
|
2014-02-06 19:19:12 -08:00
|
|
|
isElementVisible: function(aElement) {
|
|
|
|
let targetStyle = aElement.ownerDocument.defaultView.getComputedStyle(aElement);
|
|
|
|
return (targetStyle.display != "none" && targetStyle.visibility == "visible");
|
|
|
|
},
|
|
|
|
|
2013-05-29 14:50:59 -07:00
|
|
|
getTarget: function(aWindow, aTargetName, aSticky = false) {
|
2013-12-10 23:35:11 -08:00
|
|
|
let deferred = Promise.defer();
|
|
|
|
if (typeof aTargetName != "string" || !aTargetName) {
|
|
|
|
deferred.reject("Invalid target name specified");
|
|
|
|
return deferred.promise;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (aTargetName == "pinnedTab") {
|
2014-02-05 01:02:40 -08:00
|
|
|
deferred.resolve({
|
|
|
|
targetName: aTargetName,
|
|
|
|
node: this.ensurePinnedTab(aWindow, aSticky)
|
|
|
|
});
|
2013-12-10 23:35:11 -08:00
|
|
|
return deferred.promise;
|
|
|
|
}
|
|
|
|
|
|
|
|
let targetObject = this.targets.get(aTargetName);
|
|
|
|
if (!targetObject) {
|
|
|
|
deferred.reject("The specified target name is not in the allowed set");
|
|
|
|
return deferred.promise;
|
|
|
|
}
|
2013-05-29 14:50:59 -07:00
|
|
|
|
2013-12-10 23:35:11 -08:00
|
|
|
let targetQuery = targetObject.query;
|
|
|
|
aWindow.PanelUI.ensureReady().then(() => {
|
|
|
|
if (typeof targetQuery == "function") {
|
|
|
|
deferred.resolve({
|
2014-02-05 01:02:40 -08:00
|
|
|
targetName: aTargetName,
|
2013-12-10 23:35:11 -08:00
|
|
|
node: targetQuery(aWindow.document),
|
|
|
|
widgetName: targetObject.widgetName,
|
|
|
|
});
|
|
|
|
return;
|
|
|
|
}
|
2013-05-29 14:50:59 -07:00
|
|
|
|
2013-12-10 23:35:11 -08:00
|
|
|
deferred.resolve({
|
2014-02-05 01:02:40 -08:00
|
|
|
targetName: aTargetName,
|
2013-12-10 23:35:11 -08:00
|
|
|
node: aWindow.document.querySelector(targetQuery),
|
|
|
|
widgetName: targetObject.widgetName,
|
|
|
|
});
|
|
|
|
}).then(null, Cu.reportError);
|
|
|
|
return deferred.promise;
|
|
|
|
},
|
2013-05-29 14:50:59 -07:00
|
|
|
|
2013-12-10 23:35:11 -08:00
|
|
|
targetIsInAppMenu: function(aTarget) {
|
|
|
|
let placement = CustomizableUI.getPlacementOfWidget(aTarget.widgetName || aTarget.node.id);
|
|
|
|
if (placement && placement.area == CustomizableUI.AREA_PANEL) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
let targetElement = aTarget.node;
|
|
|
|
// Use the widget for filtering if it exists since the target may be the icon inside.
|
|
|
|
if (aTarget.widgetName) {
|
|
|
|
targetElement = aTarget.node.ownerDocument.getElementById(aTarget.widgetName);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Handle the non-customizable buttons at the bottom of the menu which aren't proper widgets.
|
|
|
|
return targetElement.id.startsWith("PanelUI-")
|
2014-03-13 06:29:24 -07:00
|
|
|
&& targetElement.id != "PanelUI-button";
|
2013-12-10 23:35:11 -08:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Called before opening or after closing a highlight or info panel to see if
|
|
|
|
* we need to open or close the appMenu to see the annotation's anchor.
|
|
|
|
*/
|
|
|
|
_setAppMenuStateForAnnotation: function(aWindow, aAnnotationType, aShouldOpenForHighlight, aCallback = null) {
|
|
|
|
// If the panel is in the desired state, we're done.
|
|
|
|
let panelIsOpen = aWindow.PanelUI.panel.state != "closed";
|
|
|
|
if (aShouldOpenForHighlight == panelIsOpen) {
|
|
|
|
if (aCallback)
|
|
|
|
aCallback();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Don't close the menu if it wasn't opened by us (e.g. via showmenu instead).
|
|
|
|
if (!aShouldOpenForHighlight && !this.appMenuOpenForAnnotation.has(aAnnotationType)) {
|
|
|
|
if (aCallback)
|
|
|
|
aCallback();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (aShouldOpenForHighlight) {
|
|
|
|
this.appMenuOpenForAnnotation.add(aAnnotationType);
|
|
|
|
} else {
|
|
|
|
this.appMenuOpenForAnnotation.delete(aAnnotationType);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Actually show or hide the menu
|
|
|
|
if (this.appMenuOpenForAnnotation.size) {
|
|
|
|
this.showMenu(aWindow, "appMenu", aCallback);
|
|
|
|
} else {
|
|
|
|
this.hideMenu(aWindow, "appMenu");
|
|
|
|
if (aCallback)
|
|
|
|
aCallback();
|
|
|
|
}
|
2013-05-29 14:50:59 -07:00
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
previewTheme: function(aTheme) {
|
|
|
|
let origin = Services.prefs.getCharPref("browser.uitour.themeOrigin");
|
|
|
|
let data = LightweightThemeManager.parseTheme(aTheme, origin);
|
|
|
|
if (data)
|
|
|
|
LightweightThemeManager.previewTheme(data);
|
|
|
|
},
|
|
|
|
|
|
|
|
resetTheme: function() {
|
|
|
|
LightweightThemeManager.resetPreview();
|
|
|
|
},
|
|
|
|
|
|
|
|
ensurePinnedTab: function(aWindow, aSticky = false) {
|
|
|
|
let tabInfo = this.pinnedTabs.get(aWindow);
|
|
|
|
|
|
|
|
if (tabInfo) {
|
|
|
|
tabInfo.sticky = tabInfo.sticky || aSticky;
|
|
|
|
} else {
|
|
|
|
let url = Services.urlFormatter.formatURLPref("browser.uitour.pinnedTabUrl");
|
|
|
|
|
|
|
|
let tab = aWindow.gBrowser.addTab(url);
|
|
|
|
aWindow.gBrowser.pinTab(tab);
|
|
|
|
tab.addEventListener("TabClose", () => {
|
|
|
|
this.pinnedTabs.delete(aWindow);
|
|
|
|
});
|
|
|
|
|
|
|
|
tabInfo = {
|
|
|
|
tab: tab,
|
|
|
|
sticky: aSticky
|
|
|
|
};
|
|
|
|
this.pinnedTabs.set(aWindow, tabInfo);
|
|
|
|
}
|
|
|
|
|
|
|
|
return tabInfo.tab;
|
|
|
|
},
|
|
|
|
|
|
|
|
removePinnedTab: function(aWindow) {
|
|
|
|
let tabInfo = this.pinnedTabs.get(aWindow);
|
|
|
|
if (tabInfo)
|
|
|
|
aWindow.gBrowser.removeTab(tabInfo.tab);
|
|
|
|
},
|
|
|
|
|
2013-12-10 23:35:31 -08:00
|
|
|
/**
|
|
|
|
* @param aTarget The element to highlight.
|
|
|
|
* @param aEffect (optional) The effect to use from UITour.highlightEffects or "none".
|
|
|
|
* @see UITour.highlightEffects
|
|
|
|
*/
|
|
|
|
showHighlight: function(aTarget, aEffect = "none") {
|
2013-12-10 23:35:11 -08:00
|
|
|
function showHighlightPanel(aTargetEl) {
|
|
|
|
let highlighter = aTargetEl.ownerDocument.getElementById("UITourHighlight");
|
2013-05-29 14:50:59 -07:00
|
|
|
|
2013-12-10 23:35:31 -08:00
|
|
|
let effect = aEffect;
|
|
|
|
if (effect == "random") {
|
|
|
|
// Exclude "random" from the randomly selected effects.
|
|
|
|
let randomEffect = 1 + Math.floor(Math.random() * (this.highlightEffects.length - 1));
|
|
|
|
if (randomEffect == this.highlightEffects.length)
|
|
|
|
randomEffect--; // On the order of 1 in 2^62 chance of this happening.
|
|
|
|
effect = this.highlightEffects[randomEffect];
|
|
|
|
}
|
|
|
|
highlighter.setAttribute("active", effect);
|
2014-02-05 01:02:40 -08:00
|
|
|
highlighter.parentElement.setAttribute("targetName", aTarget.targetName);
|
2013-12-13 02:54:04 -08:00
|
|
|
highlighter.parentElement.hidden = false;
|
2013-05-29 14:50:59 -07:00
|
|
|
|
2013-12-10 23:35:11 -08:00
|
|
|
let targetRect = aTargetEl.getBoundingClientRect();
|
2014-02-05 01:02:32 -08:00
|
|
|
let highlightHeight = targetRect.height;
|
|
|
|
let highlightWidth = targetRect.width;
|
|
|
|
let minDimension = Math.min(highlightHeight, highlightWidth);
|
|
|
|
let maxDimension = Math.max(highlightHeight, highlightWidth);
|
|
|
|
|
2014-03-13 06:29:24 -07:00
|
|
|
// If the dimensions are within 110% of each other (to include the bookmarks button),
|
|
|
|
// make the highlight a circle with the largest dimension as the diameter.
|
|
|
|
if (maxDimension / minDimension <= 2.1) {
|
2014-02-05 01:02:32 -08:00
|
|
|
highlightHeight = highlightWidth = maxDimension;
|
|
|
|
highlighter.style.borderRadius = "100%";
|
|
|
|
} else {
|
|
|
|
highlighter.style.borderRadius = "";
|
|
|
|
}
|
2013-05-29 14:50:59 -07:00
|
|
|
|
2014-02-05 01:02:32 -08:00
|
|
|
highlighter.style.height = highlightHeight + "px";
|
|
|
|
highlighter.style.width = highlightWidth + "px";
|
2013-05-29 14:50:59 -07:00
|
|
|
|
2013-12-10 23:35:20 -08:00
|
|
|
// Close a previous highlight so we can relocate the panel.
|
|
|
|
if (highlighter.parentElement.state == "open") {
|
|
|
|
highlighter.parentElement.hidePopup();
|
|
|
|
}
|
2013-12-10 23:35:31 -08:00
|
|
|
/* The "overlap" position anchors from the top-left but we want to centre highlights at their
|
|
|
|
minimum size. */
|
|
|
|
let highlightWindow = aTargetEl.ownerDocument.defaultView;
|
|
|
|
let containerStyle = highlightWindow.getComputedStyle(highlighter.parentElement);
|
|
|
|
let paddingTopPx = 0 - parseFloat(containerStyle.paddingTop);
|
|
|
|
let paddingLeftPx = 0 - parseFloat(containerStyle.paddingLeft);
|
|
|
|
let highlightStyle = highlightWindow.getComputedStyle(highlighter);
|
2014-02-05 01:02:32 -08:00
|
|
|
let highlightHeightWithMin = Math.max(highlightHeight, parseFloat(highlightStyle.minHeight));
|
|
|
|
let highlightWidthWithMin = Math.max(highlightWidth, parseFloat(highlightStyle.minWidth));
|
2013-12-10 23:35:31 -08:00
|
|
|
let offsetX = paddingTopPx
|
2014-02-05 01:02:32 -08:00
|
|
|
- (Math.max(0, highlightWidthWithMin - targetRect.width) / 2);
|
2013-12-10 23:35:31 -08:00
|
|
|
let offsetY = paddingLeftPx
|
2014-02-05 01:02:32 -08:00
|
|
|
- (Math.max(0, highlightHeightWithMin - targetRect.height) / 2);
|
2014-03-05 21:59:00 -08:00
|
|
|
|
|
|
|
this._addAnnotationPanelMutationObserver(highlighter.parentElement);
|
2013-12-10 23:35:31 -08:00
|
|
|
highlighter.parentElement.openPopup(aTargetEl, "overlap", offsetX, offsetY);
|
2013-12-10 23:35:11 -08:00
|
|
|
}
|
2013-05-29 14:50:59 -07:00
|
|
|
|
2014-01-13 07:28:18 -08:00
|
|
|
// Prevent showing a panel at an undefined position.
|
2014-02-06 19:19:12 -08:00
|
|
|
if (!this.isElementVisible(aTarget.node))
|
2014-01-13 07:28:18 -08:00
|
|
|
return;
|
|
|
|
|
2013-12-10 23:35:11 -08:00
|
|
|
this._setAppMenuStateForAnnotation(aTarget.node.ownerDocument.defaultView, "highlight",
|
|
|
|
this.targetIsInAppMenu(aTarget),
|
|
|
|
showHighlightPanel.bind(this, aTarget.node));
|
2013-05-29 14:50:59 -07:00
|
|
|
},
|
|
|
|
|
|
|
|
hideHighlight: function(aWindow) {
|
|
|
|
let tabData = this.pinnedTabs.get(aWindow);
|
|
|
|
if (tabData && !tabData.sticky)
|
|
|
|
this.removePinnedTab(aWindow);
|
|
|
|
|
|
|
|
let highlighter = aWindow.document.getElementById("UITourHighlight");
|
2014-03-05 21:59:00 -08:00
|
|
|
this._removeAnnotationPanelMutationObserver(highlighter.parentElement);
|
2013-12-10 23:35:20 -08:00
|
|
|
highlighter.parentElement.hidePopup();
|
2013-05-29 14:50:59 -07:00
|
|
|
highlighter.removeAttribute("active");
|
2013-12-10 23:35:11 -08:00
|
|
|
|
|
|
|
this._setAppMenuStateForAnnotation(aWindow, "highlight", false);
|
2013-05-29 14:50:59 -07:00
|
|
|
},
|
|
|
|
|
2014-03-07 10:00:18 -08:00
|
|
|
/**
|
|
|
|
* Show an info panel.
|
|
|
|
*
|
|
|
|
* @param {Document} aContentDocument
|
|
|
|
* @param {Node} aAnchor
|
|
|
|
* @param {String} [aTitle=""]
|
|
|
|
* @param {String} [aDescription=""]
|
|
|
|
* @param {String} [aIconURL=""]
|
|
|
|
* @param {Object[]} [aButtons=[]]
|
|
|
|
* @param {Object} [aOptions={}]
|
|
|
|
* @param {String} [aOptions.closeButtonCallbackID]
|
|
|
|
*/
|
|
|
|
showInfo: function(aContentDocument, aAnchor, aTitle = "", aDescription = "", aIconURL = "",
|
|
|
|
aButtons = [], aOptions = {}) {
|
2013-12-10 23:35:11 -08:00
|
|
|
function showInfoPanel(aAnchorEl) {
|
|
|
|
aAnchorEl.focus();
|
2013-05-29 14:50:59 -07:00
|
|
|
|
2013-12-10 23:35:11 -08:00
|
|
|
let document = aAnchorEl.ownerDocument;
|
|
|
|
let tooltip = document.getElementById("UITourTooltip");
|
|
|
|
let tooltipTitle = document.getElementById("UITourTooltipTitle");
|
|
|
|
let tooltipDesc = document.getElementById("UITourTooltipDescription");
|
2014-01-05 15:27:25 -08:00
|
|
|
let tooltipIcon = document.getElementById("UITourTooltipIcon");
|
|
|
|
let tooltipButtons = document.getElementById("UITourTooltipButtons");
|
2013-05-29 14:50:59 -07:00
|
|
|
|
2013-12-13 02:54:04 -08:00
|
|
|
if (tooltip.state == "open") {
|
|
|
|
tooltip.hidePopup();
|
|
|
|
}
|
2013-05-29 14:50:59 -07:00
|
|
|
|
2014-01-05 15:27:25 -08:00
|
|
|
tooltipTitle.textContent = aTitle || "";
|
|
|
|
tooltipDesc.textContent = aDescription || "";
|
|
|
|
tooltipIcon.src = aIconURL || "";
|
|
|
|
tooltipIcon.hidden = !aIconURL;
|
|
|
|
|
|
|
|
while (tooltipButtons.firstChild)
|
|
|
|
tooltipButtons.firstChild.remove();
|
|
|
|
|
|
|
|
for (let button of aButtons) {
|
|
|
|
let el = document.createElement("button");
|
|
|
|
el.setAttribute("label", button.label);
|
|
|
|
if (button.iconURL)
|
|
|
|
el.setAttribute("image", button.iconURL);
|
|
|
|
|
2014-01-30 18:25:45 -08:00
|
|
|
if (button.style == "link")
|
|
|
|
el.setAttribute("class", "button-link");
|
|
|
|
|
2014-01-05 15:27:25 -08:00
|
|
|
let callbackID = button.callbackID;
|
|
|
|
el.addEventListener("command", event => {
|
|
|
|
tooltip.hidePopup();
|
|
|
|
this.sendPageCallback(aContentDocument, callbackID);
|
|
|
|
});
|
|
|
|
|
|
|
|
tooltipButtons.appendChild(el);
|
|
|
|
}
|
|
|
|
|
|
|
|
tooltipButtons.hidden = !aButtons.length;
|
2013-05-29 14:50:59 -07:00
|
|
|
|
2014-01-30 18:25:45 -08:00
|
|
|
let tooltipClose = document.getElementById("UITourTooltipClose");
|
2014-03-07 10:00:18 -08:00
|
|
|
let closeButtonCallback = (event) => {
|
|
|
|
this.hideInfo(document.defaultView);
|
|
|
|
if (aOptions && aOptions.closeButtonCallbackID)
|
|
|
|
this.sendPageCallback(aContentDocument, aOptions.closeButtonCallbackID);
|
|
|
|
};
|
|
|
|
tooltipClose.addEventListener("command", closeButtonCallback);
|
|
|
|
tooltip.addEventListener("popuphiding", function tooltipHiding(event) {
|
|
|
|
tooltip.removeEventListener("popuphiding", tooltipHiding);
|
|
|
|
tooltipClose.removeEventListener("command", closeButtonCallback);
|
|
|
|
});
|
2014-01-30 18:25:45 -08:00
|
|
|
|
2014-02-05 01:02:40 -08:00
|
|
|
tooltip.setAttribute("targetName", aAnchor.targetName);
|
2013-12-13 02:54:04 -08:00
|
|
|
tooltip.hidden = false;
|
2013-12-10 23:35:11 -08:00
|
|
|
let alignment = "bottomcenter topright";
|
2014-03-05 21:59:00 -08:00
|
|
|
this._addAnnotationPanelMutationObserver(tooltip);
|
2013-12-10 23:35:11 -08:00
|
|
|
tooltip.openPopup(aAnchorEl, alignment);
|
|
|
|
}
|
|
|
|
|
2014-01-13 07:28:18 -08:00
|
|
|
// Prevent showing a panel at an undefined position.
|
2014-02-06 19:19:12 -08:00
|
|
|
if (!this.isElementVisible(aAnchor.node))
|
2014-01-13 07:28:18 -08:00
|
|
|
return;
|
|
|
|
|
2013-12-10 23:35:11 -08:00
|
|
|
this._setAppMenuStateForAnnotation(aAnchor.node.ownerDocument.defaultView, "info",
|
|
|
|
this.targetIsInAppMenu(aAnchor),
|
|
|
|
showInfoPanel.bind(this, aAnchor.node));
|
2013-05-29 14:50:59 -07:00
|
|
|
},
|
|
|
|
|
|
|
|
hideInfo: function(aWindow) {
|
2014-01-05 15:27:25 -08:00
|
|
|
let document = aWindow.document;
|
|
|
|
|
|
|
|
let tooltip = document.getElementById("UITourTooltip");
|
2014-03-05 21:59:00 -08:00
|
|
|
this._removeAnnotationPanelMutationObserver(tooltip);
|
2013-05-29 14:50:59 -07:00
|
|
|
tooltip.hidePopup();
|
2013-12-10 23:35:11 -08:00
|
|
|
this._setAppMenuStateForAnnotation(aWindow, "info", false);
|
2014-01-05 15:27:25 -08:00
|
|
|
|
|
|
|
let tooltipButtons = document.getElementById("UITourTooltipButtons");
|
|
|
|
while (tooltipButtons.firstChild)
|
|
|
|
tooltipButtons.firstChild.remove();
|
2013-05-29 14:50:59 -07:00
|
|
|
},
|
|
|
|
|
2013-12-10 23:35:11 -08:00
|
|
|
showMenu: function(aWindow, aMenuName, aOpenCallback = null) {
|
2014-03-03 17:39:35 -08:00
|
|
|
function openMenuButton(aID) {
|
|
|
|
let menuBtn = aWindow.document.getElementById(aID);
|
2013-12-10 23:35:11 -08:00
|
|
|
if (!menuBtn || !menuBtn.boxObject) {
|
|
|
|
aOpenCallback();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (aOpenCallback)
|
|
|
|
menuBtn.addEventListener("popupshown", onPopupShown);
|
|
|
|
menuBtn.boxObject.QueryInterface(Ci.nsIMenuBoxObject).openMenu(true);
|
|
|
|
}
|
|
|
|
function onPopupShown(event) {
|
|
|
|
this.removeEventListener("popupshown", onPopupShown);
|
|
|
|
aOpenCallback(event);
|
2013-05-29 14:50:59 -07:00
|
|
|
}
|
|
|
|
|
2013-12-10 23:35:11 -08:00
|
|
|
if (aMenuName == "appMenu") {
|
|
|
|
aWindow.PanelUI.panel.setAttribute("noautohide", "true");
|
2014-02-07 13:36:09 -08:00
|
|
|
// If the popup is already opened, don't recreate the widget as it may cause a flicker.
|
|
|
|
if (aWindow.PanelUI.panel.state != "open") {
|
|
|
|
this.recreatePopup(aWindow.PanelUI.panel);
|
|
|
|
}
|
2014-02-06 21:43:28 -08:00
|
|
|
aWindow.PanelUI.panel.addEventListener("popuphiding", this.hidePanelAnnotations);
|
|
|
|
aWindow.PanelUI.panel.addEventListener("ViewShowing", this.hidePanelAnnotations);
|
2013-12-10 23:35:11 -08:00
|
|
|
if (aOpenCallback) {
|
|
|
|
aWindow.PanelUI.panel.addEventListener("popupshown", onPopupShown);
|
|
|
|
}
|
2013-09-16 18:55:00 -07:00
|
|
|
aWindow.PanelUI.show();
|
2013-12-10 23:35:11 -08:00
|
|
|
} else if (aMenuName == "bookmarks") {
|
2013-05-29 14:50:59 -07:00
|
|
|
openMenuButton("bookmarks-menu-button");
|
2013-12-10 23:35:11 -08:00
|
|
|
}
|
2013-05-29 14:50:59 -07:00
|
|
|
},
|
2013-11-15 16:26:59 -08:00
|
|
|
|
|
|
|
hideMenu: function(aWindow, aMenuName) {
|
2014-03-03 17:39:35 -08:00
|
|
|
function closeMenuButton(aID) {
|
|
|
|
let menuBtn = aWindow.document.getElementById(aID);
|
2013-11-15 16:26:59 -08:00
|
|
|
if (menuBtn && menuBtn.boxObject)
|
|
|
|
menuBtn.boxObject.QueryInterface(Ci.nsIMenuBoxObject).openMenu(false);
|
|
|
|
}
|
|
|
|
|
2013-12-10 23:35:11 -08:00
|
|
|
if (aMenuName == "appMenu") {
|
|
|
|
aWindow.PanelUI.panel.removeAttribute("noautohide");
|
2013-11-15 16:26:59 -08:00
|
|
|
aWindow.PanelUI.hide();
|
2014-02-07 13:36:09 -08:00
|
|
|
this.recreatePopup(aWindow.PanelUI.panel);
|
2013-12-10 23:35:11 -08:00
|
|
|
} else if (aMenuName == "bookmarks") {
|
2013-11-15 16:26:59 -08:00
|
|
|
closeMenuButton("bookmarks-menu-button");
|
2013-12-10 23:35:11 -08:00
|
|
|
}
|
2013-11-15 16:26:59 -08:00
|
|
|
},
|
2013-05-29 14:50:59 -07:00
|
|
|
|
2014-02-06 21:43:28 -08:00
|
|
|
hidePanelAnnotations: function(aEvent) {
|
2014-02-05 01:02:40 -08:00
|
|
|
let win = aEvent.target.ownerDocument.defaultView;
|
|
|
|
let annotationElements = new Map([
|
|
|
|
// [annotationElement (panel), method to hide the annotation]
|
|
|
|
[win.document.getElementById("UITourHighlightContainer"), UITour.hideHighlight.bind(UITour)],
|
|
|
|
[win.document.getElementById("UITourTooltip"), UITour.hideInfo.bind(UITour)],
|
|
|
|
]);
|
|
|
|
annotationElements.forEach((hideMethod, annotationElement) => {
|
|
|
|
if (annotationElement.state != "closed") {
|
|
|
|
let targetName = annotationElement.getAttribute("targetName");
|
|
|
|
UITour.getTarget(win, targetName).then((aTarget) => {
|
|
|
|
// Since getTarget is async, we need to make sure that the target hasn't
|
|
|
|
// changed since it may have just moved to somewhere outside of the app menu.
|
|
|
|
if (annotationElement.getAttribute("targetName") != aTarget.targetName ||
|
|
|
|
annotationElement.state == "closed" ||
|
|
|
|
!UITour.targetIsInAppMenu(aTarget)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
hideMethod(win);
|
|
|
|
}).then(null, Cu.reportError);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
UITour.appMenuOpenForAnnotation.clear();
|
|
|
|
},
|
|
|
|
|
2014-02-07 13:36:09 -08:00
|
|
|
recreatePopup: function(aPanel) {
|
|
|
|
// After changing popup attributes that relate to how the native widget is created
|
|
|
|
// (e.g. @noautohide) we need to re-create the frame/widget for it to take effect.
|
2014-02-21 14:42:40 -08:00
|
|
|
if (aPanel.hidden) {
|
|
|
|
// If the panel is already hidden, we don't need to recreate it but flush
|
|
|
|
// in case someone just hid it.
|
|
|
|
aPanel.clientWidth; // flush
|
|
|
|
return;
|
|
|
|
}
|
2014-02-07 13:36:09 -08:00
|
|
|
aPanel.hidden = true;
|
|
|
|
aPanel.clientWidth; // flush
|
|
|
|
aPanel.hidden = false;
|
|
|
|
},
|
|
|
|
|
2013-05-29 14:50:59 -07:00
|
|
|
startUrlbarCapture: function(aWindow, aExpectedText, aUrl) {
|
|
|
|
let urlbar = aWindow.document.getElementById("urlbar");
|
|
|
|
this.urlbarCapture.set(aWindow, {
|
|
|
|
expected: aExpectedText.toLocaleLowerCase(),
|
|
|
|
url: aUrl
|
|
|
|
});
|
|
|
|
urlbar.addEventListener("input", this);
|
|
|
|
},
|
|
|
|
|
|
|
|
endUrlbarCapture: function(aWindow) {
|
|
|
|
let urlbar = aWindow.document.getElementById("urlbar");
|
|
|
|
urlbar.removeEventListener("input", this);
|
|
|
|
this.urlbarCapture.delete(aWindow);
|
|
|
|
},
|
|
|
|
|
|
|
|
handleUrlbarInput: function(aWindow) {
|
|
|
|
if (!this.urlbarCapture.has(aWindow))
|
|
|
|
return;
|
|
|
|
|
|
|
|
let urlbar = aWindow.document.getElementById("urlbar");
|
|
|
|
|
|
|
|
let {expected, url} = this.urlbarCapture.get(aWindow);
|
|
|
|
|
|
|
|
if (urlbar.value.toLocaleLowerCase().localeCompare(expected) != 0)
|
|
|
|
return;
|
|
|
|
|
|
|
|
urlbar.handleRevert();
|
|
|
|
|
|
|
|
let tab = aWindow.gBrowser.addTab(url, {
|
|
|
|
owner: aWindow.gBrowser.selectedTab,
|
|
|
|
relatedToCurrent: true
|
|
|
|
});
|
|
|
|
aWindow.gBrowser.selectedTab = tab;
|
|
|
|
},
|
2014-01-17 03:21:33 -08:00
|
|
|
|
2014-03-03 17:39:35 -08:00
|
|
|
getConfiguration: function(aContentDocument, aConfiguration, aCallbackID) {
|
2014-01-17 03:21:33 -08:00
|
|
|
switch (aConfiguration) {
|
2014-03-03 17:39:35 -08:00
|
|
|
case "availableTargets":
|
|
|
|
this.getAvailableTargets(aContentDocument, aCallbackID);
|
|
|
|
break;
|
2014-01-17 03:21:33 -08:00
|
|
|
case "sync":
|
2014-03-03 17:39:35 -08:00
|
|
|
this.sendPageCallback(aContentDocument, aCallbackID, {
|
2014-01-17 03:21:33 -08:00
|
|
|
setup: Services.prefs.prefHasUserValue("services.sync.username"),
|
2014-03-03 17:39:35 -08:00
|
|
|
});
|
2014-01-17 03:21:33 -08:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
Cu.reportError("getConfiguration: Unknown configuration requested: " + aConfiguration);
|
|
|
|
break;
|
|
|
|
}
|
2014-03-03 17:39:35 -08:00
|
|
|
},
|
|
|
|
|
|
|
|
getAvailableTargets: function(aContentDocument, aCallbackID) {
|
|
|
|
let window = this.getChromeWindow(aContentDocument);
|
|
|
|
let data = this.availableTargetsCache.get(window);
|
|
|
|
if (data) {
|
|
|
|
this.sendPageCallback(aContentDocument, aCallbackID, data);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
let promises = [];
|
|
|
|
for (let targetName of this.targets.keys()) {
|
|
|
|
promises.push(this.getTarget(window, targetName));
|
|
|
|
}
|
|
|
|
Promise.all(promises).then((targetObjects) => {
|
|
|
|
let targetNames = [
|
|
|
|
"pinnedTab",
|
|
|
|
];
|
|
|
|
for (let targetObject of targetObjects) {
|
|
|
|
if (targetObject.node)
|
|
|
|
targetNames.push(targetObject.targetName);
|
|
|
|
}
|
|
|
|
let data = {
|
|
|
|
targets: targetNames,
|
|
|
|
};
|
|
|
|
this.availableTargetsCache.set(window, data);
|
|
|
|
this.sendPageCallback(aContentDocument, aCallbackID, data);
|
|
|
|
}, (err) => {
|
|
|
|
Cu.reportError(err);
|
|
|
|
this.sendPageCallback(aContentDocument, aCallbackID, {
|
|
|
|
targets: [],
|
|
|
|
});
|
|
|
|
});
|
2014-01-17 03:21:33 -08:00
|
|
|
},
|
2014-03-05 21:59:00 -08:00
|
|
|
|
|
|
|
_addAnnotationPanelMutationObserver: function(aPanelEl) {
|
|
|
|
#ifdef XP_LINUX
|
|
|
|
let observer = this._annotationPanelMutationObservers.get(aPanelEl);
|
|
|
|
if (observer) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
let win = aPanelEl.ownerDocument.defaultView;
|
|
|
|
observer = new win.MutationObserver(this._annotationMutationCallback);
|
|
|
|
this._annotationPanelMutationObservers.set(aPanelEl, observer);
|
|
|
|
let observerOptions = {
|
|
|
|
attributeFilter: ["height", "width"],
|
|
|
|
attributes: true,
|
|
|
|
};
|
|
|
|
observer.observe(aPanelEl, observerOptions);
|
|
|
|
#endif
|
|
|
|
},
|
|
|
|
|
|
|
|
_removeAnnotationPanelMutationObserver: function(aPanelEl) {
|
|
|
|
#ifdef XP_LINUX
|
|
|
|
let observer = this._annotationPanelMutationObservers.get(aPanelEl);
|
|
|
|
if (observer) {
|
|
|
|
observer.disconnect();
|
|
|
|
this._annotationPanelMutationObservers.delete(aPanelEl);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Workaround for Ubuntu panel craziness in bug 970788 where incorrect sizes get passed to
|
|
|
|
* nsXULPopupManager::PopupResized and lead to incorrect width and height attributes getting
|
|
|
|
* set on the panel.
|
|
|
|
*/
|
|
|
|
_annotationMutationCallback: function(aMutations) {
|
|
|
|
for (let mutation of aMutations) {
|
|
|
|
// Remove both attributes at once and ignore remaining mutations to be proccessed.
|
|
|
|
mutation.target.removeAttribute("width");
|
|
|
|
mutation.target.removeAttribute("height");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
},
|
2013-05-29 14:50:59 -07:00
|
|
|
};
|
2014-01-13 07:28:18 -08:00
|
|
|
|
2014-02-06 19:19:12 -08:00
|
|
|
this.UITour.init();
|