Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
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.EXPORTED_SYMBOLS = ["LightweightThemeConsumer"];
7 Components.utils.import("resource://gre/modules/XPCOMUtils.jsm");
8 Components.utils.import("resource://gre/modules/Services.jsm");
10 XPCOMUtils.defineLazyModuleGetter(this, "LightweightThemeImageOptimizer",
11 "resource://gre/modules/addons/LightweightThemeImageOptimizer.jsm");
13 XPCOMUtils.defineLazyModuleGetter(this, "PrivateBrowsingUtils",
14 "resource://gre/modules/PrivateBrowsingUtils.jsm");
16 this.LightweightThemeConsumer =
17 function LightweightThemeConsumer(aDocument) {
18 this._doc = aDocument;
19 this._win = aDocument.defaultView;
20 this._footerId = aDocument.documentElement.getAttribute("lightweightthemesfooter");
22 if (PrivateBrowsingUtils.isWindowPrivate(this._win) &&
23 !PrivateBrowsingUtils.permanentPrivateBrowsing) {
24 return;
25 }
27 let screen = this._win.screen;
28 this._lastScreenWidth = screen.width;
29 this._lastScreenHeight = screen.height;
31 Components.classes["@mozilla.org/observer-service;1"]
32 .getService(Components.interfaces.nsIObserverService)
33 .addObserver(this, "lightweight-theme-styling-update", false);
35 var temp = {};
36 Components.utils.import("resource://gre/modules/LightweightThemeManager.jsm", temp);
37 this._update(temp.LightweightThemeManager.currentThemeForDisplay);
38 this._win.addEventListener("resize", this);
39 }
41 LightweightThemeConsumer.prototype = {
42 _lastData: null,
43 _lastScreenWidth: null,
44 _lastScreenHeight: null,
45 // Whether the active lightweight theme should be shown on the window.
46 _enabled: true,
47 // Whether a lightweight theme is enabled.
48 _active: false,
50 enable: function() {
51 this._enabled = true;
52 this._update(this._lastData);
53 },
55 disable: function() {
56 // Dance to keep the data, but reset the applied styles:
57 let lastData = this._lastData
58 this._update(null);
59 this._enabled = false;
60 this._lastData = lastData;
61 },
63 observe: function (aSubject, aTopic, aData) {
64 if (aTopic != "lightweight-theme-styling-update")
65 return;
67 this._update(JSON.parse(aData));
68 },
70 handleEvent: function (aEvent) {
71 let {width, height} = this._win.screen;
73 if (this._lastScreenWidth != width || this._lastScreenHeight != height) {
74 this._lastScreenWidth = width;
75 this._lastScreenHeight = height;
76 if (!this._active)
77 return;
78 this._update(this._lastData);
79 Services.obs.notifyObservers(this._win, "lightweight-theme-optimized",
80 JSON.stringify(this._lastData));
81 }
82 },
84 destroy: function () {
85 if (!PrivateBrowsingUtils.isWindowPrivate(this._win) ||
86 PrivateBrowsingUtils.permanentPrivateBrowsing) {
87 Components.classes["@mozilla.org/observer-service;1"]
88 .getService(Components.interfaces.nsIObserverService)
89 .removeObserver(this, "lightweight-theme-styling-update");
91 this._win.removeEventListener("resize", this);
92 }
94 this._win = this._doc = null;
95 },
97 _update: function (aData) {
98 if (!aData) {
99 aData = { headerURL: "", footerURL: "", textcolor: "", accentcolor: "" };
100 this._lastData = aData;
101 } else {
102 this._lastData = aData;
103 aData = LightweightThemeImageOptimizer.optimize(aData, this._win.screen);
104 }
105 if (!this._enabled)
106 return;
108 let root = this._doc.documentElement;
109 let active = !!aData.headerURL;
110 let stateChanging = (active != this._active);
112 if (active) {
113 root.style.color = aData.textcolor || "black";
114 root.style.backgroundColor = aData.accentcolor || "white";
115 let [r, g, b] = _parseRGB(this._doc.defaultView.getComputedStyle(root, "").color);
116 let luminance = 0.2125 * r + 0.7154 * g + 0.0721 * b;
117 root.setAttribute("lwthemetextcolor", luminance <= 110 ? "dark" : "bright");
118 root.setAttribute("lwtheme", "true");
119 } else {
120 root.style.color = "";
121 root.style.backgroundColor = "";
122 root.removeAttribute("lwthemetextcolor");
123 root.removeAttribute("lwtheme");
124 }
126 this._active = active;
128 _setImage(root, active, aData.headerURL);
129 if (this._footerId) {
130 let footer = this._doc.getElementById(this._footerId);
131 footer.style.backgroundColor = active ? aData.accentcolor || "white" : "";
132 _setImage(footer, active, aData.footerURL);
133 if (active && aData.footerURL)
134 footer.setAttribute("lwthemefooter", "true");
135 else
136 footer.removeAttribute("lwthemefooter");
137 }
139 #ifdef XP_MACOSX
140 // On OS X, we extend the lightweight theme into the titlebar, which means setting
141 // the chromemargin attribute. Some XUL applications already draw in the titlebar,
142 // so we need to save the chromemargin value before we overwrite it with the value
143 // that lets us draw in the titlebar. We stash this value on the root attribute so
144 // that XUL applications have the ability to invalidate the saved value.
145 if (stateChanging) {
146 if (!root.hasAttribute("chromemargin-nonlwtheme")) {
147 root.setAttribute("chromemargin-nonlwtheme", root.getAttribute("chromemargin"));
148 }
150 if (active) {
151 root.setAttribute("chromemargin", "0,-1,-1,-1");
152 } else {
153 let defaultChromemargin = root.getAttribute("chromemargin-nonlwtheme");
154 if (defaultChromemargin) {
155 root.setAttribute("chromemargin", defaultChromemargin);
156 } else {
157 root.removeAttribute("chromemargin");
158 }
159 }
160 }
161 #endif
162 }
163 }
165 function _setImage(aElement, aActive, aURL) {
166 aElement.style.backgroundImage =
167 (aActive && aURL) ? 'url("' + aURL.replace(/"/g, '\\"') + '")' : "";
168 }
170 function _parseRGB(aColorString) {
171 var rgb = aColorString.match(/^rgba?\((\d+), (\d+), (\d+)/);
172 rgb.shift();
173 return rgb.map(function (x) parseInt(x));
174 }