Bug 1314361: Part 6a - Add helper functions for stripping addonId from origin suffix strings. r?bholley draft
authorKris Maglione <maglione.k@gmail.com>
Fri, 04 Nov 2016 17:58:13 -0700
changeset 434556 c3b85da8670e195467ba123f51733cf6592b6e85
parent 434555 3e81fcd527538a46b3681362f1a2345f523e8943
child 434557 968ee72c07fc87fcdc1ff4313e397bb9728fa198
push id34775
push usermaglione.k@gmail.com
push dateSun, 06 Nov 2016 20:02:30 +0000
reviewersbholley
bugs1314361
milestone52.0a1
Bug 1314361: Part 6a - Add helper functions for stripping addonId from origin suffix strings. r?bholley MozReview-Commit-ID: ItPzQKZlRaL
caps/BasePrincipal.cpp
caps/BasePrincipal.h
--- 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() {}