summaryrefslogtreecommitdiffstats
path: root/devtools/client/application/src/reducers
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-19 00:47:55 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-19 00:47:55 +0000
commit26a029d407be480d791972afb5975cf62c9360a6 (patch)
treef435a8308119effd964b339f76abb83a57c29483 /devtools/client/application/src/reducers
parentInitial commit. (diff)
downloadfirefox-26a029d407be480d791972afb5975cf62c9360a6.tar.xz
firefox-26a029d407be480d791972afb5975cf62c9360a6.zip
Adding upstream version 124.0.1.upstream/124.0.1
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'devtools/client/application/src/reducers')
-rw-r--r--devtools/client/application/src/reducers/index.js28
-rw-r--r--devtools/client/application/src/reducers/manifest-state.js158
-rw-r--r--devtools/client/application/src/reducers/moz.build11
-rw-r--r--devtools/client/application/src/reducers/page-state.js39
-rw-r--r--devtools/client/application/src/reducers/ui-state.js30
-rw-r--r--devtools/client/application/src/reducers/workers-state.js65
6 files changed, 331 insertions, 0 deletions
diff --git a/devtools/client/application/src/reducers/index.js b/devtools/client/application/src/reducers/index.js
new file mode 100644
index 0000000000..8b93290d9d
--- /dev/null
+++ b/devtools/client/application/src/reducers/index.js
@@ -0,0 +1,28 @@
+/* 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 {
+ combineReducers,
+} = require("resource://devtools/client/shared/vendor/redux.js");
+const {
+ workersReducer,
+} = require("resource://devtools/client/application/src/reducers/workers-state.js");
+const {
+ pageReducer,
+} = require("resource://devtools/client/application/src/reducers/page-state.js");
+const {
+ uiReducer,
+} = require("resource://devtools/client/application/src/reducers/ui-state.js");
+const {
+ manifestReducer,
+} = require("resource://devtools/client/application/src/reducers/manifest-state.js");
+
+module.exports = combineReducers({
+ manifest: manifestReducer,
+ page: pageReducer,
+ workers: workersReducer,
+ ui: uiReducer,
+});
diff --git a/devtools/client/application/src/reducers/manifest-state.js b/devtools/client/application/src/reducers/manifest-state.js
new file mode 100644
index 0000000000..61a2fa6759
--- /dev/null
+++ b/devtools/client/application/src/reducers/manifest-state.js
@@ -0,0 +1,158 @@
+/* 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 {
+ MANIFEST_CATEGORIES,
+ MANIFEST_ISSUE_LEVELS,
+ MANIFEST_MEMBER_VALUE_TYPES,
+ FETCH_MANIFEST_FAILURE,
+ FETCH_MANIFEST_START,
+ FETCH_MANIFEST_SUCCESS,
+ RESET_MANIFEST,
+} = require("resource://devtools/client/application/src/constants.js");
+
+function _processRawManifestIcons(rawIcons) {
+ // NOTE: about `rawIcons` array we are getting from platform:
+ // - Icons that do not comform to the spec are filtered out
+ // - We will always get a `src`
+ // - We will always get `purpose` with a value (default is `["any"]`)
+ // - `sizes` may be undefined
+ // - `type` may be undefined
+ return rawIcons.map(icon => {
+ return {
+ key: {
+ sizes: Array.isArray(icon.sizes) ? icon.sizes.join(" ") : icon.sizes,
+ contentType: icon.type,
+ },
+ value: {
+ src: icon.src,
+ purpose: icon.purpose.join(" "),
+ },
+ type: MANIFEST_MEMBER_VALUE_TYPES.ICON,
+ };
+ });
+}
+
+function _processRawManifestMembers(rawManifest) {
+ function getCategoryForMember(key) {
+ switch (key) {
+ case "name":
+ case "short_name":
+ return MANIFEST_CATEGORIES.IDENTITY;
+ default:
+ return MANIFEST_CATEGORIES.PRESENTATION;
+ }
+ }
+
+ function getValueTypeForMember(key) {
+ switch (key) {
+ case "start_url":
+ case "scope":
+ return MANIFEST_MEMBER_VALUE_TYPES.URL;
+ case "theme_color":
+ case "background_color":
+ return MANIFEST_MEMBER_VALUE_TYPES.COLOR;
+ default:
+ return MANIFEST_MEMBER_VALUE_TYPES.STRING;
+ }
+ }
+
+ const res = {
+ [MANIFEST_CATEGORIES.IDENTITY]: [],
+ [MANIFEST_CATEGORIES.PRESENTATION]: [],
+ };
+
+ // filter out extra metadata members (those with moz_ prefix) and icons
+ const rawMembers = Object.entries(rawManifest).filter(
+ ([key, value]) => !key.startsWith("moz_") && !(key === "icons")
+ );
+
+ for (const [key, value] of rawMembers) {
+ const category = getCategoryForMember(key);
+ const type = getValueTypeForMember(key);
+ res[category].push({ key, value, type });
+ }
+
+ return res;
+}
+
+function _processRawManifestIssues(issues) {
+ return issues.map(x => {
+ return {
+ level: x.warn
+ ? MANIFEST_ISSUE_LEVELS.WARNING
+ : MANIFEST_ISSUE_LEVELS.ERROR,
+ message: x.warn || x.error,
+ type: x.type || null,
+ };
+ });
+}
+
+function _processRawManifest(rawManifest) {
+ const res = {
+ url: rawManifest.moz_manifest_url,
+ };
+
+ // group manifest members by category
+ Object.assign(res, _processRawManifestMembers(rawManifest));
+ // process icons
+ res.icons = _processRawManifestIcons(rawManifest.icons || []);
+ // process error messages
+ res.validation = _processRawManifestIssues(rawManifest.moz_validation || []);
+
+ return res;
+}
+
+function ManifestState() {
+ return {
+ errorMessage: "",
+ isLoading: false,
+ manifest: undefined,
+ };
+}
+
+function manifestReducer(state = ManifestState(), action) {
+ switch (action.type) {
+ case FETCH_MANIFEST_START:
+ return Object.assign({}, state, {
+ isLoading: true,
+ mustLoadManifest: false,
+ });
+
+ case FETCH_MANIFEST_FAILURE:
+ const { error } = action;
+ // If we add a redux middleware to log errors, we should move the
+ // console.error below there.
+ console.error(error);
+ return Object.assign({}, state, {
+ errorMessage: error,
+ isLoading: false,
+ manifest: null,
+ });
+
+ case FETCH_MANIFEST_SUCCESS:
+ // NOTE: we don't get an error when the page does not have a manifest,
+ // but a `null` value there.
+ const { manifest } = action;
+ return Object.assign({}, state, {
+ errorMessage: "",
+ isLoading: false,
+ manifest: manifest ? _processRawManifest(manifest) : null,
+ });
+
+ case RESET_MANIFEST:
+ const defaultState = ManifestState();
+ return defaultState;
+
+ default:
+ return state;
+ }
+}
+
+module.exports = {
+ ManifestState,
+ manifestReducer,
+};
diff --git a/devtools/client/application/src/reducers/moz.build b/devtools/client/application/src/reducers/moz.build
new file mode 100644
index 0000000000..752b27a685
--- /dev/null
+++ b/devtools/client/application/src/reducers/moz.build
@@ -0,0 +1,11 @@
+# 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/.
+
+DevToolsModules(
+ "index.js",
+ "manifest-state.js",
+ "page-state.js",
+ "ui-state.js",
+ "workers-state.js",
+)
diff --git a/devtools/client/application/src/reducers/page-state.js b/devtools/client/application/src/reducers/page-state.js
new file mode 100644
index 0000000000..ef3b6c970d
--- /dev/null
+++ b/devtools/client/application/src/reducers/page-state.js
@@ -0,0 +1,39 @@
+/* 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 {
+ UPDATE_DOMAIN,
+} = require("resource://devtools/client/application/src/constants.js");
+
+function PageState() {
+ return {
+ // Domain
+ domain: null,
+ };
+}
+
+function getDomainFromUrl(url) {
+ return new URL(url).hostname;
+}
+
+function pageReducer(state = PageState(), action) {
+ switch (action.type) {
+ case UPDATE_DOMAIN: {
+ const { url } = action;
+ return {
+ domain: getDomainFromUrl(url),
+ };
+ }
+
+ default:
+ return state;
+ }
+}
+
+module.exports = {
+ PageState,
+ pageReducer,
+};
diff --git a/devtools/client/application/src/reducers/ui-state.js b/devtools/client/application/src/reducers/ui-state.js
new file mode 100644
index 0000000000..81a57c8086
--- /dev/null
+++ b/devtools/client/application/src/reducers/ui-state.js
@@ -0,0 +1,30 @@
+/* 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 {
+ DEFAULT_PAGE,
+ UPDATE_SELECTED_PAGE,
+} = require("resource://devtools/client/application/src/constants.js");
+
+function UiState() {
+ return {
+ selectedPage: DEFAULT_PAGE,
+ };
+}
+
+function uiReducer(state = UiState(), action) {
+ switch (action.type) {
+ case UPDATE_SELECTED_PAGE:
+ return Object.assign({}, state, { selectedPage: action.selectedPage });
+ default:
+ return state;
+ }
+}
+
+module.exports = {
+ UiState,
+ uiReducer,
+};
diff --git a/devtools/client/application/src/reducers/workers-state.js b/devtools/client/application/src/reducers/workers-state.js
new file mode 100644
index 0000000000..004c25ddfa
--- /dev/null
+++ b/devtools/client/application/src/reducers/workers-state.js
@@ -0,0 +1,65 @@
+/* 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 {
+ START_WORKER,
+ UNREGISTER_WORKER,
+ UPDATE_CAN_DEBUG_WORKERS,
+ UPDATE_WORKERS,
+} = require("resource://devtools/client/application/src/constants.js");
+
+function WorkersState() {
+ return {
+ // Array of all service worker registrations
+ list: [],
+ canDebugWorkers: false,
+ };
+}
+
+function buildWorkerDataFromFronts({ registration, workers }) {
+ return {
+ id: registration.id,
+ lastUpdateTime: registration.lastUpdateTime,
+ registrationFront: registration,
+ scope: registration.scope,
+ workers: workers.map(worker => ({
+ id: worker.id,
+ url: worker.url,
+ state: worker.state,
+ stateText: worker.stateText,
+ registrationFront: registration,
+ workerDescriptorFront: worker.workerDescriptorFront,
+ })),
+ };
+}
+
+function workersReducer(state = WorkersState(), action) {
+ switch (action.type) {
+ case UPDATE_CAN_DEBUG_WORKERS: {
+ return Object.assign({}, state, {
+ canDebugWorkers: action.canDebugWorkers,
+ });
+ }
+ case UPDATE_WORKERS: {
+ const { workers } = action;
+ return Object.assign({}, state, {
+ list: workers.map(buildWorkerDataFromFronts).flat(),
+ });
+ }
+ // these actions don't change the state, but get picked up by the
+ // telemetry middleware
+ case START_WORKER:
+ case UNREGISTER_WORKER:
+ return state;
+ default:
+ return state;
+ }
+}
+
+module.exports = {
+ WorkersState,
+ workersReducer,
+};