PlatformObject.h 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. /*
  2. * Copyright (c) 2022, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #pragma once
  7. #include <AK/Weakable.h>
  8. #include <LibJS/Heap/GCPtr.h>
  9. #include <LibJS/Runtime/Object.h>
  10. #include <LibWeb/Forward.h>
  11. namespace Web::Bindings {
  12. #define WEB_PLATFORM_OBJECT(class_, base_class) \
  13. JS_OBJECT(class_, base_class) \
  14. virtual bool implements_interface(DeprecatedString const& interface) const override \
  15. { \
  16. if (interface == #class_) \
  17. return true; \
  18. return Base::implements_interface(interface); \
  19. }
  20. // https://webidl.spec.whatwg.org/#dfn-platform-object
  21. class PlatformObject
  22. : public JS::Object
  23. , public Weakable<PlatformObject> {
  24. JS_OBJECT(PlatformObject, JS::Object);
  25. public:
  26. virtual ~PlatformObject() override;
  27. JS::Realm& realm() const;
  28. // FIXME: This should return a type that works in both window and worker contexts.
  29. HTML::Window& global_object() const;
  30. // https://webidl.spec.whatwg.org/#implements
  31. // This is implemented by overrides that get generated by the WEB_PLATFORM_OBJECT macro.
  32. [[nodiscard]] virtual bool implements_interface(DeprecatedString const&) const { return false; }
  33. protected:
  34. explicit PlatformObject(JS::Realm&);
  35. explicit PlatformObject(JS::Object& prototype);
  36. };
  37. }