Bug 511771 - Provide a way for Web content to preview and install lightweight themes. r=mconnor, sr=vlad

This commit is contained in:
Dão Gottwald 2009-09-22 10:40:12 +02:00
parent 7722899178
commit e932bb0cf9
7 changed files with 155 additions and 11 deletions

View File

@ -175,8 +175,8 @@ pref("extensions.dss.switchPending", false); // Non-dynamic switch pending af
pref("extensions.{972ce4c6-7e08-4474-a285-3208198ce6fd}.name", "chrome://browser/locale/browser.properties");
pref("extensions.{972ce4c6-7e08-4474-a285-3208198ce6fd}.description", "chrome://browser/locale/browser.properties");
pref("xpinstall.whitelist.add", "update.mozilla.org");
pref("xpinstall.whitelist.add.103", "addons.mozilla.org");
pref("xpinstall.whitelist.add", "addons.mozilla.org");
pref("xpinstall.whitelist.add.36", "getpersonas.com");
pref("keyword.enabled", true);
pref("keyword.URL", "chrome://browser-region/locale/region.properties");

View File

@ -1356,6 +1356,10 @@ function delayedStartup(isLoadingBlank, mustLoadSidebar) {
// initialize the private browsing UI
gPrivateBrowsingUI.init();
gBrowser.mPanelContainer.addEventListener("InstallBrowserTheme", LightWeightThemeWebInstaller, false, true);
gBrowser.mPanelContainer.addEventListener("PreviewBrowserTheme", LightWeightThemeWebInstaller, false, true);
gBrowser.mPanelContainer.addEventListener("ResetBrowserThemePreview", LightWeightThemeWebInstaller, false, true);
}
function BrowserShutdown()
@ -7056,3 +7060,141 @@ let gURLBarEmptyText = {
return gURLBar.getAttribute(type + "emptytext");
}
};
var LightWeightThemeWebInstaller = {
handleEvent: function (event) {
switch (event.type) {
case "InstallBrowserTheme":
this._install(event);
break;
case "PreviewBrowserTheme":
this._preview(event);
break;
case "ResetBrowserThemePreview":
this._resetPreview(event);
break;
}
},
get _manager () {
var temp = {};
Cu.import("resource://gre/modules/LightweightThemeManager.jsm", temp);
delete this._manager;
return this._manager = temp.LightweightThemeManager;
},
_install: function (event) {
var node = event.target;
var data = this._getThemeFromNode(node);
if (!data)
return;
if (this._isAllowed(node)) {
this._manager.currentTheme = data;
return;
}
var allowButtonText =
gNavigatorBundle.getString("lwthemeInstallRequest.allowButton");
var allowButtonAccesskey =
gNavigatorBundle.getString("lwthemeInstallRequest.allowButton.accesskey");
var message =
gNavigatorBundle.getFormattedString("lwthemeInstallRequest.message",
[node.ownerDocument.location.host]);
var buttons = [{
label: allowButtonText,
accessKey: allowButtonAccesskey,
callback: function () {
LightWeightThemeWebInstaller._manager.currentTheme = data;
}
}];
var notificationBox = gBrowser.getNotificationBox();
notificationBox.appendNotification(message, "lwtheme-install-request", "",
notificationBox.PRIORITY_INFO_MEDIUM,
buttons);
},
_preview: function (event) {
if (!this._isAllowed(event.target))
return;
var data = this._getThemeFromNode(event.target);
if (!data)
return;
this._manager.previewTheme(data);
},
_resetPreview: function (event) {
if (!this._isAllowed(event.target))
return;
this._manager.resetPreview();
},
_isAllowed: function (node) {
var pm = Cc["@mozilla.org/permissionmanager;1"].getService(Ci.nsIPermissionManager);
var prefs = [["xpinstall.whitelist.add", pm.ALLOW_ACTION],
["xpinstall.whitelist.add.36", pm.ALLOW_ACTION],
["xpinstall.blacklist.add", pm.DENY_ACTION]];
prefs.forEach(function ([pref, permission]) {
try {
var hosts = gPrefService.getCharPref(pref);
} catch (e) {}
if (hosts) {
hosts.split(",").forEach(function (host) {
pm.add(makeURI("http://" + host), "install", permission);
});
gPrefService.setCharPref(pref, "");
}
});
var uri = node.ownerDocument.documentURIObject;
return pm.testPermission(uri, "install") == pm.ALLOW_ACTION;
},
_getThemeFromNode: function (node) {
const MANDATORY = ["id", "name", "headerURL"];
const OPTIONAL = ["footerURL", "textcolor", "accentcolor", "iconURL",
"previewURL", "author", "description", "homepageURL"];
try {
var data = JSON.parse(node.getAttribute("data-browsertheme"));
} catch (e) {
return null;
}
if (!data || typeof data != "object")
return null;
for (let prop in data) {
if (!data[prop] ||
typeof data[prop] != "string" ||
MANDATORY.indexOf(prop) == -1 && OPTIONAL.indexOf(prop) == -1) {
delete data[prop];
continue;
}
if (/URL$/.test(prop)) {
try {
data[prop] = makeURLAbsolute(node.baseURI, data[prop]);
if (/^https?:/.test(data[prop]))
continue;
} catch (e) {}
delete data[prop];
}
}
for (let i = 0; i < MANDATORY.length; i++) {
if (!(MANDATORY[i] in data))
return null;
}
return data;
}
}

