2007-06-19 11:58:14 -07:00
|
|
|
/*
|
|
|
|
#ifdef 0
|
2012-05-21 04:12:37 -07: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/.
|
2007-06-19 11:58:14 -07:00
|
|
|
#endif
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
2007-10-25 16:02:20 -07:00
|
|
|
* Controls the "full zoom" setting and its site-specific preferences.
|
2007-06-19 11:58:14 -07:00
|
|
|
*/
|
2007-10-25 16:02:20 -07:00
|
|
|
var FullZoom = {
|
2010-07-24 01:57:04 -07:00
|
|
|
// Identifies the setting in the content prefs database.
|
2007-10-25 16:02:20 -07:00
|
|
|
name: "browser.content.full-zoom",
|
2007-06-19 11:58:14 -07:00
|
|
|
|
2008-02-26 15:05:53 -08:00
|
|
|
// browser.zoom.siteSpecific preference cache
|
2009-01-23 01:12:51 -08:00
|
|
|
_siteSpecificPref: undefined,
|
2008-02-26 15:05:53 -08:00
|
|
|
|
2009-03-01 03:22:44 -08:00
|
|
|
// browser.zoom.updateBackgroundTabs preference cache
|
|
|
|
updateBackgroundTabs: undefined,
|
|
|
|
|
2013-06-26 09:07:14 -07:00
|
|
|
// One of the possible values for the mousewheel.* preferences.
|
|
|
|
// From nsEventStateManager.h.
|
|
|
|
ACTION_ZOOM: 3,
|
|
|
|
|
2013-07-09 14:12:43 -07:00
|
|
|
// This maps browser outer window IDs to monotonically increasing integer
|
|
|
|
// tokens. _browserTokenMap[outerID] is increased each time the zoom is
|
|
|
|
// changed in the browser whose outer window ID is outerID. See
|
|
|
|
// _getBrowserToken and _ignorePendingZoomAccesses.
|
|
|
|
_browserTokenMap: new Map(),
|
2013-06-05 17:05:36 -07:00
|
|
|
|
2010-04-17 03:19:33 -07:00
|
|
|
get siteSpecific() {
|
bug 679784: let nsIContentPrefService handle private browsing mode; r=ehsan
Manage private browsing mode in content pref service. CPS should be available
in private browsing mode, but should not store informations on disk, and should
clear all informations once the private session ends.
When setting a pref in private mode, it is stored in an in-memory hash table.
When getting a pref, it is retrieved from that hash table if available.
Otherwise, it is retrieved using the standard mechanism. When removing a pref,
it is retrieved from the hash table. The rationale is that in private mode,
it's ok to read a pref from normal database, but not ok to set it.
The in-memory hash table is cleared when leaving the private browsing mode.
When removing a set of preferences (with removeGroupedPrefs or
removePrefsByName), preferences are removed from the in-memory hashtable, *and*
from normal mode database. The rationale is that visiting a website may trigger
setting/getting/removing for a specific preference only. But removing many
prefs at once is the result of an action not associated with a website. For
example, user may wish to delete all its informations. In that case, user
probably expects to not have those informations restored once it leaves private
browsing mode.
2011-09-01 11:13:03 -07:00
|
|
|
return this._siteSpecificPref;
|
2009-01-23 01:12:51 -08:00
|
|
|
},
|
2007-06-19 11:58:14 -07:00
|
|
|
|
|
|
|
//**************************************************************************//
|
|
|
|
// nsISupports
|
|
|
|
|
2010-07-24 01:57:04 -07:00
|
|
|
QueryInterface: XPCOMUtils.generateQI([Ci.nsIDOMEventListener,
|
|
|
|
Ci.nsIObserver,
|
|
|
|
Ci.nsIContentPrefObserver,
|
|
|
|
Ci.nsISupportsWeakReference,
|
|
|
|
Ci.nsISupports]),
|
2007-06-19 11:58:14 -07:00
|
|
|
|
|
|
|
//**************************************************************************//
|
|
|
|
// Initialization & Destruction
|
|
|
|
|
2007-11-26 00:35:22 -08:00
|
|
|
init: function FullZoom_init() {
|
2013-04-22 19:47:52 -07:00
|
|
|
// Bug 691614 - zooming support for electrolysis
|
|
|
|
if (gMultiProcessBrowser)
|
|
|
|
return;
|
|
|
|
|
2007-06-19 11:58:14 -07:00
|
|
|
// Listen for scrollwheel events so we can save scrollwheel-based changes.
|
|
|
|
window.addEventListener("DOMMouseScroll", this, false);
|
|
|
|
|
|
|
|
// Register ourselves with the service so we know when our pref changes.
|
2013-03-16 15:12:31 -07:00
|
|
|
this._cps2 = Cc["@mozilla.org/content-pref/service;1"].
|
|
|
|
getService(Ci.nsIContentPrefService2);
|
|
|
|
this._cps2.addObserverForName(this.name, this);
|
2008-02-26 15:05:53 -08:00
|
|
|
|
2009-01-23 01:12:51 -08:00
|
|
|
this._siteSpecificPref =
|
2010-03-08 23:49:46 -08:00
|
|
|
gPrefService.getBoolPref("browser.zoom.siteSpecific");
|
2010-05-10 21:28:19 -07:00
|
|
|
this.updateBackgroundTabs =
|
2010-03-08 23:49:46 -08:00
|
|
|
gPrefService.getBoolPref("browser.zoom.updateBackgroundTabs");
|
2009-03-01 03:22:44 -08:00
|
|
|
// Listen for changes to the browser.zoom branch so we can enable/disable
|
|
|
|
// updating background tabs and per-site saving and restoring of zoom levels.
|
2010-03-08 23:49:46 -08:00
|
|
|
gPrefService.addObserver("browser.zoom.", this, true);
|
2013-07-09 14:12:43 -07:00
|
|
|
|
|
|
|
Services.obs.addObserver(this, "outer-window-destroyed", false);
|
2007-06-19 11:58:14 -07:00
|
|
|
},
|
|
|
|
|
2007-11-26 00:35:22 -08:00
|
|
|
destroy: function FullZoom_destroy() {
|
2013-04-22 19:47:52 -07:00
|
|
|
// Bug 691614 - zooming support for electrolysis
|
|
|
|
if (gMultiProcessBrowser)
|
|
|
|
return;
|
|
|
|
|
2010-03-08 23:49:46 -08:00
|
|
|
gPrefService.removeObserver("browser.zoom.", this);
|
2013-03-16 15:12:31 -07:00
|
|
|
this._cps2.removeObserverForName(this.name, this);
|
2007-06-19 11:58:14 -07:00
|
|
|
window.removeEventListener("DOMMouseScroll", this, false);
|
2013-07-09 14:12:43 -07:00
|
|
|
Services.obs.removeObserver(this, "outer-window-destroyed");
|
2007-06-19 11:58:14 -07:00
|
|
|
},
|
|
|
|
|
|
|
|
|
|
|
|
//**************************************************************************//
|
|
|
|
// Event Handlers
|
|
|
|
|
|
|
|
// nsIDOMEventListener
|
|
|
|
|
2007-11-26 00:35:22 -08:00
|
|
|
handleEvent: function FullZoom_handleEvent(event) {
|
2007-10-25 16:02:20 -07:00
|
|
|
switch (event.type) {
|
|
|
|
case "DOMMouseScroll":
|
|
|
|
this._handleMouseScrolled(event);
|
|
|
|
break;
|
|
|
|
}
|
2007-06-19 11:58:14 -07:00
|
|
|
},
|
|
|
|
|
2007-11-26 00:35:22 -08:00
|
|
|
_handleMouseScrolled: function FullZoom__handleMouseScrolled(event) {
|
2007-06-19 11:58:14 -07:00
|
|
|
// Construct the "mousewheel action" pref key corresponding to this event.
|
2012-08-15 08:51:13 -07:00
|
|
|
// Based on nsEventStateManager::WheelPrefs::GetBasePrefName().
|
|
|
|
var pref = "mousewheel.";
|
|
|
|
|
|
|
|
var pressedModifierCount = event.shiftKey + event.ctrlKey + event.altKey +
|
|
|
|
event.metaKey + event.getModifierState("OS");
|
|
|
|
if (pressedModifierCount != 1) {
|
|
|
|
pref += "default.";
|
|
|
|
} else if (event.shiftKey) {
|
|
|
|
pref += "with_shift.";
|
|
|
|
} else if (event.ctrlKey) {
|
|
|
|
pref += "with_control.";
|
|
|
|
} else if (event.altKey) {
|
|
|
|
pref += "with_alt.";
|
|
|
|
} else if (event.metaKey) {
|
|
|
|
pref += "with_meta.";
|
|
|
|
} else {
|
|
|
|
pref += "with_win.";
|
|
|
|
}
|
|
|
|
|
|
|
|
pref += "action";
|
2007-06-19 11:58:14 -07:00
|
|
|
|
2007-10-25 16:02:20 -07:00
|
|
|
// Don't do anything if this isn't a "zoom" scroll event.
|
|
|
|
var isZoomEvent = false;
|
|
|
|
try {
|
2013-06-26 09:07:14 -07:00
|
|
|
isZoomEvent = (gPrefService.getIntPref(pref) == this.ACTION_ZOOM);
|
2007-10-25 16:02:20 -07:00
|
|
|
} catch (e) {}
|
|
|
|
if (!isZoomEvent)
|
2007-06-19 11:58:14 -07:00
|
|
|
return;
|
|
|
|
|
|
|
|
// XXX Lazily cache all the possible action prefs so we don't have to get
|
|
|
|
// them anew from the pref service for every scroll event? We'd have to
|
|
|
|
// make sure to observe them so we can update the cache when they change.
|
|
|
|
|
2013-06-05 17:05:36 -07:00
|
|
|
// We have to call _applyZoomToPref in a timeout because we handle the
|
|
|
|
// event before the event state manager has a chance to apply the zoom
|
2007-06-19 11:58:14 -07:00
|
|
|
// during nsEventStateManager::PostHandleEvent.
|
2013-07-09 14:12:43 -07:00
|
|
|
let browser = gBrowser.selectedBrowser;
|
|
|
|
let token = this._getBrowserToken(browser);
|
2013-06-05 17:05:36 -07:00
|
|
|
window.setTimeout(function () {
|
2013-07-09 14:12:43 -07:00
|
|
|
if (token.isCurrent)
|
|
|
|
this._applyZoomToPref(browser);
|
2013-06-05 17:05:36 -07:00
|
|
|
}.bind(this), 0);
|
2007-06-19 11:58:14 -07:00
|
|
|
},
|
|
|
|
|
2008-02-26 15:05:53 -08:00
|
|
|
// nsIObserver
|
|
|
|
|
|
|
|
observe: function (aSubject, aTopic, aData) {
|
2010-02-06 02:40:06 -08:00
|
|
|
switch (aTopic) {
|
2008-02-26 15:05:53 -08:00
|
|
|
case "nsPref:changed":
|
2010-02-06 02:40:06 -08:00
|
|
|
switch (aData) {
|
2008-02-26 15:05:53 -08:00
|
|
|
case "browser.zoom.siteSpecific":
|
2009-01-23 01:12:51 -08:00
|
|
|
this._siteSpecificPref =
|
2010-03-08 23:49:46 -08:00
|
|
|
gPrefService.getBoolPref("browser.zoom.siteSpecific");
|
2008-02-26 15:05:53 -08:00
|
|
|
break;
|
2009-03-01 03:22:44 -08:00
|
|
|
case "browser.zoom.updateBackgroundTabs":
|
|
|
|
this.updateBackgroundTabs =
|
2010-03-08 23:49:46 -08:00
|
|
|
gPrefService.getBoolPref("browser.zoom.updateBackgroundTabs");
|
2009-03-01 03:22:44 -08:00
|
|
|
break;
|
2008-02-26 15:05:53 -08:00
|
|
|
}
|
|
|
|
break;
|
2013-07-09 14:12:43 -07:00
|
|
|
case "outer-window-destroyed":
|
|
|
|
let outerID = aSubject.QueryInterface(Ci.nsISupportsPRUint64).data;
|
|
|
|
this._browserTokenMap.delete(outerID);
|
|
|
|
break;
|
2008-02-26 15:05:53 -08:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2007-06-19 11:58:14 -07:00
|
|
|
// nsIContentPrefObserver
|
|
|
|
|
2007-11-26 00:35:22 -08:00
|
|
|
onContentPrefSet: function FullZoom_onContentPrefSet(aGroup, aName, aValue) {
|
2013-03-16 15:12:31 -07:00
|
|
|
this._onContentPrefChanged(aGroup, aValue);
|
2007-06-19 11:58:14 -07:00
|
|
|
},
|
|
|
|
|
2007-11-26 00:35:22 -08:00
|
|
|
onContentPrefRemoved: function FullZoom_onContentPrefRemoved(aGroup, aName) {
|
2013-03-16 15:12:31 -07:00
|
|
|
this._onContentPrefChanged(aGroup, undefined);
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Appropriately updates the zoom level after a content preference has
|
|
|
|
* changed.
|
|
|
|
*
|
|
|
|
* @param aGroup The group of the changed preference.
|
|
|
|
* @param aValue The new value of the changed preference. Pass undefined to
|
|
|
|
* indicate the preference's removal.
|
|
|
|
*/
|
|
|
|
_onContentPrefChanged: function FullZoom__onContentPrefChanged(aGroup, aValue) {
|
2013-06-05 17:05:36 -07:00
|
|
|
if (this._isNextContentPrefChangeInternal) {
|
|
|
|
// Ignore changes that FullZoom itself makes. This works because the
|
|
|
|
// content pref service calls callbacks before notifying observers, and it
|
|
|
|
// does both in the same turn of the event loop.
|
|
|
|
delete this._isNextContentPrefChangeInternal;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-07-09 14:12:43 -07:00
|
|
|
let browser = gBrowser.selectedBrowser;
|
|
|
|
if (!browser.currentURI)
|
2013-03-16 15:12:31 -07:00
|
|
|
return;
|
|
|
|
|
2013-07-09 14:12:43 -07:00
|
|
|
let domain = this._cps2.extractDomain(browser.currentURI.spec);
|
2013-03-16 15:12:31 -07:00
|
|
|
if (aGroup) {
|
|
|
|
if (aGroup == domain)
|
2013-07-09 14:12:43 -07:00
|
|
|
this._applyPrefToZoom(aValue, browser);
|
2013-03-16 15:12:31 -07:00
|
|
|
return;
|
2007-06-19 11:58:14 -07:00
|
|
|
}
|
2013-03-16 15:12:31 -07:00
|
|
|
|
|
|
|
this._globalValue = aValue === undefined ? aValue :
|
|
|
|
this._ensureValid(aValue);
|
|
|
|
|
|
|
|
// If the current page doesn't have a site-specific preference, then its
|
|
|
|
// zoom should be set to the new global preference now that the global
|
|
|
|
// preference has changed.
|
|
|
|
let hasPref = false;
|
2013-07-09 14:12:43 -07:00
|
|
|
let ctxt = this._loadContextFromWindow(browser.contentWindow);
|
|
|
|
let token = this._getBrowserToken(browser);
|
|
|
|
this._cps2.getByDomainAndName(browser.currentURI.spec, this.name, ctxt, {
|
2013-03-16 15:12:31 -07:00
|
|
|
handleResult: function () hasPref = true,
|
|
|
|
handleCompletion: function () {
|
2013-07-09 14:12:43 -07:00
|
|
|
if (!hasPref && token.isCurrent)
|
|
|
|
this._applyPrefToZoom(undefined, browser);
|
2013-03-16 15:12:31 -07:00
|
|
|
}.bind(this)
|
|
|
|
});
|
2007-06-19 11:58:14 -07:00
|
|
|
},
|
|
|
|
|
2007-11-26 00:35:22 -08:00
|
|
|
// location change observer
|
2007-06-19 11:58:14 -07:00
|
|
|
|
2009-08-02 10:53:01 -07:00
|
|
|
/**
|
|
|
|
* Called when the location of a tab changes.
|
|
|
|
* When that happens, we need to update the current zoom level if appropriate.
|
|
|
|
*
|
|
|
|
* @param aURI
|
|
|
|
* A URI object representing the new location.
|
|
|
|
* @param aIsTabSwitch
|
|
|
|
* Whether this location change has happened because of a tab switch.
|
|
|
|
* @param aBrowser
|
|
|
|
* (optional) browser object displaying the document
|
|
|
|
*/
|
|
|
|
onLocationChange: function FullZoom_onLocationChange(aURI, aIsTabSwitch, aBrowser) {
|
2013-08-08 17:01:08 -07:00
|
|
|
// Bug 691614 - zooming support for electrolysis
|
|
|
|
if (gMultiProcessBrowser)
|
|
|
|
return;
|
|
|
|
|
2013-07-09 14:12:43 -07:00
|
|
|
// Ignore all pending async zoom accesses in the browser. Pending accesses
|
|
|
|
// that started before the location change will be prevented from applying
|
|
|
|
// to the new location.
|
|
|
|
let browser = aBrowser || gBrowser.selectedBrowser;
|
|
|
|
this._ignorePendingZoomAccesses(browser);
|
|
|
|
|
2013-03-16 15:12:31 -07:00
|
|
|
if (!aURI || (aIsTabSwitch && !this.siteSpecific)) {
|
|
|
|
this._notifyOnLocationChange();
|
2007-11-26 00:35:22 -08:00
|
|
|
return;
|
2013-03-16 15:12:31 -07:00
|
|
|
}
|
2010-03-25 03:52:21 -07:00
|
|
|
|
|
|
|
// Avoid the cps roundtrip and apply the default/global pref.
|
2012-04-25 12:50:09 -07:00
|
|
|
if (aURI.spec == "about:blank") {
|
2013-07-09 14:12:43 -07:00
|
|
|
this._applyPrefToZoom(undefined, browser,
|
|
|
|
this._notifyOnLocationChange.bind(this));
|
2010-03-25 03:52:21 -07:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-01-20 11:43:24 -08:00
|
|
|
// Media documents should always start at 1, and are not affected by prefs.
|
|
|
|
if (!aIsTabSwitch && browser.contentDocument.mozSyntheticDocument) {
|
2012-01-18 16:01:19 -08:00
|
|
|
ZoomManager.setZoomForBrowser(browser, 1);
|
2013-07-09 14:12:43 -07:00
|
|
|
// _ignorePendingZoomAccesses already called above, so no need here.
|
2013-03-16 15:12:31 -07:00
|
|
|
this._notifyOnLocationChange();
|
2013-03-16 15:12:31 -07:00
|
|
|
return;
|
2013-03-16 18:28:48 -07:00
|
|
|
}
|
2013-03-16 15:12:31 -07:00
|
|
|
|
2013-06-05 17:05:36 -07:00
|
|
|
// See if the zoom pref is cached.
|
2013-03-16 15:12:31 -07:00
|
|
|
let ctxt = this._loadContextFromWindow(browser.contentWindow);
|
|
|
|
let pref = this._cps2.getCachedByDomainAndName(aURI.spec, this.name, ctxt);
|
|
|
|
if (pref) {
|
2013-07-09 14:12:43 -07:00
|
|
|
this._applyPrefToZoom(pref.value, browser,
|
|
|
|
this._notifyOnLocationChange.bind(this));
|
2013-03-16 15:12:31 -07:00
|
|
|
return;
|
2013-03-26 12:45:57 -07:00
|
|
|
}
|
2013-03-16 15:12:31 -07:00
|
|
|
|
2013-07-09 14:12:43 -07:00
|
|
|
// It's not cached, so we have to asynchronously fetch it.
|
2013-03-16 15:12:31 -07:00
|
|
|
let value = undefined;
|
2013-07-09 14:12:43 -07:00
|
|
|
let token = this._getBrowserToken(browser);
|
2013-03-16 15:12:31 -07:00
|
|
|
this._cps2.getByDomainAndName(aURI.spec, this.name, ctxt, {
|
|
|
|
handleResult: function (resultPref) value = resultPref.value,
|
|
|
|
handleCompletion: function () {
|
2013-07-09 14:12:43 -07:00
|
|
|
if (!token.isCurrent) {
|
|
|
|
this._notifyOnLocationChange();
|
|
|
|
return;
|
2013-03-16 15:12:31 -07:00
|
|
|
}
|
2013-07-09 14:12:43 -07:00
|
|
|
this._applyPrefToZoom(value, browser,
|
|
|
|
this._notifyOnLocationChange.bind(this));
|
2013-03-16 15:12:31 -07:00
|
|
|
}.bind(this)
|
|
|
|
});
|
2007-06-19 11:58:14 -07:00
|
|
|
},
|
|
|
|
|
2008-02-13 03:00:45 -08:00
|
|
|
// update state of zoom type menu item
|
|
|
|
|
|
|
|
updateMenu: function FullZoom_updateMenu() {
|
|
|
|
var menuItem = document.getElementById("toggle_zoom");
|
|
|
|
|
|
|
|
menuItem.setAttribute("checked", !ZoomManager.useFullZoom);
|
|
|
|
},
|
2007-06-19 11:58:14 -07:00
|
|
|
|
|
|
|
//**************************************************************************//
|
|
|
|
// Setting & Pref Manipulation
|
|
|
|
|
2013-03-16 15:12:31 -07:00
|
|
|
/**
|
|
|
|
* Reduces the zoom level of the page in the current browser.
|
|
|
|
*/
|
2013-06-05 17:05:36 -07:00
|
|
|
reduce: function FullZoom_reduce() {
|
2007-10-25 16:02:20 -07:00
|
|
|
ZoomManager.reduce();
|
2013-07-09 14:12:43 -07:00
|
|
|
let browser = gBrowser.selectedBrowser;
|
|
|
|
this._ignorePendingZoomAccesses(browser);
|
|
|
|
this._applyZoomToPref(browser);
|
2007-06-19 11:58:14 -07:00
|
|
|
},
|
|
|
|
|
2013-03-16 15:12:31 -07:00
|
|
|
/**
|
|
|
|
* Enlarges the zoom level of the page in the current browser.
|
|
|
|
*/
|
2013-06-05 17:05:36 -07:00
|
|
|
enlarge: function FullZoom_enlarge() {
|
2007-10-25 16:02:20 -07:00
|
|
|
ZoomManager.enlarge();
|
2013-07-09 14:12:43 -07:00
|
|
|
let browser = gBrowser.selectedBrowser;
|
|
|
|
this._ignorePendingZoomAccesses(browser);
|
|
|
|
this._applyZoomToPref(browser);
|
2007-06-19 11:58:14 -07:00
|
|
|
},
|
|
|
|
|
2013-03-16 15:12:31 -07:00
|
|
|
/**
|
|
|
|
* Sets the zoom level of the page in the current browser to the global zoom
|
|
|
|
* level.
|
|
|
|
*/
|
2013-06-05 17:05:36 -07:00
|
|
|
reset: function FullZoom_reset() {
|
2013-07-09 14:12:43 -07:00
|
|
|
let browser = gBrowser.selectedBrowser;
|
|
|
|
let token = this._getBrowserToken(browser);
|
|
|
|
this._getGlobalValue(browser.contentWindow, function (value) {
|
|
|
|
if (token.isCurrent) {
|
|
|
|
ZoomManager.setZoomForBrowser(browser, value === undefined ? 1 : value);
|
|
|
|
this._ignorePendingZoomAccesses(browser);
|
2013-06-05 17:05:36 -07:00
|
|
|
}
|
2013-03-16 15:12:31 -07:00
|
|
|
});
|
2013-07-09 14:12:43 -07:00
|
|
|
this._removePref(browser);
|
2007-06-19 11:58:14 -07:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
2013-07-09 14:12:43 -07:00
|
|
|
* Set the zoom level for a given browser.
|
2007-06-19 11:58:14 -07:00
|
|
|
*
|
2007-11-26 00:35:22 -08:00
|
|
|
* Per nsPresContext::setFullZoom, we can set the zoom to its current value
|
|
|
|
* without significant impact on performance, as the setting is only applied
|
|
|
|
* if it differs from the current setting. In fact getting the zoom and then
|
|
|
|
* checking ourselves if it differs costs more.
|
2013-07-09 14:12:43 -07:00
|
|
|
*
|
2007-11-26 00:35:22 -08:00
|
|
|
* And perhaps we should always set the zoom even if it was more expensive,
|
2012-11-28 16:26:39 -08:00
|
|
|
* since nsDocumentViewer::SetTextZoom claims that child documents can have
|
2007-11-26 00:35:22 -08:00
|
|
|
* a different text zoom (although it would be unusual), and it implies that
|
|
|
|
* those child text zooms should get updated when the parent zoom gets set,
|
|
|
|
* and perhaps the same is true for full zoom
|
2012-11-28 16:26:39 -08:00
|
|
|
* (although nsDocumentViewer::SetFullZoom doesn't mention it).
|
2007-06-19 11:58:14 -07:00
|
|
|
*
|
|
|
|
* So when we apply new zoom values to the browser, we simply set the zoom.
|
|
|
|
* We don't check first to see if the new value is the same as the current
|
|
|
|
* one.
|
2013-03-16 15:12:31 -07:00
|
|
|
*
|
2013-07-09 14:12:43 -07:00
|
|
|
* @param aValue The zoom level value.
|
|
|
|
* @param aBrowser The zoom is set in this browser. Required.
|
|
|
|
* @param aCallback If given, it's asynchronously called when complete.
|
2013-03-16 15:12:31 -07:00
|
|
|
*/
|
2013-07-09 14:12:43 -07:00
|
|
|
_applyPrefToZoom: function FullZoom__applyPrefToZoom(aValue, aBrowser, aCallback) {
|
2013-03-16 15:12:31 -07:00
|
|
|
if (!this.siteSpecific || gInPrintPreviewMode) {
|
2013-07-09 14:12:43 -07:00
|
|
|
this._executeSoon(aCallback);
|
2009-09-09 13:04:05 -07:00
|
|
|
return;
|
2013-03-16 15:12:31 -07:00
|
|
|
}
|
2007-11-09 02:19:12 -08:00
|
|
|
|
2013-07-09 14:12:43 -07:00
|
|
|
// aBrowser.contentDocument is sometimes gone because this method is called
|
|
|
|
// by content pref service callbacks, which themselves can be called at any
|
|
|
|
// time, even after browsers are closed.
|
|
|
|
if (!aBrowser.contentDocument ||
|
|
|
|
aBrowser.contentDocument.mozSyntheticDocument) {
|
|
|
|
this._executeSoon(aCallback);
|
2013-03-16 15:12:31 -07:00
|
|
|
return;
|
|
|
|
}
|
2012-01-18 16:01:19 -08:00
|
|
|
|
2013-03-16 15:12:31 -07:00
|
|
|
if (aValue !== undefined) {
|
2013-07-09 14:12:43 -07:00
|
|
|
ZoomManager.setZoomForBrowser(aBrowser, this._ensureValid(aValue));
|
|
|
|
this._ignorePendingZoomAccesses(aBrowser);
|
|
|
|
this._executeSoon(aCallback);
|
2013-03-16 15:12:31 -07:00
|
|
|
return;
|
2007-06-19 11:58:14 -07:00
|
|
|
}
|
2013-03-16 15:12:31 -07:00
|
|
|
|
2013-07-09 14:12:43 -07:00
|
|
|
let token = this._getBrowserToken(aBrowser);
|
|
|
|
this._getGlobalValue(aBrowser.contentWindow, function (value) {
|
|
|
|
if (token.isCurrent) {
|
|
|
|
ZoomManager.setZoomForBrowser(aBrowser, value === undefined ? 1 : value);
|
|
|
|
this._ignorePendingZoomAccesses(aBrowser);
|
2013-06-05 17:05:36 -07:00
|
|
|
}
|
2013-07-09 14:12:43 -07:00
|
|
|
this._executeSoon(aCallback);
|
2013-03-16 15:12:31 -07:00
|
|
|
});
|
2007-06-19 11:58:14 -07:00
|
|
|
},
|
|
|
|
|
2013-03-16 15:12:31 -07:00
|
|
|
/**
|
2013-07-09 14:12:43 -07:00
|
|
|
* Saves the zoom level of the page in the given browser to the content
|
2013-03-16 15:12:31 -07:00
|
|
|
* prefs store.
|
2013-07-09 14:12:43 -07:00
|
|
|
*
|
|
|
|
* @param browser The zoom of this browser will be saved. Required.
|
2013-03-16 15:12:31 -07:00
|
|
|
*/
|
2013-07-09 14:12:43 -07:00
|
|
|
_applyZoomToPref: function FullZoom__applyZoomToPref(browser) {
|
2013-05-14 09:55:21 -07:00
|
|
|
Services.obs.notifyObservers(null, "browser-fullZoom:zoomChange", "");
|
2013-03-16 15:12:31 -07:00
|
|
|
if (!this.siteSpecific ||
|
|
|
|
gInPrintPreviewMode ||
|
2013-07-09 14:12:43 -07:00
|
|
|
browser.contentDocument.mozSyntheticDocument)
|
2007-11-09 02:19:12 -08:00
|
|
|
return;
|
|
|
|
|
2013-07-09 14:12:43 -07:00
|
|
|
this._cps2.set(browser.currentURI.spec, this.name,
|
|
|
|
ZoomManager.getZoomForBrowser(browser),
|
|
|
|
this._loadContextFromWindow(browser.contentWindow), {
|
2013-03-16 15:12:31 -07:00
|
|
|
handleCompletion: function () {
|
2013-06-05 17:05:36 -07:00
|
|
|
this._isNextContentPrefChangeInternal = true;
|
|
|
|
}.bind(this),
|
2013-03-16 15:12:31 -07:00
|
|
|
});
|
2007-06-19 11:58:14 -07:00
|
|
|
},
|
|
|
|
|
2013-03-16 15:12:31 -07:00
|
|
|
/**
|
2013-07-09 14:12:43 -07:00
|
|
|
* Removes from the content prefs store the zoom level of the given browser.
|
|
|
|
*
|
|
|
|
* @param browser The zoom of this browser will be removed. Required.
|
2013-03-16 15:12:31 -07:00
|
|
|
*/
|
2013-07-09 14:12:43 -07:00
|
|
|
_removePref: function FullZoom__removePref(browser) {
|
2013-07-10 14:53:05 -07:00
|
|
|
Services.obs.notifyObservers(null, "browser-fullZoom:zoomReset", "");
|
2013-07-09 14:12:43 -07:00
|
|
|
if (browser.contentDocument.mozSyntheticDocument)
|
2013-03-16 15:12:31 -07:00
|
|
|
return;
|
2013-07-09 14:12:43 -07:00
|
|
|
let ctxt = this._loadContextFromWindow(browser.contentWindow);
|
|
|
|
this._cps2.removeByDomainAndName(browser.currentURI.spec, this.name, ctxt, {
|
2013-03-16 15:12:31 -07:00
|
|
|
handleCompletion: function () {
|
2013-06-05 17:05:36 -07:00
|
|
|
this._isNextContentPrefChangeInternal = true;
|
|
|
|
}.bind(this),
|
2013-03-16 15:12:31 -07:00
|
|
|
});
|
2007-06-19 11:58:14 -07:00
|
|
|
},
|
|
|
|
|
|
|
|
//**************************************************************************//
|
|
|
|
// Utilities
|
|
|
|
|
2013-06-05 17:05:36 -07:00
|
|
|
/**
|
2013-07-09 14:12:43 -07:00
|
|
|
* Returns the zoom change token of the given browser. Asynchronous
|
|
|
|
* operations that access the given browser's zoom should use this method to
|
|
|
|
* capture the token before starting and use token.isCurrent to determine if
|
|
|
|
* it's safe to access the zoom when done. If token.isCurrent is false, then
|
2013-08-08 20:36:15 -07:00
|
|
|
* after the async operation started, either the browser's zoom was changed or
|
|
|
|
* the browser was destroyed, and depending on what the operation is doing, it
|
|
|
|
* may no longer be safe to set and get its zoom.
|
2013-06-05 17:05:36 -07:00
|
|
|
*
|
2013-07-09 14:12:43 -07:00
|
|
|
* @param browser The token of this browser will be returned.
|
|
|
|
* @return An object with an "isCurrent" getter.
|
2013-06-05 17:05:36 -07:00
|
|
|
*/
|
2013-07-09 14:12:43 -07:00
|
|
|
_getBrowserToken: function FullZoom__getBrowserToken(browser) {
|
|
|
|
let outerID = this._browserOuterID(browser);
|
|
|
|
let map = this._browserTokenMap;
|
|
|
|
if (!map.has(outerID))
|
|
|
|
map.set(outerID, 0);
|
2013-06-06 16:29:50 -07:00
|
|
|
return {
|
2013-07-09 14:12:43 -07:00
|
|
|
token: map.get(outerID),
|
|
|
|
get isCurrent() {
|
2013-08-08 20:36:15 -07:00
|
|
|
// At this point, the browser may have been destructed and unbound but
|
|
|
|
// its outer ID not removed from the map because outer-window-destroyed
|
|
|
|
// hasn't been received yet. In that case, the browser is unusable, it
|
|
|
|
// has no properties, so return false. Check for this case by getting a
|
|
|
|
// property, say, docShell.
|
|
|
|
return map.get(outerID) === this.token && browser.docShell;
|
2013-07-09 14:12:43 -07:00
|
|
|
},
|
|
|
|
};
|
2013-06-05 17:05:36 -07:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
2013-07-09 14:12:43 -07:00
|
|
|
* Increments the zoom change token for the given browser so that pending
|
|
|
|
* async operations know that it may be unsafe to access they zoom when they
|
|
|
|
* finish.
|
|
|
|
*
|
|
|
|
* @param browser Pending accesses in this browser will be ignored.
|
2013-06-05 17:05:36 -07:00
|
|
|
*/
|
2013-07-09 14:12:43 -07:00
|
|
|
_ignorePendingZoomAccesses: function FullZoom__ignorePendingZoomAccesses(browser) {
|
|
|
|
let outerID = this._browserOuterID(browser);
|
|
|
|
let map = this._browserTokenMap;
|
|
|
|
map.set(outerID, (map.get(outerID) || 0) + 1);
|
|
|
|
},
|
|
|
|
|
|
|
|
_browserOuterID: function FullZoom__browserOuterID(browser) {
|
|
|
|
return browser.
|
|
|
|
contentWindow.
|
|
|
|
QueryInterface(Ci.nsIInterfaceRequestor).
|
|
|
|
getInterface(Ci.nsIDOMWindowUtils).
|
|
|
|
outerWindowID;
|
2013-06-05 17:05:36 -07:00
|
|
|
},
|
|
|
|
|
2007-11-26 00:35:22 -08:00
|
|
|
_ensureValid: function FullZoom__ensureValid(aValue) {
|
2013-03-16 15:12:31 -07:00
|
|
|
// Note that undefined is a valid value for aValue that indicates a known-
|
|
|
|
// not-to-exist value.
|
2007-06-19 11:58:14 -07:00
|
|
|
if (isNaN(aValue))
|
2007-10-25 16:02:20 -07:00
|
|
|
return 1;
|
2007-06-19 11:58:14 -07:00
|
|
|
|
2007-10-25 16:02:20 -07:00
|
|
|
if (aValue < ZoomManager.MIN)
|
|
|
|
return ZoomManager.MIN;
|
2007-06-19 11:58:14 -07:00
|
|
|
|
2007-10-25 16:02:20 -07:00
|
|
|
if (aValue > ZoomManager.MAX)
|
|
|
|
return ZoomManager.MAX;
|
2007-06-19 11:58:14 -07:00
|
|
|
|
|
|
|
return aValue;
|
2013-03-16 15:12:31 -07:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets the global browser.content.full-zoom content preference.
|
|
|
|
*
|
|
|
|
* WARNING: callback may be called synchronously or asynchronously. The
|
|
|
|
* reason is that it's usually desirable to avoid turns of the event loop
|
|
|
|
* where possible, since they can lead to visible, jarring jumps in zoom
|
|
|
|
* level. It's not always possible to avoid them, though. As a convenience,
|
|
|
|
* then, this method takes a callback and returns nothing.
|
|
|
|
*
|
|
|
|
* @param window The content window pertaining to the zoom.
|
|
|
|
* @param callback Synchronously or asynchronously called when done. It's
|
2013-07-09 14:12:43 -07:00
|
|
|
* bound to this object (FullZoom) and called as:
|
|
|
|
* callback(prefValue)
|
2013-03-16 15:12:31 -07:00
|
|
|
*/
|
|
|
|
_getGlobalValue: function FullZoom__getGlobalValue(window, callback) {
|
|
|
|
// * !("_globalValue" in this) => global value not yet cached.
|
|
|
|
// * this._globalValue === undefined => global value known not to exist.
|
|
|
|
// * Otherwise, this._globalValue is a number, the global value.
|
|
|
|
if ("_globalValue" in this) {
|
2013-07-09 14:12:43 -07:00
|
|
|
callback.call(this, this._globalValue, true);
|
2013-03-16 15:12:31 -07:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
let value = undefined;
|
|
|
|
this._cps2.getGlobal(this.name, this._loadContextFromWindow(window), {
|
|
|
|
handleResult: function (pref) value = pref.value,
|
2013-07-09 14:12:43 -07:00
|
|
|
handleCompletion: function (reason) {
|
2013-03-16 15:12:31 -07:00
|
|
|
this._globalValue = this._ensureValid(value);
|
|
|
|
callback.call(this, this._globalValue);
|
|
|
|
}.bind(this)
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets the load context from the given window.
|
|
|
|
*
|
|
|
|
* @param window The window whose load context will be returned.
|
|
|
|
* @return The nsILoadContext of the given window.
|
|
|
|
*/
|
|
|
|
_loadContextFromWindow: function FullZoom__loadContextFromWindow(window) {
|
|
|
|
return window.
|
|
|
|
QueryInterface(Ci.nsIInterfaceRequestor).
|
|
|
|
getInterface(Ci.nsIWebNavigation).
|
|
|
|
QueryInterface(Ci.nsILoadContext);
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
2013-08-12 16:41:43 -07:00
|
|
|
* Asynchronously broadcasts "FullZoom:TESTS:location-change" so that tests
|
|
|
|
* can select tabs, load pages, etc. and be notified when the zoom levels on
|
|
|
|
* those pages change. The notification is always asynchronous so that
|
|
|
|
* observers are guaranteed a consistent behavior.
|
2013-03-16 15:12:31 -07:00
|
|
|
*/
|
|
|
|
_notifyOnLocationChange: function FullZoom__notifyOnLocationChange() {
|
|
|
|
this._executeSoon(function () {
|
2013-08-12 16:41:43 -07:00
|
|
|
Services.obs.notifyObservers(null, "FullZoom:TESTS:location-change", "");
|
2013-03-16 15:12:31 -07:00
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
_executeSoon: function FullZoom__executeSoon(callback) {
|
|
|
|
if (!callback)
|
|
|
|
return;
|
|
|
|
Services.tm.mainThread.dispatch(callback, Ci.nsIThread.DISPATCH_NORMAL);
|
|
|
|
},
|
2007-06-19 11:58:14 -07:00
|
|
|
};
|