Window.h 3.5 KB

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