Bug 1453869 part 16. Add a way to create a DOMParser directly from C++. r=mrbkap
authorBoris Zbarsky <bzbarsky@mit.edu>
Fri, 20 Apr 2018 23:04:46 -0400
changeset 414874 757c523ca40f0d55e764e1c4f0685b3db3816c0d
parent 414873 3c6dbba979108358ff053420ecb42e35ba37d339
child 414875 54cd958c5398e463a5a799526111c6abb7fc94af
push id102429
push userbzbarsky@mozilla.com
push dateSat, 21 Apr 2018 03:06:41 +0000
treeherdermozilla-inbound@54cd958c5398 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersmrbkap
bugs1453869
milestone61.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 1453869 part 16. Add a way to create a DOMParser directly from C++. r=mrbkap Thunderbird uses DOMParser from C++ for now. They should ideally migrate that into JS, but we can give them something that works for the moment. MozReview-Commit-ID: C4D6QuFdbn8
dom/base/DOMParser.cpp
dom/base/DOMParser.h
--- a/dom/base/DOMParser.cpp
+++ b/dom/base/DOMParser.cpp
@@ -31,17 +31,16 @@ using namespace mozilla::dom;
 DOMParser::DOMParser(nsIGlobalObject* aOwner, nsIPrincipal* aDocPrincipal,
                      nsIURI* aDocumentURI, nsIURI* aBaseURI)
   : mOwner(aOwner)
   , mPrincipal(aDocPrincipal)
   , mDocumentURI(aDocumentURI)
   , mBaseURI(aBaseURI)
   , mForceEnableXULXBL(false)
 {
-  MOZ_ASSERT(aOwner);
   MOZ_ASSERT(aDocPrincipal);
   MOZ_ASSERT(aDocumentURI);
 }
 
 DOMParser::~DOMParser()
 {
 }
 
@@ -266,21 +265,41 @@ DOMParser::Constructor(const GlobalObjec
   }
 
   if (!documentURI) {
     rv.Throw(NS_ERROR_UNEXPECTED);
     return nullptr;
   }
 
   nsCOMPtr<nsIGlobalObject> global = do_QueryInterface(aOwner.GetAsSupports());
+  MOZ_ASSERT(global);
   RefPtr<DOMParser> domParser = new DOMParser(global, docPrincipal,
                                               documentURI, baseURI);
   return domParser.forget();
 }
 
+// static
+already_AddRefed<DOMParser>
+DOMParser::CreateWithoutGlobal(ErrorResult& aRv)
+{
+  nsCOMPtr<nsIPrincipal> docPrincipal =
+    NullPrincipal::CreateWithoutOriginAttributes();
+  nsCOMPtr<nsIURI> documentURI;
+  docPrincipal->GetURI(getter_AddRefs(documentURI));
+
+  if (!documentURI) {
+    aRv.Throw(NS_ERROR_UNEXPECTED);
+    return nullptr;
+  }
+
+  RefPtr<DOMParser> domParser = new DOMParser(nullptr, docPrincipal,
+                                              documentURI, nullptr);
+  return domParser.forget();
+}
+
 already_AddRefed<nsIDocument>
 DOMParser::SetUpDocument(DocumentFlavor aFlavor, ErrorResult& aRv)
 {
   // We should really just use mOwner here, but nsDocument gets confused
   // if we pass it a scriptHandlingObject that doesn't QI to
   // nsIScriptGlobalObject, and test_isequalnode.js (an xpcshell test without
   // a window global) breaks. The correct solution is just to wean nsDocument
   // off of nsIScriptGlobalObject, but that's a yak to shave another day.
--- a/dom/base/DOMParser.h
+++ b/dom/base/DOMParser.h
@@ -67,16 +67,19 @@ public:
     return mOwner;
   }
 
   virtual JSObject* WrapObject(JSContext* aCx, JS::Handle<JSObject*> aGivenProto) override
   {
     return mozilla::dom::DOMParserBinding::Wrap(aCx, this, aGivenProto);
   }
 
+  // A way to create a non-global-associated DOMParser from C++.
+  static already_AddRefed<DOMParser> CreateWithoutGlobal(ErrorResult& aRv);
+
 private:
   DOMParser(nsIGlobalObject* aOwner, nsIPrincipal* aDocPrincipal,
             nsIURI* aDocumentURI, nsIURI* aBaseURI);
 
   already_AddRefed<nsIDocument> SetUpDocument(DocumentFlavor aFlavor,
                                               ErrorResult& aRv);
 
   nsCOMPtr<nsIGlobalObject> mOwner;