CloseWatcher.h (2466B)
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 mozilla_dom_CloseWatcher_h 8 #define mozilla_dom_CloseWatcher_h 9 10 #include "mozilla/DOMEventTargetHelper.h" 11 #include "mozilla/dom/CloseWatcherBinding.h" 12 #include "mozilla/dom/DebuggerNotificationBinding.h" 13 14 namespace mozilla::dom { 15 16 class CloseWatcher : public DOMEventTargetHelper, public AbortFollower { 17 public: 18 NS_DECL_ISUPPORTS_INHERITED 19 20 nsIGlobalObject* GetParentObject() const { return GetOwnerGlobal(); } 21 22 mozilla::Maybe<EventCallbackDebuggerNotificationType> 23 GetDebuggerNotificationType() const override { 24 return mozilla::Some(EventCallbackDebuggerNotificationType::Closewatcher); 25 } 26 27 JSObject* WrapObject(JSContext* aCx, 28 JS::Handle<JSObject*> aGivenProto) override; 29 30 IMPL_EVENT_HANDLER(cancel); 31 IMPL_EVENT_HANDLER(close); 32 33 static already_AddRefed<CloseWatcher> Constructor( 34 const GlobalObject& aGlobal, const CloseWatcherOptions& aOptions, 35 ErrorResult& aRv); 36 37 explicit CloseWatcher(nsPIDOMWindowInner* aWindow) 38 : DOMEventTargetHelper(aWindow) {} 39 40 // The IDL binding for RequestClose returns void so that the history 41 // consumption is not observable. 42 MOZ_CAN_RUN_SCRIPT void RequestClose() { RequestToClose(false); } 43 44 // RequestToClose returns a boolean so callers can determine if the action 45 // was handled, so that other fallback behaviours can be executed. 46 MOZ_CAN_RUN_SCRIPT bool RequestToClose(bool aRequireHistoryActionActivation); 47 48 MOZ_CAN_RUN_SCRIPT void Close(); 49 50 void AddToWindowsCloseWatcherManager(); 51 52 void Destroy(); 53 54 // AbortFollower 55 void RunAbortAlgorithm() override; 56 57 bool IsActive() const; 58 59 void SetEnabled(bool aEnabled) { mEnabled = aEnabled; } 60 61 void DisconnectFromOwner() override { 62 Destroy(); 63 DOMEventTargetHelper::DisconnectFromOwner(); 64 } 65 66 protected: 67 virtual ~CloseWatcher() = default; 68 69 bool mIsRunningCancelAction = false; 70 71 // https://html.spec.whatwg.org/multipage/interaction.html#create-close-watcher-getenabledstate 72 // HTMLDialogElement can enable/disable close watcher using ClosedBy=none 73 bool mEnabled = true; 74 }; 75 76 } // namespace mozilla::dom 77 78 #endif // mozilla_dom_CloseWatcher_h