Window.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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/Badge.h>
  28. #include <AK/IDAllocator.h>
  29. #include <AK/RefCounted.h>
  30. #include <AK/RefPtr.h>
  31. #include <LibWeb/Bindings/WindowObject.h>
  32. #include <LibWeb/Bindings/Wrappable.h>
  33. #include <LibWeb/CSS/Screen.h>
  34. #include <LibWeb/DOM/Event.h>
  35. #include <LibWeb/DOM/EventTarget.h>
  36. namespace Web::DOM {
  37. class Window final
  38. : public RefCounted<Window>
  39. , public EventTarget {
  40. public:
  41. static NonnullRefPtr<Window> create_with_document(Document&);
  42. ~Window();
  43. using RefCounted::ref;
  44. using RefCounted::unref;
  45. virtual void ref_event_target() override { RefCounted::ref(); }
  46. virtual void unref_event_target() override { RefCounted::unref(); }
  47. virtual bool dispatch_event(NonnullRefPtr<Event>) override;
  48. virtual JS::Object* create_wrapper(JS::GlobalObject&) override;
  49. const Document& document() const { return m_document; }
  50. Document& document() { return m_document; }
  51. void alert(const String&);
  52. bool confirm(const String&);
  53. String prompt(const String&, const String&);
  54. i32 request_animation_frame(JS::Function&);
  55. void cancel_animation_frame(i32);
  56. i32 set_timeout(JS::Function&, i32);
  57. i32 set_interval(JS::Function&, i32);
  58. void clear_timeout(i32);
  59. void clear_interval(i32);
  60. int inner_width() const;
  61. int inner_height() const;
  62. void did_set_location_href(Badge<Bindings::LocationObject>, const URL& new_href);
  63. void did_call_location_reload(Badge<Bindings::LocationObject>);
  64. Bindings::WindowObject* wrapper() { return m_wrapper; }
  65. const Bindings::WindowObject* wrapper() const { return m_wrapper; }
  66. void set_wrapper(Badge<Bindings::WindowObject>, Bindings::WindowObject&);
  67. i32 allocate_timer_id(Badge<Timer>);
  68. void deallocate_timer_id(Badge<Timer>, i32);
  69. void timer_did_fire(Badge<Timer>, Timer&);
  70. HighResolutionTime::Performance& performance() { return *m_performance; }
  71. CSS::Screen& screen() { return *m_screen; }
  72. const Event* current_event() const { return m_current_event; }
  73. void set_current_event(Event* event) { m_current_event = event; }
  74. private:
  75. explicit Window(Document&);
  76. Document& m_document;
  77. WeakPtr<Bindings::WindowObject> m_wrapper;
  78. IDAllocator m_timer_id_allocator;
  79. HashMap<int, NonnullRefPtr<Timer>> m_timers;
  80. NonnullOwnPtr<HighResolutionTime::Performance> m_performance;
  81. NonnullRefPtr<CSS::Screen> m_screen;
  82. RefPtr<Event> m_current_event;
  83. };
  84. }