Bug 1182347 - Implement OriginAttributesPattern. r=sicking,f=allstars.chh

This commit is contained in:
Bobby Holley 2015-07-09 11:36:35 -07:00
parent bd0df6e67e
commit 087490dde5
4 changed files with 74 additions and 5 deletions

View File

@ -56,6 +56,39 @@ public:
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

View File

@ -29,5 +29,15 @@ ChromeUtils::OriginAttributesToSuffix(dom::GlobalObject& aGlobal,
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

View File

@ -49,6 +49,11 @@ public:
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

View File

@ -26,19 +26,40 @@ interface ChromeUtils : ThreadSafeChromeUtils {
*/
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;
};