summaryrefslogtreecommitdiffstats
path: root/devtools/client/jsonview/Sniffer.sys.mjs
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-07 17:32:43 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-07 17:32:43 +0000
commit6bf0a5cb5034a7e684dcc3500e841785237ce2dd (patch)
treea68f146d7fa01f0134297619fbe7e33db084e0aa /devtools/client/jsonview/Sniffer.sys.mjs
parentInitial commit. (diff)
downloadthunderbird-6bf0a5cb5034a7e684dcc3500e841785237ce2dd.tar.xz
thunderbird-6bf0a5cb5034a7e684dcc3500e841785237ce2dd.zip
Adding upstream version 1:115.7.0.upstream/1%115.7.0upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'devtools/client/jsonview/Sniffer.sys.mjs')
-rw-r--r--devtools/client/jsonview/Sniffer.sys.mjs64
1 files changed, 64 insertions, 0 deletions
diff --git a/devtools/client/jsonview/Sniffer.sys.mjs b/devtools/client/jsonview/Sniffer.sys.mjs
new file mode 100644
index 0000000000..d585ce8ab2
--- /dev/null
+++ b/devtools/client/jsonview/Sniffer.sys.mjs
@@ -0,0 +1,64 @@
+/* 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/. */
+
+import { XPCOMUtils } from "resource://gre/modules/XPCOMUtils.sys.mjs";
+
+const gPrefs = {};
+
+XPCOMUtils.defineLazyPreferenceGetter(
+ gPrefs,
+ "gEnabled",
+ "devtools.jsonview.enabled"
+);
+
+const JSON_VIEW_MIME_TYPE = "application/vnd.mozilla.json.view";
+
+function getContentDisposition(channel) {
+ try {
+ return channel.contentDisposition;
+ } catch (e) {
+ // Channel doesn't support content dispositions
+ return null;
+ }
+}
+
+/**
+ * This component represents a sniffer (implements nsIContentSniffer
+ * interface) responsible for changing top level 'application/json'
+ * document types to: 'application/vnd.mozilla.json.view'.
+ *
+ * This internal type is consequently rendered by JSON View component
+ * that represents the JSON through a viewer interface.
+ *
+ * This is done in the .js file rather than a .jsm to avoid creating
+ * a compartment at startup when no JSON is being viewed.
+ */
+export class Sniffer {
+ getMIMETypeFromContent(request, data, length) {
+ if (request instanceof Ci.nsIChannel) {
+ // JSON View is enabled only for top level loads only.
+ if (
+ gPrefs.gEnabled &&
+ request.loadInfo?.isTopLevelLoad &&
+ request.loadFlags & Ci.nsIChannel.LOAD_DOCUMENT_URI &&
+ getContentDisposition(request) != Ci.nsIChannel.DISPOSITION_ATTACHMENT
+ ) {
+ // Check the response content type and if it's a valid type
+ // such as application/json or application/manifest+json
+ // change it to new internal type consumed by JSON View.
+ if (/^application\/(?:.+\+)?json$/.test(request.contentType)) {
+ return JSON_VIEW_MIME_TYPE;
+ }
+ } else if (request.contentType === JSON_VIEW_MIME_TYPE) {
+ return "application/json";
+ }
+ }
+
+ return "";
+ }
+}
+
+Sniffer.prototype.QueryInterface = ChromeUtils.generateQI([
+ "nsIContentSniffer",
+]);