browser-textZoom.js
Go to the documentation of this file.
1 /*
2 #ifdef 0
3  * ***** BEGIN LICENSE BLOCK *****
4  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
5  *
6  * The contents of this file are subject to the Mozilla Public License Version
7  * 1.1 (the "License"); you may not use this file except in compliance with
8  * the License. You may obtain a copy of the License at
9  * http://www.mozilla.org/MPL/
10  *
11  * Software distributed under the License is distributed on an "AS IS" basis,
12  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
13  * for the specific language governing rights and limitations under the
14  * License.
15  *
16  * The Original Code is Content Preferences (cpref).
17  *
18  * The Initial Developer of the Original Code is Mozilla.
19  * Portions created by the Initial Developer are Copyright (C) 2006
20  * the Initial Developer. All Rights Reserved.
21  *
22  * Contributor(s):
23  * Myk Melez <myk@mozilla.org>
24  * Dão Gottwald <dao@mozilla.com>
25  * Ehsan Akhgari <ehsan.akhgari@gmail.com>
26  *
27  * Alternatively, the contents of this file may be used under the terms of
28  * either the GNU General Public License Version 2 or later (the "GPL"), or
29  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
30  * in which case the provisions of the GPL or the LGPL are applicable instead
31  * of those above. If you wish to allow use of your version of this file only
32  * under the terms of either the GPL or the LGPL, and not to allow others to
33  * use your version of this file under the terms of the MPL, indicate your
34  * decision by deleting the provisions above and replace them with the notice
35  * and other provisions required by the GPL or the LGPL. If you do not delete
36  * the provisions above, a recipient may use your version of this file under
37  * the terms of any one of the MPL, the GPL or the LGPL.
38  *
39  * ***** END LICENSE BLOCK *****
40 #endif
41  */
42 
43 // One of the possible values for the mousewheel.* preferences.
44 // From nsEventStateManager.cpp.
46 
50 var FullZoom = {
51 
52  //**************************************************************************//
53  // Name & Values
54 
55  // The name of the setting. Identifies the setting in the prefs database.
56  name: "browser.content.full-zoom",
57 
58  // The global value (if any) for the setting. Lazily loaded from the service
59  // when first requested, then updated by the pref change listener as it changes.
60  // If there is no global value, then this should be undefined.
61  get globalValue FullZoom_get_globalValue() {
62  var globalValue = this._cps.getPref(null, this.name);
63  if (typeof globalValue != "undefined")
64  globalValue = this._ensureValid(globalValue);
65  delete this.globalValue;
66  return this.globalValue = globalValue;
67  },
68 
69 
70  //**************************************************************************//
71  // Convenience Getters
72 
73  // Content Pref Service
74  get _cps FullZoom_get__cps() {
75  delete this._cps;
76  return this._cps = Cc["@mozilla.org/content-pref/service;1"].
77  getService(Ci.nsIContentPrefService);
78  },
79 
80  get _prefBranch FullZoom_get__prefBranch() {
81  delete this._prefBranch;
82  return this._prefBranch = Cc["@mozilla.org/preferences-service;1"].
83  getService(Ci.nsIPrefBranch2);
84  },
85 
86  // browser.zoom.siteSpecific preference cache
87  _siteSpecificPref: undefined,
88 
89  // browser.zoom.updateBackgroundTabs preference cache
90  updateBackgroundTabs: undefined,
91 
92  // whether we are in private browsing mode
93  _inPrivateBrowsing: false,
94 
95  get siteSpecific FullZoom_get_siteSpecific() {
96  return !this._inPrivateBrowsing && this._siteSpecificPref;
97  },
98 
99  //**************************************************************************//
100  // nsISupports
101 
102  // We can't use the Ci shortcut here because it isn't defined yet.
103  interfaces: [Components.interfaces.nsIDOMEventListener,
104  Components.interfaces.nsIObserver,
105  Components.interfaces.nsIContentPrefObserver,
106  Components.interfaces.nsISupportsWeakReference,
107  Components.interfaces.nsISupports],
108 
109  QueryInterface: function FullZoom_QueryInterface(aIID) {
110  if (!this.interfaces.some(function (v) aIID.equals(v)))
111  throw Cr.NS_ERROR_NO_INTERFACE;
112  return this;
113  },
114 
115 
116  //**************************************************************************//
117  // Initialization & Destruction
118 
119  init: function FullZoom_init() {
120  // Listen for scrollwheel events so we can save scrollwheel-based changes.
121  window.addEventListener("DOMMouseScroll", this, false);
122 
123  // Register ourselves with the service so we know when our pref changes.
124  this._cps.addObserver(this.name, this);
125 
126  // We disable site-specific preferences in Private Browsing mode, because the
127  // content preferences module is disabled
128  let os = Cc["@mozilla.org/observer-service;1"].
129  getService(Ci.nsIObserverService);
130  os.addObserver(this, "private-browsing", true);
131 
132  // Retrieve the initial status of the Private Browsing mode.
133  // XXX Songbird: allow not having private browsing support
134  if ("@mozilla.org/privatebrowsing;1" in Cc) {
135  this._inPrivateBrowsing = Cc["@mozilla.org/privatebrowsing;1"].
136  getService(Ci.nsIPrivateBrowsingService).
137  privateBrowsingEnabled;
138  }
139 
140  this._siteSpecificPref =
141  this._prefBranch.getBoolPref("browser.zoom.siteSpecific");
142  this.updateBackgroundTabs =
143  this._prefBranch.getBoolPref("browser.zoom.updateBackgroundTabs");
144  // Listen for changes to the browser.zoom branch so we can enable/disable
145  // updating background tabs and per-site saving and restoring of zoom levels.
146  this._prefBranch.addObserver("browser.zoom.", this, true);
147  },
148 
149  destroy: function FullZoom_destroy() {
150  let os = Cc["@mozilla.org/observer-service;1"].
151  getService(Ci.nsIObserverService);
152  os.removeObserver(this, "private-browsing");
153  this._prefBranch.removeObserver("browser.zoom.", this);
154  this._cps.removeObserver(this.name, this);
155  window.removeEventListener("DOMMouseScroll", this, false);
156  delete this._cps;
157  },
158 
159 
160  //**************************************************************************//
161  // Event Handlers
162 
163  // nsIDOMEventListener
164 
165  handleEvent: function FullZoom_handleEvent(event) {
166  switch (event.type) {
167  case "DOMMouseScroll":
168  this._handleMouseScrolled(event);
169  break;
170  }
171  },
172 
173  _handleMouseScrolled: function FullZoom__handleMouseScrolled(event) {
174  // Construct the "mousewheel action" pref key corresponding to this event.
175  // Based on nsEventStateManager::GetBasePrefKeyForMouseWheel.
176  var pref = "mousewheel";
177  if (event.axis == event.HORIZONTAL_AXIS)
178  pref += ".horizscroll";
179 
180  if (event.shiftKey)
181  pref += ".withshiftkey";
182  else if (event.ctrlKey)
183  pref += ".withcontrolkey";
184  else if (event.altKey)
185  pref += ".withaltkey";
186  else if (event.metaKey)
187  pref += ".withmetakey";
188  else
189  pref += ".withnokey";
190 
191  pref += ".action";
192 
193  // Don't do anything if this isn't a "zoom" scroll event.
194  var isZoomEvent = false;
195  try {
196  isZoomEvent = (gPrefService.getIntPref(pref) == MOUSE_SCROLL_ZOOM);
197  } catch (e) {}
198  if (!isZoomEvent)
199  return;
200 
201  // XXX Lazily cache all the possible action prefs so we don't have to get
202  // them anew from the pref service for every scroll event? We'd have to
203  // make sure to observe them so we can update the cache when they change.
204 
205  // We have to call _applySettingToPref in a timeout because we handle
206  // the event before the event state manager has a chance to apply the zoom
207  // during nsEventStateManager::PostHandleEvent.
208  window.setTimeout(function (self) { self._applySettingToPref() }, 0, this);
209  },
210 
211  // nsIObserver
212 
213  observe: function (aSubject, aTopic, aData) {
214  switch(aTopic) {
215  case "nsPref:changed":
216  switch(aData) {
217  case "browser.zoom.siteSpecific":
218  this._siteSpecificPref =
219  this._prefBranch.getBoolPref("browser.zoom.siteSpecific");
220  break;
221  case "browser.zoom.updateBackgroundTabs":
222  this.updateBackgroundTabs =
223  this._prefBranch.getBoolPref("browser.zoom.updateBackgroundTabs");
224  break;
225  }
226  break;
227  case "private-browsing":
228  switch (aData) {
229  case "enter":
230  this._inPrivateBrowsing = true;
231  break;
232  case "exit":
233  this._inPrivateBrowsing = false;
234  break;
235  }
236  break;
237  }
238  },
239 
240  // nsIContentPrefObserver
241 
242  onContentPrefSet: function FullZoom_onContentPrefSet(aGroup, aName, aValue) {
243  if (aGroup == this._cps.grouper.group(gBrowser.currentURI))
244  this._applyPrefToSetting(aValue);
245  else if (aGroup == null) {
246  this.globalValue = this._ensureValid(aValue);
247 
248  // If the current page doesn't have a site-specific preference,
249  // then its zoom should be set to the new global preference now that
250  // the global preference has changed.
251  if (!this._cps.hasPref(gBrowser.currentURI, this.name))
252  this._applyPrefToSetting();
253  }
254  },
255 
256  onContentPrefRemoved: function FullZoom_onContentPrefRemoved(aGroup, aName) {
257  if (aGroup == this._cps.grouper.group(gBrowser.currentURI))
258  this._applyPrefToSetting();
259  else if (aGroup == null) {
260  this.globalValue = undefined;
261 
262  // If the current page doesn't have a site-specific preference,
263  // then its zoom should be set to the default preference now that
264  // the global preference has changed.
265  if (!this._cps.hasPref(gBrowser.currentURI, this.name))
266  this._applyPrefToSetting();
267  }
268  },
269 
270  // location change observer
271 
283  onLocationChange: function FullZoom_onLocationChange(aURI, aIsTabSwitch, aBrowser) {
284  if (!aURI || (aIsTabSwitch && !this.siteSpecific))
285  return;
286  this._applyPrefToSetting(this._cps.getPref(aURI, this.name), aBrowser);
287  },
288 
289  // update state of zoom type menu item
290 
291  updateMenu: function FullZoom_updateMenu() {
292  var menuItem = document.getElementById("toggle_zoom");
293 
294  menuItem.setAttribute("checked", !ZoomManager.useFullZoom);
295  },
296 
297  //**************************************************************************//
298  // Setting & Pref Manipulation
299 
300  reduce: function FullZoom_reduce() {
301  ZoomManager.reduce();
302  this._applySettingToPref();
303  },
304 
305  enlarge: function FullZoom_enlarge() {
306  ZoomManager.enlarge();
307  this._applySettingToPref();
308  },
309 
310  reset: function FullZoom_reset() {
311  if (typeof this.globalValue != "undefined")
312  ZoomManager.zoom = this.globalValue;
313  else
314  ZoomManager.reset();
315 
316  this._removePref();
317  },
318 
338  _applyPrefToSetting: function FullZoom__applyPrefToSetting(aValue, aBrowser) {
339  if (!this.siteSpecific && !this._inPrivateBrowsing)
340  return;
341 
342  var browser = aBrowser || gBrowser.selectedBrowser;
343  try {
344  if (gInPrintPreviewMode ||
345  browser.contentDocument instanceof Ci.nsIImageDocument ||
346  this._inPrivateBrowsing)
347  ZoomManager.setZoomForBrowser(browser, 1);
348  else if (typeof aValue != "undefined")
349  ZoomManager.setZoomForBrowser(browser, this._ensureValid(aValue));
350  else if (typeof this.globalValue != "undefined")
351  ZoomManager.setZoomForBrowser(browser, this.globalValue);
352  else
353  ZoomManager.setZoomForBrowser(browser, 1);
354  }
355  catch(ex) {}
356  },
357 
358  _applySettingToPref: function FullZoom__applySettingToPref() {
359  if (!this.siteSpecific || gInPrintPreviewMode ||
360  content.document instanceof Ci.nsIImageDocument)
361  return;
362 
363  var zoomLevel = ZoomManager.zoom;
364  this._cps.setPref(gBrowser.currentURI, this.name, zoomLevel);
365  },
366 
367  _removePref: function FullZoom__removePref() {
368  if (!(content.document instanceof Ci.nsIImageDocument))
369  this._cps.removePref(gBrowser.currentURI, this.name);
370  },
371 
372 
373  //**************************************************************************//
374  // Utilities
375 
376  _ensureValid: function FullZoom__ensureValid(aValue) {
377  if (isNaN(aValue))
378  return 1;
379 
380  if (aValue < ZoomManager.MIN)
381  return ZoomManager.MIN;
382 
383  if (aValue > ZoomManager.MAX)
384  return ZoomManager.MAX;
385 
386  return aValue;
387  }
388 };
const Cc
const MOUSE_SCROLL_ZOOM
var FullZoom
var menuItem
Definition: FeedWriter.js:970
onPageChanged aValue
Definition: FeedWriter.js:1395
var pref
Definition: openLocation.js:44
var event
sbOSDControlService prototype QueryInterface
getService(Ci.sbIFaceplateManager)
systray _prefBranch
Definition: mainwin.js:150
let window
_window init
Definition: FeedWriter.js:1144
var gInPrintPreviewMode
Definition: browser.js:80
var zoomLevel
return null
Definition: FeedWriter.js:1143
var gPrefService
Definition: overlay.js:34
var os
_updateCookies aName
_updateTextAndScrollDataForTab aBrowser
const Cr
const Ci
sbDeviceFirmwareAutoCheckForUpdate prototype interfaces
var browser
Definition: openLocation.js:42
_updateTextAndScrollDataForFrame aData
sbDeviceFirmwareAutoCheckForUpdate prototype observe