Frame.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. /*
  2. * Copyright (c) 2018-2021, 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/Function.h>
  28. #include <AK/Noncopyable.h>
  29. #include <AK/RefPtr.h>
  30. #include <AK/WeakPtr.h>
  31. #include <LibCore/Timer.h>
  32. #include <LibGfx/Bitmap.h>
  33. #include <LibGfx/Rect.h>
  34. #include <LibGfx/Size.h>
  35. #include <LibWeb/DOM/Position.h>
  36. #include <LibWeb/Loader/FrameLoader.h>
  37. #include <LibWeb/Page/EventHandler.h>
  38. #include <LibWeb/TreeNode.h>
  39. namespace Web {
  40. class Frame : public TreeNode<Frame> {
  41. public:
  42. static NonnullRefPtr<Frame> create_subframe(DOM::Element& host_element, Frame& main_frame) { return adopt(*new Frame(host_element, main_frame)); }
  43. static NonnullRefPtr<Frame> create(Page& page) { return adopt(*new Frame(page)); }
  44. ~Frame();
  45. class ViewportClient {
  46. public:
  47. virtual ~ViewportClient() { }
  48. virtual void frame_did_set_viewport_rect(const Gfx::IntRect&) = 0;
  49. };
  50. void register_viewport_client(ViewportClient&);
  51. void unregister_viewport_client(ViewportClient&);
  52. bool is_main_frame() const { return this == &m_main_frame; }
  53. bool is_focused_frame() const;
  54. const DOM::Document* document() const { return m_document; }
  55. DOM::Document* document() { return m_document; }
  56. void set_document(DOM::Document*);
  57. Page* page() { return m_page; }
  58. const Page* page() const { return m_page; }
  59. const Gfx::IntSize& size() const { return m_size; }
  60. void set_size(const Gfx::IntSize&);
  61. void set_needs_display(const Gfx::IntRect&);
  62. void set_viewport_scroll_offset(const Gfx::IntPoint&);
  63. Gfx::IntRect viewport_rect() const { return { m_viewport_scroll_offset, m_size }; }
  64. void did_scroll(Badge<InProcessWebView>);
  65. FrameLoader& loader() { return m_loader; }
  66. const FrameLoader& loader() const { return m_loader; }
  67. EventHandler& event_handler() { return m_event_handler; }
  68. const EventHandler& event_handler() const { return m_event_handler; }
  69. void scroll_to(const Gfx::IntPoint&);
  70. void scroll_to_anchor(const String&);
  71. Frame& main_frame() { return m_main_frame; }
  72. const Frame& main_frame() const { return m_main_frame; }
  73. DOM::Element* host_element() { return m_host_element; }
  74. const DOM::Element* host_element() const { return m_host_element; }
  75. Gfx::IntPoint to_main_frame_position(const Gfx::IntPoint&);
  76. Gfx::IntRect to_main_frame_rect(const Gfx::IntRect&);
  77. const DOM::Position& cursor_position() const { return m_cursor_position; }
  78. void set_cursor_position(DOM::Position);
  79. bool cursor_blink_state() const { return m_cursor_blink_state; }
  80. String selected_text() const;
  81. void did_edit(Badge<EditEventHandler>);
  82. private:
  83. explicit Frame(DOM::Element& host_element, Frame& main_frame);
  84. explicit Frame(Page&);
  85. void reset_cursor_blink_cycle();
  86. void setup();
  87. WeakPtr<Page> m_page;
  88. Frame& m_main_frame;
  89. FrameLoader m_loader;
  90. EventHandler m_event_handler;
  91. WeakPtr<DOM::Element> m_host_element;
  92. RefPtr<DOM::Document> m_document;
  93. Gfx::IntSize m_size;
  94. Gfx::IntPoint m_viewport_scroll_offset;
  95. DOM::Position m_cursor_position;
  96. RefPtr<Core::Timer> m_cursor_blink_timer;
  97. bool m_cursor_blink_state { false };
  98. HashTable<ViewportClient*> m_viewport_clients;
  99. };
  100. }