Bug 1576126 - Port Bug 961529 "Add Feeds module" to SeaMonkey. r=frg
authorIan Neal <iann_cvs@blueyonder.co.uk>
Tue, 03 Sep 2019 21:29:12 +0200
changeset 27492 f4394597a1cd467b79aaaf5a38f151792f152ead
parent 27491 f76b01af132e761aa7e3c32caaef7602cc965393
child 27493 7a0523b39bc54c162fdce0c90fb0fc64dee810a8
push id16358
push userfrgrahl@gmx.net
push dateTue, 03 Sep 2019 19:29:52 +0000
treeherdercomm-central@7a0523b39bc5 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersfrg
bugs1576126, 961529
Bug 1576126 - Port Bug 961529 "Add Feeds module" to SeaMonkey. r=frg
suite/base/content/utilityOverlay.js
suite/browser/linkToolbarHandler.js
suite/browser/pageinfo/feeds.js
suite/browser/test/browser/browser_bug413915.js
suite/modules/Feeds.jsm
suite/modules/moz.build
--- a/suite/base/content/utilityOverlay.js
+++ b/suite/base/content/utilityOverlay.js
@@ -1302,46 +1302,16 @@ function hostUrl()
   return url;
 }
 
 function disablePopupBlockerNotifications()
 {
   Services.prefs.setBoolPref("privacy.popups.showBrowserMessage", false);
 }
 
