Bug 1177916 - URLSearchParams::GetParentObject should not return nullptr, r=smaug
authorAndrea Marchesini <amarchesini@mozilla.com>
Fri, 26 Jun 2015 17:44:34 -0700
changeset 281235 d4c34d7fb112dbfd548a9c371021980abd14ac96
parent 281234 30cefdf8d020e2badcffdeed6649b9df0adfeeb0
child 281236 0d4f428037b5c4dbce6cdbbeea9a3afb43a2e882
push idunknown
push userunknown
push dateunknown
reviewerssmaug
bugs1177916
milestone41.0a1
Bug 1177916 - URLSearchParams::GetParentObject should not return nullptr, r=smaug
dom/base/Link.cpp
dom/base/URL.cpp
dom/base/URLSearchParams.cpp
dom/base/URLSearchParams.h
dom/fetch/Fetch.cpp
dom/workers/URL.cpp
toolkit/components/places/tests/cpp/mock_Link.h
--- a/dom/base/Link.cpp
+++ b/dom/base/Link.cpp
@@ -608,17 +608,17 @@ Link::UpdateURLSearchParams()
 
   mSearchParams->ParseInput(search);
 }
 
 void
 Link::CreateSearchParamsIfNeeded()
 {
   if (!mSearchParams) {
-    mSearchParams = new URLSearchParams(this);
+    mSearchParams = new URLSearchParams(this, this);
     UpdateURLSearchParams();
   }
 }
 
 void
 Link::Unlink()
 {
   if (mSearchParams) {
--- a/dom/base/URL.cpp
+++ b/dom/base/URL.cpp
@@ -528,15 +528,15 @@ bool IsChromeURI(nsIURI* aURI)
       return isChrome;
   return false;
 }
 
 void
 URL::CreateSearchParamsIfNeeded()
 {
   if (!mSearchParams) {
-    mSearchParams = new URLSearchParams(this);
+    mSearchParams = new URLSearchParams(this, this);
     UpdateURLSearchParams();
   }
 }
 
 }
 }
--- a/dom/base/URLSearchParams.cpp
+++ b/dom/base/URLSearchParams.cpp
@@ -288,32 +288,38 @@ URLParams::Serialize(nsAString& aValue) 
     }
 
     SerializeString(NS_ConvertUTF16toUTF8(mParams[i].mKey), aValue);
     aValue.Append('=');
     SerializeString(NS_ConvertUTF16toUTF8(mParams[i].mValue), aValue);
   }
 }
 
-NS_IMPL_CYCLE_COLLECTION_WRAPPERCACHE(URLSearchParams, mObserver)
+NS_IMPL_CYCLE_COLLECTION_WRAPPERCACHE(URLSearchParams, mParent, mObserver)
 NS_IMPL_CYCLE_COLLECTING_ADDREF(URLSearchParams)
 NS_IMPL_CYCLE_COLLECTING_RELEASE(URLSearchParams)
 
 NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(URLSearchParams)
   NS_WRAPPERCACHE_INTERFACE_MAP_ENTRY
   NS_INTERFACE_MAP_ENTRY(nsISupports)
 NS_INTERFACE_MAP_END
 
-URLSearchParams::URLSearchParams(URLSearchParamsObserver* aObserver)
-  : mParams(new URLParams()), mObserver(aObserver)
+URLSearchParams::URLSearchParams(nsISupports* aParent,
+                                 URLSearchParamsObserver* aObserver)
+  : mParams(new URLParams())
+  , mParent(aParent)
+  , mObserver(aObserver)
 {
 }
 
-URLSearchParams::URLSearchParams(const URLSearchParams& aOther)
-  : mParams(new URLParams(*aOther.mParams.get())), mObserver(aOther.mObserver)
+URLSearchParams::URLSearchParams(nsISupports* aParent,
+                                 const URLSearchParams& aOther)
+  : mParams(new URLParams(*aOther.mParams.get()))
+  , mParent(aParent)
+  , mObserver(aOther.mObserver)
 {
 }
 
 URLSearchParams::~URLSearchParams()
 {
   DeleteAll();
 }
 
@@ -323,27 +329,31 @@ URLSearchParams::WrapObject(JSContext* a
   return URLSearchParamsBinding::Wrap(aCx, this, aGivenProto);
 }
 
 /* static */ already_AddRefed<URLSearchParams>
 URLSearchParams::Constructor(const GlobalObject& aGlobal,
                              const nsAString& aInit,
                              ErrorResult& aRv)
 {
-  nsRefPtr<URLSearchParams> sp = new URLSearchParams(nullptr);
+  nsRefPtr<URLSearchParams> sp =
+    new URLSearchParams(aGlobal.GetAsSupports(), nullptr);
   sp->ParseInput(NS_ConvertUTF16toUTF8(aInit));
+
   return sp.forget();
 }
 
 /* static */ already_AddRefed<URLSearchParams>
 URLSearchParams::Constructor(const GlobalObject& aGlobal,
                              URLSearchParams& aInit,
                              ErrorResult& aRv)
 {
-  nsRefPtr<URLSearchParams> sp = new URLSearchParams(aInit);
+  nsRefPtr<URLSearchParams> sp =
+    new URLSearchParams(aGlobal.GetAsSupports(), aInit);
+
   return sp.forget();
 }
 
 void
 URLSearchParams::ParseInput(const nsACString& aInput)
 {
   mParams->ParseInput(aInput);
 }