View File

@ -371,7 +371,7 @@ var gPermissionManager = {
var pbi = Components.classes["@mozilla.org/preferences-service;1"]
.getService(Components.interfaces.nsIPrefBranch2);
var prefList = [["xpinstall.whitelist.add", nsIPermissionManager.ALLOW_ACTION],
["xpinstall.whitelist.add.103", nsIPermissionManager.ALLOW_ACTION],
["xpinstall.whitelist.add.36", nsIPermissionManager.ALLOW_ACTION],
["xpinstall.blacklist.add", nsIPermissionManager.DENY_ACTION]];
for (var i = 0; i < prefList.length; ++i) {

View File

@ -67,19 +67,21 @@ LightweightThemeConsumer.prototype = {
_update: function (aData) {
if (!aData)
aData = { headerURL: "", footerURL: "", textColor: "", dominantColor: "" };
aData = { headerURL: "", footerURL: "", textcolor: "", accentcolor: "" };
var root = this._doc.documentElement;
var active = !!aData.headerURL;
if (active) {
root.style.color = aData.textColor || "black";
root.style.color = aData.textcolor || "black";
root.style.backgroundColor = aData.accentcolor || "white";
let [r, g, b] = _parseRGB(this._doc.defaultView.getComputedStyle(root, "").color);
let brightness = (r + g + b) / 3;
root.setAttribute("lwthemetextcolor", brightness <= 127 ? "dark" : "bright");
root.setAttribute("lwtheme", "true");
} else {
root.style.color = "";
root.style.backgroundColor = "";
root.removeAttribute("lwthemetextcolor");
root.removeAttribute("lwtheme");
}
@ -101,9 +103,9 @@ LightweightThemeConsumer.prototype = {
root.setAttribute("originalinactivetitlebarcolor",
root.getAttribute("inactivetitlebarcolor"));
}
root.setAttribute("activetitlebarcolor", aData.dominantColor
root.setAttribute("activetitlebarcolor", (active && aData.accentcolor)
|| root.getAttribute("originalactivetitlebarcolor"));
root.setAttribute("inactivetitlebarcolor", aData.dominantColor
root.setAttribute("inactivetitlebarcolor", (active && aData.accentcolor)
|| root.getAttribute("originalinactivetitlebarcolor"));
}
}

View File

@ -4,8 +4,8 @@ function dummy(id) {
name: Math.random(),
headerURL: Math.random(),
footerURL: Math.random(),
textColor: Math.random(),
dominantColor: Math.random()
textcolor: Math.random(),
accentcolor: Math.random()
};
}

View File

@ -350,7 +350,7 @@ nsInstallTrigger::AllowInstall(nsIURI* aLaunchURI)
updatePermissions( XPINSTALL_WHITELIST_ADD,
nsIPermissionManager::ALLOW_ACTION,
permissionMgr, prefBranch );
updatePermissions( XPINSTALL_WHITELIST_ADD_103,
updatePermissions( XPINSTALL_WHITELIST_ADD_36,
nsIPermissionManager::ALLOW_ACTION,
permissionMgr, prefBranch );
updatePermissions( XPINSTALL_BLACKLIST_ADD,

View File

@ -32,7 +32,7 @@
#define XPINSTALL_ENABLE_PREF "xpinstall.enabled"
#define XPINSTALL_WHITELIST_ADD "xpinstall.whitelist.add"
#define XPINSTALL_WHITELIST_ADD_103 "xpinstall.whitelist.add.103"
#define XPINSTALL_WHITELIST_ADD_36 "xpinstall.whitelist.add.36"
#define XPINSTALL_WHITELIST_REQUIRED "xpinstall.whitelist.required"
#define XPINSTALL_BLACKLIST_ADD "xpinstall.blacklist.add"