forked from mirrors/gecko-dev
MozReview-Commit-ID: 1g9d4UTuOgr --HG-- extra : rebase_source : 29e07cff103e7751bf4ca414a88f89136d3ac237
171 lines
6.1 KiB
JavaScript
171 lines
6.1 KiB
JavaScript
/* 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/. */
|
|
/* import-globals-from ext-browser.js */
|
|
|
|
"use strict";
|
|
|
|
XPCOMUtils.defineLazyModuleGetter(this, "ExtensionSettingsStore",
|
|
"resource://gre/modules/ExtensionSettingsStore.jsm");
|
|
XPCOMUtils.defineLazyModuleGetter(this, "AddonManager",
|
|
"resource://gre/modules/AddonManager.jsm");
|
|
XPCOMUtils.defineLazyModuleGetter(this, "CustomizableUI",
|
|
"resource:///modules/CustomizableUI.jsm");
|
|
|
|
XPCOMUtils.defineLazyServiceGetter(this, "aboutNewTabService",
|
|
"@mozilla.org/browser/aboutnewtab-service;1",
|
|
"nsIAboutNewTabService");
|
|
|
|
const STORE_TYPE = "url_overrides";
|
|
const NEW_TAB_SETTING_NAME = "newTabURL";
|
|
const NEW_TAB_CONFIRMED_TYPE = "newTabNotification";
|
|
|
|
function userWasNotified(extensionId) {
|
|
let setting = ExtensionSettingsStore.getSetting(NEW_TAB_CONFIRMED_TYPE, extensionId);
|
|
return setting && setting.value;
|
|
}
|
|
|
|
async function handleNewTabOpened() {
|
|
// We don't need to open the doorhanger again until the controlling add-on changes.
|
|
// eslint-disable-next-line no-use-before-define
|
|
removeNewTabObserver();
|
|
|
|
let item = ExtensionSettingsStore.getSetting(STORE_TYPE, NEW_TAB_SETTING_NAME);
|
|
|
|
if (!item || !item.id || userWasNotified(item.id)) {
|
|
return;
|
|
}
|
|
|
|
// Find the elements we need.
|
|
let win = windowTracker.getCurrentWindow({});
|
|
let doc = win.document;
|
|
let panel = doc.getElementById("extension-notification-panel");
|
|
|
|
// Setup the command handler.
|
|
let handleCommand = async (event) => {
|
|
if (event.originalTarget.getAttribute("anonid") == "button") {
|
|
// Main action is to keep changes.
|
|
await ExtensionSettingsStore.addSetting(
|
|
item.id, NEW_TAB_CONFIRMED_TYPE, item.id, true, () => false);
|
|
} else {
|
|
// Secondary action is to restore settings.
|
|
ExtensionSettingsStore.removeSetting(NEW_TAB_CONFIRMED_TYPE, item.id);
|
|
let addon = await AddonManager.getAddonByID(item.id);
|
|
addon.userDisabled = true;
|
|
}
|
|
panel.hidePopup();
|
|
win.gURLBar.focus();
|
|
};
|
|
panel.addEventListener("command", handleCommand);
|
|
panel.addEventListener("popuphidden", () => {
|
|
panel.removeEventListener("command", handleCommand);
|
|
}, {once: true});
|
|
|
|
// Look for a browserAction on the toolbar.
|
|
let action = CustomizableUI.getWidget(
|
|
`${global.makeWidgetId(item.id)}-browser-action`);
|
|
if (action) {
|
|
action = action.areaType == "toolbar" && action.forWindow(win).node;
|
|
}
|
|
|
|
// Anchor to a toolbar browserAction if found, otherwise use the menu button.
|
|
let anchor = doc.getAnonymousElementByAttribute(
|
|
action || doc.getElementById("PanelUI-menu-button"),
|
|
"class", "toolbarbutton-icon");
|
|
panel.hidden = false;
|
|
panel.openPopup(anchor);
|
|
}
|
|
|
|
let newTabOpenedListener = {
|
|
observe(subject, topic, data) {
|
|
// Do this work in an idle callback to avoid interfering with new tab performance tracking.
|
|
windowTracker
|
|
.getCurrentWindow({})
|
|
.requestIdleCallback(handleNewTabOpened);
|
|
},
|
|
};
|
|
|
|
function removeNewTabObserver() {
|
|
if (aboutNewTabService.willNotifyUser) {
|
|
Services.obs.removeObserver(newTabOpenedListener, "browser-open-newtab-start");
|
|
aboutNewTabService.willNotifyUser = false;
|
|
}
|
|
}
|
|
|
|
function addNewTabObserver(extensionId) {
|
|
if (!aboutNewTabService.willNotifyUser && extensionId && !userWasNotified(extensionId)) {
|
|
Services.obs.addObserver(newTabOpenedListener, "browser-open-newtab-start");
|
|
aboutNewTabService.willNotifyUser = true;
|
|
}
|
|
}
|
|
|
|
function setNewTabURL(extensionId, url) {
|
|
aboutNewTabService.newTabURL = url;
|
|
if (aboutNewTabService.overridden) {
|
|
addNewTabObserver(extensionId);
|
|
} else {
|
|
removeNewTabObserver();
|
|
}
|
|
}
|
|
|
|
this.urlOverrides = class extends ExtensionAPI {
|
|
processNewTabSetting(action) {
|
|
let {extension} = this;
|
|
let item = ExtensionSettingsStore[action](extension.id, STORE_TYPE, NEW_TAB_SETTING_NAME);
|
|
if (item) {
|
|
setNewTabURL(item.id, item.value || item.initialValue);
|
|
}
|
|
}
|
|
|
|
async onManifestEntry(entryName) {
|
|
let {extension} = this;
|
|
let {manifest} = extension;
|
|
|
|
await ExtensionSettingsStore.initialize();
|
|
|
|
if (manifest.chrome_url_overrides.newtab) {
|
|
// Set up the shutdown code for the setting.
|
|
extension.callOnClose({
|
|
close: () => {
|
|
if (extension.shutdownReason == "ADDON_DISABLE"
|
|
|| extension.shutdownReason == "ADDON_UNINSTALL") {
|
|
ExtensionSettingsStore.removeSetting(
|
|
extension.id, NEW_TAB_CONFIRMED_TYPE, extension.id);
|
|
}
|
|
switch (extension.shutdownReason) {
|
|
case "ADDON_DISABLE":
|
|
this.processNewTabSetting("disable");
|
|
break;
|
|
|
|
// We can remove the setting on upgrade or downgrade because it will be
|
|
// added back in when the manifest is re-read. This will cover the case
|
|
// where a new version of an add-on removes the manifest key.
|
|
case "ADDON_DOWNGRADE":
|
|
case "ADDON_UPGRADE":
|
|
case "ADDON_UNINSTALL":
|
|
this.processNewTabSetting("removeSetting");
|
|
break;
|
|
}
|
|
},
|
|
});
|
|
|
|
let url = extension.baseURI.resolve(manifest.chrome_url_overrides.newtab);
|
|
|
|
let item = await ExtensionSettingsStore.addSetting(
|
|
extension.id, STORE_TYPE, NEW_TAB_SETTING_NAME, url,
|
|
() => aboutNewTabService.newTabURL);
|
|
|
|
// If the extension was just re-enabled, change the setting to enabled.
|
|
// This is required because addSetting above is used for both add and update.
|
|
if (["ADDON_ENABLE", "ADDON_UPGRADE", "ADDON_DOWNGRADE"]
|
|
.includes(extension.startupReason)) {
|
|
item = ExtensionSettingsStore.enable(extension.id, STORE_TYPE, NEW_TAB_SETTING_NAME);
|
|
}
|
|
|
|
// Set the newTabURL to the current value of the setting.
|
|
if (item) {
|
|
setNewTabURL(extension.id, item.value || item.initialValue);
|
|
}
|
|
}
|
|
}
|
|
};
|