-/**
- * isValidFeed: checks whether the given data represents a valid feed.
- *
- * @param  aData
- *         An object representing a feed with title, href and type.
- * @param  aPrincipal
- *         The principal of the document, used for security check.
- * @param  aIsFeed
- *         Whether this is already a known feed or not, if true only a security
- *         check will be performed.
- */
-function isValidFeed(aData, aPrincipal, aIsFeed)
-{
-  if (!aData || !aPrincipal)
-    return null;
-
-  var type = aData.type.toLowerCase().replace(/^\s+|\s*(?:;.*)?$/g, "");
-  if (aIsFeed || /^application\/(?:atom|rss)\+xml$/.test(type)) {
-    try {
-      urlSecurityCheck(aData.href, aPrincipal,
-                       Ci.nsIScriptSecurityManager.DISALLOW_INHERIT_PRINCIPAL);
-      return type || "application/rss+xml";
-    }
-    catch(ex) {
-    }
-  }
-
-  return null;
-}
-
 // Used as an onclick handler for UI elements with link-like behavior.
 // e.g. onclick="checkForMiddleClick(this, event);"
 function checkForMiddleClick(node, event) {
   // We should be using the disabled property here instead of the attribute,
   // but some elements that this function is used with don't support it (e.g.
   // menuitem).
   if (node.getAttribute("disabled") == "true")
     return; // Do nothing
--- a/suite/browser/linkToolbarHandler.js
+++ b/suite/browser/linkToolbarHandler.js
@@ -1,12 +1,15 @@
 /* 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/. */
 
+XPCOMUtils.defineLazyModuleGetter(this, "Feeds",
+                                  "resource:///modules/Feeds.jsm");
+
 /**
  * LinkToolbarHandler is a Singleton that displays LINK elements
  * and nodeLists of LINK elements in the Link Toolbar.  It
  * associates the LINK with a corresponding LinkToolbarItem based
  * on it's REL attribute and the toolbar item's ID attribute.
  * LinkToolbarHandler is also a Factory and will create
  * LinkToolbarItems as necessary.
  */
@@ -77,17 +80,17 @@ function(relAttribute, element)
     case "contents":
     case "toc":
       return "toc";
 
     case "feed":
       isFeed = true;
       // fall through
     case "alternate":
-      if (isValidFeed(element, element.nodePrincipal, isFeed)) {
+      if (Feeds.isValidFeed(element, element.nodePrincipal, isFeed)) {
         return "feed";
       }
 
       if (!isFeed) {
         return "alternate";
       }
       // fall through
     case "prefetch":
--- a/suite/browser/pageinfo/feeds.js
+++ b/suite/browser/pageinfo/feeds.js
@@ -1,13 +1,16 @@
 /* -*- Mode: Javascript; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
 /* 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/. */
 
+XPCOMUtils.defineLazyModuleGetter(this, "Feeds",
+                                  "resource:///modules/Feeds.jsm");
+
 function initFeedTab()
 {
   const feedTypes = {
     "application/rss+xml": gBundle.getString("feedRss"),
     "application/atom+xml": gBundle.getString("feedAtom"),
     "text/xml": gBundle.getString("feedXML"),
     "application/xml": gBundle.getString("feedXML"),
     "application/rdf+xml": gBundle.getString("feedXML")
@@ -20,17 +23,17 @@ function initFeedTab()
     var link = linkNodes[i];
     if (!link.href)
       continue;
 
     var rel = link.rel && link.rel.toLowerCase();
     var isFeed = /(?:^|\s)feed(?:\s|$)/i.test(rel);
     if (isFeed || (/(?:^|\s)alternate(?:\s|$)/i.test(rel) &&
                    !/(?:^|\s)stylesheet(?:\s|$)/i.test(rel))) {
-      var type = isValidFeed(link, link.nodePrincipal, isFeed);
+      var type = Feeds.isValidFeed(link, link.nodePrincipal, isFeed);
       if (type) {
         if (type in feedTypes)
           type = feedTypes[type];
         else
           type = feedTypes["application/rss+xml"];
         addRow(link.title, type, link.href);
       }
     }
--- a/suite/browser/test/browser/browser_bug413915.js
+++ b/suite/browser/test/browser/browser_bug413915.js
@@ -1,19 +1,29 @@
+XPCOMUtils.defineLazyModuleGetter(this, "Feeds",
+                                  "resource:///modules/Feeds.jsm");
+
 function test() {
-  var ioserv = Cc["@mozilla.org/network/io-service;1"].
-               getService(Ci.nsIIOService);
+  var ioserv = Cc["@mozilla.org/network/io-service;1"]
+                 .getService(Ci.nsIIOService);
   var exampleUri = ioserv.newURI("http://example.com/");
-  var secman = Cc["@mozilla.org/scriptsecuritymanager;1"].
-               getService(Ci.nsIScriptSecurityManager);
+  var secman = Cc["@mozilla.org/scriptsecuritymanager;1"]
+                 .getService(Ci.nsIScriptSecurityManager);
   var principal = secman.createCodebasePrincipal(exampleUri, {});
 
   function testIsFeed(aTitle, aHref, aType, aKnown) {
-    var link = { title: aTitle, href: aHref, type: aType };
-    return isValidFeed(link, principal, aKnown);
+    var link = {
+      title: aTitle,
+      href: aHref,
+      type: aType,
+      ownerDocument: {
+        characterSet: "UTF-8"
+      }
+    };
+    return Feeds.isValidFeed(link, principal, aKnown);
   }
 
   var href = "http://example.com/feed/";
   var atomType = "application/atom+xml";
   var funkyAtomType = " aPPLICAtion/Atom+XML ";
   var rssType = "application/rss+xml";
   var funkyRssType = " Application/RSS+XML  ";
   var rdfType = "application/rdf+xml";
new file mode 100644
--- /dev/null
+++ b/suite/modules/Feeds.jsm
@@ -0,0 +1,51 @@
+/* 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";
+
+var EXPORTED_SYMBOLS = [ "Feeds" ];
+
+const { XPCOMUtils } = ChromeUtils.import("resource://gre/modules/XPCOMUtils.jsm");
+
+XPCOMUtils.defineLazyModuleGetters(this, {
+  BrowserUtils: "resource://gre/modules/BrowserUtils.jsm",
+});
+
+var Feeds = {
+
+  /**
+   * isValidFeed: checks whether the given data represents a valid feed.
+   *
+   * @param  aLink
+   *         An object representing a feed with title, href and type.
+   * @param  aPrincipal
+   *         The principal of the document, used for security check.
+   * @param  aIsFeed
+   *         Whether this is already a known feed or not, if true only a security
+   *         check will be performed.
+   */
+  isValidFeed(aLink, aPrincipal, aIsFeed) {
+    if (!aLink || !aPrincipal)
+      return false;
+
+    var type = aLink.type.toLowerCase().replace(/^\s+|\s*(?:;.*)?$/g, "");
+    if (!aIsFeed) {
+      aIsFeed = (type == "application/rss+xml" ||
+                 type == "application/atom+xml");
+    }
+
+    if (aIsFeed) {
+      try {
+        let href = BrowserUtils.makeURI(aLink.href, aLink.ownerDocument.characterSet)
+        BrowserUtils.urlSecurityCheck(href, aPrincipal,
+                                      Ci.nsIScriptSecurityManager.DISALLOW_INHERIT_PRINCIPAL);
+        return type || "application/rss+xml";
+      } catch (ex) {
+      }
+    }
+
+    return null;
+  },
+
+};
--- a/suite/modules/moz.build
+++ b/suite/modules/moz.build
@@ -1,16 +1,17 @@
 # vim: set filetype=python:
 # 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/.
 
 XPCSHELL_TESTS_MANIFESTS += ['test/unit/xpcshell.ini']
 
 EXTRA_JS_MODULES += [
+    'Feeds.jsm',
     'OpenInTabsUtils.jsm',
     'PermissionUI.jsm',
     'RecentWindow.jsm',
     'Sanitizer.jsm',
     'SitePermissions.jsm',
     'WindowsPreviewPerTab.jsm',
 ]