From 2d3e2bd1c9c74d057c9a5c8ece957397e80544db Mon Sep 17 00:00:00 2001 From: Nicolas Chevobbe Date: Tue, 13 Feb 2018 17:35:32 +0100 Subject: [PATCH] Bug 1435092 - Add a util object to manage preferences; r=bgrins. This allow us to manage different sets of preferences for the console and the browser console. The util object is passed to the actions through a custom thunk middleware. MozReview-Commit-ID: 6IQLBqX7KMN --HG-- extra : rebase_source : 9dc60df24d0514c8bd0aac7655483eb3d026a16a --- devtools/client/preferences/devtools.js | 15 ++++++- .../new-console-output/actions/filters.js | 14 +++--- .../new-console-output/actions/ui.js | 11 +++-- .../new-console-output/constants.js | 24 ++++++---- .../webconsole/new-console-output/store.js | 44 ++++++++++++------- .../new-console-output/utils/moz.build | 1 + .../new-console-output/utils/prefs.js | 43 ++++++++++++++++++ 7 files changed, 111 insertions(+), 41 deletions(-) create mode 100644 devtools/client/webconsole/new-console-output/utils/prefs.js diff --git a/devtools/client/preferences/devtools.js b/devtools/client/preferences/devtools.js index be31904dfda5..540a39b1a8df 100644 --- a/devtools/client/preferences/devtools.js +++ b/devtools/client/preferences/devtools.js @@ -255,7 +255,16 @@ pref("devtools.webconsole.filter.serverwarn", false); pref("devtools.webconsole.filter.serverinfo", false); pref("devtools.webconsole.filter.serverlog", false); -// Remember the Browser Console filters +// Browser console filters +pref("devtools.browserconsole.filter.error", true); +pref("devtools.browserconsole.filter.warn", true); +pref("devtools.browserconsole.filter.info", true); +pref("devtools.browserconsole.filter.log", true); +pref("devtools.browserconsole.filter.debug", true); +pref("devtools.browserconsole.filter.css", false); +pref("devtools.browserconsole.filter.net", false); +pref("devtools.browserconsole.filter.netxhr", false); +// Remember the Browser Console filters (old frontend) pref("devtools.browserconsole.filter.network", true); pref("devtools.browserconsole.filter.networkinfo", false); pref("devtools.browserconsole.filter.netwarn", true); @@ -280,8 +289,10 @@ pref("devtools.browserconsole.filter.serverwarn", false); pref("devtools.browserconsole.filter.serverinfo", false); pref("devtools.browserconsole.filter.serverlog", false); -// Web console filter settings bar +// Web console filter bar settings pref("devtools.webconsole.ui.filterbar", false); +// Browser console filter bar settings +pref("devtools.browserconsole.ui.filterbar", false); // Max number of inputs to store in web console history. pref("devtools.webconsole.inputHistoryCount", 50); diff --git a/devtools/client/webconsole/new-console-output/actions/filters.js b/devtools/client/webconsole/new-console-output/actions/filters.js index bbcc9bdf39f5..cd0e3adbdee5 100644 --- a/devtools/client/webconsole/new-console-output/actions/filters.js +++ b/devtools/client/webconsole/new-console-output/actions/filters.js @@ -7,7 +7,6 @@ "use strict"; const { getAllFilters } = require("devtools/client/webconsole/new-console-output/selectors/filters"); -const Services = require("Services"); const { FILTER_TEXT_SET, @@ -27,19 +26,18 @@ function filterTextSet(text) { } function filterToggle(filter) { - return (dispatch, getState) => { + return (dispatch, getState, {prefsService}) => { dispatch({ type: FILTER_TOGGLE, filter, }); const filterState = getAllFilters(getState()); - Services.prefs.setBoolPref(PREFS.FILTER[filter.toUpperCase()], - filterState[filter]); + prefsService.setBoolPref(PREFS.FILTER[filter.toUpperCase()], filterState[filter]); }; } function filtersClear() { - return (dispatch, getState) => { + return (dispatch, getState, {prefsService}) => { dispatch({ type: FILTERS_CLEAR, }); @@ -47,7 +45,7 @@ function filtersClear() { const filterState = getAllFilters(getState()); for (let filter in filterState) { if (filter !== FILTERS.TEXT) { - Services.prefs.clearUserPref(PREFS.FILTER[filter.toUpperCase()]); + prefsService.clearUserPref(PREFS.FILTER[filter.toUpperCase()]); } } }; @@ -60,7 +58,7 @@ function filtersClear() { * to keep non-default filters the user might have set. */ function defaultFiltersReset() { - return (dispatch, getState) => { + return (dispatch, getState, {prefsService}) => { // Get the state before dispatching so the action does not alter prefs reset. const filterState = getAllFilters(getState()); @@ -70,7 +68,7 @@ function defaultFiltersReset() { DEFAULT_FILTERS.forEach(filter => { if (filterState[filter] === false) { - Services.prefs.clearUserPref(PREFS.FILTER[filter.toUpperCase()]); + prefsService.clearUserPref(PREFS.FILTER[filter.toUpperCase()]); } }); }; diff --git a/devtools/client/webconsole/new-console-output/actions/ui.js b/devtools/client/webconsole/new-console-output/actions/ui.js index d5cc7e360773..1ecba81fb9b0 100644 --- a/devtools/client/webconsole/new-console-output/actions/ui.js +++ b/devtools/client/webconsole/new-console-output/actions/ui.js @@ -8,7 +8,6 @@ const { getAllUi } = require("devtools/client/webconsole/new-console-output/selectors/ui"); const { getMessage } = require("devtools/client/webconsole/new-console-output/selectors/messages"); -const Services = require("Services"); const { FILTER_BAR_TOGGLE, @@ -22,22 +21,22 @@ const { } = require("devtools/client/webconsole/new-console-output/constants"); function filterBarToggle(show) { - return (dispatch, getState) => { + return (dispatch, getState, {prefsService}) => { dispatch({ type: FILTER_BAR_TOGGLE, }); - const uiState = getAllUi(getState()); - Services.prefs.setBoolPref(PREFS.UI.FILTER_BAR, uiState.filterBarVisible); + const {filterBarVisible} = getAllUi(getState()); + prefsService.setBoolPref(PREFS.UI.FILTER_BAR, filterBarVisible); }; } function persistToggle(show) { - return (dispatch, getState) => { + return (dispatch, getState, {prefsService}) => { dispatch({ type: PERSIST_TOGGLE, }); const uiState = getAllUi(getState()); - Services.prefs.setBoolPref(PREFS.UI.PERSIST, uiState.persistLogs); + prefsService.setBoolPref(PREFS.UI.PERSIST, uiState.persistLogs); }; } diff --git a/devtools/client/webconsole/new-console-output/constants.js b/devtools/client/webconsole/new-console-output/constants.js index 31d92158cdc1..2d1b8c6997d6 100644 --- a/devtools/client/webconsole/new-console-output/constants.js +++ b/devtools/client/webconsole/new-console-output/constants.js @@ -30,19 +30,25 @@ const actionTypes = { const prefs = { PREFS: { + // Filter preferences only have the suffix since they can be used either for the + // webconsole or the browser console. FILTER: { - ERROR: "devtools.webconsole.filter.error", - WARN: "devtools.webconsole.filter.warn", - INFO: "devtools.webconsole.filter.info", - LOG: "devtools.webconsole.filter.log", - DEBUG: "devtools.webconsole.filter.debug", - CSS: "devtools.webconsole.filter.css", - NET: "devtools.webconsole.filter.net", - NETXHR: "devtools.webconsole.filter.netxhr", + ERROR: "filter.error", + WARN: "filter.warn", + INFO: "filter.info", + LOG: "filter.log", + DEBUG: "filter.debug", + CSS: "filter.css", + NET: "filter.net", + NETXHR: "filter.netxhr", }, UI: { - FILTER_BAR: "devtools.webconsole.ui.filterbar", + // Filter bar UI preference only have the suffix since it can be used either for + // the webconsole or the browser console. + FILTER_BAR: "ui.filterbar", + // Persist is only used by the webconsole. PERSIST: "devtools.webconsole.persistlog", + // We use the same pref to enable the sidebar on webconsole and browser console. SIDEBAR_TOGGLE: "devtools.webconsole.sidebarToggle", } } diff --git a/devtools/client/webconsole/new-console-output/store.js b/devtools/client/webconsole/new-console-output/store.js index 45c0325c4799..4f5cc5f57b3c 100644 --- a/devtools/client/webconsole/new-console-output/store.js +++ b/devtools/client/webconsole/new-console-output/store.js @@ -11,7 +11,6 @@ const { compose, createStore } = require("devtools/client/shared/vendor/redux"); -const { thunk } = require("devtools/client/shared/redux/middleware/thunk"); const { BATCH_ACTIONS } = require("devtools/client/shared/redux/middleware/debounce"); @@ -24,7 +23,6 @@ const { PREFS, } = require("devtools/client/webconsole/new-console-output/constants"); const { reducers } = require("./reducers/index"); -const Services = require("Services"); const { getMessage, getAllMessagesUiById, @@ -33,33 +31,39 @@ const DataProvider = require("devtools/client/netmonitor/src/connector/firefox-d const { getAllNetworkMessagesUpdateById, } = require("devtools/client/webconsole/new-console-output/selectors/messages"); +const {getPrefsService} = require("devtools/client/webconsole/new-console-output/utils/prefs"); /** * Create and configure store for the Console panel. This is the place * where various enhancers and middleware can be registered. */ function configureStore(hud, options = {}) { - const logLimit = options.logLimit - || Math.max(Services.prefs.getIntPref("devtools.hud.loglimit"), 1); + const prefsService = getPrefsService(hud); + const { + getBoolPref, + getIntPref, + } = prefsService; - const sidebarToggle = Services.prefs.getBoolPref(PREFS.UI.SIDEBAR_TOGGLE); + const logLimit = options.logLimit + || Math.max(getIntPref("devtools.hud.loglimit"), 1); + const sidebarToggle = getBoolPref(PREFS.UI.SIDEBAR_TOGGLE); const initialState = { prefs: PrefState({ logLimit, sidebarToggle }), filters: FilterState({ - error: Services.prefs.getBoolPref(PREFS.FILTER.ERROR), - warn: Services.prefs.getBoolPref(PREFS.FILTER.WARN), - info: Services.prefs.getBoolPref(PREFS.FILTER.INFO), - debug: Services.prefs.getBoolPref(PREFS.FILTER.DEBUG), - log: Services.prefs.getBoolPref(PREFS.FILTER.LOG), - css: Services.prefs.getBoolPref(PREFS.FILTER.CSS), - net: Services.prefs.getBoolPref(PREFS.FILTER.NET), - netxhr: Services.prefs.getBoolPref(PREFS.FILTER.NETXHR), + error: getBoolPref(PREFS.FILTER.ERROR), + warn: getBoolPref(PREFS.FILTER.WARN), + info: getBoolPref(PREFS.FILTER.INFO), + debug: getBoolPref(PREFS.FILTER.DEBUG), + log: getBoolPref(PREFS.FILTER.LOG), + css: getBoolPref(PREFS.FILTER.CSS), + net: getBoolPref(PREFS.FILTER.NET), + netxhr: getBoolPref(PREFS.FILTER.NETXHR), }), ui: UiState({ - filterBarVisible: Services.prefs.getBoolPref(PREFS.UI.FILTER_BAR), + filterBarVisible: getBoolPref(PREFS.UI.FILTER_BAR), networkMessageActiveTabId: "headers", - persistLogs: Services.prefs.getBoolPref(PREFS.UI.PERSIST), + persistLogs: getBoolPref(PREFS.UI.PERSIST), }) }; @@ -67,7 +71,7 @@ function configureStore(hud, options = {}) { createRootReducer(), initialState, compose( - applyMiddleware(thunk), + applyMiddleware(thunk.bind(null, {prefsService})), enableActorReleaser(hud), enableBatching(), enableNetProvider(hud) @@ -75,6 +79,14 @@ function configureStore(hud, options = {}) { ); } +function thunk(options = {}, { dispatch, getState }) { + return next => action => { + return (typeof action === "function") + ? action(dispatch, getState, options) + : next(action); + }; +} + function createRootReducer() { return function rootReducer(state, action) { // We want to compute the new state for all properties except "messages". diff --git a/devtools/client/webconsole/new-console-output/utils/moz.build b/devtools/client/webconsole/new-console-output/utils/moz.build index 3f4edbecd84b..c47fa23029b8 100644 --- a/devtools/client/webconsole/new-console-output/utils/moz.build +++ b/devtools/client/webconsole/new-console-output/utils/moz.build @@ -8,4 +8,5 @@ DevToolsModules( 'id-generator.js', 'messages.js', 'object-inspector.js', + 'prefs.js', ) diff --git a/devtools/client/webconsole/new-console-output/utils/prefs.js b/devtools/client/webconsole/new-console-output/utils/prefs.js new file mode 100644 index 000000000000..2f6b41a3e366 --- /dev/null +++ b/devtools/client/webconsole/new-console-output/utils/prefs.js @@ -0,0 +1,43 @@ +/* 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/. */ + +"use strict"; +const Services = require("Services"); + +function getPreferenceName(hud, suffix) { + if (!suffix) { + console.error("Suffix shouldn't be falsy", {suffix}); + return null; + } + + if (!hud) { + console.error("hud shouldn't be falsy", {hud}); + return null; + } + + if (suffix.startsWith("devtools.")) { + // We don't have a suffix but a full pref name. Let's return it. + return suffix; + } + + const component = hud.isBrowserConsole + ? "browserconsole" + : "webconsole"; + return `devtools.${component}.${suffix}`; +} + +function getPrefsService(hud) { + const getPrefName = pref => getPreferenceName(hud, pref); + + return { + getBoolPref: (pref, deflt) => Services.prefs.getBoolPref(getPrefName(pref), deflt), + getIntPref: (pref, deflt) => Services.prefs.getIntPref(getPrefName(pref), deflt), + setBoolPref: (pref, value) => Services.prefs.setBoolPref(getPrefName(pref), value), + clearUserPref: pref => Services.prefs.clearUserPref(getPrefName(pref)), + }; +} + +module.exports = { + getPrefsService, +};