From f1c2cbc686215599857dea6dfa3153d389060d55 Mon Sep 17 00:00:00 2001 From: Jonathan Kingston Date: Wed, 22 Feb 2017 17:02:53 +0000 Subject: [PATCH] Add ability to check for theme dark on changing themes. Fixes #207 --- index.js | 51 ++++++++++++++++++++++++++++++++++++++ webextension/background.js | 47 +++++++++++++++++++++++++++++++++++ 2 files changed, 98 insertions(+) diff --git a/index.js b/index.js index 1615cad..2b9a1fa 100644 --- a/index.js +++ b/index.js @@ -52,10 +52,12 @@ const { attachTo, detachFrom } = require("sdk/content/mod"); const { Cu } = require("chrome"); const { ContextualIdentityService } = require("resource://gre/modules/ContextualIdentityService.jsm"); const { getFavicon } = require("sdk/places/favicon"); +const { LightweightThemeManager } = Cu.import("resource://gre/modules/LightweightThemeManager.jsm", {}); const Metrics = require("./testpilot-metrics"); const { modelFor } = require("sdk/model/core"); const prefService = require("sdk/preferences/service"); const self = require("sdk/self"); +const { Services } = require("resource://gre/modules/Services.jsm"); const ss = require("sdk/simple-storage"); const { Style } = require("sdk/stylesheet/style"); const tabs = require("sdk/tabs"); @@ -115,6 +117,7 @@ const ContainerService = { _identitiesState: {}, _windowMap: new Map(), _containerWasEnabled: false, + _onThemeChangedCallback: null, init(installation) { // If we are just been installed, we must store some information for the @@ -196,6 +199,7 @@ const ContainerService = { "updateIdentity", "getPreference", "sendTelemetryPayload", + "getTheme", "checkIncompatibleAddons" ]; @@ -254,6 +258,8 @@ const ContainerService = { sendReply(this[message.method](message)); } }); + + this.registerThemeConnection(api); }).catch(() => { throw new Error("WebExtension startup failed. Unable to continue."); }); @@ -292,6 +298,51 @@ const ContainerService = { }; } // End-Of-Hack + + Services.obs.addObserver(this, "lightweight-theme-changed", false); + }, + + registerThemeConnection(api) { + // This is only used for theme notifications + api.browser.runtime.onConnect.addListener((port) => { + this.onThemeChanged((theme, topic) => { + port.postMessage({ + type: topic, + theme + }); + }); + }); + }, + + triggerThemeChanged(theme, topic) { + if (this._onThemeChangedCallback) { + this._onThemeChangedCallback(theme, topic); + } + }, + + observe(subject, topic) { + if (topic === "lightweight-theme-changed") { + this.getTheme().then((theme) => { + this.triggerThemeChanged(theme, topic); + }).catch(() => { + throw new Error("Unable to get theme"); + }); + } + }, + + getTheme() { + const defaultTheme = "firefox-compact-light@mozilla.org"; + return new Promise(function (resolve) { + let theme = defaultTheme; + if (LightweightThemeManager.currentTheme && LightweightThemeManager.currentTheme.id) { + theme = LightweightThemeManager.currentTheme.id; + } + resolve(theme); + }); + }, + + onThemeChanged(callback) { + this._onThemeChangedCallback = callback; }, // utility methods diff --git a/webextension/background.js b/webextension/background.js index 3875580..e909c6b 100644 --- a/webextension/background.js +++ b/webextension/background.js @@ -1,3 +1,50 @@ + +const themeManager = { + existingTheme: null, + init() { + this.check(); + + const port = browser.runtime.connect(); + port.onMessage.addListener(m => { + if (m.type === "lightweight-theme-changed") { + this.update(m.theme); + } + }); + }, + setPopupIcon(theme) { + let icons = { + 16: "img/container-site-d-24.png", + 32: "img/container-site-d-48.png" + }; + if (theme === "firefox-compact-dark@mozilla.org") { + icons = { + 16: "img/container-site-w-24.png", + 32: "img/container-site-w-48.png" + }; + } + browser.browserAction.setIcon({ + path: icons + }); + }, + check() { + browser.runtime.sendMessage({ + method: "getTheme" + }).then((theme) => { + this.update(theme); + }).catch(() => { + throw new Error("Unable to get theme"); + }); + }, + update(theme) { + if (this.existingTheme !== theme) { + this.setPopupIcon(theme); + this.existingTheme = theme; + } + } +}; + +themeManager.init(); + browser.runtime.sendMessage({ method: "getPreference", pref: "browser.privatebrowsing.autostart"