--- a/dom/base/URLSearchParams.h
+++ b/dom/base/URLSearchParams.h
@@ -109,24 +109,26 @@ class URLSearchParams final : public nsI
                               public nsWrapperCache
 {
   ~URLSearchParams();
 
 public:
   NS_DECL_CYCLE_COLLECTING_ISUPPORTS
   NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_CLASS(URLSearchParams)
 
-  explicit URLSearchParams(URLSearchParamsObserver* aObserver);
+  URLSearchParams(nsISupports* aParent,
+                  URLSearchParamsObserver* aObserver);
 
-  explicit URLSearchParams(const URLSearchParams& aOther);
+  URLSearchParams(nsISupports* aParent,
+                  const URLSearchParams& aOther);
 
   // WebIDL methods
   nsISupports* GetParentObject() const
   {
-    return nullptr;
+    return mParent;
   }
 
   virtual JSObject*
   WrapObject(JSContext* aCx, JS::Handle<JSObject*> aGivenProto) override;
 
   static already_AddRefed<URLSearchParams>
   Constructor(const GlobalObject& aGlobal, const nsAString& aInit,
               ErrorResult& aRv);
@@ -169,15 +171,16 @@ public:
 private:
   void AppendInternal(const nsAString& aName, const nsAString& aValue);
 
   void DeleteAll();
 
   void NotifyObserver();
 
   UniquePtr<URLParams> mParams;
+  nsCOMPtr<nsISupports> mParent;
   nsRefPtr<URLSearchParamsObserver> mObserver;
 };
 
 } // namespace dom
 } // namespace mozilla
 
 #endif /* mozilla_dom_URLSearchParams_h */
--- a/dom/fetch/Fetch.cpp
+++ b/dom/fetch/Fetch.cpp
@@ -1538,22 +1538,22 @@ FetchBody<Derived>::ContinueConsumeBody(
         NS_NAMED_LITERAL_CSTRING(urlDataMimeType, "application/x-www-form-urlencoded");
         bool isValidUrlEncodedMimeType = StringBeginsWith(mMimeType, urlDataMimeType);
 
         if (isValidUrlEncodedMimeType && mMimeType.Length() > urlDataMimeType.Length()) {
           isValidUrlEncodedMimeType = mMimeType[urlDataMimeType.Length()] == ';';
         }
 
         if (isValidUrlEncodedMimeType) {
-          nsRefPtr<URLSearchParams> params = new URLSearchParams(nullptr);
-          params->ParseInput(data);
+          URLParams params;
+          params.ParseInput(data);
 
           nsRefPtr<nsFormData> fd = new nsFormData(DerivedClass()->GetParentObject());
           FillFormIterator iterator(fd);
-          DebugOnly<bool> status = params->ForEach(iterator);
+          DebugOnly<bool> status = params.ForEach(iterator);
           MOZ_ASSERT(status);
 
           localPromise->MaybeResolve(fd);
         } else {
           ErrorResult result;
           result.ThrowTypeError(MSG_BAD_FORMDATA);
           localPromise->MaybeReject(result);
         }
--- a/dom/workers/URL.cpp
+++ b/dom/workers/URL.cpp
@@ -947,14 +947,14 @@ URL::UpdateURLSearchParams()
     mSearchParams->ParseInput(NS_ConvertUTF16toUTF8(Substring(search, 1)));
   }
 }
 
 void
 URL::CreateSearchParamsIfNeeded()
 {
   if (!mSearchParams) {
-    mSearchParams = new URLSearchParams(this);
+    mSearchParams = new URLSearchParams(this, this);
     UpdateURLSearchParams();
   }
 }
 
 END_WORKERS_NAMESPACE
--- a/toolkit/components/places/tests/cpp/mock_Link.h
+++ b/toolkit/components/places/tests/cpp/mock_Link.h
@@ -140,17 +140,18 @@ NS_IMPL_CYCLE_COLLECTING_ADDREF(URLSearc
 NS_IMPL_CYCLE_COLLECTING_RELEASE(URLSearchParams)
 
 NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(URLSearchParams)
   NS_WRAPPERCACHE_INTERFACE_MAP_ENTRY
   NS_INTERFACE_MAP_ENTRY(nsISupports)
 NS_INTERFACE_MAP_END
 
 
-URLSearchParams::URLSearchParams(URLSearchParamsObserver* aObserver)
+URLSearchParams::URLSearchParams(nsISupports* aParent,
+                                 URLSearchParamsObserver* aObserver)
 {
 }
 
 URLSearchParams::~URLSearchParams()
 {
 }
 
 JSObject*