Bug 1252855 - make reader mode use AsyncPrefs, r?margaret
MozReview-Commit-ID: U6ZTyQQSHG
--- a/browser/modules/ReaderParent.jsm
+++ b/browser/modules/ReaderParent.jsm
@@ -21,18 +21,16 @@ const gStringBundle = Services.strings.c
var ReaderParent = {
_readerModeInfoPanelOpen: false,
MESSAGES: [
"Reader:ArticleGet",
"Reader:FaviconRequest",
"Reader:UpdateReaderButton",
- "Reader:SetIntPref",
- "Reader:SetCharPref",
],
init: function() {
let mm = Cc["@mozilla.org/globalmessagemanager;1"].getService(Ci.nsIMessageListenerManager);
for (let msg of this.MESSAGES) {
mm.addMessageListener(msg, this);
}
},
@@ -71,28 +69,16 @@ var ReaderParent = {
case "Reader:UpdateReaderButton": {
let browser = message.target;
if (message.data && message.data.isArticle !== undefined) {
browser.isArticle = message.data.isArticle;
}
this.updateReaderButton(browser);
break;
}
- case "Reader:SetIntPref": {
- if (message.data && message.data.name !== undefined) {
- Services.prefs.setIntPref(message.data.name, message.data.value);
- }
- break;
- }
- case "Reader:SetCharPref": {
- if (message.data && message.data.name !== undefined) {
- Services.prefs.setCharPref(message.data.name, message.data.value);
- }
- break;
- }
}
},
updateReaderButton: function(browser) {
let win = browser.ownerDocument.defaultView;
if (browser != win.gBrowser.selectedBrowser) {
return;
}
--- a/mobile/android/chrome/content/Reader.js
+++ b/mobile/android/chrome/content/Reader.js
@@ -147,28 +147,16 @@ var Reader = {
break;
case "Reader:UpdateReaderButton": {
let tab = BrowserApp.getTabForBrowser(message.target);
tab.browser.isArticle = message.data.isArticle;
this.updatePageAction(tab);
break;
}
- case "Reader:SetIntPref": {
- if (message.data && message.data.name !== undefined) {
- Services.prefs.setIntPref(message.data.name, message.data.value);
- }
- break;
- }
- case "Reader:SetCharPref": {
- if (message.data && message.data.name !== undefined) {
- Services.prefs.setCharPref(message.data.name, message.data.value);
- }
- break;
- }
}
},
pageAction: {
readerModeCallback: function(browser) {
let url = browser.currentURI.spec;
if (url.startsWith("about:reader")) {
let originalURL = ReaderMode.getOriginalUrl(url);
--- a/mobile/android/chrome/content/browser.js
+++ b/mobile/android/chrome/content/browser.js
@@ -191,18 +191,16 @@ lazilyLoadedObserverScripts.forEach(func
["Reader", [
["Reader:ArticleGet", false],
["Reader:DropdownClosed", true], // 'true' allows us to survive mid-air cycle-collection.
["Reader:DropdownOpened", false],
["Reader:FaviconRequest", false],
["Reader:ToolbarHidden", false],
["Reader:SystemUIVisibility", false],
["Reader:UpdateReaderButton", false],
- ["Reader:SetIntPref", false],
- ["Reader:SetCharPref", false],
], "chrome://browser/content/Reader.js"],
].forEach(aScript => {
let [name, messages, script] = aScript;
XPCOMUtils.defineLazyGetter(window, name, function() {
let sandbox = {};
Services.scriptloader.loadSubScript(script, sandbox);
return sandbox[name];
});
--- a/toolkit/components/narrate/NarrateControls.jsm
+++ b/toolkit/components/narrate/NarrateControls.jsm
@@ -4,16 +4,17 @@
"use strict";
const Cu = Components.utils;
Cu.import("resource://gre/modules/narrate/VoiceSelect.jsm");
Cu.import("resource://gre/modules/narrate/Narrator.jsm");
Cu.import("resource://gre/modules/Services.jsm");
+Cu.import("resource://gre/modules/AsyncPrefs.jsm");
this.EXPORTED_SYMBOLS = ["NarrateControls"];
var gStrings = Services.strings.createBundle("chrome://global/locale/narrate.properties");
function NarrateControls(mm, win) {
this._mm = mm;
this._winRef = Cu.getWeakReference(win);
@@ -114,26 +115,24 @@ NarrateControls.prototype = {
case "click":
this._onButtonClick(evt);
break;
}
},
_onRateInput: function(evt) {
if (!this._rateMousedown) {
- this._mm.sendAsyncMessage("Reader:SetIntPref",
- { name: "narrate.rate", value: evt.target.value });
+ AsyncPrefs.set("narrate.rate", parseInt(evt.target.value, 10));
this.narrator.setRate(this._convertRate(evt.target.value));
}
},
_onVoiceChange: function() {
let voice = this.voice;
- this._mm.sendAsyncMessage("Reader:SetCharPref",
- { name: "narrate.voice", value: voice });
+ AsyncPrefs.set("narrate.voice", voice);
this.narrator.setVoice(voice);
},
_onButtonClick: function(evt) {
switch (evt.target.id) {
case "narrate-skip-previous":
this.narrator.skipPrevious();
break;
@@ -144,17 +143,17 @@ NarrateControls.prototype = {
if (this.narrator.speaking) {
this.narrator.stop();
} else {
this._updateSpeechControls(true);
let options = { rate: this.rate, voice: this.voice };
this.narrator.start(options).then(() => {
this._updateSpeechControls(false);
}, err => {
- Cu.reportError(`Narrate failed: ${err}.`)
+ Cu.reportError(`Narrate failed: ${err}.`);
this._updateSpeechControls(false);
});
}
break;
case "narrate-toggle":
let dropdown = this._doc.getElementById("narrate-dropdown");
if (dropdown.classList.contains("open")) {
if (this.narrator.speaking) {
--- a/toolkit/components/reader/AboutReader.jsm
+++ b/toolkit/components/reader/AboutReader.jsm
@@ -7,20 +7,21 @@
var Ci = Components.interfaces, Cc = Components.classes, Cu = Components.utils;
this.EXPORTED_SYMBOLS = [ "AboutReader" ];
Cu.import("resource://gre/modules/ReaderMode.jsm");
Cu.import("resource://gre/modules/Services.jsm");
Cu.import("resource://gre/modules/XPCOMUtils.jsm");
+XPCOMUtils.defineLazyModuleGetter(this, "AsyncPrefs", "resource://gre/modules/AsyncPrefs.jsm");
+XPCOMUtils.defineLazyModuleGetter(this, "NarrateControls", "resource://gre/modules/narrate/NarrateControls.jsm");
XPCOMUtils.defineLazyModuleGetter(this, "Rect", "resource://gre/modules/Geometry.jsm");
XPCOMUtils.defineLazyModuleGetter(this, "Task", "resource://gre/modules/Task.jsm");
XPCOMUtils.defineLazyModuleGetter(this, "UITelemetry", "resource://gre/modules/UITelemetry.jsm");
-XPCOMUtils.defineLazyModuleGetter(this, "NarrateControls", "resource://gre/modules/narrate/NarrateControls.jsm");
var gStrings = Services.strings.createBundle("chrome://global/locale/aboutReader.properties");
var AboutReader = function(mm, win, articlePromise) {
let url = this._getOriginalUrl(win);
if (!(url.startsWith("http://") || url.startsWith("https://"))) {
let errorMsg = "Only http:// and https:// URLs can be loaded in about:reader.";
if (Services.prefs.getBoolPref("reader.errors.includeURLs"))
@@ -264,21 +265,17 @@ AboutReader.prototype = {
_setFontSize: function(newFontSize) {
let containerClasses = this._doc.getElementById("container").classList;
if (this._fontSize > 0)
containerClasses.remove("font-size" + this._fontSize);
this._fontSize = newFontSize;
containerClasses.add("font-size" + this._fontSize);
-
- this._mm.sendAsyncMessage("Reader:SetIntPref", {
- name: "reader.font_size",
- value: this._fontSize
- });
+ return AsyncPrefs.set("reader.font_size", this._fontSize);
},
_setupFontSizeButtons: function() {
const FONT_SIZE_MIN = 1;
const FONT_SIZE_MAX = 9;
// Sample text shown in Android UI.
let sampleText = this._doc.getElementById("font-size-sample");
@@ -420,38 +417,32 @@ AboutReader.prototype = {
},
// Pref values include "dark", "light", and "auto", which automatically switches
// between light and dark color schemes based on the ambient light level.
_setColorSchemePref: function(colorSchemePref) {
this._enableAmbientLighting(colorSchemePref === "auto");
this._setColorScheme(colorSchemePref);
- this._mm.sendAsyncMessage("Reader:SetCharPref", {
- name: "reader.color_scheme",
- value: colorSchemePref
- });
+ AsyncPrefs.set("reader.color_scheme", colorSchemePref);
},
_setFontType: function(newFontType) {
if (this._fontType === newFontType)
return;
let bodyClasses = this._doc.body.classList;
if (this._fontType)
bodyClasses.remove(this._fontType);
this._fontType = newFontType;
bodyClasses.add(this._fontType);
- this._mm.sendAsyncMessage("Reader:SetCharPref", {
- name: "reader.font_type",
- value: this._fontType
- });
+ AsyncPrefs.set("reader.font_type", this._fontType);
},
_setSystemUIVisibility: function(visible) {
this._mm.sendAsyncMessage("Reader:SystemUIVisibility", { visible: visible });
},
_loadArticle: Task.async(function* () {
let url = this._getOriginalUrl();
--- a/toolkit/modules/AsyncPrefs.jsm
+++ b/toolkit/modules/AsyncPrefs.jsm
@@ -8,19 +8,28 @@ this.EXPORTED_SYMBOLS = ["AsyncPrefs"];
const {interfaces: Ci, utils: Cu, classes: Cc} = Components;
Cu.import("resource://gre/modules/Services.jsm");
Cu.import("resource://gre/modules/Task.jsm");
const kInChildProcess = Services.appinfo.processType == Services.appinfo.PROCESS_TYPE_CONTENT;
const kAllowedPrefs = new Set([
+ // NB: please leave the testing prefs at the top, and sort the rest alphabetically if you add
+ // anything.
"testing.allowed-prefs.some-bool-pref",
"testing.allowed-prefs.some-char-pref",
"testing.allowed-prefs.some-int-pref",
+
+ "narrate.rate",
+ "narrate.voice",
+
+ "reader.font_size",
+ "reader.font_type",
+ "reader.color_scheme",
]);
const kPrefTypeMap = new Map([
["boolean", Services.prefs.PREF_BOOL],
["number", Services.prefs.PREF_INT],
["string", Services.prefs.PREF_STRING],
]);