AWToolbarUtils.sys.mjs (2735B)
1 /* This Source Code Form is subject to the terms of the Mozilla Public 2 * License, v. 2.0. If a copy of the MPL was not distributed with this 3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 4 5 import { XPCOMUtils } from "resource://gre/modules/XPCOMUtils.sys.mjs"; 6 7 const lazy = {}; 8 const widgetId = "aboutwelcome-button"; 9 10 ChromeUtils.defineESModuleGetters(lazy, { 11 BrowserUsageTelemetry: "resource:///modules/BrowserUsageTelemetry.sys.mjs", 12 CustomizableUI: 13 "moz-src:///browser/components/customizableui/CustomizableUI.sys.mjs", 14 }); 15 16 export const AWToolbarButton = { 17 async maybeAddSetupButton() { 18 // First check if we've already completed onboarding, in which 19 // case we should remove the button. 20 if (AWToolbarButton.didSeeFinalScreen) { 21 AWToolbarButton.removeSetupButtonIfOnboardingComplete(); 22 return; 23 } 24 // Otherwise, check if this is a new profile where we need to add it. 25 if (AWToolbarButton.hasToolbarButtonEnabled) { 26 lazy.CustomizableUI.createWidget({ 27 id: widgetId, 28 l10nId: "browser-aboutwelcome-button", 29 defaultArea: lazy.CustomizableUI.AREA_BOOKMARKS, 30 type: "button", 31 onCreated(aNode) { 32 aNode.className = "bookmark-item chromeclass-toolbar-additional"; 33 lazy.BrowserUsageTelemetry.recordWidgetChange( 34 widgetId, 35 lazy.CustomizableUI.AREA_BOOKMARKS, 36 "create" 37 ); 38 }, 39 onCommand(aEvent) { 40 AWToolbarButton.openWelcome(aEvent.view); 41 }, 42 onDestroyed() { 43 lazy.BrowserUsageTelemetry.recordWidgetChange( 44 widgetId, 45 null, 46 "destroy" 47 ); 48 }, 49 }); 50 } 51 }, 52 53 removeSetupButtonIfOnboardingComplete() { 54 //Look for completion of Onboarding (we're using an AWFinish() 55 //call from about:welcome as a proxy here) 56 if ( 57 AWToolbarButton.didSeeFinalScreen || 58 !AWToolbarButton.hasToolbarButtonEnabled 59 ) { 60 lazy.CustomizableUI.destroyWidget(widgetId); 61 } 62 }, 63 64 openWelcome(win) { 65 Services.prefs.setStringPref( 66 "browser.aboutwelcome.entrypoint", 67 "toolbarButton" 68 ); 69 let viewURL = "about:welcome"; 70 win.gBrowser.addTrustedTab(viewURL, { 71 inBackground: false, 72 }); 73 }, 74 }; 75 76 XPCOMUtils.defineLazyPreferenceGetter( 77 AWToolbarButton, 78 "didSeeFinalScreen", 79 "browser.aboutwelcome.didSeeFinalScreen", 80 false, 81 () => { 82 AWToolbarButton.removeSetupButtonIfOnboardingComplete(); 83 } 84 ); 85 86 XPCOMUtils.defineLazyPreferenceGetter( 87 AWToolbarButton, 88 "hasToolbarButtonEnabled", 89 "browser.aboutwelcome.toolbarButtonEnabled", 90 false, 91 () => { 92 AWToolbarButton.removeSetupButtonIfOnboardingComplete(); 93 } 94 );