Window.h 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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 Window final
  19. : public RefCounted<Window>
  20. , public EventTarget {
  21. public:
  22. static NonnullRefPtr<Window> create_with_document(Document&);
  23. ~Window();
  24. using RefCounted::ref;
  25. using RefCounted::unref;
  26. virtual void ref_event_target() override { RefCounted::ref(); }
  27. virtual void unref_event_target() override { RefCounted::unref(); }
  28. virtual bool dispatch_event(NonnullRefPtr<Event>) override;
  29. virtual JS::Object* create_wrapper(JS::GlobalObject&) override;
  30. Page* page();
  31. Page const* page() const;
  32. Document const& associated_document() const { return *m_associated_document; }
  33. Document& associated_document() { return *m_associated_document; }
  34. void alert(String const&);
  35. bool confirm(String const&);
  36. String prompt(String const&, String const&);
  37. i32 request_animation_frame(JS::FunctionObject&);
  38. void cancel_animation_frame(i32);
  39. i32 set_timeout(JS::FunctionObject&, i32);
  40. i32 set_interval(JS::FunctionObject&, i32);
  41. void clear_timeout(i32);
  42. void clear_interval(i32);
  43. int inner_width() const;
  44. int inner_height() const;
  45. void did_set_location_href(Badge<Bindings::LocationObject>, URL const& new_href);
  46. void did_call_location_reload(Badge<Bindings::LocationObject>);
  47. Bindings::WindowObject* wrapper() { return m_wrapper; }
  48. Bindings::WindowObject const* wrapper() const { return m_wrapper; }
  49. void set_wrapper(Badge<Bindings::WindowObject>, Bindings::WindowObject&);
  50. i32 allocate_timer_id(Badge<Timer>);
  51. void deallocate_timer_id(Badge<Timer>, i32);
  52. void timer_did_fire(Badge<Timer>, Timer&);
  53. HighResolutionTime::Performance& performance() { return *m_performance; }
  54. CSS::Screen& screen() { return *m_screen; }
  55. Event const* current_event() const { return m_current_event; }
  56. void set_current_event(Event* event) { m_current_event = event; }
  57. NonnullRefPtr<CSS::CSSStyleDeclaration> get_computed_style(DOM::Element&) const;
  58. NonnullRefPtr<CSS::MediaQueryList> match_media(String);
  59. private:
  60. explicit Window(Document&);
  61. // https://html.spec.whatwg.org/multipage/window-object.html#concept-document-window
  62. WeakPtr<Document> m_associated_document;
  63. WeakPtr<Bindings::WindowObject> m_wrapper;
  64. IDAllocator m_timer_id_allocator;
  65. HashMap<int, NonnullRefPtr<Timer>> m_timers;
  66. NonnullOwnPtr<HighResolutionTime::Performance> m_performance;
  67. NonnullRefPtr<CSS::Screen> m_screen;
  68. RefPtr<Event> m_current_event;
  69. };
  70. }