1 /* ***** BEGIN LICENSE BLOCK *****
2 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
4 * The contents of this file are subject to the Mozilla Public License Version
5 * 1.1 (the "License"); you may not use this file except in compliance with
6 * the License. You may obtain a copy of the License at
7 * http://www.mozilla.org/MPL/
9 * Software distributed under the License is distributed on an "AS IS" basis,
10 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
11 * for the specific language governing rights and limitations under the
14 * The Original Code is the Extension Manager.
16 * The Initial Developer of the Original Code is
17 * the Mozilla Foundation.
18 * Portions created by the Initial Developer are Copyright (C) 2009
19 * the Initial Developer. All Rights Reserved.
22 * Dão Gottwald <dao@mozilla.com>
24 * Alternatively, the contents of this file may be used under the terms of
25 * either the GNU General Public License Version 2 or later (the "GPL"), or
26 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
27 * in which case the provisions of the GPL or the LGPL are applicable instead
28 * of those above. If you wish to allow use of your version of this file only
29 * under the terms of either the GPL or the LGPL, and not to allow others to
30 * use your version of this file under the terms of the MPL, indicate your
31 * decision by deleting the provisions above and replace them with the notice
32 * and other provisions required by the GPL or the LGPL. If you do not delete
33 * the provisions above, a recipient may use your version of this file under
34 * the terms of any one of the MPL, the GPL or the LGPL.
36 * ***** END LICENSE BLOCK ***** */
38 var EXPORTED_SYMBOLS = ["LightweightThemeManager"];
40 const Cc = Components.classes;
41 const Ci = Components.interfaces;
43 Components.utils.import("resource://gre/modules/AddonManager.jsm");
44 Components.utils.import("resource://gre/modules/Services.jsm");
46 const ID_SUFFIX = "@personas.mozilla.org";
47 const PREF_LWTHEME_TO_SELECT = "extensions.lwThemeToSelect";
48 const PREF_GENERAL_SKINS_SELECTEDSKIN = "general.skins.selectedSkin";
49 const PREF_EM_DSS_ENABLED = "extensions.dss.enabled";
50 const ADDON_TYPE = "theme";
52 const DEFAULT_MAX_USED_THEMES_COUNT = 30;
54 const MAX_PREVIEW_SECONDS = 30;
56 const MANDATORY = ["id", "name", "headerURL"];
57 const OPTIONAL = ["footerURL", "textcolor", "accentcolor", "iconURL",
58 "previewURL", "author", "description", "homepageURL",
59 "updateURL", "version"];
61 const PERSIST_ENABLED = true;
62 const PERSIST_BYPASS_CACHE = false;
63 const PERSIST_FILES = {
64 headerURL: "lightweighttheme-header",
65 footerURL: "lightweighttheme-footer"
68 __defineGetter__("_prefs", function () {
70 return this._prefs = Services.prefs.getBranch("lightweightThemes.")
71 .QueryInterface(Ci.nsIPrefBranch2);
74 __defineGetter__("_maxUsedThemes", function() {
75 delete this._maxUsedThemes;
77 this._maxUsedThemes = _prefs.getIntPref("maxUsedThemes");
80 this._maxUsedThemes = DEFAULT_MAX_USED_THEMES_COUNT;
82 return this._maxUsedThemes;
85 __defineSetter__("_maxUsedThemes", function(aVal) {
86 delete this._maxUsedThemes;
87 return this._maxUsedThemes = aVal;
90 // Holds the ID of the theme being enabled while sending out the events so
91 // cached AddonWrapper instances can return correct values for permissions and
93 var _themeIDBeingEnabled = null;
95 var LightweightThemeManager = {
98 return JSON.parse(_prefs.getComplexValue("usedThemes",
99 Ci.nsISupportsString).data);
105 get currentTheme () {
107 if (_prefs.getBoolPref("isThemeSelected"))
108 var data = this.usedThemes[0];
114 get currentThemeForDisplay () {
115 var data = this.currentTheme;
117 if (data && PERSIST_ENABLED) {
118 for (let key in PERSIST_FILES) {
120 if (data[key] && _prefs.getBoolPref("persisted." + key))
121 data[key] = _getLocalImageURI(PERSIST_FILES[key]).spec
122 + "?" + data.id + ";" + _version(data);
130 set currentTheme (aData) {
131 return _setCurrentTheme(aData, false);
134 setLocalTheme: function (aData) {
135 _setCurrentTheme(aData, true);
138 getUsedTheme: function (aId) {
139 var usedThemes = this.usedThemes;
140 for (let i = 0; i < usedThemes.length; i++) {
141 if (usedThemes[i].id == aId)
142 return usedThemes[i];
147 forgetUsedTheme: function (aId) {
148 let theme = this.getUsedTheme(aId);
152 let wrapper = new AddonWrapper(theme);
153 AddonManagerPrivate.callAddonListeners("onUninstalling", wrapper, false);
155 var currentTheme = this.currentTheme;
156 if (currentTheme && currentTheme.id == aId) {
157 this.themeChanged(null);
158 AddonManagerPrivate.notifyAddonChanged(null, ADDON_TYPE, false);
161 _updateUsedThemes(_usedThemesExceptId(aId));
162 AddonManagerPrivate.callAddonListeners("onUninstalled", wrapper);
165 previewTheme: function (aData) {
169 let cancel = Cc["@mozilla.org/supports-PRBool;1"].createInstance(Ci.nsISupportsPRBool);
171 Services.obs.notifyObservers(cancel, "lightweight-theme-preview-requested",
172 JSON.stringify(aData));
177 _previewTimer.cancel();
179 _previewTimer = Cc["@mozilla.org/timer;1"].createInstance(Ci.nsITimer);
180 _previewTimer.initWithCallback(_previewTimerCallback,
181 MAX_PREVIEW_SECONDS * 1000,
182 _previewTimer.TYPE_ONE_SHOT);
184 _notifyWindows(aData);
187 resetPreview: function () {
189 _previewTimer.cancel();
190 _previewTimer = null;
191 _notifyWindows(this.currentThemeForDisplay);
195 parseTheme: function (aString, aBaseURI) {
197 return _sanitizeTheme(JSON.parse(aString), aBaseURI, false);
203 updateCurrentTheme: function () {
205 if (!_prefs.getBoolPref("update.enabled"))
211 var theme = this.currentTheme;
212 if (!theme || !theme.updateURL)
215 var req = Cc["@mozilla.org/xmlextras/xmlhttprequest;1"]
216 .createInstance(Ci.nsIXMLHttpRequest);
218 req.mozBackgroundRequest = true;
219 req.overrideMimeType("text/plain");
220 req.open("GET", theme.updateURL, true);
223 req.onload = function () {
224 if (req.status != 200)
227 let newData = self.parseTheme(req.responseText, theme.updateURL);
229 newData.id != theme.id ||
230 _version(newData) == _version(theme))
233 var currentTheme = self.currentTheme;
234 if (currentTheme && currentTheme.id == theme.id)
235 self.currentTheme = newData;
242 * Switches to a new lightweight theme.
245 * The lightweight theme to switch to
247 themeChanged: function(aData) {
249 _previewTimer.cancel();
250 _previewTimer = null;
254 let usedThemes = _usedThemesExceptId(aData.id);
255 usedThemes.unshift(aData);
256 _updateUsedThemes(usedThemes);
258 _persistImages(aData);
261 _prefs.setBoolPref("isThemeSelected", aData != null);
262 _notifyWindows(aData);
263 Services.obs.notifyObservers(null, "lightweight-theme-changed", null);
267 * Starts the Addons provider and enables the new lightweight theme if
270 startup: function() {
271 if (Services.prefs.prefHasUserValue(PREF_LWTHEME_TO_SELECT)) {
272 let id = Services.prefs.getCharPref(PREF_LWTHEME_TO_SELECT);
274 this.themeChanged(this.getUsedTheme(id));
276 this.themeChanged(null);
277 Services.prefs.clearUserPref(PREF_LWTHEME_TO_SELECT);
280 _prefs.addObserver("", _prefObserver, false);
284 * Shuts down the provider.
286 shutdown: function() {
287 _prefs.removeObserver("", _prefObserver);
291 * Called when a new add-on has been enabled when only one add-on of that type
295 * The ID of the newly enabled add-on
297 * The type of the newly enabled add-on
298 * @param aPendingRestart
299 * true if the newly enabled add-on will only become enabled after a
302 addonChanged: function(aId, aType, aPendingRestart) {
303 if (aType != ADDON_TYPE)
306 let id = _getInternalID(aId);
307 let current = this.currentTheme;
310 let next = Services.prefs.getCharPref(PREF_LWTHEME_TO_SELECT);
311 if (id == next && aPendingRestart)
314 Services.prefs.clearUserPref(PREF_LWTHEME_TO_SELECT);
316 AddonManagerPrivate.callAddonListeners("onOperationCancelled",
317 new AddonWrapper(this.getUsedTheme(next)));
320 if (id == current.id) {
321 AddonManagerPrivate.callAddonListeners("onOperationCancelled",
322 new AddonWrapper(current));
331 if (current.id == id)
333 let wrapper = new AddonWrapper(current);
334 if (aPendingRestart) {
335 Services.prefs.setCharPref(PREF_LWTHEME_TO_SELECT, "");
336 AddonManagerPrivate.callAddonListeners("onDisabling", wrapper, true);
339 AddonManagerPrivate.callAddonListeners("onDisabling", wrapper, false);
340 this.themeChanged(null);
341 AddonManagerPrivate.callAddonListeners("onDisabled", wrapper);
346 let theme = this.getUsedTheme(id);
347 _themeIDBeingEnabled = id;
348 let wrapper = new AddonWrapper(theme);
349 if (aPendingRestart) {
350 AddonManagerPrivate.callAddonListeners("onEnabling", wrapper, true);
351 Services.prefs.setCharPref(PREF_LWTHEME_TO_SELECT, id);
353 // Flush the preferences to disk so they survive any crash
354 Services.prefs.savePrefFile(null);
357 AddonManagerPrivate.callAddonListeners("onEnabling", wrapper, false);
358 this.themeChanged(theme);
359 AddonManagerPrivate.callAddonListeners("onEnabled", wrapper);
361 _themeIDBeingEnabled = null;
366 * Called to get an Addon with a particular ID.
369 * The ID of the add-on to retrieve
371 * A callback to pass the Addon to
373 getAddonByID: function(aId, aCallback) {
374 let id = _getInternalID(aId);
380 let theme = this.getUsedTheme(id);
386 aCallback(new AddonWrapper(theme));
390 * Called to get Addons of a particular type.
393 * An array of types to fetch. Can be null to get all types.
395 * A callback to pass an array of Addons to
397 getAddonsByTypes: function(aTypes, aCallback) {
398 if (aTypes && aTypes.indexOf(ADDON_TYPE) == -1) {
403 aCallback([new AddonWrapper(a) for each (a in this.usedThemes)]);
408 * The AddonWrapper wraps lightweight theme to provide the data visible to
409 * consumers of the AddonManager API.
411 function AddonWrapper(aTheme) {
412 this.__defineGetter__("id", function() aTheme.id + ID_SUFFIX);
413 this.__defineGetter__("type", function() ADDON_TYPE);
414 this.__defineGetter__("isActive", function() {
415 let current = LightweightThemeManager.currentTheme;
417 return aTheme.id == current.id;
421 this.__defineGetter__("name", function() aTheme.name);
422 this.__defineGetter__("version", function() {
423 return "version" in aTheme ? aTheme.version : "";
426 ["description", "homepageURL", "iconURL"].forEach(function(prop) {
427 this.__defineGetter__(prop, function() {
428 return prop in aTheme ? aTheme[prop] : null;
432 ["installDate", "updateDate"].forEach(function(prop) {
433 this.__defineGetter__(prop, function() {
434 return prop in aTheme ? new Date(aTheme[prop]) : null;
438 this.__defineGetter__("creator", function() {
439 return new AddonManagerPrivate.AddonAuthor(aTheme.author);
442 this.__defineGetter__("screenshots", function() {
443 let url = aTheme.previewURL;
444 return [new AddonManagerPrivate.AddonScreenshot(url)];
447 this.__defineGetter__("pendingOperations", function() {
448 let pending = AddonManager.PENDING_NONE;
449 if (this.isActive == this.userDisabled)
450 pending |= this.isActive ? AddonManager.PENDING_DISABLE : AddonManager.PENDING_ENABLE;
454 this.__defineGetter__("operationsRequiringRestart", function() {
455 // If a non-default theme is in use then a restart will be required to
456 // enable lightweight themes unless dynamic theme switching is enabled
457 if (Services.prefs.prefHasUserValue(PREF_GENERAL_SKINS_SELECTEDSKIN)) {
459 if (Services.prefs.getBoolPref(PREF_EM_DSS_ENABLED))
460 return AddonManager.OP_NEEDS_RESTART_NONE;
464 return AddonManager.OP_NEEDS_RESTART_ENABLE;
467 return AddonManager.OP_NEEDS_RESTART_NONE;
470 this.__defineGetter__("size", function() {
471 // The size changes depending on whether the theme is in use or not, this is
472 // probably not worth exposing.
476 this.__defineGetter__("permissions", function() {
477 let permissions = AddonManager.PERM_CAN_UNINSTALL;
478 if (this.userDisabled)
479 permissions |= AddonManager.PERM_CAN_ENABLE;
483 this.__defineGetter__("userDisabled", function() {
484 if (_themeIDBeingEnabled == aTheme.id)
488 let toSelect = Services.prefs.getCharPref(PREF_LWTHEME_TO_SELECT);
489 return aTheme.id != toSelect;
492 let current = LightweightThemeManager.currentTheme;
493 return !current || current.id != aTheme.id;
497 this.__defineSetter__("userDisabled", function(val) {
498 if (val == this.userDisabled)
502 LightweightThemeManager.currentTheme = null;
504 LightweightThemeManager.currentTheme = aTheme;
509 this.uninstall = function() {
510 LightweightThemeManager.forgetUsedTheme(aTheme.id);
513 this.cancelUninstall = function() {
514 throw new Error("Theme is not marked to be uninstalled");
517 this.findUpdates = function(listener, reason, appVersion, platformVersion) {
518 if ("onNoCompatibilityUpdateAvailable" in listener)
519 listener.onNoCompatibilityUpdateAvailable(this);
520 if ("onNoUpdateAvailable" in listener)
521 listener.onNoUpdateAvailable(this);
522 if ("onUpdateFinished" in listener)
523 listener.onUpdateFinished(this);
527 AddonWrapper.prototype = {
528 // Lightweight themes are never disabled by the application
533 // Lightweight themes are always compatible
538 get isPlatformCompatible() {
543 return AddonManager.SCOPE_PROFILE;
546 // Lightweight themes are always compatible
547 isCompatibleWith: function(appVersion, platformVersion) {
551 // Lightweight themes are always securely updated
552 get providesUpdatesSecurely() {
556 // Lightweight themes are never blocklisted
557 get blocklistState() {
558 return Ci.nsIBlocklistService.STATE_NOT_BLOCKED;
563 * Converts the ID used by the public AddonManager API to an lightweight theme
567 * The ID to be converted
569 * @return the lightweight theme ID or null if the ID was not for a lightweight
572 function _getInternalID(id) {
575 let len = id.length - ID_SUFFIX.length;
576 if (len > 0 && id.substring(len) == ID_SUFFIX)
577 return id.substring(0, len);
581 function _setCurrentTheme(aData, aLocal) {
582 aData = _sanitizeTheme(aData, null, aLocal);
584 let needsRestart = (ADDON_TYPE == "theme") &&
585 Services.prefs.prefHasUserValue(PREF_GENERAL_SKINS_SELECTEDSKIN);
587 let cancel = Cc["@mozilla.org/supports-PRBool;1"].createInstance(Ci.nsISupportsPRBool);
589 Services.obs.notifyObservers(cancel, "lightweight-theme-change-requested",
590 JSON.stringify(aData));
593 let theme = LightweightThemeManager.getUsedTheme(aData.id);
594 let isInstall = !theme || theme.version != aData.version;
596 aData.updateDate = Date.now();
597 if (theme && "installDate" in theme)
598 aData.installDate = theme.installDate;
600 aData.installDate = aData.updateDate;
602 var oldWrapper = theme ? new AddonWrapper(theme) : null;
603 var wrapper = new AddonWrapper(aData);
604 AddonManagerPrivate.callInstallListeners("onExternalInstall", null,
605 wrapper, oldWrapper, false);
606 AddonManagerPrivate.callAddonListeners("onInstalling", wrapper, false);
609 let current = LightweightThemeManager.currentTheme;
610 let usedThemes = _usedThemesExceptId(aData.id);
611 if (current && current.id != aData.id)
612 usedThemes.splice(1, 0, aData);
614 usedThemes.unshift(aData);
615 _updateUsedThemes(usedThemes);
618 AddonManagerPrivate.callAddonListeners("onInstalled", wrapper);
624 AddonManagerPrivate.notifyAddonChanged(aData ? aData.id + ID_SUFFIX : null,
625 ADDON_TYPE, needsRestart);
627 return LightweightThemeManager.currentTheme;
630 function _sanitizeTheme(aData, aBaseURI, aLocal) {
631 if (!aData || typeof aData != "object")
634 var resourceProtocols = ["http", "https"];
636 resourceProtocols.push("file");
637 var resourceProtocolExp = new RegExp("^(" + resourceProtocols.join("|") + "):");
639 function sanitizeProperty(prop) {
640 if (!(prop in aData))
642 if (typeof aData[prop] != "string")
644 let val = aData[prop].trim();
648 if (!/URL$/.test(prop))
652 val = _makeURI(val, aBaseURI ? _makeURI(aBaseURI) : null).spec;
653 if ((prop == "updateURL" ? /^https:/ : resourceProtocolExp).test(val))
663 for (let i = 0; i < MANDATORY.length; i++) {
664 let val = sanitizeProperty(MANDATORY[i]);
666 throw Components.results.NS_ERROR_INVALID_ARG;
667 result[MANDATORY[i]] = val;
670 for (let i = 0; i < OPTIONAL.length; i++) {
671 let val = sanitizeProperty(OPTIONAL[i]);
674 result[OPTIONAL[i]] = val;
680 function _usedThemesExceptId(aId)
681 LightweightThemeManager.usedThemes.filter(function (t) "id" in t && t.id != aId);
683 function _version(aThemeData)
684 aThemeData.version || "";
686 function _makeURI(aURL, aBaseURI)
687 Services.io.newURI(aURL, null, aBaseURI);
689 function _updateUsedThemes(aList) {
690 // Send uninstall events for all themes that need to be removed.
691 while (aList.length > _maxUsedThemes) {
692 let wrapper = new AddonWrapper(aList[aList.length - 1]);
693 AddonManagerPrivate.callAddonListeners("onUninstalling", wrapper, false);
695 AddonManagerPrivate.callAddonListeners("onUninstalled", wrapper);
698 var str = Cc["@mozilla.org/supports-string;1"]
699 .createInstance(Ci.nsISupportsString);
700 str.data = JSON.stringify(aList);
701 _prefs.setComplexValue("usedThemes", Ci.nsISupportsString, str);
703 Services.obs.notifyObservers(null, "lightweight-theme-list-changed", null);
706 function _notifyWindows(aThemeData) {
707 Services.obs.notifyObservers(null, "lightweight-theme-styling-update",
708 JSON.stringify(aThemeData));
712 var _previewTimerCallback = {
713 notify: function () {
714 LightweightThemeManager.resetPreview();
719 * Called when any of the lightweightThemes preferences are changed.
721 function _prefObserver(aSubject, aTopic, aData) {
723 case "maxUsedThemes":
725 _maxUsedThemes = _prefs.getIntPref(aData);
728 _maxUsedThemes = DEFAULT_MAX_USED_THEMES_COUNT;
730 // Update the theme list to remove any themes over the number we keep
731 _updateUsedThemes(LightweightThemeManager.usedThemes);
736 function _persistImages(aData) {
737 function onSuccess(key) function () {
738 let current = LightweightThemeManager.currentTheme;
739 if (current && current.id == aData.id)
740 _prefs.setBoolPref("persisted." + key, true);
743 for (let key in PERSIST_FILES) {
744 _prefs.setBoolPref("persisted." + key, false);
746 _persistImage(aData[key], PERSIST_FILES[key], onSuccess(key));
750 function _getLocalImageURI(localFileName) {
751 var localFile = Services.dirsvc.get("ProfD", Ci.nsILocalFile);
752 localFile.append(localFileName);
753 return Services.io.newFileURI(localFile);
756 function _persistImage(sourceURL, localFileName, successCallback) {
757 if (/^file:/.test(sourceURL))
760 var targetURI = _getLocalImageURI(localFileName);
761 var sourceURI = _makeURI(sourceURL);
763 var persist = Cc["@mozilla.org/embedding/browser/nsWebBrowserPersist;1"]
764 .createInstance(Ci.nsIWebBrowserPersist);
766 persist.persistFlags =
767 Ci.nsIWebBrowserPersist.PERSIST_FLAGS_REPLACE_EXISTING_FILES |
768 Ci.nsIWebBrowserPersist.PERSIST_FLAGS_AUTODETECT_APPLY_CONVERSION |
769 (PERSIST_BYPASS_CACHE ?
770 Ci.nsIWebBrowserPersist.PERSIST_FLAGS_BYPASS_CACHE :
771 Ci.nsIWebBrowserPersist.PERSIST_FLAGS_FROM_CACHE);
773 persist.progressListener = new _persistProgressListener(successCallback);
775 persist.saveURI(sourceURI, null, null, null, null, targetURI);
778 function _persistProgressListener(successCallback) {
779 this.onLocationChange = function () {};
780 this.onProgressChange = function () {};
781 this.onStatusChange = function () {};
782 this.onSecurityChange = function () {};
783 this.onStateChange = function (aWebProgress, aRequest, aStateFlags, aStatus) {
785 aStateFlags & Ci.nsIWebProgressListener.STATE_IS_NETWORK &&
786 aStateFlags & Ci.nsIWebProgressListener.STATE_STOP) {
788 if (aRequest.QueryInterface(Ci.nsIHttpChannel).requestSucceeded) {
799 AddonManagerPrivate.registerProvider(LightweightThemeManager);