HTMLDialogElement.h (4987B)
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */ 3 /* This Source Code Form is subject to the terms of the Mozilla Public 4 * License, v. 2.0. If a copy of the MPL was not distributed with this 5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 6 7 #ifndef HTMLDialogElement_h 8 #define HTMLDialogElement_h 9 10 #include "mozilla/AsyncEventDispatcher.h" 11 #include "mozilla/Attributes.h" 12 #include "mozilla/dom/CloseWatcher.h" 13 #include "nsGenericHTMLElement.h" 14 #include "nsGkAtoms.h" 15 16 namespace mozilla::dom { 17 18 class HTMLDialogElement final : public nsGenericHTMLElement { 19 public: 20 using Element::Command; 21 22 enum class ClosedBy : uint8_t { 23 Auto, 24 None, 25 Any, 26 CloseRequest, 27 }; 28 29 explicit HTMLDialogElement( 30 already_AddRefed<mozilla::dom::NodeInfo>&& aNodeInfo) 31 : nsGenericHTMLElement(std::move(aNodeInfo)), 32 mPreviouslyFocusedElement(nullptr), 33 mRequestCloseSourceElement(nullptr) {} 34 35 NS_IMPL_FROMNODE_HTML_WITH_TAG(HTMLDialogElement, dialog) 36 37 nsresult Clone(dom::NodeInfo* aNodeInfo, nsINode** aResult) const override; 38 39 ClosedBy GetClosedBy() const; 40 void GetClosedBy(nsAString& aValue) const; 41 void SetClosedBy(const nsAString& aClosedby, ErrorResult& aError) { 42 SetHTMLAttr(nsGkAtoms::closedby, aClosedby, aError); 43 } 44 bool ParseClosedByAttribute(const nsAString& aValue, nsAttrValue& aResult); 45 46 // nsIContent 47 bool ParseAttribute(int32_t aNamespaceID, nsAtom* aAttribute, 48 const nsAString& aValue, 49 nsIPrincipal* aMaybeScriptedPrincipal, 50 nsAttrValue& aResult) override; 51 52 bool Open() const; 53 void SetOpen(bool aOpen, ErrorResult& aError) { 54 SetHTMLBoolAttr(nsGkAtoms::open, aOpen, aError); 55 } 56 57 void GetReturnValue(nsAString& aReturnValue) { aReturnValue = mReturnValue; } 58 void SetReturnValue(const nsAString& aReturnValue) { 59 mReturnValue = aReturnValue; 60 } 61 62 void GetRequestCloseReturnValue(Optional<nsAString>& aReturnValue) { 63 if (mRequestCloseReturnValue.isSome()) { 64 aReturnValue = &mRequestCloseReturnValue.ref(); 65 } 66 } 67 void ClearRequestCloseReturnValue() { mRequestCloseReturnValue.reset(); } 68 void SetRequestCloseReturnValue(const nsAString& aReturnValue) { 69 mRequestCloseReturnValue.emplace(aReturnValue); 70 } 71 72 nsresult BindToTree(BindContext&, nsINode&) override; 73 void UnbindFromTree(UnbindContext&) override; 74 75 MOZ_CAN_RUN_SCRIPT_BOUNDARY void Close( 76 const mozilla::dom::Optional<nsAString>& aReturnValue) { 77 return Close(nullptr, aReturnValue); 78 } 79 MOZ_CAN_RUN_SCRIPT_BOUNDARY void Close( 80 Element* aSource, const mozilla::dom::Optional<nsAString>& aReturnValue); 81 MOZ_CAN_RUN_SCRIPT void RequestClose( 82 const mozilla::dom::Optional<nsAString>& aReturnValue) { 83 RequestClose(nullptr, aReturnValue); 84 } 85 MOZ_CAN_RUN_SCRIPT_BOUNDARY void RequestClose( 86 Element* aSource, const mozilla::dom::Optional<nsAString>& aReturnValue); 87 88 RefPtr<Element> GetRequestCloseSourceElement(); 89 90 MOZ_CAN_RUN_SCRIPT void Show(ErrorResult& aError); 91 92 MOZ_CAN_RUN_SCRIPT void ShowModal(Element* aSource, ErrorResult& aError); 93 MOZ_CAN_RUN_SCRIPT void ShowModal(ErrorResult& aError) { 94 return ShowModal(nullptr, aError); 95 } 96 97 void AfterSetAttr(int32_t aNameSpaceID, nsAtom* aName, 98 const nsAttrValue* aValue, const nsAttrValue* aOldValue, 99 nsIPrincipal* aMaybeScriptedPrincipal, 100 bool aNotify) override; 101 102 void AsyncEventRunning(AsyncEventDispatcher* aEvent) override; 103 104 bool IsInTopLayer() const; 105 void QueueCancelDialog(); 106 MOZ_CAN_RUN_SCRIPT void RunCancelDialogSteps(); 107 108 MOZ_CAN_RUN_SCRIPT_BOUNDARY void FocusDialog(); 109 110 int32_t TabIndexDefault() override; 111 112 bool IsValidCommandAction(Command aCommand) const override; 113 MOZ_CAN_RUN_SCRIPT bool HandleCommandInternal(Element* aSource, 114 Command aCommand, 115 ErrorResult& aRv) override; 116 117 Maybe<nsString> mRequestCloseReturnValue; 118 nsString mReturnValue; 119 120 protected: 121 virtual ~HTMLDialogElement(); 122 JSObject* WrapNode(JSContext* aCx, 123 JS::Handle<JSObject*> aGivenProto) override; 124 125 private: 126 void AddToTopLayerIfNeeded(); 127 void RemoveFromTopLayerIfNeeded(); 128 void StorePreviouslyFocusedElement(); 129 MOZ_CAN_RUN_SCRIPT_BOUNDARY void QueueToggleEventTask(Element* aSource); 130 void SetDialogCloseWatcherIfNeeded(); 131 void SetCloseWatcherEnabledState(); 132 133 void SetupSteps(); 134 void CleanupSteps(); 135 136 nsWeakPtr mPreviouslyFocusedElement; 137 138 nsWeakPtr mRequestCloseSourceElement; 139 140 RefPtr<AsyncEventDispatcher> mToggleEventDispatcher; 141 142 // This won't need to be cycle collected as CloseWatcher only has strong 143 // references to event listeners, which themselves have Weak References back 144 // to the Node. 145 RefPtr<CloseWatcher> mCloseWatcher; 146 }; 147 148 } // namespace mozilla::dom 149 150 #endif