Merge mozilla-central to autoland. a=merge CLOSED TREE
[gecko.git] / browser / base / content / browser-profiles.js
blob72eca39e445b9771f34d6b549782a4c970d3b265
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/. */
5 // This file is loaded into the browser window scope.
6 /* eslint-env mozilla/browser-window */
8 XPCOMUtils.defineLazyServiceGetter(
9   this,
10   "ProfileService",
11   "@mozilla.org/toolkit/profile-service;1",
12   "nsIToolkitProfileService"
15 var gProfiles = {
16   init() {
17     XPCOMUtils.defineLazyPreferenceGetter(
18       this,
19       "PROFILES_ENABLED",
20       "browser.profiles.enabled",
21       false,
22       this.toggleProfileButtonsVisibility.bind(this)
23     );
25     if (!this.PROFILES_ENABLED) {
26       return;
27     }
29     this.toggleProfileButtonsVisibility();
30   },
32   toggleProfileButtonsVisibility() {
33     let profilesButton = PanelMultiView.getViewNode(
34       document,
35       "appMenu-profiles-button"
36     );
38     profilesButton.hidden = !this.PROFILES_ENABLED;
40     if (this.PROFILES_ENABLED) {
41       document.l10n.setArgs(profilesButton, {
42         profilename: ProfileService.currentProfile?.name ?? "",
43       });
44     }
45   },
47   updateView(panel) {
48     this.populateSubView();
49     PanelUI.showSubView("PanelUI-profiles", panel);
50   },
52   async populateSubView() {
53     let closeProfileButton = PanelMultiView.getViewNode(
54       document,
55       "profiles-close-profile-button"
56     );
57     document.l10n.setArgs(closeProfileButton, {
58       profilename: ProfileService.currentProfile?.name ?? "",
59     });
61     let profileIconEl = PanelMultiView.getViewNode(
62       document,
63       "profile-icon-image"
64     );
65     profileIconEl.style.listStyleImage = `url(${
66       ProfileService.currentProfile?.iconURL ??
67       "chrome://branding/content/icon64.png"
68     })`;
70     let profileNameEl = PanelMultiView.getViewNode(document, "profile-name");
71     profileNameEl.textContent = ProfileService.currentProfile?.name ?? "";
73     let profilesList = PanelMultiView.getViewNode(
74       document,
75       "PanelUI-profiles"
76     ).querySelector("#profiles-list");
77     while (profilesList.lastElementChild) {
78       profilesList.lastElementChild.remove();
79     }
81     for (let profile of ProfileService.profiles) {
82       if (profile === ProfileService.currentProfile) {
83         continue;
84       }
86       let button = document.createXULElement("toolbarbutton");
87       button.setAttribute("label", profile.name);
88       button.className = "subviewbutton subviewbutton-iconic";
89       button.style.listStyleImage = `url(${
90         profile.iconURL ?? "chrome://branding/content/icon16.png"
91       })`;
92       button.onclick = () => {
93         Services.startup.createInstanceWithProfile(profile);
94       };
96       profilesList.appendChild(button);
97     }
98   },