2012-05-21 11:12:37 +00:00
|
|
|
/* 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/. */
|
2009-09-02 16:23:40 +00:00
|
|
|
|
2011-07-19 20:16:35 +00:00
|
|
|
"use strict";
|
|
|
|
|
2009-09-02 16:23:40 +00:00
|
|
|
var EXPORTED_SYMBOLS = ["LightweightThemeManager"];
|
|
|
|
|
|
|
|
const Cc = Components.classes;
|
|
|
|
const Ci = Components.interfaces;
|
2009-11-17 07:55:47 +00:00
|
|
|
|
2012-09-22 19:24:26 +00:00
|
|
|
Components.utils.import("resource://gre/modules/XPCOMUtils.jsm");
|
2010-04-29 20:11:23 +00:00
|
|
|
Components.utils.import("resource://gre/modules/AddonManager.jsm");
|
|
|
|
Components.utils.import("resource://gre/modules/Services.jsm");
|
|
|
|
|
|
|
|
const ID_SUFFIX = "@personas.mozilla.org";
|
|
|
|
const PREF_LWTHEME_TO_SELECT = "extensions.lwThemeToSelect";
|
|
|
|
const PREF_GENERAL_SKINS_SELECTEDSKIN = "general.skins.selectedSkin";
|
2010-09-01 17:02:28 +00:00
|
|
|
const PREF_EM_DSS_ENABLED = "extensions.dss.enabled";
|
2010-04-29 20:11:23 +00:00
|
|
|
const ADDON_TYPE = "theme";
|
|
|
|
|
2011-05-20 17:36:28 +00:00
|
|
|
const URI_EXTENSION_STRINGS = "chrome://mozapps/locale/extensions/extensions.properties";
|
|
|
|
|
|
|
|
const STRING_TYPE_NAME = "type.%ID%.name";
|
|
|
|
|
2010-07-27 01:31:10 +00:00
|
|
|
const DEFAULT_MAX_USED_THEMES_COUNT = 30;
|
2009-11-17 07:55:47 +00:00
|
|
|
|
2009-09-02 16:23:40 +00:00
|
|
|
const MAX_PREVIEW_SECONDS = 30;
|
2009-11-17 07:55:47 +00:00
|
|
|
|
|
|
|
const MANDATORY = ["id", "name", "headerURL"];
|
|
|
|
const OPTIONAL = ["footerURL", "textcolor", "accentcolor", "iconURL",
|
|
|
|
"previewURL", "author", "description", "homepageURL",
|
|
|
|
"updateURL", "version"];
|
|
|
|
|
2009-10-09 18:25:58 +00:00
|
|
|
const PERSIST_ENABLED = true;
|
|
|
|
const PERSIST_BYPASS_CACHE = false;
|
|
|
|
const PERSIST_FILES = {
|
|
|
|
headerURL: "lightweighttheme-header",
|
|
|
|
footerURL: "lightweighttheme-footer"
|
|
|
|
};
|
|
|
|
|
2012-09-22 19:24:26 +00:00
|
|
|
XPCOMUtils.defineLazyModuleGetter(this, "LightweightThemeImageOptimizer",
|
|
|
|
"resource://gre/modules/LightweightThemeImageOptimizer.jsm");
|
|
|
|
|
2009-10-09 18:25:58 +00:00
|
|
|
__defineGetter__("_prefs", function () {
|
|
|
|
delete this._prefs;
|
2012-01-17 01:18:29 +00:00
|
|
|
return this._prefs = Services.prefs.getBranch("lightweightThemes.");
|
2009-10-09 18:25:58 +00:00
|
|
|
});
|
2009-09-02 16:23:40 +00:00
|
|
|
|
2010-07-27 01:31:10 +00:00
|
|
|
__defineGetter__("_maxUsedThemes", function() {
|
|
|
|
delete this._maxUsedThemes;
|
|
|
|
try {
|
|
|
|
this._maxUsedThemes = _prefs.getIntPref("maxUsedThemes");
|
|
|
|
}
|
|
|
|
catch (e) {
|
|
|
|
this._maxUsedThemes = DEFAULT_MAX_USED_THEMES_COUNT;
|
|
|
|
}
|
|
|
|
return this._maxUsedThemes;
|
|
|
|
});
|
|
|
|
|
|
|
|
__defineSetter__("_maxUsedThemes", function(aVal) {
|
|
|
|
delete this._maxUsedThemes;
|
|
|
|
return this._maxUsedThemes = aVal;
|
|
|
|
});
|
|
|
|
|
2011-02-18 12:14:11 +00:00
|
|
|
// Holds the ID of the theme being enabled or disabled while sending out the
|
|
|
|
// events so cached AddonWrapper instances can return correct values for
|
|
|
|
// permissions and pendingOperations
|
2010-11-11 19:49:43 +00:00
|
|
|
var _themeIDBeingEnabled = null;
|
2011-02-18 12:14:11 +00:00
|
|
|
var _themeIDBeingDisbled = null;
|
2010-11-11 19:49:43 +00:00
|
|
|
|
2009-09-02 16:23:40 +00:00
|
|
|
var LightweightThemeManager = {
|
|
|
|
get usedThemes () {
|
|
|
|
try {
|
2012-05-11 12:29:34 +00:00
|
|
|
return JSON.parse(_prefs.getComplexValue("usedThemes",
|
|
|
|
Ci.nsISupportsString).data);
|
2009-09-02 16:23:40 +00:00
|
|
|
} catch (e) {
|
2012-05-11 12:29:34 +00:00
|
|
|
return [];
|
2009-09-02 16:23:40 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
get currentTheme () {
|
|
|
|
try {
|
|
|
|
if (_prefs.getBoolPref("isThemeSelected"))
|
2009-10-09 18:25:58 +00:00
|
|
|
var data = this.usedThemes[0];
|
2009-09-02 16:23:40 +00:00
|
|
|
} catch (e) {}
|
2009-10-09 18:25:58 +00:00
|
|
|
|
2009-11-06 07:03:22 +00:00
|
|
|
return data || null;
|
|
|
|
},
|
|
|
|
|
|
|
|
get currentThemeForDisplay () {
|
|
|
|
var data = this.currentTheme;
|
2009-10-09 18:25:58 +00:00
|
|
|
|
2009-11-06 07:03:22 +00:00
|
|
|
if (data && PERSIST_ENABLED) {
|
2009-10-09 18:25:58 +00:00
|
|
|
for (let key in PERSIST_FILES) {
|
|
|
|
try {
|
|
|
|
if (data[key] && _prefs.getBoolPref("persisted." + key))
|
2009-11-17 07:55:47 +00:00
|
|
|
data[key] = _getLocalImageURI(PERSIST_FILES[key]).spec
|
|
|
|
+ "?" + data.id + ";" + _version(data);
|
2009-10-09 18:25:58 +00:00
|
|
|
} catch (e) {}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return data;
|
2009-09-02 16:23:40 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
set currentTheme (aData) {
|
2010-04-26 18:38:39 +00:00
|
|
|
return _setCurrentTheme(aData, false);
|
|
|
|
},
|
2009-10-09 18:25:58 +00:00
|
|
|
|
2010-04-26 18:38:39 +00:00
|
|
|
setLocalTheme: function (aData) {
|
|
|
|
_setCurrentTheme(aData, true);
|
2009-09-02 16:23:40 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
getUsedTheme: function (aId) {
|
|
|
|
var usedThemes = this.usedThemes;
|
2012-05-27 12:21:48 +00:00
|
|
|
for (let usedTheme of usedThemes) {
|
|
|
|
if (usedTheme.id == aId)
|
|
|
|
return usedTheme;
|
2009-09-02 16:23:40 +00:00
|
|
|
}
|
|
|
|
return null;
|
|
|
|
},
|
|
|
|
|
|
|
|
forgetUsedTheme: function (aId) {
|
2010-04-29 20:11:23 +00:00
|
|
|
let theme = this.getUsedTheme(aId);
|
|
|
|
if (!theme)
|
|
|
|
return;
|
|
|
|
|
|
|
|
let wrapper = new AddonWrapper(theme);
|
|
|
|
AddonManagerPrivate.callAddonListeners("onUninstalling", wrapper, false);
|
|
|
|
|
2009-09-02 16:23:40 +00:00
|
|
|
var currentTheme = this.currentTheme;
|
2010-04-29 20:11:23 +00:00
|
|
|
if (currentTheme && currentTheme.id == aId) {
|
2010-06-08 16:45:30 +00:00
|
|
|
this.themeChanged(null);
|
2010-04-29 20:11:23 +00:00
|
|
|
AddonManagerPrivate.notifyAddonChanged(null, ADDON_TYPE, false);
|
|
|
|
}
|
2009-09-02 16:23:40 +00:00
|
|
|
|
|
|
|
_updateUsedThemes(_usedThemesExceptId(aId));
|
2010-04-29 20:11:23 +00:00
|
|
|
AddonManagerPrivate.callAddonListeners("onUninstalled", wrapper);
|
2009-09-02 16:23:40 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
previewTheme: function (aData) {
|
|
|
|
if (!aData)
|
|
|
|
return;
|
|
|
|
|
2009-09-17 09:03:33 +00:00
|
|
|
let cancel = Cc["@mozilla.org/supports-PRBool;1"].createInstance(Ci.nsISupportsPRBool);
|
|
|
|
cancel.data = false;
|
2010-08-08 18:48:19 +00:00
|
|
|
Services.obs.notifyObservers(cancel, "lightweight-theme-preview-requested",
|
|
|
|
JSON.stringify(aData));
|
2009-09-17 09:03:33 +00:00
|
|
|
if (cancel.data)
|
|
|
|
return;
|
|
|
|
|
2009-09-02 16:23:40 +00:00
|
|
|
if (_previewTimer)
|
|
|
|
_previewTimer.cancel();
|
|
|
|
else
|
|
|
|
_previewTimer = Cc["@mozilla.org/timer;1"].createInstance(Ci.nsITimer);
|
|
|
|
_previewTimer.initWithCallback(_previewTimerCallback,
|
|
|
|
MAX_PREVIEW_SECONDS * 1000,
|
|
|
|
_previewTimer.TYPE_ONE_SHOT);
|
|
|
|
|
|
|
|
_notifyWindows(aData);
|
|
|
|
},
|
|
|
|
|
|
|
|
resetPreview: function () {
|
|
|
|
if (_previewTimer) {
|
|
|
|
_previewTimer.cancel();
|
|
|
|
_previewTimer = null;
|
2009-11-06 07:03:22 +00:00
|
|
|
_notifyWindows(this.currentThemeForDisplay);
|
2009-09-02 16:23:40 +00:00
|
|
|
}
|
2009-11-17 07:55:47 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
parseTheme: function (aString, aBaseURI) {
|
|
|
|
try {
|
2010-04-26 18:38:39 +00:00
|
|
|
return _sanitizeTheme(JSON.parse(aString), aBaseURI, false);
|
2009-11-17 07:55:47 +00:00
|
|
|
} catch (e) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
updateCurrentTheme: function () {
|
|
|
|
try {
|
|
|
|
if (!_prefs.getBoolPref("update.enabled"))
|
|
|
|
return;
|
|
|
|
} catch (e) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
var theme = this.currentTheme;
|
|
|
|
if (!theme || !theme.updateURL)
|
|
|
|
return;
|
|
|
|
|
|
|
|
var req = Cc["@mozilla.org/xmlextras/xmlhttprequest;1"]
|
|
|
|
.createInstance(Ci.nsIXMLHttpRequest);
|
|
|
|
|
|
|
|
req.mozBackgroundRequest = true;
|
|
|
|
req.overrideMimeType("text/plain");
|
|
|
|
req.open("GET", theme.updateURL, true);
|
|
|
|
|
|
|
|
var self = this;
|
2011-09-29 16:06:36 +00:00
|
|
|
req.addEventListener("load", function () {
|
2009-11-17 07:55:47 +00:00
|
|
|
if (req.status != 200)
|
|
|
|
return;
|
|
|
|
|
|
|
|
let newData = self.parseTheme(req.responseText, theme.updateURL);
|
|
|
|
if (!newData ||
|
|
|
|
newData.id != theme.id ||
|
|
|
|
_version(newData) == _version(theme))
|
|
|
|
return;
|
|
|
|
|
|
|
|
var currentTheme = self.currentTheme;
|
|
|
|
if (currentTheme && currentTheme.id == theme.id)
|
|
|
|
self.currentTheme = newData;
|
2011-09-29 16:06:36 +00:00
|
|
|
}, false);
|
2009-11-17 07:55:47 +00:00
|
|
|
|
|
|
|
req.send(null);
|
2010-04-29 20:11:23 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Switches to a new lightweight theme.
|
|
|
|
*
|
2010-04-28 16:42:07 +00:00
|
|
|
* @param aData
|
|
|
|
* The lightweight theme to switch to
|
2010-04-29 20:11:23 +00:00
|
|
|
*/
|
|
|
|
themeChanged: function(aData) {
|
|
|
|
if (_previewTimer) {
|
|
|
|
_previewTimer.cancel();
|
|
|
|
_previewTimer = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (aData) {
|
|
|
|
let usedThemes = _usedThemesExceptId(aData.id);
|
|
|
|
usedThemes.unshift(aData);
|
|
|
|
_updateUsedThemes(usedThemes);
|
2012-09-22 19:24:26 +00:00
|
|
|
if (PERSIST_ENABLED) {
|
|
|
|
LightweightThemeImageOptimizer.purge();
|
|
|
|
_persistImages(aData, function () {
|
|
|
|
_notifyWindows(this.currentThemeForDisplay);
|
|
|
|
}.bind(this));
|
|
|
|
}
|
2010-04-29 20:11:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
_prefs.setBoolPref("isThemeSelected", aData != null);
|
|
|
|
_notifyWindows(aData);
|
2010-08-08 18:48:19 +00:00
|
|
|
Services.obs.notifyObservers(null, "lightweight-theme-changed", null);
|
2010-04-29 20:11:23 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Starts the Addons provider and enables the new lightweight theme if
|
|
|
|
* necessary.
|
|
|
|
*/
|
|
|
|
startup: function() {
|
|
|
|
if (Services.prefs.prefHasUserValue(PREF_LWTHEME_TO_SELECT)) {
|
|
|
|
let id = Services.prefs.getCharPref(PREF_LWTHEME_TO_SELECT);
|
|
|
|
if (id)
|
|
|
|
this.themeChanged(this.getUsedTheme(id));
|
|
|
|
else
|
|
|
|
this.themeChanged(null);
|
|
|
|
Services.prefs.clearUserPref(PREF_LWTHEME_TO_SELECT);
|
|
|
|
}
|
2010-07-27 01:31:10 +00:00
|
|
|
|
|
|
|
_prefs.addObserver("", _prefObserver, false);
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Shuts down the provider.
|
|
|
|
*/
|
|
|
|
shutdown: function() {
|
|
|
|
_prefs.removeObserver("", _prefObserver);
|
2010-04-29 20:11:23 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Called when a new add-on has been enabled when only one add-on of that type
|
|
|
|
* can be enabled.
|
|
|
|
*
|
2010-04-28 16:42:07 +00:00
|
|
|
* @param aId
|
|
|
|
* The ID of the newly enabled add-on
|
|
|
|
* @param aType
|
|
|
|
* The type of the newly enabled add-on
|
|
|
|
* @param aPendingRestart
|
|
|
|
* true if the newly enabled add-on will only become enabled after a
|
|
|
|
* restart
|
2010-04-29 20:11:23 +00:00
|
|
|
*/
|
|
|
|
addonChanged: function(aId, aType, aPendingRestart) {
|
|
|
|
if (aType != ADDON_TYPE)
|
|
|
|
return;
|
|
|
|
|
|
|
|
let id = _getInternalID(aId);
|
|
|
|
let current = this.currentTheme;
|
|
|
|
|
|
|
|
try {
|
|
|
|
let next = Services.prefs.getCharPref(PREF_LWTHEME_TO_SELECT);
|
|
|
|
if (id == next && aPendingRestart)
|
|
|
|
return;
|
|
|
|
|
|
|
|
Services.prefs.clearUserPref(PREF_LWTHEME_TO_SELECT);
|
|
|
|
if (next) {
|
|
|
|
AddonManagerPrivate.callAddonListeners("onOperationCancelled",
|
|
|
|
new AddonWrapper(this.getUsedTheme(next)));
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
if (id == current.id) {
|
|
|
|
AddonManagerPrivate.callAddonListeners("onOperationCancelled",
|
|
|
|
new AddonWrapper(current));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch (e) {
|
|
|
|
}
|
|
|
|
|
|
|
|
if (current) {
|
|
|
|
if (current.id == id)
|
|
|
|
return;
|
2011-02-18 12:14:11 +00:00
|
|
|
_themeIDBeingDisbled = current.id;
|
2010-04-29 20:11:23 +00:00
|
|
|
let wrapper = new AddonWrapper(current);
|
|
|
|
if (aPendingRestart) {
|
|
|
|
Services.prefs.setCharPref(PREF_LWTHEME_TO_SELECT, "");
|
|
|
|
AddonManagerPrivate.callAddonListeners("onDisabling", wrapper, true);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
AddonManagerPrivate.callAddonListeners("onDisabling", wrapper, false);
|
|
|
|
this.themeChanged(null);
|
|
|
|
AddonManagerPrivate.callAddonListeners("onDisabled", wrapper);
|
|
|
|
}
|
2011-02-18 12:14:11 +00:00
|
|
|
_themeIDBeingDisbled = null;
|
2010-04-29 20:11:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (id) {
|
|
|
|
let theme = this.getUsedTheme(id);
|
2010-11-11 19:49:43 +00:00
|
|
|
_themeIDBeingEnabled = id;
|
|
|
|
let wrapper = new AddonWrapper(theme);
|
2010-04-29 20:11:23 +00:00
|
|
|
if (aPendingRestart) {
|
|
|
|
AddonManagerPrivate.callAddonListeners("onEnabling", wrapper, true);
|
|
|
|
Services.prefs.setCharPref(PREF_LWTHEME_TO_SELECT, id);
|
2010-10-06 22:19:41 +00:00
|
|
|
|
|
|
|
// Flush the preferences to disk so they survive any crash
|
|
|
|
Services.prefs.savePrefFile(null);
|
2010-04-29 20:11:23 +00:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
AddonManagerPrivate.callAddonListeners("onEnabling", wrapper, false);
|
|
|
|
this.themeChanged(theme);
|
|
|
|
AddonManagerPrivate.callAddonListeners("onEnabled", wrapper);
|
|
|
|
}
|
2010-11-11 19:49:43 +00:00
|
|
|
_themeIDBeingEnabled = null;
|
2010-04-29 20:11:23 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Called to get an Addon with a particular ID.
|
|
|
|
*
|
2010-04-28 16:42:07 +00:00
|
|
|
* @param aId
|
|
|
|
* The ID of the add-on to retrieve
|
|
|
|
* @param aCallback
|
|
|
|
* A callback to pass the Addon to
|
2010-04-29 20:11:23 +00:00
|
|
|
*/
|
2010-04-26 17:49:19 +00:00
|
|
|
getAddonByID: function(aId, aCallback) {
|
2010-04-29 20:11:23 +00:00
|
|
|
let id = _getInternalID(aId);
|
|
|
|
if (!id) {
|
|
|
|
aCallback(null);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
let theme = this.getUsedTheme(id);
|
|
|
|
if (!theme) {
|
|
|
|
aCallback(null);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
aCallback(new AddonWrapper(theme));
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Called to get Addons of a particular type.
|
|
|
|
*
|
2010-04-28 16:42:07 +00:00
|
|
|
* @param aTypes
|
|
|
|
* An array of types to fetch. Can be null to get all types.
|
|
|
|
* @param aCallback
|
|
|
|
* A callback to pass an array of Addons to
|
2010-04-29 20:11:23 +00:00
|
|
|
*/
|
|
|
|
getAddonsByTypes: function(aTypes, aCallback) {
|
|
|
|
if (aTypes && aTypes.indexOf(ADDON_TYPE) == -1) {
|
|
|
|
aCallback([]);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-05-11 12:29:34 +00:00
|
|
|
aCallback([new AddonWrapper(a) for each (a in this.usedThemes)]);
|
2010-04-29 20:11:23 +00:00
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The AddonWrapper wraps lightweight theme to provide the data visible to
|
|
|
|
* consumers of the AddonManager API.
|
|
|
|
*/
|
2010-11-11 19:49:43 +00:00
|
|
|
function AddonWrapper(aTheme) {
|
2010-04-29 20:11:23 +00:00
|
|
|
this.__defineGetter__("id", function() aTheme.id + ID_SUFFIX);
|
|
|
|
this.__defineGetter__("type", function() ADDON_TYPE);
|
|
|
|
this.__defineGetter__("isActive", function() {
|
|
|
|
let current = LightweightThemeManager.currentTheme;
|
|
|
|
if (current)
|
|
|
|
return aTheme.id == current.id;
|
|
|
|
return false;
|
|
|
|
});
|
|
|
|
|
2010-05-05 22:02:45 +00:00
|
|
|
this.__defineGetter__("name", function() aTheme.name);
|
|
|
|
this.__defineGetter__("version", function() {
|
|
|
|
return "version" in aTheme ? aTheme.version : "";
|
|
|
|
});
|
|
|
|
|
|
|
|
["description", "homepageURL", "iconURL"].forEach(function(prop) {
|
|
|
|
this.__defineGetter__(prop, function() {
|
|
|
|
return prop in aTheme ? aTheme[prop] : null;
|
|
|
|
});
|
2010-04-29 20:11:23 +00:00
|
|
|
}, this);
|
2010-05-05 22:02:45 +00:00
|
|
|
|
2010-07-02 04:33:34 +00:00
|
|
|
["installDate", "updateDate"].forEach(function(prop) {
|
|
|
|
this.__defineGetter__(prop, function() {
|
|
|
|
return prop in aTheme ? new Date(aTheme[prop]) : null;
|
|
|
|
});
|
|
|
|
}, this);
|
|
|
|
|
2010-08-24 01:04:06 +00:00
|
|
|
this.__defineGetter__("creator", function() {
|
|
|
|
return new AddonManagerPrivate.AddonAuthor(aTheme.author);
|
|
|
|
});
|
|
|
|
|
|
|
|
this.__defineGetter__("screenshots", function() {
|
|
|
|
let url = aTheme.previewURL;
|
|
|
|
return [new AddonManagerPrivate.AddonScreenshot(url)];
|
|
|
|
});
|
2010-04-29 20:11:23 +00:00
|
|
|
|
|
|
|
this.__defineGetter__("pendingOperations", function() {
|
|
|
|
let pending = AddonManager.PENDING_NONE;
|
|
|
|
if (this.isActive == this.userDisabled)
|
|
|
|
pending |= this.isActive ? AddonManager.PENDING_DISABLE : AddonManager.PENDING_ENABLE;
|
|
|
|
return pending;
|
|
|
|
});
|
|
|
|
|
2010-07-26 17:34:06 +00:00
|
|
|
this.__defineGetter__("operationsRequiringRestart", function() {
|
2010-09-01 17:02:28 +00:00
|
|
|
// If a non-default theme is in use then a restart will be required to
|
|
|
|
// enable lightweight themes unless dynamic theme switching is enabled
|
|
|
|
if (Services.prefs.prefHasUserValue(PREF_GENERAL_SKINS_SELECTEDSKIN)) {
|
|
|
|
try {
|
|
|
|
if (Services.prefs.getBoolPref(PREF_EM_DSS_ENABLED))
|
|
|
|
return AddonManager.OP_NEEDS_RESTART_NONE;
|
|
|
|
}
|
|
|
|
catch (e) {
|
|
|
|
}
|
|
|
|
return AddonManager.OP_NEEDS_RESTART_ENABLE;
|
|
|
|
}
|
|
|
|
|
2010-07-26 17:34:06 +00:00
|
|
|
return AddonManager.OP_NEEDS_RESTART_NONE;
|
|
|
|
});
|
|
|
|
|
2010-07-02 14:48:41 +00:00
|
|
|
this.__defineGetter__("size", function() {
|
|
|
|
// The size changes depending on whether the theme is in use or not, this is
|
|
|
|
// probably not worth exposing.
|
|
|
|
return null;
|
|
|
|
});
|
|
|
|
|
2010-04-29 20:11:23 +00:00
|
|
|
this.__defineGetter__("permissions", function() {
|
|
|
|
let permissions = AddonManager.PERM_CAN_UNINSTALL;
|
|
|
|
if (this.userDisabled)
|
|
|
|
permissions |= AddonManager.PERM_CAN_ENABLE;
|
2011-02-18 12:14:11 +00:00
|
|
|
else
|
|
|
|
permissions |= AddonManager.PERM_CAN_DISABLE;
|
2010-04-29 20:11:23 +00:00
|
|
|
return permissions;
|
|
|
|
});
|
|
|
|
|
|
|
|
this.__defineGetter__("userDisabled", function() {
|
2010-11-11 19:49:43 +00:00
|
|
|
if (_themeIDBeingEnabled == aTheme.id)
|
2010-04-29 20:11:23 +00:00
|
|
|
return false;
|
2011-02-18 12:14:11 +00:00
|
|
|
if (_themeIDBeingDisbled == aTheme.id)
|
|
|
|
return true;
|
2010-04-29 20:11:23 +00:00
|
|
|
|
|
|
|
try {
|
|
|
|
let toSelect = Services.prefs.getCharPref(PREF_LWTHEME_TO_SELECT);
|
|
|
|
return aTheme.id != toSelect;
|
|
|
|
}
|
|
|
|
catch (e) {
|
|
|
|
let current = LightweightThemeManager.currentTheme;
|
|
|
|
return !current || current.id != aTheme.id;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
this.__defineSetter__("userDisabled", function(val) {
|
|
|
|
if (val == this.userDisabled)
|
|
|
|
return val;
|
|
|
|
|
|
|
|
if (val)
|
2010-07-26 17:20:35 +00:00
|
|
|
LightweightThemeManager.currentTheme = null;
|
|
|
|
else
|
|
|
|
LightweightThemeManager.currentTheme = aTheme;
|
2010-04-29 20:11:23 +00:00
|
|
|
|
|
|
|
return val;
|
|
|
|
});
|
|
|
|
|
|
|
|
this.uninstall = function() {
|
|
|
|
LightweightThemeManager.forgetUsedTheme(aTheme.id);
|
|
|
|
};
|
|
|
|
|
|
|
|
this.cancelUninstall = function() {
|
|
|
|
throw new Error("Theme is not marked to be uninstalled");
|
|
|
|
};
|
|
|
|
|
|
|
|
this.findUpdates = function(listener, reason, appVersion, platformVersion) {
|
|
|
|
if ("onNoCompatibilityUpdateAvailable" in listener)
|
|
|
|
listener.onNoCompatibilityUpdateAvailable(this);
|
|
|
|
if ("onNoUpdateAvailable" in listener)
|
|
|
|
listener.onNoUpdateAvailable(this);
|
|
|
|
if ("onUpdateFinished" in listener)
|
|
|
|
listener.onUpdateFinished(this);
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
AddonWrapper.prototype = {
|
|
|
|
// Lightweight themes are never disabled by the application
|
|
|
|
get appDisabled() {
|
|
|
|
return false;
|
|
|
|
},
|
|
|
|
|
|
|
|
// Lightweight themes are always compatible
|
|
|
|
get isCompatible() {
|
|
|
|
return true;
|
|
|
|
},
|
|
|
|
|
2010-07-01 18:37:32 +00:00
|
|
|
get isPlatformCompatible() {
|
|
|
|
return true;
|
|
|
|
},
|
|
|
|
|
2010-04-26 17:48:27 +00:00
|
|
|
get scope() {
|
|
|
|
return AddonManager.SCOPE_PROFILE;
|
|
|
|
},
|
|
|
|
|
2011-10-22 18:37:15 +00:00
|
|
|
get foreignInstall() {
|
|
|
|
return false;
|
|
|
|
},
|
|
|
|
|
2010-04-29 20:11:23 +00:00
|
|
|
// Lightweight themes are always compatible
|
|
|
|
isCompatibleWith: function(appVersion, platformVersion) {
|
|
|
|
return true;
|
|
|
|
},
|
|
|
|
|
|
|
|
// Lightweight themes are always securely updated
|
|
|
|
get providesUpdatesSecurely() {
|
|
|
|
return true;
|
|
|
|
},
|
|
|
|
|
|
|
|
// Lightweight themes are never blocklisted
|
|
|
|
get blocklistState() {
|
|
|
|
return Ci.nsIBlocklistService.STATE_NOT_BLOCKED;
|
2009-09-02 16:23:40 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2010-04-29 20:11:23 +00:00
|
|
|
/**
|
|
|
|
* Converts the ID used by the public AddonManager API to an lightweight theme
|
|
|
|
* ID.
|
|
|
|
*
|
|
|
|
* @param id
|
|
|
|
* The ID to be converted
|
|
|
|
*
|
|
|
|
* @return the lightweight theme ID or null if the ID was not for a lightweight
|
|
|
|
* theme.
|
|
|
|
*/
|
|
|
|
function _getInternalID(id) {
|
|
|
|
if (!id)
|
|
|
|
return null;
|
|
|
|
let len = id.length - ID_SUFFIX.length;
|
|
|
|
if (len > 0 && id.substring(len) == ID_SUFFIX)
|
|
|
|
return id.substring(0, len);
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2010-04-26 18:38:39 +00:00
|
|
|
function _setCurrentTheme(aData, aLocal) {
|
|
|
|
aData = _sanitizeTheme(aData, null, aLocal);
|
|
|
|
|
2010-04-29 20:11:23 +00:00
|
|
|
let needsRestart = (ADDON_TYPE == "theme") &&
|
|
|
|
Services.prefs.prefHasUserValue(PREF_GENERAL_SKINS_SELECTEDSKIN);
|
|
|
|
|
2010-04-26 18:38:39 +00:00
|
|
|
let cancel = Cc["@mozilla.org/supports-PRBool;1"].createInstance(Ci.nsISupportsPRBool);
|
|
|
|
cancel.data = false;
|
2010-08-08 18:48:19 +00:00
|
|
|
Services.obs.notifyObservers(cancel, "lightweight-theme-change-requested",
|
|
|
|
JSON.stringify(aData));
|
2010-04-26 18:38:39 +00:00
|
|
|
|
|
|
|
if (aData) {
|
2010-04-29 20:11:23 +00:00
|
|
|
let theme = LightweightThemeManager.getUsedTheme(aData.id);
|
2010-04-28 18:47:22 +00:00
|
|
|
let isInstall = !theme || theme.version != aData.version;
|
|
|
|
if (isInstall) {
|
2010-07-02 04:33:34 +00:00
|
|
|
aData.updateDate = Date.now();
|
|
|
|
if (theme && "installDate" in theme)
|
|
|
|
aData.installDate = theme.installDate;
|
|
|
|
else
|
|
|
|
aData.installDate = aData.updateDate;
|
|
|
|
|
2010-04-28 18:47:22 +00:00
|
|
|
var oldWrapper = theme ? new AddonWrapper(theme) : null;
|
2010-04-29 20:11:23 +00:00
|
|
|
var wrapper = new AddonWrapper(aData);
|
2010-04-28 18:46:00 +00:00
|
|
|
AddonManagerPrivate.callInstallListeners("onExternalInstall", null,
|
2010-04-28 18:47:22 +00:00
|
|
|
wrapper, oldWrapper, false);
|
2010-04-29 20:11:23 +00:00
|
|
|
AddonManagerPrivate.callAddonListeners("onInstalling", wrapper, false);
|
|
|
|
}
|
2010-04-28 18:47:22 +00:00
|
|
|
|
2010-04-29 20:11:23 +00:00
|
|
|
let current = LightweightThemeManager.currentTheme;
|
2010-04-28 18:47:22 +00:00
|
|
|
let usedThemes = _usedThemesExceptId(aData.id);
|
|
|
|
if (current && current.id != aData.id)
|
|
|
|
usedThemes.splice(1, 0, aData);
|
|
|
|
else
|
|
|
|
usedThemes.unshift(aData);
|
|
|
|
_updateUsedThemes(usedThemes);
|
2010-04-29 20:11:23 +00:00
|
|
|
|
2010-04-28 18:47:22 +00:00
|
|
|
if (isInstall)
|
2010-04-29 20:11:23 +00:00
|
|
|
AddonManagerPrivate.callAddonListeners("onInstalled", wrapper);
|
2010-04-26 18:38:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (cancel.data)
|
|
|
|
return null;
|
|
|
|
|
2010-04-29 20:11:23 +00:00
|
|
|
AddonManagerPrivate.notifyAddonChanged(aData ? aData.id + ID_SUFFIX : null,
|
|
|
|
ADDON_TYPE, needsRestart);
|
2010-04-26 18:38:39 +00:00
|
|
|
|
2010-04-29 20:11:23 +00:00
|
|
|
return LightweightThemeManager.currentTheme;
|
2010-04-26 18:38:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function _sanitizeTheme(aData, aBaseURI, aLocal) {
|
2010-02-10 21:37:48 +00:00
|
|
|
if (!aData || typeof aData != "object")
|
|
|
|
return null;
|
|
|
|
|
2010-04-26 18:38:39 +00:00
|
|
|
var resourceProtocols = ["http", "https"];
|
|
|
|
if (aLocal)
|
|
|
|
resourceProtocols.push("file");
|
|
|
|
var resourceProtocolExp = new RegExp("^(" + resourceProtocols.join("|") + "):");
|
|
|
|
|
2010-02-10 21:37:48 +00:00
|
|
|
function sanitizeProperty(prop) {
|
|
|
|
if (!(prop in aData))
|
|
|
|
return null;
|
|
|
|
if (typeof aData[prop] != "string")
|
|
|
|
return null;
|
|
|
|
let val = aData[prop].trim();
|
|
|
|
if (!val)
|
|
|
|
return null;
|
|
|
|
|
|
|
|
if (!/URL$/.test(prop))
|
|
|
|
return val;
|
|
|
|
|
|
|
|
try {
|
|
|
|
val = _makeURI(val, aBaseURI ? _makeURI(aBaseURI) : null).spec;
|
2010-04-26 18:38:39 +00:00
|
|
|
if ((prop == "updateURL" ? /^https:/ : resourceProtocolExp).test(val))
|
2010-02-10 21:37:48 +00:00
|
|
|
return val;
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
catch (e) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let result = {};
|
2012-05-27 12:21:48 +00:00
|
|
|
for (let mandatoryProperty of MANDATORY) {
|
|
|
|
let val = sanitizeProperty(mandatoryProperty);
|
2010-02-10 21:37:48 +00:00
|
|
|
if (!val)
|
|
|
|
throw Components.results.NS_ERROR_INVALID_ARG;
|
2012-05-27 12:21:48 +00:00
|
|
|
result[mandatoryProperty] = val;
|
2010-02-10 21:37:48 +00:00
|
|
|
}
|
|
|
|
|
2012-05-27 12:21:48 +00:00
|
|
|
for (let optionalProperty of OPTIONAL) {
|
|
|
|
let val = sanitizeProperty(optionalProperty);
|
2010-02-10 21:37:48 +00:00
|
|
|
if (!val)
|
|
|
|
continue;
|
2012-05-27 12:21:48 +00:00
|
|
|
result[optionalProperty] = val;
|
2010-02-10 21:37:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2009-09-02 16:23:40 +00:00
|
|
|
function _usedThemesExceptId(aId)
|
2010-02-10 21:37:48 +00:00
|
|
|
LightweightThemeManager.usedThemes.filter(function (t) "id" in t && t.id != aId);
|
2009-09-02 16:23:40 +00:00
|
|
|
|
2009-11-17 07:55:47 +00:00
|
|
|
function _version(aThemeData)
|
|
|
|
aThemeData.version || "";
|
|
|
|
|
|
|
|
function _makeURI(aURL, aBaseURI)
|
2010-08-08 18:48:19 +00:00
|
|
|
Services.io.newURI(aURL, null, aBaseURI);
|
2009-11-17 07:55:47 +00:00
|
|
|
|
2009-09-02 16:23:40 +00:00
|
|
|
function _updateUsedThemes(aList) {
|
2010-04-29 20:11:23 +00:00
|
|
|
// Send uninstall events for all themes that need to be removed.
|
2010-07-27 01:31:10 +00:00
|
|
|
while (aList.length > _maxUsedThemes) {
|
2010-04-29 20:11:23 +00:00
|
|
|
let wrapper = new AddonWrapper(aList[aList.length - 1]);
|
|
|
|
AddonManagerPrivate.callAddonListeners("onUninstalling", wrapper, false);
|
|
|
|
aList.pop();
|
|
|
|
AddonManagerPrivate.callAddonListeners("onUninstalled", wrapper);
|
|
|
|
}
|
2009-09-02 16:23:40 +00:00
|
|
|
|
2010-02-26 17:03:19 +00:00
|
|
|
var str = Cc["@mozilla.org/supports-string;1"]
|
|
|
|
.createInstance(Ci.nsISupportsString);
|
|
|
|
str.data = JSON.stringify(aList);
|
|
|
|
_prefs.setComplexValue("usedThemes", Ci.nsISupportsString, str);
|
2009-09-02 16:23:40 +00:00
|
|
|
|
2010-08-08 18:48:19 +00:00
|
|
|
Services.obs.notifyObservers(null, "lightweight-theme-list-changed", null);
|
2009-09-02 16:23:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function _notifyWindows(aThemeData) {
|
2010-08-08 18:48:19 +00:00
|
|
|
Services.obs.notifyObservers(null, "lightweight-theme-styling-update",
|
|
|
|
JSON.stringify(aThemeData));
|
2009-09-02 16:23:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var _previewTimer;
|
|
|
|
var _previewTimerCallback = {
|
|
|
|
notify: function () {
|
|
|
|
LightweightThemeManager.resetPreview();
|
|
|
|
}
|
2009-10-09 18:25:58 +00:00
|
|
|
};
|
|
|
|
|
2010-08-08 18:48:19 +00:00
|
|
|
/**
|
|
|
|
* Called when any of the lightweightThemes preferences are changed.
|
|
|
|
*/
|
|
|
|
function _prefObserver(aSubject, aTopic, aData) {
|
|
|
|
switch (aData) {
|
|
|
|
case "maxUsedThemes":
|
|
|
|
try {
|
|
|
|
_maxUsedThemes = _prefs.getIntPref(aData);
|
|
|
|
}
|
|
|
|
catch (e) {
|
|
|
|
_maxUsedThemes = DEFAULT_MAX_USED_THEMES_COUNT;
|
|
|
|
}
|
|
|
|
// Update the theme list to remove any themes over the number we keep
|
|
|
|
_updateUsedThemes(LightweightThemeManager.usedThemes);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2010-07-27 01:31:10 +00:00
|
|
|
|
2012-09-22 19:24:26 +00:00
|
|
|
function _persistImages(aData, aCallback) {
|
2009-10-09 18:25:58 +00:00
|
|
|
function onSuccess(key) function () {
|
|
|
|
let current = LightweightThemeManager.currentTheme;
|
2012-09-22 19:24:26 +00:00
|
|
|
if (current && current.id == aData.id) {
|
2009-10-09 18:25:58 +00:00
|
|
|
_prefs.setBoolPref("persisted." + key, true);
|
2012-09-22 19:24:26 +00:00
|
|
|
}
|
|
|
|
if (--numFilesToPersist == 0 && aCallback) {
|
|
|
|
aCallback();
|
|
|
|
}
|
2009-10-09 18:25:58 +00:00
|
|
|
};
|
|
|
|
|
2012-09-22 19:24:26 +00:00
|
|
|
let numFilesToPersist = 0;
|
2009-10-09 18:25:58 +00:00
|
|
|
for (let key in PERSIST_FILES) {
|
|
|
|
_prefs.setBoolPref("persisted." + key, false);
|
2012-09-22 19:24:26 +00:00
|
|
|
if (aData[key]) {
|
|
|
|
numFilesToPersist++;
|
2009-10-09 18:25:58 +00:00
|
|
|
_persistImage(aData[key], PERSIST_FILES[key], onSuccess(key));
|
2012-09-22 19:24:26 +00:00
|
|
|
}
|
2009-10-09 18:25:58 +00:00
|
|
|
}
|
2009-09-02 16:23:40 +00:00
|
|
|
}
|
|
|
|
|
2009-10-09 18:25:58 +00:00
|
|
|
function _getLocalImageURI(localFileName) {
|
2012-08-14 04:04:00 +00:00
|
|
|
var localFile = Services.dirsvc.get("ProfD", Ci.nsIFile);
|
2009-10-09 18:25:58 +00:00
|
|
|
localFile.append(localFileName);
|
2010-08-08 18:48:19 +00:00
|
|
|
return Services.io.newFileURI(localFile);
|
2009-10-09 18:25:58 +00:00
|
|
|
}
|
2009-10-09 18:25:58 +00:00
|
|
|
|
2010-04-26 18:38:39 +00:00
|
|
|
function _persistImage(sourceURL, localFileName, successCallback) {
|
|
|
|
if (/^file:/.test(sourceURL))
|
|
|
|
return;
|
|
|
|
|
2009-10-09 18:25:58 +00:00
|
|
|
var targetURI = _getLocalImageURI(localFileName);
|
2009-11-17 07:55:47 +00:00
|
|
|
var sourceURI = _makeURI(sourceURL);
|
2009-10-09 18:25:58 +00:00
|
|
|
|
|
|
|
var persist = Cc["@mozilla.org/embedding/browser/nsWebBrowserPersist;1"]
|
|
|
|
.createInstance(Ci.nsIWebBrowserPersist);
|
|
|
|
|
|
|
|
persist.persistFlags =
|
|
|
|
Ci.nsIWebBrowserPersist.PERSIST_FLAGS_REPLACE_EXISTING_FILES |
|
|
|
|
Ci.nsIWebBrowserPersist.PERSIST_FLAGS_AUTODETECT_APPLY_CONVERSION |
|
|
|
|
(PERSIST_BYPASS_CACHE ?
|
|
|
|
Ci.nsIWebBrowserPersist.PERSIST_FLAGS_BYPASS_CACHE :
|
|
|
|
Ci.nsIWebBrowserPersist.PERSIST_FLAGS_FROM_CACHE);
|
|
|
|
|
2010-04-26 18:38:39 +00:00
|
|
|
persist.progressListener = new _persistProgressListener(successCallback);
|
2009-10-09 18:25:58 +00:00
|
|
|
|
2012-10-04 19:07:51 +00:00
|
|
|
persist.saveURI(sourceURI, null, null, null, null, targetURI, null);
|
2009-10-09 18:25:58 +00:00
|
|
|
}
|
|
|
|
|
2010-04-26 18:38:39 +00:00
|
|
|
function _persistProgressListener(successCallback) {
|
2009-10-09 18:25:58 +00:00
|
|
|
this.onLocationChange = function () {};
|
|
|
|
this.onProgressChange = function () {};
|
|
|
|
this.onStatusChange = function () {};
|
|
|
|
this.onSecurityChange = function () {};
|
|
|
|
this.onStateChange = function (aWebProgress, aRequest, aStateFlags, aStatus) {
|
|
|
|
if (aRequest &&
|
|
|
|
aStateFlags & Ci.nsIWebProgressListener.STATE_IS_NETWORK &&
|
|
|
|
aStateFlags & Ci.nsIWebProgressListener.STATE_STOP) {
|
|
|
|
try {
|
|
|
|
if (aRequest.QueryInterface(Ci.nsIHttpChannel).requestSucceeded) {
|
|
|
|
// success
|
2010-04-26 18:38:39 +00:00
|
|
|
successCallback();
|
2009-10-09 18:25:58 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
} catch (e) { }
|
|
|
|
// failure
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
2010-04-29 20:11:23 +00:00
|
|
|
|
2011-05-20 17:36:28 +00:00
|
|
|
AddonManagerPrivate.registerProvider(LightweightThemeManager, [
|
|
|
|
new AddonManagerPrivate.AddonType("theme", URI_EXTENSION_STRINGS,
|
|
|
|
STRING_TYPE_NAME,
|
|
|
|
AddonManager.VIEW_TYPE_LIST, 5000)
|
|
|
|
]);
|