WindowObject.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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. JS_OBJECT(WindowObject, JS::GlobalObject);
  37. public:
  38. explicit WindowObject(DOM::Window&);
  39. virtual void initialize_global_object() override;
  40. virtual ~WindowObject() override;
  41. DOM::Window& impl() { return *m_impl; }
  42. const DOM::Window& impl() const { return *m_impl; }
  43. Origin origin() const;
  44. JS::Object* web_prototype(const String& class_name) { return m_prototypes.get(class_name).value_or(nullptr); }
  45. JS::NativeFunction* web_constructor(const String& class_name) { return m_constructors.get(class_name).value_or(nullptr); }
  46. template<typename T>
  47. JS::Object& ensure_web_prototype(const String& class_name)
  48. {
  49. auto it = m_prototypes.find(class_name);
  50. if (it != m_prototypes.end())
  51. return *it->value;
  52. auto* prototype = heap().allocate<T>(*this, *this);
  53. m_prototypes.set(class_name, prototype);
  54. return *prototype;
  55. }
  56. template<typename T>
  57. JS::NativeFunction& ensure_web_constructor(const String& class_name)
  58. {
  59. auto it = m_constructors.find(class_name);
  60. if (it != m_constructors.end())
  61. return *it->value;
  62. auto* constructor = heap().allocate<T>(*this, *this);
  63. m_constructors.set(class_name, constructor);
  64. define_property(class_name, JS::Value(constructor), JS::Attribute::Writable | JS::Attribute::Configurable);
  65. return *constructor;
  66. }
  67. private:
  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_GETTER(inner_width_getter);
  74. JS_DECLARE_NATIVE_GETTER(inner_height_getter);
  75. JS_DECLARE_NATIVE_FUNCTION(alert);
  76. JS_DECLARE_NATIVE_FUNCTION(confirm);
  77. JS_DECLARE_NATIVE_FUNCTION(prompt);
  78. JS_DECLARE_NATIVE_FUNCTION(set_interval);
  79. JS_DECLARE_NATIVE_FUNCTION(set_timeout);
  80. JS_DECLARE_NATIVE_FUNCTION(clear_interval);
  81. JS_DECLARE_NATIVE_FUNCTION(clear_timeout);
  82. JS_DECLARE_NATIVE_FUNCTION(request_animation_frame);
  83. JS_DECLARE_NATIVE_FUNCTION(cancel_animation_frame);
  84. JS_DECLARE_NATIVE_FUNCTION(atob);
  85. JS_DECLARE_NATIVE_FUNCTION(btoa);
  86. NonnullRefPtr<DOM::Window> m_impl;
  87. HashMap<String, JS::Object*> m_prototypes;
  88. HashMap<String, JS::NativeFunction*> m_constructors;
  89. };
  90. }
  91. }