Window.h 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. /*
  2. * Copyright (c) 2020-2021, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #pragma once
  7. #include <AK/Badge.h>
  8. #include <AK/IDAllocator.h>
  9. #include <AK/RefCounted.h>
  10. #include <AK/RefPtr.h>
  11. #include <LibWeb/Bindings/WindowObject.h>
  12. #include <LibWeb/Bindings/Wrappable.h>
  13. #include <LibWeb/CSS/MediaQueryList.h>
  14. #include <LibWeb/CSS/Screen.h>
  15. #include <LibWeb/DOM/Event.h>
  16. #include <LibWeb/DOM/EventTarget.h>
  17. namespace Web::DOM {
  18. class RequestAnimationFrameCallback;
  19. class Window final
  20. : public RefCounted<Window>
  21. , public EventTarget {
  22. public:
  23. static NonnullRefPtr<Window> create_with_document(Document&);
  24. ~Window();
  25. using RefCounted::ref;
  26. using RefCounted::unref;
  27. virtual void ref_event_target() override { RefCounted::ref(); }
  28. virtual void unref_event_target() override { RefCounted::unref(); }
  29. virtual bool dispatch_event(NonnullRefPtr<Event>) override;
  30. virtual JS::Object* create_wrapper(JS::GlobalObject&) override;
  31. Page* page();
  32. Page const* page() const;
  33. Document const& associated_document() const { return *m_associated_document; }
  34. Document& associated_document() { return *m_associated_document; }
  35. void alert(String const&);
  36. bool confirm(String const&);
  37. String prompt(String const&, String const&);
  38. i32 request_animation_frame(JS::FunctionObject&);
  39. void cancel_animation_frame(i32);
  40. i32 set_timeout(JS::FunctionObject&, i32);
  41. i32 set_interval(JS::FunctionObject&, i32);
  42. void clear_timeout(i32);
  43. void clear_interval(i32);
  44. int inner_width() const;
  45. int inner_height() const;
  46. void did_set_location_href(Badge<Bindings::LocationObject>, AK::URL const& new_href);
  47. void did_call_location_reload(Badge<Bindings::LocationObject>);
  48. Bindings::WindowObject* wrapper() { return m_wrapper; }
  49. Bindings::WindowObject const* wrapper() const { return m_wrapper; }
  50. void set_wrapper(Badge<Bindings::WindowObject>, Bindings::WindowObject&);
  51. i32 allocate_timer_id(Badge<Timer>);
  52. void deallocate_timer_id(Badge<Timer>, i32);
  53. void timer_did_fire(Badge<Timer>, Timer&);
  54. HighResolutionTime::Performance& performance() { return *m_performance; }
  55. CSS::Screen& screen() { return *m_screen; }
  56. Event const* current_event() const { return m_current_event; }
  57. void set_current_event(Event* event) { m_current_event = event; }
  58. NonnullRefPtr<CSS::CSSStyleDeclaration> get_computed_style(DOM::Element&) const;
  59. NonnullRefPtr<CSS::MediaQueryList> match_media(String);
  60. private:
  61. explicit Window(Document&);
  62. // https://html.spec.whatwg.org/multipage/window-object.html#concept-document-window
  63. WeakPtr<Document> m_associated_document;
  64. WeakPtr<Bindings::WindowObject> m_wrapper;
  65. IDAllocator m_timer_id_allocator;
  66. HashMap<int, NonnullRefPtr<Timer>> m_timers;
  67. NonnullOwnPtr<HighResolutionTime::Performance> m_performance;
  68. NonnullRefPtr<CSS::Screen> m_screen;
  69. RefPtr<Event> m_current_event;
  70. HashMap<i32, NonnullRefPtr<RequestAnimationFrameCallback>> m_request_animation_frame_callbacks;
  71. };
  72. }