Window.h 2.8 KB

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