/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ /* vim: set ts=8 sts=2 et sw=2 tw=80: */ /* This Source Code Form is subject to the terms of the Mozilla Public * License, v. 2.0. If a copy of the MPL was not distributed with this * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ #include "mozilla/dom/CustomElementRegistry.h" #include "mozilla/AsyncEventDispatcher.h" #include "mozilla/CycleCollectedJSContext.h" #include "mozilla/dom/CustomElementRegistryBinding.h" #include "mozilla/dom/HTMLElementBinding.h" #include "mozilla/dom/XULElementBinding.h" #include "mozilla/dom/Promise.h" #include "mozilla/dom/WebComponentsBinding.h" #include "mozilla/dom/DocGroup.h" #include "mozilla/dom/CustomEvent.h" #include "mozilla/dom/ShadowRoot.h" #include "nsHTMLTags.h" #include "jsapi.h" #include "xpcprivate.h" #include "nsGlobalWindow.h" namespace mozilla { namespace dom { //----------------------------------------------------- // CustomElementUpgradeReaction class CustomElementUpgradeReaction final : public CustomElementReaction { public: explicit CustomElementUpgradeReaction(CustomElementDefinition* aDefinition) : mDefinition(aDefinition) { mIsUpgradeReaction = true; } virtual void Traverse(nsCycleCollectionTraversalCallback& aCb) const override { NS_CYCLE_COLLECTION_NOTE_EDGE_NAME(aCb, "mDefinition"); aCb.NoteNativeChild(mDefinition, NS_CYCLE_COLLECTION_PARTICIPANT(CustomElementDefinition)); } private: virtual void Invoke(Element* aElement, ErrorResult& aRv) override { CustomElementRegistry::Upgrade(aElement, mDefinition, aRv); } RefPtr mDefinition; }; //----------------------------------------------------- // CustomElementCallbackReaction class CustomElementCallbackReaction final : public CustomElementReaction { public: explicit CustomElementCallbackReaction(UniquePtr aCustomElementCallback) : mCustomElementCallback(std::move(aCustomElementCallback)) { } virtual void Traverse(nsCycleCollectionTraversalCallback& aCb) const override { mCustomElementCallback->Traverse(aCb); } private: virtual void Invoke(Element* aElement, ErrorResult& aRv) override { mCustomElementCallback->Call(); } UniquePtr mCustomElementCallback; }; //----------------------------------------------------- // CustomElementCallback void CustomElementCallback::Call() { switch (mType) { case nsIDocument::eConnected: static_cast(mCallback.get())->Call(mThisObject); break; case nsIDocument::eDisconnected: static_cast(mCallback.get())->Call(mThisObject); break; case nsIDocument::eAdopted: static_cast(mCallback.get())->Call(mThisObject, mAdoptedCallbackArgs.mOldDocument, mAdoptedCallbackArgs.mNewDocument); break; case nsIDocument::eAttributeChanged: static_cast(mCallback.get())->Call(mThisObject, mArgs.name, mArgs.oldValue, mArgs.newValue, mArgs.namespaceURI); break; case nsIDocument::eGetCustomInterface: MOZ_ASSERT_UNREACHABLE("Don't call GetCustomInterface through callback"); break; } } void CustomElementCallback::Traverse(nsCycleCollectionTraversalCallback& aCb) const { NS_CYCLE_COLLECTION_NOTE_EDGE_NAME(aCb, "mThisObject"); aCb.NoteXPCOMChild(mThisObject); NS_CYCLE_COLLECTION_NOTE_EDGE_NAME(aCb, "mCallback"); aCb.NoteXPCOMChild(mCallback); } CustomElementCallback::CustomElementCallback(Element* aThisObject, nsIDocument::ElementCallbackType aCallbackType, mozilla::dom::CallbackFunction* aCallback) : mThisObject(aThisObject), mCallback(aCallback), mType(aCallbackType) { } //----------------------------------------------------- // CustomElementConstructor already_AddRefed CustomElementConstructor::Construct(const char* aExecutionReason, ErrorResult& aRv) { CallSetup s(this, aRv, aExecutionReason, CallbackFunction::eRethrowExceptions); JSContext* cx = s.GetContext(); if (!cx) { MOZ_ASSERT(aRv.Failed()); return nullptr; } JS::Rooted result(cx); JS::Rooted constructor(cx, JS::ObjectValue(*mCallback)); if (!JS::Construct(cx, constructor, JS::HandleValueArray::empty(), &result)) { aRv.NoteJSContextException(cx); return nullptr; } RefPtr element; if (NS_FAILED(UNWRAP_OBJECT(Element, &result, element))) { return nullptr; } return element.forget(); } //----------------------------------------------------- // CustomElementData CustomElementData::CustomElementData(nsAtom* aType) : CustomElementData(aType, CustomElementData::State::eUndefined) { } CustomElementData::CustomElementData(nsAtom* aType, State aState) : mState(aState) , mType(aType) { } void CustomElementData::SetCustomElementDefinition(CustomElementDefinition* aDefinition) { MOZ_ASSERT(mState == State::eCustom); MOZ_ASSERT(!mCustomElementDefinition); MOZ_ASSERT(aDefinition->mType == mType); mCustomElementDefinition = aDefinition; } CustomElementDefinition* CustomElementData::GetCustomElementDefinition() { MOZ_ASSERT(mCustomElementDefinition ? mState == State::eCustom : mState != State::eCustom); return mCustomElementDefinition; } nsAtom* CustomElementData::GetCustomElementType() { return mType; } void CustomElementData::Traverse(nsCycleCollectionTraversalCallback& aCb) const { for (uint32_t i = 0; i < mReactionQueue.Length(); i++) { if (mReactionQueue[i]) { mReactionQueue[i]->Traverse(aCb); } } if (mCustomElementDefinition) { NS_CYCLE_COLLECTION_NOTE_EDGE_NAME(aCb, "mCustomElementDefinition"); aCb.NoteNativeChild(mCustomElementDefinition, NS_CYCLE_COLLECTION_PARTICIPANT(CustomElementDefinition)); } } void CustomElementData::Unlink() { mReactionQueue.Clear(); mCustomElementDefinition = nullptr; } //----------------------------------------------------- // CustomElementRegistry namespace { class MOZ_RAII AutoConstructionStackEntry final { public: AutoConstructionStackEntry(nsTArray>& aStack, Element* aElement) : mStack(aStack) { MOZ_ASSERT(aElement->IsHTMLElement() || aElement->IsXULElement()); mIndex = mStack.Length(); mStack.AppendElement(aElement); } ~AutoConstructionStackEntry() { MOZ_ASSERT(mIndex == mStack.Length() - 1, "Removed element should be the last element"); mStack.RemoveElementAt(mIndex); } private: nsTArray>& mStack; uint32_t mIndex; }; } // namespace anonymous // Only needed for refcounted objects. NS_IMPL_CYCLE_COLLECTION_CLASS(CustomElementRegistry) NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN(CustomElementRegistry) tmp->mConstructors.clear(); NS_IMPL_CYCLE_COLLECTION_UNLINK(mCustomDefinitions) NS_IMPL_CYCLE_COLLECTION_UNLINK(mWhenDefinedPromiseMap) NS_IMPL_CYCLE_COLLECTION_UNLINK(mElementCreationCallbacks) NS_IMPL_CYCLE_COLLECTION_UNLINK(mWindow) NS_IMPL_CYCLE_COLLECTION_UNLINK_PRESERVED_WRAPPER NS_IMPL_CYCLE_COLLECTION_UNLINK_END NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN(CustomElementRegistry) NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mCustomDefinitions) NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mWhenDefinedPromiseMap) NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mElementCreationCallbacks) NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mWindow) NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END NS_IMPL_CYCLE_COLLECTION_TRACE_BEGIN(CustomElementRegistry) for (auto iter = tmp->mConstructors.iter(); !iter.done(); iter.next()) { aCallbacks.Trace(&iter.get().mutableKey(), "mConstructors key", aClosure); } NS_IMPL_CYCLE_COLLECTION_TRACE_PRESERVED_WRAPPER NS_IMPL_CYCLE_COLLECTION_TRACE_END NS_IMPL_CYCLE_COLLECTING_ADDREF(CustomElementRegistry) NS_IMPL_CYCLE_COLLECTING_RELEASE(CustomElementRegistry) NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(CustomElementRegistry) NS_WRAPPERCACHE_INTERFACE_MAP_ENTRY NS_INTERFACE_MAP_ENTRY(nsISupports) NS_INTERFACE_MAP_END CustomElementRegistry::CustomElementRegistry(nsPIDOMWindowInner* aWindow) : mWindow(aWindow) , mIsCustomDefinitionRunning(false) { MOZ_ASSERT(aWindow); mozilla::HoldJSObjects(this); } CustomElementRegistry::~CustomElementRegistry() { mozilla::DropJSObjects(this); } bool CustomElementRegistry::IsCustomElementEnabled(JSContext* aCx, JSObject* aObject) { if (nsContentUtils::IsCustomElementsEnabled()) { return true; } if (!XRE_IsParentProcess()) { return false; } nsIPrincipal* principal = nsContentUtils::ObjectPrincipal(js::UncheckedUnwrap(aObject)); return nsContentUtils::AllowXULXBLForPrincipal(principal); } bool CustomElementRegistry::IsCustomElementEnabled(nsIDocument* aDoc) { if (nsContentUtils::IsCustomElementsEnabled()) { return true; } return XRE_IsParentProcess() && aDoc->AllowXULXBL(); } NS_IMETHODIMP CustomElementRegistry::RunCustomElementCreationCallback::Run() { ErrorResult er; nsDependentAtomString value(mAtom); mCallback->Call(value, er); MOZ_ASSERT(NS_SUCCEEDED(er.StealNSResult()), "chrome JavaScript error in the callback."); CustomElementDefinition* definition = mRegistry->mCustomDefinitions.GetWeak(mAtom); MOZ_ASSERT(definition, "Callback should define the definition of type."); MOZ_ASSERT(!mRegistry->mElementCreationCallbacks.GetWeak(mAtom), "Callback should be removed."); nsAutoPtr>> elements; mRegistry->mElementCreationCallbacksUpgradeCandidatesMap.Remove(mAtom, &elements); MOZ_ASSERT(elements, "There should be a list"); for (auto iter = elements->Iter(); !iter.Done(); iter.Next()) { nsCOMPtr elem = do_QueryReferent(iter.Get()->GetKey()); if (!elem) { continue; } CustomElementRegistry::Upgrade(elem, definition, er); MOZ_ASSERT(NS_SUCCEEDED(er.StealNSResult()), "chrome JavaScript error in custom element construction."); } return NS_OK; } CustomElementDefinition* CustomElementRegistry::LookupCustomElementDefinition(nsAtom* aNameAtom, int32_t aNameSpaceID, nsAtom* aTypeAtom) { CustomElementDefinition* data = mCustomDefinitions.GetWeak(aTypeAtom); if (!data) { RefPtr callback; mElementCreationCallbacks.Get(aTypeAtom, getter_AddRefs(callback)); if (callback) { mElementCreationCallbacks.Remove(aTypeAtom); mElementCreationCallbacksUpgradeCandidatesMap.LookupOrAdd(aTypeAtom); RefPtr runnable = new RunCustomElementCreationCallback(this, aTypeAtom, callback); nsContentUtils::AddScriptRunner(runnable); data = mCustomDefinitions.GetWeak(aTypeAtom); } } if (data && data->mLocalName == aNameAtom && data->mNamespaceID == aNameSpaceID) { return data; } return nullptr; } CustomElementDefinition* CustomElementRegistry::LookupCustomElementDefinition(JSContext* aCx, JSObject* aConstructor) const { JS::Rooted constructor(aCx, js::CheckedUnwrap(aConstructor)); const auto& ptr = mConstructors.lookup(constructor); if (!ptr) { return nullptr; } CustomElementDefinition* definition = mCustomDefinitions.GetWeak(ptr->value()); MOZ_ASSERT(definition, "Definition must be found in mCustomDefinitions"); return definition; } void CustomElementRegistry::RegisterUnresolvedElement(Element* aElement, nsAtom* aTypeName) { // We don't have a use-case for a Custom Element inside NAC, and continuing // here causes performance issues for NAC + XBL anonymous content. if (aElement->IsInNativeAnonymousSubtree()) { return; } mozilla::dom::NodeInfo* info = aElement->NodeInfo(); // Candidate may be a custom element through extension, // in which case the custom element type name will not // match the element tag name. e.g.