WindowObject.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. /*
  2. * Copyright (c) 2020-2021, 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/TypeCasts.h>
  28. #include <AK/Weakable.h>
  29. #include <LibJS/Runtime/GlobalObject.h>
  30. #include <LibWeb/Forward.h>
  31. namespace Web {
  32. namespace Bindings {
  33. class WindowObject final
  34. : public JS::GlobalObject
  35. , public Weakable<WindowObject> {
  36. public:
  37. explicit WindowObject(DOM::Window&);
  38. virtual void initialize() override;
  39. virtual ~WindowObject() override;
  40. DOM::Window& impl() { return *m_impl; }
  41. const DOM::Window& impl() const { return *m_impl; }
  42. Origin origin() const;
  43. JS::Object* web_prototype(const String& class_name) { return m_prototypes.get(class_name).value_or(nullptr); }
  44. JS::NativeFunction* web_constructor(const String& class_name) { return m_constructors.get(class_name).value_or(nullptr); }
  45. template<typename T>
  46. JS::Object& ensure_web_prototype(const String& class_name)
  47. {
  48. auto it = m_prototypes.find(class_name);
  49. if (it != m_prototypes.end())
  50. return *it->value;
  51. auto* prototype = heap().allocate<T>(*this, *this);
  52. m_prototypes.set(class_name, prototype);
  53. return *prototype;
  54. }
  55. template<typename T>
  56. JS::NativeFunction& ensure_web_constructor(const String& class_name)
  57. {
  58. auto it = m_constructors.find(class_name);
  59. if (it != m_constructors.end())
  60. return *it->value;
  61. auto* constructor = heap().allocate<T>(*this, *this);
  62. m_constructors.set(class_name, constructor);
  63. define_property(class_name, JS::Value(constructor), JS::Attribute::Writable | JS::Attribute::Configurable);
  64. return *constructor;
  65. }
  66. private:
  67. virtual const char* class_name() const override { return "WindowObject"; }
  68. virtual void visit_edges(Visitor&) override;
  69. JS_DECLARE_NATIVE_GETTER(document_getter);
  70. JS_DECLARE_NATIVE_SETTER(document_setter);
  71. JS_DECLARE_NATIVE_GETTER(performance_getter);
  72. JS_DECLARE_NATIVE_GETTER(event_getter);
  73. JS_DECLARE_NATIVE_FUNCTION(alert);
  74. JS_DECLARE_NATIVE_FUNCTION(confirm);
  75. JS_DECLARE_NATIVE_FUNCTION(set_interval);
  76. JS_DECLARE_NATIVE_FUNCTION(set_timeout);
  77. JS_DECLARE_NATIVE_FUNCTION(clear_interval);
  78. JS_DECLARE_NATIVE_FUNCTION(clear_timeout);
  79. JS_DECLARE_NATIVE_FUNCTION(request_animation_frame);
  80. JS_DECLARE_NATIVE_FUNCTION(cancel_animation_frame);
  81. JS_DECLARE_NATIVE_FUNCTION(atob);
  82. JS_DECLARE_NATIVE_FUNCTION(btoa);
  83. NonnullRefPtr<DOM::Window> m_impl;
  84. HashMap<String, JS::Object*> m_prototypes;
  85. HashMap<String, JS::NativeFunction*> m_constructors;
  86. };
  87. }
  88. }