EventTarget.h 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. /*
  2. * Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #pragma once
  27. #include <AK/FlyString.h>
  28. #include <AK/Function.h>
  29. #include <AK/Noncopyable.h>
  30. #include <AK/Vector.h>
  31. #include <LibJS/Forward.h>
  32. #include <LibWeb/Forward.h>
  33. namespace Web::DOM {
  34. class EventTarget {
  35. AK_MAKE_NONCOPYABLE(EventTarget);
  36. AK_MAKE_NONMOVABLE(EventTarget);
  37. public:
  38. virtual ~EventTarget();
  39. void ref() { ref_event_target(); }
  40. void unref() { unref_event_target(); }
  41. void add_event_listener(const FlyString& event_name, RefPtr<EventListener>);
  42. void remove_event_listener(const FlyString& event_name, RefPtr<EventListener>);
  43. void remove_from_event_listener_list(NonnullRefPtr<EventListener>);
  44. virtual bool dispatch_event(NonnullRefPtr<Event>) = 0;
  45. virtual JS::Object* create_wrapper(JS::GlobalObject&) = 0;
  46. Bindings::ScriptExecutionContext* script_execution_context() { return m_script_execution_context; }
  47. virtual EventTarget* get_parent(const Event&) { return nullptr; }
  48. struct EventListenerRegistration {
  49. FlyString event_name;
  50. NonnullRefPtr<EventListener> listener;
  51. };
  52. Vector<EventListenerRegistration>& listeners() { return m_listeners; }
  53. const Vector<EventListenerRegistration>& listeners() const { return m_listeners; }
  54. Function<void(const Event&)> activation_behaviour;
  55. // NOTE: These only exist for checkbox and radio input elements.
  56. Function<void()> legacy_pre_activation_behaviour;
  57. Function<void()> legacy_cancelled_activation_behaviour;
  58. protected:
  59. explicit EventTarget(Bindings::ScriptExecutionContext&);
  60. virtual void ref_event_target() = 0;
  61. virtual void unref_event_target() = 0;
  62. private:
  63. // FIXME: This should not be a raw pointer.
  64. Bindings::ScriptExecutionContext* m_script_execution_context { nullptr };
  65. Vector<EventListenerRegistration> m_listeners;
  66. };
  67. }