Bug 1182347 - Implement OriginAttributesPattern. r=sicking,f=allstars.chh
authorBobby Holley <bobbyholley@gmail.com>
Thu, 09 Jul 2015 11:36:35 -0700
changeset 252950 696c6303dcd69c3dcc7a8ff8b9e5965ca4c94c12
parent 252949 f590b208d656ab60925b90cc639002ed5868350e
child 252951 ea814efe34e923ea3db566919d78324e729df3df
push id29052
push userryanvm@gmail.com
push dateWed, 15 Jul 2015 17:20:06 +0000
treeherdermozilla-central@04fa2c708b30 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewerssicking
bugs1182347
milestone42.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1182347 - Implement OriginAttributesPattern. r=sicking,f=allstars.chh
caps/BasePrincipal.h
dom/base/ChromeUtils.cpp
dom/base/ChromeUtils.h
dom/webidl/ChromeUtils.webidl
--- a/caps/BasePrincipal.h
+++ b/caps/BasePrincipal.h
@@ -51,16 +51,49 @@ public:
   void CookieJar(nsACString& aStr);
 
   // Populates the attributes from a string like
   // |uri!key1=value1&key2=value2| and returns the uri without the suffix.
   bool PopulateFromOrigin(const nsACString& aOrigin,
                           nsACString& aOriginNoSuffix);
 };
 
+class OriginAttributesPattern : public dom::OriginAttributesPatternDictionary
+{
+public:
+  // To convert a JSON string to an OriginAttributesPattern, do the following:
+  //
+  // OriginAttributesPattern pattern;
+  // if (!pattern.Init(aJSONString)) {
+  //   ... // handle failure.
+  // }
+  OriginAttributesPattern() {}
+
+  explicit OriginAttributesPattern(const OriginAttributesPatternDictionary& aOther)
+    : OriginAttributesPatternDictionary(aOther) {}
+
+  // Performs a match of |aAttrs| against this pattern.
+  bool Matches(const OriginAttributes& aAttrs) const
+  {
+    if (mAppId.WasPassed() && mAppId.Value() != aAttrs.mAppId) {
+      return false;
+    }
+
+    if (mInBrowser.WasPassed() && mInBrowser.Value() != aAttrs.mInBrowser) {
+      return false;
+    }
+
+    if (mAddonId.WasPassed() && mAddonId.Value() != aAttrs.mAddonId) {
+      return false;
+    }
+
+    return true;
+  }
+};
+
 /*
  * Base class from which all nsIPrincipal implementations inherit. Use this for
  * default implementations and other commonalities between principal
  * implementations.
  *
  * We should merge nsJSPrincipals into this class at some point.
  */
 class BasePrincipal : public nsJSPrincipals
--- a/dom/base/ChromeUtils.cpp
+++ b/dom/base/ChromeUtils.cpp
@@ -24,10 +24,20 @@ ChromeUtils::OriginAttributesToSuffix(do
                                       const dom::OriginAttributesDictionary& aAttrs,
                                       nsCString& aSuffix)
 
 {
   OriginAttributes attrs(aAttrs);
   attrs.CreateSuffix(aSuffix);
 }
 
+/* static */ bool
+ChromeUtils::OriginAttributesMatchPattern(dom::GlobalObject& aGlobal,
+                                          const dom::OriginAttributesDictionary& aAttrs,
+                                          const dom::OriginAttributesPatternDictionary& aPattern)
+{
+  OriginAttributes attrs(aAttrs);
+  OriginAttributesPattern pattern(aPattern);
+  return pattern.Matches(attrs);
+}
+
 } // namespace dom
 } // namespace mozilla
--- a/dom/base/ChromeUtils.h
+++ b/dom/base/ChromeUtils.h
@@ -44,14 +44,19 @@ public:
   OriginAttributesToCookieJar(dom::GlobalObject& aGlobal,
                               const dom::OriginAttributesDictionary& aAttrs,
                               nsCString& aCookieJar);
 
   static void
   OriginAttributesToSuffix(dom::GlobalObject& aGlobal,
                            const dom::OriginAttributesDictionary& aAttrs,
                            nsCString& aSuffix);
+
+  static bool
+  OriginAttributesMatchPattern(dom::GlobalObject& aGlobal,
+                               const dom::OriginAttributesDictionary& aAttrs,
+                               const dom::OriginAttributesPatternDictionary& aPattern);
 };
 
 } // namespace dom
 } // namespace mozilla
 
 #endif // mozilla_dom_ChromeUtils__
--- a/dom/webidl/ChromeUtils.webidl
+++ b/dom/webidl/ChromeUtils.webidl
@@ -21,24 +21,45 @@ interface ChromeUtils : ThreadSafeChrome
 
   /**
    * A helper that converts OriginAttributesDictionary to a opaque suffix string.
    *
    * @param originAttrs       The originAttributes from the caller.
    */
   static ByteString
   originAttributesToSuffix(optional OriginAttributesDictionary originAttrs);
+
+  /**
+   * Returns true if the members of |originAttrs| match the provided members
+   * of |pattern|.
+   *
+   * @param originAttrs       The originAttributes under consideration.
+   * @param pattern           The pattern to use for matching.
+   */
+  static boolean
+  originAttributesMatchPattern(optional OriginAttributesDictionary originAttrs,
+                               optional OriginAttributesPatternDictionary pattern);
 };
 
 /**
  * Used by principals and the script security manager to represent origin
- * attributes.
+ * attributes. The first dictionary is designed to contain the full set of
+ * OriginAttributes, the second is used for pattern-matching (i.e. does this
+ * OriginAttributesDictionary match the non-empty attributes in this pattern).
  *
- * IMPORTANT: If you add any members here, you need to update the
- * methods on mozilla::OriginAttributes, and bump the CIDs of all
- * the principal implementations that use OriginAttributes in their
- * nsISerializable implementations.
+ * IMPORTANT: If you add any members here, you need to do the following:
+ * (1) Add them to both dictionaries.
+ * (2) Update the methods on mozilla::OriginAttributes, including equality,
+ *     serialization, and deserialization.
+ * (3) Update the methods on mozilla::OriginAttributesPattern, including matching.
+ * (4) Bump the CIDs (_not_ IIDs) of all the principal implementations that
+ *     use OriginAttributes in their nsISerializable implementations.
  */
 dictionary OriginAttributesDictionary {
   unsigned long appId = 0;
   boolean inBrowser = false;
   DOMString addonId = "";
 };
+dictionary OriginAttributesPatternDictionary {
+  unsigned long appId;
+  boolean inBrowser;
+  DOMString addonId;
+};