2015-11-30 17:13:57 +03:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
2012-05-21 15:12:37 +04:00
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
2015-11-30 17:13:57 +03:00
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
2007-06-19 22:58:14 +04:00
|
|
|
|
2017-07-23 01:17:55 +03:00
|
|
|
// This file is loaded into the browser window scope.
|
|
|
|
/* eslint-env mozilla/browser-window */
|
|
|
|
|
2007-06-19 22:58:14 +04:00
|
|
|
/**
|
2007-10-26 03:02:20 +04:00
|
|
|
* Controls the "full zoom" setting and its site-specific preferences.
|
2007-06-19 22:58:14 +04:00
|
|
|
*/
|
2007-10-26 03:02:20 +04:00
|
|
|
var FullZoom = {
|
2010-07-24 12:57:04 +04:00
|
|
|
// Identifies the setting in the content prefs database.
|
2007-10-26 03:02:20 +04:00
|
|
|
name: "browser.content.full-zoom",
|
2007-06-19 22:58:14 +04:00
|
|
|
|
2008-02-27 02:05:53 +03:00
|
|
|
// browser.zoom.siteSpecific preference cache
|
2017-06-19 05:33:23 +03:00
|
|
|
// Enabling privacy.resistFingerprinting implies disabling browser.zoom.siteSpecific.
|
2009-01-23 12:12:51 +03:00
|
|
|
_siteSpecificPref: undefined,
|
2008-02-27 02:05:53 +03:00
|
|
|
|
2009-03-01 14:22:44 +03:00
|
|
|
// browser.zoom.updateBackgroundTabs preference cache
|
|
|
|
updateBackgroundTabs: undefined,
|
|
|
|
|
2014-01-13 21:36:26 +04:00
|
|
|
// This maps the browser to monotonically increasing integer
|
|
|
|
// tokens. _browserTokenMap[browser] is increased each time the zoom is
|
|
|
|
// changed in the browser. See _getBrowserToken and _ignorePendingZoomAccesses.
|
|
|
|
_browserTokenMap: new WeakMap(),
|
2013-06-06 04:05:36 +04:00
|
|
|
|
2014-06-25 18:30:17 +04:00
|
|
|
// Stores initial locations if we receive onLocationChange
|
|
|
|
// events before we're initialized.
|
|
|
|
_initialLocations: new WeakMap(),
|
|
|
|
|
2010-04-17 14:19:33 +04:00
|
|
|
get siteSpecific() {
|
2017-06-19 05:33:23 +03:00
|
|
|
if (this._siteSpecificPref === undefined) {
|
|
|
|
this._siteSpecificPref =
|
2017-12-20 01:45:10 +03:00
|
|
|
!Services.prefs.getBoolPref("privacy.resistFingerprinting") &&
|
|
|
|
Services.prefs.getBoolPref("browser.zoom.siteSpecific");
|
2017-06-19 05:33:23 +03:00
|
|
|
}
|
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 22:13:03 +04:00
|
|
|
return this._siteSpecificPref;
|
2009-01-23 12:12:51 +03:00
|
|
|
},
|
2007-06-19 22:58:14 +04:00
|
|
|
|
|
|
|
// nsISupports
|
|
|
|
|
2010-07-24 12:57:04 +04:00
|
|
|
QueryInterface: XPCOMUtils.generateQI([Ci.nsIDOMEventListener,
|
|
|
|
Ci.nsIObserver,
|
|
|
|
Ci.nsIContentPrefObserver,
|
|
|
|
Ci.nsISupportsWeakReference,
|
|
|
|
Ci.nsISupports]),
|
2007-06-19 22:58:14 +04:00
|
|
|
|
|
|
|
// Initialization & Destruction
|
|
|
|
|
2007-11-26 11:35:22 +03:00
|
|
|
init: function FullZoom_init() {
|
2014-08-20 19:13:35 +04:00
|
|
|
gBrowser.addEventListener("ZoomChangeUsingMouseWheel", this);
|
2007-06-19 22:58:14 +04:00
|
|
|
|
|
|
|
// Register ourselves with the service so we know when our pref changes.
|
2013-03-17 02:12:31 +04:00
|
|
|
this._cps2 = Cc["@mozilla.org/content-pref/service;1"].
|
|
|
|
getService(Ci.nsIContentPrefService2);
|
|
|
|
this._cps2.addObserverForName(this.name, this);
|
2008-02-27 02:05:53 +03:00
|
|
|
|
2010-05-11 08:28:19 +04:00
|
|
|
this.updateBackgroundTabs =
|
2017-12-20 01:45:10 +03:00
|
|
|
Services.prefs.getBoolPref("browser.zoom.updateBackgroundTabs");
|
2017-06-19 05:33:23 +03:00
|
|
|
|
2009-03-01 14:22:44 +03: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.
|
2017-12-20 01:45:10 +03:00
|
|
|
Services.prefs.addObserver("browser.zoom.", this, true);
|
2014-06-25 18:30:17 +04:00
|
|
|
|
2017-06-19 05:33:23 +03:00
|
|
|
// Also need to listen to privacy.resistFingerprinting in order to update
|
|
|
|
// this._siteSpecificPref.
|
2017-12-20 01:45:10 +03:00
|
|
|
Services.prefs.addObserver("privacy.resistFingerprinting", this, true);
|
2017-06-19 05:33:23 +03:00
|
|
|
|
2014-06-25 18:30:17 +04:00
|
|
|
// If we received onLocationChange events for any of the current browsers
|
|
|
|
// before we were initialized we want to replay those upon initialization.
|
|
|
|
for (let browser of gBrowser.browsers) {
|
|
|
|
if (this._initialLocations.has(browser)) {
|
|
|
|
this.onLocationChange(...this._initialLocations.get(browser), browser);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// This should be nulled after initialization.
|
|
|
|
this._initialLocations = null;
|
2007-06-19 22:58:14 +04:00
|
|
|
},
|
|
|
|
|
2007-11-26 11:35:22 +03:00
|
|
|
destroy: function FullZoom_destroy() {
|
2017-12-20 01:45:10 +03:00
|
|
|
Services.prefs.removeObserver("browser.zoom.", this);
|
2013-03-17 02:12:31 +04:00
|
|
|
this._cps2.removeObserverForName(this.name, this);
|
2014-08-20 19:13:35 +04:00
|
|
|
gBrowser.removeEventListener("ZoomChangeUsingMouseWheel", this);
|
2007-06-19 22:58:14 +04:00
|
|
|
},
|
|
|
|
|
|
|
|
|
|
|
|
// Event Handlers
|
|
|
|
|
|
|
|
// nsIDOMEventListener
|
|
|
|
|
2007-11-26 11:35:22 +03:00
|
|
|
handleEvent: function FullZoom_handleEvent(event) {
|
2007-10-26 03:02:20 +04:00
|
|
|
switch (event.type) {
|
2014-08-20 19:13:35 +04:00
|
|
|
case "ZoomChangeUsingMouseWheel":
|
|
|
|
let browser = this._getTargetedBrowser(event);
|
|
|
|
this._ignorePendingZoomAccesses(browser);
|
|
|
|
this._applyZoomToPref(browser);
|
2007-10-26 03:02:20 +04:00
|
|
|
break;
|
|
|
|
}
|
2007-06-19 22:58:14 +04:00
|
|
|
},
|
|
|
|
|
2008-02-27 02:05:53 +03:00
|
|
|
// nsIObserver
|
|
|
|
|
2016-12-30 02:34:54 +03:00
|
|
|
observe(aSubject, aTopic, aData) {
|
2010-02-06 13:40:06 +03:00
|
|
|
switch (aTopic) {
|
2008-02-27 02:05:53 +03:00
|
|
|
case "nsPref:changed":
|
2010-02-06 13:40:06 +03:00
|
|
|
switch (aData) {
|
2017-06-19 05:33:23 +03:00
|
|
|
case "privacy.resistFingerprinting":
|
|
|
|
// fall through
|
2008-02-27 02:05:53 +03:00
|
|
|
case "browser.zoom.siteSpecific":
|
2017-06-19 05:33:23 +03:00
|
|
|
// Invalidate pref cache.
|
|
|
|
this._siteSpecificPref = undefined;
|
2008-02-27 02:05:53 +03:00
|
|
|
break;
|
2009-03-01 14:22:44 +03:00
|
|
|
case "browser.zoom.updateBackgroundTabs":
|
|
|
|
this.updateBackgroundTabs =
|
2017-12-20 01:45:10 +03:00
|
|
|
Services.prefs.getBoolPref("browser.zoom.updateBackgroundTabs");
|
2009-03-01 14:22:44 +03:00
|
|
|
break;
|
2008-02-27 02:05:53 +03:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2007-06-19 22:58:14 +04:00
|
|
|
// nsIContentPrefObserver
|
|
|
|
|
2015-02-21 01:24:35 +03:00
|
|
|
onContentPrefSet: function FullZoom_onContentPrefSet(aGroup, aName, aValue, aIsPrivate) {
|
|
|
|
this._onContentPrefChanged(aGroup, aValue, aIsPrivate);
|
2007-06-19 22:58:14 +04:00
|
|
|
},
|
|
|
|
|
2016-04-04 22:25:58 +03:00
|
|
|
onContentPrefRemoved: function FullZoom_onContentPrefRemoved(aGroup, aName, aIsPrivate) {
|
|
|
|
this._onContentPrefChanged(aGroup, undefined, aIsPrivate);
|
2013-03-17 02:12:31 +04:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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.
|
|
|
|
*/
|
2015-02-21 01:24:35 +03:00
|
|
|
_onContentPrefChanged: function FullZoom__onContentPrefChanged(aGroup, aValue, aIsPrivate) {
|
2013-06-06 04:05:36 +04: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-10 01:12:43 +04:00
|
|
|
let browser = gBrowser.selectedBrowser;
|
|
|
|
if (!browser.currentURI)
|
2013-03-17 02:12:31 +04:00
|
|
|
return;
|
|
|
|
|
2015-02-21 01:24:35 +03:00
|
|
|
let ctxt = this._loadContextFromBrowser(browser);
|
2013-07-10 01:12:43 +04:00
|
|
|
let domain = this._cps2.extractDomain(browser.currentURI.spec);
|
2013-03-17 02:12:31 +04:00
|
|
|
if (aGroup) {
|
2015-02-21 01:24:35 +03:00
|
|
|
if (aGroup == domain && ctxt.usePrivateBrowsing == aIsPrivate)
|
2013-07-10 01:12:43 +04:00
|
|
|
this._applyPrefToZoom(aValue, browser);
|
2013-03-17 02:12:31 +04:00
|
|
|
return;
|
2007-06-19 22:58:14 +04:00
|
|
|
}
|
2013-03-17 02:12:31 +04: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-10 01:12:43 +04:00
|
|
|
let token = this._getBrowserToken(browser);
|
|
|
|
this._cps2.getByDomainAndName(browser.currentURI.spec, this.name, ctxt, {
|
2016-12-30 02:34:54 +03:00
|
|
|
handleResult() { hasPref = true; },
|
2017-04-27 01:25:45 +03:00
|
|
|
handleCompletion: () => {
|
2013-07-10 01:12:43 +04:00
|
|
|
if (!hasPref && token.isCurrent)
|
|
|
|
this._applyPrefToZoom(undefined, browser);
|
2017-04-27 01:25:45 +03:00
|
|
|
}
|
2013-03-17 02:12:31 +04:00
|
|
|
});
|
2007-06-19 22:58:14 +04:00
|
|
|
},
|
|
|
|
|
2007-11-26 11:35:22 +03:00
|
|
|
// location change observer
|
2007-06-19 22:58:14 +04:00
|
|
|
|
2009-08-02 21:53:01 +04: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) {
|
2014-06-25 18:30:17 +04:00
|
|
|
let browser = aBrowser || gBrowser.selectedBrowser;
|
|
|
|
|
|
|
|
// If we haven't been initialized yet but receive an onLocationChange
|
|
|
|
// notification then let's store and replay it upon initialization.
|
|
|
|
if (this._initialLocations) {
|
|
|
|
this._initialLocations.set(browser, [aURI, aIsTabSwitch]);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-07-10 01:12:43 +04: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.
|
|
|
|
this._ignorePendingZoomAccesses(browser);
|
|
|
|
|
2013-03-17 02:12:31 +04:00
|
|
|
if (!aURI || (aIsTabSwitch && !this.siteSpecific)) {
|
2016-08-12 20:48:59 +03:00
|
|
|
this._notifyOnLocationChange(browser);
|
2007-11-26 11:35:22 +03:00
|
|
|
return;
|
2013-03-17 02:12:31 +04:00
|
|
|
}
|
2010-03-25 13:52:21 +03:00
|
|
|
|
|
|
|
// Avoid the cps roundtrip and apply the default/global pref.
|
2012-04-25 23:50:09 +04:00
|
|
|
if (aURI.spec == "about:blank") {
|
2013-07-10 01:12:43 +04:00
|
|
|
this._applyPrefToZoom(undefined, browser,
|
2016-08-12 20:48:59 +03:00
|
|
|
this._notifyOnLocationChange.bind(this, browser));
|
2010-03-25 13:52:21 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-01-20 23:43:24 +04:00
|
|
|
// Media documents should always start at 1, and are not affected by prefs.
|
2014-01-13 21:36:26 +04:00
|
|
|
if (!aIsTabSwitch && browser.isSyntheticDocument) {
|
2012-01-19 04:01:19 +04:00
|
|
|
ZoomManager.setZoomForBrowser(browser, 1);
|
2013-07-10 01:12:43 +04:00
|
|
|
// _ignorePendingZoomAccesses already called above, so no need here.
|
2016-08-12 20:48:59 +03:00
|
|
|
this._notifyOnLocationChange(browser);
|
2013-03-17 02:12:31 +04:00
|
|
|
return;
|
2013-03-17 05:28:48 +04:00
|
|
|
}
|
2013-03-17 02:12:31 +04:00
|
|
|
|
2013-06-06 04:05:36 +04:00
|
|
|
// See if the zoom pref is cached.
|
2014-01-13 21:36:26 +04:00
|
|
|
let ctxt = this._loadContextFromBrowser(browser);
|
2013-03-17 02:12:31 +04:00
|
|
|
let pref = this._cps2.getCachedByDomainAndName(aURI.spec, this.name, ctxt);
|
|
|
|
if (pref) {
|
2013-07-10 01:12:43 +04:00
|
|
|
this._applyPrefToZoom(pref.value, browser,
|
2016-08-12 20:48:59 +03:00
|
|
|
this._notifyOnLocationChange.bind(this, browser));
|
2013-03-17 02:12:31 +04:00
|
|
|
return;
|
2013-03-26 23:45:57 +04:00
|
|
|
}
|
2013-03-17 02:12:31 +04:00
|
|
|
|
2013-07-10 01:12:43 +04:00
|
|
|
// It's not cached, so we have to asynchronously fetch it.
|
2013-03-17 02:12:31 +04:00
|
|
|
let value = undefined;
|
2013-07-10 01:12:43 +04:00
|
|
|
let token = this._getBrowserToken(browser);
|
2013-03-17 02:12:31 +04:00
|
|
|
this._cps2.getByDomainAndName(aURI.spec, this.name, ctxt, {
|
2016-12-30 02:34:54 +03:00
|
|
|
handleResult(resultPref) { value = resultPref.value; },
|
2017-04-27 01:25:45 +03:00
|
|
|
handleCompletion: () => {
|
2013-07-10 01:12:43 +04:00
|
|
|
if (!token.isCurrent) {
|
2016-08-12 20:48:59 +03:00
|
|
|
this._notifyOnLocationChange(browser);
|
2013-07-10 01:12:43 +04:00
|
|
|
return;
|
2013-03-17 02:12:31 +04:00
|
|
|
}
|
2013-07-10 01:12:43 +04:00
|
|
|
this._applyPrefToZoom(value, browser,
|
2016-08-12 20:48:59 +03:00
|
|
|
this._notifyOnLocationChange.bind(this, browser));
|
2017-04-27 01:25:45 +03:00
|
|
|
}
|
2013-03-17 02:12:31 +04:00
|
|
|
});
|
2007-06-19 22:58:14 +04:00
|
|
|
},
|
|
|
|
|
2008-02-13 14:00:45 +03: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 22:58:14 +04:00
|
|
|
|
|
|
|
// Setting & Pref Manipulation
|
|
|
|
|
2013-03-17 02:12:31 +04:00
|
|
|
/**
|
|
|
|
* Reduces the zoom level of the page in the current browser.
|
|
|
|
*/
|
2013-06-06 04:05:36 +04:00
|
|
|
reduce: function FullZoom_reduce() {
|
2007-10-26 03:02:20 +04:00
|
|
|
ZoomManager.reduce();
|
2013-07-10 01:12:43 +04:00
|
|
|
let browser = gBrowser.selectedBrowser;
|
|
|
|
this._ignorePendingZoomAccesses(browser);
|
|
|
|
this._applyZoomToPref(browser);
|
2007-06-19 22:58:14 +04:00
|
|
|
},
|
|
|
|
|
2013-03-17 02:12:31 +04:00
|
|
|
/**
|
|
|
|
* Enlarges the zoom level of the page in the current browser.
|
|
|
|
*/
|
2013-06-06 04:05:36 +04:00
|
|
|
enlarge: function FullZoom_enlarge() {
|
2007-10-26 03:02:20 +04:00
|
|
|
ZoomManager.enlarge();
|
2013-07-10 01:12:43 +04:00
|
|
|
let browser = gBrowser.selectedBrowser;
|
|
|
|
this._ignorePendingZoomAccesses(browser);
|
|
|
|
this._applyZoomToPref(browser);
|
2007-06-19 22:58:14 +04:00
|
|
|
},
|
|
|
|
|
2013-03-17 02:12:31 +04:00
|
|
|
/**
|
2016-04-02 01:16:52 +03:00
|
|
|
* Sets the zoom level for the given browser to the given floating
|
|
|
|
* point value, where 1 is the default zoom level.
|
|
|
|
*/
|
2016-12-30 02:34:54 +03:00
|
|
|
setZoom(value, browser = gBrowser.selectedBrowser) {
|
2016-04-02 01:16:52 +03:00
|
|
|
ZoomManager.setZoomForBrowser(browser, value);
|
|
|
|
this._ignorePendingZoomAccesses(browser);
|
|
|
|
this._applyZoomToPref(browser);
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets the zoom level of the page in the given browser to the global zoom
|
2013-03-17 02:12:31 +04:00
|
|
|
* level.
|
2016-04-24 08:17:50 +03:00
|
|
|
*
|
|
|
|
* @return A promise which resolves when the zoom reset has been applied.
|
2013-03-17 02:12:31 +04:00
|
|
|
*/
|
2016-04-02 01:16:52 +03:00
|
|
|
reset: function FullZoom_reset(browser = gBrowser.selectedBrowser) {
|
2013-07-10 01:12:43 +04:00
|
|
|
let token = this._getBrowserToken(browser);
|
2016-04-24 08:17:50 +03:00
|
|
|
let result = this._getGlobalValue(browser).then(value => {
|
2013-07-10 01:12:43 +04:00
|
|
|
if (token.isCurrent) {
|
|
|
|
ZoomManager.setZoomForBrowser(browser, value === undefined ? 1 : value);
|
|
|
|
this._ignorePendingZoomAccesses(browser);
|
2013-06-06 04:05:36 +04:00
|
|
|
}
|
2013-03-17 02:12:31 +04:00
|
|
|
});
|
2013-07-10 01:12:43 +04:00
|
|
|
this._removePref(browser);
|
2016-04-24 08:17:50 +03:00
|
|
|
return result;
|
2007-06-19 22:58:14 +04:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
2013-07-10 01:12:43 +04:00
|
|
|
* Set the zoom level for a given browser.
|
2007-06-19 22:58:14 +04:00
|
|
|
*
|
2007-11-26 11:35:22 +03: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-10 01:12:43 +04:00
|
|
|
*
|
2007-11-26 11:35:22 +03:00
|
|
|
* And perhaps we should always set the zoom even if it was more expensive,
|
2012-11-29 04:26:39 +04:00
|
|
|
* since nsDocumentViewer::SetTextZoom claims that child documents can have
|
2007-11-26 11:35:22 +03: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-29 04:26:39 +04:00
|
|
|
* (although nsDocumentViewer::SetFullZoom doesn't mention it).
|
2007-06-19 22:58:14 +04: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-17 02:12:31 +04:00
|
|
|
*
|
2013-07-10 01:12:43 +04: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-17 02:12:31 +04:00
|
|
|
*/
|
2013-07-10 01:12:43 +04:00
|
|
|
_applyPrefToZoom: function FullZoom__applyPrefToZoom(aValue, aBrowser, aCallback) {
|
2013-03-17 02:12:31 +04:00
|
|
|
if (!this.siteSpecific || gInPrintPreviewMode) {
|
2013-07-10 01:12:43 +04:00
|
|
|
this._executeSoon(aCallback);
|
2009-09-10 00:04:05 +04:00
|
|
|
return;
|
2013-03-17 02:12:31 +04:00
|
|
|
}
|
2007-11-09 13:19:12 +03:00
|
|
|
|
2014-01-13 21:36:26 +04:00
|
|
|
// The browser is sometimes half-destroyed because this method is called
|
2013-07-10 01:12:43 +04:00
|
|
|
// by content pref service callbacks, which themselves can be called at any
|
|
|
|
// time, even after browsers are closed.
|
2014-01-13 21:36:26 +04:00
|
|
|
if (!aBrowser.parentNode || aBrowser.isSyntheticDocument) {
|
2013-07-10 01:12:43 +04:00
|
|
|
this._executeSoon(aCallback);
|
2013-03-17 02:12:31 +04:00
|
|
|
return;
|
|
|
|
}
|
2012-01-19 04:01:19 +04:00
|
|
|
|
2013-03-17 02:12:31 +04:00
|
|
|
if (aValue !== undefined) {
|
2013-07-10 01:12:43 +04:00
|
|
|
ZoomManager.setZoomForBrowser(aBrowser, this._ensureValid(aValue));
|
|
|
|
this._ignorePendingZoomAccesses(aBrowser);
|
|
|
|
this._executeSoon(aCallback);
|
2013-03-17 02:12:31 +04:00
|
|
|
return;
|
2007-06-19 22:58:14 +04:00
|
|
|
}
|
2013-03-17 02:12:31 +04:00
|
|
|
|
2013-07-10 01:12:43 +04:00
|
|
|
let token = this._getBrowserToken(aBrowser);
|
2016-04-24 08:17:50 +03:00
|
|
|
this._getGlobalValue(aBrowser).then(value => {
|
2013-07-10 01:12:43 +04:00
|
|
|
if (token.isCurrent) {
|
|
|
|
ZoomManager.setZoomForBrowser(aBrowser, value === undefined ? 1 : value);
|
|
|
|
this._ignorePendingZoomAccesses(aBrowser);
|
2013-06-06 04:05:36 +04:00
|
|
|
}
|
2013-07-10 01:12:43 +04:00
|
|
|
this._executeSoon(aCallback);
|
2013-03-17 02:12:31 +04:00
|
|
|
});
|
2007-06-19 22:58:14 +04:00
|
|
|
},
|
|
|
|
|
2013-03-17 02:12:31 +04:00
|
|
|
/**
|
2013-07-10 01:12:43 +04:00
|
|
|
* Saves the zoom level of the page in the given browser to the content
|
2013-03-17 02:12:31 +04:00
|
|
|
* prefs store.
|
2013-07-10 01:12:43 +04:00
|
|
|
*
|
|
|
|
* @param browser The zoom of this browser will be saved. Required.
|
2013-03-17 02:12:31 +04:00
|
|
|
*/
|
2013-07-10 01:12:43 +04:00
|
|
|
_applyZoomToPref: function FullZoom__applyZoomToPref(browser) {
|
2013-03-17 02:12:31 +04:00
|
|
|
if (!this.siteSpecific ||
|
|
|
|
gInPrintPreviewMode ||
|
2014-01-13 21:36:26 +04:00
|
|
|
browser.isSyntheticDocument)
|
2007-11-09 13:19:12 +03:00
|
|
|
return;
|
|
|
|
|
2013-07-10 01:12:43 +04:00
|
|
|
this._cps2.set(browser.currentURI.spec, this.name,
|
|
|
|
ZoomManager.getZoomForBrowser(browser),
|
2014-01-13 21:36:26 +04:00
|
|
|
this._loadContextFromBrowser(browser), {
|
2017-04-27 01:25:45 +03:00
|
|
|
handleCompletion: () => {
|
2013-06-06 04:05:36 +04:00
|
|
|
this._isNextContentPrefChangeInternal = true;
|
2017-04-27 01:25:45 +03:00
|
|
|
},
|
2013-03-17 02:12:31 +04:00
|
|
|
});
|
2007-06-19 22:58:14 +04:00
|
|
|
},
|
|
|
|
|
2013-03-17 02:12:31 +04:00
|
|
|
/**
|
2013-07-10 01:12:43 +04: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-17 02:12:31 +04:00
|
|
|
*/
|
2013-07-10 01:12:43 +04:00
|
|
|
_removePref: function FullZoom__removePref(browser) {
|
2014-01-13 21:36:26 +04:00
|
|
|
if (browser.isSyntheticDocument)
|
2013-03-17 02:12:31 +04:00
|
|
|
return;
|
2014-01-13 21:36:26 +04:00
|
|
|
let ctxt = this._loadContextFromBrowser(browser);
|
2013-07-10 01:12:43 +04:00
|
|
|
this._cps2.removeByDomainAndName(browser.currentURI.spec, this.name, ctxt, {
|
2017-04-27 01:25:45 +03:00
|
|
|
handleCompletion: () => {
|
2013-06-06 04:05:36 +04:00
|
|
|
this._isNextContentPrefChangeInternal = true;
|
2017-04-27 01:25:45 +03:00
|
|
|
},
|
2013-03-17 02:12:31 +04:00
|
|
|
});
|
2007-06-19 22:58:14 +04:00
|
|
|
},
|
|
|
|
|
|
|
|
// Utilities
|
|
|
|
|
2013-06-06 04:05:36 +04:00
|
|
|
/**
|
2013-07-10 01:12:43 +04: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-09 07:36:15 +04: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-06 04:05:36 +04:00
|
|
|
*
|
2013-07-10 01:12:43 +04:00
|
|
|
* @param browser The token of this browser will be returned.
|
|
|
|
* @return An object with an "isCurrent" getter.
|
2013-06-06 04:05:36 +04:00
|
|
|
*/
|
2013-07-10 01:12:43 +04:00
|
|
|
_getBrowserToken: function FullZoom__getBrowserToken(browser) {
|
|
|
|
let map = this._browserTokenMap;
|
2014-01-13 21:36:26 +04:00
|
|
|
if (!map.has(browser))
|
|
|
|
map.set(browser, 0);
|
2013-06-07 03:29:50 +04:00
|
|
|
return {
|
2014-01-13 21:36:26 +04:00
|
|
|
token: map.get(browser),
|
2013-07-10 01:12:43 +04:00
|
|
|
get isCurrent() {
|
2013-08-09 07:36:15 +04: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.
|
2014-01-13 21:36:26 +04:00
|
|
|
return map.get(browser) === this.token && browser.parentNode;
|
2013-07-10 01:12:43 +04:00
|
|
|
},
|
|
|
|
};
|
2013-06-06 04:05:36 +04:00
|
|
|
},
|
|
|
|
|
2014-08-20 19:13:35 +04:00
|
|
|
/**
|
|
|
|
* Returns the browser that the supplied zoom event is associated with.
|
|
|
|
* @param event The ZoomChangeUsingMouseWheel event.
|
|
|
|
* @return The associated browser element, if one exists, otherwise null.
|
|
|
|
*/
|
|
|
|
_getTargetedBrowser: function FullZoom__getTargetedBrowser(event) {
|
|
|
|
let target = event.originalTarget;
|
|
|
|
|
|
|
|
// With remote content browsers, the event's target is the browser
|
|
|
|
// we're looking for.
|
|
|
|
const XUL_NS = "http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul";
|
|
|
|
if (target instanceof window.XULElement &&
|
|
|
|
target.localName == "browser" &&
|
|
|
|
target.namespaceURI == XUL_NS)
|
|
|
|
return target;
|
|
|
|
|
|
|
|
// With in-process content browsers, the event's target is the content
|
|
|
|
// document.
|
|
|
|
if (target.nodeType == Node.DOCUMENT_NODE)
|
|
|
|
return gBrowser.getBrowserForDocument(target);
|
|
|
|
|
|
|
|
throw new Error("Unexpected ZoomChangeUsingMouseWheel event source");
|
|
|
|
},
|
|
|
|
|
2013-06-06 04:05:36 +04:00
|
|
|
/**
|
2013-07-10 01:12:43 +04: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-06 04:05:36 +04:00
|
|
|
*/
|
2013-07-10 01:12:43 +04:00
|
|
|
_ignorePendingZoomAccesses: function FullZoom__ignorePendingZoomAccesses(browser) {
|
|
|
|
let map = this._browserTokenMap;
|
2014-01-13 21:36:26 +04:00
|
|
|
map.set(browser, (map.get(browser) || 0) + 1);
|
2013-06-06 04:05:36 +04:00
|
|
|
},
|
|
|
|
|
2007-11-26 11:35:22 +03:00
|
|
|
_ensureValid: function FullZoom__ensureValid(aValue) {
|
2013-03-17 02:12:31 +04:00
|
|
|
// Note that undefined is a valid value for aValue that indicates a known-
|
|
|
|
// not-to-exist value.
|
2007-06-19 22:58:14 +04:00
|
|
|
if (isNaN(aValue))
|
2007-10-26 03:02:20 +04:00
|
|
|
return 1;
|
2007-06-19 22:58:14 +04:00
|
|
|
|
2007-10-26 03:02:20 +04:00
|
|
|
if (aValue < ZoomManager.MIN)
|
|
|
|
return ZoomManager.MIN;
|
2007-06-19 22:58:14 +04:00
|
|
|
|
2007-10-26 03:02:20 +04:00
|
|
|
if (aValue > ZoomManager.MAX)
|
|
|
|
return ZoomManager.MAX;
|
2007-06-19 22:58:14 +04:00
|
|
|
|
|
|
|
return aValue;
|
2013-03-17 02:12:31 +04:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets the global browser.content.full-zoom content preference.
|
|
|
|
*
|
2014-01-13 21:36:26 +04:00
|
|
|
* @param browser The browser pertaining to the zoom.
|
2016-04-24 08:17:50 +03:00
|
|
|
* @returns Promise<prefValue>
|
|
|
|
* Resolves to the preference value when done.
|
2013-03-17 02:12:31 +04:00
|
|
|
*/
|
2016-04-24 08:17:50 +03:00
|
|
|
_getGlobalValue: function FullZoom__getGlobalValue(browser) {
|
2013-03-17 02:12:31 +04:00
|
|
|
// * !("_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.
|
2016-04-24 08:17:50 +03:00
|
|
|
return new Promise(resolve => {
|
|
|
|
if ("_globalValue" in this) {
|
|
|
|
resolve(this._globalValue);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
let value = undefined;
|
|
|
|
this._cps2.getGlobal(this.name, this._loadContextFromBrowser(browser), {
|
2016-12-30 02:34:54 +03:00
|
|
|
handleResult(pref) { value = pref.value; },
|
2016-04-24 08:17:50 +03:00
|
|
|
handleCompletion: (reason) => {
|
|
|
|
this._globalValue = this._ensureValid(value);
|
|
|
|
resolve(this._globalValue);
|
|
|
|
}
|
|
|
|
});
|
2013-03-17 02:12:31 +04:00
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
2014-01-13 21:36:26 +04:00
|
|
|
* Gets the load context from the given Browser.
|
2013-03-17 02:12:31 +04:00
|
|
|
*
|
2014-01-13 21:36:26 +04:00
|
|
|
* @param Browser The Browser whose load context will be returned.
|
|
|
|
* @return The nsILoadContext of the given Browser.
|
2013-03-17 02:12:31 +04:00
|
|
|
*/
|
2014-01-13 21:36:26 +04:00
|
|
|
_loadContextFromBrowser: function FullZoom__loadContextFromBrowser(browser) {
|
|
|
|
return browser.loadContext;
|
2013-03-17 02:12:31 +04:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
2014-01-20 14:25:00 +04:00
|
|
|
* Asynchronously broadcasts "browser-fullZoom:location-change" so that
|
|
|
|
* listeners can 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-17 02:12:31 +04:00
|
|
|
*/
|
2016-08-12 20:48:59 +03:00
|
|
|
_notifyOnLocationChange: function FullZoom__notifyOnLocationChange(browser) {
|
2016-11-11 19:10:51 +03:00
|
|
|
this._executeSoon(function() {
|
2017-04-14 22:51:39 +03:00
|
|
|
Services.obs.notifyObservers(browser, "browser-fullZoom:location-change");
|
2013-03-17 02:12:31 +04:00
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
_executeSoon: function FullZoom__executeSoon(callback) {
|
|
|
|
if (!callback)
|
|
|
|
return;
|
2017-04-14 19:29:12 +03:00
|
|
|
Services.tm.dispatchToMainThread(callback);
|
2013-03-17 02:12:31 +04:00
|
|
|
},
|
2007-06-19 22:58:14 +04:00
|
|
|
};
|