Bug 1314361: Part 6a - Add helper functions for stripping addonId from origin suffix strings. r?bholley
MozReview-Commit-ID: ItPzQKZlRaL
--- a/caps/BasePrincipal.cpp
+++ b/caps/BasePrincipal.cpp
@@ -331,16 +331,71 @@ OriginAttributes::PopulateFromOrigin(con
aOriginNoSuffix = origin;
return true;
}
aOriginNoSuffix = Substring(origin, 0, pos);
return PopulateFromSuffix(Substring(origin, pos));
}
+bool
+OriginAttributes::StripAddonIdFromSuffix(const nsACString& aStr, nsACString& aOutput)
+{
+ if (aStr.IsEmpty()) {
+ return false;
+ }
+
+ if (aStr[0] != '^') {
+ return false;
+ }
+
+ UniquePtr<URLParams> params(new URLParams());
+ params->ParseInput(Substring(aStr, 1, aStr.Length() - 1));
+
+ if (!params->Delete(NS_LITERAL_STRING("addonId"))) {
+ return false;
+ }
+
+ aOutput.Truncate();
+
+ nsAutoString value;
+ params->Serialize(value);
+ if (!value.IsEmpty()) {
+ aOutput.AppendLiteral("^");
+ aOutput.Append(NS_ConvertUTF16toUTF8(value));
+ }
+
+ return true;
+}
+
+bool
+OriginAttributes::StripAddonIdFromOrigin(const nsACString& aOrigin,
+ nsACString& aOutput)
+{
+ // RFindChar is only available on nsCString.
+ nsCString origin(aOrigin);
+ int32_t pos = origin.RFindChar('^');
+
+ if (pos == kNotFound) {
+ return false;
+ }
+
+ nsAutoCString newSuffix;
+ if (!StripAddonIdFromSuffix(Substring(origin, pos), newSuffix)) {
+ return false;
+ }
+
+ nsAutoCString result;
+ result.Append(Substring(origin, 0, pos));
+ result.Append(newSuffix);
+ aOutput.Assign(result);
+
+ return true;
+}
+
void
OriginAttributes::SyncAttributesWithPrivateBrowsing(bool aInPrivateBrowsing)
{
mPrivateBrowsingId = aInPrivateBrowsing ? 1 : 0;
}
void
OriginAttributes::SetFromGenericAttributes(const GenericOriginAttributes& aAttrs)
--- a/caps/BasePrincipal.h
+++ b/caps/BasePrincipal.h
@@ -51,16 +51,22 @@ public:
void CreateSuffix(nsACString& aStr) const;
MOZ_MUST_USE bool PopulateFromSuffix(const nsACString& aStr);
// Populates the attributes from a string like
// |uri!key1=value1&key2=value2| and returns the uri without the suffix.
MOZ_MUST_USE bool PopulateFromOrigin(const nsACString& aOrigin,
nsACString& aOriginNoSuffix);
+ MOZ_MUST_USE static bool StripAddonIdFromSuffix(const nsACString& aSuffix,
+ nsACString& aOutput);
+
+ MOZ_MUST_USE static bool StripAddonIdFromOrigin(const nsACString& aOrigin,
+ nsACString& aOutput);
+
// Helper function to match mIsPrivateBrowsing to existing private browsing
// flags. Once all other flags are removed, this can be removed too.
void SyncAttributesWithPrivateBrowsing(bool aInPrivateBrowsing);
void SetFromGenericAttributes(const GenericOriginAttributes& aAttrs);
protected:
OriginAttributes() {}