Skip to content

Truly independent web browser

4.6 KB
Raw Blame
/*
 * Copyright (c) 2020-2022, Andreas Kling <[email protected]>
 *
 * SPDX-License-Identifier: BSD-2-Clause
 */

#pragma once

#include <AK/Noncopyable.h>
#include <AK/Utf16FlyString.h>
#include <AK/Vector.h>
#include <LibGC/RootVector.h>
#include <LibJS/Forward.h>
#include <LibWeb/Bindings/Wrappable.h>
#include <LibWeb/DOM/DOMEventListener.h>
#include <LibWeb/Export.h>
#include <LibWeb/Forward.h>
#include <LibWeb/HTML/EventHandler.h>
#include <LibWeb/WebIDL/ExceptionOr.h>

namespace Web::DOM {

class WEB_API EventTarget : public Bindings::GCAllocatedWrappable {
    WEB_WRAPPABLE(EventTarget, Bindings::GCAllocatedWrappable);
    GC_DECLARE_ALLOCATOR(EventTarget);

public:
    virtual ~EventTarget() override;

    static GC::Ref<EventTarget> create();

    virtual bool is_focusable() const { return false; }

    struct EventListenerOptions {
        bool capture { false };
    };

    struct AddEventListenerOptions {
        bool capture { false };
        Optional<bool> passive;
        bool once { false };
        GC::Ptr<AbortSignal> signal;
    };

    void add_event_listener(FlyString const& type, GC::Ptr<IDLEventListener> callback, AddEventListenerOptions const& options);
    void add_event_listener(Utf16FlyString const& type, GC::Ptr<IDLEventListener> callback, AddEventListenerOptions const& options);
    void remove_event_listener(FlyString const& type, GC::Ptr<IDLEventListener> callback, EventListenerOptions const& options);
    void remove_event_listener(Utf16FlyString const& type, GC::Ptr<IDLEventListener> callback, EventListenerOptions const& options);

    // NOTE: These are for internal use only. They operate as though addEventListener(type, callback) was called instead of addEventListener(type, callback, options).
    void add_event_listener_without_options(Utf16FlyString const& type, IDLEventListener& callback);
    void remove_event_listener_without_options(Utf16FlyString const& type, IDLEventListener& callback);

    virtual bool dispatch_event(Event&);
    WebIDL::ExceptionOr<bool> dispatch_event_for_bindings(Event&);

    virtual EventTarget* get_parent(Event const&) { return nullptr; }

    void add_an_event_listener(DOMEventListener&);
    void remove_an_event_listener(DOMEventListener&);
    void remove_all_event_listeners();
    void erase_all_event_listeners_and_handlers();

    // A dispatch clones only the listeners it can run, and rarely more than a couple, so keep them inline.
    using ClonedEventListeners = GC::RootVector<GC::Ref<DOMEventListener>, 4>;
    ClonedEventListeners event_listener_list_matching(Utf16FlyString const& type, Optional<Utf16FlyString> const& legacy_type) const;

    virtual bool has_activation_behavior() const;
    virtual void activation_behavior(Event const&);

    // NOTE: These only exist for checkbox and radio input elements.
    virtual void legacy_pre_activation_behavior() { }
    virtual void legacy_cancelled_activation_behavior() { }
    virtual void legacy_cancelled_activation_behavior_was_not_called() { }

    WebIDL::CallbackType* event_handler_attribute(Utf16FlyString const& name);
    void set_event_handler_attribute(Utf16FlyString const& name, GC::Ptr<WebIDL::CallbackType>);

    bool has_event_listener(Utf16FlyString const& type) const;
    bool has_blocking_wheel_event_listener() const;
    bool has_blocking_keyboard_event_listener() const;
    bool has_event_listeners() const;

protected:
    EventTarget();

    void element_event_handler_attribute_changed(Utf16FlyString const& local_name, Optional<Utf16String> const& value);
    virtual void event_listener_list_changed() { }

    virtual void visit_edges(Cell::Visitor&) override;
    virtual size_t external_memory_size() const override;

private:
    struct Data {
        AK_ALLOC_WITH_KMALLOC;

        Vector<GC::Ref<DOMEventListener>> event_listener_list;

        // https://html.spec.whatwg.org/multipage/webappapis.html#event-handler-map
        // Spec Note: The order of the entries of event handler map could be arbitrary. It is not observable through any algorithms that operate on the map.
        HashMap<Utf16FlyString, GC::Ref<HTML::EventHandler>> event_handler_map;
    };

    Data& ensure_data();
    OwnPtr<Data> m_data;

    WebIDL::CallbackType* get_current_value_of_event_handler(Utf16FlyString const& name);
    void activate_event_handler(Utf16FlyString const& name, HTML::EventHandler& event_handler);
    void deactivate_event_handler(Utf16FlyString const& name);
    JS::ThrowCompletionOr<void> process_event_handler_for_event(Utf16FlyString const& name, Event& event);
};

bool is_window_reflecting_body_element_event_handler(Utf16FlyString const& name);

}